changeset 893:9875385c77f6 octave-forge

flag_implicit_skip_nan added again, it's useful for temporary turnoff of skipping NaN's
author schloegl
date Tue, 15 Apr 2003 07:56:25 +0000
parents 0e20218654fa
children 3954c7596792
files extra/NaN/INDEX extra/NaN/flag_implicit_skip_nan.m extra/NaN/median.m extra/NaN/percentile.m extra/NaN/quantile.m extra/NaN/sumskipnan.m extra/NaN/trimean.m
diffstat 7 files changed, 260 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/extra/NaN/INDEX	Fri Apr 11 13:04:24 2003 +0000
+++ b/extra/NaN/INDEX	Tue Apr 15 07:56:25 2003 +0000
@@ -4,3 +4,5 @@
  detrend kurtosis moment std mad nantest normpdf 
  normcdf norminv sumskipnan meandev rms var mean 
  sem trimean zscore spearman rankcorr ranks 
+ quantiles percentile
+ 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/NaN/flag_implicit_skip_nan.m	Tue Apr 15 07:56:25 2003 +0000
@@ -0,0 +1,61 @@
+function FLAG = flag_implicit_skip_nan(i)
+% FLAG_IMPLICIT_SKIP_NAN sets and gets default mode for handling NaNs
+%	1 skips NaN's (the default mode if no mode is set)
+% 	0 NaNs are propagated; input NaN's give NaN's at the output
+% 
+% FLAG = flag_implicit_skip_nan()
+% 	gets current mode
+%
+% flag_implicit_skip_nan(FLAG)
    % sets mode 
+%
+% prevFLAG = flag_implicit_skip_nan(nextFLAG)
+%	gets previous set FLAG and sets FLAG for the future
+% flag_implicit_skip_nan(prevFLAG)
+%	resets FLAG to previous mode
+%
+% It is used in: 
+%	SUMSKIPNAN, MEDIAN, QUANTILES, TRIMEAN
+% and affects many other functions like: 
+%	CENTER, KURTOSIS, MAD, MEAN, MOMENT, RMS, SEM, SKEWNESS, 
+%	STATISTIC, STD, VAR, ZSCORE etc. 
+%
+% The mode is stored in the global variable FLAG_implicit_skip_nan
+% It is recommended to use flag_implicit_skip_nan(1) as default and 
+% flag_implicit_skip_nan(0) should be used for exceptional cases only. 
+
+
+%    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
+%    (at your option) any later version.
+%
+%    This program is distributed in the hope that it will be useful,
+%    but WITHOUT ANY WARRANTY; without even the implied warranty of
+%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%    GNU General Public License for more details.
+%
+%    You should have received a copy of the GNU General Public License
+%    along with this program; if not, write to the Free Software
+%    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+%	$Revision$
+%	$Id$
+% 	Copyright (C) 2001-2003 by Alois Schloegl <a.schloegl@ieee.org>	
+
+
+global FLAG_implicit_skip_nan; 
+
+%%% check whether FLAG was already defined 
+if exist('FLAG_implicit_skip_nan')~=1,
+	FLAG_implicit_skip_nan = [];
+end;
+
+%%% 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
+end;
+
+FLAG = FLAG_implicit_skip_nan;
+if nargin>0,
+	FLAG_implicit_skip_nan = (i~=0); %logical(i); %logical.m not available in 2.0.16 
+end;    
--- a/extra/NaN/median.m	Fri Apr 11 13:04:24 2003 +0000
+++ b/extra/NaN/median.m	Tue Apr 15 07:56:25 2003 +0000
@@ -29,8 +29,9 @@
 %    along with this program; if not, write to the Free Software
 %    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-%	Version 1.28;	24 Oct 2002
-%	Copyright (c) 2000-2002 by  Alois Schloegl <a.schloegl@ieee.org>	
+%	$Revision$
+%	$Id$
+%	Copyright (C) 2000-2003 by  Alois Schloegl <a.schloegl@ieee.org>	
 
 % check dimension of x
 sz=size(x);
@@ -46,7 +47,11 @@
 end;
 
 % number of valid elements
-n = sumskipnan(~isnan(x),DIM);   % make it compatible to 2.0.17
+if flag_implicit_skip_nan,	% default
+	n = sumskipnan(~isnan(x),DIM);   % make it compatible to 2.0.17
+else				%  
+	n = sumskipnan(ones(size(x)),DIM);
+end;
         
 if 0; %~exist('OCTAVE_VERSION'),
         [x,ix] = sort(x,DIM); % this relays on the sort order of IEEE754 inf < nan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/NaN/percentile.m	Tue Apr 15 07:56:25 2003 +0000
