QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
capletcoterminalalphacalibration.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Mark Joshi
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/math/matrixutilities/pseudosqrt.hpp>
22#include <ql/models/marketmodels/models/alphafinder.hpp>
23#include <ql/models/marketmodels/models/alphaformconcrete.hpp>
24#include <ql/models/marketmodels/models/capletcoterminalalphacalibration.hpp>
25#include <ql/models/marketmodels/models/piecewiseconstantvariance.hpp>
26#include <ql/models/marketmodels/swapforwardmappings.hpp>
27#include <utility>
28
29namespace QuantLib {
30
32 const EvolutionDescription& evolution,
33 const ext::shared_ptr<PiecewiseConstantCorrelation>& corr,
34 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >& displacedSwapVariances,
35 const std::vector<Volatility>& mktCapletVols,
36 const ext::shared_ptr<CurveState>& cs,
37 Spread displacement,
38 const std::vector<Real>& alphaInitial,
39 const std::vector<Real>& alphaMax,
40 const std::vector<Real>& alphaMin,
41 bool maximizeHomogeneity,
42 ext::shared_ptr<AlphaForm> parametricForm)
44 evolution, corr, displacedSwapVariances, mktCapletVols, cs, displacement),
45 alphaInitial_(alphaInitial), alphaMax_(alphaMax), alphaMin_(alphaMin),
46 maximizeHomogeneity_(maximizeHomogeneity), parametricForm_(std::move(parametricForm)),
47 alpha_(numberOfRates_), a_(numberOfRates_), b_(numberOfRates_) {
48 if (!parametricForm_)
50 ext::shared_ptr<AlphaForm>(new AlphaFormLinearHyperbolic(evolution.rateTimes()));
51
52 QL_REQUIRE(numberOfRates_==alphaInitial.size(),
53 "mismatch between number of rates (" << numberOfRates_ <<
54 ") and alphaInitial (" << alphaInitial.size() << ")");
55
56 QL_REQUIRE(numberOfRates_==alphaMax.size(),
57 "mismatch between number of rates (" << numberOfRates_ <<
58 ") and alphaMax (" << alphaMax.size() << ")");
59
60 QL_REQUIRE(numberOfRates_==alphaMin.size(),
61 "mismatch between number of rates (" << numberOfRates_ <<
62 ") and alphaMin (" << alphaMin.size() << ")");
63 }
64
66 const EvolutionDescription& evolution,
68 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >& displacedSwapVariances,
69 const std::vector<Volatility>& capletVols,
70 const CurveState& cs,
71 const Spread displacement,
72
73 const std::vector<Real>& alphaInitial,
74 const std::vector<Real>& alphaMax,
75 const std::vector<Real>& alphaMin,
76 bool maximizeHomogeneity,
77 const ext::shared_ptr<AlphaForm>& parametricForm,
78
79 const Size numberOfFactors,
80 Integer maxIterations,
81 Real tolerance,
82
83 std::vector<Real>& alpha,
84 std::vector<Real>& a,
85 std::vector<Real>& b,
86
87 std::vector<Matrix>& swapCovariancePseudoRoots) {
88
90 displacedSwapVariances, capletVols, cs);
91
92 Size numberOfSteps = evolution.numberOfSteps();
93 Size numberOfRates = evolution.numberOfRates();
94 const std::vector<Time>& rateTimes = evolution.rateTimes();
95
96 QL_REQUIRE(numberOfFactors<=numberOfRates,
97 "number of factors (" << numberOfFactors <<
98 ") cannot be greater than numberOfRates (" <<
99 numberOfRates << ")");
100 QL_REQUIRE(numberOfFactors>0,
101 "number of factors (" << numberOfFactors <<
102 ") must be greater than zero");
103
105
106 alpha.resize(numberOfRates);
107 a.resize(numberOfRates);
108 b.resize(numberOfRates);
109
110 // factor reduction
111 std::vector<Matrix> corrPseudo(corr.times().size());
112 for (Size i=0; i<corrPseudo.size(); ++i)
113 corrPseudo[i] = rankReducedSqrt(corr.correlation(i),
114 numberOfFactors, 1.0,
116
117 // get Zinverse, we can get wj later
118 Matrix zedMatrix =
120 Matrix invertedZedMatrix = inverse(zedMatrix);
121
122 // vectors for new vol
123 std::vector<std::vector<Volatility> > newVols;
124 std::vector<Volatility> theseNewVols(numberOfRates);
125 std::vector<Volatility> firstRateVols(numberOfRates);
126 firstRateVols[0] = std::sqrt(displacedSwapVariances[0]->variances()[0]);
127 std::vector<Volatility> secondRateVols(numberOfRates);
128 std::vector<Real> correlations(numberOfRates);
129 newVols.push_back(firstRateVols);
130
131
132 alpha[0] = alphaInitial[0]; // has no effect on anything in any case
133 a[0] = b[0] = 1.0; // no modifications to swap vol for first rate
134
135 AlphaFinder solver(parametricForm);
136
137 // final caplet and swaption are the same, so we skip that case
138 for (Size i=0; i<numberOfRates-1; ++i) {
139 // we will calibrate caplet on forward rate i,
140 // we will do this by modifying the vol of swap rate i+1
141 const std::vector<Real>& var =
142 displacedSwapVariances[i+1]->variances();
143 for (Size j =0; j < i+2; ++j)
144 secondRateVols[j] = std::sqrt(var[j]);
145
146 for (Size k=0; k < i+1; k++) {
147 Real correlation=0.0;
148 for (Size l=0; l < numberOfFactors; ++l) {
149 Real term1 = corrPseudo[k][i][l];
150 Real term2 = corrPseudo[k][i+1][l];
151 correlation += term1*term2;
152 }
153 correlations[k] = correlation;
154 }
155
156 Real w0 = invertedZedMatrix[i][i];
157 Real w1 = invertedZedMatrix[i][i+1];
158 // w0 adjustment
159 for (Size k = i+2; k <invertedZedMatrix.columns(); ++k)
160 w0+= invertedZedMatrix[i][k];
161
162 Real targetVariance= capletVols[i]*capletVols[i]*rateTimes[i];
163
164 bool success;
165 if (maximizeHomogeneity)
166 success = solver.solveWithMaxHomogeneity(
167 alphaInitial[i+1] ,
168 i,
169 firstRateVols,
170 secondRateVols,
171 correlations,
172 w0,
173 w1,
174 targetVariance,
175 tolerance,
176 alphaMax[i+1],
177 alphaMin[i+1],
178 maxIterations,
179 alpha[i+1],
180 a[i+1],
181 b[i+1],
182 theseNewVols);
183 else
184 success = solver.solve(alphaInitial[i+1] ,
185 i,
186 firstRateVols,
187 secondRateVols,
188 correlations,
189 w0,
190 w1,
191 targetVariance,
192 tolerance,
193 alphaMax[i+1],
194 alphaMin[i+1],
195 maxIterations,
196 alpha[i+1],
197 a[i+1],
198 b[i+1],
199 theseNewVols);
200
201 if (!success) {
202 //++failures;
203 QL_FAIL("alpha form failure");
204 }
205
206 newVols.push_back(theseNewVols);
207 firstRateVols = theseNewVols;
208 }
209
210 swapCovariancePseudoRoots.resize(numberOfSteps);
211 for (Size k=0; k<numberOfSteps; ++k) {
212 swapCovariancePseudoRoots[k] = corrPseudo[k];
213 for (Size j=0; j<numberOfRates; ++j) {
214 Real coeff =newVols[j][k];
215 for (Size i=0; i<numberOfFactors; ++i)
216 swapCovariancePseudoRoots[k][j][i]*=coeff;
217 }
218 QL_ENSURE(swapCovariancePseudoRoots[k].rows()==numberOfRates,
219 "step " << k
220 << " abcd vol wrong number of rows: "
221 << swapCovariancePseudoRoots[k].rows()
222 << " instead of " << numberOfRates);
223 QL_ENSURE(swapCovariancePseudoRoots[k].columns()==numberOfFactors,
224 "step " << k
225 << " abcd vol wrong number of columns: "
226 << swapCovariancePseudoRoots[k].columns()
227 << " instead of " << numberOfFactors);
228 }
229
230 return failures;
231 }
232
234 Natural numberOfFactors,
235 Natural maxIterations,
236 Real tolerance) {
237
239 *corr_,
241 // not mktCapletVols_ but...
243 *cs_,
245
247 alphaMax_,
248 alphaMin_,
251
252 numberOfFactors,
253 maxIterations,
254 tolerance,
255
256 alpha_,
257 a_,
258 b_,
259
261 }
262}
bool solveWithMaxHomogeneity(Real alpha0, Integer stepindex, const std::vector< Volatility > &rateonevols, const std::vector< Volatility > &ratetwohomogeneousvols, const std::vector< Real > &correlations, Real w0, Real w1, Real targetVariance, Real tolerance, Real alphaMax, Real alphaMin, Integer steps, Real &alpha, Real &a, Real &b, std::vector< Volatility > &ratetwovols)
bool solve(Real alpha0, Integer stepindex, const std::vector< Volatility > &rateonevols, const std::vector< Volatility > &ratetwohomogeneousvols, const std::vector< Real > &correlations, Real w0, Real w1, Real targetVariance, Real tolerance, Real alphaMax, Real alphaMin, Integer steps, Real &alpha, Real &a, Real &b, std::vector< Volatility > &ratetwovols)
static Natural capletAlphaFormCalibration(const EvolutionDescription &evolution, const PiecewiseConstantCorrelation &corr, const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > &displacedSwapVariances, const std::vector< Volatility > &capletVols, const CurveState &cs, Spread displacement, const std::vector< Real > &alphaInitial, const std::vector< Real > &alphaMax, const std::vector< Real > &alphaMin, bool maximizeHomogeneity, const ext::shared_ptr< AlphaForm > &parametricForm, Size numberOfFactors, Integer steps, Real toleranceForAlphaSolving, std::vector< Real > &alpha, std::vector< Real > &a, std::vector< Real > &b, std::vector< Matrix > &swapCovariancePseudoRoots)
CTSMMCapletAlphaFormCalibration(const EvolutionDescription &evolution, const ext::shared_ptr< PiecewiseConstantCorrelation > &corr, const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > &displacedSwapVariances, const std::vector< Volatility > &capletVols, const ext::shared_ptr< CurveState > &cs, Spread displacement, const std::vector< Real > &alphaInitial, const std::vector< Real > &alphaMax, const std::vector< Real > &alphaMin, bool maximizeHomogeneity, ext::shared_ptr< AlphaForm > parametricForm=ext::shared_ptr< AlphaForm >())
Natural calibrationImpl_(Natural numberOfFactors, Natural maxIterations, Real tolerance) override
ext::shared_ptr< PiecewiseConstantCorrelation > corr_
ext::shared_ptr< CurveState > cs_
static void performChecks(const EvolutionDescription &evolution, const PiecewiseConstantCorrelation &corr, const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > &displacedSwapVariances, const std::vector< Volatility > &mktCapletVols, const CurveState &cs)
std::vector< ext::shared_ptr< PiecewiseConstantVariance > > displacedSwapVariances_
std::vector< Volatility > usedCapletVols_
std::vector< Matrix > swapCovariancePseudoRoots_
Curve state for market-model simulations
Definition: curvestate.hpp:41
Market-model evolution description.
const std::vector< Time > & rateTimes() const
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size columns() const
Definition: matrix.hpp:508
virtual const Matrix & correlation(Size i) const
virtual const std::vector< Time > & times() const =0
static Matrix coterminalSwapZedMatrix(const CurveState &cs, Spread displacement)
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
QL_INTEGER Integer
integer number
Definition: types.hpp:35
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
Matrix rankReducedSqrt(const Matrix &matrix, Size maxRank, Real componentRetainedPercentage, SalvagingAlgorithm::Type sa)
Definition: pseudosqrt.cpp:427
Matrix inverse(const Matrix &m)
Definition: matrix.cpp:44
STL namespace.