Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
hwcg.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 HwCG::numeraire(const Date& d, const std::size_t x, const Handle<YieldTermStructure>& discountCurve,
35 const std::string& discountCurveId) const {
36 std::string id = "__hw_" + 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_);
41 Real t = p()->termStructure()->timeFromReference(d);
42 // Real ts = p()->termStructure()->timeFromReference(ds);
43 std::size_t I = cg_var(g_, "__hw_" + qualifier_ + "_I_" + ore::data::to_string(ds));
44
45 std::string id_P0t = "__dsc_" + ore::data::to_string(ds) + "_" + discountCurveId;
46 std::size_t P0t = addModelParameter(g_, modelParameters_, id_P0t, [p, discountCurve, t] {
47 return (discountCurve.empty() ? p()->termStructure() : discountCurve)->discount(t);
48 });
49 n = cg_div(g_, cg_exp(g_, I), P0t);
50 g_.setVariable(id, n);
51 }
52 return n;
53}
54
55std::size_t HwCG::discountBond(const Date& d, Date e, const std::size_t x,
56 const Handle<YieldTermStructure>& discountCurve,
57 const std::string& discountCurveId) const {
58 e = std::max(d, e);
59 std::string id =
60 "__hw_" + qualifier_ + "_P_" + ore::data::to_string(d) + "_" + ore::data::to_string(e) + "_" + discountCurveId;
61 std::size_t n;
62 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan), n == ComputationGraph::nan) {
63 auto p = p_;
66 Real t = p()->termStructure()->timeFromReference(d);
67 Real T = p()->termStructure()->timeFromReference(e);
68 Real ts = p()->termStructure()->timeFromReference(ds);
69 Real Ts = p()->termStructure()->timeFromReference(es);
70 std::string id_P0t = "__dsc_" + ore::data::to_string(ds) + "_" + discountCurveId;
71 std::string id_P0T = "__dsc_" + ore::data::to_string(es) + "_" + discountCurveId;
72
73 std::size_t y = cg_var(g_, "__hw_" + qualifier_ + "_y_" + ore::data::to_string(ds));
74 std::size_t g = cg_const(g_, Ts - ts);
75 std::size_t exponent = cg_negative(
76 g_, cg_add(g_, cg_mult(g_, g, x), cg_mult(g_, cg_const(g_, 0.5), cg_mult(g_, y, cg_mult(g_, g, g)))));
77
78 std::size_t P0t = addModelParameter(g_, modelParameters_, id_P0t, [p, discountCurve, t] {
79 return (discountCurve.empty() ? p()->termStructure() : discountCurve)->discount(t);
80 });
81 std::size_t P0T = addModelParameter(g_, modelParameters_, id_P0T, [p, discountCurve, T] {
82 return (discountCurve.empty() ? p()->termStructure() : discountCurve)->discount(T);
83 });
84
85 n = cg_mult(g_, cg_div(g_, P0T, P0t), cg_exp(g_, exponent));
86 g_.setVariable(id, n);
87 }
88 return n;
89}
90
91std::size_t HwCG::reducedDiscountBond(const Date& d, const Date& e, const std::size_t x,
92 const Handle<YieldTermStructure>& discountCurve,
93 const std::string& discountCurveId) const {
94 std::string id =
95 "__hw_" + qualifier_ + "_Pr_" + ore::data::to_string(d) + "_" + ore::data::to_string(e) + "_" + discountCurveId;
96 std::size_t n;
97 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan), n == ComputationGraph::nan) {
98 n = cg_div(g_, discountBond(d, e, x, discountCurve, discountCurveId),
99 numeraire(d, x, discountCurve, discountCurveId));
100 g_.setVariable(id, n);
101 }
102 return n;
103}
104
105/* Handles IborIndex and SwapIndex. Requires observation time t <= fixingDate */
106std::size_t HwCG::fixing(const QuantLib::ext::shared_ptr<InterestRateIndex>& index, const Date& fixingDate, const Date& t,
107 const std::size_t x) const {
108
109 std::string id =
110 "__irFix_" + index->name() + "_" + ore::data::to_string(fixingDate) + "_" + ore::data::to_string(t);
111 std::size_t n;
112 if (n = cg_var(g_, id, ComputationGraph::VarDoesntExist::Nan); n == ComputationGraph::nan) {
113
114 // handle case where fixing is deterministic
115
116 Date today = Settings::instance().evaluationDate();
117 if (fixingDate <= today) {
118
119 // handle historical fixing
120
122 [index, fixingDate]() { return index->fixing(fixingDate); });
123
124 } else if (auto ibor = QuantLib::ext::dynamic_pointer_cast<IborIndex>(index)) {
125
126 // Ibor Index
127
128 Date d1 = std::max(t, ibor->valueDate(fixingDate));
129 Date d2 = ibor->maturityDate(d1);
130
131 Time dt = ibor->dayCounter().yearFraction(d1, d2);
132
133 std::size_t disc1 = discountBond(t, d1, x, ibor->forwardingTermStructure(), "fwd_" + index->name());
134 std::size_t disc2 = discountBond(t, d2, x, ibor->forwardingTermStructure(), "fwd_" + index->name());
135
136 n = cg_div(g_, cg_subtract(g_, cg_div(g_, disc1, disc2), cg_const(g_, 1.0)), cg_const(g_, dt));
137
138 } else {
139 QL_FAIL("HwCG::fixing(): only ibor indices handled so far, index = " << index->name());
140 }
141 g_.setVariable(id, n);
142 }
143 return n;
144}
145
146} // namespace ore::data
void setVariable(const std::string &name, const std::size_t node)
static std::size_t nan
QuantExt::ComputationGraph & g_
Definition: hwcg.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: hwcg.cpp:34
std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameters_
Definition: hwcg.hpp:67
std::string qualifier_
Definition: hwcg.hpp:64
std::function< QuantLib::ext::shared_ptr< IrLgm1fParametrization >()> p_
Definition: hwcg.hpp:66
std::set< Date > effSimDates_
Definition: hwcg.hpp:69
std::size_t reducedDiscountBond(const Date &d, const Date &e, const std::size_t x, const Handle< YieldTermStructure > &discountCurve=Handle< YieldTermStructure >(), const std::string &discountCurveId="default") const
Definition: hwcg.cpp:91
std::size_t fixing(const QuantLib::ext::shared_ptr< InterestRateIndex > &index, const Date &fixingDate, const Date &t, const std::size_t x) const
Definition: hwcg.cpp:106
bool sloppySimDates_
Definition: hwcg.hpp:68
std::size_t discountBond(const Date &d, Date e, const std::size_t x, const Handle< YieldTermStructure > &discountCurve=Handle< YieldTermStructure >(), const std::string &discountCurveId="default") const
Definition: hwcg.cpp:55
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)
Date getSloppyDate(const Date &d, const bool sloppyDates, const std::set< Date > &dates)
string conversion utilities