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

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

+ Inheritance diagram for GaussianCamCG:
+ Collaboration diagram for GaussianCamCG:

Public Member Functions

 GaussianCamCG (const Handle< CrossAssetModel > &cam, const Size paths, const std::vector< std::string > &currencies, const std::vector< Handle< YieldTermStructure > > &curves, const std::vector< Handle< Quote > > &fxSpots, 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 Size timeStepsPerYear=1, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig(), const std::vector< Size > &projectedStateProcessIndices={}, const std::vector< std::string > &conditionalExpectationModelStates={}, const bool sloppySimDates=false)
 
Type type () const override
 
const Date & referenceDate () const override
 
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 override
 
std::size_t fwdCompAvg (const bool isAvg, const std::string &indexInput, 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 override
 
QuantLib::Size size () const override
 
Real getDirectFxSpotT0 (const std::string &forCcy, const std::string &domCcy) const override
 
Real getDirectDiscountT0 (const Date &paydate, const std::string &currency) const override
 
- Public Member Functions inherited from 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)
 
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 getFutureBarrierProb (const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const override
 
void performCalculations () const override
 
std::size_t getIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
 
std::size_t getIrIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
 
std::size_t getInfIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
 
std::size_t getDiscount (const Size idx, const Date &s, const Date &t) const override
 
std::size_t getNumeraire (const Date &s) const override
 
std::size_t getFxSpot (const Size idx) const override
 
- Protected Member Functions inherited from ModelCGImpl
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 Handle< CrossAssetModelcam_
 
const std::vector< Handle< YieldTermStructure > > curves_
 
const std::vector< Handle< Quote > > fxSpots_
 
const Size timeStepsPerYear_
 
const std::vector< Size > projectedStateProcessIndices_
 
const bool sloppySimDates_
 
Date referenceDate_
 
std::set< Date > effectiveSimulationDates_
 
TimeGrid timeGrid_
 
std::vector< Size > positionInTimeGrid_
 
std::map< Date, std::vector< std::size_t > > underlyingPaths_
 
std::map< Date, std::vector< std::size_t > > irStates_
 
std::map< Date, std::vector< std::pair< std::size_t, std::size_t > > > infStates_
 
std::vector< Size > indexPositionInProcess_
 
std::vector< Size > infIndexPositionInProcess_
 
std::vector< Size > currencyPositionInProcess_
 
std::vector< Size > irIndexPositionInCam_
 
std::vector< Size > infIndexPositionInCam_
 
std::vector< Size > currencyPositionInCam_
 
std::vector< Size > eqIndexInCam_
 
bool conditionalExpectationUseIr_
 
bool conditionalExpectationUseInf_
 
bool conditionalExpectationUseAsset_
 
std::size_t underlyingPathsCgVersion_ = 0
 
const std::vector< QuantLib::Real > * injectedPathTimes_ = nullptr
 
const std::vector< std::vector< std::size_t > > * injectedPaths_ = nullptr
 
const std::vector< bool > * injectedPathIsRelevantTime_
 
bool injectedPathStickyCloseOutRun_
 
Size overwriteModelSize_ = Null<Size>()
 
- Protected Attributes inherited from ModelCGImpl
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_
 

Additional Inherited Members

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

Detailed Description

Definition at line 23 of file gaussiancamcg.hpp.

Constructor & Destructor Documentation

◆ GaussianCamCG()

GaussianCamCG ( const Handle< CrossAssetModel > &  cam,
const Size  paths,
const std::vector< std::string > &  currencies,
const std::vector< Handle< YieldTermStructure > > &  curves,
const std::vector< Handle< Quote > > &  fxSpots,
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 Size  timeStepsPerYear = 1,
const IborFallbackConfig iborFallbackConfig = IborFallbackConfig::defaultConfig(),
const std::vector< Size > &  projectedStateProcessIndices = {},
const std::vector< std::string > &  conditionalExpectationModelStates = {},
const bool  sloppySimDates = false 
)

Definition at line 26 of file gaussiancamcg.cpp.

35 : ModelCGImpl(curves.front()->dayCounter(), paths, currencies, irIndices, infIndices, indices, indexCurrencies,
36 simulationDates, iborFallbackConfig),
37 cam_(cam), curves_(curves), fxSpots_(fxSpots), timeStepsPerYear_(timeStepsPerYear),
38 projectedStateProcessIndices_(projectedStateProcessIndices), sloppySimDates_(sloppySimDates) {
39
40 // check inputs
41
42 QL_REQUIRE(!cam_.empty(), "model is empty");
43
44 // check restrictions on cam model (ir-fx with hw, bs and Euler disc only at the moment)
45
46 QL_REQUIRE(cam_->discretization() == CrossAssetModel::Discretization::Euler,
47 "GaussianCamCG requires discretization 'Euler'.");
48
49 QL_REQUIRE(cam_->components(CrossAssetModel::AssetType::IR) > 0, "GaussianCamCG: no IR component given");
50 QL_REQUIRE(cam_->components(CrossAssetModel::AssetType::INF) == 0, "GaussianCamCG: asset type INF not supported");
51 QL_REQUIRE(cam_->components(CrossAssetModel::AssetType::CR) == 0, "GaussianCamCG: asset type CR not supported");
52 QL_REQUIRE(cam_->components(CrossAssetModel::AssetType::EQ) == 0, "GaussianCamCG: asset type EQ not supported");
53 QL_REQUIRE(cam_->components(CrossAssetModel::AssetType::COM) == 0, "GaussianCamCG: asset type COM not supported");
54
55 for (Size i = 0; i < cam_->components(CrossAssetModel::AssetType::IR); ++i) {
56 QL_REQUIRE(cam_->modelType(CrossAssetModel::AssetType::IR, i) == CrossAssetModel::ModelType::LGM1F,
57 "GaussianCamCG: IR model type LGM required.");
58 }
59 for (Size i = 0; i < cam_->components(CrossAssetModel::AssetType::FX); ++i) {
60 QL_REQUIRE(cam_->modelType(CrossAssetModel::AssetType::FX, i) == CrossAssetModel::ModelType::BS,
61 "GaussianCamCG: FX model type BS required.");
62 }
63
64 // register with observables
65
66 for (auto const& o : curves_)
67 registerWith(o);
68 for (auto const& o : fxSpots_)
69 registerWith(o);
70
71 registerWith(cam_);
72
73 // set conditional expectation model states
74
76 conditionalExpectationModelStates.empty() ||
77 std::find(conditionalExpectationModelStates.begin(), conditionalExpectationModelStates.end(), "IR") !=
78 conditionalExpectationModelStates.end();
80 conditionalExpectationModelStates.empty() ||
81 std::find(conditionalExpectationModelStates.begin(), conditionalExpectationModelStates.end(), "INF") !=
82 conditionalExpectationModelStates.end();
84 conditionalExpectationModelStates.empty() ||
85 std::find(conditionalExpectationModelStates.begin(), conditionalExpectationModelStates.end(), "Asset") !=
86 conditionalExpectationModelStates.end();
87
88} // GaussianCamCG ctor
const Handle< CrossAssetModel > cam_
const std::vector< Handle< Quote > > fxSpots_
const std::vector< Size > projectedStateProcessIndices_
const std::vector< Handle< YieldTermStructure > > curves_
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: modelcgimpl.cpp:37

Member Function Documentation

◆ type()

Type type ( ) const
overridevirtual

Implements ModelCG.

Definition at line 38 of file gaussiancamcg.hpp.

◆ referenceDate()

const Date & referenceDate ( ) const
overridevirtual

Implements ModelCG.

Definition at line 90 of file gaussiancamcg.cpp.

90 {
91 calculate();
92 return referenceDate_;
93}
void calculate() const override
Definition: modelcg.hpp:142
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ npv()

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
overridevirtual

Implements ModelCG.

Definition at line 391 of file gaussiancamcg.cpp.

393 {
394
395 calculate();
396
397 QL_REQUIRE(!memSlot, "GuassiaCamCG::npv() with memSlot not yet supported!");
398
399 // if obsdate is today, take a plain expectation
400
401 if (obsdate == referenceDate()) {
402 return cg_conditionalExpectation(*g_, amount, {}, cg_const(*g_, 1.0));
403 }
404
405 // build the state
406
407 std::vector<std::size_t> state;
408
410 state.push_back(irStates_.at(sd).at(0));
411
412 if (addRegressor1 != ComputationGraph::nan)
413 state.push_back(addRegressor1);
414 if (addRegressor2 != ComputationGraph::nan)
415 state.push_back(addRegressor2);
416
417 // if the state is empty, return the plain expectation (no conditioning)
418
419 if (state.empty()) {
420 return cg_conditionalExpectation(*g_, amount, {}, cg_const(*g_, 1.0));
421 }
422
423 // if a memSlot is given and coefficients are stored, we use them
424 // TODO ...
425
426 // otherwise compute coefficients and store them if a memSlot is given
427 // TODO ...
428
429 // compute conditional expectation and return the result
430
431 return cg_conditionalExpectation(*g_, amount, state, filter);
432}
static std::size_t nan
const Date & referenceDate() const override
std::set< Date > effectiveSimulationDates_
std::map< Date, std::vector< std::size_t > > irStates_
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > g_
Definition: modelcg.hpp:149
SafeStack< Filter > filter
std::size_t cg_const(ComputationGraph &g, const double value)
std::size_t cg_conditionalExpectation(ComputationGraph &g, const std::size_t regressand, const std::vector< std::size_t > &regressor, const std::size_t filter, const std::string &label)
Date getSloppyDate(const Date &d, const bool sloppyDates, const std::set< Date > &dates)
+ Here is the call graph for this function:

◆ fwdCompAvg()

std::size_t fwdCompAvg ( const bool  isAvg,
const std::string &  indexInput,
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
overridevirtual

Implements ModelCG.

Definition at line 326 of file gaussiancamcg.cpp.

330 {
331 calculate();
332 QL_FAIL("GaussianCamCG::fwdCompAvg(): not implemented");
333}
+ Here is the call graph for this function:

◆ size()

Size size ( ) const
overridevirtual

Reimplemented from ModelCG.

Definition at line 95 of file gaussiancamcg.cpp.

95 {
96 if (injectedPathTimes_ == nullptr)
97 return ModelCG::size();
98 else {
100 }
101}
const std::vector< QuantLib::Real > * injectedPathTimes_
virtual QuantLib::Size size() const
Definition: modelcg.hpp:62
+ Here is the call graph for this function:

◆ getDirectFxSpotT0()

Real getDirectFxSpotT0 ( const std::string &  forCcy,
const std::string &  domCcy 
) const
overridevirtual

Implements ModelCG.

Definition at line 369 of file gaussiancamcg.cpp.

369 {
370 auto c1 = std::find(currencies_.begin(), currencies_.end(), forCcy);
371 auto c2 = std::find(currencies_.begin(), currencies_.end(), domCcy);
372 QL_REQUIRE(c1 != currencies_.end(), "currency " << forCcy << " not handled");
373 QL_REQUIRE(c2 != currencies_.end(), "currency " << domCcy << " not handled");
374 Size cidx1 = std::distance(currencies_.begin(), c1);
375 Size cidx2 = std::distance(currencies_.begin(), c2);
376 Real fx = 1.0;
377 if (cidx1 > 0)
378 fx *= fxSpots_.at(cidx1 - 1)->value();
379 if (cidx2 > 0)
380 fx /= fxSpots_.at(cidx2 - 1)->value();
381 return fx;
382}
const std::vector< std::string > currencies_

◆ getDirectDiscountT0()

Real getDirectDiscountT0 ( const Date &  paydate,
const std::string &  currency 
) const
overridevirtual

Implements ModelCG.

Definition at line 384 of file gaussiancamcg.cpp.

384 {
385 auto c = std::find(currencies_.begin(), currencies_.end(), currency);
386 QL_REQUIRE(c != currencies_.end(), "currency " << currency << " not handled");
387 Size cidx = std::distance(currencies_.begin(), c);
388 return curves_.at(cidx)->discount(paydate);
389}

◆ 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
overrideprotectedvirtual

Implements ModelCGImpl.

Definition at line 55 of file gaussiancamcg.hpp.

56 {
57 QL_FAIL("getFutureBarrierProb not implemented by GaussianCamCG");
58 }

◆ performCalculations()

void performCalculations ( ) const
overrideprotected

Definition at line 103 of file gaussiancamcg.cpp.

103 {
104
105 // needed for base class performCalculations()
106
107 referenceDate_ = curves_.front()->referenceDate();
108
109 // update cg version if necessary (eval date changed)
110
112
113 // if cg version has changed => update time grid related members and clear paths, so that they
114 // are populated in derived classes
115
117
118 // set up eff sim dates and time grid
119
122 for (auto const& d : simulationDates_) {
123 if (d >= referenceDate())
125 }
126
127 std::vector<Real> times;
128 for (auto const& d : effectiveSimulationDates_) {
129 times.push_back(curves_.front()->timeFromReference(d));
130 }
131
132 Size steps = std::max(std::lround(timeStepsPerYear_ * times.back() + 0.5), 1l);
133 timeGrid_ = TimeGrid(times.begin(), times.end(), steps);
134
135 positionInTimeGrid_.resize(times.size());
136 for (Size i = 0; i < positionInTimeGrid_.size(); ++i)
137 positionInTimeGrid_[i] = timeGrid_.index(times[i]);
138
139 // clear underlying paths
140
141 underlyingPaths_.clear();
143 }
144
145 // noting to do if underlying paths are populated
146
147 if (!underlyingPaths_.empty())
148 return;
149
150 // exit if there are no future simulation dates (i.e. only the reference date)
151
152 if (effectiveSimulationDates_.size() == 1)
153 return;
154
155 // init underlying path where we map a date to a node representing the path value
156
157 for (auto const& d : effectiveSimulationDates_) {
158 underlyingPaths_[d] = std::vector<std::size_t>(indices_.size(), ComputationGraph::nan);
159 irStates_[d] = std::vector<std::size_t>(currencies_.size(), ComputationGraph::nan);
160 infStates_[d] = std::vector<std::pair<std::size_t, std::size_t>>(
162 }
163
164 // populate index mappings
165
168 for (Size i = 0; i < currencies_.size(); ++i) {
170 cam_->pIdx(CrossAssetModel::AssetType::IR, cam_->ccyIndex(parseCurrency(currencies_[i]))));
171 currencyPositionInCam_.push_back(
172 cam_->idx(CrossAssetModel::AssetType::IR, cam_->ccyIndex(parseCurrency(currencies_[i]))));
173 }
174
175 irIndexPositionInCam_.clear();
176 for (Size i = 0; i < irIndices_.size(); ++i) {
177 irIndexPositionInCam_.push_back(cam_->ccyIndex(irIndices_[i].second->currency()));
178 }
179
182 for (Size i = 0; i < infIndices_.size(); ++i) {
183 Size infIdx = cam_->infIndex(infIndices_[i].first.infName());
184 infIndexPositionInProcess_.push_back(cam_->pIdx(CrossAssetModel::AssetType::INF, infIdx));
185 infIndexPositionInCam_.push_back(infIdx);
186 }
187
189 for (Size i = 0; i < indices_.size(); ++i) {
190 if (indices_[i].isFx()) {
191 // FX
192 Size ccyIdx = cam_->ccyIndex(parseCurrency(indexCurrencies_[i]));
193 QL_REQUIRE(ccyIdx > 0, "fx index '" << indices_[i] << "' has unexpected for ccy = base ccy");
194 indexPositionInProcess_.push_back(cam_->pIdx(CrossAssetModel::AssetType::FX, ccyIdx - 1));
195 eqIndexInCam_.push_back(Null<Size>());
196 } else if (indices_[i].isEq()) {
197 // EQ
198 Size eqIdx = cam_->eqIndex(indices_[i].eq()->name());
199 indexPositionInProcess_.push_back(cam_->pIdx(CrossAssetModel::AssetType::EQ, eqIdx));
200 eqIndexInCam_.push_back(eqIdx);
201 } else {
202 QL_FAIL("index '" << indices_[i].name() << "' expected to be FX or EQ");
203 }
204 }
205
206 // set the required random variables to evolve the stochastic process
207
208 randomVariates_ = std::vector<std::vector<std::size_t>>(cam_->brownians() + cam_->auxBrownians(),
209 std::vector<std::size_t>(timeGrid_.size() - 1));
210 for (Size j = 0; j < cam_->brownians() + cam_->auxBrownians(); ++j) {
211 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
212 randomVariates_[j][i] = cg_insert(*g_);
213 }
214 }
215
216 // evolve the stochastic process, for now we only evolve IR LGM process #0 as a PoC (!)
217
218 auto cam(cam_);
219
220 QL_REQUIRE(timeGrid_.size() == effectiveSimulationDates_.size(),
221 "GaussianCamCG: time grid size ("
222 << timeGrid_.size() << ") does not match effective simulation dates size ("
224 << "), this is currently not supported. The parameter timeStepsPerYear (" << timeStepsPerYear_
225 << ") shoudl be 1");
226
227 // Hull White with zero mean reversion (!) for testing numerical stability
228
229 // std::vector<std::size_t> sigma(timeGrid_.size() - 1);
230 // for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
231 // Real t = timeGrid_[i + 1];
232 // sigma[i] = addModelParameter("__hw_" + currencies_[0] + "_sigma_" + std::to_string(i),
233 // [cam, t] { return cam->irlgm1f(0)->hullWhiteSigma(t); });
234 // }
235
236 // std::size_t last_y = cg_const(*g_, 0.0);
237 // std::vector<std::size_t> y(timeGrid_.size() - 1);
238 // for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
239 // Real t = timeGrid_[i + 1];
240 // Real t0 = timeGrid_[i];
241 // y[i] = cg_add(*g_, last_y, cg_mult(*g_, cg_const(*g_, t - t0), cg_mult(*g_, sigma[i], sigma[i])));
242 // g_->setVariable("__hw_" + currencies_[0] + "_y_" +
243 // ore::data::to_string(*std::next(effectiveSimulationDates_.begin(), i + 1)),
244 // y[i]);
245 // last_y = y[i];
246 // }
247
248 // std::size_t irState = cg_const(*g_, 0.0);
249 // std::size_t I = cg_const(*g_, 0.0);
250 // std::size_t dateIndex = 1;
251 // for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
252 // Real t = timeGrid_[i + 1];
253 // Real t0 = timeGrid_[i];
254 // I = cg_add(*g_, I, cg_mult(*g_, cg_const(*g_, t - t0), irState));
255 // g_->setVariable("__hw_" + currencies_[0] + "_I_" +
256 // ore::data::to_string(*std::next(effectiveSimulationDates_.begin(), i + 1)),
257 // I);
258 // std::size_t drift = cg_mult(*g_, y[i], cg_const(*g_, t - t0));
259 // irState = cg_add(*g_, cg_add(*g_, irState, drift),
260 // cg_mult(*g_, cg_mult(*g_, cg_const(*g_, std::sqrt(t - t0)), sigma[i]), randomVariates_[0][i]));
261 // if (positionInTimeGrid_[dateIndex] == i + 1) {
262 // irStates_[*std::next(effectiveSimulationDates_.begin(), dateIndex)][0] = irState;
263 // ++dateIndex;
264 // }
265 // }
266 // QL_REQUIRE(dateIndex == effectiveSimulationDates_.size(),
267 // "GaussianCamCG:internal error, did not populate all irState time steps.");
268
269 // LGM
270
271 std::size_t lastZeta = cg_const(*g_, 0.0);
272 std::vector<std::size_t> diffusion(timeGrid_.size() - 1);
273 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
274 std::string id = "__lgm_" + currencies_[0] + "_zeta_" +
275 ore::data::to_string(*std::next(effectiveSimulationDates_.begin(), i + 1));
276 Real t = timeGrid_[i + 1];
277 std::size_t zeta = addModelParameter(id, [cam, t] { return cam->irlgm1f(0)->zeta(t); });
278 diffusion[i] = cg_sqrt(*g_, cg_subtract(*g_, zeta, lastZeta));
279 lastZeta = zeta;
280 }
281
282 std::string id = "__z_0";
283 std::size_t irState;
284 irState = addModelParameter(id, [cam] { return cam->stateProcess()->initialValues()[0]; });
285 irStates_[*effectiveSimulationDates_.begin()][0] = irState;
286
287 std::size_t dateIndex = 1;
288 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
289 irState = cg_add(*g_, irState, cg_mult(*g_, diffusion[i], randomVariates_[0][i]));
290 if (positionInTimeGrid_[dateIndex] == i + 1) {
291 irStates_[*std::next(effectiveSimulationDates_.begin(), dateIndex)][0] = irState;
292 ++dateIndex;
293 }
294 }
295 QL_REQUIRE(dateIndex == effectiveSimulationDates_.size(),
296 "GaussianCamCG:internal error, did not populate all irState time steps.");
297}
std::map< Date, std::vector< std::size_t > > underlyingPaths_
std::size_t underlyingPathsCgVersion_
std::vector< Size > infIndexPositionInCam_
std::vector< Size > infIndexPositionInProcess_
std::vector< Size > indexPositionInProcess_
std::vector< Size > eqIndexInCam_
std::vector< Size > currencyPositionInProcess_
std::vector< Size > irIndexPositionInCam_
std::map< Date, std::vector< std::pair< std::size_t, std::size_t > > > infStates_
std::vector< Size > currencyPositionInCam_
std::vector< Size > positionInTimeGrid_
std::size_t addModelParameter(const std::string &id, std::function< double(void)> f) const
void performCalculations() const override
std::vector< std::vector< size_t > > randomVariates_
std::size_t cgVersion() const override
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
std::vector< IndexInfo > indices_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
const std::vector< std::string > indexCurrencies_
const std::set< Date > simulationDates_
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:290
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_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_sqrt(ComputationGraph &g, const std::size_t a, const std::string &label)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
std::vector< Size > steps
string name
+ Here is the call graph for this function:

