QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
timeseries.hpp
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
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
42
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:
59 TimeSeries() = default;
64 template <class DateIterator, class ValueIterator>
65 TimeSeries(DateIterator dBegin, DateIterator dEnd,
66 ValueIterator vBegin) {
67 while (dBegin != dEnd)
68 values_[*(dBegin++)] = *(vBegin++);
69 }
75 template <class ValueIterator>
77 ValueIterator begin, ValueIterator end) {
78 Date d = firstDate;
79 while (begin != end)
80 values_[d++] = *(begin++);
81 }
83
84
85 Date firstDate() const;
87 Date lastDate() const;
89 Size size() const;
91 bool empty() const;
93
95
96 T operator[](const Date& d) const {
97 typename Container::const_iterator 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 }
107
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<iterator_category, std::bidirectional_iterator_tag>::value ||
149 std::is_base_of<std::bidirectional_iterator_tag, iterator_category>::value,
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(); }
169
170 private:
171 typedef typename Container::value_type container_value_type;
172 typedef ext::function<Date(const container_value_type&)>
174 typedef ext::function<T(const container_value_type&)>
176
177 public:
179
180
184 [[deprecated("Use const_iterator instead and access the `first` data member.")]]
185 typedef boost::transform_iterator<projection_time, const_iterator> const_time_iterator;
186
190 [[deprecated("Use const_iterator instead and access the `second` data member.")]]
191 typedef boost::transform_iterator<projection_value, const_iterator> const_value_iterator;
192
196 [[deprecated("Use const_reverse_iterator instead and access the `first` data member.")]]
197 typedef boost::transform_iterator<projection_time, const_reverse_iterator> const_reverse_time_iterator;
198
202 [[deprecated("Use const_reverse_iterator instead and access the `second` data member.")]]
203 typedef boost::transform_iterator<projection_value, const_reverse_iterator> const_reverse_value_iterator;
204
205 QL_DEPRECATED_DISABLE_WARNING
206
210 [[deprecated("Use cbegin instead and access the `second` data member.")]]
213 }
214
218 [[deprecated("Use cend instead and access the `second` data member.")]]
221 }
222
226 [[deprecated("Use crbegin instead and access the `second` data member.")]]
229 }
230
234 [[deprecated("Use crend instead and access the `second` data member.")]]
237 }
238
242 [[deprecated("Use cbegin instead and access the `first` data member.")]]
245 }
246
250 [[deprecated("Use cend instead and access the `first` data member.")]]
253 }
254
258 [[deprecated("Use crbegin instead and access the `first` data member.")]]
261 }
262
266 [[deprecated("Use crend instead and access the `first` data member.")]]
269 }
270
271 QL_DEPRECATED_ENABLE_WARNING
272
274
277 std::vector<Date> dates() const;
279 std::vector<T> values() const;
281
282 private:
283 static const Date& get_time (const container_value_type& v) {
284 return v.first;
285 }
286 static const T& get_value (const container_value_type& v) {
287 return v.second;
288 }
289 };
290
291
292 // inline definitions
293
294 template <class T, class C>
296 QL_REQUIRE(!values_.empty(), "empty timeseries");
297 return values_.begin()->first;
298 }
299
300 template <class T, class C>
302 QL_REQUIRE(!values_.empty(), "empty timeseries");
303 return rbegin()->first;
304 }
305
306 template <class T, class C>
308 return values_.size();
309 }
310
311 template <class T, class C>
312 inline bool TimeSeries<T,C>::empty() const {
313 return values_.empty();
314 }
315
316 template <class T, class C>
317 inline typename TimeSeries<T,C>::const_iterator
319 return values_.begin();
320 }
321
322 template <class T, class C>
323 inline typename TimeSeries<T,C>::const_iterator
325 return values_.end();
326 }
327
328 template <class T, class C>
329 inline typename TimeSeries<T,C>::const_iterator
331 const_iterator i = values_.find(d);
332 if (i == values_.end()) {
333 values_[d] = Null<T>();
334 i = values_.find(d);
335 }
336 return i;
337 }
338
339 template <class T, class C>
340 std::vector<Date> TimeSeries<T,C>::dates() const {
341 std::vector<Date> v;
342 v.reserve(size());
343 std::transform(cbegin(), cend(), std::back_inserter(v),
345 return v;
346 }
347
348 template <class T, class C>
349 std::vector<T> TimeSeries<T,C>::values() const {
350 std::vector<T> v;
351 v.reserve(size());
352 std::transform(cbegin(), cend(), std::back_inserter(v),
354 return v;
355 }
356
357}
358
359#endif
Concrete date class.
Definition: date.hpp:125
template class providing a null value for a given type.
Definition: null.hpp:76
Container for historical data.
Definition: timeseries.hpp:51
static const Date & get_time(const container_value_type &v)
Definition: timeseries.hpp:283
const_reverse_iterator rend() const
Definition: timeseries.hpp:167
Container::const_iterator const_iterator
Definition: timeseries.hpp:110
const_value_iterator cend_values() const
Definition: timeseries.hpp:219
reverse< Container, enable_reverse >::const_reverse_iterator const_reverse_iterator
Definition: timeseries.hpp:154
const_reverse_time_iterator crend_time() const
Definition: timeseries.hpp:267
ext::function< T(const container_value_type &)> projection_value
Definition: timeseries.hpp:175
boost::transform_iterator< projection_time, const_reverse_iterator > const_reverse_time_iterator
Definition: timeseries.hpp:197
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:330
std::conditional_t< std::is_same< iterator_category, std::bidirectional_iterator_tag >::value||std::is_base_of< std::bidirectional_iterator_tag, iterator_category >::value, std::bidirectional_iterator_tag, std::input_iterator_tag > enable_reverse
Definition: timeseries.hpp:150
const_iterator cbegin() const
Definition: timeseries.hpp:318
const_reverse_time_iterator crbegin_time() const
Definition: timeseries.hpp:259
TimeSeries(DateIterator dBegin, DateIterator dEnd, ValueIterator vBegin)
Definition: timeseries.hpp:65
const_reverse_value_iterator crbegin_values() const
Definition: timeseries.hpp:227
std::vector< T > values() const
returns the historical data
Definition: timeseries.hpp:349
ext::function< Date(const container_value_type &)> projection_time
Definition: timeseries.hpp:173
Container::value_type container_value_type
Definition: timeseries.hpp:171
bool empty() const
returns whether the series contains any data
Definition: timeseries.hpp:312
boost::transform_iterator< projection_value, const_reverse_iterator > const_reverse_value_iterator
Definition: timeseries.hpp:203
T & operator[](const Date &d)
Definition: timeseries.hpp:102
const_iterator cend() const
Definition: timeseries.hpp:324
const_reverse_value_iterator crend_values() const
Definition: timeseries.hpp:235
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:340
Date firstDate() const
returns the first date for which a historical datum exists
Definition: timeseries.hpp:295
boost::transform_iterator< projection_value, const_iterator > const_value_iterator
Definition: timeseries.hpp:191
Date lastDate() const
returns the last date for which a historical datum exists
Definition: timeseries.hpp:301
const_reverse_iterator crend() const
Definition: timeseries.hpp:163
const_time_iterator cbegin_time() const
Definition: timeseries.hpp:243
QL_DEPRECATED_DISABLE_WARNING const_value_iterator cbegin_values() const
Definition: timeseries.hpp:211
static const T & get_value(const container_value_type &v)
Definition: timeseries.hpp:286
Size size() const
returns the number of historical data including null ones
Definition: timeseries.hpp:307
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
boost::transform_iterator< projection_time, const_iterator > const_time_iterator
Definition: timeseries.hpp:185
const_time_iterator cend_time() const
Definition: timeseries.hpp:251
const_reverse_iterator rbegin() const
Definition: timeseries.hpp:166
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
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