home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / poisson_rnd.m < prev    next >
Encoding:
Text File  |  1997-03-04  |  2.4 KB  |  81 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:  poisson_rnd (lambda [, r, c])
  18. ##
  19. ## poisson_rnd (lambda) returns a matrix of random samples from the
  20. ## Poisson distribution with parameter lambda.  The size of the matrix
  21. ## is the size of lambda.
  22. ##
  23. ## poisson_rnd (lambda, r, c) returns an r by c matrix of random samples
  24. ## from the Poisson distribution with parameter lambda, which must be a
  25. ## scalar or of size r by c.
  26.   
  27. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  28. ## Description:  Random deviates from the Poisson distribution
  29.   
  30. function rnd = poisson_rnd (l, r, c)
  31.  
  32.   if (nargin == 3)
  33.     if ( !(is_scalar (r) && (r > 0) && (r == round (r))) )
  34.       error ("poisson_rnd:  r must be a positive integer");
  35.     endif
  36.     if ( !(is_scalar (c) && (c > 0) && (c == round (c))) )
  37.       error ("poisson_rnd:  c must be a positive integer");
  38.     endif
  39.     [retval, l] = common_size (l, zeros (r, c));
  40.     if (retval > 0)
  41.       error (strcat("poisson_rnd:  ",
  42.             "lambda must be scalar or of size ",
  43.             sprintf ("%d by %d", r, c)));
  44.     endif
  45.   elseif (nargin != 1)
  46.     usage ("poisson_rnd (lambda [, r, c])");
  47.   endif
  48.   
  49.   [r, c] = size (l);
  50.   s = r * c;
  51.   l = reshape (l, 1, s);
  52.   rnd = zeros (1, s);
  53.   
  54.   k = find (!(l > 0) | !(l < Inf));
  55.   if (any (k))
  56.     rnd(k) = NaN * ones (1, length (k));
  57.   endif
  58.   
  59.   k = find ((l > 0) & (l < Inf));
  60.   if (any (k))
  61.     l = l(k);
  62.     len = length (k);
  63.     num = zeros (1, len);
  64.     sum = - log (1 - rand (1, len)) ./ l;
  65.     while (1)
  66.       ind = find (sum < 1);
  67.       if (any (ind))
  68.     sum(ind) = (sum(ind)
  69.             - log (1 - rand (1, length (ind))) ./ l(ind));
  70.     num(ind) = num(ind) + 1;
  71.       else
  72.     break;
  73.       endif
  74.     endwhile
  75.     rnd(k) = num;
  76.   endif
  77.   
  78.   rnd = reshape (rnd, r, c);
  79.   
  80. endfunction
  81.