/* linest.java
 * Linear Regression
 */

package com.finance;
import java.lang.*;

public class linest {
   double fSlope, fIntercept;

public linest(double[] x, double[] y){
   int count=0;
   double fX=0, fY=0, fX2=0, fXY=0;

   // least squares method
   count = StrictMath.min(x.length, y.length);
   for (int index=0; index<count; index++) {
      fX += x[index];
      fY += y[index];
      fX2 += StrictMath.pow(x[index],2);
      fXY += x[index] * y[index];
   }

   // System.out.println(fY +" "+fX+" "+fX2+" "+fXY);
   fSlope = (count*fXY-fX*fY)/(count*fX2-StrictMath.pow(fX,2));
   fIntercept = (fY-fSlope*fX)/count;
}
public double Slope() {
   return(fSlope);
}
public double Intercept() {
   return(fIntercept);
}
}
