public virtual void test_optionPrice_down() { double tol = 1.0e-12; double barrier = 97d; ConstantContinuousSingleBarrierKnockoutFunction test = ConstantContinuousSingleBarrierKnockoutFunction.of(STRIKE, TIME_TO_EXPIRY, PutCall.CALL, NUM, BarrierType.DOWN, barrier, REBATE); double spot = 100d; double u = 1.05; double d = 0.98; double m = Math.Sqrt(u * d); double up = 0.29; double dp = 0.25; double mp = 1d - up - dp; // test getPayoffAtExpiryTrinomial DoubleArray computedPayoff = test.getPayoffAtExpiryTrinomial(spot, d, m); int expectedSize = 2 * NUM + 1; assertEquals(computedPayoff.size(), expectedSize); double[] price = new double[expectedSize]; for (int i = 0; i < expectedSize; ++i) { price[i] = spot * Math.Pow(u, 0.5 * i) * Math.Pow(d, NUM - 0.5 * i); } for (int i = 0; i < expectedSize; ++i) { double expectedPayoff = price[i] > barrier?Math.Max(price[i] - STRIKE, 0d) : REBATE_AMOUNT; if (i != 0 && price[i - 1] < barrier && price[i] > barrier) { expectedPayoff = 0.5 * (expectedPayoff * (price[i] - barrier) + REBATE_AMOUNT * (barrier - price[i - 1])) / (price[i] - price[i - 1]) + 0.5 * expectedPayoff; } assertEquals(computedPayoff.get(i), expectedPayoff, tol); } // test getNextOptionValues double df = 0.92; int n = 2; DoubleArray values = DoubleArray.of(1.4, 0.9, 0.1, 0.05, 0.0, 0.0, 0.0); DoubleArray computedNextValues = test.getNextOptionValues(df, up, mp, dp, values, spot, d, m, n); double tmp = df * (0.9 * dp + 0.1 * mp + 0.05 * up); DoubleArray expectedNextValues = DoubleArray.of(REBATE_AMOUNT, 0.5 * (tmp * (m * d - barrier / spot) + REBATE_AMOUNT * (barrier / spot - d * d)) / (m * d - d * d) + 0.5 * tmp, df * (0.1 * dp + 0.05 * mp), df * 0.05 * dp, 0.0); assertTrue(DoubleArrayMath.fuzzyEquals(computedNextValues.toArray(), expectedNextValues.toArray(), tol)); }
public virtual void test_optionPrice_up() { double tol = 1.0e-12; ConstantContinuousSingleBarrierKnockoutFunction test = ConstantContinuousSingleBarrierKnockoutFunction.of(STRIKE, TIME_TO_EXPIRY, PutCall.PUT, NUM, BarrierType.UP, BARRIER, REBATE); double spot = 130d; double u = 1.05; double d = 0.98; double m = Math.Sqrt(u * d); double up = 0.29; double dp = 0.25; double mp = 1d - up - dp; // test getPayoffAtExpiryTrinomial DoubleArray computedPayoff = test.getPayoffAtExpiryTrinomial(spot, d, m); int expectedSize = 2 * NUM + 1; assertEquals(computedPayoff.size(), expectedSize); double[] price = new double[expectedSize]; for (int i = 0; i < expectedSize; ++i) { price[i] = spot * Math.Pow(u, 0.5 * i) * Math.Pow(d, NUM - 0.5 * i); } for (int i = 0; i < expectedSize; ++i) { double expectedPayoff = price[i] < BARRIER?Math.Max(STRIKE - price[i], 0d) : REBATE_AMOUNT; if (i != expectedSize - 1 && price[i] < BARRIER && price[i + 1] > BARRIER) { expectedPayoff = 0.5 * ((BARRIER - price[i]) * expectedPayoff + (price[i + 1] - BARRIER) * REBATE_AMOUNT) / (price[i + 1] - price[i]) + 0.5 * expectedPayoff; } assertEquals(computedPayoff.get(i), expectedPayoff, tol); } // test getNextOptionValues double df = 0.92; int n = 2; DoubleArray values = DoubleArray.of(1.4, 0.9, 0.1, 0.05, 0.0, 0.0, 0.0); DoubleArray computedNextValues = test.getNextOptionValues(df, up, mp, dp, values, spot, d, m, n); double tmp = df * 0.05 * dp; DoubleArray expectedNextValues = DoubleArray.of(df * (1.4 * dp + 0.9 * mp + 0.1 * up), df * (0.9 * dp + 0.1 * mp + 0.05 * up), df * (0.1 * dp + 0.05 * mp), 0.5 * ((BARRIER / spot - u * m) * tmp + (u * u - BARRIER / spot) * REBATE_AMOUNT) / (u * u - u * m) + 0.5 * tmp, REBATE_AMOUNT); assertTrue(DoubleArrayMath.fuzzyEquals(computedNextValues.toArray(), expectedNextValues.toArray(), tol)); }