Page 113 - Computational Statistics Handbook with MATLAB
P. 113

100                        Computational Statistics Handbook with MATLAB


                             Example 4.11
                             The following function cssphrnd generates random variables on a d-dimen-
                             sional unit sphere. We illustrate its use by generating random variables that
                             are on the unit circle S 2  .
                                % function X = cssphrnd(n,d);
                                % This function will generate n d-dimensional
                                % random variates that are distributed on the
                                % unit d-dimensional sphere. d >= 2

                                function X = cssphrnd(n,d)
                                if d < 2
                                   error('ERROR - d must be greater than 1.')
                                   break
                                end
                                % Generate standard normal random variables.
                                tmp = randn(d,n);
                                % Find the magnitude of each column.
                                % Square each element, add and take the square root.
                                mag = sqrt(sum(tmp.^2));
                                % Make a diagonal matrix of them - inverses.
                                dm = diag(1./mag);
                                % Multiply to scale properly.
                                % Transpose so X contains the observations.
                                X = (tmp*dm)';

                             We can use this function to generate a set of random variables for d =  2   and
                             plot the result in Figure 4.8.
                                X = cssphrnd(500,2);
                                plot(X(:,1),X(:,2),'x')
                                axis equal
                                xlabel('X_1'),ylabel('X_2')









                             4.4 Generating Discrete Random Variables




                             Binomia  l ll
                             BinomiaBinomia
                             Binomia
                             l
                             A binomial random variable with parameters n and p represents the number
                             of successes in n independent trials. We can obtain a binomial random vari-
                            © 2002 by Chapman & Hall/CRC
   108   109   110   111   112   113   114   115   116   117   118