changeset 5622:dba495427d5e octave-forge

NaN: weightening of data; fix support for FreeMat v3.6
author schloegl
date Tue, 12 May 2009 11:32:58 +0000
parents 84de7b58ec0a
children 852ca6a071c7
files extra/NaN/doc/README.TXT extra/NaN/inst/flag_implicit_skip_nan.m extra/NaN/inst/rms.m extra/NaN/inst/sem.m extra/NaN/inst/std.m extra/NaN/inst/var.m
diffstat 6 files changed, 90 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/extra/NaN/doc/README.TXT	Tue May 12 10:55:57 2009 +0000
+++ b/extra/NaN/doc/README.TXT	Tue May 12 11:32:58 2009 +0000
@@ -5,16 +5,16 @@
 
 FEATURES of the NaN-tb:
 -----------------------
- - implements statistical tools
+ - statistical toolbox
  - NaN's are treated as missing values 
+ - supports DIM argument 
+ - supports unbiased estimation 
+ - supports weightening of data   
  - less but more powerful functions (no nan-FUN needed)
  - fixes known bugs 
  - compatible to Matlab and Octave
  - easy to use
- - supports DIM argument 
- - supports unbiased estimation; 
- - The toolbox was tested with
-   Matlab 5.2, 5.3, 6.1, 6.5, 7.0, 7.6 and Octave 2.1.x, 2.9.x, 3.0.1, 3.1.51+
+ - The toolbox is tested with Octave 3.x and Matlab 7.x, FreeMat v3.6
  
   
 Currently are implemented:
@@ -144,7 +144,7 @@
 Permits to implement useful modifications. 
 
 12) NORMPDF, NORMCDF, NORMINV
-In the Matlab statistics toolbox V 3.0, NORMPDF, NORMCDF and NORMINV give 
+In the Matlab statistics toolbox V 3.0, NORMPDF, NORMCDF and NORMINV gave 
 incorrect results for SIGMA=0; A Similar problem was observed in Octave 
 with NORMAL_INV, NORMAL_PDF, and NORMALCDF. 
 
@@ -237,7 +237,7 @@
 LICENSE:
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
+    the Free Software Foundation; either version 3 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
--- a/extra/NaN/inst/flag_implicit_skip_nan.m	Tue May 12 10:55:57 2009 +0000
+++ b/extra/NaN/inst/flag_implicit_skip_nan.m	Tue May 12 11:32:58 2009 +0000
@@ -40,7 +40,7 @@
 %    along with this program; if not, write to the Free Software
 %    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-%	$Id: flag_implicit_skip_nan.m,v 1.8 2003/11/05 10:40:21 schloegl dead $
+%	$Id$
 % 	Copyright (C) 2001-2003,2009 by Alois Schloegl <a.schloegl@ieee.org>	
 %       This function is part of the NaN-toolbox
 %       http://hci.tu-graz.ac.at/~schloegl/matlab/NaN/
@@ -48,6 +48,8 @@
 
 global FLAG_implicit_skip_nan; 
 
+if strcmp(version,'3.6'), FLAG_implicit_skip_nan=(1==1); end;	%% hack for the use with Freemat3.6
+
 %%% set DEFAULT value of FLAG
 if isempty(FLAG_implicit_skip_nan),
 	FLAG_implicit_skip_nan = (1==1); %logical(1); % logical.m not available on 2.0.16
@@ -59,5 +61,5 @@
 	if (~i)
 		warning('flag_implicit_skipnan(0): You are warned!!! You have turned off skipping NaN in sumskipnan. This is not recommended. Make sure you really know what you do.')
 	end;
-end;    
+end;
 
--- a/extra/NaN/inst/rms.m	Tue May 12 10:55:57 2009 +0000
+++ b/extra/NaN/inst/rms.m	Tue May 12 11:32:58 2009 +0000
@@ -1,17 +1,23 @@
-function o=rms(x,DIM)
+function o=rms(x,DIM,W)
 % RMS calculates the root mean square
 %   can deal with complex data. 
 %
-% y = rms(x,DIM)
+% y = rms(x,DIM,W)
 %
 % DIM	dimension
 %	1 STD of columns
 %	2 STD of rows
 % 	N STD of  N-th dimension 
 %	default or []: first DIMENSION, with more than 1 element
+% W	weights to compute weighted s.d. (default: [])
+%	if W=[], all weights are 1. 
+%	number of elements in W must match size(x,DIM) 
+%
+% y	estimated standard deviation
 %
 % features:
 % - can deal with NaN's (missing values)
+% - weighting of data 
 % - dimension argument also in Octave
 % - compatible to Matlab and Octave
 %
@@ -33,14 +39,16 @@
 
 
 %	$Id$
