Module Library |
generates a random sample from a Dirichlet distribution
The following example generates 1000 samples from a two-dimensional Dirichlet distribution. Each row of the returned matrix x is a row vector sampled from the Dirichlet distribution. The example then computes the sample mean and covariance and compares them with the expected values. Here are the code and the output:
call randseed(1); n = 1000; Shape = {2, 1, 1}; x = RANDDIRICHLET(n,Shape); Shape0 = sum(Shape); d = nrow(Shape)-1; s = Shape[1:d]; ExpectedValue = s`/Shape0; Cov = -s*s` / (Shape0##2*(Shape0+1)); /* replace diagonal elements with variance */ Variance = s#(Shape0-s) / (Shape0##2*(Shape0+1)); do i = 1 to d; Cov[i,i] = Variance[i]; end; SampleMean = x[:,]; n = nrow(x); y = x - repeat( SampleMean, n ); SampleCov = y`*y / (n-1); print SampleMean ExpectedValue, SampleCov Cov; SampleMean ExpectedValue 0.4992449 0.2485677 0.5 0.25 SampleCov Cov 0.0502652 -0.026085 0.05 -0.025 -0.026085 0.0393922 -0.025 0.0375
For further details about sampling from the Dirichlet distribution, see Kotz, Balakrishnan, and Johnson (2000, p. 448); Gentle (2003, p. 205); or Devroye (1986, p. 593).
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.