File: forwardforwardmappings.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 (134 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download | duplicates (7)
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*

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/forwardforwardmappings.hpp>
#include <ql/models/marketmodels/curvestate.hpp>
#include <ql/models/marketmodels/curvestates/lmmcurvestate.hpp>
#include <vector>

namespace QuantLib {

    Disposable<Matrix>
        ForwardForwardMappings::ForwardForwardJacobian(const CurveState& cs,
        Size multiplier,
        Size offset)
        {

        Size n = cs.numberOfRates();

        QL_REQUIRE(offset < multiplier, "offset  must be less than period in"
            "  forward forward mappings");
        Size k = (n-offset)/multiplier;

        const std::vector<Time>& tau = cs.rateTaus();

        Matrix jacobian = Matrix(k, n, 0.0);

        Size m=offset;
        for (Size l=0; l < k; ++l)
            {
            Real df = cs.discountRatio(m,m+multiplier);
            Real bigTau = cs.rateTimes()[m+multiplier]
            -  cs.rateTimes()[m];

            for (Size r=0; r < multiplier; ++r, ++m)
                {
                Real value = df * tau[m]*cs.discountRatio(m+1,m)-1;
                value /= bigTau;
                jacobian[l][m]=-value;

                }
            }

        return jacobian;
        }

    Disposable<Matrix>
        ForwardForwardMappings::YMatrix(const CurveState& cs,
        const std::vector<Spread>& shortDisplacements,
        const std::vector<Spread>& longDisplacements,
        Size multiplier,
        Size offset
        )
        {
        Size n = cs.numberOfRates();

        QL_REQUIRE(offset < multiplier, "offset  must be less than period in"
            "  forward forward mappings");
        Size k = (n-offset)/multiplier;


        QL_REQUIRE(shortDisplacements.size() == n , "shortDisplacements must be of size"
            " equal to number of rates");

        QL_REQUIRE(longDisplacements.size() == k , "longDisplacements must be of size"
            " equal to (number of rates minus offset) divided by multiplier");

        Matrix jacobian(ForwardForwardJacobian(cs,multiplier,offset));

        for (Size i=0; i < k ; ++i)
            {
            Real tau = cs.rateTimes()[(i+1)*multiplier+offset]
            -  cs.rateTimes()[i*multiplier+offset];

            Real longForward = (cs.discountRatio((i+1)*multiplier+offset,i*multiplier+offset)-1.0)
                /tau;
            Real longForwardDisplaced = longForward+ longDisplacements[i];
            for (Size j=0; j < n; ++j)
                {
                Real shortForward = cs.forwardRate(j);
                Real shortForwardDisplaced = shortForward+shortDisplacements[j];
                jacobian[i][j] *= shortForwardDisplaced/longForwardDisplaced;
                }

            }

        return jacobian;

        }

    LMMCurveState
        ForwardForwardMappings::RestrictCurveState(const CurveState& cs,
                                 Size multiplier,
                                 Size offset
                                )
        {
           Size n = cs.numberOfRates();

           QL_REQUIRE(offset < multiplier, "offset  must be less than period in"
           "  forward forward mappings");
           Size k = (n-offset)/multiplier;

           std::vector<Time> times(k+1);
           std::vector<DiscountFactor> discRatios(k+1);


           for (Size i=0; i < k+1; ++i)
           {
               times[i] = cs.rateTimes()[i*multiplier+offset];
               discRatios[i] = cs.discountRatio(i*multiplier+offset,0);
           }

           LMMCurveState newState(times);
           newState.setOnDiscountRatios(discRatios);
           return newState;

        }
}