@Test(expectedExceptions = IllegalArgumentException.class) public void testNullStrikes2() { getInstance().getWeight(null, INDEX, STRIKE); }
public void testName() { assertEquals(getInstance().getName(), "Sine"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testBadName() { WeightingFunction.of("Random"); }
public void testName() { assertEquals(getInstance().getName(), "Linear"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeIndex() { getInstance().getWeight(STRIKES, -INDEX, STRIKE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testHighIndex() { getInstance().getWeight(STRIKES, STRIKES.length, STRIKE); }
/** * Gets the function weight for point x, based on the lower bound index. * * @param xs the independent data points * @param index the index of the data point below x * @param x the x-point to find the weight for * @return the weight */ public default double getWeight(double[] xs, int index, double x) { ArgChecker.notNull(xs, "strikes"); ArgChecker.notNegative(index, "index"); ArgChecker.isTrue(index <= xs.length - 2, "index cannot be larger than {}, have {}", xs.length - 2, index); double y = (xs[index + 1] - x) / (xs[index + 1] - xs[index]); return getWeight(y); }
@Override protected double doInterpolate(double xValue) { // x-value is less than the x-value of the last node (lowerIndex < intervalCount) int lowerIndex = lowerBoundIndex(xValue, xValues); int higherIndex = lowerIndex + 1; // at start of curve if (lowerIndex == 0) { RealPolynomialFunction1D quadratic = quadratics[0]; double x = xValue - xValues[1]; return quadratic.applyAsDouble(x); } // at end of curve if (higherIndex == intervalCount) { RealPolynomialFunction1D quadratic = quadratics[intervalCount - 2]; double x = xValue - xValues[intervalCount - 1]; return quadratic.applyAsDouble(x); } // normal case RealPolynomialFunction1D quadratic1 = quadratics[lowerIndex - 1]; RealPolynomialFunction1D quadratic2 = quadratics[higherIndex - 1]; double w = WEIGHT_FUNCTION.getWeight((xValues[higherIndex] - xValue) / (xValues[higherIndex] - xValues[lowerIndex])); return w * quadratic1.applyAsDouble(xValue - xValues[lowerIndex]) + (1 - w) * quadratic2.applyAsDouble(xValue - xValues[higherIndex]); }
@Override protected double doFirstDerivative(double xValue) { int lowerIndex = lowerBoundIndex(xValue, xValues); int higherIndex = lowerIndex + 1; RealPolynomialFunction1D[] quadFirstDerivative = quadraticsFirstDerivative.get(); // at start of curve, or only one interval if (lowerIndex == 0 || intervalCount == 1) { RealPolynomialFunction1D quadraticFirstDerivative = quadFirstDerivative[0]; double x = xValue - xValues[1]; return quadraticFirstDerivative.applyAsDouble(x); } // at end of curve if (higherIndex >= intervalCount) { RealPolynomialFunction1D quadraticFirstDerivative = quadFirstDerivative[intervalCount - 2]; double x = xValue - xValues[intervalCount - 1]; return quadraticFirstDerivative.applyAsDouble(x); } RealPolynomialFunction1D quadratic1 = quadratics[lowerIndex - 1]; RealPolynomialFunction1D quadratic2 = quadratics[higherIndex - 1]; RealPolynomialFunction1D quadratic1FirstDerivative = quadFirstDerivative[lowerIndex - 1]; RealPolynomialFunction1D quadratic2FirstDerivative = quadFirstDerivative[higherIndex - 1]; double w = WEIGHT_FUNCTION.getWeight((xValues[higherIndex] - xValue) / (xValues[higherIndex] - xValues[lowerIndex])); return w * quadratic1FirstDerivative.applyAsDouble(xValue - xValues[lowerIndex]) + (1 - w) * quadratic2FirstDerivative.applyAsDouble(xValue - xValues[higherIndex]) + (quadratic2.applyAsDouble(xValue - xValues[higherIndex]) - quadratic1.applyAsDouble(xValue - xValues[lowerIndex])) / (xValues[higherIndex] - xValues[lowerIndex]); }
double w = WEIGHT_FUNCTION.getWeight((xValues[higherIndex] - xValue) / (xValues[higherIndex] - xValues[lowerIndex])); result[lowerIndex - 1] = w * temp1[0]; result[lowerIndex] = w * temp1[1] + (1 - w) * temp2[0];