Page 107 - Computational Statistics Handbook with MATLAB
P. 107

94                         Computational Statistics Handbook with MATLAB


                                                                 k  
                                                      X =  – 2log  ∏ U  i  .             (4.15)
                                                                    
                                                                i =  1
                                   ν
                             When   is odd, say  2k +  1 , we can use the fact that the chi-square distribu-
                                      ν
                                                                      ν
                             tion with   degrees of freedom is the sum of   squared independent stan-
                             dard normals [Ross, 1997]. We obtain the required random variable by first
                             simulating a chi-square with  2k  degrees of freedom and adding a squared
                             standard normal variate Z, as follows
                                                                   k
                                                                     
                                                          2
                                                    X =  Z –  2log  ∏  U i  .            (4.16)
                                                                     
                                                                  i =  1
                             Example 4.8
                             In this example, we provide a function that will generate chi-square random
                             variables.
                                % function X = cschirnd(n,nu)
                                % This function will return n chi-square
                                % random variables with degrees of freedom nu.

                                function X = cschirnd(n,nu)
                                % Generate the uniforms needed.
                                rm = rem(nu,2);
                                k = floor(nu/2);
                                if rm == 0      % then even degrees of freedom
                                   U = rand(k,n);
                                   if k ~= 1
                                      X = -2*log(prod(U));
                                   else
                                      X = -2*log(U);
                                   end
                                else            % odd degrees of freedom
                                   U = rand(k,n);
                                   Z = randn(1,n);
                                   if k ~= 1
                                      X = Z.^2-2*log(prod(U));
                                   else
                                      X = Z.^2-2*log(U);
                                   end
                                end
                             The use of this function to generate random variables is left as an exercise.


                            © 2002 by Chapman & Hall/CRC
   102   103   104   105   106   107   108   109   110   111   112