home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / pascal_cdf.m < prev    next >
Encoding:
Text File  |  1998-10-23  |  2.3 KB  |  83 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:  pascal_cdf (x, n, p)
  18. ##
  19. ## For each element of x, compute the CDF at x of the Pascal (negative
  20. ## binomial) distribution with parameters n and p.
  21. ##
  22. ## The number of failures in a Bernoulli experiment with success
  23. ## probability p before the n-th success follows this distribution.
  24.  
  25. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  26. ## Description:  CDF of the Pascal (negative binomial) distribution
  27.  
  28. function cdf = pascal_cdf (x, n, p)
  29.   
  30.   if (nargin != 3)
  31.     usage ("pascal_cdf (x, n, p)");
  32.   endif
  33.  
  34.   [retval, x, n, p] = common_size (x, n, p);
  35.   if (retval > 0)
  36.     error (["pascal_cdf:  ", ...
  37.         "x, n and p must be of common size or scalar"]);
  38.   endif
  39.   
  40.   [r, c] = size (x);
  41.   s = r * c;
  42.   x   = reshape (x, 1, s);
  43.   n   = reshape (n, 1, s);
  44.   p   = reshape (p, 1, s);
  45.   cdf = zeros (1, s);
  46.  
  47.   k = find (isnan (x) | (n < 1) | (n == Inf) | (n != round (n)) ...
  48.       | (p < 0) | (p > 1));
  49.   if any (k)
  50.     cdf(k) = NaN * ones (1, length (k));
  51.   endif
  52.   
  53.   k = find ((x == Inf) & (n > 0) & (n < Inf) & (n == round (n)) ...
  54.       & (p >= 0) & (p <= 1));
  55.   if any (k)
  56.     cdf(k) = ones (1, length (k));
  57.   endif
  58.   
  59.   k = find ((x >= 0) & (x < Inf) & (x == round (x)) & (n > 0) ...
  60.       & (n < Inf) & (n == round (n)) & (p > 0) & (p <= 1));
  61.   if any (k)
  62.     ## Does anyone know a better way to do the summation?
  63.     m = zeros (1, length (k));
  64.     x = floor (x(k));
  65.     n = n(k);
  66.     p = p(k);
  67.     y = cdf(k);
  68.     while (1)
  69.       l = find (m <= x);
  70.       if any (l)
  71.     y(l) = y(l) + pascal_pdf (m(l), n(l), p(l));
  72.     m(l) = m(l) + 1;
  73.       else
  74.     break;
  75.       endif
  76.     endwhile
  77.     cdf(k) = y;
  78.   endif
  79.  
  80.   cdf = reshape (cdf, r, c);
  81.   
  82. endfunction
  83.