Mercurial > hg > octave-nkf
annotate scripts/statistics/base/var.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | 80d499b82ff3 |
children | 56751dfc6ebd |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
9245 | 2 ## 2007, 2009 Kurt Hornik |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
3200 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
3200 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
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 |
8980 | 44 function retval = 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 |
8980 | 59 n = size (x, dim); |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 if (n == 1) |
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 retval = zeros (sz); |
8980 | 62 elseif (numel (x) > 0) |
63 retval = sumsq (center (x, dim), dim) / (n + opt - 1); | |
3200 | 64 else |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 error ("var: x must not be empty"); |
3200 | 66 endif |
3426 | 67 |
3200 | 68 endfunction |