QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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 if (diff.front()==0.0)
110 diff.erase(diff.begin());
111 dtMax = *(std::min_element(diff.begin(), diff.end()));
112 } else {
113 dtMax = last/steps;
114 }
115
116 Time periodBegin = 0.0;
117 times_.push_back(periodBegin);
118 for (std::vector<Time>::const_iterator t=mandatoryTimes_.begin();
119 t<mandatoryTimes_.end();
120 ++t) {
121 Time periodEnd = *t;
122 if (periodEnd != 0.0) {
123 // the nearest integer, at least 1
124 Size nSteps = std::max(Size(std::lround((periodEnd - periodBegin)/dtMax)), Size(1));
125 Time dt = (periodEnd - periodBegin)/nSteps;
126 for (Size n=1; n<=nSteps; ++n)
127 times_.push_back(periodBegin + n*dt);
128 }
129 periodBegin = periodEnd;
130 }
131
132 dt_.reserve(times_.size()-1);
133 std::adjacent_difference(times_.begin()+1,times_.end(),
134 std::back_inserter(dt_));
135 }
136 TimeGrid(std::initializer_list<Time> times)
137 : TimeGrid(times.begin(), times.end()) {}
138 TimeGrid(std::initializer_list<Time> times, Size steps)
139 : TimeGrid(times.begin(), times.end(), steps) {}
140 //@}
141 //! \name Time grid interface
142 //@{
143 //! returns the index i such that grid[i] = t
144 Size index(Time t) const;
145 //! returns the index i such that grid[i] is closest to t
146 Size closestIndex(Time t) const;
147 //! returns the time on the grid closest to the given t
149 return times_[closestIndex(t)];
150 }
151 const std::vector<Time>& mandatoryTimes() const {
152 return mandatoryTimes_;
153 }
154 Time dt(Size i) const { return dt_[i]; }
155 //@}
156 //! \name sequence interface
157 //@{
158 typedef std::vector<Time>::const_iterator const_iterator;
159 typedef std::vector<Time>::const_reverse_iterator
161
162 Time operator[](Size i) const { return times_[i]; }
163 Time at(Size i) const { return times_.at(i); }
164 Size size() const { return times_.size(); }
165 bool empty() const { return times_.empty(); }
166 const_iterator begin() const { return times_.begin(); }
167 const_iterator end() const { return times_.end(); }
168 const_reverse_iterator rbegin() const { return times_.rbegin(); }
169 const_reverse_iterator rend() const { return times_.rend(); }
170 Time front() const { return times_.front(); }
171 Time back() const { return times_.back(); }
172 //@}
173 private:
174 std::vector<Time> times_;
175 std::vector<Time> dt_;
176 std::vector<Time> mandatoryTimes_;
177 };
178
179}
180
181
182#endif
time grid class
Definition: timegrid.hpp:43
const_reverse_iterator rend() const
Definition: timegrid.hpp:169
std::vector< Time > mandatoryTimes_
Definition: timegrid.hpp:176
Time back() const
Definition: timegrid.hpp:171
Time dt(Size i) const
Definition: timegrid.hpp:154
TimeGrid(Iterator begin, Iterator end)
Time grid with mandatory time points.
Definition: timegrid.hpp:55
const_iterator begin() const
Definition: timegrid.hpp:166
std::vector< Time > dt_
Definition: timegrid.hpp:175
std::vector< Time >::const_reverse_iterator const_reverse_iterator
Definition: timegrid.hpp:160
TimeGrid(std::initializer_list< Time > times)
Definition: timegrid.hpp:136
Time operator[](Size i) const
Definition: timegrid.hpp:162
TimeGrid(Iterator begin, Iterator end, Size steps)
Time grid with mandatory time points.
Definition: timegrid.hpp:86
std::vector< Time > times_
Definition: timegrid.hpp:174
bool empty() const
Definition: timegrid.hpp:165
Time closestTime(Time t) const
returns the time on the grid closest to the given t
Definition: timegrid.hpp:148
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:163
Time front() const
Definition: timegrid.hpp:170
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:164
const_iterator end() const
Definition: timegrid.hpp:167
const std::vector< Time > & mandatoryTimes() const
Definition: timegrid.hpp:151
TimeGrid(std::initializer_list< Time > times, Size steps)
Definition: timegrid.hpp:138
const_reverse_iterator rbegin() const
Definition: timegrid.hpp:168
std::vector< Time >::const_iterator const_iterator
Definition: timegrid.hpp:158
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:35
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182