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

  1. ## Copyright (C) 1995, 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:  binomial_inv (x, n, p)
  18. ##
  19. ## For each element of x, compute the quantile at x of the binomial
  20. ## distribution with parameters n and p.
  21.  
  22. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  23. ## Description:  Quantile function of the binomial distribution
  24.  
  25. function inv = binomial_inv (x, n, p)
  26.   
  27.   if (nargin != 3)
  28.     usage ("binomial_inv (x, n, p)");
  29.   endif
  30.   
  31.   [retval, x, n, p] = common_size (x, n, p);
  32.   if (retval > 0)
  33.     error (["binomial_inv:  ", ...
  34.         "x, n and p must be of common size or scalars"]);
  35.   endif
  36.  
  37.   [r, c] = size (x);
  38.   s   = r * c;
  39.   x   = reshape (x, 1, s);
  40.   n   = reshape (n, 1, s);
  41.   p   = reshape (p, 1, s);
  42.   inv = zeros (1, s);
  43.   
  44.   k = find (!(x >= 0) | !(x <= 1) | !(n >= 0) | (n != round (n)) ...
  45.       | !(p >= 0) | !(p <= 1));
  46.   if any (k)
  47.     inv(k) = NaN * ones (1, length (k));
  48.   endif
  49.   
  50.   k = find ((x >= 0) & (x <= 1) & (n >= 0) & (n == round (n)) ...
  51.       & (p >= 0) & (p <= 1));
  52.   if any (k)
  53.     cdf = binomial_pdf (0, n(k), p(k));
  54.     while (any (inv(k) < n(k)))
  55.       m = find (cdf < x(k));
  56.       if any (m)
  57.     inv(k(m)) = inv(k(m)) + 1;
  58.     cdf(m) = cdf(m) + binomial_pdf (inv(k(m)), n(k(m)), p(k(m)));
  59.       else
  60.     break;
  61.       endif
  62.     endwhile
  63.   endif
  64.  
  65.   inv = reshape (inv, r, c);
  66.   
  67. endfunction
  68.