Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 correlation (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.

like image 688
Grzenio Avatar asked Dec 13 '22 00:12

Grzenio


2 Answers

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.

like image 188
dangerstat Avatar answered Dec 28 '22 22:12

dangerstat


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).

like image 42
Alexandre C. Avatar answered Dec 29 '22 00:12

Alexandre C.