X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Ffinancial-0.4.0%2Fvol.m;fp=octave_packages%2Ffinancial-0.4.0%2Fvol.m;h=0444ed50dc5d584d558248c1597adab78fac882d;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/financial-0.4.0/vol.m b/octave_packages/financial-0.4.0/vol.m new file mode 100644 index 0000000..0444ed5 --- /dev/null +++ b/octave_packages/financial-0.4.0/vol.m @@ -0,0 +1,70 @@ +## Copyright (C) 1995-1998, 2000, 2002, 2005-2007 Friedrich Leisch +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{volat} =} vol (@var{x}, @var{m}, @var{n}) +## Return the volatility @var{volat} of each column of the input matrix @var{x}. +## +## The number of data sets per period is given by @var{m} (e.g. the +## number of data per year if you want to compute the volatility per +## year). The optional parameter @var{n} gives the number of past +## periods used for computation, if it is omitted, a value of 1 is used. +## +## If @var{t} is the number of rows of @var{x}, @code{vol} returns the +## volatility from @code{n*m} to @var{t}. +## +## @end deftypefn + +function retval = vol (X, m, n) + + if (nargin < 2) + print_usage (); + endif + + [xr, xc] = size (X); + + if (nargin > 2) + if (n * m > xr) + error ("vol: I need more data!"); + endif + else + n = 1; + if (n * m > xr) + error ("vol: I need more data!"); + endif + endif + + U = zeros (xr - 1, xc); + + if (all (X)) + U = X ((2 : xr), :) ./ X((1 : (xr-1)), :); + else + error ("vol: zero element in X"); + endif + + U = log(U); + U = U - ones (xr - 1, 1) * sum (U) / (xr - 1); + + retval = zeros (xr - n * m, xc); + + retval(1, :) = sumsq (U((1 : n*m), :)); + for i = 2 : (xr - n * m) + retval(i, :) = retval(i - 1, :) ... + - U(i - 1, :).^2 + U(i + n * m - 1, :).^2; + endfor + + retval = sqrt (retval * m / (n * m - 1)); + +endfunction