
/* Finds the rengression of y onto x, that is
   y == alpha + beta * x
 
   Example usage:
   x := 1 .. 5;
   y := (2.34*x+2) +(2*Random()-1); 
   ans :=Regress(x,y);
    
   To find the residuals, we do
   (y-alpha-beta*x) /: ans
 */


Regress(x,y) :=
[
 Local(xy,x2,i,mx,my);

 mx := Mean(x);
 my := Mean(y);
 xy := Add((x-mx)*(y-my));
 x2 := Add((x-mx)^2);  
 {alpha <- (my-xy*mx/x2) , beta <-  xy/x2}; 
];

