QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
forwardforwardmappings.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
5Copyright (C) 2007 Mark Joshi
6
7This file is part of QuantLib, a free-software/open-source library
8for financial quantitative analysts and developers - http://quantlib.org/
9
10QuantLib is free software: you can redistribute it and/or modify it
11under the terms of the QuantLib license. You should have received a
12copy 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
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
24#include <vector>
25
26namespace QuantLib {
27
29 Size multiplier,
30 Size offset) {
31
32 Size n = cs.numberOfRates();
33
34 QL_REQUIRE(offset < multiplier, "offset must be less than period in"
35 " forward forward mappings");
36 Size k = (n-offset)/multiplier;
37
38 const std::vector<Time>& tau = cs.rateTaus();
39
40 Matrix jacobian = Matrix(k, n, 0.0);
41
42 Size m=offset;
43 for (Size l=0; l < k; ++l)
44 {
45 Real df = cs.discountRatio(m,m+multiplier);
46 Real bigTau = cs.rateTimes()[m+multiplier]
47 - cs.rateTimes()[m];
48
49 for (Size r=0; r < multiplier; ++r, ++m)
50 {
51 Real value = df * tau[m]*cs.discountRatio(m+1,m)-1;
52 value /= bigTau;
53 jacobian[l][m]=-value;
54
55 }
56 }
57
58 return jacobian;
59 }
60
62 const std::vector<Spread>& shortDisplacements,
63 const std::vector<Spread>& longDisplacements,
64 Size multiplier,
65 Size offset) {
66 Size n = cs.numberOfRates();
67
68 QL_REQUIRE(offset < multiplier, "offset must be less than period in"
69 " forward forward mappings");
70 Size k = (n-offset)/multiplier;
71
72
73 QL_REQUIRE(shortDisplacements.size() == n , "shortDisplacements must be of size"
74 " equal to number of rates");
75
76 QL_REQUIRE(longDisplacements.size() == k , "longDisplacements must be of size"
77 " equal to (number of rates minus offset) divided by multiplier");
78
79 Matrix jacobian(ForwardForwardJacobian(cs,multiplier,offset));
80
81 for (Size i=0; i < k ; ++i)
82 {
83 Real tau = cs.rateTimes()[(i+1)*multiplier+offset]
84 - cs.rateTimes()[i*multiplier+offset];
85
86 Real longForward = (cs.discountRatio((i+1)*multiplier+offset,i*multiplier+offset)-1.0)
87 /tau;
88 Real longForwardDisplaced = longForward+ longDisplacements[i];
89 for (Size j=0; j < n; ++j)
90 {
91 Real shortForward = cs.forwardRate(j);
92 Real shortForwardDisplaced = shortForward+shortDisplacements[j];
93 jacobian[i][j] *= shortForwardDisplaced/longForwardDisplaced;
94 }
95
96 }
97
98 return jacobian;
99 }
100
102 Size multiplier,
103 Size offset) {
104 Size n = cs.numberOfRates();
105
106 QL_REQUIRE(offset < multiplier, "offset must be less than period in"
107 " forward forward mappings");
108 Size k = (n-offset)/multiplier;
109
110 std::vector<Time> times(k+1);
111 std::vector<DiscountFactor> discRatios(k+1);
112
113
114 for (Size i=0; i < k+1; ++i)
115 {
116 times[i] = cs.rateTimes()[i*multiplier+offset];
117 discRatios[i] = cs.discountRatio(i*multiplier+offset,0);
118 }
119
120 LMMCurveState newState(times);
121 newState.setOnDiscountRatios(discRatios);
122 return newState;
123
124 }
125}
Curve state for market-model simulations
Definition: curvestate.hpp:41
Size numberOfRates() const
Definition: curvestate.hpp:58
virtual Rate forwardRate(Size i) const =0
const std::vector< Time > & rateTimes() const
Definition: curvestate.hpp:60
const std::vector< Time > & rateTaus() const
Definition: curvestate.hpp:61
virtual Real discountRatio(Size i, Size j) const =0
Curve state for Libor market models
void setOnDiscountRatios(const std::vector< DiscountFactor > &discRatios, Size firstValidIndex=0)
Matrix used in linear algebra.
Definition: matrix.hpp:41
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Utility functions for mapping between forward rates of varying tenor.
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Matrix ForwardForwardJacobian(const CurveState &cs, Size multiplier, Size offset)
LMMCurveState RestrictCurveState(const CurveState &cs, Size multiplier, Size offSet)
Matrix YMatrix(const CurveState &cs, const std::vector< Spread > &shortDisplacements, const std::vector< Spread > &longDisplacements, Size Multiplier, Size offset)
Definition: any.hpp:35
ext::shared_ptr< YieldTermStructure > r