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

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

+ Inheritance diagram for LocalVol:
+ Collaboration diagram for LocalVol:

Public Member Functions

 LocalVol (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 Handle< BlackScholesModelWrapper > &model, const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > &correlations, const McParams &mcparams, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig())
 
 LocalVol (const Size paths, const std::string &currency, const Handle< YieldTermStructure > &curve, const std::string &index, const std::string &indexCurrency, const Handle< BlackScholesModelWrapper > &model, const McParams &mcparams, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig())
 
- Public Member Functions inherited from BlackScholesBase
 BlackScholesBase (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 Handle< BlackScholesModelWrapper > &model, const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > &correlations, const McParams &mcParams, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig)
 
 BlackScholesBase (const Size paths, const std::string &currency, const Handle< YieldTermStructure > &curve, const std::string &index, const std::string &indexCurrency, const Handle< BlackScholesModelWrapper > &model, const Model::McParams &mcParams, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig)
 
Type type () const override
 
const Date & referenceDate () 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
 
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
 
void releaseMemory () override
 
void resetNPVMem () override
 
void toggleTrainingPaths () const override
 
Size trainingSamples () const override
 
Size size () const override
 
- Public Member Functions inherited from ModelImpl
 ModelImpl (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
 
Real dt (const Date &d1, const Date &d2) const override
 
RandomVariable pay (const RandomVariable &amount, const Date &obsdate, const Date &paydate, const std::string &currency) const override
 
RandomVariable discount (const Date &obsdate, const Date &paydate, const std::string &currency) const override
 
RandomVariable eval (const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
 
Real fxSpotT0 (const std::string &forCcy, const std::string &domCcy) const override
 
RandomVariable barrierProbability (const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const override
 
Real extractT0Result (const RandomVariable &value) const override
 
- Public Member Functions inherited from Model
 Model (const Size n)
 
virtual ~Model ()
 
virtual Type type () const =0
 
virtual 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 Real dt (const Date &d1, const Date &d2) const
 
Real timeFromReference (const Date &d) const
 
virtual RandomVariable pay (const RandomVariable &amount, const Date &obsdate, const Date &paydate, const std::string &currency) const =0
 
virtual RandomVariable discount (const Date &obsdate, const Date &paydate, const std::string &currency) const =0
 
virtual RandomVariable npv (const RandomVariable &amount, const Date &obsdate, const Filter &filter, const boost::optional< long > &memSlot, const RandomVariable &addRegressor1, const RandomVariable &addRegressor2) const =0
 
virtual RandomVariable eval (const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingFixingAsNull=false, const bool ignoreTodaysFixing=false) const =0
 
virtual 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 =0
 
virtual RandomVariable barrierProbability (const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const =0
 
virtual Real fxSpotT0 (const std::string &forCcy, const std::string &domCcy) const =0
 
virtual Real extractT0Result (const RandomVariable &value) const =0
 
virtual void releaseMemory ()
 
virtual void resetNPVMem ()
 
const std::map< std::string, boost::any > & additionalResults () const
 

Private Member Functions

RandomVariable getFutureBarrierProb (const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const override
 
void performCalculations () const override
 
void populatePathValues (const Size nSamples, std::map< Date, std::vector< RandomVariable > > &paths, const QuantLib::ext::shared_ptr< MultiPathVariateGeneratorBase > &gen, const Matrix &correlation, const Matrix &sqrtCorr, const std::vector< Array > &deterministicDrift, const std::vector< Size > &eqComIdx, const std::vector< Real > &t, const std::vector< Real > &dt, const std::vector< Real > &sqrtdt) const
 

Additional Inherited Members

- Public Types inherited from Model
enum class  Type { MC , FD }
 
- Protected Member Functions inherited from BlackScholesBase
void performCalculations () const override
 
RandomVariable getIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
 
RandomVariable getIrIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
 
RandomVariable getInfIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
 
RandomVariable getDiscount (const Size idx, const Date &s, const Date &t) const override
 
RandomVariable getNumeraire (const Date &s) const override
 
Real getFxSpot (const Size idx) const override
 
Matrix getCorrelation () const
 
virtual RandomVariable getIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
 
virtual RandomVariable getIrIndexValue (const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
 
virtual RandomVariable getInfIndexValue (const Size indexNo, const Date &d, const Date &fwd) const =0
 
virtual RandomVariable getDiscount (const Size idx, const Date &s, const Date &t) const =0
 
virtual RandomVariable getNumeraire (const Date &s) const =0
 
virtual Real getFxSpot (const Size idx) const =0
 
virtual RandomVariable getFutureBarrierProb (const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const =0
 
- Protected Member Functions inherited from Model
void performCalculations () const override
 
- Protected Attributes inherited from BlackScholesBase
const std::vector< Handle< YieldTermStructure > > curves_
 
const std::vector< Handle< Quote > > fxSpots_
 
const Handle< BlackScholesModelWrappermodel_
 
const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > correlations_
 
const McParams mcParams_
 
const std::vector< Date > simulationDates_
 
Date referenceDate_
 
std::set< Date > effectiveSimulationDates_
 
TimeGrid timeGrid_
 
std::vector< Size > positionInTimeGrid_
 
std::map< Date, std::vector< RandomVariable > > underlyingPaths_
 
std::map< Date, std::vector< RandomVariable > > underlyingPathsTraining_
 
bool inTrainingPhase_ = false
 
std::map< long, std::tuple< Array, Size, Matrix > > storedRegressionModel_
 
- Protected Attributes inherited from ModelImpl
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_
 
- Protected Attributes inherited from Model
std::map< std::string, boost::any > additionalResults_
 

Detailed Description

Definition at line 33 of file localvol.hpp.

Constructor & Destructor Documentation

◆ LocalVol() [1/2]

LocalVol ( 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 Handle< BlackScholesModelWrapper > &  model,
const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > &  correlations,
const McParams mcparams,
const std::set< Date > &  simulationDates,
const IborFallbackConfig iborFallbackConfig = IborFallbackConfig::defaultConfig() 
)

Definition at line 41 of file localvol.cpp.

50 : BlackScholesBase(paths, currencies, curves, fxSpots, irIndices, infIndices, indices, indexCurrencies, model,
51 correlations, mcParams, simulationDates, iborFallbackConfig) {}
BlackScholesBase(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 Handle< BlackScholesModelWrapper > &model, const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > &correlations, const McParams &mcParams, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig)

◆ LocalVol() [2/2]

LocalVol ( const Size  paths,
const std::string &  currency,
const Handle< YieldTermStructure > &  curve,
const std::string &  index,
const std::string &  indexCurrency,
const Handle< BlackScholesModelWrapper > &  model,
const McParams mcparams,
const std::set< Date > &  simulationDates,
const IborFallbackConfig iborFallbackConfig = IborFallbackConfig::defaultConfig() 
)

Definition at line 34 of file localvol.cpp.

38 : LocalVol(paths, {currency}, {curve}, {}, {}, {}, {index}, {indexCurrency}, model, {}, mcParams, simulationDates,
39 iborFallbackConfig) {}
LocalVol(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 Handle< BlackScholesModelWrapper > &model, const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > &correlations, const McParams &mcparams, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig())
Definition: localvol.cpp:41

Member Function Documentation

◆ getFutureBarrierProb()

RandomVariable getFutureBarrierProb ( const std::string &  index,
const Date &  obsdate1,
const Date &  obsdate2,
const RandomVariable barrier,
const bool  above 
) const
overrideprivatevirtual

Implements ModelImpl.

Definition at line 216 of file localvol.cpp.

217 {
218 QL_FAIL("getFutureBarrierProb not implemented by LocalVol");
219}

◆ performCalculations()

void performCalculations ( ) const
overrideprivate

Definition at line 53 of file localvol.cpp.

53 {
54
56
57 // nothing to do if we do not have any indices
58
59 if (indices_.empty())
60 return;
61
62 // init underlying path where we map a date to a randomvariable representing the path values
63
64 for (auto const& d : effectiveSimulationDates_) {
65 underlyingPaths_[d] = std::vector<RandomVariable>(model_->processes().size(), RandomVariable(size(), 0.0));
66 if (trainingSamples() != Null<Size>())
68 std::vector<RandomVariable>(model_->processes().size(), RandomVariable(trainingSamples(), 0.0));
69 }
70
71 // compile the correlation matrix
72
73 Matrix correlation = getCorrelation();
74
75 // set reference date values, if there are no future simulation dates we are done
76
77 for (Size l = 0; l < indices_.size(); ++l) {
78 underlyingPaths_[*effectiveSimulationDates_.begin()][l].setAll(model_->processes()[l]->x0());
79 if (trainingSamples() != Null<Size>()) {
80 underlyingPathsTraining_[*effectiveSimulationDates_.begin()][l].setAll(model_->processes()[l]->x0());
81 }
82 }
83
84 if (effectiveSimulationDates_.size() == 1)
85 return;
86
87 // compute the sqrt correlation
88
89 Matrix sqrtCorr = pseudoSqrt(correlation, SalvagingAlgorithm::Spectral);
90
91 // precompute the deterministic part of the drift on each time step
92
93 std::vector<Array> deterministicDrift(timeGrid_.size() - 1, Array(indices_.size(), 0.0));
94
95 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
96 for (Size j = 0; j < indices_.size(); ++j) {
97 Real t0 = timeGrid_[i];
98 Real t1 = timeGrid_[i + 1];
99 deterministicDrift[i][j] = -std::log(model_->processes()[j]->riskFreeRate()->discount(t1) /
100 model_->processes()[j]->dividendYield()->discount(t1) /
101 (model_->processes()[j]->riskFreeRate()->discount(t0) /
102 model_->processes()[j]->dividendYield()->discount(t0)));
103 }
104 }
105
106 // precompute index for drift adjustment for eq / com indices that are not in base ccy
107
108 std::vector<Size> eqComIdx(indices_.size());
109 for (Size j = 0; j < indices_.size(); ++j) {
110 Size idx = Null<Size>();
111 if (!indices_[j].isFx()) {
112 // do we have an fx index with the desired currency?
113 for (Size jj = 0; jj < indices_.size(); ++jj) {
114 if (indices_[jj].isFx()) {
116 idx = jj;
117 }
118 }
119 }
120 eqComIdx[j] = idx;
121 }
122
123 // precompute some time related quantities
124
125 std::vector<Real> t(timeGrid_.size() - 1), dt(timeGrid_.size() - 1), sqrtdt(timeGrid_.size() - 1);
126 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
127 t[i] = timeGrid_[i];
128 dt[i] = timeGrid_[i + 1] - timeGrid_[i];
129 sqrtdt[i] = std::sqrt(dt[i]);
130 }
131
132 // evolve the process using correlated normal variates and set the underlying path values
133
138 correlation, sqrtCorr, deterministicDrift, eqComIdx, t, dt, sqrtdt);
139
140 if (trainingSamples() != Null<Size>()) {
143 timeGrid_.size() - 1, mcParams_.trainingSeed,
145 correlation, sqrtCorr, deterministicDrift, eqComIdx, t, dt, sqrtdt);
146 }
147
148} // initPaths()
void performCalculations() const override
Size size() const override
std::map< Date, std::vector< RandomVariable > > underlyingPathsTraining_
std::map< Date, std::vector< RandomVariable > > underlyingPaths_
std::set< Date > effectiveSimulationDates_
const Handle< BlackScholesModelWrapper > model_
Size trainingSamples() const override
void populatePathValues(const Size nSamples, std::map< Date, std::vector< RandomVariable > > &paths, const QuantLib::ext::shared_ptr< MultiPathVariateGeneratorBase > &gen, const Matrix &correlation, const Matrix &sqrtCorr, const std::vector< Array > &deterministicDrift, const std::vector< Size > &eqComIdx, const std::vector< Real > &t, const std::vector< Real > &dt, const std::vector< Real > &sqrtdt) const
Definition: localvol.cpp:150
std::vector< IndexInfo > indices_
Definition: modelimpl.hpp:114
Real dt(const Date &d1, const Date &d2) const override
Definition: modelimpl.hpp:73
const std::vector< std::string > indexCurrencies_
Definition: modelimpl.hpp:108
boost::shared_ptr< MultiPathVariateGeneratorBase > makeMultiPathVariateGenerator(const SequenceType s, const Size dimension, const Size timeSteps, const BigNatural seed, const SobolBrownianGenerator::Ordering ordering, const SobolRsg::DirectionIntegers directionIntegers)
QuantExt::SequenceType sequenceType
Definition: model.hpp:54
QuantLib::SobolBrownianGenerator::Ordering sobolOrdering
Definition: model.hpp:59
QuantExt::SequenceType trainingSequenceType
Definition: model.hpp:55
QuantLib::SobolRsg::DirectionIntegers sobolDirectionIntegers
Definition: model.hpp:60
+ Here is the call graph for this function:

◆ populatePathValues()

void populatePathValues ( const Size  nSamples,
std::map< Date, std::vector< RandomVariable > > &  paths,
const QuantLib::ext::shared_ptr< MultiPathVariateGeneratorBase > &  gen,
const Matrix &  correlation,
const Matrix &  sqrtCorr,
const std::vector< Array > &  deterministicDrift,
const std::vector< Size > &  eqComIdx,
const std::vector< Real > &  t,
const std::vector< Real > &  dt,
const std::vector< Real > &  sqrtdt 
) const
private

Definition at line 150 of file localvol.cpp.

155 {
156
157 Array stateDiff(indices_.size()), logState(indices_.size()), logState0(indices_.size());
158 for (Size j = 0; j < indices_.size(); ++j) {
159 logState0[j] = std::log(model_->processes()[j]->x0());
160 }
161
162 std::vector<std::vector<RandomVariable*>> rvs(indices_.size(),
163 std::vector<RandomVariable*>(effectiveSimulationDates_.size() - 1));
164 auto date = effectiveSimulationDates_.begin();
165 for (Size i = 0; i < effectiveSimulationDates_.size() - 1; ++i) {
166 ++date;
167 for (Size j = 0; j < indices_.size(); ++j) {
168 rvs[j][i] = &paths[*date][j];
169 rvs[j][i]->expand();
170 }
171 }
172
173 for (Size path = 0; path < size(); ++path) {
174 auto p = gen->next();
175 logState = logState0;
176 std::size_t date = 0;
177 auto pos = positionInTimeGrid_.begin();
178 ++pos;
179 // evolve the process on the refined time grid
180 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
181 for (Size j = 0; j < indices_.size(); ++j) {
182 // localVol might throw / return nan, inf, handle these cases here
183 // by setting the local vol to zero
184 Real volj = 0.0;
185 try {
186 volj = model_->processes()[j]->localVolatility()->localVol(t[i], std::exp(logState[j]));
187 } catch (...) {
188 }
189 if (!std::isfinite(volj))
190 volj = 0.0;
191 Real dw = 0;
192 for (Size k = 0; k < indices_.size(); ++k) {
193 dw += sqrtCorr[j][k] * p.value[i][k];
194 }
195 stateDiff[j] = volj * dw * sqrtdt[i] - 0.5 * volj * volj * dt[i];
196 // drift adjustment for eq / com indices that are not in base ccy
197 if (eqComIdx[j] != Null<Size>()) {
198 Real volIdx = model_->processes()[eqComIdx[j]]->localVolatility()->localVol(
199 t[i], std::exp(logState[eqComIdx[j]]));
200 stateDiff[j] -= correlation[eqComIdx[j]][j] * volIdx * volj * dt[i];
201 }
202 }
203 // update state with stateDiff from above and deterministic part of the drift
204 logState += stateDiff + deterministicDrift[i];
205 // on the effective simulation dates populate the underlying paths
206 if (i + 1 == *pos) {
207 for (Size j = 0; j < indices_.size(); ++j)
208 rvs[j][date]->data()[path] = std::exp(logState[j]);
209 ++date;
210 ++pos;
211 }
212 }
213 }
214} // populatePathValues()
std::vector< Size > positionInTimeGrid_
@ data
Definition: log.hpp:77
+ Here is the call graph for this function:
+ Here is the caller graph for this function: