QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
timeseries.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) 2006 Joseph Wang
5 Copyright (C) 2010 Liquidnet Holdings, Inc.
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/*! \file timeseries.hpp
22 \brief Container for historical data
23*/
24
25#ifndef quantlib_timeseries_hpp
26#define quantlib_timeseries_hpp
27
28#include <ql/time/date.hpp>
29#include <ql/utilities/null.hpp>
30#include <ql/errors.hpp>
31#include <ql/functional.hpp>
32#include <boost/iterator/transform_iterator.hpp>
33#include <iterator>
34#include <algorithm>
35#include <map>
36#include <vector>
37#include <type_traits>
38
39namespace QuantLib {
40
41 //! Container for historical data
42 /*! This class acts as a generic repository for a set of
43 historical data. Any single datum can be accessed through its
44 date, while sets of consecutive data can be accessed through
45 iterators.
46
47 \pre The <c>Container</c> type must satisfy the requirements
48 set by the C++ standard for associative containers.
49 */
50 template <class T, class Container = std::map<Date, T> >
51 class TimeSeries {
52 public:
53 typedef Date key_type;
54 typedef T value_type;
55 private:
56 mutable Container values_;
57 public:
58 /*! Default constructor */
59 TimeSeries() = default;
60 /*! This constructor initializes the history with a set of
61 values passed as two sequences, the first containing dates
62 and the second containing corresponding values.
63 */
64 template <class DateIterator, class ValueIterator>
65 TimeSeries(DateIterator dBegin, DateIterator dEnd,
66 ValueIterator vBegin) {
67 while (dBegin != dEnd)
68 values_[*(dBegin++)] = *(vBegin++);
69 }
70 /*! This constructor initializes the history with a set of
71 values. Such values are assigned to a corresponding number
72 of consecutive dates starting from <b><i>firstDate</i></b>
73 included.
74 */
75 template <class ValueIterator>
77 ValueIterator begin, ValueIterator end) {
79 while (begin != end)
80 values_[d++] = *(begin++);
81 }
82 //! \name Inspectors
83 //@{
84 //! returns the first date for which a historical datum exists
85 Date firstDate() const;
86 //! returns the last date for which a historical datum exists
87 Date lastDate() const;
88 //! returns the number of historical data including null ones
89 Size size() const;
90 //! returns whether the series contains any data
91 bool empty() const;
92 //@}
93 //! \name Historical data access
94 //@{
95 //! returns the (possibly null) datum corresponding to the given date
96 T operator[](const Date& d) const {
97 auto found = values_.find(d);
98 if (found == values_.cend())
99 return Null<T>();
100 return found->second;
101 }
102 T& operator[](const Date& d) {
103 auto found = values_.insert(std::pair<Date, T>(d, Null<T>())).first;
104 return found->second;
105 }
106 //@}
107
108 //! \name Iterators
109 //@{
110 typedef typename Container::const_iterator const_iterator;
111 typedef typename const_iterator::iterator_category iterator_category;
112
113 // Reverse iterators
114 // The following class makes compilation fail for the code
115 // that calls rbegin or rend with a container that does not
116 // support reverse iterators. All the rest TimeSeries class
117 // features should compile and work for this type of
118 // containers.
119 template <class container, class iterator_category>
120 struct reverse {
121 typedef std::reverse_iterator<typename container::const_iterator>
123 reverse(const container& c) : c_(c) {}
125 return const_reverse_iterator(c_.end());
126 }
128 return const_reverse_iterator(c_.begin());
129 }
130 const container& c_;
131 };
132
133 // This class defines reverse iterator features via
134 // container's native calls.
135 template <class container>
136 struct reverse<container, std::bidirectional_iterator_tag> {
137 typedef typename container::const_reverse_iterator
139 reverse(const container& c) : c_(c) {}
140 const_reverse_iterator rbegin() const { return c_.rbegin(); }
141 const_reverse_iterator rend() const { return c_.rend(); }
142 const container& c_;
143 };
144
145 // The following typedef enables reverse iterators for
146 // bidirectional_iterator_tag category.
147 typedef std::conditional_t<
148 std::is_same_v<iterator_category, std::bidirectional_iterator_tag> ||
149 std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>,
150 std::bidirectional_iterator_tag, std::input_iterator_tag> enable_reverse;
151
152 typedef typename
155
158 const_iterator begin() const { return cbegin(); }
159 const_iterator end() const { return cend(); }
162 }
165 }
167 const_reverse_iterator rend() const { return crend(); }
168 //@}
169
170 private:
171 typedef typename Container::value_type container_value_type;
172 typedef std::function<Date(const container_value_type&)>
174 typedef std::function<T(const container_value_type&)>
176
177 public:
178 //! \name Utilities
179 //@{
181 //! returns the dates for which historical data exist
182 std::vector<Date> dates() const;
183 //! returns the historical data
184 std::vector<T> values() const;
185 //@}
186
187 private:
188 static const Date& get_time (const container_value_type& v) {
189 return v.first;
190 }
191 static const T& get_value (const container_value_type& v) {
192 return v.second;
193 }
194 };
195
196
197 // inline definitions
198
199 template <class T, class C>
201 QL_REQUIRE(!values_.empty(), "empty timeseries");
202 return values_.begin()->first;
203 }
204
205 template <class T, class C>
207 QL_REQUIRE(!values_.empty(), "empty timeseries");
208 return rbegin()->first;
209 }
210
211 template <class T, class C>
213 return values_.size();
214 }
215
216 template <class T, class C>
217 inline bool TimeSeries<T,C>::empty() const {
218 return values_.empty();
219 }
220
221 template <class T, class C>
222 inline typename TimeSeries<T,C>::const_iterator
224 return values_.begin();
225 }
226
227 template <class T, class C>
228 inline typename TimeSeries<T,C>::const_iterator
230 return values_.end();
231 }
232
233 template <class T, class C>
234 inline typename TimeSeries<T,C>::const_iterator
236 auto i = values_.find(d);
237 if (i == values_.end()) {
238 values_[d] = Null<T>();
239 i = values_.find(d);
240 }
241 return i;
242 }
243
244 template <class T, class C>
245 std::vector<Date> TimeSeries<T,C>::dates() const {
246 std::vector<Date> v;
247 v.reserve(size());
248 std::transform(cbegin(), cend(), std::back_inserter(v),
250 return v;
251 }
252
253 template <class T, class C>
254 std::vector<T> TimeSeries<T,C>::values() const {
255 std::vector<T> v;
256 v.reserve(size());
257 std::transform(cbegin(), cend(), std::back_inserter(v),
259 return v;
260 }
261
262}
263
264#endif
Concrete date class.
Definition: date.hpp:125
template class providing a null value for a given type.
Definition: null.hpp:59
Container for historical data.
Definition: timeseries.hpp:51
static const Date & get_time(const container_value_type &v)
Definition: timeseries.hpp:188
const_reverse_iterator rend() const
Definition: timeseries.hpp:167
Container::const_iterator const_iterator
Definition: timeseries.hpp:110
reverse< Container, enable_reverse >::const_reverse_iterator const_reverse_iterator
Definition: timeseries.hpp:154
std::conditional_t< std::is_same_v< iterator_category, std::bidirectional_iterator_tag >||std::is_base_of_v< std::bidirectional_iterator_tag, iterator_category >, std::bidirectional_iterator_tag, std::input_iterator_tag > enable_reverse
Definition: timeseries.hpp:150
const_iterator::iterator_category iterator_category
Definition: timeseries.hpp:111
const_iterator begin() const
Definition: timeseries.hpp:158
const_iterator find(const Date &)
Definition: timeseries.hpp:235
const_iterator cbegin() const
Definition: timeseries.hpp:223
TimeSeries(DateIterator dBegin, DateIterator dEnd, ValueIterator vBegin)
Definition: timeseries.hpp:65
std::function< T(const container_value_type &)> projection_value
Definition: timeseries.hpp:175
std::vector< T > values() const
returns the historical data
Definition: timeseries.hpp:254
Container::value_type container_value_type
Definition: timeseries.hpp:171
bool empty() const
returns whether the series contains any data
Definition: timeseries.hpp:217
std::function< Date(const container_value_type &)> projection_time
Definition: timeseries.hpp:173
T & operator[](const Date &d)
Definition: timeseries.hpp:102
const_iterator cend() const
Definition: timeseries.hpp:229
const_reverse_iterator crbegin() const
Definition: timeseries.hpp:160
TimeSeries(const Date &firstDate, ValueIterator begin, ValueIterator end)
Definition: timeseries.hpp:76
std::vector< Date > dates() const
returns the dates for which historical data exist
Definition: timeseries.hpp:245
Date firstDate() const
returns the first date for which a historical datum exists
Definition: timeseries.hpp:200
Date lastDate() const
returns the last date for which a historical datum exists
Definition: timeseries.hpp:206
const_reverse_iterator crend() const
Definition: timeseries.hpp:163
static const T & get_value(const container_value_type &v)
Definition: timeseries.hpp:191
Size size() const
returns the number of historical data including null ones
Definition: timeseries.hpp:212
const_iterator end() const
Definition: timeseries.hpp:159
T operator[](const Date &d) const
returns the (possibly null) datum corresponding to the given date
Definition: timeseries.hpp:96
const_reverse_iterator rbegin() const
Definition: timeseries.hpp:166
date- and time-related classes, typedefs and enumerations
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Maps function, bind and cref to either the boost or std implementation.
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:37
STL namespace.
null values
ext::shared_ptr< BlackVolTermStructure > v
const_reverse_iterator rend() const
Definition: timeseries.hpp:127
std::reverse_iterator< typename container::const_iterator > const_reverse_iterator
Definition: timeseries.hpp:122
reverse(const container &c)
Definition: timeseries.hpp:123
const_reverse_iterator rbegin() const
Definition: timeseries.hpp:124