QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
gaussian1dmodel.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2013, 2015 Peter Caspers
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file gaussian1dmodel.hpp
21 \brief basic interface for one factor interest rate models
22*/
23
24// uncomment to enable NTL support (see below for more details and references)
25// #define GAUSS1D_ENABLE_NTL
26
27#ifndef quantlib_gaussian1dmodel_hpp
28#define quantlib_gaussian1dmodel_hpp
29
30#include <ql/models/model.hpp>
35#include <ql/time/date.hpp>
36#include <ql/time/period.hpp>
39#include <ql/utilities/null.hpp>
41
42#ifdef GAUSS1D_ENABLE_NTL
43#include <boost/math/bindings/rr.hpp>
44#endif
45
46#if BOOST_VERSION < 106700
47#include <boost/functional/hash.hpp>
48#else
49#include <boost/container_hash/hash.hpp>
50#endif
51
52#include <unordered_map>
53
54namespace QuantLib {
55
56/*! One factor interest rate model interface class
57 The only methods that must be implemented by subclasses
58 are the numeraire and zerobond methods for an input array
59 of state variable values. The variable $y$ is understood
60 to be the standardized (zero mean, unit variance) version
61 of the model's original state variable $x$.
62
63 NTL support may be enabled by defining GAUSS1D_ENABLE_NTL in this
64 file. For details on NTL see
65 http://www.shoup.net/ntl/
66
67 \warning the variance of the state process conditional on
68 $x(t)=x$ must be independent of the value of $x$
69
70*/
71
73 public:
74 ext::shared_ptr<StochasticProcess1D> stateProcess() const;
75
77 Real y = 0.0,
79
81 Time t = 0.0,
82 Real y = 0.0,
84
85 Real numeraire(const Date& referenceDate,
86 Real y = 0.0,
88
89 Real zerobond(const Date& maturity,
90 const Date& referenceDate = Null<Date>(),
91 Real y = 0.0,
93
95 const Date& expiry,
96 const Date& valueDate,
97 const Date& maturity,
98 Rate strike,
99 const Date& referenceDate = Null<Date>(),
100 Real y = 0.0,
102 Real yStdDevs = 7.0,
103 Size yGridPoints = 64,
104 bool extrapolatePayoff = true,
105 bool flatPayoffExtrapolation = false) const;
106
107 Real
108 forwardRate(const Date& fixing,
109 const Date& referenceDate = Null<Date>(),
110 Real y = 0.0,
111 const ext::shared_ptr<IborIndex>& iborIdx = ext::shared_ptr<IborIndex>()) const;
112
113 Real swapRate(const Date& fixing,
114 const Period& tenor,
115 const Date& referenceDate = Null<Date>(),
116 Real y = 0.0,
117 const ext::shared_ptr<SwapIndex>& swapIdx = ext::shared_ptr<SwapIndex>()) const;
118
119 Real
120 swapAnnuity(const Date& fixing,
121 const Period& tenor,
122 const Date& referenceDate = Null<Date>(),
123 Real y = 0.0,
124 const ext::shared_ptr<SwapIndex>& swapIdx = ext::shared_ptr<SwapIndex>()) const;
125
126 /*! Computes the integral
127 \f[ {2\pi}^{-0.5} \int_{a}^{b} p(x) \exp{-0.5*x*x} \mathrm{d}x \f]
128 with
129 \f[ p(x) = ax^4+bx^3+cx^2+dx+e \f].
130 */
131 static Real
133
134 /*! Computes the integral
135 \f[ {2\pi}^{-0.5} \int_{a}^{b} p(x) \exp{-0.5*x*x} \mathrm{d}x \f]
136 with
137 \f[ p(x) = a(x-h)^4+b(x-h)^3+c(x-h)^2+d(x-h)+e \f].
138 */
140 Real a, Real b, Real c, Real d, Real e, Real h, Real x0, Real x1);
141
142 /*! Generates a grid of values for the standardized state variable $y$
143 at time $T$
144 conditional on $y(t)=y$, covering yStdDevs standard deviations
145 consisting of
146 2*gridPoints+1 points */
147
148 Array yGrid(Real yStdDevs, int gridPoints, Real T = 1.0, Real t = 0, Real y = 0) const;
149
150 private:
151 // It is of great importance for performance reasons to cache underlying
152 // swaps generated from indexes. In addition the indexes may only be given
153 // as templates for the conventions with the tenor replaced by the actual
154 // one later on.
155
157 const ext::shared_ptr<SwapIndex> index;
160 bool operator==(const CachedSwapKey &o) const {
161 return index->name() == o.index->name() && fixing == o.fixing &&
162 tenor == o.tenor;
163 }
164 };
165
167 std::size_t operator()(CachedSwapKey const &x) const {
168 std::size_t seed = 0;
169 boost::hash_combine(seed, x.index->name());
170 boost::hash_combine(seed, x.fixing.serialNumber());
171 boost::hash_combine(seed, x.tenor.length());
172 boost::hash_combine(seed, x.tenor.units());
173 return seed;
174 }
175 };
176
177 mutable std::unordered_map<CachedSwapKey, ext::shared_ptr<VanillaSwap>, CachedSwapKeyHasher> swapCache_;
178
179 protected:
180 // we let derived classes register with the termstructure
182 : TermStructureConsistentModel(yieldTermStructure) {
183 registerWith(Settings::instance().evaluationDate());
184 }
185
186 virtual Real numeraireImpl(Time t, Real y, const Handle<YieldTermStructure>& yts) const = 0;
187
188 virtual Real
190
191 void performCalculations() const override {
195 }
196
198 calculate();
200 }
201
202 // retrieve underlying swap from cache if possible, otherwise
203 // create it and store it in the cache
204 ext::shared_ptr<VanillaSwap>
205 underlyingSwap(const ext::shared_ptr<SwapIndex> &index,
206 const Date &expiry, const Period &tenor) const {
207
208 CachedSwapKey k = {index, expiry, tenor};
209 auto i = swapCache_.find(k);
210 if (i == swapCache_.end()) {
211 ext::shared_ptr<VanillaSwap> underlying =
212 index->clone(tenor)->underlyingSwap(expiry);
213 swapCache_.insert(std::make_pair(k, underlying));
214 return underlying;
215 }
216 return i->second;
217 }
218
219 ext::shared_ptr<StochasticProcess1D> stateProcess_;
222};
223
224inline ext::shared_ptr<StochasticProcess1D> Gaussian1dModel::stateProcess() const {
225
226 QL_REQUIRE(stateProcess_ != nullptr, "state process not set");
227 return stateProcess_;
228}
229
230inline Real
232 const Handle<YieldTermStructure> &yts) const {
233
234 return numeraireImpl(t, y, yts);
235}
236
237inline Real
239 const Handle<YieldTermStructure> &yts) const {
240 return zerobondImpl(T, t, y, yts);
241}
242
243inline Real
244Gaussian1dModel::numeraire(const Date &referenceDate, const Real y,
245 const Handle<YieldTermStructure> &yts) const {
246
247 return numeraire(termStructure()->timeFromReference(referenceDate), y, yts);
248}
249
250inline Real
251Gaussian1dModel::zerobond(const Date &maturity, const Date &referenceDate,
252 const Real y, const Handle<YieldTermStructure> &yts) const {
253
254 return zerobond(termStructure()->timeFromReference(maturity),
255 referenceDate != Null<Date>()
256 ? termStructure()->timeFromReference(referenceDate)
257 : 0.0,
258 y, yts);
259}
260}
261
262#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Concrete date class.
Definition: date.hpp:125
Date::serial_type serialNumber() const
Definition: date.hpp:408
void performCalculations() const override
static Real gaussianPolynomialIntegral(Real a, Real b, Real c, Real d, Real e, Real x0, Real x1)
static Real gaussianShiftedPolynomialIntegral(Real a, Real b, Real c, Real d, Real e, Real h, Real x0, Real x1)
ext::shared_ptr< VanillaSwap > underlyingSwap(const ext::shared_ptr< SwapIndex > &index, const Date &expiry, const Period &tenor) const
virtual Real zerobondImpl(Time T, Time t, Real y, const Handle< YieldTermStructure > &yts) const =0
Real numeraire(Time t, Real y=0.0, const Handle< YieldTermStructure > &yts=Handle< YieldTermStructure >()) const
virtual Real numeraireImpl(Time t, Real y, const Handle< YieldTermStructure > &yts) const =0
Real swapRate(const Date &fixing, const Period &tenor, const Date &referenceDate=Null< Date >(), Real y=0.0, const ext::shared_ptr< SwapIndex > &swapIdx=ext::shared_ptr< SwapIndex >()) const
Array yGrid(Real yStdDevs, int gridPoints, Real T=1.0, Real t=0, Real y=0) const
std::unordered_map< CachedSwapKey, ext::shared_ptr< VanillaSwap >, CachedSwapKeyHasher > swapCache_
Real forwardRate(const Date &fixing, const Date &referenceDate=Null< Date >(), Real y=0.0, const ext::shared_ptr< IborIndex > &iborIdx=ext::shared_ptr< IborIndex >()) const
Real zerobond(Time T, Time t=0.0, Real y=0.0, const Handle< YieldTermStructure > &yts=Handle< YieldTermStructure >()) const
ext::shared_ptr< StochasticProcess1D > stateProcess() const
Real zerobondOption(const Option::Type &type, const Date &expiry, const Date &valueDate, const Date &maturity, Rate strike, const Date &referenceDate=Null< Date >(), Real y=0.0, const Handle< YieldTermStructure > &yts=Handle< YieldTermStructure >(), Real yStdDevs=7.0, Size yGridPoints=64, bool extrapolatePayoff=true, bool flatPayoffExtrapolation=false) const
Real swapAnnuity(const Date &fixing, const Period &tenor, const Date &referenceDate=Null< Date >(), Real y=0.0, const ext::shared_ptr< SwapIndex > &swapIdx=ext::shared_ptr< SwapIndex >()) const
Gaussian1dModel(const Handle< YieldTermStructure > &yieldTermStructure)
ext::shared_ptr< StochasticProcess1D > stateProcess_
Shared handle to an observable.
Definition: handle.hpp:41
Framework for calculation on demand and result caching.
Definition: lazyobject.hpp:35
virtual void calculate() const
Definition: lazyobject.hpp:253
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
TimeUnit units() const
Definition: period.hpp:51
Integer length() const
Definition: period.hpp:50
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
bool & enforcesTodaysHistoricFixings()
Definition: settings.hpp:171
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Term-structure consistent model class.
Definition: model.hpp:73
const Handle< YieldTermStructure > & termStructure() const
Definition: model.hpp:77
date- and time-related classes, typedefs and enumerations
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
ext::function< Real(Real)> b
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
base class for Inter-Bank-Offered-Rate indexes
framework for calculation on demand and result caching
Abstract interest rate model class.
Definition: any.hpp:35
null values
Model parameter classes.
period- and frequency-related classes and enumerations
stochastic processes
std::size_t operator()(CachedSwapKey const &x) const
const ext::shared_ptr< SwapIndex > index
bool operator==(const CachedSwapKey &o) const
swap-rate indexes
Simple fixed-rate vs Libor swap.
Interest-rate term structure.