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