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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, 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. |
|
23 ## For matrix arguments, return a row vector contaning the variance for |
|
24 ## each column. |
4849
|
25 ## |
|
26 ## The argument @var{opt} determines the type of normalization to use. Valid |
|
27 ## values are |
|
28 ## |
|
29 ## @table @asis |
|
30 ## @item 0: |
|
31 ## normalizes with N-1, provides the square root of best unbiased estimator |
|
32 ## of the variance [default] |
|
33 ## @item 1: |
|
34 ## normalizes with N, this provides the square root of the second moment |
|
35 ## around the mean |
|
36 ## @end table |
|
37 ## |
|
38 ## The third argument @var{dim} determines the dimension along which the |
|
39 ## variance is calculated. |
3453
|
40 ## @end deftypefn |
3426
|
41 |
3456
|
42 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
43 ## Description: Compute variance |
3200
|
44 |
4849
|
45 function y = var (x, opt, dim) |
3426
|
46 |
4849
|
47 if (nargin < 1 || nargin > 3) |
|
48 usage ("var (x, opt, sim)"); |
|
49 endif |
|
50 if (nargin < 3) |
|
51 dim = min (find (size (x) > 1)); |
|
52 if (isempty (dim)) |
|
53 dim = 1; |
|
54 endif |
|
55 endif |
|
56 if (nargin < 2 || isempty (opt)) |
|
57 opt = 0; |
3200
|
58 endif |
3426
|
59 |
4849
|
60 sz = size (x); |
|
61 if (prod (sz) < 1) |
3456
|
62 error ("var: x must not be empty"); |
4849
|
63 elseif (sz(dim) == 1) |
3200
|
64 y = 0; |
|
65 else |
4849
|
66 rng = ones (1, length (sz)); |
|
67 rng (dim) = sz (dim); |
|
68 if (opt == 0) |
|
69 y = sumsq (x - repmat(mean (x, dim), rng), dim) / (sz(dim) - 1); |
|
70 else |
|
71 y = sumsq (x - repmat(mean (x, dim), rng), dim) / sz(dim); |
|
72 endif |
3200
|
73 endif |
3426
|
74 |
3200
|
75 endfunction |