QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
timegrid.cpp
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#include <ql/timegrid.hpp>
22#include <iomanip>
23
24namespace QuantLib {
25
27 // We seem to assume that the grid begins at 0.
28 // Let's enforce the assumption for the time being
29 // (even though I'm not sure that I agree.)
30 QL_REQUIRE(end > 0.0,
31 "negative times not allowed");
32 Time dt = end/steps;
33 times_.reserve(steps+1);
34 for (Size i=0; i<=steps; i++)
35 times_.push_back(dt*i);
36
37 mandatoryTimes_ = std::vector<Time>(1);
39
40 dt_ = std::vector<Time>(steps,dt);
41 }
42
44 Size i = closestIndex(t);
45 if (close_enough(t,times_[i])) {
46 return i;
47 } else {
48 if (t < times_.front()) {
49 QL_FAIL("using inadequate time grid: all nodes "
50 "are later than the required time t = "
51 << std::setprecision(12) << t
52 << " (earliest node is t1 = "
53 << std::setprecision(12) << times_.front() << ")");
54 } else if (t > times_.back()) {
55 QL_FAIL("using inadequate time grid: all nodes "
56 "are earlier than the required time t = "
57 << std::setprecision(12) << t
58 << " (latest node is t1 = "
59 << std::setprecision(12) << times_.back() << ")");
60 } else {
61 Size j, k;
62 if (t > times_[i]) {
63 j = i;
64 k = i+1;
65 } else {
66 j = i-1;
67 k = i;
68 }
69 QL_FAIL("using inadequate time grid: the nodes closest "
70 "to the required time t = "
71 << std::setprecision(12) << t
72 << " are t1 = "
73 << std::setprecision(12) << times_[j]
74 << " and t2 = "
75 << std::setprecision(12) << times_[k]);
76 }
77 }
78 }
79
81 auto begin = times_.begin(), end = times_.end();
82 auto result = std::lower_bound(begin, end, t);
83 if (result == begin) {
84 return 0;
85 } else if (result == end) {
86 return size()-1;
87 } else {
88 Time dt1 = *result - t;
89 Time dt2 = t - *(result-1);
90 if (dt1 < dt2)
91 return result-begin;
92 else
93 return (result-begin)-1;
94 }
95 }
96
97}
98
std::vector< Time > mandatoryTimes_
Definition: timegrid.hpp:176
Time dt(Size i) const
Definition: timegrid.hpp:154
const_iterator begin() const
Definition: timegrid.hpp:166
std::vector< Time > dt_
Definition: timegrid.hpp:175
std::vector< Time > times_
Definition: timegrid.hpp:174
Size index(Time t) const
returns the index i such that grid[i] = t
Definition: timegrid.cpp:43
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 DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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
discrete time grid