QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ctsmmcapletcalibration.cpp
Go to the documentation of this file.
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
30#include <utility>
31
32namespace QuantLib {
33
35 EvolutionDescription evolution,
36 ext::shared_ptr<PiecewiseConstantCorrelation> corr,
37 std::vector<ext::shared_ptr<PiecewiseConstantVariance> > displacedSwapVariances,
38 std::vector<Volatility> mktCapletVols,
39 ext::shared_ptr<CurveState> cs,
40 Spread displacement)
41 : evolution_(std::move(evolution)), corr_(std::move(corr)),
42 displacedSwapVariances_(std::move(displacedSwapVariances)),
43 mktCapletVols_(std::move(mktCapletVols)), mdlCapletVols_(evolution_.numberOfRates()),
44 mktSwaptionVols_(evolution_.numberOfRates()), mdlSwaptionVols_(evolution_.numberOfRates()),
45 cs_(std::move(cs)), displacement_(displacement), numberOfRates_(evolution_.numberOfRates()) {
48 }
49
50 const std::vector<Volatility>&
52 {
54 "index (" << i << ") must less than number of rates (" <<
55 numberOfRates_ << ")");
56 return displacedSwapVariances_[i]->volatilities();
57 }
58
59 const std::vector<Volatility>&
61 {
63 "index (" << i << ") must less than number of rates (" <<
64 numberOfRates_ << ")");
66 }
67
69 const EvolutionDescription& evolution,
71 const std::vector<ext::shared_ptr<
73 displacedSwapVariances,
74 const std::vector<Volatility>& mktCapletVols,
75 const CurveState& cs)
76 {
77 const std::vector<Time>& evolutionTimes = evolution.evolutionTimes();
78 QL_REQUIRE(evolutionTimes==corr.times(),
79 "evolutionTimes "
80 << io::sequence(evolutionTimes)
81 << " not equal to correlation times "
82 << io::sequence(corr.times()));
83
84 const std::vector<Time>& rateTimes = evolution.rateTimes();
85 QL_REQUIRE(rateTimes==cs.rateTimes(),
86 "mismatch between EvolutionDescription and "
87 "CurveState rate times");
88
89 Size numberOfRates = evolution.numberOfRates();
90 QL_REQUIRE(numberOfRates==displacedSwapVariances.size(),
91 "mismatch between EvolutionDescription number of rates ("
92 << numberOfRates << ") and displacedSwapVariances size ("
93 << displacedSwapVariances.size() << ")");
94 QL_REQUIRE(numberOfRates==corr.numberOfRates(),
95 "mismatch between EvolutionDescription number of rates ("
96 << numberOfRates << ") and corr number of rates ("
97 << corr.numberOfRates() << ")");
98 QL_REQUIRE(numberOfRates==mktCapletVols.size(),
99 "mismatch between EvolutionDescription number of rates ("
100 << numberOfRates << ") and mktCapletVols size ("
101 << mktCapletVols.size() << ")");
102 QL_REQUIRE(numberOfRates==cs.numberOfRates(),
103 "mismatch between EvolutionDescription number of rates ("
104 << numberOfRates << ") and CurveState number of rates ("
105 << cs.numberOfRates() << ")");
106
107 std::vector<Time> temp(rateTimes.begin(), rateTimes.end()-1);
108 QL_REQUIRE(temp==evolutionTimes,
109 "mismatch between evolutionTimes and rateTimes");
110
111 Volatility lastSwaptionVol =
112 displacedSwapVariances.back()->totalVolatility(numberOfRates-1);
113 QL_REQUIRE(close(lastSwaptionVol, mktCapletVols[numberOfRates-1]),
114 "last caplet vol (" << std::setprecision(16) <<
115 mktCapletVols[numberOfRates-1] <<
116 ") must be equal to last swaption vol (" <<
117 lastSwaptionVol << "); discrepancy is " <<
118 lastSwaptionVol-mktCapletVols[numberOfRates-1]);
119 }
120
122
123 Natural maxIterations,
124 Real capletVolTolerance,
125
126 Natural innerSolvingMaxIterations,
127 Real innerSolvingTolerance) {
128 // initialize results
129 calibrated_ = false;
130 failures_ = 987654321; // a positive large number
131 deformationSize_ = 987654321;
134
135 // initialize working variables
137
138 for (Size i=0; i<numberOfRates_; ++i)
139 mktSwaptionVols_[i]=displacedSwapVariances_[i]->totalVolatility(i);
140
141 std::vector<Spread> displacements(numberOfRates_,
143 const std::vector<Time>& rateTimes = evolution_.rateTimes();
144 Natural iterations = 0;
145
146 // calibration loop
147 do {
148 failures_ = calibrationImpl_(numberOfFactors,
149 innerSolvingMaxIterations,
150 innerSolvingTolerance);
151
152 ext::shared_ptr<MarketModel> ctsmm(new
154 rateTimes,
155 cs_->coterminalSwapRates(),
157 const Matrix& swaptionTotCovariance =
158 ctsmm->totalCovariance(numberOfRates_-1);
159
160 CotSwapToFwdAdapter flmm(ctsmm);
161 const Matrix& capletTotCovariance =
163
164 // check fit
167
168 for (Size i=0; i<numberOfRates_; ++i) {
169 mdlSwaptionVols_[i] = std::sqrt(swaptionTotCovariance[i][i]/rateTimes[i]);
170 Real swaptionError = std::fabs(mktSwaptionVols_[i]-mdlSwaptionVols_[i]);
171 swaptionRmsError_ += swaptionError*swaptionError;
172 swaptionMaxError_ = std::max(swaptionMaxError_, swaptionError);
173
174 mdlCapletVols_[i] = std::sqrt(capletTotCovariance[i][i]/rateTimes[i]);
175 Real capletError = std::fabs(mktCapletVols_[i]-mdlCapletVols_[i]);
176 capletRmsError_ += capletError*capletError;
177 capletMaxError_ = std::max(capletMaxError_, capletError);
178
179 if (i < numberOfRates_-1)
181 }
184 ++iterations;
185 } while (iterations<maxIterations &&
186 capletRmsError_>capletVolTolerance);
187
188 ext::shared_ptr<MarketModel> ctsmm(new
190 rateTimes,
191 cs_->coterminalSwapRates(),
193
195 for (Size i=0; i<numberOfRates_; ++i)
197 ctsmm->timeDependentVolatility(i));
198
199 // calculate deformationSize_ ??
200 calibrated_ = true;
201 return failures_==0;
202 }
203
204}
ext::shared_ptr< PiecewiseConstantCorrelation > corr_
std::vector< std::vector< Volatility > > timeDependentCalibratedSwaptionVols_
const std::vector< Volatility > & timeDependentCalibratedSwaptionVols(Size i) const
std::vector< Volatility > mktCapletVols_
ext::shared_ptr< CurveState > cs_
std::vector< Volatility > mktSwaptionVols_
bool calibrate(Natural numberOfFactors, Natural maxIterations, Real tolerance, Natural innerMaxIterations=100, Real innerTolerance=1e-8)
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< Spread > displacements() const
std::vector< Volatility > mdlSwaptionVols_
std::vector< ext::shared_ptr< PiecewiseConstantVariance > > displacedSwapVariances_
virtual Natural calibrationImpl_(Natural numberOfFactors, Natural innerMaxIterations, Real innerTolerance)=0
std::vector< Volatility > usedCapletVols_
const std::vector< Volatility > & timeDependentUnCalibratedSwaptionVols(Size i) const
const std::vector< Volatility > & mktCapletVols() const
std::vector< Volatility > mdlCapletVols_
CTSMMCapletCalibration(EvolutionDescription evolution, ext::shared_ptr< PiecewiseConstantCorrelation > corr, std::vector< ext::shared_ptr< PiecewiseConstantVariance > > displacedSwapVariances, std::vector< Volatility > mktCapletVols, ext::shared_ptr< CurveState > cs, Spread displacement)
std::vector< Matrix > swapCovariancePseudoRoots_
Curve state for market-model simulations
Definition: curvestate.hpp:41
Size numberOfRates() const
Definition: curvestate.hpp:58
const std::vector< Time > & rateTimes() const
Definition: curvestate.hpp:60
Market-model evolution description.
const std::vector< Time > & rateTimes() const
const std::vector< Time > & evolutionTimes() const
virtual const Matrix & totalCovariance(Size endIndex) const
Definition: marketmodel.cpp:40
Matrix used in linear algebra.
Definition: matrix.hpp:41
virtual Size numberOfRates() const =0
virtual const std::vector< Time > & times() const =0
floating-point comparisons
output manipulators
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
detail::sequence_holder< typename Container::const_iterator > sequence(const Container &c)
output STL-compliant containers as space-separated sequences
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
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
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
STL namespace.
pseudo square root of a real symmetric matrix
Utility functions for mapping between swap rate and forward rate.