Java Code Examples for org.apache.commons.math.linear.RealMatrix#walkInOptimizedOrder()

The following examples show how to use org.apache.commons.math.linear.RealMatrix#walkInOptimizedOrder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QRSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testOverdetermined() {
    final Random r    = new Random(5559252868205245l);
    int          p    = (7 * BlockRealMatrix.BLOCK_SIZE) / 4;
    int          q    = (5 * BlockRealMatrix.BLOCK_SIZE) / 4;
    RealMatrix   a    = createTestMatrix(r, p, q);
    RealMatrix   xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3);

    // build a perturbed system: A.X + noise = B
    RealMatrix b = a.multiply(xRef);
    final double noise = 0.001;
    b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return value * (1.0 + noise * (2 * r.nextDouble() - 1));
        }
    });

    // despite perturbation, the least square solution should be pretty good
    RealMatrix x = new QRDecompositionImpl(a).getSolver().solve(b);
    assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q);

}
 
Example 2
Source File: QRSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testOverdetermined() {
    final Random r    = new Random(5559252868205245l);
    int          p    = (7 * BlockRealMatrix.BLOCK_SIZE) / 4;
    int          q    = (5 * BlockRealMatrix.BLOCK_SIZE) / 4;
    RealMatrix   a    = createTestMatrix(r, p, q);
    RealMatrix   xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3);

    // build a perturbed system: A.X + noise = B
    RealMatrix b = a.multiply(xRef);
    final double noise = 0.001;
    b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return value * (1.0 + noise * (2 * r.nextDouble() - 1));
        }
    });

    // despite perturbation, the least square solution should be pretty good
    RealMatrix x = new QRDecompositionImpl(a).getSolver().solve(b);
    assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q);

}
 
Example 3
Source File: QRSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testOverdetermined() {
    final Random r    = new Random(5559252868205245l);
    int          p    = (7 * BlockRealMatrix.BLOCK_SIZE) / 4;
    int          q    = (5 * BlockRealMatrix.BLOCK_SIZE) / 4;
    RealMatrix   a    = createTestMatrix(r, p, q);
    RealMatrix   xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3);

    // build a perturbed system: A.X + noise = B
    RealMatrix b = a.multiply(xRef);
    final double noise = 0.001;
    b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return value * (1.0 + noise * (2 * r.nextDouble() - 1));
        }
    });

    // despite perturbation, the least square solution should be pretty good
    RealMatrix x = new QRDecompositionImpl(a).getSolver().solve(b);
    assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q);

}
 
Example 4
Source File: QRDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
    RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
    m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            return 2.0 * r.nextDouble() - 1.0;
        }
    });
    return m;
}
 
Example 5
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 6
Source File: QRDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
    RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
    m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            return 2.0 * r.nextDouble() - 1.0;
        }
    });
    return m;
}
 
Example 7
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat, 
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 8
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat, 
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 9
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 10
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 11
Source File: QRSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
    RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
    m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            return 2.0 * r.nextDouble() - 1.0;
        }
    });
    return m;
}
 
Example 12
Source File: QRSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
    RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
    m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            return 2.0 * r.nextDouble() - 1.0;
        }
    });
    return m;
}
 
Example 13
Source File: QRSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
    RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
    m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            return 2.0 * r.nextDouble() - 1.0;
        }
    });
    return m;
}
 
Example 14
Source File: QRDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkTrapezoidal(RealMatrix m) {
    m.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
        @Override
        public void visit(int row, int column, double value) {
            if (column > row) {
                assertEquals(0.0, value, entryTolerance);
            }
        }
    });
}
 
Example 15
Source File: QRDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkTrapezoidal(RealMatrix m) {
    m.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
        @Override
        public void visit(int row, int column, double value) {
            if (column > row) {
                assertEquals(0.0, value, entryTolerance);
            }
        }
    });
}
 
Example 16
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 17
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
}
 
Example 18
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() throws Exception {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   Assert.assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
   Assert.assertEquals(1, ((OLSMultipleLinearRegression) regression).calculateRSquared(), 1E-12);
}
 
Example 19
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() throws Exception {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value)
            throws MatrixVisitorException {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
   assertEquals(1, ((OLSMultipleLinearRegression) regression).calculateRSquared(), 1E-12);
}
 
Example 20
Source File: OLSMultipleLinearRegressionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPerfectFit() throws Exception {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat,
                           new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 },
                           1e-14);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d},
                           1e-14);
    RealMatrix errors =
        new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false);
    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 };
    RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length);
    referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row == 0) {
                return s[column];
            }
            double x = s[row] * s[column];
            return (row == column) ? 2 * x : x;
        }
    });
   assertEquals(0.0,
                 errors.subtract(referenceVariance).getNorm(),
                 5.0e-16 * referenceVariance.getNorm());
   assertEquals(1, ((OLSMultipleLinearRegression) regression).calculateRSquared(), 1E-12);
}