Example: Linear Regression

The coefficients of a simple linear regression model, without an intercept, are computed.

import com.imsl.stat.*;

public class LinearRegressionEx1 {

    public static void main(String args[]) {
        // y = 4*x0 + 3*x1
        LinearRegression r = new LinearRegression(2, false);
        double c[] = {4, 3};
        double x[][] = {{1, 5}, {0, 2}, {-1, 4}};

        r.update(x[0], 1 * c[0] + 5 * c[1]);
        r.update(x[1], 0 * c[0] + 2 * c[1]);
        r.update(x[2], -1 * c[0] + 4 * c[1]);
        double coef[] = r.getCoefficients();
        System.out.println("The computed regression coefficients are {"
                + coef[0] + ", " + coef[1] + "}");
    }
}

Output

The computed regression coefficients are {4.0, 3.0}
Link to Java source.