QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
swapforwardmappings.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2006 Ferdinando Ametrano
5Copyright (C) 2006 Marco Bianchetti
6Copyright (C) 2006 Giorgio Facchinetti
7Copyright (C) 2006, 2008 Mark Joshi
8
9This file is part of QuantLib, a free-software/open-source library
10for financial quantitative analysts and developers - http://quantlib.org/
11
12QuantLib is free software: you can redistribute it and/or modify it
13under the terms of the QuantLib license. You should have received a
14copy of the license along with this program; if not, please email
15<quantlib-dev@lists.sf.net>. The license is also available online at
16<http://quantlib.org/license.shtml>.
17
18This program is distributed in the hope that it will be useful, but WITHOUT
19ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23#include <ql/models/marketmodels/swapforwardmappings.hpp>
24#include <ql/models/marketmodels/curvestate.hpp>
25#include <ql/models/marketmodels/marketmodel.hpp>
26#include <ql/models/marketmodels/evolutiondescription.hpp>
27#include <ql/models/marketmodels/curvestates/lmmcurvestate.hpp>
28#include <vector>
29
30namespace QuantLib {
31
32
34 Size startIndex,
35 Size endIndex,
36 Size numeraireIndex)
37 {
38 Real annuity = 0.0;
39 for (Size i=startIndex; i<endIndex; ++i)
40 annuity += cs.rateTaus()[i]*cs.discountRatio(i+1, numeraireIndex);
41 return annuity;
42 }
43
44 // compute derivative of swap-rate to underlying forward rate
46 Size startIndex,
47 Size endIndex,
48 Size forwardIndex)
49 {
50 if (forwardIndex < startIndex)
51 return 0.0;
52 if (forwardIndex >= endIndex)
53 return 0.0;
54
55 Real numerator = cs.discountRatio(startIndex, endIndex)-1;
56 Real swapAnnuity = annuity(cs, startIndex, endIndex,endIndex);
57
58 Real ratio = cs.rateTaus()[forwardIndex] /
59 (1 + cs.rateTaus()[forwardIndex] * cs.forwardRate(forwardIndex));
60
61 Real part1 = ratio*(numerator+1)/swapAnnuity;
62 Real part2 = numerator/(swapAnnuity*swapAnnuity);
63
64 if (forwardIndex >=1)
65 part2 *= ratio* annuity(cs, startIndex, forwardIndex, endIndex);
66 else
67 part2 = 0.0;
68
69 return part1-part2;
70 }
71
73 {
74 Size n = cs.numberOfRates();
75 const std::vector<Rate>& f = cs.forwardRates();
76 const std::vector<Time>& tau = cs.rateTaus();
77
78 // coterminal floating leg values
79 std::vector<Real> a(n);
80 for (Size k=0; k<n; ++k)
81 a[k] = cs.discountRatio(k,n)-1.0;
82 //p[k]-p[n];
83
84 Matrix jacobian = Matrix(n, n, 0.0);
85 for (Size i=0; i<n; ++i) { // i = swap rate index
86 for (Size j=i; j<n; ++j) { // j = forward rate index
87 Real bi = cs.coterminalSwapAnnuity(n,i);
88 Real bj = cs.coterminalSwapAnnuity(n,j);
89 jacobian[i][j] =
90 // p[j+1]*tau[j]/b[i] +
91 tau[j]/cs.coterminalSwapAnnuity(j+1,i) +
92 // tau[j]/(1.0+f[j]*tau[j]) *
93 tau[j]/(1.0+f[j]*tau[j]) *
94 // (-a[j]*b[i]+a[i]*b[j])/(b[i]*b[i]);
95 (-a[j]*bi+a[i]*bj)/(bi*bi);
96
97 }
98 }
99 return jacobian;
100 }
101
103 const Spread displacement) {
104 Size n = cs.numberOfRates();
106 const std::vector<Rate>& f = cs.forwardRates();
107 const std::vector<Rate>& sr = cs.coterminalSwapRates();
108 for (Size i=0; i<n; ++i)
109 for (Size j=i; j<n; ++j)
110 zMatrix[i][j] *= (f[j]+displacement)/(sr[i]+displacement);
111 return zMatrix;
112 }
113
114
116 {
117 Size n = cs.numberOfRates();
118
119 Matrix jacobian = Matrix(n, n, 0.0);
120 for (Size i=0; i<n; ++i) // i = swap rate index
121 for (Size j=0; j<n; ++j) // j = forward rate index
122 jacobian[i][j] =swapDerivative(cs, 0, i+1, j);
123
124 return jacobian;
125 }
126
128 const Size spanningForwards)
129 {
130 Size n = cs.numberOfRates();
131
132 Matrix jacobian = Matrix(n, n, 0.0);
133 for (Size i=0; i<n; ++i) // i = swap rate index
134 for (Size j=0; j<n; ++j) // j = forward rate index
135 jacobian[i][j] =swapDerivative(cs, i, std::min(n,i+spanningForwards), j);
136
137 return jacobian;
138 }
139
141 const Spread displacement)
142 {
143 Size n = cs.numberOfRates();
145 const std::vector<Rate>& f = cs.forwardRates();
146 std::vector<Rate> sr(n);
147
148 for (Size i=0; i<n; ++i)
149 sr[i] = cs.cmSwapRate(0,i+1);
150
151 for (Size i=0; i<n; ++i)
152 for (Size j=i; j<n; ++j)
153 zMatrix[i][j] *= (f[j]+displacement)/(sr[i]+displacement);
154 return zMatrix;
155 }
156
158 const Size spanningForwards,
159 const Spread displacement)
160 {
161 Size n = cs.numberOfRates();
162 Matrix zMatrix = cmSwapForwardJacobian(cs,spanningForwards);
163 const std::vector<Rate>& f = cs.forwardRates();
164 std::vector<Rate> sr(n);
165
166 for (Size i=0; i<n; ++i)
167 sr[i] = cs.cmSwapRate(i,spanningForwards);
168
169 for (Size i=0; i<n; ++i)
170 for (Size j=i; j<n; ++j)
171 zMatrix[i][j] *= (f[j]+displacement)/(sr[i]+displacement);
172 return zMatrix;
173 }
174
176 Size startIndex,
177 Size endIndex)
178 {
179 QL_REQUIRE(startIndex < endIndex, "start index must be before end index in swaptionImpliedVolatility");
180
181 LMMCurveState cs(volStructure.evolution().rateTimes());
182 cs.setOnForwardRates(volStructure.initialRates());
183 Real displacement = volStructure.displacements()[0];
184
185 Matrix cmsZed(cmSwapZedMatrix(cs, endIndex-startIndex,displacement));
186
187 Real variance=0.0;
188
189 Size index=0;
190
191 const EvolutionDescription& evolution(volStructure.evolution());
192 Size factors = volStructure.numberOfFactors();
193
194 while (index < evolution.numberOfSteps() && startIndex >= evolution.firstAliveRate()[index] )
195 {
196 const Matrix& thisPseudo = volStructure.pseudoRoot(index);
197 Real thisVariance =0.0;
198
199 for (Size f=0; f < factors; ++f)
200 {
201 Real sum=0.0;
202
203 for (Size j=startIndex; j < endIndex;++j)
204 {
205 sum += cmsZed[startIndex][j]*thisPseudo[j][f];
206
207 }
208 thisVariance += sum*sum;
209
210 }
211 variance += thisVariance;
212 ++index;
213
214 }
215
216 Real expiry = evolution.rateTimes()[startIndex];
217 return std::sqrt(variance/expiry);
218
219 }
220
221}
Curve state for market-model simulations
Definition: curvestate.hpp:41
Size numberOfRates() const
Definition: curvestate.hpp:58
virtual Rate coterminalSwapAnnuity(Size numeraire, Size i) const =0
virtual Rate cmSwapRate(Size i, Size spanningForwards) const =0
virtual Rate forwardRate(Size i) const =0
virtual const std::vector< Rate > & coterminalSwapRates() const =0
virtual const std::vector< Rate > & forwardRates() const =0
const std::vector< Time > & rateTaus() const
Definition: curvestate.hpp:61
virtual Real discountRatio(Size i, Size j) const =0
Market-model evolution description.
const std::vector< Time > & rateTimes() const
const std::vector< Size > & firstAliveRate() const
Curve state for Libor market models
void setOnForwardRates(const std::vector< Rate > &fwdRates, Size firstValidIndex=0)
base class for market models
Definition: marketmodel.hpp:39
virtual Size numberOfFactors() const =0
virtual const std::vector< Rate > & initialRates() const =0
virtual const Matrix & pseudoRoot(Size i) const =0
virtual const EvolutionDescription & evolution() const =0
virtual const std::vector< Spread > & displacements() const =0
Matrix used in linear algebra.
Definition: matrix.hpp:41
static Matrix coinitialSwapZedMatrix(const CurveState &cs, Spread displacement)
static Matrix coterminalSwapZedMatrix(const CurveState &cs, Spread displacement)
static Matrix cmSwapZedMatrix(const CurveState &cs, Size spanningForwards, Spread displacement)
static Real swaptionImpliedVolatility(const MarketModel &volStructure, Size startIndex, Size endIndex)
static Real swapDerivative(const CurveState &cs, Size startIndex, Size endIndex, Size forwardIndex)
compute derivative of swap-rate to underlying forward rate
static Matrix coinitialSwapForwardJacobian(const CurveState &cs)
static Real annuity(const CurveState &cs, Size startIndex, Size endIndex, Size numeraireIndex)
compute annuity of arbitrary swap-rate
static Matrix cmSwapForwardJacobian(const CurveState &cs, Size spanningForwards)
static Matrix coterminalSwapForwardJacobian(const CurveState &cs)
QL_REAL Real
real number
Definition: types.hpp:50
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