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