Database Reference
In-Depth Information
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.2</version>
</dependency>
Using the Apache Commons Math library's QR factorization classes, it is
easy to simply write down the final equation in LinearModel 's fit
implementation:
public void fit(double[] y,double[][] x) {
RealMatrix X = new Array2DRowRealMatrix(x);
RealVector Y = new ArrayRealVector(y);
B = (new
QRDecomposition(X)).getSolver().solve(Y).toArray();
}
Of course, the Apache Commons Math library also includes a class that
implements ordinary least squares, so it is possible to simply use that
instead:
public void fitOLS( double [] y, double [][] x) {
OLSMultipleLinearRegression ols = new
OLSMultipleLinearRegression();
ols.newSampleData(y, x);
B = ols.estimateRegressionParameters();
}
For use in streaming data environments, Apache Commons also includes
an “online” version of the ordinary least squares solver. Called the Miller
Updating Regression after its inventor, it relies on the fact that the QR
factorization can be updated with new data. This allows observations to be
streamed into the model with the ability to produce regression coefficients
B after a sufficient number of observations (at least twice the number of
coefficients in the model) have been made. To use it, simply initialize the
model with the number of coefficients in the model:
Search WWH ::




Custom Search