QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
cmsmmdriftcalculator.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 François du Vignaud
6 Copyright (C) 2007 Mark Joshi
7
8This file is part of QuantLib, a free-software/open-source library
9for financial quantitative analysts and developers - http://quantlib.org/
10
11QuantLib is free software: you can redistribute it and/or modify it
12under the terms of the QuantLib license. You should have received a
13copy of the license along with this program; if not, please email
14<quantlib-dev@lists.sf.net>. The license is also available online at
15<http://quantlib.org/license.shtml>.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/models/marketmodels/driftcomputation/cmsmmdriftcalculator.hpp>
23#include <ql/models/marketmodels/curvestates/cmswapcurvestate.hpp>
24
25namespace QuantLib {
26
28 const Matrix& pseudo,
29 const std::vector<Spread>& displacements,
30 const std::vector<Time>& taus,
31 Size numeraire,
32 Size alive,
33 Size spanningFwds)
34 : numberOfRates_(taus.size()), numberOfFactors_(pseudo.columns()),
35 numeraire_(numeraire), alive_(alive),
36 displacements_(displacements), oneOverTaus_(taus.size()),
37 pseudo_(pseudo), tmp_(taus.size(), 0.0),
38 PjPnWk_(numberOfFactors_,1+taus.size()),
39 wkaj_(numberOfFactors_, taus.size()),
40 wkajN_(numberOfFactors_, taus.size()),
41 downs_(taus.size()), ups_(taus.size()),
42 spanningFwds_(spanningFwds) {
43
44 // Check requirements
45 QL_REQUIRE(numberOfRates_>0, "Dim out of range");
46 QL_REQUIRE(displacements.size() == numberOfRates_,
47 "Displacements out of range");
48 QL_REQUIRE(pseudo.rows()==numberOfRates_,
49 "pseudo.rows() not consistent with dim");
50 QL_REQUIRE(pseudo.columns()>0 && pseudo.columns()<=numberOfRates_,
51 "pseudo.rows() not consistent with pseudo.columns()");
52 QL_REQUIRE(alive<numberOfRates_,
53 "Alive out of bounds");
54 QL_REQUIRE(numeraire_<=numberOfRates_,
55 "Numeraire larger than dim");
56 QL_REQUIRE(numeraire_>=alive,
57 "Numeraire smaller than alive");
58
59 // Precompute 1/taus
60 for (Size i=0; i<taus.size(); ++i)
61 oneOverTaus_[i] = 1.0/taus[i];
62
63 // Compute covariance matrix from pseudoroot
65 C_ = pseudo_*pT;
66
67 // Compute lower and upper extrema for (non reduced) drift calculation
68 for (Size i=alive_; i<numberOfRates_; ++i) {
69 downs_[i] = std::min(i+1, numeraire_);
70 ups_[i] = std::max(i+1, numeraire_);
71 }
72 }
73
75 std::vector<Real>& drifts) const {
76 #if defined(QL_EXTRA_SAFETY_CHECKS)
77 QL_REQUIRE(drifts.size()==cs.numberOfRates(),
78 "drifts.size() <> numberOfRates");
79 #endif
80
81 const std::vector<Time>& taus = cs.rateTaus();
82 // final bond is numeraire
83
84 // Compute cross variations
85 for (Size k=0; k<PjPnWk_.rows(); ++k) {
86 PjPnWk_[k][numberOfRates_]=0.0;
87 wkaj_[k][numberOfRates_-1]=0.0;
88
89 for (Integer j=static_cast<Integer>(numberOfRates_)-2;
90 j>=static_cast<Integer>(alive_)-1; --j)
91 {
92 Real sr = cs.cmSwapRate(j+1,spanningFwds_);
93 Integer endIndex =
94 std::min<Integer>(j + static_cast<Integer>(spanningFwds_) + 1,
95 static_cast<Integer>(numberOfRates_));
96 Real first = sr * wkaj_[k][j+1];
98 * (sr+displacements_[j+1])
99 *pseudo_[j+1][k];
100 Real third = PjPnWk_[k][endIndex];
101 PjPnWk_[k][j+1] = first
102 + second
103 + third;
104
105 if (j>=static_cast<Integer>(alive_))
106 {
107 wkaj_[k][j] = wkaj_[k][j+1] + PjPnWk_[k][j+1]*taus[j];
108
109 if (j+spanningFwds_+1 <= numberOfRates_)
110 wkaj_[k][j] -= PjPnWk_[k][endIndex]*taus[endIndex-1];
111 }
112
113 }
114 }
115
117 //Real PnOverPN = 1.0;
118
119 for (Size j=alive_; j<numberOfRates_; ++j)
120 for (Size k=0; k<numberOfFactors_; ++k)
121 wkajN_[k][j] = wkaj_[k][j]*PnOverPN
123
124
125
126 for (Size j=alive_; j<numberOfRates_; ++j)
127 {
128 drifts[j]=0.0;
129 for (Size k=0; k<numberOfFactors_; ++k)
130 {
131 drifts[j] += pseudo_[j][k]*wkajN_[k][j];
132 }
133 drifts[j] /= -cs.cmSwapAnnuity(numeraire_,j,spanningFwds_);
134 }
135 }
136
137}
CMSMMDriftCalculator(const Matrix &pseudo, const std::vector< Spread > &displacements, const std::vector< Time > &taus, Size numeraire, Size alive, Size spanningFwds)
void compute(const CMSwapCurveState &cs, std::vector< Real > &drifts) const
Computes the drifts.
Curve state for constant-maturity-swap market models
Real discountRatio(Size i, Size j) const override
Rate cmSwapAnnuity(Size numeraire, Size i, Size spanningForwards) const override
Rate cmSwapRate(Size i, Size spanningForwards) const override
Size numberOfRates() const
Definition: curvestate.hpp:58
const std::vector< Time > & rateTaus() const
Definition: curvestate.hpp:61
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Matrix transpose(const Matrix &m)
Definition: matrix.hpp:700