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{cfConv} =} cfconv (@var{cf}, @var{yield})
18 ## Calculate convexity @var{cfConv} from given fixed-paid cash flow @var{cf} and
19 ## period yield @var{yield}.
23 ## [1] http://thismatter.com/money/bonds/duration-convexity.htm
25 ## [2] http://en.wikipedia.org/wiki/Bond_convexity
30 function [cfConv] = cfconv (cf, yield)
34 elseif ( ! isscalar(yield) )
35 error("yield: must be scalar");
36 elseif ( rows(cf) != 1 )
37 error("Cash Flow: must be 1xN");
40 v_idx = 1:columns(cf);
41 t1 = (1+yield) .^ (-v_idx);
42 t2 = ((v_idx .^ 2) + v_idx) .* t1;
44 cfConv = (cf*t2') / (cf*t1') / (1+yield) / (1+yield);
49 %! cf = [2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 102.5];
51 %! cfConv = cfconv( cf, yield )
52 %! %--------------------------------------------------
53 %! % Input cash flow and yield, output convexity
56 %! cf = [2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 102.5];
57 %! cfConv = cfconv( cf, 0.025 );
58 %! errVal = round(cfConv*(1e+4))*(1e-4) - 90.4493;
59 %! errVal = round(errVal*(1e+10));