示例#1
0
const Real Gaussian1dModel::zerobondOption(
    const Option::Type &type, const Date &expiry, const Date &valueDate,
    const Date &maturity, const Rate strike, const Date &referenceDate,
    const Real y, const Handle<YieldTermStructure> &yts, const Real yStdDevs,
    const Size yGridPoints, const bool extrapolatePayoff,
    const bool flatPayoffExtrapolation) const {

    calculate();

    Time fixingTime = termStructure()->timeFromReference(expiry);
    Time referenceTime =
        referenceDate == Null<Date>()
        ? 0.0
        : termStructure()->timeFromReference(referenceDate);

    Array yg = yGrid(yStdDevs, yGridPoints, fixingTime, referenceTime, y);
    Array z = yGrid(yStdDevs, yGridPoints);

    Array p(yg.size());

    for (Size i = 0; i < yg.size(); i++) {
        Real expValDsc = zerobond(valueDate, expiry, yg[i], yts);
        Real discount =
            zerobond(maturity, expiry, yg[i], yts) / expValDsc;
        p[i] =
            std::max((type == Option::Call ? 1.0 : -1.0) * (discount - strike),
                     0.0) /
            numeraire(fixingTime, yg[i], yts) * expValDsc;
    }

    CubicInterpolation payoff(
        z.begin(), z.end(), p.begin(), CubicInterpolation::Spline, true,
        CubicInterpolation::Lagrange, 0.0, CubicInterpolation::Lagrange, 0.0);

    Real price = 0.0;
    for (Size i = 0; i < z.size() - 1; i++) {
        price += gaussianShiftedPolynomialIntegral(
                     0.0, payoff.cCoefficients()[i], payoff.bCoefficients()[i],
                     payoff.aCoefficients()[i], p[i], z[i], z[i], z[i + 1]);
    }
    if (extrapolatePayoff) {
        if (flatPayoffExtrapolation) {
            price += gaussianShiftedPolynomialIntegral(
                         0.0, 0.0, 0.0, 0.0, p[z.size() - 2], z[z.size() - 2],
                         z[z.size() - 1], 100.0);
            price += gaussianShiftedPolynomialIntegral(0.0, 0.0, 0.0, 0.0, p[0],
                     z[0], -100.0, z[0]);
        } else {
            if (type == Option::Call)
                price += gaussianShiftedPolynomialIntegral(
                             0.0, payoff.cCoefficients()[z.size() - 2],
                             payoff.bCoefficients()[z.size() - 2],
                             payoff.aCoefficients()[z.size() - 2], p[z.size() - 2],
                             z[z.size() - 2], z[z.size() - 1], 100.0);
            if (type == Option::Put)
                price += gaussianShiftedPolynomialIntegral(
                             0.0, payoff.cCoefficients()[0], payoff.bCoefficients()[0],
                             payoff.aCoefficients()[0], p[0], z[0], -100.0, z[0]);
        }
    }

    return numeraire(referenceTime, y, yts) * price;
}
示例#2
0
extern std::vector
< std::unique_ptr< math::API::InterpolationTableIntegral > > referenceIntegrals;

SCENARIO("The composite interpolation table's integral functions correctly",
         "[math], [CompositeInterpolationTable], [integral]"){
  GIVEN("An CompositeInterpolationTable and "
        "the vector of integral tables generated from it's components"){    
    WHEN("queried for their xGrid and yGrids"){
      THEN("the stored values will be equal"){
        LOG(INFO) << "Test " << ++testNumber
                  << ": [integral] No Errors Expected";
        auto integralTable = cit->integral(0);
        auto xGrids = integralTable->xGrid();
        auto xGrid = xGrids.begin();
        auto yGrids = integralTable->yGrid();
        auto yGrid = yGrids.begin();
        for (auto& referenceIntegral : referenceIntegrals){
          auto refXGrids = referenceIntegral->xGrid();
          auto& refXGrid = refXGrids[0];
          REQUIRE(true == std::equal(refXGrid.begin(), refXGrid.end(),
                                     xGrid->begin(), xGrid->end()));
          auto refYGrids = referenceIntegral->yGrid();
          auto& refYGrid = refYGrids[0];
          REQUIRE(true == std::equal(refYGrid.begin(), refYGrid.end(),
                                     yGrid->begin(), yGrid->end()));
          ++xGrid;
          ++yGrid;
        }
      }
    }