private boolean getNextPosition( final Function1D<DoubleMatrix1D, Double> function, final Function1D<DoubleMatrix1D, DoubleMatrix1D> grad, final DataBundle data) { final DoubleMatrix1D p = getDirection(data); if (data.getLambda0() < 1.0) { data.setLambda0(1.0); } else { data.setLambda0(data.getLambda0() * BETA); } updatePosition(p, function, data); final double g1 = data.getG1(); // the function is invalid at the new position, try to recover if (Double.isInfinite(g1) || Double.isNaN(g1)) { bisectBacktrack(p, function, data); } if (data.getG1() > data.getG0() / (1 + ALPHA * data.getLambda0())) { quadraticBacktrack(p, function, data); int count = 0; while (data.getG1() > data.getG0() / (1 + ALPHA * data.getLambda0())) { if (count > 5) { return false; } cubicBacktrack(p, function, data); count++; } } final DoubleMatrix1D deltaX = data.getDeltaX(); data.setX((DoubleMatrix1D) MA.add(data.getX(), deltaX)); data.setG0(data.getG1()); final DoubleMatrix1D gradNew = grad.evaluate(data.getX()); data.setDeltaGrad((DoubleMatrix1D) MA.subtract(gradNew, data.getGrad())); data.setGrad(gradNew); return true; }
protected void updatePosition( final DoubleMatrix1D p, final Function1D<DoubleMatrix1D, Double> function, final DataBundle data) { final double lambda0 = data.getLambda0(); final DoubleMatrix1D deltaX = (DoubleMatrix1D) MA.scale(p, lambda0); final DoubleMatrix1D xNew = (DoubleMatrix1D) MA.add(data.getX(), deltaX); data.setDeltaX(deltaX); data.setG2(data.getG1()); final double y = function.evaluate(xNew); data.setG1(y * y); }
/** Compare results with Common decomposition */ public void compareCommon() { final CholeskyDecompositionResult resultOG = CDOG.evaluate(A3); final CholeskyDecompositionResult resultC = CDC.evaluate(A3); checkEquals(resultC.getL(), resultOG.getL()); checkEquals(ALGEBRA.getTranspose(resultC.getL()), resultOG.getLT()); assertEquals("Determinant", resultC.getDeterminant(), resultOG.getDeterminant(), 1.0E-10); }
/** * Calculate the instrument sensitivity from the yield sensitivity, the jacobian matrix and the * coupon sensitivity. * * @param curveSensitivities The sensitivity to points of the yield curve. * @param curves The curve bundle. * @param couponSensitivity The sensitivity * @param jacobian The present value coupon sensitivity. * @return The instrument quote/rate sensitivity. */ public DoubleMatrix1D calculateFromPresentValue( final Map<String, List<DoublesPair>> curveSensitivities, final YieldCurveBundle curves, final DoubleMatrix1D couponSensitivity, final DoubleMatrix2D jacobian) { final DoubleArrayList resultList = new DoubleArrayList(); for (final String curveName : curves.getAllNames()) { final DoubleMatrix1D nodeSensitivity = new DoubleMatrix1D( (_parameterSensitivityCalculator.pointToParameterSensitivity( curveSensitivities.get(curveName), curves.getCurve(curveName))) .toArray(new Double[0])); final int n = nodeSensitivity.getNumberOfElements(); final DoubleMatrix2D inverseJacobian = MATRIX_ALGEBRA.getInverse(jacobian); for (int i = 0; i < n; i++) { double sum = 0; for (int j = 0; j < n; j++) { sum += -couponSensitivity.getEntry(i) * inverseJacobian.getEntry(j, i) * nodeSensitivity.getEntry(j); } resultList.add(sum); } } return new DoubleMatrix1D(resultList.toDoubleArray()); }
/** Tests solve Ax = b from A and b. */ public void solveVector() { final CholeskyDecompositionResult result = CDOG.evaluate(A5); double[] b = new double[] {1.0, 2.0, 3.0, 4.0, -1.0}; double[] x = result.solve(b); DoubleMatrix1D ax = (DoubleMatrix1D) ALGEBRA.multiply(A5, new DoubleMatrix1D(x)); ArrayAsserts.assertArrayEquals( "Cholesky decomposition OpenGamma - solve", b, ax.getData(), 1.0E-10); }
/** Tests solve AX = B from A and B. */ public void solveMatrix() { final CholeskyDecompositionResult result = CDOG.evaluate(A5); double[][] b = new double[][] {{1.0, 2.0}, {2.0, 3.0}, {3.0, 4.0}, {4.0, -2.0}, {-1.0, -1.0}}; DoubleMatrix2D x = result.solve(new DoubleMatrix2D(b)); DoubleMatrix2D ax = (DoubleMatrix2D) ALGEBRA.multiply(A5, x); ArrayAsserts.assertArrayEquals( "Cholesky decomposition OpenGamma - solve", b[0], ax.getData()[0], 1.0E-10); ArrayAsserts.assertArrayEquals( "Cholesky decomposition OpenGamma - solve", b[1], ax.getData()[1], 1.0E-10); }
private boolean isConverged(final DataBundle data) { final DoubleMatrix1D deltaX = data.getDeltaX(); final DoubleMatrix1D x = data.getX(); final int n = deltaX.getNumberOfElements(); double diff, scale; for (int i = 0; i < n; i++) { diff = Math.abs(deltaX.getEntry(i)); scale = Math.abs(x.getEntry(i)); if (diff > _absoluteTol + scale * _relativeTol) { return false; } } return (MA.getNorm2(data.getGrad()) < _absoluteTol); }
/** Tests A = L L^T. */ public void recoverOrginal() { final CholeskyDecompositionResult result = CDOG.evaluate(A3); final DoubleMatrix2D a = (DoubleMatrix2D) ALGEBRA.multiply(result.getL(), result.getLT()); checkEquals(A3, a); }
/** * Compute the market quote sensitivity from the parameter sensitivity and the inverse Jacobian * matrix. * * @param parameterSensitivity The parameter sensitivity. * @param inverseJacobian The inverse Jacobian matrix (derivative of the curve parameters with * respect to the market quotes). * @return The market quote sensitivity. */ public DoubleMatrix1D fromParameterSensitivityInverseJacobian( final DoubleMatrix1D parameterSensitivity, final DoubleMatrix2D inverseJacobian) { return (DoubleMatrix1D) MATRIX_ALGEBRA.multiply(parameterSensitivity, inverseJacobian); }
private DoubleMatrix1D getDirection(final DataBundle data) { return (DoubleMatrix1D) MA.multiply(data.getInverseHessianEsimate(), MA.scale(data.getGrad(), -1.0)); }