Search in sources :

Example 76 with Matrix

use of org.apache.ignite.ml.math.Matrix in project ignite by apache.

the class AbstractMultipleLinearRegressionTest method testNewSample.

/**
     * Verifies that newSampleData methods consistently insert unitary columns
     * in design matrix.  Confirms the fix for MATH-411.
     */
@Test
public void testNewSample() {
    double[] design = new double[] { 1, 19, 22, 33, 2, 20, 30, 40, 3, 25, 35, 45, 4, 27, 37, 47 };
    double[] y = new double[] { 1, 2, 3, 4 };
    double[][] x = new double[][] { { 19, 22, 33 }, { 20, 30, 40 }, { 25, 35, 45 }, { 27, 37, 47 } };
    AbstractMultipleLinearRegression regression = createRegression();
    regression.newSampleData(design, 4, 3, new DenseLocalOnHeapMatrix());
    Matrix flatX = regression.getX().copy();
    Vector flatY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(flatX, regression.getX());
    Assert.assertEquals(flatY, regression.getY());
    // No intercept
    regression.setNoIntercept(true);
    regression.newSampleData(design, 4, 3, new DenseLocalOnHeapMatrix());
    flatX = regression.getX().copy();
    flatY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(flatX, regression.getX());
    Assert.assertEquals(flatY, regression.getY());
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 77 with Matrix

use of org.apache.ignite.ml.math.Matrix 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);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 78 with Matrix

use of org.apache.ignite.ml.math.Matrix 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());
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Aggregations

Matrix (org.apache.ignite.ml.math.Matrix)78 Test (org.junit.Test)38 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)25 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)17 Vector (org.apache.ignite.ml.math.Vector)7 DenseLocalOffHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix)6 SparseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)6 RandomMatrix (org.apache.ignite.ml.math.impls.matrix.RandomMatrix)5 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)4 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)3 UnsupportedOperationException (org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)3 LUDecomposition (org.apache.ignite.ml.math.decompositions.LUDecomposition)2 PivotedMatrixView (org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView)2 CholeskyDecomposition (org.apache.ignite.ml.math.decompositions.CholeskyDecomposition)1 ColumnIndexException (org.apache.ignite.ml.math.exceptions.ColumnIndexException)1 IndexException (org.apache.ignite.ml.math.exceptions.IndexException)1 RowIndexException (org.apache.ignite.ml.math.exceptions.RowIndexException)1 AbstractMatrix (org.apache.ignite.ml.math.impls.matrix.AbstractMatrix)1 MatrixView (org.apache.ignite.ml.math.impls.matrix.MatrixView)1 MatrixDelegateStorage (org.apache.ignite.ml.math.impls.storage.matrix.MatrixDelegateStorage)1