-%	Copyright (C) 2000-2003,2008 by Alois Schloegl <a.schloegl@ieee.org>	
%       This function is part of the NaN-toolbox
+%	Copyright (C) 2000-2003,2008,2009 by Alois Schloegl <a.schloegl@ieee.org>	
%       This function is part of the NaN-toolbox
 %       http://www.dpmi.tu-graz.ac.at/~schloegl/matlab/NaN/
 
 
 if nargin<2,
 	[o,N,ssq] = sumskipnan(x);
+elseif nargin<3
+	[o,N,ssq] = sumskipnan(x,DIM);
 else
-	[o,N,ssq] = sumskipnan(x,DIM);
+	[o,N,ssq] = sumskipnan(x,DIM,W);
 end;
 
 o = sqrt(ssq./N);
--- a/extra/NaN/inst/sem.m	Tue May 12 10:55:57 2009 +0000
+++ b/extra/NaN/inst/sem.m	Tue May 12 11:32:58 2009 +0000
@@ -1,7 +1,7 @@
-function [SE,M]=sem(x,DIM)
+function [SE,M]=sem(x,DIM, W)
 % SEM calculates the standard error of the mean
 % 
-% [SE,M] = SEM(x [, DIM])
+% [SE,M] = SEM(x [, DIM [,W]])
 %   calculates the standard error (SE) in dimension DIM
 %   the default DIM is the first non-single dimension
 %   M returns the mean. 
@@ -12,9 +12,13 @@
 %	2: SEM of rows
 % 	N: SEM of  N-th dimension 
 %	default or []: first DIMENSION, with more than 1 element
+% W	weights to compute weighted mean and s.d. (default: [])
+%	if W=[], all weights are 1. 
+%	number of elements in W must match size(x,DIM) 
 %
 % features:
 % - can deal with NaN's (missing values)
+% - weighting of data 
 % - dimension argument 
 % - compatible to Matlab and Octave
 %
@@ -22,7 +26,7 @@
 
 %    This program is free software; you can redistribute it and/or modify
 %    it under the terms of the GNU General Public License as published by
-%    the Free Software Foundation; either version 2 of the License, or
+%    the Free Software Foundation; either version 3 of the License, or
 %    (at your option) any later version.
 %
 %    This program is distributed in the hope that it will be useful,
@@ -33,12 +37,15 @@
 %    You should have received a copy of the GNU General Public License
 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
 
-%	Copyright (C) 2000-2003 by Alois Schloegl <a.schloegl@ieee.org>	
-%	$Revision$
+%	Copyright (C) 2000-2003,2008,2009 by Alois Schloegl <a.schloegl@ieee.org>	
 %	$Id$
+%       This function is part of the NaN-toolbox
+%       http://www.dpmi.tu-graz.ac.at/~schloegl/matlab/NaN/
 
 
-if nargin>1,
+if nargin>2,
+	[S,N,SSQ] = sumskipnan(x,DIM,W);
+elseif nargin>1,
 	[S,N,SSQ] = sumskipnan(x,DIM);
 else    
 	[S,N,SSQ] = sumskipnan(x);
--- a/extra/NaN/inst/std.m	Tue May 12 10:55:57 2009 +0000
+++ b/extra/NaN/inst/std.m	Tue May 12 11:32:58 2009 +0000
@@ -1,7 +1,7 @@
-function [o,v]=std(x,opt,DIM)
+function [o,v]=std(x,opt,DIM,W)
 % STD calculates the standard deviation.
 % 
-% [y,v] = std(x [, opt[, DIM]])
+% [y,v] = std(x [, opt[, DIM [, W]]])
 % 
 % opt   option 
 %	0:  normalizes with N-1 [default]
@@ -14,12 +14,16 @@
 % DIM	dimension
 % 	N STD of  N-th dimension 
 %	default or []: first DIMENSION, with more than 1 element
+% W	weights to compute weighted s.d. (default: [])
+%	if W=[], all weights are 1. 
+%	number of elements in W must match size(x,DIM) 
 %
 % y	estimated standard deviation
 %
 % features:
 % - provides an unbiased estimation of the S.D. 
 % - can deal with NaN's (missing values)
+% - weighting of data 
 % - dimension argument also in Octave
 % - compatible to Matlab and Octave
 %
@@ -32,7 +36,7 @@
 
 %    This program is free software; you can redistribute it and/or modify
 %    it under the terms of the GNU General Public License as published by
-%    the Free Software Foundation; either version 2 of the License, or
+%    the Free Software Foundation; either version 3 of the License, or
 %    (at your option) any later version.
 %
 %    This program is distributed in the hope that it will be useful,
@@ -44,10 +48,13 @@
 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
 
 %	$Id$
