QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
squarerootclvmodel.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2016 Klaus Spanderen
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
24#include <ql/processes/blackscholesprocess.hpp>
25#include <ql/processes/squarerootprocess.hpp>
26#include <ql/math/integrals/gaussianquadratures.hpp>
27
28#include <ql/experimental/models/squarerootclvmodel.hpp>
29#include <ql/methods/finitedifferences/utilities/gbsmrndcalculator.hpp>
30
31#include <boost/math/distributions/non_central_chi_squared.hpp>
32
33#include <utility>
34
35namespace QuantLib {
37 const ext::shared_ptr<GeneralizedBlackScholesProcess>& bsProcess,
38 ext::shared_ptr<SquareRootProcess> sqrtProcess,
39 std::vector<Date> maturityDates,
40 Size lagrangeOrder,
41 Real pMax,
42 Real pMin)
43 : pMax_(pMax), pMin_(pMin), bsProcess_(bsProcess), sqrtProcess_(std::move(sqrtProcess)),
44 maturityDates_(std::move(maturityDates)), lagrangeOrder_(lagrangeOrder),
45 rndCalculator_(ext::make_shared<GBSMRNDCalculator>(bsProcess)) {}
46
48 return rndCalculator_->cdf(k, bsProcess_->time(d));
49 }
50
51
53 return rndCalculator_->invcdf(q, bsProcess_->time(d));
54 }
55
57 const Date& d) const {
58
59 const Time t = bsProcess_->time(d);
60
61 const Real kappa = sqrtProcess_->a();
62 const Real theta = sqrtProcess_->b();
63 const Real sigma = sqrtProcess_->sigma();
64
65 const Real df = 4*theta*kappa/(sigma*sigma);
66 const Real ncp = 4*kappa*std::exp(-kappa*t)
67 / (sigma*sigma*(1-std::exp(-kappa*t)))*sqrtProcess_->x0();
68
69 return std::make_pair(df, ncp);
70 }
71
72
74
75 const std::pair<Real, Real> p = nonCentralChiSquaredParams(d);
76
78 GaussNonCentralChiSquaredPolynomial(p.first, p.second))
79 .x();
80
81 std::sort(x.begin(), x.end());
82
83 const boost::math::non_central_chi_squared_distribution<Real>
84 dist(p.first, p.second);
85
86 const Real xMin = std::max(x.front(),
87 (pMin_ == Null<Real>())
88 ? 0.0 : boost::math::quantile(dist, pMin_));
89
90 const Real xMax = std::min(x.back(),
91 (pMax_ == Null<Real>())
92 ? QL_MAX_REAL : boost::math::quantile(dist, pMax_));
93
94 const Real b = xMin - x.front();
95 const Real a = (xMax - xMin)/(x.back() - x.front());
96
97 for (Real& i : x) {
98 i = a * i + b;
99 }
100
101 return x;
102 }
103
105
106 const Array x = collocationPointsX(d);
107 const std::pair<Real, Real> params = nonCentralChiSquaredParams(d);
108 const boost::math::non_central_chi_squared_distribution<Real>
109 dist(params.first, params.second);
110
111 Array s(x.size());
112 for (Size i=0, n=s.size(); i < n; ++i) {
113 const Real q = boost::math::cdf(dist, x[i]);
114
115 s[i] = invCDF(d, q);
116 }
117
118 return s;
119 }
120
121 ext::function<Real(Time, Real)> SquareRootCLVModel::g() const {
122 calculate();
123 return g_;
124 }
125
127 g_ = ext::function<Real(Time, Real)>(MappingFunction(*this));
128 }
129
131 const SquareRootCLVModel& model)
132 : s_(ext::make_shared<Matrix>(
133 model.maturityDates_.size(), model.lagrangeOrder_)),
134 x_(ext::make_shared<Matrix>(
135 model.maturityDates_.size(), model.lagrangeOrder_)) {
136
137 std::vector<Date> maturityDates = model.maturityDates_;
138 std::sort(maturityDates.begin(), maturityDates.end());
139
140 const ext::shared_ptr<GeneralizedBlackScholesProcess>&
141 bsProcess = model.bsProcess_;
142
143 for (Size i=0, n = maturityDates.size(); i < n; ++i) {
144 const Date maturityDate = maturityDates[i];
145
146 const Array x = model.collocationPointsX(maturityDate);
147 const Array y = model.collocationPointsY(maturityDate);
148
149 std::copy(x.begin(), x.end(), x_->row_begin(i));
150 std::copy(y.begin(), y.end(), s_->row_begin(i));
151
152 const Time maturity = bsProcess->time(maturityDate);
153
154 interpl.insert(
155 std::make_pair(maturity,
156 ext::make_shared<LagrangeInterpolation>(
157 x_->row_begin(i), x_->row_end(i),
158 s_->row_begin(i))));
159 }
160 }
161
163 const interpl_type::const_iterator ge = interpl.lower_bound(t);
164
165 if (close_enough(ge->first, t)) {
166 return (*ge->second)(x, true);
167 }
168
169 QL_REQUIRE(ge != interpl.end() && ge != interpl.begin(),
170 "extrapolation to large or small t is not allowed");
171
172 const Time t1 = ge->first;
173 const Real y1 = (*ge->second)(x, true);
174
175 interpl_type::const_iterator lt = ge;
176 std::advance(lt, -1);
177
178 const Time t0 = lt->first;
179 const Real y0 = (*lt->second)(x, true);
180
181 return y0 + (y1 - y0)/(t1 - t0)*(t - t0);
182 }
183}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Real back() const
Definition: array.hpp:458
Size size() const
dimension of the array
Definition: array.hpp:495
Real front() const
Definition: array.hpp:451
const_iterator begin() const
Definition: array.hpp:503
Concrete date class.
Definition: date.hpp:125
Integral of a 1-dimensional function using the Gauss quadratures method.
virtual void calculate() const
Definition: lazyobject.hpp:253
Matrix used in linear algebra.
Definition: matrix.hpp:41
template class providing a null value for a given type.
Definition: null.hpp:76
MappingFunction(const SquareRootCLVModel &model)
const std::vector< Date > maturityDates_
void performCalculations() const override
const ext::shared_ptr< GeneralizedBlackScholesProcess > bsProcess_
SquareRootCLVModel(const ext::shared_ptr< GeneralizedBlackScholesProcess > &bsProcess, ext::shared_ptr< SquareRootProcess > sqrtProcess, std::vector< Date > maturityDates, Size lagrangeOrder, Real pMax=Null< Real >(), Real pMin=Null< Real >())
const ext::shared_ptr< SquareRootProcess > sqrtProcess_
Real invCDF(const Date &d, Real q) const
Real cdf(const Date &d, Real x) const
ext::function< Real(Time, Real)> g_
const ext::shared_ptr< GBSMRNDCalculator > rndCalculator_
std::pair< Real, Real > nonCentralChiSquaredParams(const Date &d) const
Array collocationPointsX(const Date &d) const
Array collocationPointsY(const Date &d) const
ext::function< Real(Time, Real)> g() const
#define QL_MAX_REAL
Definition: qldefines.hpp:176
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
STL namespace.