3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
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 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3200
|
19 |
3453
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} var (@var{x}) |
3200
|
22 ## For vector arguments, return the (real) variance of the values. |
7001
|
23 ## For matrix arguments, return a row vector containing the variance for |
3200
|
24 ## each column. |
4849
|
25 ## |
5435
|
26 ## The argument @var{opt} determines the type of normalization to use. |
|
27 ## Valid values are |
4849
|
28 ## |
|
29 ## @table @asis |
|
30 ## @item 0: |
6754
|
31 ## Normalizes with @math{N-1}, provides the best unbiased estimator of the |
5435
|
32 ## variance [default]. |
4849
|
33 ## @item 1: |
6754
|
34 ## Normalizes with @math{N}, this provides the second moment around the mean. |
4849
|
35 ## @end table |
|
36 ## |
|
37 ## The third argument @var{dim} determines the dimension along which the |
|
38 ## variance is calculated. |
3453
|
39 ## @end deftypefn |
3426
|
40 |
5428
|
41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
42 ## Description: Compute variance |
3200
|
43 |
4849
|
44 function y = var (x, opt, dim) |
3426
|
45 |
4849
|
46 if (nargin < 1 || nargin > 3) |
6046
|
47 print_usage (); |
4849
|
48 endif |
|
49 if (nargin < 3) |
6024
|
50 dim = find (size (x) > 1, 1); |
4849
|
51 if (isempty (dim)) |
|
52 dim = 1; |
|
53 endif |
|
54 endif |
|
55 if (nargin < 2 || isempty (opt)) |
|
56 opt = 0; |
3200
|
57 endif |
3426
|
58 |
4849
|
59 sz = size (x); |
|
60 if (prod (sz) < 1) |
3456
|
61 error ("var: x must not be empty"); |
4849
|
62 elseif (sz(dim) == 1) |
3200
|
63 y = 0; |
|
64 else |
4849
|
65 rng = ones (1, length (sz)); |
|
66 rng (dim) = sz (dim); |
|
67 if (opt == 0) |
|
68 y = sumsq (x - repmat(mean (x, dim), rng), dim) / (sz(dim) - 1); |
|
69 else |
|
70 y = sumsq (x - repmat(mean (x, dim), rng), dim) / sz(dim); |
|
71 endif |
3200
|
72 endif |
3426
|
73 |
3200
|
74 endfunction |