QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
coterminalswapcurvestate.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) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Marco Bianchetti
6 Copyright (C) 2007 Cristina Duminuco
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy 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
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
23
24namespace QuantLib {
25
27 const std::vector<Time>& rateTimes)
28 : CurveState(rateTimes),
29 first_(numberOfRates_),
30 discRatios_(numberOfRates_+1, 1.0),
31 forwardRates_(numberOfRates_),
32 cmSwapRates_(numberOfRates_),
33 cmSwapAnnuities_(numberOfRates_, rateTaus_[numberOfRates_-1]),
34 cotSwapRates_(numberOfRates_),
35 cotAnnuities_(numberOfRates_, rateTaus_[numberOfRates_-1]) {}
36
38 const std::vector<Rate>& rates,
39 Size firstValidIndex) {
40 QL_REQUIRE(rates.size()==numberOfRates_,
41 "rates mismatch: " <<
42 numberOfRates_ << " required, " <<
43 rates.size() << " provided");
44 QL_REQUIRE(firstValidIndex<numberOfRates_,
45 "first valid index must be less than " <<
46 numberOfRates_ << ": " <<
47 firstValidIndex << " not allowed");
48 // first copy input...
49 first_ = firstValidIndex;
50 std::copy(rates.begin()+first_, rates.end(),
51 cotSwapRates_.begin()+first_);
52 // ...then calculate discount ratios and coterminal annuities:
53 // reference discount bond = P(n) (the last one)
54 // discRatios_[numberOfRates_] = P(n)/P(n) = 1.0 by construction/definition
56 // j < n
57 for (Size i=numberOfRates_-1; i>first_; --i) {
58 discRatios_[i] = 1.0 + cotSwapRates_[i] * cotAnnuities_[i];
60 }
62
63 // Insert here lazy evaluation of:
64 // - forward rates
65 // - constant maturity swap rates/annuities
66 }
67
69 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
70 QL_REQUIRE(std::min(i, j)>=first_, "invalid index");
71 QL_REQUIRE(std::max(i, j)<=numberOfRates_, "invalid index");
72 return discRatios_[i]/discRatios_[j];
73 }
74
76 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
77 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
79 return forwardRates_[i];
80 }
81
83 Size i) const {
84 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
85 QL_REQUIRE(numeraire>=first_ && numeraire<=numberOfRates_,
86 "invalid numeraire");
87 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
88 return cotAnnuities_[i]/discRatios_[numeraire];
89 }
90
92 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
93 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
94 return cotSwapRates_[i];
95 }
96
98 Size i,
99 Size spanningForwards) const {
100 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
101 QL_REQUIRE(numeraire>=first_ && numeraire<=numberOfRates_,
102 "invalid numeraire");
103 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
104
105 // consider lazy evaluation here
109 return cmSwapAnnuities_[i]/discRatios_[numeraire];
110 }
111
113 Size spanningForwards) const {
114 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
115 QL_REQUIRE(i>=first_ && i<=numberOfRates_, "invalid index");
116
117 // consider lazy evaluation here
121 return cmSwapRates_[i];
122 }
123
124 const std::vector<Rate>& CoterminalSwapCurveState::forwardRates() const {
125 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
127 return forwardRates_;
128 }
129
130 const std::vector<Rate>& CoterminalSwapCurveState::coterminalSwapRates() const {
131 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
132 return cotSwapRates_;
133 }
134
135 const std::vector<Rate>& CoterminalSwapCurveState::cmSwapRates(Size spanningForwards) const {
136 QL_REQUIRE(first_<numberOfRates_, "curve state not initialized yet");
140 return cmSwapRates_;
141 }
142
143 std::unique_ptr<CurveState>
145 return std::unique_ptr<CurveState>(new CoterminalSwapCurveState(*this));
146 }
147
148}
Curve state for coterminal-swap market models
const std::vector< Rate > & cmSwapRates(Size spanningForwards) const override
void setOnCoterminalSwapRates(const std::vector< Rate > &swapRates, Size firstValidIndex=0)
Real discountRatio(Size i, Size j) const override
CoterminalSwapCurveState(const std::vector< Time > &rateTimes)
Rate cmSwapAnnuity(Size numeraire, Size i, Size spanningForwards) const override
Rate coterminalSwapRate(Size i) const override
Rate cmSwapRate(Size i, Size spanningForwards) const override
Rate coterminalSwapAnnuity(Size numeraire, Size i) const override
const std::vector< Rate > & forwardRates() const override
std::vector< DiscountFactor > discRatios_
std::unique_ptr< CurveState > clone() const override
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
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 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