DiscreteVolatilityFunctionProviderDirect extends DiscreteVolatilityFunctionProvider { @Override public DiscreteVolatilityFunction from(final DoublesPair[] expiryStrikePoints) { ArgumentChecker.noNulls(expiryStrikePoints, "strikeExpiryPoints"); final int n = expiryStrikePoints.length; final IdentityMatrix iM = new IdentityMatrix(n); return new DiscreteVolatilityFunction() { @Override public DoubleMatrix1D evaluate(final DoubleMatrix1D x) { ArgumentChecker.isTrue(x.getNumberOfElements() == n, "size of x"); return new DoubleMatrix1D(x.getData()); } @Override public DoubleMatrix2D calculateJacobian(final DoubleMatrix1D x) { return iM; } @Override public int getLengthOfDomain() { return n; } @Override public int getLengthOfRange() { return n; } }; }  @Override DiscreteVolatilityFunction from(final DoublesPair[] expiryStrikePoints);  }
@Test public void test() { final int nSamples = 10; final List<DoublesPair> points = new ArrayList<>(nSamples); final DoubleMatrix1D x = new DoubleMatrix1D(nSamples); for (int i = 0; i < nSamples; i++) { final double t = RANDOM.nextDouble() * 20.0; final double k = RANDOM.nextDouble() * 0.15; points.add(DoublesPair.of(t, k)); x.getData()[i] = RANDOM.nextDouble(); } final DiscreteVolatilityFunctionProvider pro = new DiscreteVolatilityFunctionProviderDirect(); final DiscreteVolatilityFunction func = pro.from(points); final DoubleMatrix1D y = func.evaluate(x); AssertMatrix.assertEqualsVectors(x, y, 0.0); final DoubleMatrix2D jac = func.calculateJacobian(x); AssertMatrix.assertEqualsMatrix(new IdentityMatrix(nSamples), jac, 0.0); assertEquals(nSamples, func.getLengthOfDomain()); assertEquals(nSamples, func.getLengthOfRange()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongLengthTest() { final int nSamples = 10; final List<DoublesPair> points = new ArrayList<>(nSamples); final DoubleMatrix1D x = new DoubleMatrix1D(nSamples + 1); x.getData()[0] = RANDOM.nextDouble(); for (int i = 0; i < nSamples; i++) { final double t = RANDOM.nextDouble() * 20.0; final double k = RANDOM.nextDouble() * 0.15; points.add(DoublesPair.of(t, k)); x.getData()[i + 1] = RANDOM.nextDouble(); } final DiscreteVolatilityFunctionProvider pro = new DiscreteVolatilityFunctionProviderDirect(); final DiscreteVolatilityFunction func = pro.from(points); func.evaluate(x); }