File: ratepseudorootjacobian.cpp

package info (click to toggle)
quantlib 1.41-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,480 kB
  • sloc: cpp: 400,885; makefile: 6,547; python: 214; sh: 150; lisp: 86
file content (320 lines) | stat: -rw-r--r-- 10,938 bytes parent folder | download
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*

Copyright (C) 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
<https://www.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/pathwisegreeks/ratepseudorootjacobian.hpp>
#include <utility>

namespace QuantLib
{


    RatePseudoRootJacobianNumerical::RatePseudoRootJacobianNumerical(const Matrix& pseudoRoot,
        Size aliveIndex,
        Size numeraire,
        const std::vector<Time>& taus,
        const std::vector<Matrix>& pseudoBumps,
        const std::vector<Spread>& displacements)
        :
    pseudoRoot_(pseudoRoot),
        aliveIndex_(aliveIndex),
        taus_(taus),
        displacements_(displacements),
        numberBumps_(pseudoBumps.size()),
        factors_(pseudoRoot.columns()),
        drifts_(taus.size()),
        bumpedRates_(taus.size())
    {
        Size numberRates= taus.size();

        QL_REQUIRE(pseudoRoot_.rows()==numberRates,
            "pseudoRoot_.rows()<> taus.size()");

        QL_REQUIRE(displacements_.size()==numberRates,
            "displacements_.size()<> taus.size()");

        QL_REQUIRE(drifts_.size()==numberRates,
            "drifts_.size()<> taus.size()");

        for (Size i=0; i < pseudoBumps.size(); ++i)
        {
            QL_REQUIRE(pseudoBumps[i].rows()==numberRates,
                "pseudoBumps[i].rows()<> taus.size() with i =" << i);

            QL_REQUIRE(pseudoBumps[i].columns()==factors_,
                "pseudoBumps[i].columns()<> factors with i = " << i);


            Matrix pseudo(pseudoRoot_);
            pseudo += pseudoBumps[i];
            pseudoBumped_.push_back(pseudo);
            driftsComputers_.emplace_back(pseudo, displacements, taus, numeraire, aliveIndex);
        }

    }


    void RatePseudoRootJacobianNumerical::getBumps(const std::vector<Rate>& oldRates,
        const std::vector<Real>& , // not used in the numerical implementation
        const std::vector<Rate>& newRates,
        const std::vector<Real>& gaussians,
        Matrix& B)
    {
        Size numberRates = taus_.size();

        QL_REQUIRE(B.rows()==numberBumps_,
            "B.rows()<> numberBumps_");

        QL_REQUIRE(B.columns()==taus_.size(),
            "B.columns()<> number of rates");

        for (Size i =0; i < numberBumps_; ++i)
        {
            const Matrix& pseudo = pseudoBumped_[i];
            driftsComputers_[i].compute(oldRates,
                drifts_);

            for (Size j =0; j < aliveIndex_; ++j)
                B[i][j]=0.0;

            for (Size j=aliveIndex_; j < numberRates; ++j)
            {
                bumpedRates_[j] = std::log(oldRates[j]+displacements_[j]);

                for (Size k=0; k < factors_; ++k)
                    bumpedRates_[j] += -0.5*pseudo[j][k]*pseudo[j][k];

                bumpedRates_[j] +=drifts_[j];

                for (Size k=0; k < factors_; ++k)
                    bumpedRates_[j] += pseudo[j][k]*gaussians[k];

                bumpedRates_[j] = std::exp(bumpedRates_[j]);
                bumpedRates_[j] -= displacements_[j];
                Real tmp = bumpedRates_[j] - newRates[j];

                B[i][j] =  tmp;
            }
        }

    }

    RatePseudoRootJacobian::RatePseudoRootJacobian(const Matrix& pseudoRoot,
                                                   Size aliveIndex,
                                                   Size numeraire,
                                                   const std::vector<Time>& taus,
                                                   const std::vector<Matrix>& pseudoBumps,
                                                   std::vector<Spread> displacements)
    : pseudoRoot_(pseudoRoot), aliveIndex_(aliveIndex), taus_(taus), pseudoBumps_(pseudoBumps),
      displacements_(std::move(displacements)), numberBumps_(pseudoBumps.size()),
      factors_(pseudoRoot.columns()),
      //   bumpedRates_(taus.size()),
      e_(pseudoRoot.rows(), pseudoRoot.columns()), ratios_(taus_.size()) {
        Size numberRates= taus.size();

        QL_REQUIRE(aliveIndex == numeraire,
            "we can do only do discretely compounding MM acount so aliveIndex must equal numeraire");

        QL_REQUIRE(pseudoRoot_.rows()==numberRates,
            "pseudoRoot_.rows()<> taus.size()");

        QL_REQUIRE(displacements_.size()==numberRates,
            "displacements_.size()<> taus.size()");


        for (Size i=0; i < pseudoBumps.size(); ++i)
        {
            QL_REQUIRE(pseudoBumps[i].rows()==numberRates,
                "pseudoBumps[i].rows()<> taus.size() with i =" << i);

            QL_REQUIRE(pseudoBumps[i].columns()==factors_,
                "pseudoBumps[i].columns()<> factors with i = " << i);




        }

        for (Size i=0; i < numberRates; ++i)
        {
            allDerivatives_.emplace_back(numberRates, factors_);
        }
    }


