Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
modelcgimpl.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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/modelcgimpl.hpp
20 \brief basis implementation for a script engine model
21 \ingroup utilities
22*/
23
24#pragma once
25
28
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 provides an implementation of the model interface. Derived classes have to implement
39 - ModelCG::referenceDate()
40 - ModelCG::npv()
41 - ModelCG::fwdCompAvg()
42 - ModelCG::getDirectFxSpotT0()
43 - ModelCG::getDirectDiscountT0()
44 and the interface defined by this class (the pure virtual methods defined below) */
45class ModelCGImpl : public ModelCG {
46public:
47 /* Constructor arguments:
48 - dayCounter: the convention to convert dates to times
49 - size: the dimension of the randomvariables used by the model
50 - currencies: the supported currencies, the first one is the model's base ccy
51 - irIndices: pairs of ORE labels and QL indices, linked to T0 curves
52 - infIndices: pairs of ORE labels and QL indices, linked to T0 curves
53 - indices: eq, fx, comm index names following the ORE naming conventions; fx indices must have a domestic
54 currency equal to the model's base ccy; fx indices should have the tag GENERIC here (historical fixings
55 will still be handled using the original tag); we do not require an fx index for each non-base currency
56 supported, we fall back on a zero vol conversion instead
57 - indexCurrencies: index ccy for eq, comm, ir and the foreign ccy for fx indices
58 - conventions: currently needed to resolve comm indices only
59 - simulationDates: currently needed to resolve comm indices only
60 - the conversion of a payment ccy != base ccy uses the fx index value (if existent), otherwise the zero vol
61 version (i.e. the given fx spot and curves)
62 - new and inverse currency pairs are implied from the existing ones in eval() for non-historical fixings
63 - historical fixings are retrieved in eval() only; there they override today's spot if given
64 */
65 ModelCGImpl(const DayCounter& dayCounter, const Size size, const std::vector<std::string>& currencies,
66 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
67 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
68 const std::vector<std::string>& indices, const std::vector<std::string>& indexCurrencies,
69 const std::set<Date>& simulationDates, const IborFallbackConfig& iborFallbackConfig);
70
71 // Model interface implementation (partial)
72 const std::string& baseCcy() const override { return currencies_.front(); }
73 std::size_t dt(const Date& d1, const Date& d2) const override;
74 std::size_t pay(const std::size_t amount, const Date& obsdate, const Date& paydate,
75 const std::string& currency) const override;
76 std::size_t discount(const Date& obsdate, const Date& paydate, const std::string& currency) const override;
77 std::size_t eval(const std::string& index, const Date& obsdate, const Date& fwddate,
78 const bool returnMissingMissingAsNull = false,
79 const bool ignoreTodaysFixing = false) const override;
80 std::size_t fxSpotT0(const std::string& forCcy, const std::string& domCcy) const override;
81 std::size_t barrierProbability(const std::string& index, const Date& obsdate1, const Date& obsdate2,
82 const std::size_t barrier, const bool above) const override;
83
84 // provide default implementation for MC type models (taking a simple expectation)
85 Real extractT0Result(const RandomVariable& value) const override;
86
87 // CG / AD part of the interface
88 std::size_t cgVersion() const override;
89 const std::vector<std::vector<std::size_t>>& randomVariates() const override; // dim / steps
90 std::vector<std::pair<std::size_t, double>> modelParameters() const override;
91 std::vector<std::pair<std::size_t, std::function<double(void)>>>& modelParameterFunctors() const override;
92
93protected:
94 // get (non-ir) index (forward) value for index[indexNo] for (fwd >=) d >= reference date
95 virtual std::size_t getIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const = 0;
96 // get projection for irIndices[indexNo] for (fwd >=) d >= reference date, this should also return a value
97 // if d (resp. fwd if given) is not a valid fixing date for the index
98 virtual std::size_t getIrIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const = 0;
99 // get projection for infIndices[indexNo] for fwd >= d >= base date; fwd will always be given and be a first day of
100 // an inflation period (this function is called twice, interpolation will be handled in the ModelCGImpl class then)
101 virtual std::size_t getInfIndexValue(const Size indexNo, const Date& d, const Date& fwd) const = 0;
102 // get discount factor P(s,t) for ccy currencies[idx], t > s >= referenceDate
103 virtual std::size_t getDiscount(const Size idx, const Date& s, const Date& t) const = 0;
104 // get numeraire N(s) for ccy curencies[idx], s >= referenceDate
105 virtual std::size_t getNumeraire(const Date& s) const = 0;
106 // get fx spot for currencies[idx] vs. currencies[0], as of the referenceDate, should be 1 for idx=0
107 virtual std::size_t getFxSpot(const Size idx) const = 0;
108 // get barrier probability for refDate <= obsdate1 <= obsdate2, the case obsdate1 < refDate is handled in this class
109 virtual std::size_t getFutureBarrierProb(const std::string& index, const Date& obsdate1, const Date& obsdate2,
110 const std::size_t barrier, const bool above) const = 0;
111
112 const DayCounter dayCounter_;
113 const std::vector<std::string> currencies_;
114 const std::vector<std::string> indexCurrencies_;
115 const std::set<Date> simulationDates_;
117
118 std::vector<std::pair<IndexInfo, QuantLib::ext::shared_ptr<InterestRateIndex>>> irIndices_;
119 std::vector<std::pair<IndexInfo, QuantLib::ext::shared_ptr<ZeroInflationIndex>>> infIndices_;
120 std::vector<IndexInfo> indices_;
121
122 // to be populated by derived classes when building the computation graph
123 mutable std::vector<std::vector<size_t>> randomVariates_;
124 mutable std::vector<std::pair<std::size_t, std::function<double(void)>>> modelParameters_;
125
126 // convenience function to add model parameters
127 std::size_t addModelParameter(const std::string& id, std::function<double(void)> f) const;
128
129 // manages cg version and triggers recalculations of random variate / model parameter nodes
130 void performCalculations() const override;
131
132private:
133 mutable std::size_t cgVersion_ = 0;
134 mutable QuantLib::Date cgEvalDate_ = Date();
135
136 // helper method to handle inflation fixings and their interpolation
137 std::size_t getInflationIndexFixing(const bool returnMissingFixingAsNull, const std::string& indexInput,
138 const QuantLib::ext::shared_ptr<ZeroInflationIndex>& infIndex, const Size indexNo,
139 const Date& limDate, const Date& obsdate, const Date& fwddate,
140 const Date& baseDate) const;
141};
142
143// convenience function to add model parameters, standalone variant
144std::size_t addModelParameter(ComputationGraph& g, std::vector<std::pair<std::size_t, std::function<double(void)>>>& m,
145 const std::string& id, std::function<double(void)> f);
146
147// map date to a coarser grid if sloppyDates = true, otherwise just return d
148Date getSloppyDate(const Date& d, const bool sloppyDates, const std::set<Date>& dates);
149
150} // namespace data
151} // namespace ore
virtual QuantLib::Size size() const
Definition: modelcg.hpp:62
virtual std::size_t getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd) const =0
std::size_t addModelParameter(const std::string &id, std::function< double(void)> f) const
void performCalculations() const override
const std::vector< std::string > currencies_
std::vector< std::vector< size_t > > randomVariates_
std::size_t pay(const std::size_t amount, const Date &obsdate, const Date &paydate, const std::string &currency) const override
Real extractT0Result(const RandomVariable &value) const override
std::size_t cgVersion() const override
std::size_t getInflationIndexFixing(const bool returnMissingFixingAsNull, const std::string &indexInput, const QuantLib::ext::shared_ptr< ZeroInflationIndex > &infIndex, const Size indexNo, const Date &limDate, const Date &obsdate, const Date &fwddate, const Date &baseDate) const
std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameterFunctors() const override
virtual std::size_t getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
std::size_t discount(const Date &obsdate, const Date &paydate, const std::string &currency) const override
std::vector< std::pair< std::size_t, std::function< double(void)> > > modelParameters_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
virtual std::size_t getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const =0
QuantLib::Date cgEvalDate_
const std::string & baseCcy() const override
Definition: modelcgimpl.hpp:72
std::size_t dt(const Date &d1, const Date &d2) const override
std::vector< IndexInfo > indices_
virtual std::size_t getFxSpot(const Size idx) const =0
virtual std::size_t getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
std::size_t barrierProbability(const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const override
const DayCounter dayCounter_
virtual std::size_t getDiscount(const Size idx, const Date &s, const Date &t) const =0
virtual std::size_t getNumeraire(const Date &s) const =0
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
std::size_t fxSpotT0(const std::string &forCcy, const std::string &domCcy) const override
std::vector< std::pair< std::size_t, double > > modelParameters() const override
const std::vector< std::vector< std::size_t > > & randomVariates() const override
const std::vector< std::string > indexCurrencies_
const IborFallbackConfig iborFallbackConfig_
const std::set< Date > simulationDates_
std::size_t eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
SafeStack< ValueType > value
@ data
Definition: log.hpp:77
interface for model against which a script can be run
std::size_t addModelParameter(ComputationGraph &g, std::vector< std::pair< std::size_t, std::function< double(void)> > > &m, const std::string &id, std::function< double(void)> f)
Date getSloppyDate(const Date &d, const bool sloppyDates, const std::set< Date > &dates)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
some utility functions