QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
historicalratesanalysis.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Ferdinando Ametrano
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/indexes/interestrateindex.hpp>
21#include <ql/models/marketmodels/historicalratesanalysis.hpp>
22#include <ql/time/calendar.hpp>
23#include <utility>
24
25namespace QuantLib {
26
28 SequenceStatistics& statistics,
29 std::vector<Date>& skippedDates,
30 std::vector<std::string>& skippedDatesErrorMessage,
31 const Date& startDate,
32 const Date& endDate,
33 const Period& step,
34 const std::vector<ext::shared_ptr<InterestRateIndex> >& indexes) {
35
36 skippedDates.clear();
37 skippedDatesErrorMessage.clear();
38
39 Size nRates = indexes.size();
40 statistics.reset(nRates);
41
42 std::vector<Rate> sample(nRates);
43 std::vector<Rate> prevSample(nRates);
44 std::vector<Rate> sampleDiff(nRates);
45
46 Calendar cal = indexes[0]->fixingCalendar();
47 // start with a valid business date
48 Date currentDate = cal.advance(startDate, 1*Days, Following);
49 bool isFirst = true;
50 // Loop over the historical dataset
51 for (; currentDate<=endDate;
52 currentDate = cal.advance(currentDate, step, Following)) {
53
54 try {
55 for (Size i=0; i<nRates; ++i) {
56 Rate fixing = indexes[i]->fixing(currentDate, false);
57 sample[i] = fixing;
58 }
59 } catch (std::exception& e) {
60 skippedDates.push_back(currentDate);
61 skippedDatesErrorMessage.emplace_back(e.what());
62 continue;
63 }
64
65 // From 2nd step onwards, calculate forward rate
66 // relative differences
67 if (!isFirst){
68 for (Size i=0; i<nRates; ++i)
69 sampleDiff[i] = sample[i]/prevSample[i] -1.0;
70 // add observation
71 statistics.add(sampleDiff.begin(), sampleDiff.end());
72 }
73 else
74 isFirst = false;
75
76 // Store last calculated forward rates
77 std::swap(prevSample, sample);
78
79 }
80 }
81
83 ext::shared_ptr<SequenceStatistics> stats,
84 const Date& startDate,
85 const Date& endDate,
86 const Period& step,
87 const std::vector<ext::shared_ptr<InterestRateIndex> >& indexes)
88 : stats_(std::move(stats)) {
90 *stats_,
92 startDate, endDate, step,
93 indexes);
94 }
95}
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
Statistics analysis of N-dimensional (sequence) data.
void add(const Sequence &sample, Real weight=1.0)
ext::shared_ptr< SequenceStatistics > stats_
std::vector< std::string > skippedDatesErrorMessage_
HistoricalRatesAnalysis(ext::shared_ptr< SequenceStatistics > stats, const Date &startDate, const Date &endDate, const Period &step, const std::vector< ext::shared_ptr< InterestRateIndex > > &indexes)
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 historicalRatesAnalysis(SequenceStatistics &statistics, std::vector< Date > &skippedDates, std::vector< std::string > &skippedDatesErrorMessage, const Date &startDate, const Date &endDate, const Period &step, const std::vector< ext::shared_ptr< InterestRateIndex > > &indexes)
STL namespace.