1 ## Copyright (C) 2011 Hong Yu <hyu0401@hotmail.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {[@var{dur}, @var{mod_dur}] =} cfdur (@var{cf}, @var{yield})
18 ## Calculate duration @var{dur} and modified duration @var{mod_dur}, from given
19 ## fixed-paid cash flow @var{cf} and period yield @var{yield}.
22 ## http://en.wikipedia.org/wiki/Bond_duration
23 ## Using periodic compounding instead of continuous compounding.
28 function [dur, modDur] = cfdur (cf, yield)
32 elseif ( ! isscalar(yield) )
33 error("input yield must be a scalar");
37 error("input cash flow must be a 1xN matrix");
40 v_idx = 1:columns(cf);
41 t1 = (1+yield) .^ (-v_idx);
44 dur = (cf*t2') / (cf*t1');
45 modDur = dur / (1+yield);
50 %! cf = [2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 102.5];
52 %! [ duration, modDuration ] = cfdur( cf, yield )
53 %! %--------------------------------------------------
54 %! % Input cash flow and yield, output duration and modified duration
57 %! cf = [2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 102.5];
58 %! [dur modDur] = cfdur( cf, 0.025 );
59 %! errVal1 = round(dur*(1e+4))*(1e-4) - 8.9709;
60 %! errVal2 = round(modDur*(1e+4))*(1e-4) - 8.7521;
61 %! assert( errVal1, 0 )
62 %! assert( errVal2, 0 )