Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
model.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/model.hpp
20 \brief interface for model against which a script can be run
21 \ingroup utilities
22*/
23
24#pragma once
25
26#include <map>
28
31
32#include <ql/patterns/lazyobject.hpp>
33#include <ql/settings.hpp>
34#include <ql/time/daycounters/actualactual.hpp>
35#include <ql/methods/montecarlo/lsmbasissystem.hpp>
36
37#include <boost/any.hpp>
38#include <boost/enable_shared_from_this.hpp>
39
40namespace ore {
41namespace data {
42
43using QuantLib::Date;
44
45class Model : public LazyObject {
46public:
47 enum class Type { MC, FD };
48
49 struct McParams {
50 McParams() = default;
51 Size seed = 42;
52 Size trainingSeed = 43;
53 Size trainingSamples = Null<Size>();
54 QuantExt::SequenceType sequenceType = QuantExt::SequenceType::SobolBrownianBridge;
55 QuantExt::SequenceType trainingSequenceType = QuantExt::SequenceType::MersenneTwister;
58 QuantLib::LsmBasisSystem::PolynomialType polynomType = QuantLib::LsmBasisSystem::PolynomialType::Monomial;
59 QuantLib::SobolBrownianGenerator::Ordering sobolOrdering = QuantLib::SobolBrownianGenerator::Steps;
60 QuantLib::SobolRsg::DirectionIntegers sobolDirectionIntegers = QuantLib::SobolRsg::DirectionIntegers::JoeKuoD7;
61 QuantLib::Real regressionVarianceCutoff = Null<QuantLib::Real>();
62 };
63
64 explicit Model(const Size n) : n_(n) {}
65 virtual ~Model() {}
66
67 // model type
68 virtual Type type() const = 0;
69
70 // number of paths
71 virtual Size size() const { return n_; }
72
73 // if not null, this model uses a separate mc training phase for NPV() calcs
74 virtual Size trainingSamples() const { return Null<Size>(); }
75
76 /* enable / disable the usage of the training paths (if trainingPaths() is not null)
77 the model should be using training paths only temporarily and reset to normal model via RAII */
78 virtual void toggleTrainingPaths() const {}
79
80 // the eval date
81 virtual const Date& referenceDate() const = 0;
82
83 // the base ccy of the model
84 virtual const std::string& baseCcy() const = 0;
85
86 // time between two dates d1 <= d2, default actact should be overriden in derived claases if appropriate
87 virtual Real dt(const Date& d1, const Date& d2) const {
88 return ActualActual(ActualActual::ISDA).yearFraction(d1, d2);
89 }
90
91 // time from reference date in this model
92 Real timeFromReference(const Date& d) const { return dt(referenceDate(), d); }
93
94 // result must be as of max(refdate, obsdate); refdate < paydate and obsdate <= paydate required
95 virtual RandomVariable pay(const RandomVariable& amount, const Date& obsdate, const Date& paydate,
96 const std::string& currency) const = 0;
97
98 // refdate <= obsdate <= paydate required
99 virtual RandomVariable discount(const Date& obsdate, const Date& paydate, const std::string& currency) const = 0;
100
101 // refdate <= obsdate required
102 virtual RandomVariable npv(const RandomVariable& amount, const Date& obsdate, const Filter& filter,
103 const boost::optional<long>& memSlot, const RandomVariable& addRegressor1,
104 const RandomVariable& addRegressor2) const = 0;
105
106 /* eval index at (past or future) obsdate:
107 - if fwddate != null, fwddate > obsdate is required. A check must be implemented that the obsdate allows for
108 the index projection. For non-inflation indices this check is simply obsdate >= refdate. For zero inflation
109 indices the is is obsdate >= basedate where the basedate is the one from the zero inflation ts asociated
110 to the index.
111 - if a historical fixing is required and missing, the behaviour depends on returnMissingFixingAsNull, if this
112 flag is true, an exception is thrown, if false, an RandomVariable rv with rv.initialised() = false is returned
113 - for non-inflation indices, if ignoreTodaysFixing is true, always return the market spot for obsdate =
114 referencedate, even if a historical fixing is available; for inflation indices, ignore this flag
115 */
116 virtual RandomVariable eval(const std::string& index, const Date& obsdate, const Date& fwddate,
117 const bool returnMissingFixingAsNull = false,
118 const bool ignoreTodaysFixing = false) const = 0;
119
120 // forward looking daily comp, obsdate <= start < end required, result must be as of max(refdate, obsdate)
121 virtual RandomVariable fwdCompAvg(const bool isAvg, const std::string& index, const Date& obsdate,
122 const Date& start, const Date& end, const Real spread, const Real gearing,
123 const Integer lookback, const Natural rateCutoff, const Natural fixingDays,
124 const bool includeSpread, const Real cap, const Real floor,
125 const bool nakedOption, const bool localCapFloor) const = 0;
126
127 // barrier hit probability, obsdate1 <= obsdate2 required, but refdate can lie anywhere w.r.t. obsdate1, 2
128 virtual RandomVariable barrierProbability(const std::string& index, const Date& obsdate1, const Date& obsdate2,
129 const RandomVariable& barrier, const bool above) const = 0;
130
131 // get T0 fx spot
132 virtual Real fxSpotT0(const std::string& forCcy, const std::string& domCcy) const = 0;
133
134 // extract T0 result from random variable
135 virtual Real extractT0Result(const RandomVariable& value) const = 0;
136
137 /* Release memory allocated for caches (if applicable). This should _not_ notify observers of the model, since
138 this would in particular trigger a recalculation of the scripted instrument pricing engine after each pricing
139 when the memory is released, although the model's observables may not have changed. */
140 virtual void releaseMemory() {}
141
142 // reset stored NPV() regression coefficients (if applicable)
143 virtual void resetNPVMem() {}
144
145 // additional results provided by the model
146 const std::map<std::string, boost::any>& additionalResults() const { return additionalResults_; }
147
148protected:
149 // default implementation lazy object interface
150 void performCalculations() const override {}
151
152 // map with additional results provided by this model instance
153 mutable std::map<std::string, boost::any> additionalResults_;
154
155private:
156 // size of random variables within model
157 const Size n_;
158};
159
160} // namespace data
161} // namespace ore
virtual Real dt(const Date &d1, const Date &d2) const
Definition: model.hpp:87
virtual RandomVariable eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingFixingAsNull=false, const bool ignoreTodaysFixing=false) const =0
virtual const Date & referenceDate() const =0
void performCalculations() const override
Definition: model.hpp:150
virtual void resetNPVMem()
Definition: model.hpp:143
const Size n_
Definition: model.hpp:157
virtual Type type() const =0
virtual RandomVariable pay(const RandomVariable &amount, const Date &obsdate, const Date &paydate, const std::string &currency) const =0
std::map< std::string, boost::any > additionalResults_
Definition: model.hpp:153
virtual Real extractT0Result(const RandomVariable &value) const =0
virtual Real fxSpotT0(const std::string &forCcy, const std::string &domCcy) const =0
Model(const Size n)
Definition: model.hpp:64
virtual void releaseMemory()
Definition: model.hpp:140
virtual void toggleTrainingPaths() const
Definition: model.hpp:78
Real timeFromReference(const Date &d) const
Definition: model.hpp:92
virtual RandomVariable discount(const Date &obsdate, const Date &paydate, const std::string &currency) const =0
virtual Size trainingSamples() const
Definition: model.hpp:74
virtual Size size() const
Definition: model.hpp:71
virtual const std::string & baseCcy() const =0
virtual ~Model()
Definition: model.hpp:65
virtual RandomVariable npv(const RandomVariable &amount, const Date &obsdate, const Filter &filter, const boost::optional< long > &memSlot, const RandomVariable &addRegressor1, const RandomVariable &addRegressor2) const =0
virtual RandomVariable barrierProbability(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const =0
const std::map< std::string, boost::any > & additionalResults() const
Definition: model.hpp:146
virtual 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 =0
SafeStack< ValueType > value
SafeStack< Filter > filter
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
bool externalDeviceCompatibilityMode
Definition: model.hpp:56
QuantExt::SequenceType sequenceType
Definition: model.hpp:54
QuantLib::SobolBrownianGenerator::Ordering sobolOrdering
Definition: model.hpp:59
QuantExt::SequenceType trainingSequenceType
Definition: model.hpp:55
QuantLib::SobolRsg::DirectionIntegers sobolDirectionIntegers
Definition: model.hpp:60
QuantLib::Real regressionVarianceCutoff
Definition: model.hpp:61
QuantLib::LsmBasisSystem::PolynomialType polynomType
Definition: model.hpp:58
value type and operations