]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/vol.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / vol.m
1 ## Copyright (C) 1995-1998, 2000, 2002, 2005-2007 Friedrich Leisch <Friedrich.Leisch@ci.tuwien.ac.at>
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{volat} =} vol (@var{x}, @var{m}, @var{n})
18 ## Return the volatility @var{volat} of each column of the input matrix @var{x}.
19 ##
20 ## The number of data sets per period is given by @var{m} (e.g. the
21 ## number of data per year if you want to compute the volatility per
22 ## year).  The optional parameter @var{n} gives the number of past
23 ## periods used for computation, if it is omitted, a value of 1 is used.
24 ##
25 ## If @var{t} is the number of rows of @var{x}, @code{vol} returns the
26 ## volatility from @code{n*m} to @var{t}.
27 ##
28 ## @end deftypefn
29
30 function retval = vol (X, m, n)
31
32   if (nargin < 2)
33     print_usage ();
34   endif
35
36   [xr, xc] = size (X);
37
38   if (nargin > 2)
39     if (n * m > xr)
40       error ("vol: I need more data!");
41     endif
42   else
43     n = 1;
44     if (n * m > xr)
45       error ("vol: I need more data!");
46     endif
47   endif
48
49   U = zeros (xr - 1, xc);
50
51   if (all (X))
52     U = X ((2 : xr), :) ./ X((1 : (xr-1)), :);
53   else
54     error ("vol: zero element in X");
55   endif
56
57   U = log(U);
58   U = U - ones (xr - 1, 1) * sum (U) / (xr - 1);
59
60   retval = zeros (xr - n * m, xc);
61
62   retval(1, :) = sumsq (U((1 : n*m), :));
63   for i = 2 : (xr - n * m)
64     retval(i, :) = retval(i - 1, :) ...
65         - U(i - 1, :).^2 + U(i + n * m - 1, :).^2;
66   endfor
67
68   retval = sqrt (retval * m / (n * m - 1));
69
70 endfunction