Search in sources :

Example 26 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class EigenDecompositionTest method testNonSquareMatrix.

/**
 */
@Test
public void testNonSquareMatrix() {
    EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 0.0d, 0.0d }, { 0.0d, 1.0d, 0.0d }, { 0.0d, 0.0d, 2.0d }, { 1.0d, 1.0d, 0.0d } }));
    // TODO: IGNITE-5828, find out why decomposition of 3X4 matrix throws row index exception
    Matrix d = decomposition.getD();
    Matrix v = decomposition.getV();
    assertNotNull("Matrix d is expected to be not null.", d);
    assertNotNull("Matrix v is expected to be not null.", v);
    assertEquals("Unexpected rows in d matrix.", 4, d.rowSize());
    assertEquals("Unexpected cols in d matrix.", 4, d.columnSize());
    assertEquals("Unexpected rows in v matrix.", 4, v.rowSize());
    assertEquals("Unexpected cols in v matrix.", 3, v.columnSize());
    assertIsDiagonal(d, true);
    decomposition.destroy();
}
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 27 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class EigenDecompositionTest method testSymmetricMatrix.

/**
 */
@Test
public void testSymmetricMatrix() {
    EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 0.0d, 0.0d, 1.0d }, { 0.0d, 1.0d, 0.0d, 1.0d }, { 0.0d, 0.0d, 2.0d, 0.0d }, { 1.0d, 1.0d, 0.0d, 2.0d } }));
    Matrix d = decomposition.getD();
    Matrix v = decomposition.getV();
    assertNotNull("Matrix d is expected to be not null.", d);
    assertNotNull("Matrix v is expected to be not null.", v);
    assertEquals("Unexpected rows in d matrix.", 4, d.rowSize());
    assertEquals("Unexpected cols in d matrix.", 4, d.columnSize());
    assertEquals("Unexpected rows in v matrix.", 4, v.rowSize());
    assertEquals("Unexpected cols in v matrix.", 4, v.columnSize());
    assertIsDiagonalNonZero(d);
    decomposition.destroy();
}
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 28 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class EigenDecompositionTest method test.

/**
 */
private void test(double[][] mRaw, double[] expRealEigenValues) {
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(mRaw);
    EigenDecomposition decomposition = new EigenDecomposition(m);
    Matrix d = decomposition.getD();
    Matrix v = decomposition.getV();
    assertIsDiagonalNonZero(d);
    // check that d's diagonal consists of eigenvalues of m.
    assertDiagonalConsistsOfEigenvalues(m, d, v);
    // m = v d v^{-1} is equivalent to
    // m v = v d
    assertMatricesAreEqual(m.times(v), v.times(d));
    assertEigenvalues(decomposition, expRealEigenValues);
    decomposition.destroy();
}
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)

Example 29 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class QRDSolverTest method basicTest.

/**
 */
@Test
public void basicTest() {
    Matrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
    QRDecomposition dec = new QRDecomposition(m);
    assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank());
    Matrix q = dec.getQ();
    Matrix r = dec.getR();
    assertNotNull("Matrix q is expected to be not null.", q);
    assertNotNull("Matrix r is expected to be not null.", r);
    Matrix qSafeCp = safeCopy(q);
    Matrix expIdentity = qSafeCp.times(qSafeCp.transpose());
    final double delta = 0.0001;
    for (int row = 0; row < expIdentity.rowSize(); row++) for (int col = 0; col < expIdentity.columnSize(); col++) assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", row == col ? 1d : 0d, expIdentity.get(col, row), delta);
    for (int row = 0; row < r.rowSize(); row++) for (int col = 0; col < row - 1; col++) assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", 0d, r.get(row, col), delta);
    Matrix recomposed = qSafeCp.times(r);
    for (int row = 0; row < m.rowSize(); row++) for (int col = 0; col < m.columnSize(); col++) assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", m.get(row, col), recomposed.get(row, col), delta);
    Matrix sol = new QRDSolver(q, r).solve(new DenseLocalOnHeapMatrix(3, 10));
    assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize());
    assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize());
    for (int row = 0; row < sol.rowSize(); row++) for (int col = 0; col < sol.columnSize(); col++) assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", 0d, sol.get(row, col), delta);
    dec.destroy();
}
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 30 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testLongly.

/**
     * Test Longley dataset against certified values provided by NIST.
     * Data Source: J. Longley (1967) "An Appraisal of Least Squares
     * Programs for the Electronic Computer from the Point of View of the User"
     * Journal of the American Statistical Association, vol. 62. September,
     * pp. 819-841.
     *
     * Certified values (and data) are from NIST:
     * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat
     */
