use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class OLSMultipleLinearRegressionTest method testPerfectFit.
/** */
@Test
public void testPerfectFit() {
double[] betaHat = regression.estimateRegressionParameters();
TestUtils.assertEquals(new double[] { 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 }, betaHat, 1e-13);
double[] residuals = regression.estimateResiduals();
TestUtils.assertEquals(new double[] { 0d, 0d, 0d, 0d, 0d, 0d }, residuals, 1e-13);
Matrix errors = regression.estimateRegressionParametersVariance();
final double[] s = { 1.0, -1.0 / 2.0, -1.0 / 3.0, -1.0 / 4.0, -1.0 / 5.0, -1.0 / 6.0 };
Matrix refVar = new DenseLocalOnHeapMatrix(s.length, s.length);
for (int i = 0; i < refVar.rowSize(); i++) for (int j = 0; j < refVar.columnSize(); j++) {
if (i == 0) {
refVar.setX(i, j, s[j]);
continue;
}
double x = s[i] * s[j];
refVar.setX(i, j, (i == j) ? 2 * x : x);
}
Assert.assertEquals(0.0, TestUtils.maximumAbsoluteRowSum(errors.minus(refVar)), 5.0e-16 * TestUtils.maximumAbsoluteRowSum(refVar));
Assert.assertEquals(1, ((OLSMultipleLinearRegression) regression).calculateRSquared(), 1E-12);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class OLSMultipleLinearRegressionTest method testMathIllegalArgumentException.
/** */
@Test(expected = MathIllegalArgumentException.class)
public void testMathIllegalArgumentException() {
OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
mdl.validateSampleData(new DenseLocalOnHeapMatrix(1, 2), new DenseLocalOnHeapVector(1));
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class OLSMultipleLinearRegressionTest method testSingularCalculateBeta.
/**
* Anything requiring beta calculation should advertise SME.
*/
@Test(expected = SingularMatrixException.class)
public void testSingularCalculateBeta() {
OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression(1e-15);
mdl.newSampleData(new double[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 }, 3, 2, new DenseLocalOnHeapMatrix());
mdl.calculateBeta();
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class OLSMultipleLinearRegressionTest method testNewSample2.
/**
* Verifies that setting X and Y separately has the same effect as newSample(X,Y).
*/
@Test
public void testNewSample2() {
double[] y = new double[] { 1, 2, 3, 4 };
double[][] x = new double[][] { { 19, 22, 33 }, { 20, 30, 40 }, { 25, 35, 45 }, { 27, 37, 47 } };
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
Matrix combinedX = regression.getX().copy();
Vector combinedY = regression.getY().copy();
regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
regression.newYSampleData(new DenseLocalOnHeapVector(y));
Assert.assertEquals(combinedX, regression.getX());
Assert.assertEquals(combinedY, regression.getY());
// No intercept
regression.setNoIntercept(true);
regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
combinedX = regression.getX().copy();
combinedY = regression.getY().copy();
regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
regression.newYSampleData(new DenseLocalOnHeapVector(y));
Assert.assertEquals(combinedX, regression.getX());
Assert.assertEquals(combinedY, regression.getY());
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class OLSMultipleLinearRegressionTest method cannotAddSampleDataWithSizeMismatch.
/** */
@Test(expected = MathIllegalArgumentException.class)
public void cannotAddSampleDataWithSizeMismatch() {
double[] y = new double[] { 1.0, 2.0 };
double[][] x = new double[1][];
x[0] = new double[] { 1.0, 0 };
createRegression().newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
}
Aggregations