/
cashflows.hpp
450 lines (419 loc) · 19.2 KB
/
cashflows.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2005, 2006 StatPro Italia srl
Copyright (C) 2005 Charles Whitmore
Copyright (C) 2007, 2008, 2009, 2010, 2011 Ferdinando Ametrano
Copyright (C) 2008 Toyin Akin
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file cashflows.hpp
\brief Cash-flow analysis functions
*/
#ifndef quantlib_cashflows_hpp
#define quantlib_cashflows_hpp
#include <ql/cashflows/duration.hpp>
#include <ql/cashflow.hpp>
#include <ql/interestrate.hpp>
#include <ql/shared_ptr.hpp>
namespace QuantLib {
class YieldTermStructure;
//! %cashflow-analysis functions
/*! \todo add tests */
class CashFlows {
private:
CashFlows() = delete;
CashFlows(const CashFlows&) = delete;
class IrrFinder {
public:
IrrFinder(const Leg& leg,
Real npv,
const DayCounter& dayCounter,
Compounding comp,
Frequency freq,
bool includeSettlementDateFlows,
Date settlementDate,
Date npvDate);
Real operator()(Rate y) const;
Real derivative(Rate y) const;
private:
void checkSign() const;
const Leg& leg_;
Real npv_;
DayCounter dayCounter_;
Compounding compounding_;
Frequency frequency_;
bool includeSettlementDateFlows_;
Date settlementDate_, npvDate_;
};
public:
//! \name Date functions
//@{
static Date startDate(const Leg& leg);
static Date maturityDate(const Leg& leg);
static bool isExpired(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
//@}
//! \name CashFlow functions
//@{
//! the last cashflow paying before or at the given date
static Leg::const_reverse_iterator
previousCashFlow(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
//! the first cashflow paying after the given date
static Leg::const_iterator
nextCashFlow(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Date
previousCashFlowDate(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Date
nextCashFlowDate(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Real
previousCashFlowAmount(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Real
nextCashFlowAmount(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
//@}
//! \name Coupon inspectors
//@{
static Rate
previousCouponRate(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Rate
nextCouponRate(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Real
nominal(const Leg& leg,
bool includeSettlementDateFlows,
Date settlDate = Date());
static Date
accrualStartDate(const Leg& leg,
bool includeSettlementDateFlows,
Date settlDate = Date());
static Date
accrualEndDate(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Date
referencePeriodStart(const Leg& leg,
bool includeSettlementDateFlows,
Date settlDate = Date());
static Date
referencePeriodEnd(const Leg& leg,
bool includeSettlementDateFlows,
Date settlDate = Date());
static Time
accrualPeriod(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Date::serial_type
accrualDays(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Time
accruedPeriod(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Date::serial_type
accruedDays(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
static Real
accruedAmount(const Leg& leg,
bool includeSettlementDateFlows,
Date settlementDate = Date());
//@}
//! \name YieldTermStructure functions
//@{
//! NPV of the cash flows.
/*! The NPV is the sum of the cash flows, each discounted
according to the given term structure.
*/
static Real npv(const Leg& leg,
const YieldTermStructure& discountCurve,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! Basis-point sensitivity of the cash flows.
/*! The result is the change in NPV due to a uniform
1-basis-point change in the rate paid by the cash
flows. The change for each coupon is discounted according
to the given term structure.
*/
static Real bps(const Leg& leg,
const YieldTermStructure& discountCurve,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//@{
//! NPV and BPS of the cash flows.
/*! The NPV and BPS of the cash flows calculated
together for performance reason
*/
static void npvbps(const Leg& leg,
const YieldTermStructure& discountCurve,
bool includeSettlementDateFlows,
Date settlementDate,
Date npvDate,
Real& npv,
Real& bps);
//! At-the-money rate of the cash flows.
/*! The result is the fixed rate for which a fixed rate cash flow
vector, equivalent to the input vector, has the required NPV
according to the given term structure. If the required NPV is
not given, the input cash flow vector's NPV is used instead.
*/
static Rate atmRate(const Leg& leg,
const YieldTermStructure& discountCurve,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date(),
Real npv = Null<Real>());
//@}
//! \name Yield (a.k.a. Internal Rate of Return, i.e. IRR) functions
/*! The IRR is the interest rate at which the NPV of the cash
flows equals the dirty price.
*/
//@{
//! NPV of the cash flows.
/*! The NPV is the sum of the cash flows, each discounted
according to the given constant interest rate. The result
is affected by the choice of the interest-rate compounding
and the relative frequency and day counter.
*/
static Real npv(const Leg& leg,
const InterestRate& yield,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
static Real npv(const Leg& leg,
Rate yield,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! Basis-point sensitivity of the cash flows.
/*! The result is the change in NPV due to a uniform
1-basis-point change in the rate paid by the cash
flows. The change for each coupon is discounted according
to the given constant interest rate. The result is
affected by the choice of the interest-rate compounding
and the relative frequency and day counter.
*/
static Real bps(const Leg& leg,
const InterestRate& yield,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
static Real bps(const Leg& leg,
Rate yield,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! Implied internal rate of return.
/*! The function verifies
the theoretical existence of an IRR and numerically
establishes the IRR to the desired precision.
*/
static Rate yield(const Leg& leg,
Real npv,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date(),
Real accuracy = 1.0e-10,
Size maxIterations = 100,
Rate guess = 0.05);
template <typename Solver>
static Rate yield(const Solver& solver,
const Leg& leg,
Real npv,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date(),
Real accuracy = 1.0e-10,
Rate guess = 0.05) {
IrrFinder objFunction(leg, npv, dayCounter, compounding,
frequency, includeSettlementDateFlows,
settlementDate, npvDate);
return solver.solve(objFunction, accuracy, guess, guess/10.0);
}
//! Cash-flow duration.
/*! The simple duration of a string of cash flows is defined as
\f[
D_{\mathrm{simple}} = \frac{\sum t_i c_i B(t_i)}{\sum c_i B(t_i)}
\f]
where \f$ c_i \f$ is the amount of the \f$ i \f$-th cash
flow, \f$ t_i \f$ is its payment time, and \f$ B(t_i) \f$
is the corresponding discount according to the passed yield.
The modified duration is defined as
\f[
D_{\mathrm{modified}} = -\frac{1}{P} \frac{\partial P}{\partial y}
\f]
where \f$ P \f$ is the present value of the cash flows
according to the given IRR \f$ y \f$.
The Macaulay duration is defined for a compounded IRR as
\f[
D_{\mathrm{Macaulay}} = \left( 1 + \frac{y}{N} \right)
D_{\mathrm{modified}}
\f]
where \f$ y \f$ is the IRR and \f$ N \f$ is the number of
cash flows per year.
*/
static Time duration(const Leg& leg,
const InterestRate& yield,
Duration::Type type,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
static Time duration(const Leg& leg,
Rate yield,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
Duration::Type type,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! Cash-flow convexity
/*! The convexity of a string of cash flows is defined as
\f[
C = \frac{1}{P} \frac{\partial^2 P}{\partial y^2}
\f]
where \f$ P \f$ is the present value of the cash flows
according to the given IRR \f$ y \f$.
*/
static Real convexity(const Leg& leg,
const InterestRate& yield,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
static Real convexity(const Leg& leg,
Rate yield,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! Basis-point value
/*! Obtained by setting dy = 0.0001 in the 2nd-order Taylor
series expansion.
*/
static Real basisPointValue(const Leg& leg,
const InterestRate& yield,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
static Real basisPointValue(const Leg& leg,
Rate yield,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! Yield value of a basis point
/*! The yield value of a one basis point change in price is
the derivative of the yield with respect to the price
multiplied by 0.01
*/
static Real yieldValueBasisPoint(const Leg& leg,
const InterestRate& yield,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
static Real yieldValueBasisPoint(const Leg& leg,
Rate yield,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//@}
//! \name Z-spread functions
/*! For details on z-spread refer to:
"Credit Spreads Explained", Lehman Brothers European Fixed
Income Research - March 2004, D. O'Kane
*/
//@{
//! NPV of the cash flows.
/*! The NPV is the sum of the cash flows, each discounted
according to the z-spreaded term structure. The result
is affected by the choice of the z-spread compounding
and the relative frequency and day counter.
*/
static Real npv(const Leg& leg,
const ext::shared_ptr<YieldTermStructure>& discount,
Spread zSpread,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date());
//! implied Z-spread.
static Spread zSpread(const Leg& leg,
Real npv,
const ext::shared_ptr<YieldTermStructure>&,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date(),
Real accuracy = 1.0e-10,
Size maxIterations = 100,
Rate guess = 0.0);
//! deprecated implied Z-spread.
static Spread zSpread(const Leg& leg,
const ext::shared_ptr<YieldTermStructure>& d,
Real npv,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
bool includeSettlementDateFlows,
Date settlementDate = Date(),
Date npvDate = Date(),
Real accuracy = 1.0e-10,
Size maxIterations = 100,
Rate guess = 0.0) {
return zSpread(leg, npv, d, dayCounter, compounding, frequency,
includeSettlementDateFlows, settlementDate, npvDate,
accuracy, maxIterations, guess);
}
//@}
};
}
#endif