QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lmmcurvestate.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) 2006, 2007 Ferdinando Ametrano
5 Copyright (C) 2006 Marco Bianchetti
6 Copyright (C) 2006 Cristina Duminuco
7 Copyright (C) 2006 Giorgio Facchinetti
8 Copyright (C) 2006, 2007 Mark Joshi
9
10 This file is part of QuantLib, a free-software/open-source library
11 for financial quantitative analysts and developers - http://quantlib.org/
12
13 QuantLib is free software: you can redistribute it and/or modify it
14 under the terms of the QuantLib license. You should have received a
15 copy of the license along with this program; if not, please email
16 <quantlib-dev@lists.sf.net>. The license is also available online at
17 <http://quantlib.org/license.shtml>.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 FOR A PARTICULAR PURPOSE. See the license for more details.
22*/
23
25
26namespace QuantLib {
27
28 LMMCurveState::LMMCurveState(const std::vector<Time>& rateTimes)
29 : CurveState(rateTimes),
30 first_(numberOfRates_),
31 discRatios_(numberOfRates_+1, 1.0),
32 forwardRates_(numberOfRates_),
33 cmSwapRates_(numberOfRates_),
34 cmSwapAnnuities_(numberOfRates_,rateTaus_[numberOfRates_-1]),
35 cotSwapRates_(numberOfRates_),
36 cotAnnuities_(numberOfRates_,
37 rateTaus_[numberOfRates_-1]),
38 firstCotAnnuityComped_(numberOfRates_)
39 {}
40
41 void LMMCurveState::setOnForwardRates(const std::vector<Rate>& rates,
42 Size firstValidIndex) {
43 QL_REQUIRE(rates.size()==numberOfRates_,
44 "rates mismatch: " <<
45 numberOfRates_ << " required, " <<
46 rates.size() << " provided");
47 QL_REQUIRE(firstValidIndex<numberOfRates_,
48 "first valid index must be less than " <<
49 numberOfRates_ << ": " <<
50 firstValidIndex << " not allowed");
51
52 // first copy input...
53 first_ = firstValidIndex;
54 std::copy(rates.begin()+first_, rates.end(),
55 forwardRates_.begin()+first_);
56 // ...then calculate discount ratios
57
58 // taken care at constructor time
59 //discRatios_[numberOfRates_] = 1.0;
60 for (Size i=first_; i<numberOfRates_; ++i)
62
63 // lazy evaluation of:
64 // - coterminal swap rates/annuities
65 // - constant maturity swap rates/annuities
66
68 }
69
70 void LMMCurveState::setOnDiscountRatios(const std::vector<DiscountFactor>& discRatios,
71 Size firstValidIndex) {
72 QL_REQUIRE(discRatios.size()==numberOfRates_+1,
73 "too many discount ratios: " <<
74 numberOfRates_+1 << " required, " <<
75 discRatios.size() << " provided");
76 QL_REQUIRE(firstValidIndex<numberOfRates_,
77 "first valid index must be less than " <<
78 numberOfRates_+1 << ": " <<
79 firstValidIndex << " not allowed");
80
81 // first copy input...
82 first_ = firstValidIndex;
83 std::copy(discRatios.begin()+first_, discRatios.end(),
84 discRatios_.begin()+first_);
85 // ...then calculate forwards
86
87 for (Size i=first_; i<numberOfRates_; ++i)
88 forwardRates_[i] = (discRatios_[i]/discRatios_[i+1]-1.0) /
89 rateTaus_[i];
90
91 // lazy evaluation of:
92 // - coterminal swap rates/annuities
93 // - constant maturity swap rates/annuities
94
96 }
97
99 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
100 QL_REQUIRE(std::min(i, j)>=first_, "invalid index");
101 QL_REQUIRE(std::max(i, j)<=numberOfRates_, "invalid index");
102 return discRatios_[i]/discRatios_[j];
103 }
104
106 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
107 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
108 return forwardRates_[i];
109 }
110
112 Size i) const {
113 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
114 QL_REQUIRE(numeraire>=first_ && numeraire<=numberOfRates_,
115 "invalid numeraire");
116 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
117 // coterminalFromDiscountRatios(first_,
118 // discRatios_, rateTaus_,
119 // cotSwapRates_, cotAnnuities_);
120
122 return cotAnnuities_[i]/discRatios_[numeraire];
123
125 {
128 }
129
130 for (int j= static_cast<int>(firstCotAnnuityComped_)-1; j >=static_cast<int>(i); --j)
132
134
135 return cotAnnuities_[i]/discRatios_[numeraire];
136 }
137
139 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
140 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
141 // coterminalFromDiscountRatios(first_,
142 // discRatios_, rateTaus_,
143 // cotSwapRates_, cotAnnuities_);
144 // return cotSwapRates_[i];
145
147 return res;
148 }
149
151 Size i,
152 Size spanningForwards) const {
153 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
154 QL_REQUIRE(numeraire>=first_ && numeraire<=numberOfRates_,
155 "invalid numeraire");
156 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
157
158 // consider lazy evaluation here
162 return cmSwapAnnuities_[i]/discRatios_[numeraire];
163 }
164
166 Size spanningForwards) const {
167 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
168 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
169
170 // consider lazy evaluation here
174 return cmSwapRates_[i];
175 }
176
177 const std::vector<Rate>& LMMCurveState::forwardRates() const {
178 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
179 return forwardRates_;
180 }
181
182 const std::vector<Rate>& LMMCurveState::coterminalSwapRates() const {
183 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
187 return cotSwapRates_;
188 }
189
190 const std::vector<Rate>& LMMCurveState::cmSwapRates(Size spanningForwards) const {
191 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
195 return cmSwapRates_;
196 }
197
198 std::unique_ptr<CurveState> LMMCurveState::clone() const {
199 return std::unique_ptr<CurveState>(new LMMCurveState(*this));
200 }
201
202}
Curve state for market-model simulations
Definition: curvestate.hpp:41
std::vector< Time > rateTaus_
Definition: curvestate.hpp:85
Curve state for Libor market models
const std::vector< Rate > & cmSwapRates(Size spanningForwards) const override
Real discountRatio(Size i, Size j) const override
std::vector< Rate > forwardRates_
void setOnForwardRates(const std::vector< Rate > &fwdRates, Size firstValidIndex=0)
void setOnDiscountRatios(const std::vector< DiscountFactor > &discRatios, Size firstValidIndex=0)
Rate cmSwapAnnuity(Size numeraire, Size i, Size spanningForwards) const override
Rate coterminalSwapRate(Size i) const override
Rate forwardRate(Size i) const override
Rate cmSwapRate(Size i, Size spanningForwards) const override
std::vector< Rate > cmSwapRates_
LMMCurveState(const std::vector< Time > &rateTimes)
std::vector< Rate > cotSwapRates_
Rate coterminalSwapAnnuity(Size numeraire, Size i) const override
std::vector< Real > cmSwapAnnuities_
const std::vector< Rate > & forwardRates() const override
std::vector< DiscountFactor > discRatios_
std::unique_ptr< CurveState > clone() const override
std::vector< Real > cotAnnuities_
const std::vector< Rate > & coterminalSwapRates() const override
#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
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
void coterminalFromDiscountRatios(const Size firstValidIndex, const std::vector< DiscountFactor > &discountFactors, const std::vector< Time > &taus, std::vector< Rate > &cotSwapRates, std::vector< Real > &cotSwapAnnuities)
Definition: curvestate.cpp:58
void constantMaturityFromDiscountRatios(const Size spanningForwards, const Size firstValidIndex, const std::vector< DiscountFactor > &ds, const std::vector< Time > &taus, std::vector< Rate > &constMatSwapRates, std::vector< Real > &constMatSwapAnnuities)
Definition: curvestate.cpp:88