@@ -0,0 +1,76 @@
+function Q=percentile(Y,q)
+% percentile calculates the quantiles of histograms or sample arrays.  
+%
+%  Q = percentile(Y,q)      
+%     returns the q-th percentile of each column of sample array Y
+%
+%  Q = percentile(HIS,q)
+%     returns the q-th percentile from the histogram HIS. 
+%     HIS must be a HISTOGRAM struct as defined in HISTO2 or HISTO3.
+%
+% If q is a vector, the each row of Q returns the q(i)-th percentile 
+%
+% see also: FLIX, HISTO2, HISTO3, QUANTILE
+
+
+%	$Revision$
+%	$Id$
+%	Copyright (C) 1996-2003 by Alois Schloegl
+%	e-mail: a.schloegl@ieee.org	
+
+
+if nargin<2,
+	help percentile
+        
+else
+        SW = isstruct(Y);
+        if SW, SW = isfield(Y,'datatype'); end;
+        if SW, SW = strcmp(Y.datatype,'HISTOGRAM'); end;
+        if SW,                 
+                [yr,yc]=size(Y.H);
+                Q = repmat(nan,length(q),yc);
+                if ~isfield(Y,'N');
+                        Y.N = sum(Y.H,1);
+                end;
+                
+                for k1=1:yc,
+                        tmp=Y.H(:,k1)>0;
+                        h=full(Y.H(tmp,k1));
+                        t = Y.X(tmp,min(size(Y.X,2),k1));
+                        for k2 = 1:length(q),
+                                tmp = cumsum(h)-Y.N(k1)*q(k2)/100;
+                                if ~any(~tmp), 
+                                        Q(k2,k1) = t(find(diff(sign(tmp)))+1);
+                                else
+                                        Q(k2,k1) = mean(t(find(~tmp)+(0:1)));
+                                end;	        
+                        end
+                end;
+                
+        elseif isnumeric(Y),
+                [yr,yc] = size(Y);
+                Q = repmat(nan,length(q),yc);
+		
+		if flag_implicit_skip_nan,    % default 
+	                N = sum(~isnan(Y),1);
+	    	        Y(isnan(Y)) = inf;   % making sure NaN's are at the end;
+		else   				% not supported
+			N = size(Y,1)*ones(1,yc);
+		end;
+    		Y = sort(Y,1);
+		
+		for k1 = 1:yc,
+	                for k2 = 1:length(q),
+				Q(k2,k1) = flix(Y(:,k1),N(k1)*q(k2)/100 + 0.5);                	        
+                	end;
+                end;
+                
+        else
+                fprintf(2,'Error PERCENTILE: invalid input argument\n');
+                return;
+        end;
+        
+end;
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/NaN/quantile.m	Tue Apr 15 07:56:25 2003 +0000
@@ -0,0 +1,100 @@
+function Q=quantile(Y,q)
+% QUANTILE calculates the quantiles of histograms or sample arrays.  
+%
+%  Q = quantile(Y,q)      
+%     returns the q-th quantile of each column of sample array Y
+%
+%  Q = quantile(HIS,q)
+%     returns the q-th quantile from the histogram HIS. 
+%     HIS must be a HISTOGRAM struct as defined in HISTO2 or HISTO3.
+%
+% If q is a vector, the each row of Q returns the q(i)-th quantile 
+%
+% see also: FLIX, HISTO2, HISTO3, PERCENTILE
+
+% QUANTILES demonstrates how to calculate quantiles 
+%
+% q-quantile Q of data series Y 
+% (1) explicite form
+%	tmp=sort(Y);
+%	N=sum(~isnan(Y));
+%	Q = flix(tmp,N*q + 0.5);
+%
+% (2) in 1 line
+%	Q = flix(sort(Y),sum(~isnan(Y))*q + 0.5);
+%
+% (3) q-quantile Q of histogram H with bins t
+%	tmp=HISTOG>0;
+%	HISTOG=full(HISTOG(tmp));
+%	t = t(tmp);
+%	N = sum(HISTOG);
+%	tmp = cumsum(HISTOG)-N*q;
+%
+%	if ~any(~tmp), 
+%		Q(k) = t(find(diff(sign(tmp)))+1);
+%	else
+%		Q(k) = mean(t(find(~tmp)+(0:1)));
+%	end;	
+
+%	$Revision$
+%	$Id$
+%	last revision 13 Mar.2003
+%	Copyright (c) 1996-2003 by Alois Schloegl
+%	e-mail: a.schloegl@ieee.org	
+
+
+if nargin<2,
+	help quantile
+        
+else
+        SW = isstruct(Y);
+        if SW, SW = isfield(Y,'datatype'); end;
+        if SW, SW = strcmp(Y.datatype,'HISTOGRAM'); end;
+        if SW,                 
+                [yr,yc]=size(Y.H);
+                Q = repmat(nan,length(q),yc);
+                if ~isfield(Y,'N');
+                        Y.N = sum(Y.H,1);
+                end;
+                
+                for k1=1:yc,
+                        tmp=Y.H(:,k1)>0;
+                        h=full(Y.H(tmp,k1));
+                        t = Y.X(tmp,min(size(Y.X,2),k1));
+                        for k2 = 1:length(q),
+                                tmp = cumsum(h)-Y.N(k1)*q(k2);
+                                if ~any(~tmp), 
+                                        Q(k2,k1) = t(find(diff(sign(tmp)))+1);
+                                else
+                                        Q(k2,k1) = mean(t(find(~tmp)+(0:1)));
+                                end;	        
+                        end
+                end;
+                
+        elseif isnumeric(Y),
+                [yr,yc] = size(Y);
+                Q = repmat(nan,length(q),yc);
+		
+		if flag_implicit_skip_nan,    % default 
+	                N = sum(~isnan(Y),1);
+	    	        Y(isnan(Y)) = inf;   % making sure NaN's are at the end;
+		else   				% not supported
+			N = size(Y,1)*ones(1,yc);
+		end;
+    		Y = sort(Y,1);
+		
+		for k1 = 1:yc,
+	                for k2 = 1:length(q),
+				Q(k2,k1) = flix(Y(:,k1),N(k1)*q(k2) + 0.5);                	        
+                	end;
+                end;
+                
+        else
+                fprintf(2,'Error QUANTILES: invalid input argument\n');
+                return;
+        end;
+        
+end;
+
+
+
--- a/extra/NaN/sumskipnan.m	Fri Apr 11 13:04:24 2003 +0000
+++ b/extra/NaN/sumskipnan.m	Tue Apr 15 07:56:25 2003 +0000
@@ -86,10 +86,10 @@
                 fprintf('Error SUMSKIPNAN: DIM argument must be 1 or 2\n');	
         end;
         
