Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
modelimpl.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/modelimpl.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 - Model::referenceDate()
40 - Model::npv()
41 - Model::fwdCompAvg()
42 - Model::releaseMemory() (if applicable)
43 and the interface defined by this class (the pure virtual methods defined below) */
44class ModelImpl : public Model {
45public:
46 /* Constructor arguments:
47 - dayCounter: the convention to convert dates to times
48 - size: the dimension of the randomvariables used by the model
49 - currencies: the supported currencies, the first one is the model's base ccy
50 - irIndices: pairs of ORE labels and QL indices, linked to T0 curves
51 - infIndices: pairs of ORE labels and QL indices, linked to T0 curves
52 - indices: eq, fx, comm index names following the ORE naming conventions; fx indices must have a domestic
53 currency equal to the model's base ccy; fx indices should have the tag GENERIC here (historical fixings
54 will still be handled using the original tag); we do not require an fx index for each non-base currency
55 supported, we fall back on a zero vol conversion instead
56 - indexCurrencies: index ccy for eq, comm, ir and the foreign ccy for fx indices
57 - conventions: currently needed to resolve comm indices only
58 - simulationDates: currently needed to resolve comm indices only
59 - the conversion of a payment ccy != base ccy uses the fx index value (if existent), otherwise the zero vol
60 version (i.e. the given fx spot and curves)
61 - new and inverse currency pairs are implied from the existing ones in eval() for non-historical fixings
62 - historical fixings are retrieved in eval() only; there they override today's spot if given
63 */
64 ModelImpl(const DayCounter& dayCounter, const Size size, const std::vector<std::string>& currencies,
65 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
66 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
67 const std::vector<std::string>& indices, const std::vector<std::string>& indexCurrencies,
68 const std::set<Date>& simulationDates,
69 const IborFallbackConfig& iborFallbackConfig);
70
71 // Model interface implementation (partial)
72 const std::string& baseCcy() const override { return currencies_.front(); }
73 Real dt(const Date& d1, const Date& d2) const override { return dayCounter_.yearFraction(d1, d2); }
74 RandomVariable pay(const RandomVariable& amount, const Date& obsdate, const Date& paydate,
75 const std::string& currency) const override;
76 RandomVariable discount(const Date& obsdate, const Date& paydate, const std::string& currency) const override;
77 RandomVariable eval(const std::string& index, const Date& obsdate, const Date& fwddate,
78 const bool returnMissingMissingAsNull = false,
79 const bool ignoreTodaysFixing = false) const override;
80 Real fxSpotT0(const std::string& forCcy, const std::string& domCcy) const override;
81 RandomVariable barrierProbability(const std::string& index, const Date& obsdate1, const Date& obsdate2,
82 const RandomVariable& barrier, const bool above) const override;
83 // provide default implementation for MC type models (taking a simple expectation)
84 Real extractT0Result(const RandomVariable& value) const override;
85 //
86
87protected:
88 // get (non-ir) index (forward) value for index[indexNo] for (fwd >=) d >= reference date
89 virtual RandomVariable getIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const = 0;
90 // get projection for irIndices[indexNo] for (fwd >=) d >= reference date, this should also return a value
91 // if d (resp. fwd if given) is not a valid fixing date for the index
92 virtual RandomVariable getIrIndexValue(const Size indexNo, const Date& d, const Date& fwd = Null<Date>()) const = 0;
93 // get projection for infIndices[indexNo] for fwd >= d >= base date; fwd will always be given and be a first day of
94 // an inflation period (this function is called twice, interpolation will be handled in the ModelImpl class then)
95 virtual RandomVariable getInfIndexValue(const Size indexNo, const Date& d, const Date& fwd) const = 0;
96 // get discount factor P(s,t) for ccy currencies[idx], t > s >= referenceDate
97 virtual RandomVariable getDiscount(const Size idx, const Date& s, const Date& t) const = 0;
98 // get numeraire N(s) for ccy curencies[idx], s >= referenceDate
99 virtual RandomVariable getNumeraire(const Date& s) const = 0;
100 // get fx spot for currencies[idx] vs. currencies[0], as of the referenceDate, should be 1 for idx=0
101 virtual Real getFxSpot(const Size idx) const = 0;
102 // get barrier probability for refDate <= obsdate1 <= obsdate2, the case obsdate1 < refDate is handled in this class
103 virtual RandomVariable getFutureBarrierProb(const std::string& index, const Date& obsdate1, const Date& obsdate2,
104 const RandomVariable& barrier, const bool above) const = 0;
105
106 const DayCounter dayCounter_;
107 const std::vector<std::string> currencies_;
108 const std::vector<std::string> indexCurrencies_;
109 const std::set<Date> simulationDates_;
111
112 std::vector<std::pair<IndexInfo, QuantLib::ext::shared_ptr<InterestRateIndex>>> irIndices_;
113 std::vector<std::pair<IndexInfo, QuantLib::ext::shared_ptr<ZeroInflationIndex>>> infIndices_;
114 std::vector<IndexInfo> indices_;
115
116private:
117 // helper method to handle inflation fixings and their interpolation
118 RandomVariable getInflationIndexFixing(const bool returnMissingFixingAsNull, const std::string& indexInput,
119 const QuantLib::ext::shared_ptr<ZeroInflationIndex>& infIndex, const Size indexNo,
120 const Date& limDate, const Date& obsdate, const Date& fwddate,
121 const Date& baseDate) const;
122};
123
124} // namespace data
125} // namespace ore
virtual Size size() const
Definition: model.hpp:71
virtual RandomVariable getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd) const =0
const std::vector< std::string > currencies_
Definition: modelimpl.hpp:107
Real extractT0Result(const RandomVariable &value) const override
Definition: modelimpl.cpp:400
Real fxSpotT0(const std::string &forCcy, const std::string &domCcy) const override
Definition: modelimpl.cpp:336
RandomVariable barrierProbability(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const override
Definition: modelimpl.cpp:351
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
Definition: modelimpl.hpp:113
RandomVariable discount(const Date &obsdate, const Date &paydate, const std::string &currency) const override
Definition: modelimpl.cpp:137
virtual RandomVariable getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const =0
const std::string & baseCcy() const override
Definition: modelimpl.hpp:72
std::vector< IndexInfo > indices_
Definition: modelimpl.hpp:114
Real dt(const Date &d1, const Date &d2) const override
Definition: modelimpl.hpp:73
RandomVariable 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
Definition: modelimpl.cpp:155
virtual Real getFxSpot(const Size idx) const =0
virtual RandomVariable getDiscount(const Size idx, const Date &s, const Date &t) const =0
RandomVariable eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
Definition: modelimpl.cpp:181
const DayCounter dayCounter_
Definition: modelimpl.hpp:106
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
Definition: modelimpl.hpp:112
virtual RandomVariable getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
RandomVariable pay(const RandomVariable &amount, const Date &obsdate, const Date &paydate, const std::string &currency) const override
Definition: modelimpl.cpp:101
virtual RandomVariable getNumeraire(const Date &s) const =0
const std::vector< std::string > indexCurrencies_
Definition: modelimpl.hpp:108
const IborFallbackConfig iborFallbackConfig_
Definition: modelimpl.hpp:110
const std::set< Date > simulationDates_
Definition: modelimpl.hpp:109
virtual RandomVariable getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
SafeStack< ValueType > value
@ data
Definition: log.hpp:77
interface for model against which a script can be run
Serializable Credit Default Swap.
Definition: namespaces.docs:23
some utility functions