Home > wafo > wstats > wquantile.m

# wquantile

## PURPOSE

Empirical quantile (percentile).

## SYNOPSIS

q = wquantile(x,p,method)

## DESCRIPTION

``` WQUANTILE Empirical quantile (percentile).

CALL:  q = wquantile(x,p,method)

q = empirical quantile
x = input vector or matrix
p = probability
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.

If input  x  is a matrix then the quantile is computed for
every column. Input  p  may be vector also. It even
accepts  x  being a vector and  p  a matrix!```

## CROSS-REFERENCE INFORMATION

This function calls:
 error Display message and abort function.
This function is called by:
 iqr Computes the Inter Quartile Range wqqplot Plot empirical quantile of X vs empirical quantile of Y

## SOURCE CODE

```001 function  q = wquantile(x,p,method)
002 %WQUANTILE Empirical quantile (percentile).
003 %
004 %  CALL:  q = wquantile(x,p,method)
005 %
006 %      q = empirical quantile
007 %      x = input vector or matrix
008 %      p = probability
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 %  If input  x  is a matrix then the quantile is computed for
014 %  every column. Input  p  may be vector also. It even
015 %  accepts  x  being a vector and  p  a matrix!
016
017 % References:
018 %  Holtsberg, Anders (1999)
019 %  Stixbox. A statistics toolbox for Matlab and Octave.
020 %  Lund University
021 %  http://www.maths.lth.se/matstat/stixbox
022
023 % Tested on: Matlab 5.3
024 % History:
025 % revised pab
027 % - updated help header to conform to wafo style
028 % by Anders Holtsberg 1994, 1998
029
030
031 error(nargchk(2,3,nargin))
032 if nargin<3|isempty(method), method=1; end
033 if min(size(x)) == 1
034    x = x(:);
035    q = zeros(size(p));
036 else
037    if min(size(p)) > 1
038       error('Not both matrix x and matrix p input')
039    end
040    q = zeros(length(p),size(x,2));
041 end
042 if any(any((p>1|p<0)))
043    error('Input p is not probability')
044 end
045
046 x = sort(x);
047 p = p(:);
048 n = size(x,1);
049 if method == 3
050    qq1 = x(ceil(max(1,p*n)),:);
051    qq2 = x(floor(min(p*n+1,n)),:);
052    qq = (qq1+qq2)/2;
053 else
054    x = [x(1,:); x; x(n,:)];
055    if method == 2
056       % This method is from Hjort's "Computer
057       % intensive statistical methods" page 102
058       i = p*(n+1)+1;
059    else % Metod 1
060       i = p*n+1.5;
061    end
062    iu = ceil(i);
063    il = floor(i);
064    d = (i-il)*ones(1,size(x,2));
065    qq = x(il,:).*(1-d)+x(iu,:).*d;
066 end
067
068 q(:) = qq;
069
070
071
072
073
074
075
076```

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