23#include <ql/exercise.hpp>
24#include <ql/math/comparison.hpp>
25#include <ql/math/matrixutilities/pseudosqrt.hpp>
26#include <ql/quotes/simplequote.hpp>
34LocalVol::LocalVol(
const Size paths,
const std::string& currency,
const Handle<YieldTermStructure>& curve,
35 const std::string& index,
const std::string& indexCurrency,
36 const Handle<BlackScholesModelWrapper>& model,
const McParams& mcParams,
38 :
LocalVol(paths, {currency}, {curve}, {}, {}, {}, {index}, {indexCurrency}, model, {}, mcParams, simulationDates,
39 iborFallbackConfig) {}
42 const Size paths,
const std::vector<std::string>& currencies,
const std::vector<Handle<YieldTermStructure>>& curves,
43 const std::vector<Handle<Quote>>& fxSpots,
44 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
45 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
46 const std::vector<std::string>& indices,
const std::vector<std::string>& indexCurrencies,
47 const Handle<BlackScholesModelWrapper>& model,
48 const std::map<std::pair<std::string, std::string>, Handle<QuantExt::CorrelationTermStructure>>& correlations,
50 :
BlackScholesBase(paths, currencies, curves, fxSpots, irIndices, infIndices, indices, indexCurrencies, model,
51 correlations, mcParams, simulationDates, iborFallbackConfig) {}
77 for (Size l = 0; l <
indices_.size(); ++l) {
89 Matrix sqrtCorr = pseudoSqrt(correlation, SalvagingAlgorithm::Spectral);
93 std::vector<Array> deterministicDrift(
timeGrid_.size() - 1, Array(
indices_.size(), 0.0));
95 for (Size i = 0; i <
timeGrid_.size() - 1; ++i) {
96 for (Size j = 0; j <
indices_.size(); ++j) {
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)));
108 std::vector<Size> eqComIdx(
indices_.size());
109 for (Size j = 0; j <
indices_.size(); ++j) {
110 Size idx = Null<Size>();
113 for (Size jj = 0; jj <
indices_.size(); ++jj) {
126 for (Size i = 0; i <
timeGrid_.size() - 1; ++i) {
129 sqrtdt[i] = std::sqrt(
dt[i]);
138 correlation, sqrtCorr, deterministicDrift, eqComIdx, t,
dt, sqrtdt);
145 correlation, sqrtCorr, deterministicDrift, eqComIdx, t,
dt, sqrtdt);
151 const QuantLib::ext::shared_ptr<MultiPathVariateGeneratorBase>& gen,
152 const Matrix& correlation,
const Matrix& sqrtCorr,
153 const std::vector<Array>& deterministicDrift,
const std::vector<Size>& eqComIdx,
154 const std::vector<Real>& t,
const std::vector<Real>& dt,
155 const std::vector<Real>& sqrtdt)
const {
158 for (Size j = 0; j <
indices_.size(); ++j) {
159 logState0[j] = std::log(
model_->processes()[j]->x0());
162 std::vector<std::vector<RandomVariable*>> rvs(
indices_.size(),
167 for (Size j = 0; j <
indices_.size(); ++j) {
168 rvs[j][i] = &paths[*date][j];
173 for (Size path = 0; path <
size(); ++path) {
174 auto p = gen->next();
175 logState = logState0;
176 std::size_t date = 0;
180 for (Size i = 0; i <
timeGrid_.size() - 1; ++i) {
181 for (Size j = 0; j <
indices_.size(); ++j) {
186 volj =
model_->processes()[j]->localVolatility()->localVol(t[i], std::exp(logState[j]));
189 if (!std::isfinite(volj))
192 for (Size k = 0; k <
indices_.size(); ++k) {
193 dw += sqrtCorr[j][k] * p.value[i][k];
195 stateDiff[j] = volj * dw * sqrtdt[i] - 0.5 * volj * volj *
dt[i];
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];
204 logState += stateDiff + deterministicDrift[i];
207 for (Size j = 0; j <
indices_.size(); ++j)
208 rvs[j][date]->
data()[path] = std::exp(logState[j]);
218 QL_FAIL(
"getFutureBarrierProb not implemented by LocalVol");
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
std::vector< Size > positionInTimeGrid_
Matrix getCorrelation() const
void performCalculations() const override
RandomVariable getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) 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
LocalVol(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 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())
std::vector< IndexInfo > indices_
Real dt(const Date &d1, const Date &d2) const override
const std::vector< std::string > indexCurrencies_
local vol model for n underlyings (fx, equity or commodity)
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)
Serializable Credit Default Swap.
QuantExt::SequenceType sequenceType
QuantLib::SobolBrownianGenerator::Ordering sobolOrdering
QuantExt::SequenceType trainingSequenceType
QuantLib::SobolRsg::DirectionIntegers sobolDirectionIntegers