Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
irlgm1fpiecewiselinearparametrization.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 irlgm1fpiecewiselinearparametrization.hpp
20 \brief piecewise linear model parametrization
21 \ingroup models
22*/
23
24#ifndef quantext_piecewiselinear_irlgm1f_parametrization_hpp
25#define quantext_piecewiselinear_irlgm1f_parametrization_hpp
26
29
30namespace QuantExt {
31//! Lgm 1f Piecewise Linear Parametrization
32/*! parametrization with piecewise linear H and zeta,
33 w.r.t. zeta this is the same as piecewise constant alpha,
34 w.r.t. H this is implemented with a new (helper) parameter
35 h > 0, such that \f$H(t) = \int_0^t h(s) ds\f$
36
37 \warning this class is considered experimental, it is not
38 tested well and might have conceptual issues
39 (e.g. kappa is zero almost everywhere); you
40 might rather want to rely on the piecewise
41 constant parametrization
42
43 \ingroup models
44*/
45
46template <class TS>
48public:
50 const Currency& currency, const Handle<TS>& termStructure, const Array& alphaTimes, const Array& alpha,
51 const Array& hTimes, const Array& h, const std::string& name = std::string(),
52 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& alphaConstraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>(),
53 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& hConstraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
55 const Currency& currency, const Handle<TS>& termStructure, const std::vector<Date>& alphaDates,
56 const Array& alpha, const std::vector<Date>& hDates, const Array& h, const std::string& name = std::string(),
57 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& alphaConstraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>(),
58 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& hConstraint = QuantLib::ext::make_shared<QuantLib::NoConstraint>());
59
60 Real zeta(const Time t) const override;
61 Real H(const Time t) const override;
62 Real alpha(const Time t) const override;
63 Real kappa(const Time t) const override;
64 Real Hprime(const Time t) const override;
65 Real Hprime2(const Time t) const override;
66 const Array& parameterTimes(const Size) const override;
67 const QuantLib::ext::shared_ptr<Parameter> parameter(const Size) const override;
68 void update() const override;
69
70protected:
71 Real direct(const Size i, const Real x) const override;
72 Real inverse(const Size j, const Real y) const override;
73
74private:
75 void initialize(const Array& alpha, const Array& h);
76};
77
78// implementation
79
80template <class TS>
82 const Currency& currency, const Handle<TS>& termStructure, const Array& alphaTimes, const Array& alpha,
83 const Array& hTimes, const Array& h, const std::string& name,
84 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& alphaConstraint,
85 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& hConstraint)
86 : Lgm1fParametrization<TS>(currency, termStructure, name),
87 PiecewiseConstantHelper11(alphaTimes, hTimes, alphaConstraint, hConstraint) {
88 initialize(alpha, h);
89}
90
91template <class TS>
93 const Currency& currency, const Handle<TS>& termStructure, const std::vector<Date>& alphaDates, const Array& alpha,
94 const std::vector<Date>& hDates, const Array& h, const std::string& name,
95 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& alphaConstraint,
96 const QuantLib::ext::shared_ptr<QuantLib::Constraint>& hConstraint)
97 : Lgm1fParametrization<TS>(currency, termStructure, name),
98 PiecewiseConstantHelper11(alphaDates, hDates, termStructure, alphaConstraint, hConstraint) {
99 initialize(alpha, h);
100}
101
102template <class TS> void Lgm1fPiecewiseLinearParametrization<TS>::initialize(const Array& alpha, const Array& h) {
103 QL_REQUIRE(helper1().t().size() + 1 == alpha.size(),
104 "alpha size (" << alpha.size() << ") inconsistent to times size (" << helper1().t().size() << ")");
105 QL_REQUIRE(helper2().t().size() + 1 == h.size(),
106 "h size (" << h.size() << ") inconsistent to times size (" << helper1().t().size() << ")");
107 // store raw parameter values
108 for (Size i = 0; i < helper1().p()->size(); ++i) {
109 helper1().p()->setParam(i, inverse(0, alpha[i]));
110 }
111 for (Size i = 0; i < helper2().p()->size(); ++i) {
112 helper2().p()->setParam(i, inverse(1, h[i]));
113 }
114 update();
115}
116
117// inline
118
119template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::direct(const Size i, const Real x) const {
120 return i == 0 ? helper1().direct(x) : helper2().direct(x);
121}
122
123template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::inverse(const Size i, const Real y) const {
124 return i == 0 ? helper1().inverse(y) : helper2().inverse(y);
125}
126
127template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::zeta(const Time t) const {
128 return helper1().int_y_sqr(t) / (this->scaling_ * this->scaling_);
129}
130
131template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::H(const Time t) const {
132 return this->scaling_ * helper2().int_y_sqr(t) + this->shift_;
133}
134
135template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::alpha(const Time t) const {
136 return helper1().y(t) / this->scaling_;
137}
138
139template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::kappa(const Time) const {
140 return 0.0; // almost everywhere
141}
142
143template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::Hprime(const Time t) const {
144 return this->scaling_ * helper2().y(t);
145}
146
147template <class TS> inline Real Lgm1fPiecewiseLinearParametrization<TS>::Hprime2(const Time) const {
148 return 0.0; // almost everywhere
149}
150
151template <class TS> inline void Lgm1fPiecewiseLinearParametrization<TS>::update() const {
153 helper1().update();
154 helper2().update();
155}
156
157template <class TS> inline const Array& Lgm1fPiecewiseLinearParametrization<TS>::parameterTimes(const Size i) const {
158 QL_REQUIRE(i < 2, "parameter " << i << " does not exist, only have 0..1");
159 if (i == 0)
160 return helper1().t();
161 else
162 return helper2().t();
163 ;
164}
165
166template <class TS>
167inline const QuantLib::ext::shared_ptr<Parameter> Lgm1fPiecewiseLinearParametrization<TS>::parameter(const Size i) const {
168 QL_REQUIRE(i < 2, "parameter " << i << " does not exist, only have 0..1");
169 if (i == 0)
170 return helper1().p();
171 else
172 return helper2().p();
173}
174
175// typedef
176
178
179} // namespace QuantExt
180
181#endif
const Handle< TS > termStructure() const
Lgm1fPiecewiseLinearParametrization(const Currency &currency, const Handle< TS > &termStructure, const Array &alphaTimes, const Array &alpha, const Array &hTimes, const Array &h, const std::string &name=std::string(), const QuantLib::ext::shared_ptr< QuantLib::Constraint > &alphaConstraint=QuantLib::ext::make_shared< QuantLib::NoConstraint >(), const QuantLib::ext::shared_ptr< QuantLib::Constraint > &hConstraint=QuantLib::ext::make_shared< QuantLib::NoConstraint >())
Real direct(const Size i, const Real x) const override
Real inverse(const Size j, const Real y) const override
const QuantLib::ext::shared_ptr< Parameter > parameter(const Size) const override
const std::string & name() const
virtual const Currency & currency() const
Interest Rate Linear Gaussian Markov 1 factor parametrization.
QuantLib::SparseMatrix inverse(QuantLib::SparseMatrix m)
Lgm1fPiecewiseLinearParametrization< YieldTermStructure > IrLgm1fPiecewiseLinearParametrization
helper classes for piecewise constant parametrizations