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 |
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 |
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3453
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} var (@var{x}) |
3200
|
21 ## For vector arguments, return the (real) variance of the values. |
7001
|
22 ## For matrix arguments, return a row vector containing the variance for |
3200
|
23 ## each column. |
4849
|
24 ## |
5435
|
25 ## The argument @var{opt} determines the type of normalization to use. |
|
26 ## Valid values are |
4849
|
27 ## |
|
28 ## @table @asis |
|
29 ## @item 0: |
6754
|
30 ## Normalizes with @math{N-1}, provides the best unbiased estimator of the |
5435
|
31 ## variance [default]. |
4849
|
32 ## @item 1: |
6754
|
33 ## Normalizes with @math{N}, this provides the second moment around the mean. |
4849
|
34 ## @end table |
|
35 ## |
|
36 ## The third argument @var{dim} determines the dimension along which the |
|
37 ## variance is calculated. |
3453
|
38 ## @end deftypefn |
3426
|
39 |
5428
|
40 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
41 ## Description: Compute variance |
3200
|
42 |
4849
|
43 function y = var (x, opt, dim) |
3426
|
44 |
4849
|
45 if (nargin < 1 || nargin > 3) |
6046
|
46 print_usage (); |
4849
|
47 endif |
|
48 if (nargin < 3) |
6024
|
49 dim = find (size (x) > 1, 1); |
4849
|
50 if (isempty (dim)) |
|
51 dim = 1; |
|
52 endif |
|
53 endif |
|
54 if (nargin < 2 || isempty (opt)) |
|
55 opt = 0; |
3200
|
56 endif |
3426
|
57 |
4849
|
58 sz = size (x); |
|
59 if (prod (sz) < 1) |
3456
|
60 error ("var: x must not be empty"); |
4849
|
61 elseif (sz(dim) == 1) |
3200
|
62 y = 0; |
|
63 else |
4849
|
64 rng = ones (1, length (sz)); |
|
65 rng (dim) = sz (dim); |
|
66 if (opt == 0) |
|
67 y = sumsq (x - repmat(mean (x, dim), rng), dim) / (sz(dim) - 1); |
|
68 else |
|
69 y = sumsq (x - repmat(mean (x, dim), rng), dim) / sz(dim); |
|
70 endif |
3200
|
71 endif |
3426
|
72 |
3200
|
73 endfunction |