-        %if ~flag_implicit_skip_nan,
+        if ~flag_implicit_skip_nan,
         % the following command implements NaN-In -> NaN-Out
-        %	o(count<size(i,DIM)) = NaN;         
-        %end;	
+        	o(count<size(i,DIM)) = NaN;         
+        end;	
         if nargout>2,
                 i = i.^2;
                 SSQ = sumskipnan(i,DIM);
@@ -116,9 +116,9 @@
 	if nargout>1
                 count = sum(~isnan(i),DIM); 
         end;
-	%if flag_implicit_skip_nan, %%% skip always NaN's
+	if flag_implicit_skip_nan, %%% skip always NaN's
                 i(isnan(i)) = 0;
-        %end;
+        end;
         o = sum(i,DIM);
         if nargout>2,
                 i = i.^2;
--- a/extra/NaN/trimean.m	Fri Apr 11 13:04:24 2003 +0000
+++ b/extra/NaN/trimean.m	Tue Apr 15 07:56:25 2003 +0000
@@ -8,8 +8,9 @@
 % REFERENCES:
 % [1] http://mathworld.wolfram.com/Trimean.html
 
-%    	Version 1.27  Date: 12 Sep 2002
-%	Copyright (C) 1996-2002 by Alois Schloegl <a.schloegl@ieee.org>	
+%	$Revision$
+%	$Id$
+%	Copyright (C) 1996-2003 by Alois Schloegl <a.schloegl@ieee.org>	
 
 
 % check dimension
@@ -25,7 +26,11 @@
         if isempty(DIM), DIM=1; end;
 end;
 
-N = sumskipnan(~isnan(Y),DIM);
+if flag_implicit_skip_nan,
+	N = sumskipnan(~isnan(Y),DIM);
+else
+	N = sumskipnan(ones(size(Y)),DIM);
+end;
 
 sz = size(Y);
 sz(DIM) = 1;