QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
timegrid.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) 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 <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 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>
30#include <vector>
31#include <algorithm>
32#include <iterator>
33#include <numeric>
34#include <cmath>
35
36namespace 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 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)
57 QL_REQUIRE(begin != end, "empty time sequence");
58 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 QL_REQUIRE(mandatoryTimes_.front() >= 0.0,
63 "negative times not allowed");
64 auto e = std::unique(mandatoryTimes_.begin(), mandatoryTimes_.end(),
65 static_cast<bool (*)(Real, Real)>(close_enough));
66 mandatoryTimes_.resize(e - mandatoryTimes_.begin());
67
68 if (mandatoryTimes_[0] > 0.0)
69 times_.push_back(0.0);
70
71 times_.insert(times_.end(),
72 mandatoryTimes_.begin(), mandatoryTimes_.end());
73
74 dt_.reserve(times_.size()-1);
75 std::adjacent_difference(times_.begin()+1,times_.end(),
76 std::back_inserter(dt_));
77
78 }
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)
88 QL_REQUIRE(begin != end, "empty time sequence");
89 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 QL_REQUIRE(mandatoryTimes_.front() >= 0.0,
94 "negative times not allowed");
95 auto e = std::unique(mandatoryTimes_.begin(), mandatoryTimes_.end(),
96 static_cast<bool (*)(Real, Real)>(close_enough));
97 mandatoryTimes_.resize(e - mandatoryTimes_.begin());
98
99 Time last = mandatoryTimes_.back();
100 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 if (steps == 0) {
105 std::vector<Time> diff;
106 std::adjacent_difference(mandatoryTimes_.begin(),
107 mandatoryTimes_.end(),
108 std::back_inserter(diff));
109 QL_REQUIRE(!diff.empty(), "at least two distinct points required in time grid");
110
111 if (diff.front()==0.0)
112 diff.erase(diff.begin());
113
114 auto i = std::min_element(diff.begin(), diff.end());
115 QL_REQUIRE(i != diff.end(), "not enough distinct points in time grid");
116 dtMax = *i;
117 } else {
118 dtMax = last/steps;
119 }
120
121 Time periodBegin = 0.0;
122 times_.push_back(periodBegin);
123 for (auto t=mandatoryTimes_.begin();
124 t<mandatoryTimes_.end();
125 ++t) {
126 Time periodEnd = *t;
127 if (periodEnd != 0.0) {
128 // the nearest integer, at least 1
129 Size nSteps = std::max(Size(std::lround((periodEnd - periodBegin)/dtMax)), Size(1));
130 Time dt = (periodEnd - periodBegin)/nSteps;
131 for (Size n=1; n<=nSteps; ++n)
132 times_.push_back(periodBegin + n*dt);
133 }
134 periodBegin = periodEnd;
135 }
136
137 dt_.reserve(times_.size()-1);
138 std::adjacent_difference(times_.begin()+1,times_.end(),
139 std::back_inserter(dt_));
140 }
141 TimeGrid(std::initializer_list<Time> times)
142 : TimeGrid(times.begin(), times.end()) {}
143 TimeGrid(std::initializer_list<Time> times, Size steps)
144 : 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
154 return times_[closestIndex(t)];
155 }
156 const std::vector<Time>& mandatoryTimes() const {
157 return mandatoryTimes_;
158 }
159 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
166
167 Time operator[](Size i) const { return times_[i]; }
168 Time at(Size i) const { return times_.at(i); }
169 Size size() const { return times_.size(); }
170 bool empty() const { return times_.empty(); }
171 const_iterator begin() const { return times_.begin(); }
172 const_iterator end() const { return times_.end(); }
173 const_reverse_iterator rbegin() const { return times_.rbegin(); }
174 const_reverse_iterator rend() const { return times_.rend(); }
175 Time front() const { return times_.front(); }
176 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
time grid class
Definition: timegrid.hpp:43
const_reverse_iterator rend() const
Definition: timegrid.hpp:174
std::vector< Time > mandatoryTimes_
Definition: timegrid.hpp:181
Time back() const
Definition: timegrid.hpp:176
Time dt(Size i) const
Definition: timegrid.hpp:159
TimeGrid(Iterator begin, Iterator end)
Time grid with mandatory time points.
Definition: timegrid.hpp:55
const_iterator begin() const
Definition: timegrid.hpp:171
std::vector< Time > dt_
Definition: timegrid.hpp:180
std::vector< Time >::const_reverse_iterator const_reverse_iterator
Definition: timegrid.hpp:165
TimeGrid(std::initializer_list< Time > times)
Definition: timegrid.hpp:141
Time operator[](Size i) const
Definition: timegrid.hpp:167
TimeGrid(Iterator begin, Iterator end, Size steps)
Time grid with mandatory time points.
Definition: timegrid.hpp:86
std::vector< Time > times_
Definition: timegrid.hpp:179
bool empty() const
Definition: timegrid.hpp:170
Time closestTime(Time t) const
returns the time on the grid closest to the given t
Definition: timegrid.hpp:153
Size index(Time t) const
returns the index i such that grid[i] = t
Definition: timegrid.cpp:43
Time at(Size i) const
Definition: timegrid.hpp:168
Time front() const
Definition: timegrid.hpp:175
Size closestIndex(Time t) const
returns the index i such that grid[i] is closest to t
Definition: timegrid.cpp:80
Size size() const
Definition: timegrid.hpp:169
const_iterator end() const
Definition: timegrid.hpp:172
const std::vector< Time > & mandatoryTimes() const
Definition: timegrid.hpp:156
TimeGrid(std::initializer_list< Time > times, Size steps)
Definition: timegrid.hpp:143
const_reverse_iterator rbegin() const
Definition: timegrid.hpp:173
std::vector< Time >::const_iterator const_iterator
Definition: timegrid.hpp:163
floating-point comparisons
const DefaultType & t
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
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:37
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182