/src/quantlib/ql/timegrid.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb |
5 | | Copyright (C) 2005, 2006 StatPro Italia srl |
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 | | <https://www.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 timegrid.hpp |
22 | | \brief discrete time grid |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_time_grid_hpp |
26 | | #define quantlib_time_grid_hpp |
27 | | |
28 | | #include <ql/errors.hpp> |
29 | | #include <ql/math/comparison.hpp> |
30 | | #include <vector> |
31 | | #include <algorithm> |
32 | | #include <iterator> |
33 | | #include <numeric> |
34 | | #include <cmath> |
35 | | |
36 | | namespace QuantLib { |
37 | | |
38 | | //! time grid class |
39 | | /*! \todo what was the rationale for limiting the grid to |
40 | | positive times? Investigate and see whether we |
41 | | can use it for negative ones as well. |
42 | | */ |
43 | | class TimeGrid { |
44 | | public: |
45 | | //! \name Constructors |
46 | | //@{ |
47 | 0 | TimeGrid() = default; |
48 | | //! Regularly spaced time-grid |
49 | | TimeGrid(Time end, Size steps); |
50 | | //! Time grid with mandatory time points |
51 | | /*! Mandatory points are guaranteed to belong to the grid. |
52 | | No additional points are added. |
53 | | */ |
54 | | template <class Iterator> |
55 | | TimeGrid(Iterator begin, Iterator end) |
56 | 0 | : mandatoryTimes_(begin, end) { |
57 | 0 | QL_REQUIRE(begin != end, "empty time sequence"); |
58 | 0 | std::sort(mandatoryTimes_.begin(),mandatoryTimes_.end()); |
59 | | // We seem to assume that the grid begins at 0. |
60 | | // Let's enforce the assumption for the time being |
61 | | // (even though I'm not sure that I agree.) |
62 | 0 | QL_REQUIRE(mandatoryTimes_.front() >= 0.0, |
63 | 0 | "negative times not allowed"); |
64 | 0 | auto e = std::unique(mandatoryTimes_.begin(), mandatoryTimes_.end(), |
65 | 0 | static_cast<bool (*)(Real, Real)>(close_enough)); |
66 | 0 | mandatoryTimes_.resize(e - mandatoryTimes_.begin()); |
67 | |
|
68 | 0 | if (mandatoryTimes_[0] > 0.0) |
69 | 0 | times_.push_back(0.0); |
70 | |
|
71 | 0 | times_.insert(times_.end(), |
72 | 0 | mandatoryTimes_.begin(), mandatoryTimes_.end()); |
73 | |
|
74 | 0 | dt_.reserve(times_.size()-1); |
75 | 0 | std::adjacent_difference(times_.begin()+1,times_.end(), |
76 | 0 | std::back_inserter(dt_)); |
77 | |
|
78 | 0 | } |
79 | | //! Time grid with mandatory time points |
80 | | /*! Mandatory points are guaranteed to belong to the grid. |
81 | | Additional points are then added with regular spacing |
82 | | between pairs of mandatory times in order to reach the |
83 | | desired number of steps. |
84 | | */ |
85 | | template <class Iterator> |
86 | | TimeGrid(Iterator begin, Iterator end, Size steps) |
87 | 0 | : mandatoryTimes_(begin, end) { |
88 | 0 | QL_REQUIRE(begin != end, "empty time sequence"); |
89 | 0 | std::sort(mandatoryTimes_.begin(),mandatoryTimes_.end()); |
90 | | // We seem to assume that the grid begins at 0. |
91 | | // Let's enforce the assumption for the time being |
92 | | // (even though I'm not sure that I agree.) |
93 | 0 | QL_REQUIRE(mandatoryTimes_.front() >= 0.0, |
94 | 0 | "negative times not allowed"); |
95 | 0 | auto e = std::unique(mandatoryTimes_.begin(), mandatoryTimes_.end(), |
96 | 0 | static_cast<bool (*)(Real, Real)>(close_enough)); |
97 | 0 | mandatoryTimes_.resize(e - mandatoryTimes_.begin()); |
98 | |
|
99 | 0 | Time last = mandatoryTimes_.back(); |
100 | 0 | Time dtMax; |
101 | | // The resulting timegrid have points at times listed in the input |
102 | | // list. Between these points, there are inner-points which are |
103 | | // regularly spaced. |
104 | 0 | if (steps == 0) { |
105 | 0 | std::vector<Time> diff; |
106 | 0 | std::adjacent_difference(mandatoryTimes_.begin(), |
107 | 0 | mandatoryTimes_.end(), |
108 | 0 | std::back_inserter(diff)); |
109 | 0 | QL_REQUIRE(!diff.empty(), "at least two distinct points required in time grid"); |
110 | | |
111 | 0 | if (diff.front()==0.0) |
112 | 0 | diff.erase(diff.begin()); |
113 | |
|
114 | 0 | auto i = std::min_element(diff.begin(), diff.end()); |
115 | 0 | QL_REQUIRE(i != diff.end(), "not enough distinct points in time grid"); |
116 | 0 | dtMax = *i; |
117 | 0 | } else { |
118 | 0 | dtMax = last/steps; |
119 | 0 | } |
120 | | |
121 | 0 | Time periodBegin = 0.0; |
122 | 0 | times_.push_back(periodBegin); |
123 | 0 | for (auto t=mandatoryTimes_.begin(); |
124 | 0 | t<mandatoryTimes_.end(); |
125 | 0 | ++t) { |
126 | 0 | Time periodEnd = *t; |
127 | 0 | if (periodEnd != 0.0) { |
128 | | // the nearest integer, at least 1 |
129 | 0 | Size nSteps = std::max(Size(std::lround((periodEnd - periodBegin)/dtMax)), Size(1)); |
130 | 0 | Time dt = (periodEnd - periodBegin)/nSteps; |
131 | 0 | for (Size n=1; n<=nSteps; ++n) |
132 | 0 | times_.push_back(periodBegin + n*dt); |
133 | 0 | } |
134 | 0 | periodBegin = periodEnd; |
135 | 0 | } |
136 | |
|
137 | 0 | dt_.reserve(times_.size()-1); |
138 | 0 | std::adjacent_difference(times_.begin()+1,times_.end(), |
139 | 0 | std::back_inserter(dt_)); |
140 | 0 | } |
141 | | TimeGrid(std::initializer_list<Time> times) |
142 | 0 | : TimeGrid(times.begin(), times.end()) {} |
143 | | TimeGrid(std::initializer_list<Time> times, Size steps) |
144 | 0 | : TimeGrid(times.begin(), times.end(), steps) {} |
145 | | //@} |
146 | | //! \name Time grid interface |
147 | | //@{ |
148 | | //! returns the index i such that grid[i] = t |
149 | | Size index(Time t) const; |
150 | | //! returns the index i such that grid[i] is closest to t |
151 | | Size closestIndex(Time t) const; |
152 | | //! returns the time on the grid closest to the given t |
153 | 0 | Time closestTime(Time t) const { |
154 | 0 | return times_[closestIndex(t)]; |
155 | 0 | } |
156 | 0 | const std::vector<Time>& mandatoryTimes() const { |
157 | 0 | return mandatoryTimes_; |
158 | 0 | } |
159 | 636k | Time dt(Size i) const { return dt_[i]; } |
160 | | //@} |
161 | | //! \name sequence interface |
162 | | //@{ |
163 | | typedef std::vector<Time>::const_iterator const_iterator; |
164 | | typedef std::vector<Time>::const_reverse_iterator |
165 | | const_reverse_iterator; |
166 | | |
167 | 636k | Time operator[](Size i) const { return times_[i]; } |
168 | 0 | Time at(Size i) const { return times_.at(i); } |
169 | 766k | Size size() const { return times_.size(); } |
170 | 0 | bool empty() const { return times_.empty(); } |
171 | 0 | const_iterator begin() const { return times_.begin(); } |
172 | 0 | const_iterator end() const { return times_.end(); } |
173 | 0 | const_reverse_iterator rbegin() const { return times_.rbegin(); } |
174 | 0 | const_reverse_iterator rend() const { return times_.rend(); } |
175 | 0 | Time front() const { return times_.front(); } |
176 | 0 | Time back() const { return times_.back(); } |
177 | | //@} |
178 | | private: |
179 | | std::vector<Time> times_; |
180 | | std::vector<Time> dt_; |
181 | | std::vector<Time> mandatoryTimes_; |
182 | | }; |
183 | | |
184 | | } |
185 | | |
186 | | |
187 | | #endif |