-%	Copyright (C) 2000-2003, 2006,2009 by Alois Schloegl <a.schloegl@ieee.org>	
+%	Copyright (C) 2000-2003,2006,2009 by Alois Schloegl <a.schloegl@ieee.org>	
 %       This is part of the NaN-toolbox for Octave and Matlab 
 %       see also: http://hci.tugraz.at/schloegl/matlab/NaN/       
 
+if nargin<4,
+	W = []; 
+end;
 if nargin<3,
 	DIM = []; 
 end;
@@ -57,12 +64,12 @@
 end;
 
 
-[y,n,ssq] = sumskipnan(x,DIM);
if all(ssq(:).*n(:) > 2*(y(:).^2))
+[y,n,ssq] = sumskipnan(x,DIM,W);
if all(ssq(:).*n(:) > 2*(y(:).^2))
 	%% rounding error is neglectable 
 	y = ssq - y.*y./n;
 else
 	%% rounding error is not neglectable 
-	[y,n] = sumskipnan(center(x,DIM).^2,DIM);
+	[y,n] = sumskipnan(center(x,DIM).^2,DIM,W);
 end; 
 
 
--- a/extra/NaN/inst/var.m	Tue May 12 10:55:57 2009 +0000
+++ b/extra/NaN/inst/var.m	Tue May 12 11:32:58 2009 +0000
@@ -1,20 +1,31 @@
-function y=var(x,opt,DIM)
+function y=var(x,opt,DIM,W)
 % VAR calculates the variance.
 % 
 % y = var(x [, opt[, DIM]])
 %   calculates the variance in dimension DIM
 %   the default DIM is the first non-single dimension
 %
-% opt   0: normalizes with N-1 [default}
+% opt   0: normalizes with N-1 [default]
 %	1: normalizes with N 
 % DIM	dimension
 %	1: VAR of columns
 %	2: VAR of rows
 % 	N: VAR of  N-th dimension 
-%	default or []: first DIMENSION, with more than 1 element
+%	default or []: first DIMENSION, with more than 1 element
+% W	weights to compute weighted variance (default: [])
+%	if W=[], all weights are 1. 
+%	number of elements in W must match size(x,DIM) 
+% 
+% usage: 
+%	var(x)	
+%	var(x, opt, DIM)	
+%	var(x, [], DIM)	
+%	var(x, W, DIM)
+%	var(x, opt, DIM, W)	
 %
 % features:
 % - can deal with NaN's (missing values)
+% - weighting of data 
 % - dimension argument 
 % - compatible to Matlab and Octave
 %
@@ -22,7 +33,7 @@
 
 %    This program is free software; you can redistribute it and/or modify
 %    it under the terms of the GNU General Public License as published by
-%    the Free Software Foundation; either version 2 of the License, or
+%    the Free Software Foundation; either version 3 of the License, or
 %    (at your option) any later version.
 %
 %    This program is distributed in the hope that it will be useful,
@@ -37,29 +48,41 @@
 %	Copyright (C) 2000-2003,2006,2009 by Alois Schloegl <a.schloegl@ieee.org>
 %       This is part of the NaN-toolbox for Octave and Matlab 
 %       see also: http://hci.tugraz.at/schloegl/matlab/NaN/       
-
-if nargin>1,
-        if ~isempty(opt) & opt~=0, 
-                fprintf(2,'Warning STD: OPTION not supported.\n');
-        end;
-else 
-        opt = 0; 
-end;
-
-if nargin<3,
-	DIM = []; 
-end;
+
+if nargin<3,
+	DIM = []; 
+end; 
+
+if nargin==1,
+	W = [];
+	opt = [];
+
+elseif any(nargin==[2,3]) 
+	if (numel(opt)<2),
+		W = [];
+	else
+		W = opt;
+		opt = []; 
+	end;
+elseif (nargin==4) && (numel(opt)<2) && (numel(DIM)<2),
+	;
+else 
+	fprintf(1,'Error VAR: incorrect usage\n');	
+	help var; 
+	return;
+end; 	
+
 if isempty(DIM), 
         DIM=min(find(size(x)>1));
         if isempty(DIM), DIM=1; end;
 end;
 
-[y,n,ssq] = sumskipnan(x,DIM);
if all(ssq(:).*n(:) > 2*(y(:).^2))
+[y,n,ssq] = sumskipnan(x,DIM,W);
if all(ssq(:).*n(:) > 2*(y(:).^2))
 	%% rounding error is neglectable 
 	y = ssq - y.*y./n;
 else
 	%% rounding error is not neglectable 
-	[y,n] = sumskipnan(center(x,DIM).^2,DIM);
+	[y,n] = sumskipnan(center(x,DIM).^2,DIM,W);
 end; 
 
 if (opt~=1)