1 ## Copyright (C) 1995-1998, 2000, 2002, 2005-2007 Friedrich Leisch <Friedrich.Leisch@ci.tuwien.ac.at>
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{volat} =} vol (@var{x}, @var{m}, @var{n})
18 ## Return the volatility @var{volat} of each column of the input matrix @var{x}.
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.
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}.
30 function retval = vol (X, m, n)
40 error ("vol: I need more data!");
45 error ("vol: I need more data!");
49 U = zeros (xr - 1, xc);
52 U = X ((2 : xr), :) ./ X((1 : (xr-1)), :);
54 error ("vol: zero element in X");
58 U = U - ones (xr - 1, 1) * sum (U) / (xr - 1);
60 retval = zeros (xr - n * m, xc);
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;
68 retval = sqrt (retval * m / (n * m - 1));