/src/quantlib/ql/models/marketmodels/pathwisediscounter.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2008 Mark Joshi |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | #include <ql/models/marketmodels/pathwisediscounter.hpp> |
20 | | #include <ql/models/marketmodels/utilities.hpp> |
21 | | #include <algorithm> |
22 | | |
23 | | namespace QuantLib |
24 | | { |
25 | | |
26 | | MarketModelPathwiseDiscounter::MarketModelPathwiseDiscounter(Time paymentTime, |
27 | | const std::vector<Time>& rateTimes) |
28 | 0 | { |
29 | 0 | checkIncreasingTimes(rateTimes); |
30 | |
|
31 | 0 | numberRates_ = rateTimes.size()-1; |
32 | 0 | before_ = std::lower_bound(rateTimes.begin(), rateTimes.end(), |
33 | 0 | paymentTime) - rateTimes.begin(); |
34 | | |
35 | | // handle the case where the payment is in the last |
36 | | // period or after the last period |
37 | 0 | if (before_ > rateTimes.size()-2) |
38 | 0 | before_ = rateTimes.size()-2; |
39 | |
|
40 | 0 | beforeWeight_=1.0-(paymentTime-rateTimes[before_])/ |
41 | 0 | (rateTimes[before_+1]-rateTimes[before_]); |
42 | |
|
43 | 0 | postWeight_ = 1.0 - beforeWeight_; |
44 | 0 | taus_.resize(numberRates_); |
45 | |
|
46 | 0 | for (Size i=0; i < numberRates_; ++i) |
47 | 0 | taus_[i] = rateTimes[i+1] - rateTimes[i]; |
48 | |
|
49 | 0 | } |
50 | | |
51 | | void MarketModelPathwiseDiscounter::getFactors( |
52 | | const Matrix& , // LIBORRates, for all steps |
53 | | const Matrix& Discounts, // P(t_0, t_j) for j=0,...n for each step |
54 | | Size currentStep, |
55 | | std::vector<Real>& factors) const |
56 | 0 | { |
57 | 0 | Real preDF = Discounts[currentStep][before_]; |
58 | 0 | Real postDF = Discounts[currentStep][before_+1]; |
59 | |
|
60 | 0 | for (Size i=before_+1; i<numberRates_; ++i) |
61 | 0 | factors[i+1] =0.0; |
62 | |
|
63 | 0 | if (postWeight_==0.0) |
64 | 0 | { |
65 | 0 | factors[0] = preDF; |
66 | |
|
67 | 0 | for (Size i=0; i<before_; ++i) |
68 | 0 | factors[i+1] = -preDF*taus_[i]*Discounts[currentStep][i+1]/Discounts[currentStep][i]; |
69 | |
|
70 | 0 | factors[before_+1]=0.0; |
71 | |
|
72 | 0 | return; |
73 | 0 | } |
74 | | |
75 | 0 | Real df = preDF * std::pow(postDF/preDF, postWeight_); |
76 | |
|
77 | 0 | factors[0] = df; |
78 | |
|
79 | 0 | for (Size i=0; i<=before_; ++i) |
80 | 0 | factors[i+1] = -df*taus_[i]*Discounts[currentStep][i+1]/Discounts[currentStep][i]; |
81 | |
|
82 | 0 | factors[before_+1] *= postWeight_; |
83 | 0 | } |
84 | | } |