/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is available * @param observedValues Set of measurement values * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param jac The model sensitivity to its parameters (i.e. the Jacobian matrix) as a function of its parameters only * @param startPos Initial value of the parameters * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, Function<DoubleArray, DoubleMatrix> jac, DoubleArray startPos) { return solve(observedValues, sigma, func, jac, startPos, UNCONSTRAINED, null); }
/** * Creates an instance. * <p> * The epsilon is the parameter used in {@link NonLinearLeastSquare}, where the iteration stops when certain * quantities are smaller than this parameter. * * @param pricer the cap pricer * @param sabrPricer the SABR cap pricer * @param epsilon the epsilon parameter * @param referenceData the reference data * @return the instance */ public static SabrIborCapletFloorletVolatilityCalibrator of( VolatilityIborCapFloorLegPricer pricer, SabrIborCapFloorLegPricer sabrPricer, double epsilon, ReferenceData referenceData) { NonLinearLeastSquare solver = new NonLinearLeastSquare(SV_COMMONS, OG_ALGEBRA, epsilon); return new SabrIborCapletFloorletVolatilityCalibrator(pricer, sabrPricer, solver, referenceData); }
private LeastSquareResults finish( DoubleMatrix alpha, DecompositionResult decmp, double newChiSqr, DoubleMatrix jacobian, DoubleArray newTheta, DoubleArray sigma) { DoubleMatrix covariance = decmp.solve(DoubleMatrix.identity(alpha.rowCount())); DoubleMatrix bT = getBTranspose(jacobian, sigma); DoubleMatrix inverseJacobian = decmp.solve(bT); return new LeastSquareResults(newChiSqr, newTheta, covariance, inverseJacobian); }
/** * * the inverse-Jacobian where the i-j entry is the sensitivity of the ith (fitted) parameter (a_i) to the jth data * point (y_j). * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param jac The model sensitivity to its parameters (i.e. the Jacobian matrix) as a function of its parameters only * @param originalSolution The value of the parameters at a converged solution * @return inverse-Jacobian */ public DoubleMatrix calInverseJacobian( DoubleArray sigma, Function<DoubleArray, DoubleArray> func, Function<DoubleArray, DoubleMatrix> jac, DoubleArray originalSolution) { DoubleMatrix jacobian = getJacobian(jac, sigma, originalSolution); DoubleMatrix a = getModifiedCurvatureMatrix(jacobian, 0.0); DoubleMatrix bT = getBTranspose(jacobian, sigma); DecompositionResult decRes = _decomposition.apply(a); return decRes.solve(bT); }
DoubleArray error = getError(func, observedValues, sigma, theta); DoubleMatrix jacobian = getJacobian(jac, sigma, theta); oldChiSqr = getChiSqr(error); return finish(oldChiSqr, jacobian, theta, sigma); DoubleArray beta = getChiSqrGrad(error, jacobian); alpha = getModifiedCurvatureMatrix(jacobian, lambda); if (!constraints.apply(trialTheta) || !allowJump(deltaTheta, maxJumps)) { lambda = increaseLambda(lambda); continue; newError = getError(func, observedValues, sigma, trialTheta); newChiSqr = getChiSqr(newError); DoubleMatrix alpha0 = lambda == 0.0 ? alpha : getModifiedCurvatureMatrix(jacobian, 0.0); return finish(alpha0, decmp, newChiSqr, jacobian, trialTheta, sigma); lambda = increaseLambda(lambda); for (int i = 0; i < nParms; i++) { if (w[i] < 0.0) { newError = getError(func, observedValues, sigma, trialTheta); newChiSqr = getChiSqr(newError);
public void solveExactWithoutGradientTest() { final DoubleArray start = DoubleArray.of(1.2, 0.8, -0.2, -0.3); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final LeastSquareResults result = ls.solve(X, Y, SIGMA, PARAM_FUNCTION, start); assertEquals(0.0, result.getChiSq(), 1e-8); assertEquals(1.0, result.getFitParameters().get(0), 1e-8); assertEquals(1.0, result.getFitParameters().get(1), 1e-8); assertEquals(0.0, result.getFitParameters().get(2), 1e-8); assertEquals(0.0, result.getFitParameters().get(3), 1e-8); }
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.get(i) * NORMAL.nextRandom(); } final DoubleArray deltaY = DoubleArray.copyOf(dy); final DoubleArray solution = DoubleArray.of(1.0, 1.0, 0.0, 0.0); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final DoubleMatrix res = ls.calInverseJacobian(SIGMA, FUNCTION, GRAD, solution); final DoubleArray deltaParms = (DoubleArray) ma.multiply(res, deltaY); final DoubleArray y = (DoubleArray) ma.add(Y, deltaY); final LeastSquareResults lsRes = ls.solve(X, y, SIGMA, PARAM_FUNCTION, PARAM_GRAD, solution); final DoubleArray trueDeltaParms = (DoubleArray) ma.subtract(lsRes.getFitParameters(), solution); assertEquals(trueDeltaParms.get(0), deltaParms.get(0), 5e-5); assertEquals(trueDeltaParms.get(1), deltaParms.get(1), 5e-5); assertEquals(trueDeltaParms.get(2), deltaParms.get(2), 5e-5); assertEquals(trueDeltaParms.get(3), deltaParms.get(3), 5e-5); }
public void solveRandomNoiseTest() { final MatrixAlgebra ma = new OGMatrixAlgebra(); final double[] y = new double[20]; for (int i = 0; i < 20; i++) { y[i] = Y.get(i) + SIGMA.get(i) * NORMAL.nextRandom(); } final DoubleArray start = DoubleArray.of(0.7, 1.4, 0.2, -0.3); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final LeastSquareResults res = ls.solve(X, DoubleArray.copyOf(y), SIGMA, PARAM_FUNCTION, PARAM_GRAD, start); final double chiSqDoF = res.getChiSq() / 16; assertTrue(chiSqDoF > 0.25); assertTrue(chiSqDoF < 3.0); final DoubleArray trueValues = DoubleArray.of(1, 1, 0, 0); final DoubleArray delta = (DoubleArray) ma.subtract(res.getFitParameters(), trueValues); final LUDecompositionCommons decmp = new LUDecompositionCommons(); final LUDecompositionResult decmpRes = decmp.apply(res.getCovariance()); final DoubleMatrix invCovariance = decmpRes.solve(DoubleMatrix.identity(4)); double z = ma.getInnerProduct(delta, ma.multiply(invCovariance, delta)); z = Math.sqrt(z); assertTrue(z < 3.0); }
/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is available * @param observedValues Set of measurement values * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param jac The model sensitivity to its parameters (i.e. the Jacobian matrix) as a function of its parameters only * @param startPos Initial value of the parameters * @param maxJumps A vector containing the maximum absolute allowed step in a particular direction in each iteration. * Can be null, in which case on constant * on the step size is applied. * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, Function<DoubleArray, DoubleMatrix> jac, DoubleArray startPos, DoubleArray maxJumps) { return solve(observedValues, sigma, func, jac, startPos, UNCONSTRAINED, maxJumps); }
/** * Creates an instance. * <p> * The epsilon is the parameter used in {@link NonLinearLeastSquare}, where the iteration stops when certain * quantities are smaller than this parameter. * * @param pricer the cap/floor pricer to convert quoted volatilities to prices * @param sabrPeriodPricer the SABR pricer * @param epsilon the epsilon parameter * @param referenceData the reference data * @return the instance */ public static SabrIborCapletFloorletVolatilityBootstrapper of( VolatilityIborCapFloorLegPricer pricer, SabrIborCapletFloorletPeriodPricer sabrPeriodPricer, double epsilon, ReferenceData referenceData) { NonLinearLeastSquare solver = new NonLinearLeastSquare(SV_COMMONS, OG_ALGEBRA, epsilon); return new SabrIborCapletFloorletVolatilityBootstrapper(pricer, sabrPeriodPricer, solver, referenceData); }
/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is not available * @param observedValues Set of measurement values * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param startPos Initial value of the parameters * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, DoubleArray startPos) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, sigma, func, jac.differentiate(func), startPos, null); }
/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is not available * @param observedValues Set of measurement values * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param startPos Initial value of the parameters * @param maxJumps A vector containing the maximum absolute allowed step in a particular direction in each iteration. * Can be null, in which case no constant * on the step size is applied. * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, DoubleArray startPos, DoubleArray maxJumps) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, sigma, func, jac.differentiate(func), startPos, maxJumps); }
/** * Use this when the model is given as a function of its parameters only (i.e. a function that takes a set of * parameters and return a set of model values, * so the measurement points are already known to the function), and analytic parameter sensitivity is not available * @param observedValues Set of measurement values * @param func The model as a function of its parameters only * @param startPos Initial value of the parameters * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray observedValues, Function<DoubleArray, DoubleArray> func, DoubleArray startPos) { int n = observedValues.size(); VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, DoubleArray.filled(n, 1.0), func, jac.differentiate(func), startPos, null); }
/** * Use this when the model is in the ParameterizedFunction form and analytic parameter sensitivity is not available. * @param x Set of measurement points * @param y Set of measurement values * @param func The model in ParameterizedFunction form (i.e. takes measurement points and a set of parameters and * returns a model value) * @param startPos Initial value of the parameters * @return A LeastSquareResults object */ public LeastSquareResults solve( DoubleArray x, DoubleArray y, ParameterizedFunction<Double, DoubleArray, Double> func, DoubleArray startPos) { ArgChecker.notNull(x, "x"); ArgChecker.notNull(y, "y"); int n = x.size(); ArgChecker.isTrue(y.size() == n, "y wrong length"); return solve(x, y, DoubleArray.filled(n, 1), func, startPos); }
/** * Use this when the model is in the ParameterizedFunction form and analytic parameter sensitivity and a single * measurement error are available. * @param x Set of measurement points * @param y Set of measurement values * @param sigma Measurement errors * @param func The model in ParameterizedFunction form (i.e. takes a measurement points and a set of parameters and * returns a model value) * @param grad The model parameter sensitivities in ParameterizedFunction form (i.e. takes a measurement points and a * set of parameters and returns a model parameter sensitivities) * @param startPos Initial value of the parameters * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray x, DoubleArray y, double sigma, ParameterizedFunction<Double, DoubleArray, Double> func, ParameterizedFunction<Double, DoubleArray, DoubleArray> grad, DoubleArray startPos) { ArgChecker.notNull(x, "x"); ArgChecker.notNull(y, "y"); int n = x.size(); ArgChecker.isTrue(y.size() == n, "y wrong length"); return solve(x, y, DoubleArray.filled(n, sigma), func, grad, startPos); }
/** * Solve using a user supplied NonLinearParameterTransforms. * <p> * This returns {@link LeastSquareResults}. * * @param start the first guess at the parameter values * @param transform transform from model parameters to fitting parameters, and vice versa * @return the calibration results */ public LeastSquareResultsWithTransform solve(DoubleArray start, NonLinearParameterTransforms transform) { NonLinearTransformFunction transFunc = new NonLinearTransformFunction(volFunc, volAdjointFunc, transform); LeastSquareResults solRes = SOLVER.solve(marketValues, errors, transFunc.getFittingFunction(), transFunc.getFittingJacobian(), transform.transform(start), getConstraintFunction(transform), getMaximumStep()); return new LeastSquareResultsWithTransform(solRes, transform); }
/** * Use this when the model is in the ParameterizedFunction form and analytic parameter sensitivity is not available * but a measurement error is. * @param x Set of measurement points * @param y Set of measurement values * @param sigma y Set of measurement errors * @param func The model in ParameterizedFunction form (i.e. takes measurement points and a set of parameters and * returns a model value) * @param startPos Initial value of the parameters * @return A LeastSquareResults object */ public LeastSquareResults solve( DoubleArray x, DoubleArray y, double sigma, ParameterizedFunction<Double, DoubleArray, Double> func, DoubleArray startPos) { ArgChecker.notNull(x, "x"); ArgChecker.notNull(y, "y"); ArgChecker.notNull(sigma, "sigma"); int n = x.size(); ArgChecker.isTrue(y.size() == n, "y wrong length"); return solve(x, y, DoubleArray.filled(n, sigma), func, startPos); }
/** * Use this when the model is in the ParameterizedFunction form and analytic parameter sensitivity. * @param x Set of measurement points * @param y Set of measurement values * @param func The model in ParameterizedFunction form (i.e. takes a measurement points and a set of parameters and * returns a model value) * @param grad The model parameter sensitivities in ParameterizedFunction form (i.e. takes a measurement points and a * set of parameters and returns a model parameter sensitivities) * @param startPos Initial value of the parameters * @return value of the fitted parameters */ public LeastSquareResults solve( DoubleArray x, DoubleArray y, ParameterizedFunction<Double, DoubleArray, Double> func, ParameterizedFunction<Double, DoubleArray, DoubleArray> grad, DoubleArray startPos) { ArgChecker.notNull(x, "x"); ArgChecker.notNull(y, "y"); ArgChecker.notNull(x, "sigma"); int n = x.size(); ArgChecker.isTrue(y.size() == n, "y wrong length"); // emcleod 31-1-2011 arbitrary value 1 for now return solve(x, y, DoubleArray.filled(n, 1), func, grad, startPos); }
sabrDefinition, ratesProvider, vols, capList, priceList, index.getCurrency()); NonLinearTransformFunction transFunc = new NonLinearTransformFunction(valueFunction, jacobianFunction, transform); LeastSquareResults res = solver.solve( DoubleArray.filled(priceList.size(), 1d), DoubleArray.copyOf(errorList),
return solve(y, sigma, func1D, startPos, null);