Home > wafo > kdetools > qlevels2.m

qlevels2

PURPOSE ^

Calculates quantile levels which encloses P% of data

SYNOPSIS ^

[q, p]=qlevels2(r,p,method)

DESCRIPTION ^

 QLEVELS2 Calculates quantile levels which encloses P% of data 
  
   CALL: [ql PL] = qlevels2(data,PL,method); 
  
     ql   = the discrete quantile levels, size Np x D 
     data = data matrix, size N x D (D = # of dimensions) 
     PL   = percent level vector, length Np (default [10:20:90 95 99 99.9]) 
   method = 1 Interpolation so that F(X_(k)) == (k-0.5)/n. (default) 
          2 Interpolation so that F(X_(k)) == k/(n+1). 
          3 Based on the empirical distribution. 
  
  QLEVELS2 sort the columns of data in ascending order and find the   
           quantile levels for each column which encloses  P% of the data.   
    
  Examples : % Finding quantile levels enclosing P% of data:  
    xs  = wnormrnd(0,1,100000,1); 
    qls = qlevels2(wnormpdf(xs),[10:20:90 95 99 99.9]); 
             % compared with the exact values 
    ql  = wnormpdf(wnorminv((100-[10:20:90 95 99 99.9])/200)); 
  
  Finding the median of xs: 
    ql  = qlevels2(xs,50);  
  
  See also  qlevels

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function [q, p]=qlevels2(r,p,method) 
002 %QLEVELS2 Calculates quantile levels which encloses P% of data 
003 % 
004 %  CALL: [ql PL] = qlevels2(data,PL,method); 
005 % 
006 %    ql   = the discrete quantile levels, size Np x D 
007 %    data = data matrix, size N x D (D = # of dimensions) 
008 %    PL   = percent level vector, length Np (default [10:20:90 95 99 99.9]) 
009 %  method = 1 Interpolation so that F(X_(k)) == (k-0.5)/n. (default) 
010 %         2 Interpolation so that F(X_(k)) == k/(n+1). 
011 %         3 Based on the empirical distribution. 
012 % 
013 % QLEVELS2 sort the columns of data in ascending order and find the   
014 %          quantile levels for each column which encloses  P% of the data.   
015 %   
016 % Examples : % Finding quantile levels enclosing P% of data:  
017 %   xs  = wnormrnd(0,1,100000,1); 
018 %   qls = qlevels2(wnormpdf(xs),[10:20:90 95 99 99.9]); 
019 %            % compared with the exact values 
020 %   ql  = wnormpdf(wnorminv((100-[10:20:90 95 99 99.9])/200)); 
021 % 
022 % Finding the median of xs: 
023 %   ql  = qlevels2(xs,50);  
024 % 
025 % See also  qlevels 
026  
027 %tested on: matlab 5.3 
028 % History: 
029 % revised pab  
030 %  -fixed a bug for D>=3 
031 %  -added different methods from wquantile 
032 %  -updated example   
033 % by pab 25.09.1999 
034 %  
035  
036 [n, d]=size(r); 
037 if nargin<2|isempty(p) 
038   p = [10:20:90 95 99 99.9]; 
039 elseif p<0 | 100<p, 
040   error('PL must satisfy 0 <= PL <= 100') 
041 end 
042 if nargin<3|isempty(method), method=1; end 
043  
044 if (n==1) & (d>1) 
045   r=r(:); 
046   n=d; 
047   d=1; 
048 end 
049 if d>1 
050   if min(size(p)) > 1  
051     error('Not both matrix r and matrix p input') 
052   end 
053   q = zeros(length(p),d); 
054 else 
055   q = zeros(size(p)); 
056 end 
057 p = 1-p(:)/100; 
058 x = sort(r); 
059  
060  
061 if method == 3 
062    qq1 = x(ceil(max(1,p*n)),:);  
063    qq2 = x(floor(min(p*n+1,n)),:); 
064    qq = (qq1+qq2)/2; 
065 else                          
066    x = [x(1,:); x; x(n,:)]; 
067    if method == 2 
068       % This method is from Hjort's "Computer 
069       % intensive statistical methods" page 102 
070       i = p*(n+1)+1; 
071    else % Metod 1 
072       i = p*n+1.5; 
073    end 
074    iu = ceil(i); 
075    il = floor(i); 
076    d1 = (i-il)*ones(1,d); 
077    qq = x(il,:).*(1-d1)+x(iu,:).*d1; 
078 end 
079  
080 q(:) = qq; 
081  
082 return 
083  
084  
085

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group


Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003