annotate scripts/statistics/base/var.m @ 8977:f464119ec165

further simplify some stats funcs
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 13 Mar 2009 21:26:13 +0100
parents a1dbe9d80eee
children 80d499b82ff3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006,
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## 2007 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
5 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
9 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
15 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
20 ## -*- texinfo -*-
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
21 ## @deftypefn {Function File} {} var (@var{x})
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
22 ## For vector arguments, return the (real) variance of the values.
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6754
diff changeset
23 ## For matrix arguments, return a row vector containing the variance for
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
24 ## each column.
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
25 ##
5435
66ad03c58880 [project @ 2005-08-31 20:41:47 by jwe]
jwe
parents: 5428
diff changeset
26 ## The argument @var{opt} determines the type of normalization to use.
66ad03c58880 [project @ 2005-08-31 20:41:47 by jwe]
jwe
parents: 5428
diff changeset
27 ## Valid values are
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
28 ##
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
29 ## @table @asis
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
30 ## @item 0:
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
31 ## Normalizes with @math{N-1}, provides the best unbiased estimator of the
5435
66ad03c58880 [project @ 2005-08-31 20:41:47 by jwe]
jwe
parents: 5428
diff changeset
32 ## variance [default].
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
33 ## @item 1:
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
34 ## Normalizes with @math{N}, this provides the second moment around the mean.
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
35 ## @end table
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
36 ##
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
37 ## The third argument @var{dim} determines the dimension along which the
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
38 ## variance is calculated.
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
39 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
40
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
42 ## Description: Compute variance
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
43
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
44 function y = var (x, opt, dim)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
45
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
46 if (nargin < 1 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
47 print_usage ();
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
48 endif
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
49 if (nargin < 3)
6024
500d884ae373 [project @ 2006-10-03 14:27:33 by jwe]
jwe
parents: 5435
diff changeset
50 dim = find (size (x) > 1, 1);
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
51 if (isempty (dim))
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
52 dim = 1;
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
53 endif
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
54 endif
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
55 if (nargin < 2 || isempty (opt))
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
56 opt = 0;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
57 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
58
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
59 n = size (a, dim);
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);
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
62 elseif (numel (a) > 0)
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
63 retval = sumsq (center (a, dim), dim) / (n + opt - 1);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
66 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
67
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
68 endfunction