Page 118 - Computational Statistics Handbook with MATLAB
P. 118

Chapter 4: Generating Random Variables                          105







                                      0.6

                                      0.5

                                      0.4

                                      0.3

                                      0.2


                                      0.1

                                        0
                                                   0      1      2     3      4
                                                               X

                               IG
                              FI F U URE G 4.1  RE 4.1 0  0
                                  4.1
                               GU
                              F F II  GU  RE RE 4.1  0 0
                              This is the histogram for random variables generated from the Poisson with  λ =  0.5  .
                                                         X =  NU   ,
                             where the function  y  ,  y ≥  0   means to round up the argument y. The next
                             example shows how to implement this in MATLAB.

                             Example 4.14
                             The method for generating discrete uniform is implemented in the function
                             csdunrnd, given below.
                                % function X = csdunrnd(N,n)
                                % This function will generate random variables
                                % from the discrete uniform distribution. It picks
                                % numbers uniformly between 1 and N.
                                function X = csdunrnd(N,n)
                                X = ceil(N*rand(1,n));
                             To verify that we are generating the right random variables, we can look at
                             the observed relative frequencies. Each should have relative frequency of
                              ⁄
                             1 N  .This is shown below where N =  5   and the sample size is 500.
                                N = 5;
                                n = 500;
                                x = csdunrnd(N,n);

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