Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
HwCG Class Reference

#include <ored/scripting/models/hwcg.hpp>

+ Collaboration diagram for HwCG:

Public Member Functions

 HwCG (const std::string &qualifier, QuantExt::ComputationGraph &g, const std::function< QuantLib::ext::shared_ptr< IrLgm1fParametrization >()> &p, std::vector< std::pair< std::size_t, std::function< double(void)> > > &modelParameters, const bool sloppySimDates=false, const std::set< Date > &effSimDates={})
 
QuantLib::ext::shared_ptr< IrLgm1fParametrizationparametrization () const
 
std::size_t numeraire (const Date &d, const std::size_t x, const Handle< YieldTermStructure > &discountCurve=Handle< YieldTermStructure >(), const std::string &discountCurveId="default") const
 
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
 
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
 
std::size_t fixing (const QuantLib::ext::shared_ptr< InterestRateIndex > &index, const Date &fixingDate, const Date &t, const std::size_t x) const
 

Private Attributes

std::string qualifier_
 
QuantExt::ComputationGraphg_
 
std::function< QuantLib::ext::shared_ptr< IrLgm1fParametrization >()> p_
 
std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameters_
 
bool sloppySimDates_
 
std::set< Date > effSimDates_
 

Detailed Description

Definition at line 36 of file hwcg.hpp.

Constructor & Destructor Documentation

◆ HwCG()

HwCG ( const std::string &  qualifier,
QuantExt::ComputationGraph g,
const std::function< QuantLib::ext::shared_ptr< IrLgm1fParametrization >()> &  p,
std::vector< std::pair< std::size_t, std::function< double(void)> > > &  modelParameters,
const bool  sloppySimDates = false,
const std::set< Date > &  effSimDates = {} 
)

Definition at line 38 of file hwcg.hpp.

41 {})
42 : qualifier_(qualifier), g_(g), p_(p), modelParameters_(modelParameters), sloppySimDates_(sloppySimDates),
43 effSimDates_(effSimDates) {}
QuantExt::ComputationGraph & g_
Definition: hwcg.hpp:65
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
bool sloppySimDates_
Definition: hwcg.hpp:68

Member Function Documentation

◆ parametrization()

QuantLib::ext::shared_ptr< IrLgm1fParametrization > parametrization ( ) const

Definition at line 45 of file hwcg.hpp.

45{ return p_(); }

◆ numeraire()

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 at line 34 of file hwcg.cpp.

35 {
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}
void setVariable(const std::string &name, const std::size_t node)
static std::size_t nan
std::size_t cg_exp(ComputationGraph &g, const std::size_t a, 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)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ discountBond()

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 at line 55 of file hwcg.cpp.

57 {
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}
std::size_t cg_const(ComputationGraph &g, const double value)
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)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reducedDiscountBond()

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 at line 91 of file hwcg.cpp.

93 {
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}
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::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
+ Here is the call graph for this function:

◆ fixing()

std::size_t fixing ( const QuantLib::ext::shared_ptr< InterestRateIndex > &  index,
const Date &  fixingDate,
const Date &  t,
const std::size_t  x 
) const

Definition at line 106 of file hwcg.cpp.

107 {
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}
std::size_t cg_subtract(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
+ Here is the call graph for this function:

Member Data Documentation

◆ qualifier_

std::string qualifier_
private

Definition at line 64 of file hwcg.hpp.

◆ g_

Definition at line 65 of file hwcg.hpp.

◆ p_

std::function<QuantLib::ext::shared_ptr<IrLgm1fParametrization>()> p_
private

Definition at line 66 of file hwcg.hpp.

◆ modelParameters_

std::vector<std::pair<std::size_t, std::function<double(void)> > >& modelParameters_
private

Definition at line 67 of file hwcg.hpp.

◆ sloppySimDates_

bool sloppySimDates_
private

Definition at line 68 of file hwcg.hpp.

◆ effSimDates_

std::set<Date> effSimDates_
private

Definition at line 69 of file hwcg.hpp.