@Test
public void testLongly() {
    // Y values are first, then independent vars
    // Each row is one observation
    double[] design = new double[] { 60323, 83.0, 234289, 2356, 1590, 107608, 1947, 61122, 88.5, 259426, 2325, 1456, 108632, 1948, 60171, 88.2, 258054, 3682, 1616, 109773, 1949, 61187, 89.5, 284599, 3351, 1650, 110929, 1950, 63221, 96.2, 328975, 2099, 3099, 112075, 1951, 63639, 98.1, 346999, 1932, 3594, 113270, 1952, 64989, 99.0, 365385, 1870, 3547, 115094, 1953, 63761, 100.0, 363112, 3578, 3350, 116219, 1954, 66019, 101.2, 397469, 2904, 3048, 117388, 1955, 67857, 104.6, 419180, 2822, 2857, 118734, 1956, 68169, 108.4, 442769, 2936, 2798, 120445, 1957, 66513, 110.8, 444546, 4681, 2637, 121950, 1958, 68655, 112.6, 482704, 3813, 2552, 123366, 1959, 69564, 114.2, 502601, 3931, 2514, 125368, 1960, 69331, 115.7, 518173, 4806, 2572, 127852, 1961, 70551, 116.9, 554894, 4007, 2827, 130081, 1962 };
    final int nobs = 16;
    final int nvars = 6;
    // Estimate the model
    OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
    mdl.newSampleData(design, nobs, nvars, new DenseLocalOnHeapMatrix());
    // Check expected beta values from NIST
    double[] betaHat = mdl.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat, new double[] { -3482258.63459582, 15.0618722713733, -0.358191792925910E-01, -2.02022980381683, -1.03322686717359, -0.511041056535807E-01, 1829.15146461355 }, //
    2E-6);
    // Check expected residuals from R
    double[] residuals = mdl.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[] { 267.340029759711, -94.0139423988359, 46.28716775752924, -410.114621930906, 309.7145907602313, -249.3112153297231, -164.0489563956039, -13.18035686637081, 14.30477260005235, 455.394094551857, -17.26892711483297, -39.0550425226967, -155.5499735953195, -85.6713080421283, 341.9315139607727, -206.7578251937366 }, 1E-7);
    // Check standard errors from NIST
    double[] errors = mdl.estimateRegressionParametersStandardErrors();
    TestUtils.assertEquals(new double[] { 890420.383607373, 84.9149257747669, 0.334910077722432E-01, 0.488399681651699, 0.214274163161675, 0.226073200069370, 455.478499142212 }, errors, 1E-6);
    // Check regression standard error against R
    Assert.assertEquals(304.8540735619638, mdl.estimateRegressionStandardError(), 1E-10);
    // Check R-Square statistics against R
    Assert.assertEquals(0.995479004577296, mdl.calculateRSquared(), 1E-12);
    Assert.assertEquals(0.992465007628826, mdl.calculateAdjustedRSquared(), 1E-12);
    // TODO: uncomment
    // checkVarianceConsistency(model);
    // Estimate model without intercept
    mdl.setNoIntercept(true);
    mdl.newSampleData(design, nobs, nvars, new DenseLocalOnHeapMatrix());
    // Check expected beta values from R
    betaHat = mdl.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat, new double[] { -52.99357013868291, 0.07107319907358, -0.42346585566399, -0.57256866841929, -0.41420358884978, 48.41786562001326 }, 1E-8);
    // Check standard errors from R
    errors = mdl.estimateRegressionParametersStandardErrors();
    TestUtils.assertEquals(new double[] { 129.54486693117232, 0.03016640003786, 0.41773654056612, 0.27899087467676, 0.32128496193363, 17.68948737819961 }, errors, 1E-11);
    // Check expected residuals from R
    residuals = mdl.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[] { 279.90274927293092, -130.32465380836874, 90.73228661967445, -401.31252201634948, -440.46768772620027, -543.54512853774793, 201.32111639536299, 215.90889365977932, 73.09368242049943, 913.21694494481869, 424.82484953610174, -8.56475876776709, -361.32974610842876, 27.34560497213464, 151.28955976355002, -492.49937355336846 }, 1E-8);
    // Check regression standard error against R
    Assert.assertEquals(475.1655079819517, mdl.estimateRegressionStandardError(), 1E-10);
    // Check R-Square statistics against R
    Assert.assertEquals(0.9999670130706, mdl.calculateRSquared(), 1E-12);
    Assert.assertEquals(0.999947220913, mdl.calculateAdjustedRSquared(), 1E-12);
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Aggregations

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2