Database Reference
In-Depth Information
of B is usually called a , and the second value called b and the equation for
reduced to the following simple form:
public class SimpleLinearModel {
public double a,b;
public SimpleLinearModel( double a, double b) {
this .a = a;
this .b = b;
}
public double y( double x) {
return a + b*x;
}
}
The error to minimize is similarly simplified:
public double error(double[] y,double [] x) {
double error = 0.0;
for(int i=0;i<y.length;i++) error +=
(y[i]-a-b*x[i])*(y[i]-a-b*x[i]);
return error;
}
After a little bit of calculus, it is found that this function is minimized when
b isequaltothecovarianceof x and y dividedbythevarianceof x .Thevalue
of a best minimizes the error when it is equal to the mean of y minus the
estimated value of b multiplied by the mean of x . If all the data are present
in an array, the following function will find the appropriate values of a and
b :
public void fit(double[] y,double[] x) {
double sumX = 0.0, sumY = 0.0;
double sumXY = 0.0, sumX2 = 0.0;
for(int i=0;i<y.length;i++) {
sumX += x[i];
sumY += y[i];
sumXY += x[i]*y[i];
sumX2 += x[i]*x[i];
}
Search WWH ::




Custom Search