Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
linearannuitymapping.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file lienarannuitymapping.hpp
20 \brief linear annuity mapping function f(S) = a*S+b
21*/
22
24
25#include <ql/cashflows/coupon.hpp>
26
27namespace QuantExt {
28
29LinearAnnuityMapping::LinearAnnuityMapping(const Real a, const Real b) : a_(a), b_(b) {}
30
31Real LinearAnnuityMapping::map(const Real S) const { return a_ * S + b_; }
32
33Real LinearAnnuityMapping::mapPrime(const Real S) const { return a_; }
34
35Real LinearAnnuityMapping::mapPrime2(const Real S) const { return 0.0; }
36
37bool LinearAnnuityMapping::mapPrime2IsZero() const { return true; }
38
39LinearAnnuityMappingBuilder::LinearAnnuityMappingBuilder(const Real a, const Real b) : a_(a), b_(b) {}
40LinearAnnuityMappingBuilder::LinearAnnuityMappingBuilder(const Handle<Quote>& reversion) : reversion_(reversion) {
41 registerWith(reversion_);
42}
43
44namespace {
45Real GsrG(const Real yf, const Real reversion) {
46 if (std::fabs(reversion) < 1.0E-4)
47 return yf;
48 else
49 return (1.0 - std::exp(-reversion * yf)) / reversion;
50}
51} // namespace
52
53QuantLib::ext::shared_ptr<AnnuityMapping> LinearAnnuityMappingBuilder::build(const Date& valuationDate, const Date& optionDate,
54 const Date& paymentDate,
55 const VanillaSwap& underlying,
56 const Handle<YieldTermStructure>& discountCurve) {
57
58 // no need for an actual mapping, since the coupon amount is deterministic, i.e. model-independent
59
60 if (optionDate <= valuationDate)
61 return QuantLib::ext::make_shared<LinearAnnuityMapping>(0.0, 0.0);
62
63 // build the mapping dependent on whether a, b or a reversion is given
64
65 if (a_ != Null<Real>() && b_ != Null<Real>()) {
66 return QuantLib::ext::make_shared<LinearAnnuityMapping>(a_, b_);
67 } else if (!reversion_.empty()) {
68 Real atmForward = underlying.fairRate();
69 Real gx = 0.0, gy = 0.0;
70 for (Size i = 0; i < underlying.fixedLeg().size(); i++) {
71 QuantLib::ext::shared_ptr<Coupon> c = QuantLib::ext::dynamic_pointer_cast<Coupon>(underlying.fixedLeg()[i]);
72 Real yf = c->accrualPeriod();
73 Real pv = yf * discountCurve->discount(c->date());
74 gx += pv * GsrG(discountCurve->dayCounter().yearFraction(optionDate, c->date()), reversion_->value());
75 gy += pv;
76 }
77 Real gamma = gx / gy;
78 Date lastd = underlying.fixedLeg().back()->date();
79 Real a =
80 discountCurve->discount(paymentDate) *
81 (gamma - GsrG(discountCurve->dayCounter().yearFraction(optionDate, paymentDate), reversion_->value())) /
82 (discountCurve->discount(lastd) *
83 GsrG(discountCurve->dayCounter().yearFraction(optionDate, lastd), reversion_->value()) +
84 atmForward * gy * gamma);
85 Real b = discountCurve->discount(paymentDate) / gy - a * atmForward;
86 return QuantLib::ext::make_shared<LinearAnnuityMapping>(a, b);
87 } else {
88 QL_FAIL("LinearAnnuityMapping::build(): failed, because neither a, b nor a reversion is given");
89 }
90}
91
92} // namespace QuantExt
QuantLib::ext::shared_ptr< AnnuityMapping > build(const Date &valuationDate, const Date &optionDate, const Date &paymentDate, const VanillaSwap &underlying, const Handle< YieldTermStructure > &discountCurve) override
LinearAnnuityMappingBuilder(const Real a, const Real b)
Real map(const Real S) const override
LinearAnnuityMapping(const Real a, const Real b)
Real mapPrime(const Real S) const override
Real mapPrime2(const Real S) const override
linear annuity mapping function f(S) = a*S+b
std::vector< Real > a_