Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
blackscholesbase.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/blackscholesbase.hpp
20 \brief black scholes model base class for n underlyings (fx, equity or commodity)
21 \ingroup utilities
22*/
23
24#pragma once
25
27
30
31#include <ql/indexes/interestrateindex.hpp>
32#include <ql/processes/blackscholesprocess.hpp>
33#include <ql/timegrid.hpp>
34
35namespace ore {
36namespace data {
37
38/* This class is the basis for the BlackScholes and LocalVol model implementations */
40public:
41 /* For the constructor arguments see ModelImpl, plus:
42 - eq, com processes are given with arbitrary riskFreeRate() and dividendYield(), these two curves only define
43 the forward curve drift for each asset
44 - the base ccy is the first ccy in the currency vector, the fx spots are given as for-base, the ccy curves define
45 the fx forwards
46 - fx processes must be given w.r.t. the base ccy and consistent with the given fx spots and curves, but we do not
47 require fx processes for all currencies (but they are required, if an fx index is evaluated in eval())
48 - correlations are for index pair names and must be constant; if not given for a pair, we assume zero correlation
49 - regressionOrder is the regression order used to compute conditional expectations in npv()
50 */
52 const Size paths, const std::vector<std::string>& currencies,
53 const std::vector<Handle<YieldTermStructure>>& curves, const std::vector<Handle<Quote>>& fxSpots,
54 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
55 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
56 const std::vector<std::string>& indices, const std::vector<std::string>& indexCurrencies,
57 const Handle<BlackScholesModelWrapper>& model,
58 const std::map<std::pair<std::string, std::string>, Handle<QuantExt::CorrelationTermStructure>>& correlations,
59 const McParams& mcParams, const std::set<Date>& simulationDates, const IborFallbackConfig& iborFallbackConfig);
60
61 // ctor for single underlying
62 BlackScholesBase(const Size paths, const std::string& currency, const Handle<YieldTermStructure>& curve,
63 const std::string& index, const std::string& indexCurrency,
64 const Handle<BlackScholesModelWrapper>& model, const Model::McParams& mcParams,
65 const std::set<Date>& simulationDates, const IborFallbackConfig& iborFallbackConfig);
66
67 // Model interface implementation
68 Type type() const override { return Type::MC; }
69 const Date& referenceDate() const override;
70 RandomVariable npv(const RandomVariable& amount, const Date& obsdate, const Filter& filter,
71 const boost::optional<long>& memSlot, const RandomVariable& addRegressor1,
72 const RandomVariable& addRegressor2) const override;
73 RandomVariable fwdCompAvg(const bool isAvg, const std::string& index, const Date& obsdate, const Date& start,
74 const Date& end, const Real spread, const Real gearing, const Integer lookback,
75 const Natural rateCutoff, const Natural fixingDays, const bool includeSpread,
76 const Real cap, const Real floor, const bool nakedOption,
77 const bool localCapFloor) const override;
78 void releaseMemory() override;
79 void resetNPVMem() override;
80 void toggleTrainingPaths() const override;
81 Size trainingSamples() const override;
82 Size size() const override;
83
84protected:
85 // ModelImpl interface implementation (except initiModelState, this is done in the derived classes)
86 void performCalculations() const override;
87 RandomVariable getIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const override;
88 RandomVariable getIrIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const override;
89 RandomVariable getInfIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const override;
90 RandomVariable getDiscount(const Size idx, const Date& s, const Date& t) const override;
91 RandomVariable getNumeraire(const Date& s) const override;
92 Real getFxSpot(const Size idx) const override;
93
94 // helper function that constructs the correlation matrix
95 Matrix getCorrelation() const;
96
97 // input parameters
98 const std::vector<Handle<YieldTermStructure>> curves_;
99 const std::vector<Handle<Quote>> fxSpots_;
100 const Handle<BlackScholesModelWrapper> model_;
101 const std::map<std::pair<std::string, std::string>, Handle<QuantExt::CorrelationTermStructure>> correlations_;
103 const std::vector<Date> simulationDates_;
104
105 // these all except underlyingPaths_ are initialised when the interface functions above are called
106 mutable Date referenceDate_; // the model reference date
107 mutable std::set<Date> effectiveSimulationDates_; // the dates effectively simulated (including today)
108 mutable TimeGrid timeGrid_; // the (possibly refined) time grid for the simulation
109 mutable std::vector<Size> positionInTimeGrid_; // for each effective simulation date the index in the time grid
110 mutable std::map<Date, std::vector<RandomVariable>> underlyingPaths_; // per simulation date index states
111 mutable std::map<Date, std::vector<RandomVariable>> underlyingPathsTraining_; // ditto (training phase)
112 mutable bool inTrainingPhase_ = false; // are we currently using training paths?
113
114 // stored regression coefficients
115 mutable std::map<long, std::tuple<Array, Size, Matrix>> storedRegressionModel_;
116};
117
118} // namespace data
119} // namespace ore
RandomVariable getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
void performCalculations() const override
const std::vector< Date > simulationDates_
Size size() const override
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
const Date & referenceDate() const override
std::map< long, std::tuple< Array, Size, Matrix > > storedRegressionModel_
std::map< Date, std::vector< RandomVariable > > underlyingPathsTraining_
RandomVariable getNumeraire(const Date &s) const override
Type type() const override
std::map< Date, std::vector< RandomVariable > > underlyingPaths_
std::set< Date > effectiveSimulationDates_
const std::vector< Handle< Quote > > fxSpots_
const Handle< BlackScholesModelWrapper > model_
RandomVariable getDiscount(const Size idx, const Date &s, const Date &t) const override
RandomVariable getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
Size trainingSamples() const override
void toggleTrainingPaths() const override
const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > correlations_
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_
SafeStack< Filter > filter
@ data
Definition: log.hpp:77
basis implementation for a script engine model
Serializable Credit Default Swap.
Definition: namespaces.docs:23