QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
flatvol.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006, 2007 Ferdinando Ametrano
5 Copyright (C) 2006 Mark Joshi
6 Copyright (C) 2007 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/math/interpolations/linearinterpolation.hpp>
23#include <ql/math/matrixutilities/pseudosqrt.hpp>
24#include <ql/models/marketmodels/correlations/expcorrelations.hpp>
25#include <ql/models/marketmodels/correlations/timehomogeneousforwardcorrelation.hpp>
26#include <ql/models/marketmodels/models/flatvol.hpp>
27#include <ql/models/marketmodels/piecewiseconstantcorrelation.hpp>
28#include <utility>
29
30using std::vector;
31
32namespace QuantLib {
33
35 Time T, Time S,
36 Volatility v1, Volatility v2) {
37 QL_REQUIRE(t1<=t2,
38 "integrations bounds (" << t1 <<
39 "," << t2 << ") are in reverse order");
40 Time cutOff = std::min(S,T);
41 if (t1>=cutOff) {
42 return 0.0;
43 } else {
44 cutOff = std::min(t2, cutOff);
45 return (cutOff - t1)*v1*v2;
46 }
47 }
48
50 const vector<Volatility>& vols,
51 const ext::shared_ptr<PiecewiseConstantCorrelation>& corr,
52 const EvolutionDescription& evolution,
53 Size numberOfFactors,
54 const vector<Rate>& initialRates,
55 const vector<Spread>& displacements)
56 : numberOfFactors_(numberOfFactors),
57 numberOfRates_(initialRates.size()),
58 numberOfSteps_(evolution.evolutionTimes().size()),
59 initialRates_(initialRates),
60 displacements_(displacements),
61 evolution_(evolution),
62 pseudoRoots_(numberOfSteps_, Matrix(numberOfRates_, numberOfFactors_))
63 {
64 const vector<Time>& rateTimes = evolution.rateTimes();
65 QL_REQUIRE(numberOfRates_==rateTimes.size()-1,
66 "mismatch between number of rates (" << numberOfRates_ <<
67 ") and rate times");
68 QL_REQUIRE(numberOfRates_==displacements.size(),
69 "mismatch between number of rates (" << numberOfRates_ <<
70 ") and displacements (" << displacements.size() << ")");
71 QL_REQUIRE(numberOfRates_==vols.size(),
72 "mismatch between number of rates (" << numberOfRates_ <<
73 ") and vols (" << vols.size() << ")");
75 "number of rates (" << numberOfRates_ <<
76 ") greater than number of factors (" << numberOfFactors_
77 << ") times number of steps (" << numberOfSteps_ << ")");
79 "number of factors (" << numberOfFactors <<
80 ") cannot be greater than numberOfRates (" <<
81 numberOfRates_ << ")");
82 QL_REQUIRE(numberOfFactors>0,
83 "number of factors (" << numberOfFactors <<
84 ") must be greater than zero");
85
86 Time effStopTime = 0.0;
87 const vector<Time>& corrTimes = corr->times();
88 const vector<Time>& evolTimes = evolution.evolutionTimes();
90 for (Size k=0, kk=0; k<numberOfSteps_; ++k) {
91 // one covariance per evolution step
92 std::fill(covariance.begin(), covariance.end(), 0.0);
93
94 // there might be more than one correlation matrix
95 // in a single evolution step
96 for (; corrTimes[kk]<evolTimes[k]; ++kk) {
97 Time effStartTime = effStopTime;
98 effStopTime = corrTimes[kk];
99 const Matrix& corrMatrix = corr->correlation(kk);
100 for (Size i=0; i<numberOfRates_; ++i) {
101 for (Size j=i; j<numberOfRates_; ++j) {
102 Real cov = flatVolCovariance(effStartTime, effStopTime,
103 rateTimes[i], rateTimes[j],
104 vols[i], vols[j]);
105 covariance[i][j] += cov * corrMatrix[i][j];
106 }
107 }
108 }
109 // last part in the evolution step
110 Time effStartTime = effStopTime;
111 effStopTime = evolTimes[k];
112 const Matrix& corrMatrix = corr->correlation(kk);
113 for (Size i=0; i<numberOfRates_; ++i) {
114 for (Size j=i; j<numberOfRates_; ++j) {
115 Real cov = flatVolCovariance(effStartTime, effStopTime,
116 rateTimes[i], rateTimes[j],
117 vols[i], vols[j]);
118 covariance[i][j] += cov * corrMatrix[i][j];
119 }
120 }
121 // no more use for the kk-th correlation matrix
122 while (kk<corrTimes.size() && corrTimes[kk]<=evolTimes[k])
123 ++kk;
124
125 // make it symmetric
126 for (Size i=0; i<numberOfRates_; ++i) {
127 for (Size j=i+1; j<numberOfRates_; ++j) {
128 covariance[j][i] = covariance[i][j];
129 }
130 }
131
133 numberOfFactors, 1.0,
135
136 QL_ENSURE(pseudoRoots_[k].rows()==numberOfRates_,
137 "step " << k
138 << " flat vol wrong number of rows: "
139 << pseudoRoots_[k].rows()
140 << " instead of " << numberOfRates_);
141 QL_ENSURE(pseudoRoots_[k].columns()==numberOfFactors,
142 "step " << k
143 << " flat vol wrong number of columns: "
144 << pseudoRoots_[k].columns()
145 << " instead of " << numberOfFactors_);
146 }
147 }
148
149
151 Real beta,
152 vector<Time> times,
153 vector<Volatility> vols,
155 Spread displacement)
156 : longTermCorrelation_(longTermCorrelation), beta_(beta), times_(std::move(times)),
157 vols_(std::move(vols)), yieldCurve_(std::move(yieldCurve)), displacement_(displacement) {
159 vols_.begin());
162 }
163
164 ext::shared_ptr<MarketModel>
166 Size numberOfFactors) const {
167 const vector<Time>& rateTimes = evolution.rateTimes();
168 Size numberOfRates = rateTimes.size()-1;
169
170 vector<Rate> initialRates(numberOfRates);
171 for (Size i=0; i<numberOfRates; ++i)
172 initialRates[i] = yieldCurve_->forwardRate(rateTimes[i],
173 rateTimes[i+1],
174 Simple);
175
176 vector<Volatility> displacedVolatilities(numberOfRates);
177 for (Size i=0; i<numberOfRates; ++i) {
178 Volatility vol = // to be changes
179 volatility_(rateTimes[i]);
180 displacedVolatilities[i] =
181 initialRates[i]*vol/(initialRates[i]+displacement_);
182 }
183
184 vector<Spread> displacements(numberOfRates, displacement_);
185
186 Matrix correlations = exponentialCorrelations(evolution.rateTimes(),
188 beta_);
189 ext::shared_ptr<PiecewiseConstantCorrelation> corr(new
191 rateTimes));
192 return ext::shared_ptr<MarketModel>(new
193 FlatVol(displacedVolatilities,
194 corr,
195 evolution,
196 numberOfFactors,
197 initialRates,
198 displacements));
199 }
200
203 }
204
205}
206
Market-model evolution description.
const std::vector< Time > & rateTimes() const
const std::vector< Time > & evolutionTimes() const
Interpolation volatility_
Definition: flatvol.hpp:90
void update() override
Definition: flatvol.cpp:201
std::vector< Time > times_
Definition: flatvol.hpp:88
FlatVolFactory(Real longTermCorrelation, Real beta, std::vector< Time > times, std::vector< Volatility > vols, Handle< YieldTermStructure > yieldCurve, Spread displacement)
Definition: flatvol.cpp:150
std::vector< Volatility > vols_
Definition: flatvol.hpp:89
Handle< YieldTermStructure > yieldCurve_
Definition: flatvol.hpp:92
ext::shared_ptr< MarketModel > create(const EvolutionDescription &, Size numberOfFactors) const override
Definition: flatvol.cpp:165
const std::vector< Spread > & displacements() const override
Definition: flatvol.hpp:103
std::vector< Matrix > pseudoRoots_
Definition: flatvol.hpp:63
FlatVol(const std::vector< Volatility > &volatilities, const ext::shared_ptr< PiecewiseConstantCorrelation > &corr, const EvolutionDescription &evolution, Size numberOfFactors, const std::vector< Rate > &initialRates, const std::vector< Spread > &displacements)
Definition: flatvol.cpp:49
const EvolutionDescription & evolution() const override
Definition: flatvol.hpp:107
Size numberOfFactors_
Definition: flatvol.hpp:59
Size numberOfFactors() const override
Definition: flatvol.hpp:115
Shared handle to an observable.
Definition: handle.hpp:41
Linear interpolation between discrete points
virtual const Matrix & covariance(Size i) const
Definition: marketmodel.cpp:28
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_iterator begin() const
Definition: matrix.hpp:327
const_iterator end() const
Definition: matrix.hpp:335
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Real Spread
spreads on interest rates
Definition: types.hpp:74
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real flatVolCovariance(Time t1, Time t2, Time T, Time S, Volatility v1, Volatility v2)
Definition: flatvol.cpp:34
Matrix exponentialCorrelations(const std::vector< Time > &rateTimes, Real longTermCorr, Real beta, Real gamma, Time time)
Matrix rankReducedSqrt(const Matrix &matrix, Size maxRank, Real componentRetainedPercentage, SalvagingAlgorithm::Type sa)
Definition: pseudosqrt.cpp:427
STL namespace.