How can I generate random samples from bivariate normal and student T distibutions in C++?
what is the best approach to generate random samples from bivariate normal and student T distributions? In both cases sigma is one, mean 0 - so the only parameter I am really interested in is cor开发者_如何转开发relation (and degrees of freedom for student t). I need to have the solution in C++, so I can't unfortunately use already implemented functions from MatLab or Mathematica.
You can use the GNU GSL libraries. See here for Bivariate normal:
http://www.gnu.org/software/gsl/manual/html_node/The-Bivariate-Gaussian-Distribution.html
and Student's t-distribution here:
http://www.gnu.org/software/gsl/manual/html_node/The-t_002ddistribution.html
They are straight forward to use.
For a bivariate normal with covariance unity and zero mean, just draw two univariate normals.
If you want to draw a bivariate normal with means (m1, m2), standard deviations (s1, s2) and correlation rho, then draw two unit univariate normals X and Y and set
u = m1 + s1 * X
v = m2 + s2 * (rho X + sqrt(1 - rho^2) Y)
Then u and v are distributed as you wish.
For the Student T, you have to draw a normal variate N and a chi^2 variate V. Then, N / sqrt(V) has T distribution.
To draw the chi^2, you should use a package. Or have a look at Numerical Recipes chapter 7 for how to draw from a Gamma distribution (xhi^2 is a special case of Gamma).
You should take a look at the Boost libraries random distributions - see http://www.boost.org/doc/libs/1_41_0/libs/random/random-distributions.html. I've found them very easy to use, once you wrap your head around their basic concepts. Unfortunately, I don't know enough about statistics to tell you whether they will exactly meet your needs.
精彩评论