]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/nanvar.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / nanvar.m
1 # Copyright (C) 2008 Sylvain Pelissier <sylvain.pelissier@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} nanvar (@var{x})
18 ## @deftypefnx{Function File} {@var{v} =} nanvar (@var{X}, @var{opt})
19 ## @deftypefnx{Function File} {@var{v} =} nanvar (@var{X}, @var{opt}, @var{dim})
20 ## Compute the variance while ignoring NaN values.
21 ##
22 ## For vector arguments, return the (real) variance of the values.
23 ## For matrix arguments, return a row vector containing the variance for
24 ## each column.
25 ##
26 ## The argument @var{opt} determines the type of normalization to use.
27 ## Valid values are
28 ##
29 ## @table @asis 
30 ## @item 0:
31 ## Normalizes with @math{N-1}, provides the best unbiased estimator of the
32 ## variance [default].
33 ## @item 1:
34 ## Normalizes with @math{N}, this provides the second moment around the mean.
35 ## @end table
36 ##
37 ## The third argument @var{dim} determines the dimension along which the 
38 ## variance is calculated.
39 ##
40 ## @seealso{var, nanmean, nanstd, nanmax, nanmin}
41 ## @end deftypefn
42
43 function y = nanvar(x,w,dim)
44   if nargin < 1
45     print_usage ();
46   else
47     if ((nargin < 2) || isempty(w))
48       w = 0;
49     endif
50     
51     if nargin < 3
52       dim = min(find(size(x)>1));
53       if isempty(dim)
54         dim=1;
55       endif
56     endif
57     
58     y = nanstd(x,w,dim).^2;
59   endif
60 endfunction
61
62 ## Tests
63 %!shared x
64 %! x = [1 2 nan 3 4 5];
65 %!assert (nanvar (x), var (x(! isnan (x))), 10*eps)