Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
mcmultilegbaseengine.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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 mcmultilegbaseengine.hpp
20 \brief base MC engine for multileg (option) instruments
21*/
22
23#pragma once
24
31
32#include <ql/indexes/interestrateindex.hpp>
33#include <ql/instruments/swaption.hpp>
34#include <ql/methods/montecarlo/lsmbasissystem.hpp>
35
36namespace QuantExt {
37
38// statistics
39
40struct McEngineStats : public QuantLib::Singleton<McEngineStats> {
42 other_timer.start();
43 other_timer.stop();
44 path_timer.stop();
45 path_timer.start();
46 calc_timer.start();
47 calc_timer.stop();
48 }
49
50 void reset() {
51 other_timer.stop();
52 path_timer.stop();
53 calc_timer.stop();
54 }
55
56 boost::timer::cpu_timer other_timer;
57 boost::timer::cpu_timer path_timer;
58 boost::timer::cpu_timer calc_timer;
59};
60
62public:
64
65protected:
66 /*! The npv is computed in the model's base currency, discounting curves are taken from the model. simulationDates
67 are additional simulation dates. The cross asset model here must be consistent with the multi path that is the
68 input to AmcCalculator::simulatePath().
69
70 Current limitations:
71 - the parameter minimalObsDate is ignored, the corresponding optimization is not implemented yet
72 - pricingSamples are ignored, the npv from the training phase is used alway
73 */
75 const Handle<CrossAssetModel>& model, const SequenceType calibrationPathGenerator,
76 const SequenceType pricingPathGenerator, const Size calibrationSamples, const Size pricingSamples,
77 const Size calibrationSeed, const Size pricingSeed, const Size polynomOrder,
78 const LsmBasisSystem::PolynomialType polynomType, const SobolBrownianGenerator::Ordering ordering,
79 const SobolRsg::DirectionIntegers directionIntegers,
80 const std::vector<Handle<YieldTermStructure>>& discountCurves = std::vector<Handle<YieldTermStructure>>(),
81 const std::vector<Date>& simulationDates = std::vector<Date>(),
82 const std::vector<Size>& externalModelIndices = std::vector<Size>(), const bool minimalObsDate = true,
83 const RegressorModel regressorModel = RegressorModel::Simple,
84 const Real regressionVarianceCutoff = Null<Real>());
85
86 // run calibration and pricing (called from derived engines)
87 void calculate() const;
88
89 // return AmcCalculator instance (called from derived engines, calculate must be called before)
90 QuantLib::ext::shared_ptr<AmcCalculator> amcCalculator() const;
91
92 // input data from the derived pricing engines, to be set in these engines
93 mutable std::vector<Leg> leg_;
94 mutable std::vector<Currency> currency_;
95 mutable std::vector<bool> payer_;
96 mutable QuantLib::ext::shared_ptr<Exercise> exercise_; // may be empty, if underlying is the actual trade
97 mutable Settlement::Type optionSettlement_ = Settlement::Physical;
98 mutable bool includeSettlementDateFlows_ = false;
99
100 // data members
101 Handle<CrossAssetModel> model_;
105 LsmBasisSystem::PolynomialType polynomType_;
106 SobolBrownianGenerator::Ordering ordering_;
107 SobolRsg::DirectionIntegers directionIntegers_;
108 std::vector<Handle<YieldTermStructure>> discountCurves_;
109 std::vector<Date> simulationDates_;
110 std::vector<Size> externalModelIndices_;
114
115 // the generated amc calculator
116 mutable QuantLib::ext::shared_ptr<AmcCalculator> amcCalculator_;
117
118 // results, these are read from derived engines
120
121private:
122 static constexpr Real tinyTime = 1E-10;
123
124 // data structure storing info needed to generate the amount for a cashflow
126 Size legNo = Null<Size>(), cfNo = Null<Size>();
127 Real payTime = Null<Real>();
128 Real exIntoCriterionTime = Null<Real>();
129 Size payCcyIndex = Null<Size>();
130 bool payer = false;
131 std::vector<Real> simulationTimes;
132 std::vector<std::vector<Size>> modelIndices;
133 std::function<RandomVariable(const Size n, const std::vector<std::vector<const RandomVariable*>>&)>
135 };
136
137 // class representing a regression model for a certain observation (= xva, exercise) time
139 public:
140 RegressionModel() = default;
141 RegressionModel(const Real observationTime, const std::vector<CashflowInfo>& cashflowInfo,
142 const std::function<bool(std::size_t)>& cashflowRelevant, const CrossAssetModel& model,
143 const RegressorModel regressorModel, const Real regressionVarianceCutoff = Null<Real>());
144 // pathTimes must contain the observation time and the relevant cashflow simulation times
145 void train(const Size polynomOrder, const LsmBasisSystem::PolynomialType polynomType,
146 const RandomVariable& regressand, const std::vector<std::vector<const RandomVariable*>>& paths,
147 const std::set<Real>& pathTimes, const Filter& filter = Filter());
148 // pathTimes do not need to contain the observation time or the relevant cashflow simulation times
149 RandomVariable apply(const Array& initialState, const std::vector<std::vector<const RandomVariable*>>& paths,
150 const std::set<Real>& pathTimes) const;
151
152 private:
153 Real observationTime_ = Null<Real>();
154 Real regressionVarianceCutoff_ = Null<Real>();
155 bool isTrained_ = false;
156 std::set<std::pair<Real, Size>> regressorTimesModelIndices_;
158 std::vector<std::function<RandomVariable(const std::vector<const RandomVariable*>&)>> basisFns_;
160 };
161
162 // the implementation of the amc calculator interface used by the amc valuation engine
164 public:
165 MultiLegBaseAmcCalculator(const std::vector<Size>& externalModelIndices, const Settlement::Type settlement,
166 const std::set<Real>& exerciseXvaTimes, const std::set<Real>& exerciseTimes,
167 const std::set<Real>& xvaTimes,
168 const std::vector<McMultiLegBaseEngine::RegressionModel>& regModelUndDirty,
169 const std::vector<McMultiLegBaseEngine::RegressionModel>& regModelUndExInto,
170 const std::vector<McMultiLegBaseEngine::RegressionModel>& regModelContinuationValue,
171 const std::vector<McMultiLegBaseEngine::RegressionModel>& regModelOption,
172 const Real resultValue, const Array& initialState, const Currency& baseCurrency);
173
174 Currency npvCurrency() override { return baseCurrency_; }
175 std::vector<QuantExt::RandomVariable> simulatePath(const std::vector<QuantLib::Real>& pathTimes,
176 std::vector<std::vector<QuantExt::RandomVariable>>& paths,
177 const std::vector<size_t>& relevantPathIndex,
178 const std::vector<size_t>& relevantTimeIndex) override;
179
180 private:
181 std::vector<Size> externalModelIndices_;
182 Settlement::Type settlement_;
183 std::set<Real> exerciseXvaTimes_;
184 std::set<Real> exerciseTimes_;
185 std::set<Real> xvaTimes_;
186 std::vector<McMultiLegBaseEngine::RegressionModel> regModelUndDirty_;
187 std::vector<McMultiLegBaseEngine::RegressionModel> regModelUndExInto_;
188 std::vector<McMultiLegBaseEngine::RegressionModel> regModelContinuationValue_;
189 std::vector<McMultiLegBaseEngine::RegressionModel> regModelOption_;
193
194 std::vector<Filter> exercised_;
195 };
196
197 // convert a date to a time w.r.t. the valuation date
198 Real time(const Date& d) const;
199
200 // create the info for a given flow
201 CashflowInfo createCashflowInfo(QuantLib::ext::shared_ptr<CashFlow> flow, const Currency& payCcy, bool payer, Size legNo,
202 Size cfNo) const;
203
204 // get the index of a time in the given simulation times set
205 Size timeIndex(const Time t, const std::set<Real>& simulationTimes) const;
206
207 // compute a cashflow path value (in model base ccy)
208 RandomVariable cashflowPathValue(const CashflowInfo& cf, const std::vector<std::vector<RandomVariable>>& pathValues,
209 const std::set<Real>& simulationTimes) const;
210
211 // valuation date
212 mutable Date today_;
213
214 // lgm vectorised instances for each ccy
215 mutable std::vector<LgmVectorised> lgmVectorised_;
216};
217
218} // namespace QuantExt
interface for amc calculator
std::vector< McMultiLegBaseEngine::RegressionModel > regModelUndExInto_
std::vector< McMultiLegBaseEngine::RegressionModel > regModelOption_
std::vector< QuantExt::RandomVariable > simulatePath(const std::vector< QuantLib::Real > &pathTimes, std::vector< std::vector< QuantExt::RandomVariable > > &paths, const std::vector< size_t > &relevantPathIndex, const std::vector< size_t > &relevantTimeIndex) override
std::vector< McMultiLegBaseEngine::RegressionModel > regModelContinuationValue_
std::vector< McMultiLegBaseEngine::RegressionModel > regModelUndDirty_
std::set< std::pair< Real, Size > > regressorTimesModelIndices_
std::vector< std::function< RandomVariable(const std::vector< const RandomVariable * > &)> > basisFns_
void train(const Size polynomOrder, const LsmBasisSystem::PolynomialType polynomType, const RandomVariable &regressand, const std::vector< std::vector< const RandomVariable * > > &paths, const std::set< Real > &pathTimes, const Filter &filter=Filter())
RandomVariable apply(const Array &initialState, const std::vector< std::vector< const RandomVariable * > > &paths, const std::set< Real > &pathTimes) const
std::vector< LgmVectorised > lgmVectorised_
QuantLib::ext::shared_ptr< Exercise > exercise_
RandomVariable cashflowPathValue(const CashflowInfo &cf, const std::vector< std::vector< RandomVariable > > &pathValues, const std::set< Real > &simulationTimes) const
SobolBrownianGenerator::Ordering ordering_
SobolRsg::DirectionIntegers directionIntegers_
LsmBasisSystem::PolynomialType polynomType_
QuantLib::ext::shared_ptr< AmcCalculator > amcCalculator_
CashflowInfo createCashflowInfo(QuantLib::ext::shared_ptr< CashFlow > flow, const Currency &payCcy, bool payer, Size legNo, Size cfNo) const
Size timeIndex(const Time t, const std::set< Real > &simulationTimes) const
Real time(const Date &d) const
std::vector< Handle< YieldTermStructure > > discountCurves_
Handle< CrossAssetModel > model_
QuantLib::ext::shared_ptr< AmcCalculator > amcCalculator() const
cross asset model
FX index class.
vectorised lgm model calculations
multi leg option instrument
base class for multi path generators
boost::timer::cpu_timer calc_timer
boost::timer::cpu_timer path_timer
boost::timer::cpu_timer other_timer
std::vector< std::vector< Size > > modelIndices
std::function< RandomVariable(const Size n, const std::vector< std::vector< const RandomVariable * > > &)> amountCalculator