QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
smmdriftcalculator.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 Mark Joshi
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/models/marketmodels/driftcomputation/smmdriftcalculator.hpp>
22#include <ql/models/marketmodels/curvestates/coterminalswapcurvestate.hpp>
23
24namespace QuantLib {
25
27 const Matrix& pseudo,
28 const std::vector<Spread>& displacements,
29 const std::vector<Time>& taus,
30 Size numeraire,
31 Size alive)
32 : numberOfRates_(taus.size()), numberOfFactors_(pseudo.columns()),
33 numeraire_(numeraire), alive_(alive),
34 displacements_(displacements), oneOverTaus_(taus.size()),
35 pseudo_(pseudo),
36 tmp_(taus.size(), 0.0),
37 // zero initialization required for (used by) the last element
38 wkaj_(pseudo_.columns(), pseudo_.rows(), 0.0),
39 wkpj_(pseudo_.columns(), pseudo_.rows()+1, 0.0),
40 wkajshifted_(pseudo_.columns(), pseudo_.rows(), 0.0)
41 /*,
42 downs_(taus.size()), ups_(taus.size())*/ {
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_, "Alive out of bounds");
53 QL_REQUIRE(numeraire_<=numberOfRates_, "Numeraire larger than dim");
54 QL_REQUIRE(numeraire_>=alive, "Numeraire smaller than alive");
55
56 // Precompute 1/taus
57 for (Size i=0; i<taus.size(); ++i)
58 oneOverTaus_[i] = 1.0/taus[i];
59
60 // Compute covariance matrix from pseudoroot
62 C_ = pseudo_*pT;
63
64 // Compute lower and upper extrema for (non reduced) drift calculation
65 //for (Size i=alive_; i<numberOfRates_; ++i) {
66 // downs_[i] = std::min(i+1, numeraire_);
67 // ups_[i] = std::max(i+1, numeraire_);
68 //}
69 }
70
72 std::vector<Real>& drifts) const {
73 #if defined(QL_EXTRA_SAFETY_CHECKS)
74 QL_REQUIRE(drifts.size()==cs.numberOfRates(),
75 "drifts.size() <> numberOfRates");
76 #endif
77
78 // Compute drifts with factor reduction,
79 // using the pseudo square root of the covariance matrix.
80
81 const std::vector<Rate>& SR=cs.coterminalSwapRates();
82 // calculates and stores wkaj_, wkpj1_
83 // assuming terminal bond measure
84 // eq 5.4-5.7
85 const std::vector<Time>& taus=cs.rateTaus();
86 for (Size k=0; k<numberOfFactors_; ++k) {
87 // taken care in the constructor
88 // wkpj1_[k][numberOfRates_-1]= 0.0;
89 // wkaj_[k][numberOfRates_-1] = 0.0;
90 for (Integer j=numberOfRates_-2; j>=static_cast<Integer>(alive_)-1; --j) {
91 // < W(k) | P(j+1)/P(n) > =
92 // = SR(j+1) a(j+1,k) A(j+1) / P(n) + SR(j+1) < W(k) | A(j+1)/P(n) >
94 wkpj_[k][j+1]= SR[j+1] *
95 ( pseudo_[j+1][k] * annuity + wkaj_[k][j+1] )+
96 pseudo_[j+1][k]*displacements_[j+1]* annuity;
97
98 if (j >=static_cast<Integer>(alive_))
99 wkaj_[k][j] = wkpj_[k][j+1]*taus[j ]+wkaj_[k][j+1];
100 }
101 }
102
103
104 Real numeraireRatio = cs.discountRatio(numberOfRates_, numeraire_);
105
106// change to work for general numeraire
107 for (Size k=0; k<numberOfFactors_; ++k) {
108 // compute < Wk, PN/pn>
109 for (Size j=alive_; j<numberOfRates_; ++j)
110 {
112 + wkpj_[k][numeraire_]
113 *numeraireRatio;
114 }
115 }
116
117 // eq 5.3 (in log coordinates)
118 for (Size j=alive_; j<numberOfRates_; ++j) {
119 drifts[j] = 0.0;
120 for (Size k=0; k<numberOfFactors_; ++k) {
121 drifts[j] += wkajshifted_[k][j]*pseudo_[j][k];
122 }
123 }
124
125 }
126
127}
Curve state for coterminal-swap market models
Real discountRatio(Size i, Size j) const override
Rate coterminalSwapAnnuity(Size numeraire, Size i) const override
const std::vector< Rate > & coterminalSwapRates() 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
std::vector< Spread > displacements_
SMMDriftCalculator(const Matrix &pseudo, const std::vector< Spread > &displacements, const std::vector< Time > &taus, Size numeraire, Size alive)
void compute(const CoterminalSwapCurveState &cs, std::vector< Real > &drifts) const
Computes the drifts.
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