QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
curvestate.cpp
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, 2007 Mark Joshi
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/models/marketmodels/curvestate.hpp>
22#include <ql/models/marketmodels/utilities.hpp>
23
24namespace QuantLib {
25
26 CurveState::CurveState(const std::vector<Time>& rateTimes)
27 : numberOfRates_(rateTimes.empty() ? 0 : rateTimes.size()-1),
28 rateTimes_(rateTimes), rateTaus_(numberOfRates_) {
30 }
31
33 Size end) const {
34
35 QL_REQUIRE(end > begin, "empty range specified");
36 QL_REQUIRE(end <= numberOfRates_, "taus/end mismatch");
37
38 Real sum = 0.0;
39 for (Size i=begin; i<end; ++i)
41
43 }
44
45 void forwardsFromDiscountRatios(const Size firstValidIndex,
46 const std::vector<DiscountFactor>& ds,
47 const std::vector<Time>& taus,
48 std::vector<Rate>& fwds) {
49 QL_REQUIRE(taus.size()==fwds.size(),
50 "taus.size()!=fwds.size()");
51 QL_REQUIRE(ds.size()==fwds.size()+1,
52 "ds.size()!=fwds.size()+1");
53
54 for (Size i=firstValidIndex; i<fwds.size(); ++i)
55 fwds[i] = (ds[i]-ds[i+1])/(ds[i+1]*taus[i]);
56 }
57
59 const Size firstValidIndex,
60 const std::vector<DiscountFactor>& discountFactors,
61 const std::vector<Time>& taus,
62 std::vector<Rate>& cotSwapRates,
63 std::vector<Real>& cotSwapAnnuities)
64 {
65 Size nCotSwapRates = cotSwapRates.size();
66 QL_REQUIRE(taus.size()==nCotSwapRates,
67 "taus.size()!=cotSwapRates.size()");
68 QL_REQUIRE(cotSwapAnnuities.size()==nCotSwapRates,
69 "cotSwapAnnuities.size()!=cotSwapRates.size()");
70 QL_REQUIRE(discountFactors.size()==nCotSwapRates+1,
71 "discountFactors.size()!=cotSwapRates.size()+1");
72
73 cotSwapAnnuities[nCotSwapRates-1] =
74 taus[nCotSwapRates-1]*discountFactors[nCotSwapRates];
75 cotSwapRates[nCotSwapRates-1] =
76 (discountFactors[nCotSwapRates-1]-discountFactors[nCotSwapRates])
77 /cotSwapAnnuities[nCotSwapRates-1];
78
79 for (Size i=nCotSwapRates-1; i>firstValidIndex; --i) {
80 cotSwapAnnuities[i-1] = cotSwapAnnuities[i] + taus[i-1] * discountFactors[i];
81 cotSwapRates[i-1] =
82 (discountFactors[i-1]-discountFactors[nCotSwapRates])
83 /cotSwapAnnuities[i-1];
84 }
85 }
86
87
88 void constantMaturityFromDiscountRatios(// Size i, // to be added later
89 const Size spanningForwards,
90 const Size firstValidIndex,
91 const std::vector<DiscountFactor>& ds,
92 const std::vector<Time>& taus,
93 std::vector<Rate>& constMatSwapRates,
94 std::vector<Real>& constMatSwapAnnuities) {
95 Size nConstMatSwapRates = constMatSwapRates.size();
96 QL_REQUIRE(taus.size()==nConstMatSwapRates,
97 "taus.size()!=nConstMatSwapRates");
98 QL_REQUIRE(constMatSwapAnnuities.size()==nConstMatSwapRates,
99 "constMatSwapAnnuities.size()!=nConstMatSwapRates");
100 QL_REQUIRE(ds.size()==nConstMatSwapRates+1,
101 "ds.size()!=nConstMatSwapRates+1");
102 // compute the first cmsrate and cmsannuity
103 constMatSwapAnnuities[firstValidIndex]=0.;
104 Size lastIndex = std::min(firstValidIndex+spanningForwards,nConstMatSwapRates);
105 for (Size i=firstValidIndex; i<lastIndex; ++i) {
106 constMatSwapAnnuities[firstValidIndex]+= taus[i] * ds[i+1];
107 }
108 constMatSwapRates[firstValidIndex] =
109 (ds[firstValidIndex]-ds[lastIndex])/
110 constMatSwapAnnuities[firstValidIndex];
111 Size oldLastIndex = lastIndex;
112
113 // compute all the other cmas rates and cms annuities
114 for (Size i=firstValidIndex+1; i<nConstMatSwapRates; ++i) {
115 Size lastIndex = std::min(i+spanningForwards,nConstMatSwapRates);
116 constMatSwapAnnuities[i] = constMatSwapAnnuities[i-1]
117 - taus[i-1] * ds[i];
118 if (lastIndex!=oldLastIndex)
119 constMatSwapAnnuities[i] += taus[lastIndex-1] * ds[lastIndex];
120 constMatSwapRates[i] = (ds[i]-ds[lastIndex])
121 /constMatSwapAnnuities[i];
122 oldLastIndex = lastIndex;
123 }
124 }
125
126}
Rate swapRate(Size begin, Size end) const
Definition: curvestate.cpp:32
std::vector< Time > rateTimes_
Definition: curvestate.hpp:85
CurveState(const std::vector< Time > &rateTimes)
Definition: curvestate.cpp:26
std::vector< Time > rateTaus_
Definition: curvestate.hpp:85
virtual Real discountRatio(Size i, Size j) const =0
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 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 checkIncreasingTimesAndCalculateTaus(const std::vector< Time > &times, std::vector< Time > &taus)
Definition: utilities.cpp:106
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