@Test public void testNullTransform() { BitSet fixed = new BitSet(); fixed.set(0); DoubleArray start = DoubleArray.of(Math.PI / 4, 1); UncoupledParameterTransforms transforms = new UncoupledParameterTransforms(start, NULL_TRANSFORMS, fixed); NonLinearTransformFunction transFunc = new NonLinearTransformFunction(FUNCTION, JACOBIAN, transforms); Function<DoubleArray, DoubleArray> func = transFunc.getFittingFunction(); Function<DoubleArray, DoubleMatrix> jacFunc = transFunc.getFittingJacobian(); DoubleArray x = DoubleArray.of(0.5); final double rootHalf = Math.sqrt(0.5); DoubleArray y = func.apply(x); assertEquals(3, y.size()); assertEquals(rootHalf * Math.cos(0.5), y.get(0), 1e-9); assertEquals(rootHalf * Math.sin(0.5), y.get(1), 1e-9); assertEquals(rootHalf, y.get(2), 1e-9); DoubleMatrix jac = jacFunc.apply(x); assertEquals(3, jac.rowCount()); assertEquals(1, jac.columnCount()); assertEquals(-rootHalf * Math.sin(0.5), jac.get(0, 0), 1e-9); assertEquals(rootHalf * Math.cos(0.5), jac.get(1, 0), 1e-9); assertEquals(0, jac.get(2, 0), 1e-9); }
/** * 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); }
@Test public void testNonLinearTransform() { BitSet fixed = new BitSet(); DoubleArray start = DoubleArray.filled(2); UncoupledParameterTransforms transforms = new UncoupledParameterTransforms(start, TRANSFORMS, fixed); NonLinearTransformFunction transFunc = new NonLinearTransformFunction(FUNCTION, JACOBIAN, transforms); Function<DoubleArray, DoubleArray> func = transFunc.getFittingFunction(); Function<DoubleArray, DoubleMatrix> jacFunc = transFunc.getFittingJacobian(); VectorFieldFirstOrderDifferentiator diff = new VectorFieldFirstOrderDifferentiator(); Function<DoubleArray, DoubleMatrix> jacFuncFD = diff.differentiate(func); DoubleArray testPoint = DoubleArray.of(4.5, -2.1); DoubleMatrix jac = jacFunc.apply(testPoint); DoubleMatrix jacFD = jacFuncFD.apply(testPoint); assertEquals(3, jac.rowCount()); assertEquals(2, jac.columnCount()); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { assertEquals(jacFD.get(i, j), jac.get(i, j), 1e-6); } } } }
DoubleArray.copyOf(errorList), transFunc.getFittingFunction(), transFunc.getFittingJacobian(), transform.transform(initialValues)); LeastSquareResultsWithTransform resTransform = new LeastSquareResultsWithTransform(res, transform);
DoubleArray errors = DoubleArray.of(nCaplets, n -> errorList.get(currentStart + n)); LeastSquareResults res = solver.solve(adjustedPrices, errors, transFunc.getFittingFunction(), transFunc.getFittingJacobian(), transform.transform(start)); LeastSquareResultsWithTransform resTransform = new LeastSquareResultsWithTransform(res, transform); vols = updateParameters(vols, nExpiries, i, betaFix, resTransform.getModelParameters());