Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
gaussiancam.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/scripting/models/gaussiancam.hpp
20 \brief gaussian cross asset model for ir, fx, eq, com
21 \ingroup utilities
22*/
23
24#pragma once
25
28
30
32
33#include <ql/termstructures/volatility/swaption/swaptionvolstructure.hpp>
34
35namespace ore {
36namespace data {
37
38class GaussianCam : public ModelImpl, public AmcModel {
39public:
40 /* analogue to ModelImpl, plus:
41 - cam: a handle to the underlying cross asset model
42 - the currencies, curves, fxSpots need to match those in the cam (FIXME, remove them from the ctor?)
43 - simulationDates are the dates on which indices can be observed
44 - regressionOrder is the regression order used to compute conditional expectations in npv()
45 - timeStepsPerYear time steps used for discretisation (overwritten by 1 if exact discretisation is used
46 - disc: choose exact or Euler discretisation of state process
47 */
48 GaussianCam(const Handle<CrossAssetModel>& cam, const Size paths, const std::vector<std::string>& currencies,
49 const std::vector<Handle<YieldTermStructure>>& curves, const std::vector<Handle<Quote>>& fxSpots,
50 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
51 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
52 const std::vector<std::string>& indices, const std::vector<std::string>& indexCurrencies,
53 const std::set<Date>& simulationDates, const McParams& mcParams, const Size timeStepsPerYear = 1,
54 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig(),
55 const std::vector<Size>& projectedStateProcessIndices = {},
56 const std::vector<std::string>& conditionalExpectationModelStates = {});
57
58 // Model interface implementation
59 Type type() const override { return Type::MC; }
60 const Date& referenceDate() const override;
61 Size size() const override;
62 RandomVariable npv(const RandomVariable& amount, const Date& obsdate, const Filter& filter,
63 const boost::optional<long>& memSlot, const RandomVariable& addRegressor1,
64 const RandomVariable& addRegressor2) const override;
65 RandomVariable fwdCompAvg(const bool isAvg, const std::string& index, const Date& obsdate, const Date& start,
66 const Date& end, const Real spread, const Real gearing, const Integer lookback,
67 const Natural rateCutoff, const Natural fixingDays, const bool includeSpread,
68 const Real cap, const Real floor, const bool nakedOption,
69 const bool localCapFloor) const override;
70 void releaseMemory() override;
71 void resetNPVMem() override;
72 void toggleTrainingPaths() const override;
73 Size trainingSamples() const override;
74
75 // AMCModel interface implementation
76 void injectPaths(const std::vector<QuantLib::Real>* pathTimes,
77 const std::vector<std::vector<QuantExt::RandomVariable>>* paths,
78 const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes) override;
79
80private:
81 // ModelImpl interface implementation
82 RandomVariable getFutureBarrierProb(const std::string& index, const Date& obsdate1, const Date& obsdate2,
83 const RandomVariable& barrier, const bool above) const override {
84 QL_FAIL("getFutureBarrierProb not implemented by GaussianCam");
85 }
86 // ModelImpl interface implementation
87 void performCalculations() const override;
88 RandomVariable getIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const override;
89 RandomVariable getIrIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const override;
90 RandomVariable getInfIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const override;
91 RandomVariable getDiscount(const Size idx, const Date& s, const Date& t) const override;
92 RandomVariable getNumeraire(const Date& s) const override;
93 Real getFxSpot(const Size idx) const override;
94
95 // same as getDiscount() above, but takes an arbitrary correction curve (for compounding on equity curves)
96 RandomVariable getDiscount(const Size idx, const Date& s, const Date& t,
97 const Handle<YieldTermStructure>& targetCurve) const;
98
99 void populatePathValues(const Size nSamples, std::map<Date, std::vector<RandomVariable>>& paths,
100 std::map<Date, std::vector<RandomVariable>>& irStates,
101 std::map<Date, std::vector<std::pair<RandomVariable, RandomVariable>>>& infStates,
102 const std::vector<Real>& times, const bool isTraining) const;
103 // input parameters
104 const Handle<CrossAssetModel> cam_;
105 const std::vector<Handle<YieldTermStructure>> curves_;
106 const std::vector<Handle<Quote>> fxSpots_;
109 const std::vector<Size> projectedStateProcessIndices_; // if data is injected via the AMCModel interface
110 const Real regressionVarianceCutoff_ = Null<Real>();
111
112 // computed values
113 mutable Date referenceDate_; // the model reference date
114 mutable std::set<Date> effectiveSimulationDates_; // the dates effectively simulated (including today)
115 mutable TimeGrid timeGrid_; // the (possibly refined) time grid for the simulation
116 mutable std::vector<Size> positionInTimeGrid_; // for each effective simulation date the index in the time grid
117 mutable std::map<Date, std::vector<RandomVariable>> underlyingPaths_; // per simulation date index states
118 mutable std::map<Date, std::vector<RandomVariable>> irStates_; // per sim date ir states for currencies_
119 mutable std::map<Date, std::vector<std::pair<RandomVariable, RandomVariable>>>
120 infStates_; // per sim date inf states dk (x,y) or jy (x,y)
121 mutable std::map<Date, std::vector<RandomVariable>> underlyingPathsTraining_; // ditto (training)
122 mutable std::map<Date, std::vector<RandomVariable>> irStatesTraining_; // ditto (training)
123 mutable std::map<Date, std::vector<std::pair<RandomVariable, RandomVariable>>>
124 infStatesTraining_; // ditto (training)
125 mutable bool inTrainingPhase_ = false; // are we currently using training paths?
126 mutable std::vector<Size> indexPositionInProcess_; // maps index no to position in state process
127 mutable std::vector<Size> infIndexPositionInProcess_; // maps inf index no to position in state process
128 mutable std::vector<Size> currencyPositionInProcess_; // maps currency no to position in state process
129 mutable std::vector<Size> irIndexPositionInCam_; // maps ir index no to currency idx in cam
130 mutable std::vector<Size> infIndexPositionInCam_; // maps inf index no to inf idx in cam
131 mutable std::vector<Size> currencyPositionInCam_; // maps currency no to position in cam parametrizations
132 mutable std::vector<Size> eqIndexInCam_; // maps index no to eq position in cam (or null, if not an eq index)
133 mutable std::vector<Size> comIndexInCam_; // maps index no to com position in cam (or null, if not a com index)
134 mutable bool conditionalExpectationUseIr_; // derived from input conditionalExpectationModelState
135 mutable bool conditionalExpectationUseInf_; // derived from input conditionalExpectationModelState
136 mutable bool conditionalExpectationUseAsset_; // derived from input conditionalExpectationModelState
137
138 // internal cache for ir index fixings
139 mutable std::map<std::tuple<Size, Date, Date>, RandomVariable> irIndexValueCache_;
140
141 // data when paths are injected via the AMCModel interface
142 const std::vector<QuantLib::Real>* injectedPathTimes_ = nullptr;
143 const std::vector<std::vector<QuantExt::RandomVariable>>* injectedPaths_ = nullptr;
144 const std::vector<size_t>* injectedPathRelevantPathIndexes_;
145 const std::vector<size_t>* injectedPathRelevantTimeIndexes_;
146 Size overwriteModelSize_ = Null<Size>();
147
148 // stored regression coefficients, state size (before possible transform) and (optional) coordinate transform
149 mutable std::map<long, std::tuple<Array, Size, Matrix>> storedRegressionModel_;
150};
151
152} // namespace data
153} // namespace ore
additional interface for amc enabled models
RandomVariable getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
void performCalculations() const override
void releaseMemory() override
RandomVariable getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const override
Definition: gaussiancam.hpp:82
std::map< Date, std::vector< std::pair< RandomVariable, RandomVariable > > > infStates_
const std::vector< size_t > * injectedPathRelevantPathIndexes_
Size size() const override
Definition: gaussiancam.cpp:93
std::vector< Size > infIndexPositionInCam_
RandomVariable getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
RandomVariable npv(const RandomVariable &amount, const Date &obsdate, const Filter &filter, const boost::optional< long > &memSlot, const RandomVariable &addRegressor1, const RandomVariable &addRegressor2) const override
Real getFxSpot(const Size idx) const override
std::map< std::tuple< Size, Date, Date >, RandomVariable > irIndexValueCache_
std::vector< Size > infIndexPositionInProcess_
std::vector< Size > indexPositionInProcess_
void populatePathValues(const Size nSamples, std::map< Date, std::vector< RandomVariable > > &paths, std::map< Date, std::vector< RandomVariable > > &irStates, std::map< Date, std::vector< std::pair< RandomVariable, RandomVariable > > > &infStates, const std::vector< Real > &times, const bool isTraining) const
std::vector< Size > eqIndexInCam_
const std::vector< std::vector< QuantExt::RandomVariable > > * injectedPaths_
const std::vector< QuantLib::Real > * injectedPathTimes_
const Date & referenceDate() const override
std::map< long, std::tuple< Array, Size, Matrix > > storedRegressionModel_
std::map< Date, std::vector< RandomVariable > > underlyingPathsTraining_
std::map< Date, std::vector< std::pair< RandomVariable, RandomVariable > > > infStatesTraining_
std::vector< Size > currencyPositionInProcess_
std::vector< Size > comIndexInCam_
RandomVariable getNumeraire(const Date &s) const override
std::vector< Size > irIndexPositionInCam_
const McParams mcParams_
std::vector< Size > currencyPositionInCam_
const Handle< CrossAssetModel > cam_
Type type() const override
Definition: gaussiancam.hpp:59
std::map< Date, std::vector< RandomVariable > > underlyingPaths_
std::set< Date > effectiveSimulationDates_
const Real regressionVarianceCutoff_
std::map< Date, std::vector< RandomVariable > > irStates_
const std::vector< Handle< Quote > > fxSpots_
std::map< Date, std::vector< RandomVariable > > irStatesTraining_
RandomVariable getDiscount(const Size idx, const Date &s, const Date &t) const override
const std::vector< Size > projectedStateProcessIndices_
RandomVariable getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
Size trainingSamples() const override
void resetNPVMem() override
const Size timeStepsPerYear_
void toggleTrainingPaths() const override
const std::vector< size_t > * injectedPathRelevantTimeIndexes_
void injectPaths(const std::vector< QuantLib::Real > *pathTimes, const std::vector< std::vector< QuantExt::RandomVariable > > *paths, const std::vector< size_t > *pathIndexes, const std::vector< size_t > *timeIndexes) override
RandomVariable 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 override
const std::vector< Handle< YieldTermStructure > > curves_
std::vector< Size > positionInTimeGrid_
static IborFallbackConfig defaultConfig()
SafeStack< Filter > filter
Build a cross asset model.
@ data
Definition: log.hpp:77
basis implementation for a script engine model
Serializable Credit Default Swap.
Definition: namespaces.docs:23