]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/cfconv.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / cfconv.m
1 ## Copyright (C) 2011 Hong Yu <hyu0401@hotmail.com>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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}.
20 ## 
21 ## Reference:
22 ##
23 ## [1] http://thismatter.com/money/bonds/duration-convexity.htm
24 ##
25 ## [2] http://en.wikipedia.org/wiki/Bond_convexity
26 ##
27 ## @seealso{cfdur}
28 ## @end deftypefn
29
30 function [cfConv] = cfconv (cf, yield)
31
32   if ( nargin != 2 )
33     print_usage ();
34   elseif ( ! isscalar(yield) )
35     error("yield: must be scalar");
36   elseif ( rows(cf) != 1 )
37     error("Cash Flow: must be 1xN");
38   endif
39
40   v_idx = 1:columns(cf);
41   t1    = (1+yield) .^ (-v_idx);
42   t2    = ((v_idx .^ 2) + v_idx) .* t1;
43
44   cfConv = (cf*t2') / (cf*t1') / (1+yield) / (1+yield);
45
46 endfunction
47
48 %!demo
49 %! cf = [2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 102.5];
50 %! yield = 0.025;
51 %! cfConv = cfconv( cf, yield )
52 %! %--------------------------------------------------
53 %! % Input cash flow and yield, output convexity
54
55 %!test
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));
60 %! assert(errVal, 0)