home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / pascal_pdf.m < prev    next >
Encoding:
Text File  |  1997-02-19  |  2.2 KB  |  71 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_pdf (x, n, p)
  18. ##
  19. ## For each element of x, compute the probability density function (PDF)
  20. ## at x of the Pascal (negative binomial) distribution with parameters n
  21. ## and p.
  22. ##
  23. ## The number of failures in a Bernoulli experiment with success
  24. ## probability p before the n-th success follows this distribution.
  25.  
  26. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  27. ## Description:  PDF of the Pascal (negative binomial) distribution
  28.  
  29. function pdf = pascal_pdf (x, n, p)
  30.   
  31.   if (nargin != 3)
  32.     usage ("pascal_pdf (x, n, p)");
  33.   endif
  34.  
  35.   [retval, x, n, p] = common_size (x, n, p);
  36.   if (retval > 0)
  37.     error (["pascal_pdf:  ", ...
  38.         "x, n and p must be of common size or scalar"]);
  39.   endif
  40.   
  41.   [r, c] = size (x);
  42.   s = r * c;
  43.   x   = reshape (x, 1, s);
  44.   n   = reshape (n, 1, s);
  45.   p   = reshape (p, 1, s);
  46.   cdf = zeros (1, s);
  47.  
  48.   k = find (isnan (x) | (n < 1) | (n == Inf) | (n != round (n)) ...
  49.       | (p < 0) | (p > 1));
  50.   if any (k)
  51.     pdf(k) = NaN * ones (1, length (k));
  52.   endif
  53.   
  54.   ## Just for the fun of it ...
  55.   k = find ((x == Inf) & (n > 0) & (n < Inf) & (n == round (n)) ...
  56.       & (p == 0));
  57.   if any (k)
  58.     pdf(k) = ones (1, length (k));
  59.   endif
  60.   
  61.   k = find ((x >= 0) & (x < Inf) & (x == round (x)) & (n > 0) ...
  62.       & (n < Inf) & (n == round (n)) & (p > 0) & (p <= 1));
  63.   if any (k)
  64.     pdf(k) = bincoeff (-n(k), x(k)) .* (p(k) .^ n(k)) ...
  65.     .* ((p(k) - 1) .^ x(k));
  66.   endif
  67.  
  68.   pdf = reshape (pdf, r, c);
  69.   
  70. endfunction
  71.