    void RatePseudoRootJacobian::getBumps(const std::vector<Rate>& oldRates,
        const std::vector<Real>& discountRatios,
        const std::vector<Rate>& newRates,
        const std::vector<Real>& gaussians,
        Matrix& B)
    {
          Size numberRates = taus_.size();

         QL_REQUIRE(B.rows() == numberBumps_, "we need B.rows() which is " << B.rows() << " to equal numberBumps_ which is "  << numberBumps_);
         QL_REQUIRE(B.columns() == numberRates, "we need B.columns() which is " << B.columns() << " to equal numberRates which is "  << numberRates);


        for (Size j=aliveIndex_; j < numberRates; ++j)
            ratios_[j] = (oldRates[j] + displacements_[j])*discountRatios[j+1];

        for (Size f=0; f < factors_; ++f)
        {
            e_[aliveIndex_][f] = 0;

            for (Size j= aliveIndex_+1; j < numberRates; ++j)
                e_[j][f] = e_[j-1][f] + ratios_[j-1]*pseudoRoot_[j-1][f];
        }


        for (Size f=0; f < factors_; ++f)
            for (Size j=aliveIndex_; j < numberRates; ++j)
            {
                for (Size k= aliveIndex_; k < j ; ++k)
                    allDerivatives_[j][k][f] = newRates[j]*ratios_[k]*taus_[k]*pseudoRoot_[j][f];

                // GG don't seem to have the 2, this term is miniscule in any case
                Real tmp = //2*
                    2*ratios_[j]*taus_[j]*pseudoRoot_[j][f];
                tmp -=  pseudoRoot_[j][f];
                tmp += e_[j][f]*taus_[j];
                tmp += gaussians[f];
                tmp *= (newRates[j]+displacements_[j]);


                allDerivatives_[j][j][f] =tmp;

                for (Size k= j+1; k < numberRates ; ++k)
                    allDerivatives_[j][k][f]=0.0;


            }


        for (Size i =0; i < numberBumps_; ++i)
        {
            Size j=0;

            for (; j < aliveIndex_; ++j)
            {
                B[i][j]=0.0;
            }
            for (; j < numberRates; ++j)
            {
                Real sum =0.0;

                for (Size k=aliveIndex_; k < numberRates; ++k)
                    for (Size f=0; f < factors_; ++f)
                        sum += pseudoBumps_[i][k][f]*allDerivatives_[j][k][f];
                B[i][j] =sum;

            }
        }

    }

    RatePseudoRootJacobianAllElements::RatePseudoRootJacobianAllElements(
        const Matrix& pseudoRoot,
        Size aliveIndex,
        Size numeraire,
        const std::vector<Time>& taus,
        std::vector<Spread> displacements)
    : pseudoRoot_(pseudoRoot), aliveIndex_(aliveIndex), taus_(taus),
      displacements_(std::move(displacements)), factors_(pseudoRoot.columns()),
      //   bumpedRates_(taus.size()),
      e_(pseudoRoot.rows(), pseudoRoot.columns()), ratios_(taus_.size()) {
        Size numberRates= taus.size();

        QL_REQUIRE(aliveIndex == numeraire,
            "we can do only do discretely compounding MM acount so aliveIndex must equal numeraire");

        QL_REQUIRE(pseudoRoot_.rows()==numberRates,
            "pseudoRoot_.rows()<> taus.size()");

        QL_REQUIRE(displacements_.size()==numberRates,
            "displacements_.size()<> taus.size()");
    }


    void RatePseudoRootJacobianAllElements::getBumps(const std::vector<Rate>& oldRates,
        const std::vector<Real>& discountRatios,
        const std::vector<Rate>& newRates,
        const std::vector<Real>& gaussians,
        std::vector<Matrix>& B)
    {
          Size numberRates = taus_.size();

           QL_REQUIRE(B.size() == numberRates, "we need B.size() which is " << B.size() << " to equal numberRates which is "  << numberRates);
           for (auto& j : B)
               QL_REQUIRE(j.columns() == factors_ && j.rows() == numberRates,
                          "we need B[j].rows() which is "
                              << j.rows() << " to equal numberRates which is " << numberRates
                              << " and B[j].columns() which is " << j.columns()
                              << " to be equal to factors which is " << factors_);


           for (Size j = aliveIndex_; j < numberRates; ++j)
               ratios_[j] = (oldRates[j] + displacements_[j]) * discountRatios[j + 1];

           for (Size f = 0; f < factors_; ++f) {
               e_[aliveIndex_][f] = 0;

               for (Size j = aliveIndex_ + 1; j < numberRates; ++j)
                   e_[j][f] = e_[j - 1][f] + ratios_[j - 1] * pseudoRoot_[j - 1][f];
        }

        // nullify B for rates that have already reset
        for (Size j=0; j < aliveIndex_; ++j)
            for (Size k=0; k < numberRates; ++k)
                for (Size f=0; f < factors_; ++f)
                      B[j][k][f] =0.0;


        for (Size f=0; f < factors_; ++f)
            for (Size j=aliveIndex_; j < numberRates; ++j)
            {
                for (Size k= aliveIndex_; k < j ; ++k)
                    B[j][k][f] = newRates[j]*ratios_[k]*taus_[k]*pseudoRoot_[j][f];

                Real tmp = 2*ratios_[j]*taus_[j]*pseudoRoot_[j][f];
                tmp -=  pseudoRoot_[j][f];
                tmp += e_[j][f]*taus_[j];
                tmp += gaussians[f];
                tmp *= (newRates[j]+displacements_[j]);


                B[j][j][f] =tmp;

                for (Size k= 0; k < aliveIndex_ ; ++k)
                    B[j][k][f]=0.0;

                for (Size k= j+1; k < numberRates ; ++k)
                    B[j][k][f]=0.0;


            }
    }

    
}