◆ getIndexValue()

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

Implements ModelCGImpl.

Definition at line 299 of file gaussiancamcg.cpp.

299 {
300 QL_FAIL("GaussianCamCG::getIndexValue(): not implemented");
301}

◆ getIrIndexValue()

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

Implements ModelCGImpl.

Definition at line 303 of file gaussiancamcg.cpp.

303 {
304 Date fixingDate = d;
305 if (fwd != Null<Date>())
306 fixingDate = fwd;
307 // ensure a valid fixing date
308 fixingDate = irIndices_[indexNo].second->fixingCalendar().adjust(fixingDate);
309 Size currencyIdx = irIndexPositionInCam_[indexNo];
310 auto cam(cam_);
312 LgmCG lgmcg(
313 currencies_[currencyIdx], *g_, [cam, currencyIdx] { return cam->irlgm1f(currencyIdx); }, modelParameters_,
315 return lgmcg.fixing(irIndices_[indexNo].second, fixingDate, sd, irStates_.at(sd).at(currencyIdx));
316 // HwCG hwcg(
317 // currencies_[currencyIdx], *g_, [cam, currencyIdx] { return cam->irlgm1f(currencyIdx); }, modelParameters_,
318 // sloppySimDates_, effectiveSimulationDates_);
319 // return hwcg.fixing(irIndices_[indexNo].second, fixingDate, sd, irStates_.at(sd).at(currencyIdx));
320}
std::vector< std::pair< std::size_t, std::function< double(void)> > > modelParameters_
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
+ Here is the call graph for this function:

◆ getInfIndexValue()

std::size_t getInfIndexValue ( const Size  indexNo,
const Date &  d,
const Date &  fwd = Null<Date>() 
) const
overrideprotectedvirtual

Implements ModelCGImpl.

Definition at line 322 of file gaussiancamcg.cpp.

322 {
323 QL_FAIL("GaussianCamCG::getInfIndexValue(): not implemented");
324}

◆ getDiscount()

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

Implements ModelCGImpl.

Definition at line 335 of file gaussiancamcg.cpp.

335 {
336 auto cam(cam_);
337 Size cpidx = currencyPositionInCam_[idx];
339 LgmCG lgmcg(
340 currencies_[idx], *g_, [cam, cpidx] { return cam->irlgm1f(cpidx); }, modelParameters_, sloppySimDates_,
342 return lgmcg.discountBond(sd, t, irStates_.at(sd)[idx]);
343 // HwCG hwcg(
344 // currencies_[idx], *g_, [cam, cpidx] { return cam->irlgm1f(cpidx); }, modelParameters_, sloppySimDates_,
345 // effectiveSimulationDates_);
346 // return hwcg.discountBond(sd, t, irStates_.at(sd)[idx]);
347}
+ Here is the call graph for this function:

◆ getNumeraire()

std::size_t getNumeraire ( const Date &  s) const
overrideprotectedvirtual

Implements ModelCGImpl.

Definition at line 349 of file gaussiancamcg.cpp.

349 {
350 auto cam(cam_);
351 Size cpidx = currencyPositionInCam_[0];
353 LgmCG lgmcg(
354 currencies_[0], *g_, [cam, cpidx] { return cam->irlgm1f(cpidx); }, modelParameters_, sloppySimDates_,
356 return lgmcg.numeraire(sd, irStates_.at(sd)[0]);
357 // HwCG hwcg(
358 // currencies_[0], *g_, [cam, cpidx] { return cam->irlgm1f(cpidx); }, modelParameters_, sloppySimDates_,
359 // effectiveSimulationDates_);
360 // return hwcg.numeraire(sd, irStates_.at(sd)[0]);
361}
+ Here is the call graph for this function:

◆ getFxSpot()

std::size_t getFxSpot ( const Size  idx) const
overrideprotectedvirtual

Implements ModelCGImpl.

Definition at line 363 of file gaussiancamcg.cpp.

363 {
364 std::string id = "__fxspot_" + std::to_string(idx);
365 auto c = fxSpots_.at(idx);
366 return addModelParameter(id, [c] { return c->value(); });
367}
+ Here is the call graph for this function:

Member Data Documentation

◆ cam_

const Handle<CrossAssetModel> cam_
protected

Definition at line 69 of file gaussiancamcg.hpp.

◆ curves_

const std::vector<Handle<YieldTermStructure> > curves_
protected

Definition at line 70 of file gaussiancamcg.hpp.

◆ fxSpots_

const std::vector<Handle<Quote> > fxSpots_
protected

Definition at line 71 of file gaussiancamcg.hpp.

◆ timeStepsPerYear_

const Size timeStepsPerYear_
protected

Definition at line 72 of file gaussiancamcg.hpp.

◆ projectedStateProcessIndices_

const std::vector<Size> projectedStateProcessIndices_
protected

Definition at line 73 of file gaussiancamcg.hpp.

◆ sloppySimDates_

const bool sloppySimDates_
protected

Definition at line 74 of file gaussiancamcg.hpp.

◆ referenceDate_

Date referenceDate_
mutableprotected

Definition at line 77 of file gaussiancamcg.hpp.

◆ effectiveSimulationDates_

std::set<Date> effectiveSimulationDates_
mutableprotected

Definition at line 78 of file gaussiancamcg.hpp.

◆ timeGrid_

TimeGrid timeGrid_
mutableprotected

Definition at line 79 of file gaussiancamcg.hpp.

◆ positionInTimeGrid_

std::vector<Size> positionInTimeGrid_
mutableprotected

Definition at line 80 of file gaussiancamcg.hpp.

◆ underlyingPaths_

std::map<Date, std::vector<std::size_t> > underlyingPaths_
mutableprotected

Definition at line 81 of file gaussiancamcg.hpp.

◆ irStates_

std::map<Date, std::vector<std::size_t> > irStates_
mutableprotected

Definition at line 82 of file gaussiancamcg.hpp.

◆ infStates_

std::map<Date, std::vector<std::pair<std::size_t, std::size_t> > > infStates_
mutableprotected

Definition at line 84 of file gaussiancamcg.hpp.

◆ indexPositionInProcess_

std::vector<Size> indexPositionInProcess_
mutableprotected

Definition at line 85 of file gaussiancamcg.hpp.

◆ infIndexPositionInProcess_

std::vector<Size> infIndexPositionInProcess_
mutableprotected

Definition at line 86 of file gaussiancamcg.hpp.

◆ currencyPositionInProcess_

std::vector<Size> currencyPositionInProcess_
mutableprotected

Definition at line 87 of file gaussiancamcg.hpp.

◆ irIndexPositionInCam_

std::vector<Size> irIndexPositionInCam_
mutableprotected

Definition at line 88 of file gaussiancamcg.hpp.

◆ infIndexPositionInCam_

std::vector<Size> infIndexPositionInCam_
mutableprotected

Definition at line 89 of file gaussiancamcg.hpp.

◆ currencyPositionInCam_

std::vector<Size> currencyPositionInCam_
mutableprotected

Definition at line 90 of file gaussiancamcg.hpp.

◆ eqIndexInCam_

std::vector<Size> eqIndexInCam_
mutableprotected

Definition at line 91 of file gaussiancamcg.hpp.

◆ conditionalExpectationUseIr_

bool conditionalExpectationUseIr_
mutableprotected

Definition at line 92 of file gaussiancamcg.hpp.

◆ conditionalExpectationUseInf_

bool conditionalExpectationUseInf_
mutableprotected

Definition at line 93 of file gaussiancamcg.hpp.

◆ conditionalExpectationUseAsset_

bool conditionalExpectationUseAsset_
mutableprotected

Definition at line 94 of file gaussiancamcg.hpp.

◆ underlyingPathsCgVersion_

std::size_t underlyingPathsCgVersion_ = 0
mutableprotected

Definition at line 96 of file gaussiancamcg.hpp.

◆ injectedPathTimes_

const std::vector<QuantLib::Real>* injectedPathTimes_ = nullptr
protected

Definition at line 99 of file gaussiancamcg.hpp.

◆ injectedPaths_

const std::vector<std::vector<std::size_t> >* injectedPaths_ = nullptr
protected

Definition at line 100 of file gaussiancamcg.hpp.

◆ injectedPathIsRelevantTime_

const std::vector<bool>* injectedPathIsRelevantTime_
protected

Definition at line 101 of file gaussiancamcg.hpp.

◆ injectedPathStickyCloseOutRun_

bool injectedPathStickyCloseOutRun_
protected

Definition at line 102 of file gaussiancamcg.hpp.

◆ overwriteModelSize_

Size overwriteModelSize_ = Null<Size>()
protected

Definition at line 103 of file gaussiancamcg.hpp.