File: swapforwardmappings.cpp

package info (click to toggle)
quantlib 1.4-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 34,340 kB
  • ctags: 64,765
  • sloc: cpp: 291,654; ansic: 21,484; sh: 11,209; makefile: 4,923; lisp: 86
file content (228 lines) | stat: -rw-r--r-- 8,139 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2006 Ferdinando Ametrano
Copyright (C) 2006 Marco Bianchetti
Copyright (C) 2006 Giorgio Facchinetti
Copyright (C) 2006, 2008 Mark Joshi

This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/

QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license.  You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/models/marketmodels/swapforwardmappings.hpp>
#include <ql/models/marketmodels/curvestate.hpp>
#include <ql/models/marketmodels/marketmodel.hpp>
#include <ql/models/marketmodels/evolutiondescription.hpp>
#include <ql/models/marketmodels/curvestates/lmmcurvestate.hpp>
#include <vector>

namespace QuantLib {


    Real SwapForwardMappings::annuity(const CurveState& cs,
                                      Size startIndex,
                                      Size endIndex,
                                      Size numeraireIndex)
    {
        Real annuity = 0.0;
        for (Size i=startIndex; i<endIndex; ++i)
            annuity += cs.rateTaus()[i]*cs.discountRatio(i+1, numeraireIndex);
        return annuity;
    }

    // compute derivative of swap-rate to underlying forward rate
    Real SwapForwardMappings::swapDerivative(const CurveState& cs,
                                             Size startIndex,
                                             Size endIndex,
                                             Size forwardIndex)
    {
        if (forwardIndex < startIndex)
            return 0.0;
        if (forwardIndex >= endIndex)
            return 0.0;

        Real numerator = cs.discountRatio(startIndex, endIndex)-1;
        Real swapAnnuity = annuity(cs, startIndex, endIndex,endIndex);

        Real ratio = cs.rateTaus()[forwardIndex] /
            (1 + cs.rateTaus()[forwardIndex] * cs.forwardRate(forwardIndex));

        Real part1 = ratio*(numerator+1)/swapAnnuity;
        Real part2 = numerator/(swapAnnuity*swapAnnuity);

        if (forwardIndex >=1)
            part2 *= ratio* annuity(cs, startIndex, forwardIndex, endIndex);
        else 
            part2 = 0.0;

        return part1-part2;
    }

    Disposable<Matrix>
    SwapForwardMappings::coterminalSwapForwardJacobian(const CurveState& cs)
    {
        Size n = cs.numberOfRates();
        const std::vector<Rate>& f = cs.forwardRates();
        const std::vector<Time>& tau = cs.rateTaus();

        // coterminal floating leg values
        std::vector<Real> a(n);
        for (Size k=0; k<n; ++k)
            a[k] = cs.discountRatio(k,n)-1.0;
        //p[k]-p[n];

        Matrix jacobian = Matrix(n, n, 0.0);
        for (Size i=0; i<n; ++i) {     // i = swap rate index
            for (Size j=i; j<n; ++j) { // j = forward rate index
                Real bi = cs.coterminalSwapAnnuity(n,i);
                Real bj = cs.coterminalSwapAnnuity(n,j);
                jacobian[i][j] =
                    //   p[j+1]*tau[j]/b[i] +
                    tau[j]/cs.coterminalSwapAnnuity(j+1,i) +
                    // tau[j]/(1.0+f[j]*tau[j]) *
                    tau[j]/(1.0+f[j]*tau[j]) *
                    //    (-a[j]*b[i]+a[i]*b[j])/(b[i]*b[i]);
                    (-a[j]*bi+a[i]*bj)/(bi*bi);

            }
        }
        return jacobian;
    }

    Disposable<Matrix>
    SwapForwardMappings::coterminalSwapZedMatrix(const CurveState& cs,
                                                 const Spread displacement) {
            Size n = cs.numberOfRates();
            Matrix zMatrix = coterminalSwapForwardJacobian(cs);
            const std::vector<Rate>& f = cs.forwardRates();
            const std::vector<Rate>& sr = cs.coterminalSwapRates();
            for (Size i=0; i<n; ++i)
                for (Size j=i; j<n; ++j)
                    zMatrix[i][j] *= (f[j]+displacement)/(sr[i]+displacement);
            return zMatrix;
    }


    Disposable<Matrix>
    SwapForwardMappings::coinitialSwapForwardJacobian(const CurveState& cs)
    {
        Size n = cs.numberOfRates();

        Matrix jacobian = Matrix(n, n, 0.0);
        for (Size i=0; i<n; ++i)      // i = swap rate index
            for (Size j=0; j<n; ++j)  // j = forward rate index
                jacobian[i][j] =swapDerivative(cs, 0, i+1, j);

        return jacobian;
    }

    Disposable<Matrix>
    SwapForwardMappings::cmSwapForwardJacobian(const CurveState& cs,
                                               const Size spanningForwards)
    {
        Size n = cs.numberOfRates();

        Matrix jacobian = Matrix(n, n, 0.0);
        for (Size i=0; i<n; ++i)      // i = swap rate index
            for (Size j=0; j<n; ++j)  // j = forward rate index
                jacobian[i][j] =swapDerivative(cs, i, std::min(n,i+spanningForwards), j);

        return jacobian;
    }

    Disposable<Matrix>
    SwapForwardMappings::coinitialSwapZedMatrix(const CurveState& cs,
                                                const Spread displacement)
    {
        Size n = cs.numberOfRates();
        Matrix zMatrix = coinitialSwapForwardJacobian(cs);
        const std::vector<Rate>& f = cs.forwardRates();
        std::vector<Rate> sr(n);

        for (Size i=0; i<n; ++i)
            sr[i] = cs.cmSwapRate(0,i+1);

        for (Size i=0; i<n; ++i)
            for (Size j=i; j<n; ++j)
                zMatrix[i][j] *= (f[j]+displacement)/(sr[i]+displacement);
        return zMatrix;
    }

    Disposable<Matrix>
    SwapForwardMappings::cmSwapZedMatrix(const CurveState& cs,
                                         const Size spanningForwards,
                                         const Spread displacement)
    {
        Size n = cs.numberOfRates();
        Matrix zMatrix = cmSwapForwardJacobian(cs,spanningForwards);
        const std::vector<Rate>& f = cs.forwardRates();
        std::vector<Rate> sr(n);

        for (Size i=0; i<n; ++i)
            sr[i] = cs.cmSwapRate(i,spanningForwards);

        for (Size i=0; i<n; ++i)
            for (Size j=i; j<n; ++j)
                zMatrix[i][j] *= (f[j]+displacement)/(sr[i]+displacement);
        return zMatrix;
    }

      Real 
      SwapForwardMappings::swaptionImpliedVolatility(const MarketModel& volStructure,
                                  Size startIndex,
                                  Size endIndex)
      {
          QL_REQUIRE(startIndex < endIndex, "start index must be before end index in swaptionImpliedVolatility");

          LMMCurveState cs(volStructure.evolution().rateTimes());
          cs.setOnForwardRates(volStructure.initialRates());
          Real displacement = volStructure.displacements()[0];

          Matrix cmsZed(cmSwapZedMatrix(cs, endIndex-startIndex,displacement));

          Real variance=0.0;

          Size index=0;

          const EvolutionDescription& evolution(volStructure.evolution());
          Size factors = volStructure.numberOfFactors();

          while (index < evolution.numberOfSteps() && startIndex >= evolution.firstAliveRate()[index] )
          {
              const Matrix& thisPseudo = volStructure.pseudoRoot(index);
              Real thisVariance =0.0;
              
              for (Size f=0; f < factors; ++f)
              {
                  Real sum=0.0;

                  for (Size j=startIndex; j < endIndex;++j)
                  {
                      sum += cmsZed[startIndex][j]*thisPseudo[j][f];

                  }
                  thisVariance += sum*sum;

              }
              variance += thisVariance;
              ++index;

          }

          Real expiry = evolution.rateTimes()[startIndex];
          return std::sqrt(variance/expiry);

      }

}