星期一, 十二月 05, 2011

Generation of Chi squared or student t distributed random numbers with MKL

The MKL includes 11 random number generators (RNG) with continuous distributions.  RNG for Χ2 and student t are not included.  They can however be obtained with the 11 available RNG.

Χ2 RNG is actually a special case of Gamma RNG. The latter in MKL has 3 parameters, shape α, displacement a, and scale factor β. To generate a random number ~Χ2(ν), just let α=ν/2, a=0, and β=2.

(to be added non central Χ2 RNG)

The student t distribution needs also Gaussian RNG.  If x~N(0,1), y~Χ2(ν), then x/ y ~t(ν).  Below are the sample codes.
const int n = 10000000;
double df, alpha, beta, mean, sigma, a, gauss[n], chisq[n], t[n], scale;
int    method, inc;

method= 0;
df    = 4.;
scale = 1./df;
inc   = 1;
alpha = df/2.;
beta  = 2.;
a     = 0.;
mean  = 0.;
sigma = 1.;
status = vdRngGaussian( method, stream, n, gauss, mean, sigma );
status = vdRngGamma( method, stream, n, chisq, alpha, a, beta );
dscal(&n, &scale, chisq, &inc);  //scales chisq by the 1/df
vdSqrt( n, chisq, chisq );       //square root of chisq
vdDiv( n, gauss, chisq, t );     //generates random t.
These codes utilized the vector calculation of MKL.  Certainly there are also some other methods to generate above ramdom number.  For example, we can use the inverse function of cdf(t).  But again, MKL haven't implemented many (cumulative) density functions.   It will take almost same efforts to assemble a RNG of t.

没有评论: