File: cmsmmdriftcalculator.cpp

package info (click to toggle)
quantlib 1.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 30,760 kB
  • sloc: cpp: 232,809; ansic: 21,483; sh: 11,108; makefile: 4,717; lisp: 86
file content (136 lines) | stat: -rw-r--r-- 5,369 bytes parent folder | download | duplicates (3)
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2007 Ferdinando Ametrano
 Copyright (C) 2007 Franois du Vignaud
 Copyright (C) 2007 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/driftcomputation/cmsmmdriftcalculator.hpp>
#include <ql/models/marketmodels/curvestates/cmswapcurvestate.hpp>

namespace QuantLib {

    CMSMMDriftCalculator::CMSMMDriftCalculator(
                                const Matrix& pseudo,
                                const std::vector<Spread>& displacements,
                                const std::vector<Time>& taus,
                                Size numeraire,
                                Size alive,
                                Size spanningFwds)
    : numberOfRates_(taus.size()), numberOfFactors_(pseudo.columns()),
      isFullFactor_(numberOfFactors_==numberOfRates_ ? true : false),
      numeraire_(numeraire), alive_(alive),
      displacements_(displacements), oneOverTaus_(taus.size()),
      pseudo_(pseudo), tmp_(taus.size(), 0.0),
      PjPnWk_(numberOfFactors_,1+taus.size()),
      wkaj_(numberOfFactors_, taus.size()),
      wkajN_(numberOfFactors_, taus.size()),
      downs_(taus.size()), ups_(taus.size()),
      spanningFwds_(spanningFwds) {

        // Check requirements
        QL_REQUIRE(numberOfRates_>0, "Dim out of range");
        QL_REQUIRE(displacements.size() == numberOfRates_,
                   "Displacements out of range");
        QL_REQUIRE(pseudo.rows()==numberOfRates_,
                   "pseudo.rows() not consistent with dim");
        QL_REQUIRE(pseudo.columns()>0 && pseudo.columns()<=numberOfRates_,
                   "pseudo.rows() not consistent with pseudo.columns()");
        QL_REQUIRE(alive<numberOfRates_,
                   "Alive out of bounds");
        QL_REQUIRE(numeraire_<=numberOfRates_,
                   "Numeraire larger than dim");
        QL_REQUIRE(numeraire_>=alive,
                   "Numeraire smaller than alive");

        // Precompute 1/taus
        for (Size i=0; i<taus.size(); ++i)
            oneOverTaus_[i] = 1.0/taus[i];

        // Compute covariance matrix from pseudoroot
        const Disposable<Matrix> pT = transpose(pseudo_);
        C_ = pseudo_*pT;

        // Compute lower and upper extrema for (non reduced) drift calculation
        for (Size i=alive_; i<numberOfRates_; ++i) {
            downs_[i] = std::min(i+1, numeraire_);
            ups_[i]   = std::max(i+1, numeraire_);
        }
    }

    void CMSMMDriftCalculator::compute(const CMSwapCurveState& cs,
                                       std::vector<Real>& drifts) const {
        #if defined(QL_EXTRA_SAFETY_CHECKS)
        QL_REQUIRE(drifts.size()==cs.numberOfRates(),
                   "drifts.size() <> numberOfRates");
        #endif

        const std::vector<Time>& taus = cs.rateTaus();
        // final bond is numeraire

        // Compute cross variations
        for (Size k=0; k<PjPnWk_.rows(); ++k) {
            PjPnWk_[k][numberOfRates_]=0.0;
            wkaj_[k][numberOfRates_-1]=0.0;

            for (Integer j=static_cast<Integer>(numberOfRates_)-2;
                 j>=static_cast<Integer>(alive_)-1; --j)
            {
                double sr = cs.cmSwapRate(j+1,spanningFwds_);
                Integer endIndex = std::min(j+spanningFwds_+1,numberOfRates_);
                Real first = sr * wkaj_[k][j+1];
                Real second = cs.cmSwapAnnuity(numberOfRates_,j+1,spanningFwds_)
                * (sr+displacements_[j+1])
                *pseudo_[j+1][k];
                Real third = PjPnWk_[k][endIndex];
                PjPnWk_[k][j+1] = first
                + second
                + third;

                if (j>=static_cast<Integer>(alive_))
                {
                    wkaj_[k][j] = wkaj_[k][j+1] + PjPnWk_[k][j+1]*taus[j];

                    if (j+spanningFwds_+1 <= numberOfRates_)
                        wkaj_[k][j] -= PjPnWk_[k][endIndex]*taus[endIndex-1];
                }

            }
        }

        Real PnOverPN = cs.discountRatio(numberOfRates_, numeraire_);
        //Real PnOverPN = 1.0;

        for (Size j=alive_; j<numberOfRates_; ++j)
            for (Size k=0; k<numberOfFactors_; ++k)
                wkajN_[k][j] =  wkaj_[k][j]*PnOverPN
                    -PjPnWk_[k][numeraire_]*PnOverPN*cs.cmSwapAnnuity(numeraire_,j,spanningFwds_);



        for (Size j=alive_; j<numberOfRates_; ++j)
        {
            drifts[j]=0.0;
            for (Size k=0; k<numberOfFactors_; ++k)
            {
                drifts[j] += pseudo_[j][k]*wkajN_[k][j];
            }
            drifts[j] /= -cs.cmSwapAnnuity(numeraire_,j,spanningFwds_);
        }
    }

}