QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
cmswapcurvestate.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2006, 2007 Ferdinando Ametrano
5Copyright (C) 2007 François du Vignaud
6Copyright (C) 2006, 2007 Mark Joshi
7
8This file is part of QuantLib, a free-software/open-source library
9for financial quantitative analysts and developers - http://quantlib.org/
10
11QuantLib is free software: you can redistribute it and/or modify it
12under the terms of the QuantLib license. You should have received a
13copy of the license along with this program; if not, please email
14<quantlib-dev@lists.sf.net>. The license is also available online at
15<http://quantlib.org/license.shtml>.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
23
24namespace QuantLib {
25
26 CMSwapCurveState::CMSwapCurveState(const std::vector<Time>& rateTimes,
27 Size spanningForwards)
28 : CurveState(rateTimes),
29 spanningFwds_(spanningForwards),
30 first_(numberOfRates_),
31 discRatios_(numberOfRates_+1, 1.0),
32 forwardRates_(numberOfRates_),
33 cmSwapRates_(numberOfRates_),
34 cmSwapAnnuities_(numberOfRates_, rateTaus_[numberOfRates_-1]),
35 irrCMSwapRates_(numberOfRates_),
36 irrCMSwapAnnuities_(numberOfRates_, rateTaus_[numberOfRates_-1]),
37 cotSwapRates_(numberOfRates_),
38 cotAnnuities_(numberOfRates_, rateTaus_[numberOfRates_-1]) {}
39
40 void CMSwapCurveState::setOnCMSwapRates(const std::vector<Rate>& rates,
41 Size firstValidIndex) {
42 QL_REQUIRE(rates.size()==numberOfRates_,
43 "rates mismatch: " <<
44 numberOfRates_ << " required, " <<
45 rates.size() << " provided");
46 QL_REQUIRE(firstValidIndex<numberOfRates_,
47 "first valid index must be less than " <<
48 numberOfRates_ << ": " <<
49 firstValidIndex << " not allowed");
50
51 // first copy input...
52 first_ = firstValidIndex;
53 std::copy(rates.begin()+first_, rates.end(),
54 cmSwapRates_.begin()+first_);
55 // ...then calculate discount ratios and annuities
56
57 // taken care at constructor time
58 //discRatios_[numberOfRates_] = 1.0;
59 //cmSwapAnnuities_[numberOfRates_-1] = rateTaus_[numberOfRates_-1];
60
61 // assume i+1 and do i
62 Integer oldAnnuityEndIndex = numberOfRates_;
63 for (Size i=numberOfRates_-1; i>first_; --i)
64 {
65 // formula 6.1 Joshi Liesch
66 Integer endIndex = std::min(i + spanningFwds_,numberOfRates_);
67 Integer annuityEndIndex = std::min(i + spanningFwds_-1,numberOfRates_);
68
69 discRatios_[i] = discRatios_[endIndex] +
72 +discRatios_[i] * rateTaus_[i-1];
73
74 if (annuityEndIndex < oldAnnuityEndIndex)
75 cmSwapAnnuities_[i-1]-=discRatios_[oldAnnuityEndIndex] * rateTaus_[oldAnnuityEndIndex-1 ];
76
77 oldAnnuityEndIndex = annuityEndIndex;
78 }
79 Integer endIndex = std::min(first_ + spanningFwds_,numberOfRates_);
80
81 discRatios_[first_]= discRatios_[endIndex] +
83
84 // lazy evaluation of forward and coterminal swap rates&annuities
85 }
86
88 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
89 QL_REQUIRE(std::min(i, j)>=first_, "invalid index");
90 QL_REQUIRE(std::max(i, j)<=numberOfRates_, "invalid index");
91 return discRatios_[i]/discRatios_[j];
92 }
93
95 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
96 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
98 return forwardRates_[i];
99 }
100
102 Size i) const {
103 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
104 QL_REQUIRE(numeraire>=first_ && numeraire<=numberOfRates_,
105 "invalid numeraire");
106 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
110 return cotAnnuities_[i]/discRatios_[numeraire];
111 }
112
114 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
115 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
119 return cotSwapRates_[i];
120 }
121
123 Size i,
124 Size spanningForwards) const{
125 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
126 QL_REQUIRE(numeraire>=first_ && numeraire<=numberOfRates_,
127 "invalid numeraire");
128 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
129 if (spanningForwards==spanningFwds_)
130 return cmSwapAnnuities_[i]/discRatios_[numeraire];
131 else {
136 return irrCMSwapAnnuities_[i]/discRatios_[numeraire];
137 }
138 }
139
141 Size spanningForwards) const {
142 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
143 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
144 if (spanningForwards==spanningFwds_)
145 return cmSwapRates_[i];
146 else {
151 return irrCMSwapRates_[i];
152 }
153 }
154
155 const std::vector<Rate>& CMSwapCurveState::forwardRates() const {
156 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
158 return forwardRates_;
159 }
160
161 const std::vector<Rate>& CMSwapCurveState::coterminalSwapRates() const {
162 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
166 return cotSwapRates_;
167 }
168
169 const std::vector<Rate>& CMSwapCurveState::cmSwapRates(Size spanningForwards) const {
170 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
171 if (spanningForwards==spanningFwds_)
172 return cmSwapRates_;
173 else {
177 return irrCMSwapRates_;
178 }
179 }
180
181 std::unique_ptr<CurveState> CMSwapCurveState::clone() const {
182 return std::unique_ptr<CurveState>(new CMSwapCurveState(*this));
183 }
184
185}
Curve state for constant-maturity-swap market models
const std::vector< Rate > & cmSwapRates(Size spanningForwards) const override
Real discountRatio(Size i, Size j) const override
std::vector< Rate > forwardRates_
Rate cmSwapAnnuity(Size numeraire, Size i, Size spanningForwards) const override
std::vector< Real > irrCMSwapAnnuities_
Rate coterminalSwapRate(Size i) const override
Rate forwardRate(Size i) const override
Rate cmSwapRate(Size i, Size spanningForwards) const override
std::vector< Rate > irrCMSwapRates_
std::vector< Rate > cmSwapRates_
CMSwapCurveState(const std::vector< Time > &rateTimes, Size spanningForwards)
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_
void setOnCMSwapRates(const std::vector< Rate > &cmSwapRates, Size firstValidIndex=0)
std::unique_ptr< CurveState > clone() const override
std::vector< Real > cotAnnuities_
const std::vector< Rate > & coterminalSwapRates() const override
Curve state for market-model simulations
Definition: curvestate.hpp:41
std::vector< Time > rateTaus_
Definition: curvestate.hpp:85
#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
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 forwardsFromDiscountRatios(const Size firstValidIndex, const std::vector< DiscountFactor > &ds, const std::vector< Time > &taus, std::vector< Rate > &fwds)
Definition: curvestate.cpp:45
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