12#include <ql/math/comparison.hpp>
13#include <ql/math/matrixutilities/pseudosqrt.hpp>
14#include <ql/quotes/simplequote.hpp>
27 const Handle<CrossAssetModel>& cam,
const Size paths,
const std::vector<std::string>& currencies,
28 const std::vector<Handle<YieldTermStructure>>& curves,
const std::vector<Handle<Quote>>& fxSpots,
29 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
30 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
31 const std::vector<std::string>& indices,
const std::vector<std::string>& indexCurrencies,
32 const std::set<Date>& simulationDates,
const Size timeStepsPerYear,
const IborFallbackConfig& iborFallbackConfig,
33 const std::vector<Size>& projectedStateProcessIndices,
34 const std::vector<std::string>& conditionalExpectationModelStates,
const bool sloppySimDates)
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) {
42 QL_REQUIRE(!
cam_.empty(),
"model is empty");
46 QL_REQUIRE(
cam_->discretization() == CrossAssetModel::Discretization::Euler,
47 "GaussianCamCG requires discretization 'Euler'.");
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");
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.");
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.");
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();
127 std::vector<Real> times;
129 times.push_back(
curves_.front()->timeFromReference(d));
160 infStates_[d] = std::vector<std::pair<std::size_t, std::size_t>>(
176 for (Size i = 0; i <
irIndices_.size(); ++i) {
189 for (Size i = 0; i <
indices_.size(); ++i) {
193 QL_REQUIRE(ccyIdx > 0,
"fx index '" <<
indices_[i] <<
"' has unexpected for ccy = base ccy");
202 QL_FAIL(
"index '" <<
indices_[i].
name() <<
"' expected to be FX or EQ");
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) {
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_
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_" +
277 std::size_t zeta =
addModelParameter(
id, [cam, t] {
return cam->irlgm1f(0)->zeta(t); });
282 std::string
id =
"__z_0";
284 irState =
addModelParameter(
id, [cam] {
return cam->stateProcess()->initialValues()[0]; });
287 std::size_t dateIndex = 1;
288 for (Size i = 0; i <
timeGrid_.size() - 1; ++i) {
296 "GaussianCamCG:internal error, did not populate all irState time steps.");
300 QL_FAIL(
"GaussianCamCG::getIndexValue(): not implemented");
305 if (fwd != Null<Date>())
323 QL_FAIL(
"GaussianCamCG::getInfIndexValue(): not implemented");
327 const Date& start,
const Date& end,
const Real spread,
const Real gearing,
328 const Integer lookback,
const Natural rateCutoff,
const Natural fixingDays,
329 const bool includeSpread,
const Real cap,
const Real floor,
330 const bool nakedOption,
const bool localCapFloor)
const {
332 QL_FAIL(
"GaussianCamCG::fwdCompAvg(): not implemented");
364 std::string
id =
"__fxspot_" + std::to_string(idx);
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);
378 fx *=
fxSpots_.at(cidx1 - 1)->value();
380 fx /=
fxSpots_.at(cidx2 - 1)->value();
386 QL_REQUIRE(c !=
currencies_.end(),
"currency " << currency <<
" not handled");
388 return curves_.at(cidx)->discount(paydate);
392 const boost::optional<long>& memSlot,
const std::size_t addRegressor1,
393 const std::size_t addRegressor2)
const {
397 QL_REQUIRE(!memSlot,
"GuassiaCamCG::npv() with memSlot not yet supported!");
407 std::vector<std::size_t> state;
413 state.push_back(addRegressor1);
415 state.push_back(addRegressor2);
void performCalculations() const override
std::map< Date, std::vector< std::size_t > > underlyingPaths_
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
QuantLib::Size size() const override
std::size_t underlyingPathsCgVersion_
std::vector< Size > infIndexPositionInCam_
std::vector< Size > infIndexPositionInProcess_
std::size_t getNumeraire(const Date &s) const override
std::vector< Size > indexPositionInProcess_
std::vector< Size > eqIndexInCam_
std::size_t getFxSpot(const Size idx) const override
const bool sloppySimDates_
Real getDirectFxSpotT0(const std::string &forCcy, const std::string &domCcy) const override
std::size_t getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
const std::vector< QuantLib::Real > * injectedPathTimes_
const Date & referenceDate() const override
Real getDirectDiscountT0(const Date &paydate, const std::string ¤cy) const override
bool conditionalExpectationUseAsset_
std::vector< Size > currencyPositionInProcess_
std::vector< Size > irIndexPositionInCam_
std::map< Date, std::vector< std::pair< std::size_t, std::size_t > > > infStates_
bool conditionalExpectationUseIr_
GaussianCamCG(const Handle< CrossAssetModel > &cam, const Size paths, const std::vector< std::string > ¤cies, 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)
std::vector< Size > currencyPositionInCam_
const Handle< CrossAssetModel > cam_
std::set< Date > effectiveSimulationDates_
std::size_t getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
std::map< Date, std::vector< std::size_t > > irStates_
bool conditionalExpectationUseInf_
const std::vector< Handle< Quote > > fxSpots_
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
std::size_t getDiscount(const Size idx, const Date &s, const Date &t) const override
std::size_t getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
const Size timeStepsPerYear_
const std::vector< Handle< YieldTermStructure > > curves_
std::vector< Size > positionInTimeGrid_
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 fixing(const QuantLib::ext::shared_ptr< InterestRateIndex > &index, const Date &fixingDate, const Date &t, const std::size_t x) 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
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > g_
void calculate() const override
virtual QuantLib::Size size() const
std::size_t addModelParameter(const std::string &id, std::function< double(void)> f) const
void performCalculations() const override
const std::vector< std::string > currencies_
std::vector< std::vector< size_t > > randomVariates_
std::size_t cgVersion() const override
std::vector< std::pair< std::size_t, std::function< double(void)> > > modelParameters_
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_
SafeStack< Filter > filter
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Map text representations to QuantLib/QuantExt types.
computation graph based lgm model calculations
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_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_conditionalExpectation(ComputationGraph &g, const std::size_t regressand, const std::vector< std::size_t > ®ressor, const std::size_t filter, 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)
Date getSloppyDate(const Date &d, const bool sloppyDates, const std::set< Date > &dates)
Serializable Credit Default Swap.
std::vector< Size > steps
string conversion utilities