Page 98 - Computational Statistics Handbook with MATLAB
P. 98

Chapter 4: Generating Random Variables                           85


                                   else
                                      X(i) = x(3); % It has to be between 0.5 and 1.
                                   end
                                end
                             One way to verify that our random variables are from the desired distribu-
                             tion is to look at the relative frequency of each x.

                                % Find the proportion of each number.
                                x0 = length(find(X==0))/100;
                                x1 = length(find(X==1))/100;
                                x2 = length(find(X==2))/100;
                             The resulting estimated probabilities are

                                                       ˆ
                                                      P x =(  x 0 ) =  0.26
                                                       ˆ
                                                      P x =(  x 1 ) =  0.21
                                                       ˆ
                                                      P x =(  x 2 ) =  0.53.


                             These values are reasonable when compared with the desired probability
                             mass values.




                                         ej
                                       -RRe
                                     cee-
                                     ccee-- RReejj
                                                   ethod
                                                 etho
                                                       d
                                                       dd
                                           eectionction
                                          jeectionctionM
                                                 M
                                                 MM ethoetho
                                c
                               cceptaneptan
                               ceptaneptan
                             Ac
                             AcAc
                             Acc
                             In some cases, we might have a simple method for generating a random vari-
                             able from one density, say gy()  , instead of the density we are seeking. We can
                             use this density to generate from the desired continuous density  f x()  . We
                             first generate a random number Y from  gy()   and accept the value with a
                             probability proportional to the ratio f Y() (⁄  gY())  .
                              If we define c as a constant that satisfies
                                                     fy()
                                                    ----------- ≤  c;  for all y  ,         (4.6)
                                                    gy()
                             then we can generate the desired variates using the procedure outlined
                             below. The constant c is needed because we might have to adjust the height
                             of  gy()  to ensure that it is above  f y() . We generate points from  cg y()  , and
                             those points that are inside the curve  f y()   are accepted as belonging to the
                             desired density. Those that are outside are rejected. It is best to keep the num-
                             ber of rejected variates small for maximum efficiency.
                            © 2002 by Chapman & Hall/CRC
   93   94   95   96   97   98   99   100   101   102   103