BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_periodicmaactualactual.h
Go to the documentation of this file.
1/// @file bbldc_periodicmaactualactual.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_periodicmaactualactual.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_PERIODICMAACTUALACTUAL
9#define INCLUDED_BBLDC_PERIODICMAACTUALACTUAL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_periodicmaactualactual bbldc_periodicmaactualactual
15/// @brief Provide stateless functions for period-based ICMA Actual/Actual.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_periodicmaactualactual
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_periodicmaactualactual-purpose"> Purpose</a>
25/// * <a href="#bbldc_periodicmaactualactual-classes"> Classes </a>
26/// * <a href="#bbldc_periodicmaactualactual-description"> Description </a>
27/// * <a href="#bbldc_periodicmaactualactual-usage"> Usage </a>
28/// * <a href="#bbldc_periodicmaactualactual-example-1-computing-day-count-and-year-fraction"> Example 1: Computing Day Count and Year Fraction </a>
29///
30/// # Purpose {#bbldc_periodicmaactualactual-purpose}
31/// Provide stateless functions for period-based ICMA Actual/Actual.
32///
33/// # Classes {#bbldc_periodicmaactualactual-classes}
34///
35/// - bbldc::PeriodIcmaActualActual: ICMA Act/Act convention stateless functions
36///
37/// # Description {#bbldc_periodicmaactualactual-description}
38/// This component provides a `struct`,
39/// `bbldc::PeriodIcmaActualActual`, that serves as a namespace for defining a
40/// suite of date-related functions used to compute the day count and the year
41/// fraction between two dates as per the ICMA Actual/Actual day-count
42/// convention. In this day-count convention, the day count between two dates
43/// is exactly the number of days occurring in the time period and the year
44/// fraction is the number of full periods multiplied by the period weighting
45/// (`periodYearDiff`) plus front-stub and back-stub adjustments. The periods
46/// are defined by the `periodDate` elements representing, say, bond coupon
47/// payment dates with the first period starting on `periodDate[0]` and ending
48/// on `periodDate[1], the second period starting on `periodDate[1]' and ending
49/// on `periodDate[2], and the `i'-th period starting on `periodDate[i - 1]` and
50/// ending on `periodDate[i]`.
51///
52/// ## Usage {#bbldc_periodicmaactualactual-usage}
53///
54///
55/// This section illustrates intended use of this component.
56///
57/// ### Example 1: Computing Day Count and Year Fraction {#bbldc_periodicmaactualactual-example-1-computing-day-count-and-year-fraction}
58///
59///
60/// The following snippets of code illustrate how to use
61/// `bbldc::PeriodIcmaActualActual` methods. First, create two `bdlt::Date`
62/// variables, `d1` and `d2`:
63/// @code
64/// const bdlt::Date d1(2003, 10, 19);
65/// const bdlt::Date d2(2003, 12, 31);
66/// @endcode
67/// Then, create a schedule of period dates, `sched`, corresponding to a
68/// quarterly payment (`periodYearDiff == 0.25`):
69/// @code
70/// bsl::vector<bdlt::Date> sched;
71/// sched.push_back(bdlt::Date(2003, 10, 1));
72/// sched.push_back(bdlt::Date(2004, 1, 1));
73/// @endcode
74/// Next, compute the day count between `d1` and `d2`:
75/// @code
76/// const int daysDiff = bbldc::PeriodIcmaActualActual::daysDiff(d1, d2);
77/// assert(73 == daysDiff);
78/// @endcode
79/// Finally, compute the year fraction between the two dates:
80/// @code
81/// const double yearsDiff = bbldc::PeriodIcmaActualActual::yearsDiff(d1,
82/// d2,
83/// sched,
84/// 0.25);
85/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
86/// assert(yearsDiff > 0.1983 && yearsDiff < 0.1985);
87/// @endcode
88/// @}
89/** @} */
90/** @} */
91
92/** @addtogroup bbl
93 * @{
94 */
95/** @addtogroup bbldc
96 * @{
97 */
98/** @addtogroup bbldc_periodicmaactualactual
99 * @{
100 */
101
102#include <bblscm_version.h>
103
104#include <bdlt_date.h>
105
106#include <bsls_libraryfeatures.h>
107
108#include <bsl_vector.h>
109
110#include <vector> // 'std::vector', 'std::pmr::vector'
111
112
113namespace bbldc {
114
115 // =============================
116 // struct PeriodIcmaActualActual
117 // =============================
118
119/// This `struct` provides a namespace for a suite of pure functions that
120/// compute values based on dates according to the ICMA Actual/Actual
121/// day-count convention.
123
124 // CLASS METHODS
125
126 /// Return the (signed) number of days between the specified `beginDate`
127 /// and `endDate` according to the ICMA Actual/Actual day-count
128 /// convention. If `beginDate <= endDate`, then the result is
129 /// non-negative. Note that reversing the order of `beginDate` and
130 /// `endDate` negates the result.
131 static int daysDiff(const bdlt::Date& beginDate,
132 const bdlt::Date& endDate);
133
134 static double yearsDiff(const bdlt::Date& beginDate,
135 const bdlt::Date& endDate,
136 const bdlt::Date *periodDateBegin,
137 const bdlt::Date *periodDateEnd,
138 double periodYearDiff);
139
140 static double yearsDiff(
141 const bdlt::Date& beginDate,
142 const bdlt::Date& endDate,
143 const bsl::vector<bdlt::Date>& periodDate,
144 double periodYearDiff);
145 /// Return the (signed fractional) number of years between the specified
146 /// `beginDate` and `endDate` according to the ICMA Actual/Actual
147 /// day-count convention with periods starting on the specified range
148 /// `[ periodDateBegin, periodDateEnd )` values and each period having a
149 /// duration of the specified `periodYearDiff` years (e.g., 0.25 for
150 /// quarterly periods). If `beginDate <= endDate`, then the result is
151 /// non-negative. The behavior is undefined unless
152 /// `2 <= periodDateEnd - periodDateBegin`, the values contained in the
153 /// range are unique and sorted from minimum to maximum,
154 /// `*periodDateBegin <= beginDate <= *(periodDateEnd - 1)`, and
155 /// `*periodDateBegin <= endDate <= *(periodDateEnd - 1)`. Note that
156 /// reversing the order of `beginDate` and `endDate` negates the result;
157 /// specifically,
158 /// `|yearsDiff(b, e, pd, pyd) + yearsDiff(e, b, pd, pyd)| <= 1.0e-15`
159 /// for all dates `b` and `e`, periods `pd`, and year fraction per
160 /// period `pyd`.
161 static double yearsDiff(
162 const bdlt::Date& beginDate,
163 const bdlt::Date& endDate,
164 const std::vector<bdlt::Date>& periodDate,
165 double periodYearDiff);
166#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
167 static double yearsDiff(
168 const bdlt::Date& beginDate,
169 const bdlt::Date& endDate,
170 const std::pmr::vector<bdlt::Date>& periodDate,
171 double periodYearDiff);
172#endif
173 // Return the (signed fractional) number of years between the specified
174 // 'beginDate' and 'endDate' according to the ICMA Actual/Actual
175 // day-count convention with periods starting on the specified
176 // 'periodDate' values and each period having a duration of the
177 // specified 'periodYearDiff' years (e.g., 0.25 for quarterly periods).
178 // If 'beginDate <= endDate', then the result is non-negative. The
179 // behavior is undefined unless 'periodDate.size() >= 2', the values
180 // contained in 'periodDate' are unique and sorted from minimum to
181 // maximum, 'periodDate.front() <= beginDate <= periodDate.back()', and
182 // 'periodDate.front() <= endDate <= periodDate.back()'. Note that
183 // reversing the order of 'beginDate' and 'endDate' negates the result;
184 // specifically,
185 // '|yearsDiff(b, e, pd, pyd) + yearsDiff(e, b, pd, pyd)| <= 1.0e-15'
186 // for all dates 'b' and 'e', periods 'pd', and year fraction per
187 // period 'pyd'.
188};
189
190// ============================================================================
191// INLINE DEFINITIONS
192// ============================================================================
193
194 // -----------------------------
195 // struct PeriodIcmaActualActual
196 // -----------------------------
197
198// CLASS METHODS
199inline
201 const bdlt::Date& endDate)
202{
203 return endDate - beginDate;
204}
205
206inline
208 const bdlt::Date& beginDate,
209 const bdlt::Date& endDate,
210 const bsl::vector<bdlt::Date>& periodDate,
211 double periodYearDiff)
212{
213 return yearsDiff(beginDate,
214 endDate,
215 periodDate.data(),
216 periodDate.data() + periodDate.size(),
217 periodYearDiff);
218}
219
220inline
222 const bdlt::Date& beginDate,
223 const bdlt::Date& endDate,
224 const std::vector<bdlt::Date>& periodDate,
225 double periodYearDiff)
226{
227 // Some implmentations of 'std::vector', notably Aix and Solaris, do not
228 // provide the 'data' accessor.
229
230 const bdlt::Date *begin = periodDate.empty() ? 0 : &*periodDate.begin();
231 const bdlt::Date *end = begin + periodDate.size();
232
233 return yearsDiff(beginDate,
234 endDate,
235 begin,
236 end,
237 periodYearDiff);
238}
239
240#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
241inline
243 const bdlt::Date& beginDate,
244 const bdlt::Date& endDate,
245 const std::pmr::vector<bdlt::Date>& periodDate,
246 double periodYearDiff)
247{
248 return yearsDiff(beginDate,
249 endDate,
250 periodDate.data(),
251 periodDate.data() + periodDate.size(),
252 periodYearDiff);
253}
254#endif
255
256} // close package namespace
257
258
259#endif
260
261// ----------------------------------------------------------------------------
262// Copyright 2015 Bloomberg Finance L.P.
263//
264// Licensed under the Apache License, Version 2.0 (the "License");
265// you may not use this file except in compliance with the License.
266// You may obtain a copy of the License at
267//
268// http://www.apache.org/licenses/LICENSE-2.0
269//
270// Unless required by applicable law or agreed to in writing, software
271// distributed under the License is distributed on an "AS IS" BASIS,
272// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
273// See the License for the specific language governing permissions and
274// limitations under the License.
275// ----------------------------- END-OF-FILE ----------------------------------
276
277/** @} */
278/** @} */
279/** @} */
Definition bdlt_date.h:294
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
VALUE_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2587
Definition bslstl_vector.h:1025
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicactual360.h:107
Definition bbldc_periodicmaactualactual.h:122
static int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate)
Definition bbldc_periodicmaactualactual.h:200
static double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, const bdlt::Date *periodDateBegin, const bdlt::Date *periodDateEnd, double periodYearDiff)