Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
irlgm1fparametrization.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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 irlgm1fparametrization.hpp
20 \brief Interest Rate Linear Gaussian Markov 1 factor parametrization
21 \ingroup models
22*/
23
24#ifndef quantext_irlgm1f_parametrization_hpp
25#define quantext_irlgm1f_parametrization_hpp
26
28
29#include <ql/experimental/math/piecewiseintegral.hpp>
30#include <ql/handle.hpp>
31#include <ql/math/integrals/integral.hpp>
32#include <ql/termstructures/yieldtermstructure.hpp>
33#include <map>
34
35namespace QuantExt {
36
37//! LGM 1F Parametrization
38/*! \ingroup models
39 */
40template <class TS> class Lgm1fParametrization : public Parametrization {
41public:
42 Lgm1fParametrization(const Currency& currency, const Handle<TS>& termStructure,
43 const std::string& name = std::string());
44 /*! zeta must satisfy zeta(0) = 0, zeta'(t) >= 0 */
45 virtual Real zeta(const Time t) const = 0;
46 /*! H must be such that H' does not change its sign */
47 virtual Real H(const Time t) const = 0;
48 virtual Real alpha(const Time t) const;
49 virtual Real kappa(const Time t) const;
50 virtual Real Hprime(const Time t) const;
51 virtual Real Hprime2(const Time t) const;
52 virtual Real hullWhiteSigma(const Time t) const;
53 const Handle<TS> termStructure() const;
54
55 /*! \f[ \int_0^t alpha^2(u) H^n(u) du \f]*/
56 Real zetan(const Size n, const Time t, const QuantLib::ext::shared_ptr<Integrator>& integrator);
57
58 /*! allows to apply a shift to H (model invariance 1) */
59 Real& shift();
60
61 /*! allows to apply a scaling to H and zeta (model invariance 2),
62 note that if a non unit scaling is provided, then
63 the parameterValues method returns the unscaled alpha,
64 while all other methods return scaled (and shifted) values */
65 Real& scaling();
66
67 Size numberOfParameters() const override { return 2; }
68
69 void update() const override;
70
71protected:
73
74private:
75 const Handle<TS> termStructure_;
76 mutable std::map<std::pair<Size, Real>, Real> zetan_cached_;
77};
78
79// implementation
80
81template <class TS>
82Lgm1fParametrization<TS>::Lgm1fParametrization(const Currency& currency, const Handle<TS>& termStructure,
83 const std::string& name)
84 : Parametrization(currency, name.empty() ? currency.code() : name), shift_(0.0), scaling_(1.0),
85 termStructure_(termStructure) {}
86
87// inline
88
89template <class TS> inline Real Lgm1fParametrization<TS>::alpha(const Time t) const {
90 return std::sqrt((zeta(tr(t)) - zeta(tl(t))) / h_) / scaling_;
91}
92
93template <class TS> inline Real Lgm1fParametrization<TS>::Hprime(const Time t) const {
94 return scaling_ * (H(tr(t)) - H(tl(t))) / h_;
95}
96
97template <class TS> inline Real Lgm1fParametrization<TS>::Hprime2(const Time t) const {
98 return scaling_ * (H(tr2(t)) - 2.0 * H(tm2(t)) + H(tl2(t))) / (h2_ * h2_);
99}
100
101template <class TS> inline Real Lgm1fParametrization<TS>::hullWhiteSigma(const Time t) const {
102 return Hprime(t) * alpha(t);
103}
104
105template <class TS> inline Real Lgm1fParametrization<TS>::kappa(const Time t) const { return -Hprime2(t) / Hprime(t); }
106
107template <class TS> inline const Handle<TS> Lgm1fParametrization<TS>::termStructure() const { return termStructure_; }
108
109template <class TS> inline Real& Lgm1fParametrization<TS>::shift() { return shift_; }
110
111template <class TS> inline Real& Lgm1fParametrization<TS>::scaling() { return scaling_; }
112
113template <class TS>
114inline Real Lgm1fParametrization<TS>::zetan(const Size n, const Time t,
115 const QuantLib::ext::shared_ptr<Integrator>& integrator) {
116 auto z = zetan_cached_.find(std::make_pair(n, t));
117 if (z == zetan_cached_.end()) {
118 std::vector<Real> times;
119 for (Size i = 0; i < numberOfParameters(); ++i)
120 times.insert(times.end(), parameterTimes(i).begin(), parameterTimes(i).end());
121 PiecewiseIntegral pwint(integrator, times, true);
122 Real v = pwint([this, n](Real s) { return std::pow(this->alpha(s), 2) * std::pow(this->H(s), n); }, 0.0, t);
123 zetan_cached_[std::make_pair(n, t)] = v;
124 return v;
125 } else {
126 return z->second;
127 }
128}
129
130template <class TS> inline void Lgm1fParametrization<TS>::update() const {
132 zetan_cached_.clear();
133}
134
135// typedef
136
138
139} // namespace QuantExt
140
141#endif
virtual Real Hprime(const Time t) const
Lgm1fParametrization(const Currency &currency, const Handle< TS > &termStructure, const std::string &name=std::string())
virtual Real H(const Time t) const =0
virtual Real kappa(const Time t) const
const Handle< TS > termStructure() const
virtual Real hullWhiteSigma(const Time t) const
Real zetan(const Size n, const Time t, const QuantLib::ext::shared_ptr< Integrator > &integrator)
virtual Real alpha(const Time t) const
virtual Real Hprime2(const Time t) const
std::map< std::pair< Size, Real >, Real > zetan_cached_
virtual Real zeta(const Time t) const =0
const std::string & name() const
virtual const Currency & currency() const
virtual void update() const
Lgm1fParametrization< YieldTermStructure > IrLgm1fParametrization
base class for model parametrizations