/** * Create an instance of the finite difference calculator. * * @param fdType the finite difference type * @param shift the shift to be applied to the curves */ private CurveGammaCalculator(FiniteDifferenceType fdType, double shift) { this.fd = new VectorFieldFirstOrderDifferentiator(fdType, shift); }
/** * Creates an instance using the default values. */ public VectorFieldSecondOrderDifferentiator() { this.eps = DEFAULT_EPS; this.epsSqr = eps * eps; this.vectorFieldDiff = new VectorFieldFirstOrderDifferentiator(eps); this.maxtrixFieldDiff = new MatrixFieldFirstOrderDifferentiator(eps); }
@Override public DoubleArray findRoot(Function<DoubleArray, DoubleArray> function, DoubleArray startPosition) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return findRoot(function, jac.differentiate(function), startPosition); }
/** * 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 penalty Penalty matrix * @return value of the fitted parameters */ public LeastSquareWithPenaltyResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, DoubleArray startPos, DoubleMatrix penalty) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, sigma, func, jac.differentiate(func), startPos, penalty); }
/** * 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 sigma Set of measurement errors * @param func The model as a function of its parameters only * @param startPos Initial value of the parameters * @param penalty Penalty matrix * @param allowedValue a function which returned true if the new trial position is allowed by the model. An example * would be to enforce positive parameters * without resorting to a non-linear parameter transform. In some circumstances this approach will lead to slow * convergence. * @return value of the fitted parameters */ public LeastSquareWithPenaltyResults solve( DoubleArray observedValues, DoubleArray sigma, Function<DoubleArray, DoubleArray> func, DoubleArray startPos, DoubleMatrix penalty, Function<DoubleArray, Boolean> allowedValue) { VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, sigma, func, jac.differentiate(func), startPos, penalty, allowedValue); }
/** * 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 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 * @param penalty Penalty matrix * @return value of the fitted parameters */ public LeastSquareWithPenaltyResults solve( DoubleArray observedValues, Function<DoubleArray, DoubleArray> func, DoubleArray startPos, DoubleMatrix penalty) { int n = observedValues.size(); VectorFieldFirstOrderDifferentiator jac = new VectorFieldFirstOrderDifferentiator(); return solve(observedValues, DoubleArray.filled(n, 1.0), func, jac.differentiate(func), startPos, penalty); }
private void testJacobian(DoubleArray x) { int n = x.size(); Function<DoubleArray, DoubleArray> func = _fitter.getModelValueFunction(); Function<DoubleArray, DoubleMatrix> jacFunc = _fitter.getModelJacobianFunction(); VectorFieldFirstOrderDifferentiator differ = new VectorFieldFirstOrderDifferentiator(); Function<DoubleArray, DoubleMatrix> jacFuncFD = differ.differentiate(func); DoubleMatrix jac = jacFunc.apply(x); DoubleMatrix jacFD = jacFuncFD.apply(x); int rows = jacFD.rowCount(); int cols = jacFD.columnCount(); assertEquals(_cleanVols.length, rows); assertEquals(n, cols); assertEquals(rows, jac.rowCount()); assertEquals(cols, jac.columnCount()); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { assertEquals(jacFD.get(i, j), jac.get(i, j), 2e-2); } } }
@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); } } } }
@Test public void test() { final ParameterizedCurveVectorFunctionProvider pro = new ParameterizedCurveVectorFunctionProvider(s_PCurve); final double[] points = new double[] {-1.0, 0.0, 1.0 }; final VectorFunction f = pro.from(points); assertEquals(2, f.getLengthOfDomain()); assertEquals(3, f.getLengthOfRange()); final DoubleArray x = DoubleArray.of(0.5, 2.0); //the parameters a & b final DoubleArray y = f.apply(x); assertEquals(0.5 * Math.sinh(-2.0), y.get(0), 1e-14); assertEquals(0.0, y.get(1), 1e-14); assertEquals(0.5 * Math.sinh(2.0), y.get(2), 1e-14); final DoubleMatrix jac = f.calculateJacobian(x); final DoubleMatrix fdJac = (new VectorFieldFirstOrderDifferentiator().differentiate(f)).apply(x); AssertMatrix.assertEqualsMatrix(fdJac, jac, 1e-9); } }