NelsonSiegelBondCurveModel { public ParameterizedFunction<Double, DoubleMatrix1D, Double> getParameterizedFunction() { return new ParameterizedFunction<Double, DoubleMatrix1D, Double>() { @Override public Double evaluate(final Double t, final DoubleMatrix1D parameters) { Validate.notNull(t, "t"); Validate.notNull(parameters, "parameters"); Validate.isTrue(parameters.getNumberOfElements() == getNumberOfParameters()); final double beta0 = parameters.getEntry(0); final double beta1 = parameters.getEntry(1); final double beta2 = parameters.getEntry(2); final double lambda = parameters.getEntry(3); final double x1 = t / lambda; final double x2 = Epsilon.epsilon(-x1); return beta0 + beta1 * x2 + beta2 * (x2 - Math.exp(-x1)); } public Object writeReplace() { return new InvokedSerializedForm(NelsonSiegelBondCurveModel.class, "getParameterizedFunction"); } @Override public int getNumberOfParameters() { return 4; } }; }  ParameterizedFunction<Double, DoubleMatrix1D, Double> getParameterizedFunction();  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTime() { MODEL.getParameterizedFunction().evaluate(null, new DoubleMatrix1D(new double[] {1, 2, 3, 4 })); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullParameters() { MODEL.getParameterizedFunction().evaluate(3., null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongNumberOfElements() { MODEL.getParameterizedFunction().evaluate(3., new DoubleMatrix1D(new double[] {1, 2, 3 })); }
@Test public void testKnownParameters() { final LeastSquareResults result = NLLS.solve(T, Y, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {2, -1, 1, 4 })); final DoubleMatrix1D fitted = result.getFitParameters(); assertArrayEquals(fitted.getData(), new double[] {BETA0, BETA1, BETA2, LAMBDA }, 1e-6); }
@Test public void testFit() { final LeastSquareResults result = NLLS.solve(TREASURY_T, TREASURY_19740102_Y, TREASURY_E, MODEL.getParameterizedFunction(), new DoubleMatrix1D(new double[] {1, 1, 1, 1 })); final DoubleMatrix1D fitted = result.getFitParameters(); assertEquals(fitted.getEntry(0), 6.88649008, 1e-3); assertEquals(fitted.getEntry(1), 1.56992400, 1e-3); assertEquals(fitted.getEntry(2), -2.58262072, 1e-3); assertEquals(fitted.getEntry(3), 1.14781832, 1e-3); }