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

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

+ Collaboration diagram for LgmCG:

Public Member Functions

 LgmCG (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, const 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, 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_
 
std::set< Date > effSimDates_
 

Detailed Description

Definition at line 36 of file lgmcg.hpp.

Constructor & Destructor Documentation

◆ LgmCG()

LgmCG ( 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 lgmcg.hpp.

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

Member Function Documentation

◆ parametrization()

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

Definition at line 45 of file lgmcg.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 lgmcg.cpp.

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

◆ discountBond()

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 at line 60 of file lgmcg.cpp.

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

◆ reducedDiscountBond()

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 at line 73 of file lgmcg.cpp.

75 {
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}
std::size_t cg_negative(ComputationGraph &g, const std::size_t a, const std::string &label)
+ Here is the call graph for this function:
+ Here is the caller 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 105 of file lgmcg.cpp.

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

Member Data Documentation

◆ qualifier_

std::string qualifier_
private

Definition at line 64 of file lgmcg.hpp.

◆ g_

Definition at line 65 of file lgmcg.hpp.

◆ p_

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

Definition at line 66 of file lgmcg.hpp.

◆ modelParameters_

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

Definition at line 67 of file lgmcg.hpp.

◆ effSimDates_

std::set<Date> effSimDates_
private

Definition at line 68 of file lgmcg.hpp.