Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
lgmcg.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 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
22
23#include <ql/instruments/overnightindexedswap.hpp>
24#include <ql/instruments/vanillaswap.hpp>
25
26#include <ql/cashflows/fixedratecoupon.hpp>
27#include <ql/cashflows/iborcoupon.hpp>
28#include <ql/cashflows/overnightindexedcoupon.hpp>
29#include <ql/indexes/iborindex.hpp>
30#include <ql/indexes/swapindex.hpp>
31
32namespace ore::data {
33
34std::size_t LgmCG::numeraire(const Date& d, const std::size_t x, const Handle<YieldTermStructure>& discountCurve,
35 const std::string& discountCurveId) const {
36 std::string id = "__lgm_" + qualifier_ + "_N_" + ore::data::to_string(d) + "_" + discountCurveId;
37 std::size_t n;
38 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan); n == ComputationGraph::nan) {
39 auto p(p_);
40 // Date ds = getSloppyDate(d, sloppySimDates_, effSimDates_);
41 Real t = p()->termStructure()->timeFromReference(d);
42 // Real ts = p()->termStructure()->timeFromReference(ds);
43 std::string id_P0t = "__dsc_" + ore::data::to_string(d) + "_" + discountCurveId;
44 std::string id_H = "__lgm_" + qualifier_ + "_H_" + ore::data::to_string(d);
45 std::string id_zeta = "__lgm_" + qualifier_ + "_zeta_" + ore::data::to_string(d);
46 std::size_t H = addModelParameter(g_, modelParameters_, id_H, [p, t] { return p()->H(t); });
47 std::size_t zeta = addModelParameter(g_, modelParameters_, id_zeta, [p, t] { return p()->zeta(t); });
48 std::size_t P0t = addModelParameter(g_, modelParameters_, id_P0t, [p, discountCurve, t] {
49 return (discountCurve.empty() ? p()->termStructure() : discountCurve)->discount(t);
50 });
51 n = cg_div(g_,
52 cg_exp(g_, cg_add(g_, cg_mult(g_, H, x),
53 cg_mult(g_, cg_mult(g_, cg_const(g_, 0.5), zeta), cg_mult(g_, H, H)))),
54 P0t);
55 g_.setVariable(id, n);
56 }
57 return n;
58}
59
60std::size_t LgmCG::discountBond(const Date& d, const Date& e, const std::size_t x,
61 const Handle<YieldTermStructure>& discountCurve,
62 const std::string& discountCurveId) const {
63 std::string id =
64 "__lgm_" + qualifier_ + "_P_" + ore::data::to_string(d) + "_" + ore::data::to_string(e) + "_" + discountCurveId;
65 std::size_t n;
66 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan), n == ComputationGraph::nan) {
67 n = cg_mult(g_, numeraire(d, x, discountCurve, discountCurveId),
68 reducedDiscountBond(d, e, x, discountCurve, discountCurveId));
69 }
70 return n;
71}
72
73std::size_t LgmCG::reducedDiscountBond(const Date& d, Date e, const std::size_t x,
74 const Handle<YieldTermStructure>& discountCurve,
75 const std::string& discountCurveId) const {
76 e = std::max(d, e);
77 std::string id = "__lgm_" + qualifier_ + "_Pr_" + ore::data::to_string(d) + "_" + ore::data::to_string(e) + "_" +
78 discountCurveId;
79 std::size_t n;
80 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan), n == ComputationGraph::nan) {
81 auto p = p_;
82 // Date ds = getSloppyDate(d, sloppySimDates_, effSimDates_);
83 // Date es = getSloppyDate(e, sloppySimDates_, effSimDates_);
84 Real t = p()->termStructure()->timeFromReference(d);
85 Real T = p()->termStructure()->timeFromReference(e);
86 // Real ts = p()->termStructure()->timeFromReference(ds);
87 // Real Ts = p()->termStructure()->timeFromReference(es);
88 std::string id_P0T = "__dsc_" + ore::data::to_string(e) + "_" + discountCurveId;
89 std::string id_H = "__lgm_" + qualifier_ + "_H_" + ore::data::to_string(e);
90 std::string id_zeta = "__lgm_" + qualifier_ + "_zeta_" + ore::data::to_string(d);
91 std::size_t H = addModelParameter(g_, modelParameters_, id_H, [p, T] { return p()->H(T); });
92 std::size_t zeta = addModelParameter(g_, modelParameters_, id_zeta, [p, t] { return p()->zeta(t); });
93 std::size_t P0T = addModelParameter(g_, modelParameters_, id_P0T, [p, discountCurve, T] {
94 return (discountCurve.empty() ? p()->termStructure() : discountCurve)->discount(T);
95 });
96 n = cg_mult(
97 g_, P0T,
99 cg_mult(g_, cg_mult(g_, cg_const(g_, 0.5), zeta), cg_mult(g_, H, H))))));
100 }
101 return n;
102}
103
104/* Handles IborIndex and SwapIndex. Requires observation time t <= fixingDate */
105std::size_t LgmCG::fixing(const QuantLib::ext::shared_ptr<InterestRateIndex>& index, const Date& fixingDate, const Date& t,
106 const std::size_t x) const {
107
108 std::string id =
109 "__irFix_" + index->name() + "_" + ore::data::to_string(fixingDate) + "_" + ore::data::to_string(t);
110 std::size_t n;
111 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan); n == ComputationGraph::nan) {
112
113 // handle case where fixing is deterministic
114
115 Date today = Settings::instance().evaluationDate();
116 if (fixingDate <= today) {
117
118 // handle historical fixing
119
121 [index, fixingDate]() { return index->fixing(fixingDate); });
122
123 } else if (auto ibor = QuantLib::ext::dynamic_pointer_cast<IborIndex>(index)) {
124
125 // Ibor Index
126
127 Date d1 = std::max(t, ibor->valueDate(fixingDate));
128 Date d2 = ibor->maturityDate(d1);
129
130 Time dt = ibor->dayCounter().yearFraction(d1, d2);
131
132 std::size_t disc1 = reducedDiscountBond(t, d1, x, ibor->forwardingTermStructure(), "fwd_" + index->name());
133 std::size_t disc2 = reducedDiscountBond(t, d2, x, ibor->forwardingTermStructure(), "fwd_" + index->name());
134
135 n = cg_div(g_, cg_subtract(g_, cg_div(g_, disc1, disc2), cg_const(g_, 1.0)), cg_const(g_, dt));
136
137 } else {
138 QL_FAIL("LgmCG::fixing(): only ibor indices handled so far, index = " << index->name());
139 }
140 }
141 return n;
142}
143
144} // namespace ore::data
void setVariable(const std::string &name, const std::size_t node)
static std::size_t nan
QuantExt::ComputationGraph & g_
Definition: lgmcg.hpp:65
std::size_t numeraire(const Date &d, const std::size_t x, const Handle< YieldTermStructure > &discountCurve=Handle< YieldTermStructure >(), const std::string &discountCurveId="default") const
Definition: lgmcg.cpp:34
std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameters_
Definition: lgmcg.hpp:67
std::string qualifier_
Definition: lgmcg.hpp:64
std::function< QuantLib::ext::shared_ptr< IrLgm1fParametrization >()> p_
Definition: lgmcg.hpp:66
std::size_t reducedDiscountBond(const Date &d, Date e, const std::size_t x, const Handle< YieldTermStructure > &discountCurve=Handle< YieldTermStructure >(), const std::string &discountCurveId="default") const
Definition: lgmcg.cpp:73
std::size_t fixing(const QuantLib::ext::shared_ptr< InterestRateIndex > &index, const Date &fixingDate, const Date &t, const std::size_t x) const
Definition: lgmcg.cpp:105
std::size_t discountBond(const Date &d, const Date &e, const std::size_t x, const Handle< YieldTermStructure > &discountCurve=Handle< YieldTermStructure >(), const std::string &discountCurveId="default") const
Definition: lgmcg.cpp:60
computation graph based lgm model calculations
basis implementation for a script engine model
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
std::size_t cg_const(ComputationGraph &g, const double value)
std::size_t cg_subtract(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_exp(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_negative(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_mult(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_add(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_var(ComputationGraph &g, const std::string &name, const bool createIfNotExists)
std::size_t cg_div(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
std::size_t addModelParameter(ComputationGraph &g, std::vector< std::pair< std::size_t, std::function< double(void)> > > &m, const std::string &id, std::function< double(void)> f)
string conversion utilities