Page 116 - Computational Statistics Handbook with MATLAB
P. 116

Chapter 4: Generating Random Variables                          103


                             This leads to the following algorithm.

                             PROCEDURE - GENERATING POISSON RANDOM VARIABLES


                                1. Generate a uniform random number U.
                                2. Initialize the quantities:  i =  0  ,  p 0 =  e  – λ  , and  F 0 =  p 0  .
                                3. If  U ≤  F i  , then deliver  X =  i . Return to step 1.
                                4. Else  increment the values:  p i + =  λp i (⁄  i +  1)  ,  i =  i +  1  ,  and
                                                                 1
                                   F i + =  F i +  p i +  1  .
                                      1
                                5. Return to step 3.
                                                                          λ
                             This algorithm could be made more efficient when   is large. The interested
                             reader is referred to Ross [1997] for more details.

                             Example 4.13
                             The following shows how to implement the procedure for generating Pois-
                             son random variables in MATLAB.
                                % function X = cspoirnd(lam,n)
                                % This function will generate Poisson
                                % random variables with parameter lambda.
                                % The reference for this is Ross, 1997, page 50.

                                function x = cspoirnd(lam,n)
                                x = zeros(1,n);
                                j = 1;
                                while j <= n
                                   flag = 1;
                                   % initialize quantities
                                   u = rand(1);
                                   i = 0;
                                   p = exp(-lam);
                                   F = p;
                                   while flag % generate the variate needed
                                      if u <= F % then accept
                                         x(j) = i;
                                         flag = 0;
                                         j = j+1;
                                      else % move to next probability
                                         p = lam*p/(i+1);
                                         i = i+1;
                                         F = F + p;
                                      end
                                   end

                            © 2002 by Chapman & Hall/CRC
   111   112   113   114   115   116   117   118   119   120   121