30#include <ql/math/comparison.hpp>
31#include <ql/quotes/simplequote.hpp>
40 const Handle<YieldTermStructure>& curve,
const std::string& index,
41 const std::string& indexCurrency,
const Handle<BlackScholesModelWrapper>& model,
44 :
BlackScholesBase(paths, {currency}, {curve}, {}, {}, {}, {index}, {indexCurrency}, model, {}, mcParams,
45 simulationDates, iborFallbackConfig) {}
48 const Size paths,
const std::vector<std::string>& currencies,
const std::vector<Handle<YieldTermStructure>>& curves,
49 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 Handle<BlackScholesModelWrapper>& model,
54 const std::map<std::pair<std::string, std::string>, Handle<QuantExt::CorrelationTermStructure>>& correlations,
57 :
ModelImpl(curves.at(0)->dayCounter(), paths, currencies, irIndices, infIndices, indices, indexCurrencies,
58 simulationDates, iborFallbackConfig),
59 curves_(curves), fxSpots_(fxSpots),
model_(model),
correlations_(correlations), mcParams_(mcParams) {
63 QL_REQUIRE(!
model_.empty(),
"model is empty");
64 QL_REQUIRE(!
curves_.empty(),
"no curves given");
66 <<
") does not match number of curves ("
69 "number of currencies (" <<
currencies_.size() <<
") does not match number of fx spots ("
73 "mismatch of processes size (" <<
model_->processes().size() <<
") and number of indices ("
81 registerWith(o.second);
89 for (Size i = 0; i <
indices_.size(); ++i)
90 correlation[i][i] = 1.0;
92 IndexInfo inf1(c.first.first), inf2(c.first.second);
97 Size i1 = std::distance(
indices_.begin(), ind1);
98 Size i2 = std::distance(
indices_.begin(), ind2);
99 correlation[i1][i2] = correlation[i2][i1] = c.second->correlation(0.0);
102 DLOG(
"BlackScholesBase correlation matrix:");
115 "BlackScholesBase::performCalculations(): state inTrainingPhase should be false, this was "
116 "not resetted appropriately.");
124 std::vector<Real> times;
141 Date expiry =
indices_[indexNo].comm(d)->expiryDate();
143 if (expiry != Date())
148 effFwd = std::max(effFwd, d);
153 if (effFwd != Null<Date>()) {
154 auto p =
model_->processes().at(indexNo);
155 res *=
RandomVariable(
size(), p->dividendYield()->discount(effFwd) / p->dividendYield()->discount(d) /
156 (p->riskFreeRate()->discount(effFwd) / p->riskFreeRate()->discount(d)));
162 Date effFixingDate = d;
163 if (fwd != Null<Date>())
166 effFixingDate =
irIndices_.at(indexNo).second->fixingCalendar().adjust(effFixingDate);
171 Date effFixingDate = d;
172 if (fwd != Null<Date>())
179 comp(
const std::string& indexInput) :
indexInput_(indexInput) {}
180 template <
typename T>
bool operator()(
const std::pair<
IndexInfo, QuantLib::ext::shared_ptr<T>>& p)
const {
188 const Date& start,
const Date& end,
const Real spread,
const Real gearing,
189 const Integer lookback,
const Natural rateCutoff,
const Natural fixingDays,
190 const bool includeSpread,
const Real cap,
const Real floor,
191 const bool nakedOption,
const bool localCapFloor)
const {
195 "BlackScholesBase::fwdCompAvg(): did not find ir index " << indexInput <<
" - this is unexpected.");
196 auto on = QuantLib::ext::dynamic_pointer_cast<OvernightIndex>(index->second);
197 QL_REQUIRE(on,
"BlackScholesBase::fwdCompAvg(): expected on index for " << indexInput);
199 QL_REQUIRE(cap > 999998.0 && floor < -999998.0,
200 "BlackScholesBase:fwdCompAvg(): cap (" << cap <<
") / floor (" << floor <<
") not supported");
201 QuantLib::ext::shared_ptr<QuantLib::FloatingRateCoupon> coupon;
202 QuantLib::ext::shared_ptr<QuantLib::FloatingRateCouponPricer> pricer;
204 coupon = QuantLib::ext::make_shared<QuantExt::AverageONIndexedCoupon>(
205 end, 1.0, start, end, on, gearing, spread, rateCutoff, on->dayCounter(), lookback * Days, fixingDays);
206 pricer = QuantLib::ext::make_shared<AverageONIndexedCouponPricer>();
208 coupon = QuantLib::ext::make_shared<QuantExt::OvernightIndexedCoupon>(end, 1.0, start, end, on, gearing, spread, Date(),
209 Date(), on->dayCounter(),
false, includeSpread,
210 lookback * Days, rateCutoff, fixingDays);
211 pricer = QuantLib::ext::make_shared<OvernightIndexedCouponPricer>();
213 coupon->setPricer(pricer);
228 const boost::optional<long>& memSlot,
const RandomVariable& addRegressor1,
245 std::vector<const RandomVariable*> state;
252 Size nModelStates = state.size();
255 state.push_back(&addRegressor1);
257 state.push_back(&addRegressor2);
259 Size nAddReg = state.size() - nModelStates;
270 Matrix coordinateTransform;
274 bool haveStoredModel =
false;
278 coeff = std::get<0>(it->second);
279 coordinateTransform = std::get<2>(it->second);
280 QL_REQUIRE(std::get<1>(it->second) == state.size(),
281 "BlackScholesBase::npv(): stored regression coefficients at mem slot "
282 << *memSlot <<
" are for state size " << std::get<1>(it->second) <<
", actual state size is "
283 << state.size() <<
" (before possible coordinate transform).");
284 haveStoredModel =
true;
290 std::vector<RandomVariable> transformedState;
292 if(!haveStoredModel) {
298 transformedState = applyCoordinateTransform(state, coordinateTransform);
299 state = vec2vecptr(transformedState);
307 filter, RandomVariableRegressionMethod::QR);
309 <<
" (got model state size " << nModelStates <<
" and " << nAddReg
310 <<
" additional regressors, coordinate transform "
311 << coordinateTransform.columns() <<
" -> " << coordinateTransform.rows() <<
")");
323 if(!coordinateTransform.empty()) {
324 transformedState = applyCoordinateTransform(state, coordinateTransform);
325 state = vec2vecptr(transformedState);
const std::string indexInput_
black scholes model base class for n underlyings (fx, equity or commodity)
const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > & correlations_
RandomVariable getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
void performCalculations() const override
void releaseMemory() override
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
std::map< Date, std::vector< RandomVariable > > underlyingPaths_
std::set< Date > effectiveSimulationDates_
BlackScholesBase(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)
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 resetNPVMem() 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_
Matrix getCorrelation() const
Real timeFromReference(const Date &d) const
virtual Size size() const
const std::vector< std::string > currencies_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
std::vector< IndexInfo > indices_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
SafeStack< Filter > filter
const QuantLib::ext::shared_ptr< ModelCG > model_
Map text representations to QuantLib/QuantExt types.
#define DLOG(text)
Logging Macro (Level = Debug)
#define DLOGGERSTREAM(text)
Array regressionCoefficients(RandomVariable r, const std::vector< const RandomVariable * > ®ressor, const std::vector< std::function< RandomVariable(const std::vector< const RandomVariable * > &)> > &basisFn, const Filter &filter, const RandomVariableRegressionMethod regressionMethod, const std::string &debugLabel)
RandomVariable expectation(const RandomVariable &r)
RandomVariable conditionalExpectation(const std::vector< const RandomVariable * > ®ressor, const std::vector< std::function< RandomVariable(const std::vector< const RandomVariable * > &)> > &basisFn, const Array &coefficients)
std::string to_string(const LocationInfo &l)
Serializable Credit Default Swap.
bool deterministic() const
QuantLib::Real regressionVarianceCutoff
QuantLib::LsmBasisSystem::PolynomialType polynomType
string conversion utilities