QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
historicalforwardratesanalysis.hpp
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 François du Vignaud
6 Copyright (C) 2007 Marco Bianchetti
7 Copyright (C) 2007 Katiuscia Manzoni
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23/*! \file historicalforwardratesanalysis.hpp
24 \brief Statistical analysis of historical forward rates
25*/
26
27#ifndef quantlib_historical_forward_rates_analysis_hpp
28#define quantlib_historical_forward_rates_analysis_hpp
29
32#include <ql/math/matrix.hpp>
37#include <ql/time/calendar.hpp>
38#include <ql/time/date.hpp>
39#include <utility>
40
41namespace QuantLib {
42
43 template<class Traits, class Interpolator>
45 SequenceStatistics& statistics,
46 std::vector<Date>& skippedDates,
47 std::vector<std::string>& skippedDatesErrorMessage,
48 std::vector<Date>& failedDates,
49 std::vector<std::string>& failedDatesErrorMessage,
50 std::vector<Period>& fixingPeriods,
51 const Date& startDate,
52 const Date& endDate,
53 const Period& step,
54 const ext::shared_ptr<InterestRateIndex>& fwdIndex,
55 const Period& initialGap,
56 const Period& horizon,
57 const std::vector<ext::shared_ptr<IborIndex> >& iborIndexes,
58 const std::vector<ext::shared_ptr<SwapIndex> >& swapIndexes,
59 const DayCounter& yieldCurveDayCounter,
60 Real yieldCurveAccuracy = 1.0e-12,
61 const Interpolator& i = Interpolator()) {
62
63
64 statistics.reset();
65 skippedDates.clear();
66 skippedDatesErrorMessage.clear();
67 failedDates.clear();
68 failedDatesErrorMessage.clear();
69 fixingPeriods.clear();
70
71 SavedSettings backup;
73
74 std::vector<ext::shared_ptr<RateHelper> > rateHelpers;
75
76 // Create DepositRateHelper
77 std::vector<ext::shared_ptr<SimpleQuote> > iborQuotes;
78 std::vector<ext::shared_ptr<IborIndex> >::const_iterator ibor;
79 for (ibor=iborIndexes.begin(); ibor!=iborIndexes.end(); ++ibor) {
80 ext::shared_ptr<SimpleQuote> quote(new SimpleQuote);
81 iborQuotes.push_back(quote);
82 Handle<Quote> quoteHandle(quote);
83 rateHelpers.push_back(ext::shared_ptr<RateHelper> (new
84 DepositRateHelper(quoteHandle,
85 (*ibor)->tenor(),
86 (*ibor)->fixingDays(),
87 (*ibor)->fixingCalendar(),
88 (*ibor)->businessDayConvention(),
89 (*ibor)->endOfMonth(),
90 (*ibor)->dayCounter())));
91 }
92
93 // Create SwapRateHelper
94 std::vector<ext::shared_ptr<SimpleQuote> > swapQuotes;
95 std::vector<ext::shared_ptr<SwapIndex> >::const_iterator swap;
96 for (swap=swapIndexes.begin(); swap!=swapIndexes.end(); ++swap) {
97 ext::shared_ptr<SimpleQuote> quote(new SimpleQuote);
98 swapQuotes.push_back(quote);
99 Handle<Quote> quoteHandle(quote);
100 rateHelpers.push_back(ext::shared_ptr<RateHelper> (new
101 SwapRateHelper(quoteHandle,
102 (*swap)->tenor(),
103 (*swap)->fixingCalendar(),
104 (*swap)->fixedLegTenor().frequency(),
105 (*swap)->fixedLegConvention(),
106 (*swap)->dayCounter(),
107 (*swap)->iborIndex())));
108 }
109
110 // Set up the forward rates time grid
111 Period indexTenor = fwdIndex->tenor();
112 Period fixingPeriod = initialGap;
113 while (fixingPeriod<=horizon) {
114 fixingPeriods.push_back(fixingPeriod);
115 fixingPeriod += indexTenor;
116 }
117
118 Size nRates = fixingPeriods.size();
119 statistics.reset(nRates);
120 std::vector<Rate> fwdRates(nRates);
121 std::vector<Rate> prevFwdRates(nRates);
122 std::vector<Rate> fwdRatesDiff(nRates);
123 DayCounter indexDayCounter = fwdIndex->dayCounter();
124 Calendar cal = fwdIndex->fixingCalendar();
125
126 // Bootstrap the yield curve at the currentDate
127 Natural settlementDays = 0;
128 typename PiecewiseYieldCurve<Traits, Interpolator>::bootstrap_type bootstrap(yieldCurveAccuracy);
130 cal,
131 rateHelpers,
132 yieldCurveDayCounter,
133 std::vector<Handle<Quote> >(),
134 std::vector<Date>(),
135 i,
136 bootstrap);
137
138 // start with a valid business date
139 Date currentDate = cal.advance(startDate, 1*Days, Following);
140 bool isFirst = true;
141 // Loop over the historical dataset
142 for (; currentDate<=endDate;
143 currentDate = cal.advance(currentDate, step, Following)) {
144
145 // move the evaluationDate to currentDate
146 // and update ratehelpers dates...
147 Settings::instance().evaluationDate() = currentDate;
148
149 try {
150 // update the quotes...
151 for (Size i=0; i<iborIndexes.size(); ++i) {
152 Rate fixing = iborIndexes[i]->fixing(currentDate, false);
153 iborQuotes[i]->setValue(fixing);
154 }
155 for (Size i=0; i<swapIndexes.size(); ++i) {
156 Rate fixing = swapIndexes[i]->fixing(currentDate, false);
157 swapQuotes[i]->setValue(fixing);
158 }
159 } catch (std::exception& e) {
160 skippedDates.push_back(currentDate);
161 skippedDatesErrorMessage.emplace_back(e.what());
162 continue;
163 }
164
165 try {
166 for (Size i=0; i<nRates; ++i) {
167 // Time-to-go forwards
168 Date d = currentDate + fixingPeriods[i];
169 fwdRates[i] = yc.forwardRate(d,
170 indexTenor,
171 indexDayCounter,
172 Simple);
173 }
174 } catch (std::exception& e) {
175 failedDates.push_back(currentDate);
176 failedDatesErrorMessage.emplace_back(e.what());
177 continue;
178 }
179
180 // From 2nd step onwards, calculate forward rate
181 // relative differences
182 if (!isFirst){
183 for (Size i=0; i<nRates; ++i)
184 fwdRatesDiff[i] = fwdRates[i]/prevFwdRates[i] -1.0;
185 // add observation
186 statistics.add(fwdRatesDiff.begin(), fwdRatesDiff.end());
187 }
188 else
189 isFirst = false;
190
191 // Store last calculated forward rates
192 std::swap(prevFwdRates, fwdRates);
193
194 }
195 }
196
198 public:
200 virtual const std::vector<Date>& skippedDates() const = 0;
201 virtual const std::vector<std::string>& skippedDatesErrorMessage() const = 0;
202 virtual const std::vector<Date>& failedDates() const = 0;
203 virtual const std::vector<std::string>& failedDatesErrorMessage() const = 0;
204 virtual const std::vector<Period>& fixingPeriods() const = 0;
205 };
206
207 //! %Historical correlation class
208 template<class Traits, class Interpolator>
210 public:
212 ext::shared_ptr<SequenceStatistics> stats,
213 const Date& startDate,
214 const Date& endDate,
215 const Period& step,
216 const ext::shared_ptr<InterestRateIndex>& fwdIndex,
217 const Period& initialGap,
218 const Period& horizon,
219 const std::vector<ext::shared_ptr<IborIndex> >& iborIndexes,
220 const std::vector<ext::shared_ptr<SwapIndex> >& swapIndexes,
221 const DayCounter& yieldCurveDayCounter,
222 Real yieldCurveAccuracy);
224 ;
225 const std::vector<Date>& skippedDates() const override;
226 const std::vector<std::string>& skippedDatesErrorMessage() const override;
227 const std::vector<Date>& failedDates() const override;
228 const std::vector<std::string>& failedDatesErrorMessage() const override;
229 const std::vector<Period>& fixingPeriods() const override;
230 //const ext::shared_ptr<SequenceStatistics>& stats() const;
231 private:
232 // calculated data
233 ext::shared_ptr<SequenceStatistics> stats_;
234 std::vector<Date> skippedDates_;
235 std::vector<std::string> skippedDatesErrorMessage_;
236 std::vector<Date> failedDates_;
237 std::vector<std::string> failedDatesErrorMessage_;
238 std::vector<Period> fixingPeriods_;
239 };
240
241 // inline
242 template<class Traits, class Interpolator>
243 const std::vector<Period>&
245 return fixingPeriods_;
246 }
247
248 template<class Traits, class Interpolator>
249 inline const std::vector<Date>&
251 return skippedDates_;
252 }
253
254 template<class Traits, class Interpolator>
255 inline const std::vector<std::string>&
257 return skippedDatesErrorMessage_;
258 }
259
260 template<class Traits, class Interpolator>
261 inline const std::vector<Date>&
263 return failedDates_;
264 }
265
266 template<class Traits, class Interpolator>
267 inline const std::vector<std::string>&
269 return failedDatesErrorMessage_;
270 }
271
272 //inline const ext::shared_ptr<SequenceStatistics>&
273 //HistoricalForwardRatesAnalysis::stats() const {
274 // return stats_;
275 //}
276 template <class Traits, class Interpolator>
278 ext::shared_ptr<SequenceStatistics> stats,
279 const Date& startDate,
280 const Date& endDate,
281 const Period& step,
282 const ext::shared_ptr<InterestRateIndex>& fwdIndex,
283 const Period& initialGap,
284 const Period& horizon,
285 const std::vector<ext::shared_ptr<IborIndex> >& iborIndexes,
286 const std::vector<ext::shared_ptr<SwapIndex> >& swapIndexes,
287 const DayCounter& yieldCurveDayCounter,
288 Real yieldCurveAccuracy)
289 : stats_(std::move(stats)) {
291 Interpolator>(
292 *stats_,
295 fixingPeriods_, startDate, endDate, step,
296 fwdIndex, initialGap, horizon,
297 iborIndexes, swapIndexes,
298 yieldCurveDayCounter, yieldCurveAccuracy);
299 }
300}
301
302#endif
calendar class
calendar class
Definition: calendar.hpp:61
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Rate helper for bootstrapping over deposit rates.
Statistics analysis of N-dimensional (sequence) data.
void add(const Sequence &sample, Real weight=1.0)
Shared handle to an observable.
Definition: handle.hpp:41
virtual const std::vector< std::string > & failedDatesErrorMessage() const =0
virtual const std::vector< Period > & fixingPeriods() const =0
virtual const std::vector< std::string > & skippedDatesErrorMessage() const =0
virtual ~HistoricalForwardRatesAnalysis()=default
virtual const std::vector< Date > & skippedDates() const =0
virtual const std::vector< Date > & failedDates() const =0
const std::vector< Period > & fixingPeriods() const override
const std::vector< Date > & skippedDates() const override
const std::vector< Date > & failedDates() const override
const std::vector< std::string > & failedDatesErrorMessage() const override
const std::vector< std::string > & skippedDatesErrorMessage() const override
Piecewise yield term structure.
Bootstrap< this_curve > bootstrap_type
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
bool & enforcesTodaysHistoricFixings()
Definition: settings.hpp:171
market element returning a stored value
Definition: simplequote.hpp:33
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Rate helper for bootstrapping over swap rates.
date- and time-related classes, typedefs and enumerations
Date d
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
base class for Inter-Bank-Offered-Rate indexes
matrix used in linear algebra.
Definition: any.hpp:35
void historicalForwardRatesAnalysis(SequenceStatistics &statistics, std::vector< Date > &skippedDates, std::vector< std::string > &skippedDatesErrorMessage, std::vector< Date > &failedDates, std::vector< std::string > &failedDatesErrorMessage, std::vector< Period > &fixingPeriods, const Date &startDate, const Date &endDate, const Period &step, const ext::shared_ptr< InterestRateIndex > &fwdIndex, const Period &initialGap, const Period &horizon, const std::vector< ext::shared_ptr< IborIndex > > &iborIndexes, const std::vector< ext::shared_ptr< SwapIndex > > &swapIndexes, const DayCounter &yieldCurveDayCounter, Real yieldCurveAccuracy=1.0e-12, const Interpolator &i=Interpolator())
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.
piecewise-interpolated term structure
deposit, FRA, futures, and various swap rate helpers
Statistics tools for sequence (vector, list, array) samples.
simple quote class
swap-rate indexes