QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
evolutiondescription.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
26#include <ql/math/matrix.hpp>
28
29namespace QuantLib {
30
32 const std::vector<Time>& rateTimes,
33 const std::vector<Time>& evolutionTimes,
34 const std::vector<std::pair<Size,Size> >& relevanceRates)
35 : numberOfRates_(rateTimes.empty() ? 0 : rateTimes.size()-1),
36 rateTimes_(rateTimes),
37 evolutionTimes_(evolutionTimes.empty() && !rateTimes.empty() ?
38 std::vector<Time>(rateTimes.begin(), rateTimes.end()-1) :
39 evolutionTimes),
40 relevanceRates_(relevanceRates),
41 rateTaus_(numberOfRates_),
42 //effStopTime_(evolutionTimes_.size(), rateTimes_.size()-1),
43 firstAliveRate_(evolutionTimes_.size())
44 {
45
47
50
52 "The last evolution time (" << evolutionTimes_.back() <<
53 ") is past the last fixing time (" <<
54 rateTimes[numberOfRates_-2] << ")");
55
56 if (relevanceRates.empty())
57 relevanceRates_ = std::vector<std::pair<Size,Size> >(
58 numberOfSteps, std::make_pair(0,numberOfRates_));
59 else
61 "relevanceRates / evolutionTimes mismatch");
62
63 //for (Size j=0; j<numberOfSteps; ++j) {
64 // for (Size i=0; i<numberOfRates_; ++i)
65 // effStopTime_[j][i] =
66 // std::min(evolutionTimes_[j], rateTimes_[i]);
67 //}
68
69 Time currentEvolutionTime = 0.0;
71 for (Size j=0; j<numberOfSteps; ++j) {
72 while (rateTimes_[firstAliveRate] <= currentEvolutionTime)
75 currentEvolutionTime = evolutionTimes_[j];
76 }
77 }
78
79 const std::vector<Time>& EvolutionDescription::rateTimes() const {
80 return rateTimes_;
81 }
82
83 const std::vector<Time>& EvolutionDescription::rateTaus() const {
84 return rateTaus_;
85 }
86
87 const std::vector<Time>& EvolutionDescription::evolutionTimes() const {
88 return evolutionTimes_;
89 }
90
91 //const Matrix& EvolutionDescription::effectiveStopTimes() const {
92 // return effStopTime_;
93 //}
94
95 const std::vector<Size>& EvolutionDescription::firstAliveRate() const {
96 return firstAliveRate_;
97 }
98
99 const std::vector<std::pair<Size,Size> >& EvolutionDescription::relevanceRates() const {
100 return relevanceRates_;
101 }
102
104 return numberOfRates_;
105 }
106
108 return evolutionTimes_.size();
109 }
110
112 const std::vector<Size>& numeraires)
113 {
114 const std::vector<Time>& evolutionTimes = evolution.evolutionTimes();
115 Size n = evolutionTimes.size();
116 QL_REQUIRE(numeraires.size() == n,
117 "Size mismatch between numeraires (" << numeraires.size()
118 << ") and evolution times (" << n << ")");
119
120 const std::vector<Time>& rateTimes = evolution.rateTimes();
121 for (Size i=0; i<n-1; i++)
122 QL_REQUIRE(rateTimes[numeraires[i]] >= evolutionTimes[i],
123 io::ordinal(i+1) << " step, evolution time " <<
124 evolutionTimes[i] << ": the numeraire (" << numeraires[i] <<
125 "), corresponding to rate time " <<
126 rateTimes[numeraires[i]] << ", is expired");
127 }
128
130 const std::vector<Size>& numeraires) {
131 const std::vector<Time>& rateTimes = evolution.rateTimes();
132 return *std::min_element(numeraires.begin(), numeraires.end()) ==
133 rateTimes.size()-1;
134 }
135
137 const std::vector<Size>& numeraires,
138 Size offset) {
139 bool res = true;
140 const std::vector<Time>& rateTimes = evolution.rateTimes();
141 Size maxNumeraire=rateTimes.size()-1;
142 QL_REQUIRE(offset<=maxNumeraire,
143 "offset (" << offset <<
144 ") is greater than the max allowed value for numeraire ("
145 << maxNumeraire << ")");
146 const std::vector<Time>& evolutionTimes = evolution.evolutionTimes();
147 for (Size i=0, j=0; i<evolutionTimes.size(); ++i) {
148 while (rateTimes[j] < evolutionTimes[i])
149 j++;
150 res = (numeraires[i] == std::min(j+offset, maxNumeraire)) && res;
151 }
152 return res;
153 }
154
156 const std::vector<Size>& numeraires) {
157 return isInMoneyMarketPlusMeasure(evolution, numeraires, 0);
158 }
159
160 std::vector<Size> terminalMeasure(const EvolutionDescription& evolution)
161 {
162 return std::vector<Size>(evolution.evolutionTimes().size(),
163 evolution.rateTimes().size()-1);
164 }
165
166 std::vector<Size> moneyMarketPlusMeasure(const EvolutionDescription& ev,
167 Size offset) {
168 const std::vector<Time>& rateTimes = ev.rateTimes();
169 Size maxNumeraire = rateTimes.size()-1;
170 QL_REQUIRE(offset<=maxNumeraire,
171 "offset (" << offset <<
172 ") is greater than the max allowed value for numeraire ("
173 << maxNumeraire << ")");
174
175 const std::vector<Time>& evolutionTimes = ev.evolutionTimes();
176 Size n = evolutionTimes.size();
177 std::vector<Size> numeraires(n);
178 for (Size i=0, j=0; i<n; ++i) {
179 while (rateTimes[j] < evolutionTimes[i])
180 j++;
181 numeraires[i] = std::min(j+offset, maxNumeraire);
182 }
183 return numeraires;
184 }
185
186 std::vector<Size> moneyMarketMeasure(const EvolutionDescription& evol) {
187 return moneyMarketPlusMeasure(evol, 0);
188 }
189
190}
Market-model evolution description.
std::vector< std::pair< Size, Size > > relevanceRates_
const std::vector< std::pair< Size, Size > > & relevanceRates() const
const std::vector< Time > & rateTimes() const
const std::vector< Time > & rateTaus() const
const std::vector< Time > & evolutionTimes() const
const std::vector< Size > & firstAliveRate() const
output manipulators
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
matrix used in linear algebra.
Definition: any.hpp:35
bool isInMoneyMarketMeasure(const EvolutionDescription &evolution, const std::vector< Size > &numeraires)
std::vector< Size > terminalMeasure(const EvolutionDescription &evolution)
Terminal measure: the last bond is used as numeraire.
void checkCompatibility(const EvolutionDescription &evolution, const std::vector< Size > &numeraires)
std::vector< Size > moneyMarketPlusMeasure(const EvolutionDescription &ev, Size offset)
void checkIncreasingTimesAndCalculateTaus(const std::vector< Time > &times, std::vector< Time > &taus)
Definition: utilities.cpp:106
bool isInTerminalMeasure(const EvolutionDescription &evolution, const std::vector< Size > &numeraires)
void checkIncreasingTimes(const std::vector< Time > &times)
check for strictly increasing times, first time greater than zero
Definition: utilities.cpp:92
std::vector< Size > moneyMarketMeasure(const EvolutionDescription &evol)
bool isInMoneyMarketPlusMeasure(const EvolutionDescription &evolution, const std::vector< Size > &numeraires, Size offset)
STL namespace.