QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
smmdriftcalculator.cpp
Go to the documentation of this file.
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
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");
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 std::vector<Real> annuities(numberOfRates_);
87
88 for (Size j=0; j<numberOfRates_; ++j) {
89 annuities[j] = cs.coterminalSwapAnnuity(numberOfRates_, j);
90 }
91 for (Size k=0; k<numberOfFactors_; ++k) {
92 // taken care in the constructor
93 // wkpj1_[k][numberOfRates_-1]= 0.0;
94 // wkaj_[k][numberOfRates_-1] = 0.0;
95 for (Integer j=numberOfRates_-2; j>=static_cast<Integer>(alive_)-1; --j) {
96 // < W(k) | P(j+1)/P(n) > =
97 // = SR(j+1) a(j+1,k) A(j+1) / P(n) + SR(j+1) < W(k) | A(j+1)/P(n) >
98 Real annuity = annuities[j+1];
99 wkpj_[k][j+1]= SR[j+1] *
100 ( pseudo_[j+1][k] * annuity + wkaj_[k][j+1] )+
101 pseudo_[j+1][k]*displacements_[j+1]* annuity;
102
103 if (j >=static_cast<Integer>(alive_))
104 wkaj_[k][j] = wkpj_[k][j+1]*taus[j ]+wkaj_[k][j+1];
105 }
106 }
107
108
109 Real numeraireRatio = cs.discountRatio(numberOfRates_, numeraire_);
110
111// change to work for general numeraire
112 for (Size k=0; k<numberOfFactors_; ++k) {
113 // compute < Wk, PN/pn>
114 for (Size j=alive_; j<numberOfRates_; ++j)
115 {
116 wkajshifted_[k][j] = -wkaj_[k][j]/annuities[j]
117 + wkpj_[k][numeraire_]
118 *numeraireRatio;
119 }
120 }
121
122 // eq 5.3 (in log coordinates)
123 for (Size j=alive_; j<numberOfRates_; ++j) {
124 drifts[j] = 0.0;
125 for (Size k=0; k<numberOfFactors_; ++k) {
126 drifts[j] += wkajshifted_[k][j]*pseudo_[j][k];
127 }
128 }
129
130 }
131
132}
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.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
Drift computation for coterminal-swap market model.