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

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

+ Inheritance diagram for ModelCGImpl:
+ Collaboration diagram for ModelCGImpl:

Public Member Functions

 ModelCGImpl (const DayCounter &dayCounter, const Size size, const std::vector< std::string > &currencies, const std::vector< std::pair< std::string, QuantLib::ext::shared_ptr< InterestRateIndex > > > &irIndices, const std::vector< std::pair< std::string, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > &infIndices, const std::vector< std::string > &indices, const std::vector< std::string > &indexCurrencies, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig)
 
const std::string & baseCcy () const override
 
std::size_t dt (const Date &d1, const Date &d2) const override
 
std::size_t pay (const std::size_t amount, const Date &obsdate, const Date &paydate, const std::string &currency) const override
 
std::size_t discount (const Date &obsdate, const Date &paydate, const std::string &currency) const override
 
std::size_t eval (const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
 
std::size_t fxSpotT0 (const std::string &forCcy, const std::string &domCcy) const override
 
std::size_t barrierProbability (const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const override
 
Real extractT0Result (const RandomVariable &value) const override
 
std::size_t cgVersion () const override
 
const std::vector< std::vector< std::size_t > > & randomVariates () const override
 
std::vector< std::pair< std::size_t, double > > modelParameters () const override
 
std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameterFunctors () const override
 
- Public Member Functions inherited from ModelCG
 ModelCG (const QuantLib::Size n)
 
virtual ~ModelCG ()
 
QuantLib::ext::shared_ptr< QuantExt::ComputationGraphcomputationGraph ()
 
virtual Type type () const =0
 
virtual QuantLib::Size size () const
 
virtual Size trainingSamples () const
 
virtual void toggleTrainingPaths () const
 
virtual const Date & referenceDate () const =0
 
virtual const std::string & baseCcy () const =0
 
virtual std::size_t dt (const Date &d1, const Date &d2) const
 
virtual std::size_t pay (const std::size_t amount, const Date &obsdate, const Date &paydate, const std::string &currency) const =0
 
virtual std::size_t discount (const Date &obsdate, const Date &paydate, const std::string &currency) const =0
 
virtual std::size_t npv (const std::size_t amount, const Date &obsdate, const std::size_t filter, const boost::optional< long > &memSlot, const std::size_t addRegressor1, const std::size_t addRegressor2) const =0
 
virtual std::size_t eval (const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingFixingAsNull=false, const bool ignoreTodaysFixing=false) const =0
 
virtual std::size_t fwdCompAvg (const bool isAvg, const std::string &index, const Date &obsdate, const Date &start, const Date &end, const Real spread, const Real gearing, const Integer lookback, const Natural rateCutoff, const Natural fixingDays, const bool includeSpread, const Real cap, const Real floor, const bool nakedOption, const bool localCapFloor) const =0
 
virtual std::size_t barrierProbability (const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const =0
 
virtual std::size_t fxSpotT0 (const std::string &forCcy, const std::string &domCcy) const =0
 
virtual Real extractT0Result (const QuantExt::RandomVariable &value) const =0
 
virtual void resetNPVMem ()
 
const std::map< std::string, boost::any > & additionalResults () const
 
virtual std::size_t cgVersion () const =0
 
virtual const std::vector< std::vector< std::size_t > > & randomVariates () const =0
 
virtual std::vector< std::pair< std::size_t, double > > modelParameters () const =0
 
virtual std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameterFunctors () const =0
 
virtual Real getDirectFxSpotT0 (const std::string &forCcy, const std::string &domCcy) const =0
 
virtual Real getDirectDiscountT0 (const Date &paydate, const std::string &currency) const =0
 
void calculate () const override
 

Protected Member Functions

virtual std::size_t getIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
 
virtual std::size_t getIrIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
 
virtual std::size_t getInfIndexValue (const Size indexNo, const Date &d, const Date &fwd) const =0
 
virtual std::size_t getDiscount (const Size idx, const Date &s, const Date &t) const =0
 
virtual std::size_t getNumeraire (const Date &s) const =0
 
virtual std::size_t getFxSpot (const Size idx) const =0
 
virtual std::size_t getFutureBarrierProb (const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const =0
 
std::size_t addModelParameter (const std::string &id, std::function< double(void)> f) const
 
void performCalculations () const override
 

Protected Attributes

const DayCounter dayCounter_
 
const std::vector< std::string > currencies_
 
const std::vector< std::string > indexCurrencies_
 
const std::set< Date > simulationDates_
 
const IborFallbackConfig iborFallbackConfig_
 
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
 
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
 
std::vector< IndexInfoindices_
 
std::vector< std::vector< size_t > > randomVariates_
 
std::vector< std::pair< std::size_t, std::function< double(void)> > > modelParameters_
 
- Protected Attributes inherited from ModelCG
std::map< std::string, boost::any > additionalResults_
 
QuantLib::ext::shared_ptr< QuantExt::ComputationGraphg_
 

Private Member Functions

std::size_t getInflationIndexFixing (const bool returnMissingFixingAsNull, const std::string &indexInput, const QuantLib::ext::shared_ptr< ZeroInflationIndex > &infIndex, const Size indexNo, const Date &limDate, const Date &obsdate, const Date &fwddate, const Date &baseDate) const
 

Private Attributes

std::size_t cgVersion_ = 0
 
QuantLib::Date cgEvalDate_ = Date()
 

Additional Inherited Members

- Public Types inherited from ModelCG
enum class  Type { MC , FD }
 

Detailed Description

Definition at line 45 of file modelcgimpl.hpp.

Constructor & Destructor Documentation

◆ ModelCGImpl()

ModelCGImpl ( const DayCounter &  dayCounter,
const Size  size,
const std::vector< std::string > &  currencies,
const std::vector< std::pair< std::string, QuantLib::ext::shared_ptr< InterestRateIndex > > > &  irIndices,
const std::vector< std::pair< std::string, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > &  infIndices,
const std::vector< std::string > &  indices,
const std::vector< std::string > &  indexCurrencies,
const std::set< Date > &  simulationDates,
const IborFallbackConfig iborFallbackConfig 
)

Definition at line 37 of file modelcgimpl.cpp.

42 : ModelCG(size), dayCounter_(dayCounter), currencies_(currencies), indexCurrencies_(indexCurrencies),
43 simulationDates_(simulationDates), iborFallbackConfig_(iborFallbackConfig) {
44
45 // populate index vectors
46
47 for (auto const& str : indices) {
48 indices_.push_back(IndexInfo(str));
49 }
50
51 for (auto const& p : irIndices) {
52 irIndices_.push_back(std::make_pair(IndexInfo(p.first), p.second));
53 }
54
55 for (auto const& p : infIndices) {
56 infIndices_.push_back(std::make_pair(IndexInfo(p.first), p.second));
57 }
58
59 // check consistency of inputs
60
61 QL_REQUIRE(indexCurrencies_.size() == indices_.size(), "mismatch of indexCurrencies (" << indexCurrencies_.size()
62 << ") and indices ("
63 << indices_.size() << ")");
64
65 for (auto const& c : currencies_)
66 QL_REQUIRE(!c.empty(), "empty currency string");
67
68 // look for fx indices, check consistency with currencies and index currencies vectors
69
70 for (Size i = 0; i < indices_.size(); ++i) {
71 if (indices_[i].isFx()) {
72 QL_REQUIRE(indices_[i].fx()->targetCurrency().code() == currencies_.front(),
73 "fx index domestic currency (" << indices_[i].fx()->targetCurrency().code()
74 << ") does not match base currency (" << currencies_.front()
75 << ")");
76 QL_REQUIRE(indices_[i].fx()->sourceCurrency().code() == indexCurrencies_[i],
77 "fx index foreign currency (" << indices_[i].fx()->sourceCurrency().code()
78 << ") does not match index currency (" << indexCurrencies_[i]);
79 auto ccy = std::find(currencies_.begin(), currencies_.end(), indexCurrencies_[i]);
80 QL_REQUIRE(ccy != currencies_.end(),
81 "fx index foreign currency (" << indexCurrencies_[i] << ") not found in model currencies");
82 }
83 }
84
85 // register with observables
86
87 for (auto const& i : irIndices_)
88 registerWith(i.second);
89
90 for (auto const& i : infIndices_)
91 registerWith(i.second);
92
93 for (auto const& i : indices_) {
94 if (i.isComm()) {
95 for (auto const& d : simulationDates_)
96 registerWith(i.index(d));
97 } else
98 registerWith(i.index());
99 }
100
101} // ModelCGImpl ctor
ModelCG(const QuantLib::Size n)
Definition: modelcg.cpp:26
virtual QuantLib::Size size() const
Definition: modelcg.hpp:62
const std::vector< std::string > currencies_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
std::vector< IndexInfo > indices_
const DayCounter dayCounter_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
const std::vector< std::string > indexCurrencies_
const IborFallbackConfig iborFallbackConfig_
const std::set< Date > simulationDates_

Member Function Documentation

◆ baseCcy()

const std::string & baseCcy ( ) const
overridevirtual

Implements ModelCG.

Definition at line 72 of file modelcgimpl.hpp.

72{ return currencies_.front(); }

◆ dt()

std::size_t dt ( const Date &  d1,
const Date &  d2 
) const
overridevirtual

Reimplemented from ModelCG.

Definition at line 103 of file modelcgimpl.cpp.

103 {
104 return cg_const(*g_, dayCounter_.yearFraction(d1, d2));
105}
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > g_
Definition: modelcg.hpp:149
std::size_t cg_const(ComputationGraph &g, const double value)
+ Here is the call graph for this function:

◆ pay()

std::size_t pay ( const std::size_t  amount,
const Date &  obsdate,
const Date &  paydate,
const std::string &  currency 
) const
overridevirtual

Implements ModelCG.

Definition at line 107 of file modelcgimpl.cpp.

108 {
109 calculate();
110
111 std::string id = "__pay_" + ore::data::to_string(obsdate) + "_" + ore::data::to_string(paydate) + "_" + currency;
112
113 std::size_t n;
114 if (n = cg_var(*g_, id, ComputationGraph::VarDoesntExist::Nan); n == ComputationGraph::nan) {
115
116 // result is as of max(obsdate, refDate) by definition of pay()
117
118 Date effectiveDate = std::max(obsdate, referenceDate());
119 auto c = std::find(currencies_.begin(), currencies_.end(), currency);
120 QL_REQUIRE(c != currencies_.end(), "currency " << currency << " not handled");
121 Size cidx = std::distance(currencies_.begin(), c);
122
123 // do we have a dynamic fx underlying to convert to base at the effective date?
124
125 std::size_t fxSpot = 0;
126 for (Size i = 0; i < indexCurrencies_.size(); ++i) {
127 if (indices_.at(i).isFx() && currency == indexCurrencies_[i]) {
128 fxSpot = getIndexValue(i, effectiveDate);
129 break;
130 }
131 }
132
133 // if no we use the zero vol fx spot at the effective date
134
135 if (fxSpot == 0) {
136 if (cidx > 0)
137 fxSpot =
138 cg_div(*g_, cg_mult(*g_, getFxSpot(cidx - 1), getDiscount(cidx, referenceDate(), effectiveDate)),
139 getDiscount(0, referenceDate(), effectiveDate));
140 else
141 fxSpot = cg_const(*g_, 1.0);
142 }
143
144 // discount from pay to obs date on ccy curve, convert to base ccy and divide by the numeraire
145
146 n = cg_mult(*g_, cg_div(*g_, getDiscount(cidx, effectiveDate, paydate), getNumeraire(effectiveDate)), fxSpot);
147 g_->setVariable(id, n);
148 }
149
150 return cg_mult(*g_, amount, n);
151}
static std::size_t nan
virtual const Date & referenceDate() const =0
void calculate() const override
Definition: modelcg.hpp:142
virtual std::size_t getFxSpot(const Size idx) const =0
virtual std::size_t getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
virtual std::size_t getDiscount(const Size idx, const Date &s, const Date &t) const =0
virtual std::size_t getNumeraire(const Date &s) const =0
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_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
+ Here is the call graph for this function:

◆ discount()

std::size_t discount ( const Date &  obsdate,
const Date &  paydate,
const std::string &  currency 
) const
overridevirtual

Implements ModelCG.

Definition at line 153 of file modelcgimpl.cpp.

153 {
154 calculate();
155 auto c = std::find(currencies_.begin(), currencies_.end(), currency);
156 QL_REQUIRE(c != currencies_.end(), "currency " << currency << " not handled");
157 Size cidx = std::distance(currencies_.begin(), c);
158 return getDiscount(cidx, obsdate, paydate);
159}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eval()

std::size_t eval ( const std::string &  index,
const Date &  obsdate,
const Date &  fwddate,
const bool  returnMissingMissingAsNull = false,
const bool  ignoreTodaysFixing = false 
) const
overridevirtual

Implements ModelCG.

Definition at line 197 of file modelcgimpl.cpp.

198 {
199 calculate();
200
201 std::string id = "__eval_" + indexInput + "_" + ore::data::to_string(obsdate) + "_" +
202 ore::data::to_string(fwddate) + "_" + (returnMissingFixingAsNull ? "1" : "0") + "_" +
203 (ignoreTodaysFixing ? "1" : "0");
204
205 if (std::size_t n = cg_var(*g_, id, ComputationGraph::VarDoesntExist::Nan); n != ComputationGraph::nan) {
206 return n;
207 }
208
209 std::string index = indexInput;
210 IndexInfo indexInfo(index);
211
212 // 1 handle inflation indices
213 QL_DEPRECATED_DISABLE_WARNING
214 if (indexInfo.isInf()) {
215 auto inf = std::find_if(infIndices_.begin(), infIndices_.end(), comp(indexInput));
216 QL_REQUIRE(inf != infIndices_.end(),
217 "ModelCGImpl::eval(): did not find inflation index '" << indexInput << "' in model index list.");
218 Date baseDate = inf->second->zeroInflationTermStructure()->baseDate();
219 Date effectiveFixingDate = fwddate != Null<Date>() ? fwddate : obsdate;
220 std::pair<Date, Date> lim = inflationPeriod(effectiveFixingDate, inf->second->frequency());
221 std::size_t indexStart =
222 getInflationIndexFixing(returnMissingFixingAsNull, indexInput, inf->second,
223 std::distance(infIndices_.begin(), inf), lim.first, obsdate, fwddate, baseDate);
224 // if the index is not interpolated we are done
225 if (!indexInfo.inf()->interpolated()) {
226 return indexStart;
227 }
228 // otherwise we need to get a second value and interpolate as in ZeroInflationIndex
229 std::size_t indexEnd = getInflationIndexFixing(returnMissingFixingAsNull, indexInput, inf->second,
230 std::distance(infIndices_.begin(), inf), lim.second + 1, obsdate,
231 fwddate, baseDate);
232 // this is not entirely correct, since we should use the days in the lagged period, but we don't know the lag
233 std::size_t n = cg_add(*g_, indexStart,
234 cg_mult(*g_, cg_subtract(*g_, indexEnd, indexStart),
235 cg_const(*g_, (static_cast<Real>(effectiveFixingDate - lim.first) /
236 static_cast<Real>(lim.second + 1 - lim.first)))));
237 g_->setVariable(id, n);
238 return n;
239 }
240 QL_DEPRECATED_ENABLE_WARNING
241 // 2 handle non-inflation indices
242
243 // 2a handle historical fixings and today's fixings (if given as a historical fixing)
244 // for fx indices try to get the fixing both from the straight and the inverse index
245
246 if (fwddate == Null<Date>()) {
247 if (obsdate < referenceDate() || (obsdate == referenceDate() && !ignoreTodaysFixing)) {
248 if (indexInfo.irIborFallback(iborFallbackConfig_, referenceDate())) {
249 // handle ibor fallback indices, they don't fit into the below treatment...
250 auto ir = std::find_if(irIndices_.begin(), irIndices_.end(), comp(indexInput));
251 if (ir != irIndices_.end()) {
252 std::size_t n =
253 cg_const(*g_, ir->second->fixing(ir->second->fixingCalendar().adjust(obsdate, Preceding)));
254 g_->setVariable(id, n);
255 return n;
256 } else {
257 QL_FAIL("ir (fallback ibor) index '" << indexInput
258 << "' not found in ir indices list, internal error.");
259 }
260 } else {
261 // handle all other cases than ibor fallback indices
262 QuantLib::ext::shared_ptr<Index> idx = indexInfo.index(obsdate);
263 Real fixing = Null<Real>();
264 try {
265 fixing = idx->fixing(idx->fixingCalendar().adjust(obsdate, Preceding));
266 } catch (...) {
267 }
268 if (fixing != Null<Real>()) {
269 std::size_t n = cg_const(*g_, fixing);
270 g_->setVariable(id, n);
271 return n;
272 } else {
273 // for dates < refDate we are stuck now
274 if (obsdate != referenceDate()) {
275 if (returnMissingFixingAsNull) {
276 std::size_t n = ComputationGraph::nan;
277 g_->setVariable(id, n);
278 return n;
279 } else {
280 QL_FAIL("missing "
281 << idx->name() << " fixing for " << QuantLib::io::iso_date(obsdate)
282 << " (adjusted fixing date = "
283 << QuantLib::io::iso_date(idx->fixingCalendar().adjust(obsdate, Preceding)) << ")");
284 }
285 }
286 }
287 }
288 }
289 } else {
290 // if fwd date is given, ensure we can project
291 QL_REQUIRE(obsdate >= referenceDate(), "if fwd date is given ("
292 << QuantLib::io::iso_date(fwddate) << "), obsdate ("
293 << QuantLib::io::iso_date(obsdate) << ") must be >= refdate ("
294 << QuantLib::io::iso_date(referenceDate()) << ")");
295 }
296
297 // 2b handle fixings >= today (and fwd fixings, in which case we know fwddate > obsdate >= refdate)
298
299 // 2b1 handle IR indices
300
301 if (indexInfo.isIr()) {
302 auto ir = std::find_if(irIndices_.begin(), irIndices_.end(), comp(indexInput));
303 if (ir != irIndices_.end()) {
304 std::size_t res = getIrIndexValue(std::distance(irIndices_.begin(), ir), obsdate, fwddate);
305 QL_REQUIRE(res != ComputationGraph::nan, "internal error: could not project "
306 << ir->second->name() << " fixing for (obsdate/fwddate) = ("
307 << QuantLib::io::iso_date(obsdate) << ","
308 << QuantLib::io::iso_date(fwddate) << ")");
309 g_->setVariable(id, res);
310 return res;
311 }
312 }
313
314 // 2b2 handle FX, EQ, COMM indices
315
316 // if we have an FX index, we "normalise" the tag by GENERIC (since it does not matter for projections)
317
318 if (indexInfo.isFx())
319 indexInfo = IndexInfo("FX-GENERIC-" + indexInfo.fx()->sourceCurrency().code() + "-" +
320 indexInfo.fx()->targetCurrency().code());
321
322 std::size_t res;
323 auto i = std::find(indices_.begin(), indices_.end(), indexInfo);
324 if (i != indices_.end()) {
325 // we have the index directly as an underlying
326 res = getIndexValue(std::distance(indices_.begin(), i), obsdate, fwddate);
327 } else {
328 // if not, we can only try something else for FX indices
329 QL_REQUIRE(indexInfo.isFx(), "ModelCGImpl::eval(): index " << index << " not handled");
330 // is it a trivial fx index (CCY-CCY) or can we triangulate it?
331 std::size_t fx1 = cg_const(*g_, 1.0), fx2 = cg_const(*g_, 1.0);
332 if (indexInfo.fx()->sourceCurrency() == indexInfo.fx()->targetCurrency()) {
333 res = fx1;
334 // no fwd correction required, if ccy1=ccy2 we have spot = fwd = 1
335 } else {
336 Size ind1 = Null<Size>(), ind2 = Null<Size>();
337 for (Size i = 0; i < indexCurrencies_.size(); ++i) {
338 if (indices_[i].isFx()) {
339 if (indexInfo.fx()->sourceCurrency().code() == indexCurrencies_[i])
340 ind1 = i;
341 if (indexInfo.fx()->targetCurrency().code() == indexCurrencies_[i])
342 ind2 = i;
343 }
344 }
345 if (ind1 != Null<Size>()) {
346 fx1 = getIndexValue(ind1, obsdate);
347 }
348 if (ind2 != Null<Size>()) {
349 fx2 = getIndexValue(ind2, obsdate);
350 }
351 res = cg_div(*g_, fx1, fx2);
352 if (fwddate != Null<Date>()) {
353 auto ind1 = std::find(currencies_.begin(), currencies_.end(), indexInfo.fx()->sourceCurrency().code());
354 auto ind2 = std::find(currencies_.begin(), currencies_.end(), indexInfo.fx()->targetCurrency().code());
355 QL_REQUIRE(ind1 != currencies_.end(), "currency " << indexInfo.fx()->sourceCurrency().code()
356 << " in index " << index << " not handled");
357 QL_REQUIRE(ind2 != currencies_.end(), "currency " << indexInfo.fx()->targetCurrency().code()
358 << " in index " << index << " not handled");
359 res = cg_mult(*g_, res,
360 cg_div(*g_, getDiscount(std::distance(currencies_.begin(), ind1), obsdate, fwddate),
361 getDiscount(std::distance(currencies_.begin(), ind2), obsdate, fwddate)));
362 }
363 }
364 }
365 g_->setVariable(id, res);
366 return res;
367}
std::size_t getInflationIndexFixing(const bool returnMissingFixingAsNull, const std::string &indexInput, const QuantLib::ext::shared_ptr< ZeroInflationIndex > &infIndex, const Size indexNo, const Date &limDate, const Date &obsdate, const Date &fwddate, const Date &baseDate) const
virtual std::size_t getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
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_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:

◆ fxSpotT0()

std::size_t fxSpotT0 ( const std::string &  forCcy,
const std::string &  domCcy 
) const
overridevirtual

Implements ModelCG.

Definition at line 369 of file modelcgimpl.cpp.

369 {
370 calculate();
371 std::string id = "__fxspott0_" + forCcy + "_" + domCcy;
372 std::size_t fx;
373 if (fx = cg_var(*g_, id, ComputationGraph::VarDoesntExist::Nan); fx == ComputationGraph::nan) {
374 auto c1 = std::find(currencies_.begin(), currencies_.end(), forCcy);
375 auto c2 = std::find(currencies_.begin(), currencies_.end(), domCcy);
376 QL_REQUIRE(c1 != currencies_.end(), "currency " << forCcy << " not handled");
377 QL_REQUIRE(c2 != currencies_.end(), "currency " << domCcy << " not handled");
378 Size cidx1 = std::distance(currencies_.begin(), c1);
379 Size cidx2 = std::distance(currencies_.begin(), c2);
380 std::size_t fx = cg_const(*g_, 1.0);
381 if (cidx1 > 0)
382 fx = cg_mult(*g_, fx, getFxSpot(cidx1 - 1));
383 if (cidx2 > 0)
384 fx = cg_div(*g_, fx, getFxSpot(cidx2 - 1));
385 g_->setVariable(id, fx);
386 }
387 return fx;
388}
+ Here is the call graph for this function:

◆ barrierProbability()

std::size_t barrierProbability ( const std::string &  index,
const Date &  obsdate1,
const Date &  obsdate2,
const std::size_t  barrier,
const bool  above 
) const
overridevirtual

Implements ModelCG.

Definition at line 390 of file modelcgimpl.cpp.

391 {
392
393 calculate();
394
395 // determine the fixing calendar (assume that for commodity this is the same for different futures)
396
397 IndexInfo ind(index);
398 auto qlIndex = ind.index(obsdate1);
399
400 // handle all dates < reference date here
401
402 std::size_t barrierHit = cg_const(*g_, 0.0);
403 Date d = obsdate1;
404 while (d < std::min(referenceDate(), obsdate2)) {
405 if (qlIndex->fixingCalendar().isBusinessDay(d)) {
406 std::size_t f = eval(index, d, Null<Date>(), true);
407 if (f != ComputationGraph::nan) {
408 if (above) {
409 barrierHit =
410 cg_min(*g_, cg_const(*g_, 1.0), cg_add(*g_, barrierHit, cg_indicatorGeq(*g_, f, barrier)));
411 } else {
412 barrierHit = cg_min(
413 *g_, cg_const(*g_, 1.0),
414 cg_add(*g_, barrierHit, cg_subtract(*g_, cg_const(*g_, 1.0), cg_indicatorGt(*g_, f, barrier))));
415 }
416 } else {
417 // lax check of historical fixings, since e.g. for equity underlyings
418 // we can't expect to get the actual fixing calendar from index info
419 // TODO can we improve this by using the correct calendars?
420 TLOG("ignore missing fixing for " << qlIndex->name() << " on " << QuantLib::io::iso_date(d)
421 << " in ModelCGImpl::barrierProbability()");
422 }
423 }
424 ++d;
425 }
426
427 if (obsdate2 < referenceDate()) {
428 return barrierHit;
429 }
430
431 // handle future part (call into derived classes, this is model dependent)
432
433 std::size_t futureBarrierHit =
434 getFutureBarrierProb(index, std::max<Date>(obsdate1, referenceDate()), obsdate2, barrier, above);
435
436 // combine historical and future part and return result
437
438 return cg_add(*g_, cg_mult(*g_, cg_subtract(*g_, cg_const(*g_, 1.0), barrierHit), futureBarrierHit), barrierHit);
439}
virtual std::size_t getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const =0
std::size_t eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
#define TLOG(text)
Logging Macro (Level = Data)
Definition: log.hpp:556
std::size_t cg_min(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_indicatorGeq(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_indicatorGt(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
+ Here is the call graph for this function:

◆ extractT0Result()

Real extractT0Result ( const RandomVariable value) const
overridevirtual

Implements ModelCG.

Definition at line 441 of file modelcgimpl.cpp.

441{ return expectation(value).at(0); }
SafeStack< ValueType > value
RandomVariable expectation(const RandomVariable &r)
Real at(const Size i) const
+ Here is the call graph for this function:

◆ cgVersion()

std::size_t cgVersion ( ) const
overridevirtual

Implements ModelCG.

Definition at line 453 of file modelcgimpl.cpp.

453 {
454 calculate();
455 return cgVersion_;
456}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randomVariates()

const std::vector< std::vector< std::size_t > > & randomVariates ( ) const
overridevirtual

Implements ModelCG.

Definition at line 458 of file modelcgimpl.cpp.

458 {
459 calculate();
460 return randomVariates_;
461}
std::vector< std::vector< size_t > > randomVariates_
+ Here is the call graph for this function:

◆ modelParameters()

std::vector< std::pair< std::size_t, double > > modelParameters ( ) const
overridevirtual

Implements ModelCG.

Definition at line 463 of file modelcgimpl.cpp.

463 {
464 calculate();
465 std::vector<std::pair<std::size_t, double>> res;
466 for (auto const& f : modelParameters_) {
467 res.push_back(std::make_pair(f.first, f.second()));
468 }
469 return res;
470}
std::vector< std::pair< std::size_t, std::function< double(void)> > > modelParameters_
+ Here is the call graph for this function:

◆ modelParameterFunctors()

std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameterFunctors ( ) const
overridevirtual

Implements ModelCG.

Definition at line 472 of file modelcgimpl.cpp.

472 {
473 return modelParameters_;
474}

◆ getIndexValue()

virtual std::size_t getIndexValue ( const Size  indexNo,
const Date &  d,
const Date &  fwd = Null< Date >() 
) const
protectedpure virtual

Implemented in BlackScholesCGBase, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ getIrIndexValue()

virtual std::size_t getIrIndexValue ( const Size  indexNo,
const Date &  d,
const Date &  fwd = Null< Date >() 
) const
protectedpure virtual

Implemented in BlackScholesCGBase, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ getInfIndexValue()

virtual std::size_t getInfIndexValue ( const Size  indexNo,
const Date &  d,
const Date &  fwd 
) const
protectedpure virtual

Implemented in BlackScholesCGBase, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ getDiscount()

virtual std::size_t getDiscount ( const Size  idx,
const Date &  s,
const Date &  t 
) const
protectedpure virtual

Implemented in BlackScholesCGBase, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ getNumeraire()

virtual std::size_t getNumeraire ( const Date &  s) const
protectedpure virtual

Implemented in BlackScholesCGBase, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ getFxSpot()

virtual std::size_t getFxSpot ( const Size  idx) const
protectedpure virtual

Implemented in BlackScholesCGBase, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ getFutureBarrierProb()

virtual std::size_t getFutureBarrierProb ( const std::string &  index,
const Date &  obsdate1,
const Date &  obsdate2,
const std::size_t  barrier,
const bool  above 
) const
protectedpure virtual

Implemented in BlackScholesCG, and GaussianCamCG.

+ Here is the caller graph for this function:

◆ addModelParameter()

std::size_t addModelParameter ( const std::string &  id,
std::function< double(void)>  f 
) const
protected

Definition at line 476 of file modelcgimpl.cpp.

476 {
477 return ::ore::data::addModelParameter(*g_, modelParameters_, id, f);
478}
+ Here is the caller graph for this function:

◆ performCalculations()

void performCalculations ( ) const
overrideprotected

Definition at line 443 of file modelcgimpl.cpp.

443 {
444 if (cgEvalDate_ != referenceDate()) {
445 ++cgVersion_;
447 randomVariates_.clear();
448 modelParameters_.clear();
449 g_->clear();
450 }
451}
QuantLib::Date cgEvalDate_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInflationIndexFixing()

std::size_t getInflationIndexFixing ( const bool  returnMissingFixingAsNull,
const std::string &  indexInput,
const QuantLib::ext::shared_ptr< ZeroInflationIndex > &  infIndex,
const Size  indexNo,
const Date &  limDate,
const Date &  obsdate,
const Date &  fwddate,
const Date &  baseDate 
) const
private

Definition at line 171 of file modelcgimpl.cpp.

174 {
175 std::size_t res;
176 Real f = infIndex->timeSeries()[limDate];
177 // we exclude historical fixings
178 // - which are "impossible" to know (limDate > refDate)
179 // - which have to be projected because a fwd date is given and they are later than the obsdate
180 if (f != Null<Real>() && !(limDate > referenceDate()) && (fwddate == Null<Date>() || limDate <= obsdate)) {
181 res = cg_const(*g_, f);
182 } else {
183 Date effectiveObsDate = std::min(obsdate, limDate);
184 if (effectiveObsDate >= baseDate) {
185 res = getInfIndexValue(indexNo, effectiveObsDate, limDate);
186 } else if (returnMissingFixingAsNull) {
188 } else {
189 QL_FAIL("missing " << indexInput << " fixing for " << QuantLib::io::iso_date(limDate) << " (obsdate="
190 << QuantLib::io::iso_date(obsdate) << ", fwddate=" << QuantLib::io::iso_date(fwddate)
191 << ", basedate=" << QuantLib::io::iso_date(baseDate) << ")");
192 }
193 }
194 return res;
195}
virtual std::size_t getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd) const =0
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ dayCounter_

const DayCounter dayCounter_
protected

Definition at line 112 of file modelcgimpl.hpp.

◆ currencies_

const std::vector<std::string> currencies_
protected

Definition at line 113 of file modelcgimpl.hpp.

◆ indexCurrencies_

const std::vector<std::string> indexCurrencies_
protected

Definition at line 114 of file modelcgimpl.hpp.

◆ simulationDates_

const std::set<Date> simulationDates_
protected

Definition at line 115 of file modelcgimpl.hpp.

◆ iborFallbackConfig_

const IborFallbackConfig iborFallbackConfig_
protected

Definition at line 116 of file modelcgimpl.hpp.

◆ irIndices_

std::vector<std::pair<IndexInfo, QuantLib::ext::shared_ptr<InterestRateIndex> > > irIndices_
protected

Definition at line 118 of file modelcgimpl.hpp.

◆ infIndices_

std::vector<std::pair<IndexInfo, QuantLib::ext::shared_ptr<ZeroInflationIndex> > > infIndices_
protected

Definition at line 119 of file modelcgimpl.hpp.

◆ indices_

std::vector<IndexInfo> indices_
protected

Definition at line 120 of file modelcgimpl.hpp.

◆ randomVariates_

std::vector<std::vector<size_t> > randomVariates_
mutableprotected

Definition at line 123 of file modelcgimpl.hpp.

◆ modelParameters_

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

Definition at line 124 of file modelcgimpl.hpp.

◆ cgVersion_

std::size_t cgVersion_ = 0
mutableprivate

Definition at line 133 of file modelcgimpl.hpp.

◆ cgEvalDate_

QuantLib::Date cgEvalDate_ = Date()
mutableprivate

Definition at line 134 of file modelcgimpl.hpp.