| 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> |
| 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 | 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 | : mandatoryTimes_(begin, end) { |
| 57 | QL_REQUIRE(begin != end, "empty time sequence" ); |
| 58 | std::sort(first: mandatoryTimes_.begin(),last: 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(first: mandatoryTimes_.begin(), last: mandatoryTimes_.end(), |
| 65 | binary_pred: static_cast<bool (*)(Real, Real)>(close_enough)); |
| 66 | mandatoryTimes_.resize(new_size: e - mandatoryTimes_.begin()); |
| 67 | |
| 68 | if (mandatoryTimes_[0] > 0.0) |
| 69 | times_.push_back(x: 0.0); |
| 70 | |
| 71 | times_.insert(position: times_.end(), |
| 72 | first: mandatoryTimes_.begin(), last: mandatoryTimes_.end()); |
| 73 | |
| 74 | dt_.reserve(n: times_.size()-1); |
| 75 | std::adjacent_difference(first: times_.begin()+1,last: times_.end(), |
| 76 | result: std::back_inserter(x&: 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) |
| 87 | : mandatoryTimes_(begin, end) { |
| 88 | QL_REQUIRE(begin != end, "empty time sequence" ); |
| 89 | std::sort(first: mandatoryTimes_.begin(),last: 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(first: mandatoryTimes_.begin(), last: mandatoryTimes_.end(), |
| 96 | binary_pred: static_cast<bool (*)(Real, Real)>(close_enough)); |
| 97 | mandatoryTimes_.resize(new_size: 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(first: mandatoryTimes_.begin(), |
| 107 | last: mandatoryTimes_.end(), |
| 108 | result: std::back_inserter(x&: diff)); |
| 109 | if (diff.front()==0.0) |
| 110 | diff.erase(position: diff.begin()); |
| 111 | dtMax = *(std::min_element(first: diff.begin(), last: diff.end())); |
| 112 | } else { |
| 113 | dtMax = last/steps; |
| 114 | } |
| 115 | |
| 116 | Time periodBegin = 0.0; |
| 117 | times_.push_back(x: 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(a: Size(std::lround(x: (periodEnd - periodBegin)/dtMax)), b: Size(1)); |
| 125 | Time dt = (periodEnd - periodBegin)/nSteps; |
| 126 | for (Size n=1; n<=nSteps; ++n) |
| 127 | times_.push_back(x: periodBegin + n*dt); |
| 128 | } |
| 129 | periodBegin = periodEnd; |
| 130 | } |
| 131 | |
| 132 | dt_.reserve(n: times_.size()-1); |
| 133 | std::adjacent_difference(first: times_.begin()+1,last: times_.end(), |
| 134 | result: std::back_inserter(x&: 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 |
| 148 | Time closestTime(Time t) const { |
| 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 |
| 160 | const_reverse_iterator; |
| 161 | |
| 162 | Time operator[](Size i) const { return times_[i]; } |
| 163 | Time at(Size i) const { return times_.at(n: 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 |
| 183 | |