BDE 4.39.x 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.
122///
123/// See @ref bbldc_periodicmaactualactual
125
126 // CLASS METHODS
127
128 /// Return the (signed) number of days between the specified `beginDate`
129 /// and `endDate` according to the ICMA Actual/Actual day-count
130 /// convention. If `beginDate <= endDate`, then the result is non-negative.
131 ///
132 /// \note Note that reversing the order of `beginDate` and
133 /// `endDate` negates the result.
134 static int daysDiff(const bdlt::Date& beginDate,
135 const bdlt::Date& endDate);
136
137 /// Return the (signed fractional) number of years between the specified
138 /// `beginDate` and `endDate` according to the ICMA Actual/Actual
139 /// day-count convention with periods starting on the specified range
140 /// `[ periodDateBegin, periodDateEnd )` values and each period having a
141 /// duration of the specified `periodYearDiff` years (e.g., 0.25 for
142 /// quarterly periods). If `beginDate <= endDate`, then the result is non-negative.
143 ///
144 /// \pre The behavior is undefined unless
145 /// `2 <= periodDateEnd - periodDateBegin`, the values contained in the
146 /// range are unique and sorted from minimum to maximum,
147 /// `*periodDateBegin <= beginDate <= *(periodDateEnd - 1)`, and
148 /// `*periodDateBegin <= endDate <= *(periodDateEnd - 1)`.
149 ///
150 /// \note Note that reversing the order of `beginDate` and `endDate` negates the result;
151 /// specifically,
152 /// `|yearsDiff(b, e, pd, pyd) + yearsDiff(e, b, pd, pyd)| <= 1.0e-15`
153 /// for all dates `b` and `e`, periods `pd`, and year fraction per
154 /// period `pyd`.
155 static double yearsDiff(const bdlt::Date& beginDate,
156 const bdlt::Date& endDate,
157 const bdlt::Date *periodDateBegin,
158 const bdlt::Date *periodDateEnd,
159 double periodYearDiff);
160
161 static double yearsDiff(
162 const bdlt::Date& beginDate,
163 const bdlt::Date& endDate,
164 const bsl::vector<bdlt::Date>& periodDate,
165 double periodYearDiff);
166 static double yearsDiff(
167 const bdlt::Date& beginDate,
168 const bdlt::Date& endDate,
169 const std::vector<bdlt::Date>& periodDate,
170 double periodYearDiff);
171#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
172 static double yearsDiff(
173 const bdlt::Date& beginDate,
174 const bdlt::Date& endDate,
175 const std::pmr::vector<bdlt::Date>& periodDate,
176 double periodYearDiff);
177#endif
178 // Return the (signed fractional) number of years between the specified
179 // 'beginDate' and 'endDate' according to the ICMA Actual/Actual
180 // day-count convention with periods starting on the specified
181 // 'periodDate' values and each period having a duration of the
182 // specified 'periodYearDiff' years (e.g., 0.25 for quarterly periods).
183 // If 'beginDate <= endDate', then the result is non-negative. The
184 // behavior is undefined unless 'periodDate.size() >= 2', the values
185 // contained in 'periodDate' are unique and sorted from minimum to
186 // maximum, 'periodDate.front() <= beginDate <= periodDate.back()', and
187 // 'periodDate.front() <= endDate <= periodDate.back()'. Note that
188 // reversing the order of 'beginDate' and 'endDate' negates the result;
189 // specifically,
190 // '|yearsDiff(b, e, pd, pyd) + yearsDiff(e, b, pd, pyd)| <= 1.0e-15'
191 // for all dates 'b' and 'e', periods 'pd', and year fraction per
192 // period 'pyd'.
193};
194
195// ============================================================================
196// INLINE DEFINITIONS
197// ============================================================================
198
199 // -----------------------------
200 // struct PeriodIcmaActualActual
201 // -----------------------------
202
203// CLASS METHODS
204inline
206 const bdlt::Date& endDate)
207{
208 return endDate - beginDate;
209}
210
211inline
213 const bdlt::Date& beginDate,
214 const bdlt::Date& endDate,
215 const bsl::vector<bdlt::Date>& periodDate,
216 double periodYearDiff)
217{
218 return yearsDiff(beginDate,
219 endDate,
220 periodDate.data(),
221 periodDate.data() + periodDate.size(),
222 periodYearDiff);
223}
224
225inline
227 const bdlt::Date& beginDate,
228 const bdlt::Date& endDate,
229 const std::vector<bdlt::Date>& periodDate,
230 double periodYearDiff)
231{
232 // Some implmentations of 'std::vector', notably Aix and Solaris, do not
233 // provide the 'data' accessor.
234
235 const bdlt::Date *begin = periodDate.empty() ? 0 : &*periodDate.begin();
236 const bdlt::Date *end = begin + periodDate.size();
237
238 return yearsDiff(beginDate,
239 endDate,
240 begin,
241 end,
242 periodYearDiff);
243}
244
245#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
246inline
248 const bdlt::Date& beginDate,
249 const bdlt::Date& endDate,
250 const std::pmr::vector<bdlt::Date>& periodDate,
251 double periodYearDiff)
252{
253 return yearsDiff(beginDate,
254 endDate,
255 periodDate.data(),
256 periodDate.data() + periodDate.size(),
257 periodYearDiff);
258}
259#endif
260
261} // close package namespace
262
263
264#endif
265
266// ----------------------------------------------------------------------------
267// Copyright 2015 Bloomberg Finance L.P.
268//
269// Licensed under the Apache License, Version 2.0 (the "License");
270// you may not use this file except in compliance with the License.
271// You may obtain a copy of the License at
272//
273// http://www.apache.org/licenses/LICENSE-2.0
274//
275// Unless required by applicable law or agreed to in writing, software
276// distributed under the License is distributed on an "AS IS" BASIS,
277// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
278// See the License for the specific language governing permissions and
279// limitations under the License.
280// ----------------------------- END-OF-FILE ----------------------------------
281
282/** @} */
283/** @} */
284/** @} */
Definition bdlt_date.h:294
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:3019
VALUE_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2942
Definition bslstl_vector.h:1120
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicactual360.h:107
Definition bbldc_periodicmaactualactual.h:124
static int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate)
Definition bbldc_periodicmaactualactual.h:205
static double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate, const bdlt::Date *periodDateBegin, const bdlt::Date *periodDateEnd, double periodYearDiff)