changeset 860:301cf3647c90 octave-forge

Quantile calculation of sample arrays and histograms implemented
author schloegl
date Thu, 13 Mar 2003 14:54:31 +0000
parents b5441baeda43
children a53847c71ad8
files extra/tsa/quantiles.m
diffstat 1 files changed, 68 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/extra/tsa/quantiles.m	Thu Mar 13 10:43:41 2003 +0000
+++ b/extra/tsa/quantiles.m	Thu Mar 13 14:54:31 2003 +0000
@@ -1,8 +1,18 @@
 function Q=quantiles(Y,q)
-% QUANTILES demonstrates how to calculate quantiles 
+% QUANTILES calculates the quantiles of histograms or sample arrays.  
+%
+%  Q = quantiles(Y,q)      
+%     returns the q-th quantile of each column of sample array Y
+%
+%  Q = quantiles(HIS,q)
+%     returns the q-th quantile from the histogram HIS. 
+%     HIS must be a HISTOGRAM struct as defined in HISTO2 or HISTO3.
 %
-% see also: FLIX
+% If q is a vector, the each row of Q returns the q(i)-th quantile 
 %
+% see also: FLIX, HISTO2, HISTO3
+
+% QUANTILES demonstrates how to calculate quantiles 
 %
 % q-quantile Q of data series Y 
 % (1) explicite form
@@ -25,13 +35,60 @@
 %	else
 %		Q(k) = mean(t(find(~tmp)+(0:1)));
 %	end;	
-
-% .CHANGELOG
-% 27.09.2001  calc of Quantiles 
+
+%	$Revision$
+%	$Id$
+%	last revision 13 Mar.2003
+%	Copyright (c) 1996-2003 by Alois Schloegl
+%	e-mail: a.schloegl@ieee.org	
+
 
-%	Version 2.83
-%	last revision 07.02.2002
-%	Copyright (c) 1996-2002 by Alois Schloegl
-%	e-mail: a.schloegl@ieee.org	
-
-help quantiles
+if nargin<2,
+	help quantiles
+        
+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);
+                N = sum(~isnan(Y),1);
+                Y(isnan(Y)) = inf;   % making sure NaN's are at the 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;
+
+
+