home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / hypergeometric_pdf.m < prev    next >
Encoding:
Text File  |  1997-02-26  |  2.2 KB  |  65 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:  hypergeometric_pdf (x, m, t, n)
  18. ##
  19. ## Compute the probability density function (PDF) at x of the
  20. ## hypergeometric distribution with parameters m, t, and n. This is the
  21. ## probability of obtaining x marked items when randomly drawing a
  22. ## sample of size n without replacement from a population of total size
  23. ## t containing m marked items.
  24. ##
  25. ## The arguments must be of common size or scalar.
  26.  
  27. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  28. ## Description:  PDF of the hypergeometric distribution
  29.   
  30. function pdf = hypergeometric_pdf (x, m, t, n)
  31.   
  32.   if (nargin != 4)
  33.     usage ("hypergeometric_pdf (x, m, t, n)");
  34.   endif
  35.   
  36.   [retval, x, m, t, n] = common_size (x, m, t, n);
  37.   if (retval > 0)
  38.     error (["hypergeometric_pdf:  ", ...
  39.         "x, m, t, and n must be of common size or scalar"]);
  40.   endif
  41.   
  42.   [r, c] = size (x);
  43.   s = r * c;
  44.   x = reshape (x, 1, s);
  45.   m = reshape (m, 1, s);
  46.   t = reshape (t, 1, s);
  47.   n = reshape (n, 1, s);
  48.   pdf = zeros * ones (1, s);
  49.   ## everything in i1 gives NaN
  50.   i1 = ((m < 0) | (t < 0) | (n <= 0) | (m != round (m)) | 
  51.     (t != round (t)) | (n != round (n)) | (m > t) | (n > t));
  52.   ## everything in i2 gives 0 unless in i1
  53.   i2 = ((x != round (x)) | (x < 0) | (x > m) | (n < x) | (n-x > t-m));
  54.   k = find (i1);
  55.   if any (k)
  56.     pdf (k) = NaN * ones (size (k));
  57.   endif
  58.   k = find (!i1 & !i2);
  59.   if any (k)
  60.     pdf (k) = (bincoeff (m(k), x(k)) .* bincoeff (t(k)-m(k), n(k)-x(k))
  61.            ./ bincoeff (t(k), n(k)));
  62.   endif
  63.   
  64. endfunction
  65.