Package com.opengamma.analytics.math.matrix

Examples of com.opengamma.analytics.math.matrix.MatrixAlgebra


  private static boolean compare(final String msg, final SimpleParameterSensitivity sensitivity1, final SimpleParameterSensitivity sensitivity2, final double tolerance, final boolean opposite) {
    ArgumentChecker.notNull(sensitivity1, "sensitivity1");
    ArgumentChecker.notNull(sensitivity2, "sensitivity2");
    ArgumentChecker.isTrue(tolerance > 0, "tolerance must be greater than 0; have {}", tolerance);
    boolean cmp = true;
    final MatrixAlgebra algebra = MatrixAlgebraFactory.COMMONS_ALGEBRA;
    final Map<String, DoubleMatrix1D> map1 = sensitivity1.getSensitivities();
    final Map<String, DoubleMatrix1D> map2 = sensitivity2.getSensitivities();
    for (final Map.Entry<String, DoubleMatrix1D> entry : map1.entrySet()) {
      final String name = entry.getKey();
      if (map2.get(name) == null) {
        if (algebra.getNormInfinity(entry.getValue()) > tolerance) {
          cmp = false;
        }
      } else {
        if (algebra.getNormInfinity(algebra.add(entry.getValue(), algebra.scale(map2.get(name), -1.0))) > tolerance) {
          cmp = false;
        }
      }
    }
    if (opposite) {
View Full Code Here


    getSVD().evaluate((DoubleMatrix2D) null);
  }

  @Test
  public void testRecoverOrginal() {
    final MatrixAlgebra algebra = getAlgebra();
    final DecompositionResult result = getSVD().evaluate(A);
    assertTrue(result instanceof SVDecompositionResult);
    final SVDecompositionResult svd_result = (SVDecompositionResult) result;
    final DoubleMatrix2D u = svd_result.getU();
    final double[] sv = svd_result.getSingularValues();
    final DoubleMatrix2D w = DoubleMatrixUtils.getTwoDimensionalDiagonalMatrix(sv);
    final DoubleMatrix2D vt = svd_result.getVT();
    final DoubleMatrix2D a = (DoubleMatrix2D) algebra.multiply(algebra.multiply(u, w), vt);
    checkEquals(A, a);
  }
View Full Code Here

    checkEquals(A, a);
  }

  @Test
  public void testInvert() {
    final MatrixAlgebra algebra = getAlgebra();
    final SVDecompositionResult result = getSVD().evaluate(A);
    final DoubleMatrix2D ut = result.getUT();
    final DoubleMatrix2D v = result.getV();
    final double[] sv = result.getSingularValues();
    final int n = sv.length;
    final double[] svinv = new double[n];
    for (int i = 0; i < n; i++) {
      if (sv[i] == 0.0) {
        svinv[i] = 0.0;
      } else {
        svinv[i] = 1.0 / sv[i];
      }
    }
    final DoubleMatrix2D winv = DoubleMatrixUtils.getTwoDimensionalDiagonalMatrix(svinv);
    final DoubleMatrix2D ainv = (DoubleMatrix2D) algebra.multiply(algebra.multiply(v, winv), ut);
    final DoubleMatrix2D identity = (DoubleMatrix2D) algebra.multiply(A, ainv);
    checkIdentity(identity);

  }
View Full Code Here

    assertEquals(0.0, result.getFitParameters().getEntry(3), 1e-8);
  }

  @Test
  public void solveRandomNoiseTest() {
    final MatrixAlgebra ma = new OGMatrixAlgebra();
    final double[] y = new double[20];
    for (int i = 0; i < 20; i++) {
      y[i] = Y.getEntry(i) + SIGMA.getEntry(i) * NORMAL.nextRandom();
    }
    final DoubleMatrix1D start = new DoubleMatrix1D(new double[] {0.7, 1.4, 0.2, -0.3 });
    final NonLinearLeastSquare ls = new NonLinearLeastSquare();
    final LeastSquareResults res = ls.solve(X, new DoubleMatrix1D(y), SIGMA, PARAM_FUNCTION, PARAM_GRAD, start);

    final double chiSqDoF = res.getChiSq() / 16;
    assertTrue(chiSqDoF > 0.25);
    assertTrue(chiSqDoF < 3.0);

    final DoubleMatrix1D trueValues = new DoubleMatrix1D(new double[] {1, 1, 0, 0 });
    final DoubleMatrix1D delta = (DoubleMatrix1D) ma.subtract(res.getFitParameters(), trueValues);

    final LUDecompositionCommons decmp = new LUDecompositionCommons();
    final LUDecompositionResult decmpRes = decmp.evaluate(res.getCovariance());
    final DoubleMatrix2D invCovariance = decmpRes.solve(DoubleMatrixUtils.getIdentityMatrix2D(4));

    double z = ma.getInnerProduct(delta, ma.multiply(invCovariance, delta));
    z = Math.sqrt(z);

    assertTrue(z < 3.0);

    //     System.out.println("chiSqr: " + res.getChiSq());
View Full Code Here

    //     System.out.println("z: " + z);
  }

  @Test
  public void smallPertubationTest() {
    final MatrixAlgebra ma = new OGMatrixAlgebra();
    final double[] dy = new double[20];
    for (int i = 0; i < 20; i++) {
      dy[i] = 0.1 * SIGMA.getEntry(i) * NORMAL.nextRandom();
    }
    final DoubleMatrix1D deltaY = new DoubleMatrix1D(dy);
    final DoubleMatrix1D solution = new DoubleMatrix1D(new double[] {1.0, 1.0, 0.0, 0.0 });
    final NonLinearLeastSquare ls = new NonLinearLeastSquare();
    final DoubleMatrix2D res = ls.calInverseJacobian(SIGMA, FUNCTION, GRAD, solution);
    //  System.out.println("invese Jac: " + res);

    final DoubleMatrix1D deltaParms = (DoubleMatrix1D) ma.multiply(res, deltaY);
    // System.out.println("delta parms: " + deltaParms);

    final DoubleMatrix1D y = (DoubleMatrix1D) ma.add(Y, deltaY);

    final LeastSquareResults lsRes = ls.solve(X, y, SIGMA, PARAM_FUNCTION, PARAM_GRAD, solution);
    final DoubleMatrix1D trueDeltaParms = (DoubleMatrix1D) ma.subtract(lsRes.getFitParameters(), solution);
    // System.out.println("true delta parms: " + trueDeltaParms);

    assertEquals(trueDeltaParms.getEntry(0), deltaParms.getEntry(0), 5e-5);
    assertEquals(trueDeltaParms.getEntry(1), deltaParms.getEntry(1), 5e-5);
    assertEquals(trueDeltaParms.getEntry(2), deltaParms.getEntry(2), 5e-5);
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.matrix.MatrixAlgebra

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.