home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / discrete_inv.m < prev    next >
Encoding:
Text File  |  1997-02-26  |  2.0 KB  |  67 lines

  1. ## Copyright (C) 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  discrete_inv (X, V, P)
  18. ##
  19. ## For each component of X, compute the quantile (the inverse of the
  20. ## CDF) at X of the univariate distribution which assumes the values in
  21. ## V with probabilities P.
  22.  
  23. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  24. ## Description:  Quantile function of a discrete distribution
  25.  
  26. function inv = discrete_inv (X, V, P)
  27.   
  28.   if (nargin != 3)
  29.     usage ("discrete_inv (X, V, P)");
  30.   endif
  31.  
  32.   [r, c] = size (X);
  33.  
  34.   if (! is_vector (V))
  35.     error ("discrete_inv:  V must be a vector");
  36.   elseif (! is_vector (P) || (length (P) != length (V)))
  37.     error ("discrete_inv:  P must be a vector with length (V) elements");
  38.   elseif (! (all (P >= 0) && any (P)))
  39.     error ("discrete_inv:  P must be a nonzero, nonnegative vector");
  40.   endif
  41.  
  42.   n = r * c;
  43.   X = reshape (X, 1, n);
  44.   m = length (V);
  45.   [V, ind] = sort (V);
  46.   s = reshape (cumsum (P / sum (P)), m, 1);
  47.  
  48.   inv = NaN * ones (n, 1);
  49.   if any (k = find (X == 0))
  50.     inv(k) = -Inf * ones (1, length (k));
  51.   endif
  52.   if any (k = find (X == 1))
  53.     inv(k) = V(m) * ones (1, length (k));
  54.   endif
  55.   if any (k = find ((X > 0) & (X < 1)))
  56.     n = length (k);
  57.     ## --FIXME--
  58.     ## This does not work!
  59.     inv(k) = V(sum ((ones (m, 1) * X(k)) > (s * ones (1, n))) + 1);
  60.   endif
  61.  
  62.   inv = reshape (inv, r, c);
  63.  
  64. endfunction
  65.  
  66.  
  67.