Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
modelcg.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/modelcg.hpp
20 \brief interface for model against which a script can be run
21 \ingroup utilities
22*/
23
24#pragma once
25
28
29#include <ql/patterns/lazyobject.hpp>
30#include <ql/settings.hpp>
31#include <ql/time/daycounters/actualactual.hpp>
32
33#include <boost/any.hpp>
34
35namespace QuantExt {
36class ComputationGraph;
37}
38
39namespace ore {
40namespace data {
41
42using QuantLib::Date;
43using QuantLib::Real;
44using QuantLib::Integer;
45using QuantLib::Natural;
46using QuantLib::Size;
47
48class ModelCG : public QuantLib::LazyObject {
49public:
50 enum class Type { MC, FD };
51
52 explicit ModelCG(const QuantLib::Size n);
53 virtual ~ModelCG() {}
54
55 // computation graph
56 QuantLib::ext::shared_ptr<QuantExt::ComputationGraph> computationGraph() { return g_; }
57
58 // model type
59 virtual Type type() const = 0;
60
61 // number of paths
62 virtual QuantLib::Size size() const { return n_; }
63
64 // if not null, this model uses a separate mc training phase for NPV() calcs
65 virtual Size trainingSamples() const { return QuantLib::Null<Size>(); }
66
67 /* enable / disable the usage of the training paths (if trainingPaths() is not null)
68 the model should be using training paths only temporarily and reset to normal model via RAII */
69 virtual void toggleTrainingPaths() const {}
70
71 // the eval date
72 virtual const Date& referenceDate() const = 0;
73
74 // the base ccy of the model
75 virtual const std::string& baseCcy() const = 0;
76
77 // time between two dates d1 <= d2, default actact should be overriden in derived claases if appropriate
78 virtual std::size_t dt(const Date& d1, const Date& d2) const;
79
80 // result must be as of max(refdate, obsdate); refdate < paydate and obsdate <= paydate required
81 virtual std::size_t pay(const std::size_t amount, const Date& obsdate, const Date& paydate,
82 const std::string& currency) const = 0;
83
84 // refdate <= obsdate <= paydate required
85 virtual std::size_t discount(const Date& obsdate, const Date& paydate, const std::string& currency) const = 0;
86
87 // refdate <= obsdate required
88 virtual std::size_t npv(const std::size_t amount, const Date& obsdate, const std::size_t filter,
89 const boost::optional<long>& memSlot, const std::size_t addRegressor1,
90 const std::size_t addRegressor2) const = 0;
91
92 /* eval index at (past or future) obsdate:
93 - if fwddate != null, fwddate > obsdate is required. A check must be implemented that the obsdate allows for
94 the index projection. For non-inflation indices this check is simply obsdate >= refdate. For zero inflation
95 indices the is is obsdate >= basedate where the basedate is the one from the zero inflation ts asociated
96 to the index.
97 - if a historical fixing is required and missing, the behaviour depends on returnMissingFixingAsNull, if this
98 flag is true, an exception is thrown, if false, the node "nan" is returned
99 - for non-inflation indices, if ignoreTodaysFixing is true, always return the market spot for obsdate =
100 referencedate, even if a historical fixing is available; for inflation indices, ignore this flag
101 */
102 virtual std::size_t eval(const std::string& index, const Date& obsdate, const Date& fwddate,
103 const bool returnMissingFixingAsNull = false,
104 const bool ignoreTodaysFixing = false) const = 0;
105
106 // forward looking daily comp/avg, obsdate <= start < end required, result must be as of max(refdate, obsdate)
107 virtual std::size_t fwdCompAvg(const bool isAvg, const std::string& index, const Date& obsdate, const Date& start,
108 const Date& end, const Real spread, const Real gearing, const Integer lookback,
109 const Natural rateCutoff, const Natural fixingDays, const bool includeSpread,
110 const Real cap, const Real floor, const bool nakedOption,
111 const bool localCapFloor) const = 0;
112
113 // barrier hit probability, obsdate1 <= obsdate2 required, but refdate can lie anywhere w.r.t. obsdate1, 2
114 virtual std::size_t barrierProbability(const std::string& index, const Date& obsdate1, const Date& obsdate2,
115 const std::size_t barrier, const bool above) const = 0;
116
117 // get T0 fx spot
118 virtual std::size_t fxSpotT0(const std::string& forCcy, const std::string& domCcy) const = 0;
119
120 // extract T0 result from random variable
121 virtual Real extractT0Result(const QuantExt::RandomVariable& value) const = 0;
122
123 // reset stored NPV() regression coefficients (if applicable)
124 virtual void resetNPVMem() {}
125
126 // additional results provided by the model
127 const std::map<std::string, boost::any>& additionalResults() const { return additionalResults_; }
128
129 // CG / AD part of the interface
130 virtual std::size_t cgVersion() const = 0;
131 virtual const std::vector<std::vector<std::size_t>>& randomVariates() const = 0; // dim / steps
132 virtual std::vector<std::pair<std::size_t, double>> modelParameters() const = 0;
133 virtual std::vector<std::pair<std::size_t, std::function<double(void)>>>& modelParameterFunctors() const = 0;
134
135 // get fx spot as of today directly, i.e. bypassing the cg
136 virtual Real getDirectFxSpotT0(const std::string& forCcy, const std::string& domCcy) const = 0;
137
138 // get discount as of today directly, i.e. bypassing the cg
139 virtual Real getDirectDiscountT0(const Date& paydate, const std::string& currency) const = 0;
140
141 // calculate the model
142 void calculate() const override { LazyObject::calculate(); }
143
144protected:
145 // map with additional results provided by this model instance
146 mutable std::map<std::string, boost::any> additionalResults_;
147
148 // the underlying computation graph
149 QuantLib::ext::shared_ptr<QuantExt::ComputationGraph> g_;
150
151private:
152 void performCalculations() const override {}
153
154 // size of random variables within model
155 const QuantLib::Size n_;
156};
157
158} // namespace data
159} // namespace ore
virtual const Date & referenceDate() const =0
void performCalculations() const override
Definition: modelcg.hpp:152
virtual void resetNPVMem()
Definition: modelcg.hpp:124
virtual Type type() const =0
virtual ~ModelCG()
Definition: modelcg.hpp:53
virtual std::size_t discount(const Date &obsdate, const Date &paydate, const std::string &currency) const =0
std::map< std::string, boost::any > additionalResults_
Definition: modelcg.hpp:146
virtual std::size_t eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingFixingAsNull=false, const bool ignoreTodaysFixing=false) const =0
virtual Real getDirectDiscountT0(const Date &paydate, const std::string &currency) const =0
virtual std::size_t cgVersion() const =0
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > computationGraph()
Definition: modelcg.hpp:56
virtual Real getDirectFxSpotT0(const std::string &forCcy, const std::string &domCcy) const =0
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > g_
Definition: modelcg.hpp:149
void calculate() const override
Definition: modelcg.hpp:142
virtual void toggleTrainingPaths() const
Definition: modelcg.hpp:69
virtual std::size_t pay(const std::size_t amount, const Date &obsdate, const Date &paydate, const std::string &currency) const =0
virtual std::size_t barrierProbability(const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const =0
virtual Size trainingSamples() const
Definition: modelcg.hpp:65
virtual std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameterFunctors() const =0
virtual const std::string & baseCcy() const =0
virtual Real extractT0Result(const QuantExt::RandomVariable &value) const =0
virtual std::size_t dt(const Date &d1, const Date &d2) const
Definition: modelcg.cpp:28
virtual std::vector< std::pair< std::size_t, double > > modelParameters() const =0
virtual std::size_t fxSpotT0(const std::string &forCcy, const std::string &domCcy) const =0
const std::map< std::string, boost::any > & additionalResults() const
Definition: modelcg.hpp:127
virtual std::size_t 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 const std::vector< std::vector< std::size_t > > & randomVariates() const =0
const QuantLib::Size n_
Definition: modelcg.hpp:155
virtual std::size_t npv(const std::size_t amount, const Date &obsdate, const std::size_t filter, const boost::optional< long > &memSlot, const std::size_t addRegressor1, const std::size_t addRegressor2) const =0
virtual QuantLib::Size size() const
Definition: modelcg.hpp:62
SafeStack< ValueType > value
SafeStack< Filter > filter
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23