QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bumpinstrumentjacobian.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
5Copyright (C) 2008 Mark Joshi
6
7This file is part of QuantLib, a free-software/open-source library
8for financial quantitative analysts and developers - http://quantlib.org/
9
10QuantLib is free software: you can redistribute it and/or modify it
11under the terms of the QuantLib license. You should have received a
12copy 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
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25
26namespace QuantLib
27{
28
29
31 const std::vector<Swaption>& swaptions,
32 const std::vector<Cap>& caps)
33 : bumps_(bumps), swaptions_(swaptions), caps_(caps), computed_(false,swaptions.size()+caps.size()),
34 derivatives_(swaptions.size()+caps.size(),std::vector<Real>(bumps.numberBumps())),
35 bumpMatrix_(swaptions.size()+caps.size(),bumps_.numberBumps())
36 {
38 allComputed_=false;
39
40 }
41
43 QL_REQUIRE(j < swaptions_.size()+caps_.size(), "too high index passed to VolatilityBumpInstrumentJacobian::derivativesVolatility");
44
45 if (computed_[j])
46 return derivatives_[j];
47
48 derivatives_[j].resize(bumps_.numberBumps());
50
51
52 Real sizesq=0.0;
53 computed_[j] = true;
54
55 Size initj = j;
56
57 if ( j < swaptions_.size()) // ok its a swaptions
58 {
59 SwaptionPseudoDerivative thisPseudo(bumps_.associatedModel(), swaptions_[j].startIndex_,swaptions_[j].endIndex_);
60
61 for (Size k=0; k < bumps_.numberBumps(); ++k)
62 {
63 Real v =0.0;
64
65 for (Size i= bumps_.allBumps()[k].stepBegin(); i < bumps_.allBumps()[k].stepEnd(); ++i)
66 {
67 const Matrix& fullDerivative = thisPseudo.volatilityDerivative(i);
68 for (Size f= bumps_.allBumps()[k].factorBegin(); f < bumps_.allBumps()[k].factorEnd(); ++f)
69 for (Size r= bumps_.allBumps()[k].rateBegin(); r < bumps_.allBumps()[k].rateEnd(); ++r)
70 v += fullDerivative[r][f];
71 }
72
73 derivatives_[j][k] =v;
74 sizesq+= v*v;
75 }
76
77 }
78 else // its a cap
79 {
80 j-= swaptions_.size();
81
82
83 CapPseudoDerivative thisPseudo(bumps_.associatedModel(), caps_[j].strike_, caps_[j].startIndex_,caps_[j].endIndex_,1.0); // ifrst df shouldn't make any difference
84
85
86
87 for (Size k=0; k < bumps_.numberBumps(); ++k)
88 {
89 Real v =0.0;
90
91 for (Size i= bumps_.allBumps()[k].stepBegin(); i < bumps_.allBumps()[k].stepEnd(); ++i)
92 {
93 const Matrix& fullDerivative = thisPseudo.volatilityDerivative(i);
94 for (Size f= bumps_.allBumps()[k].factorBegin(); f < bumps_.allBumps()[k].factorEnd(); ++f)
95 for (Size r= bumps_.allBumps()[k].rateBegin(); r < bumps_.allBumps()[k].rateEnd(); ++r)
96 v += fullDerivative[r][f];
97 }
98
99 sizesq += v*v;
100
101 derivatives_[initj][k] =v;
102 }
103
104 }
105
106 for (Size k=0; k < bumps_.numberBumps(); ++k)
107 {
108 bumpMatrix_[initj][k] = onePercentBumps_[initj][k] = 0.01 * derivatives_[initj][k]/sizesq;
109
110 }
111
112
113
114 return derivatives_[initj];
115 }
116
117
120
121 return onePercentBumps_[j];
122 }
123
125 {
126 if (!allComputed_)
127 for (Size i=0; i <swaptions_.size()+caps_.size(); ++i)
129
130 allComputed_ =true;
131
132
133 return bumpMatrix_;
134 }
135
136
137
139 const std::vector<VolatilityBumpInstrumentJacobian::Swaption>& swaptions,
140 const std::vector<VolatilityBumpInstrumentJacobian::Cap>& caps,
141 Real multiplierCutOff,
142 Real tolerance) :
143 derivativesProducer_(bumps,swaptions,caps),
144 multiplierCutOff_(multiplierCutOff),
145 tolerance_(tolerance)
146 {
147
148
149
150 }
151
152 void OrthogonalizedBumpFinder::GetVegaBumps(std::vector<std::vector<Matrix> >& theBumps) const
153 {
156 tolerance_ );
157
158
159 Size numberRestrictedBumps(projector.numberValidVectors());
160
161 ext::shared_ptr<MarketModel> marketmodel(derivativesProducer_.getInputBumps().associatedModel());
162 const EvolutionDescription& evolution(marketmodel->evolution());
163
164 Size numberSteps = evolution.numberOfSteps();
165 Size numberRates = evolution.numberOfRates();
166 Size factors = marketmodel->numberOfFactors();
167
168 theBumps.resize(numberSteps);
169 // recall that the bumps: We do the outermost vector by time step and inner one by which vega.
170
171 for (auto& theBump : theBumps)
172 theBump.resize(numberRestrictedBumps);
173
174 Matrix modelMatrix(numberRates, factors,0.0);
175
176 for (Size i=0; i< numberSteps; ++i)
177 for (Size j=0; j < numberRestrictedBumps; ++j)
178 theBumps[i][j] = modelMatrix;
179
180 const std::vector<VegaBumpCluster>& bumpClusters(derivativesProducer_.getInputBumps().allBumps());
181
182
183 Size bumpIndex =0;
184
185 for (Size instrument=0; instrument < projector.validVectors().size(); ++instrument)
186 {
187 if (projector.validVectors()[instrument])
188 {
189 for (Size cluster =0; cluster< bumpClusters.size(); ++cluster)
190 {
191 Real magnitude = projector.GetVector(instrument)[cluster];
192
193 for (Size step = bumpClusters[cluster].stepBegin(); step < bumpClusters[cluster].stepEnd(); ++step)
194 for (Size rate = bumpClusters[cluster].rateBegin(); rate < bumpClusters[cluster].rateEnd(); ++rate)
195 for (Size factor = bumpClusters[cluster].factorBegin(); factor < bumpClusters[cluster].factorEnd(); ++factor)
196 theBumps[step][bumpIndex][rate][factor] = magnitude;
197 }
198
199 ++bumpIndex;
200
201
202 }
203
204 }
205
206
207
208 }
209}
const Matrix & volatilityDerivative(Size i) const
Market-model evolution description.
Matrix used in linear algebra.
Definition: matrix.hpp:41
const std::valarray< bool > & validVectors() const
const std::vector< Real > & GetVector(Size index) const
VolatilityBumpInstrumentJacobian derivativesProducer_
OrthogonalizedBumpFinder(const VegaBumpCollection &bumps, const std::vector< VolatilityBumpInstrumentJacobian::Swaption > &swaptions, const std::vector< VolatilityBumpInstrumentJacobian::Cap > &caps, Real multiplierCutOff, Real tolerance)
void GetVegaBumps(std::vector< std::vector< Matrix > > &theBumps) const
const Matrix & volatilityDerivative(Size i) const
const ext::shared_ptr< MarketModel > & associatedModel() const
const std::vector< VegaBumpCluster > & allBumps() const
const VegaBumpCollection & getInputBumps() const
VolatilityBumpInstrumentJacobian(const VegaBumpCollection &bumps, const std::vector< Swaption > &swaptions, const std::vector< Cap > &caps)
std::vector< std::vector< Real > > derivatives_
std::vector< Real > onePercentBump(Size j) const
std::vector< std::vector< Real > > onePercentBumps_
std::vector< Real > derivativesVolatility(Size j) const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
STL namespace.
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v