home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / finance / vol.m < prev   
Text File  |  1999-04-29  |  2KB  |  71 lines

  1. ## Copyright (C) 1995, 1996, 1997  Friedrich Leisch
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  vol (X, m [, n])
  18. ##
  19. ## vol returns the volatility of each column of the input matrix X. m is
  20. ## the number of data sets per period (e.g. the number of data per year
  21. ## if you want to compute the volatility per year). The optional
  22. ## parameter n gives the number of past periods used for computation, if
  23. ## n is omitted, n=1 is used. If T is the number of rows of X, vol
  24. ## returns the volatility from n*m to T.
  25.  
  26. ## Author:  FL <Friedrich.Leisch@ci.tuwien.ac.at>
  27. ## Description:  Volatility of financial time series data
  28.  
  29. function retval = vol (X, m, n)
  30.  
  31.   if (nargin < 2)
  32.     usage ("vol (X, m [, n])");
  33.   endif
  34.  
  35.   [xr, xc] = size (X);
  36.  
  37.   if (nargin > 2)
  38.     if (n * m > xr)
  39.       error ("vol:  I need more data!");
  40.     endif
  41.   else
  42.     n = 1;
  43.     if (n * m > xr)
  44.       error ("vol:  I need more data!");
  45.     endif
  46.   endif
  47.  
  48.   U = zeros (xr - 1, xc);
  49.  
  50.   if all (X)
  51.     U = X ((2 : xr), :) ./ X((1 : (xr-1)), :);
  52.   else
  53.     error ("vol:  zero element in X");
  54.   endif
  55.  
  56.   U = log(U);
  57.   U = U - ones (xr - 1, 1) * sum (U) / (xr - 1);
  58.  
  59.   retval = zeros (xr - n * m, xc);
  60.  
  61.   retval(1, :) = sumsq (U((1 : n*m), :));
  62.   for i = 2 : (xr - n * m)
  63.     retval(i, :) = retval(i - 1, :) ...
  64.     - U(i - 1, :).^2 + U(i + n * m - 1, :).^2;
  65.   endfor
  66.  
  67.   retval = sqrt (retval * m / (n * m - 1));
  68.  
  69. endfunction
  70.  
  71.