3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <gsl/gsl_math.h>
23 #include <gsl/gsl_rng.h>
24 #include <gsl/gsl_randist.h>
26 /* The Bivariate Gaussian probability distribution is
28 p(x,y) dxdy = (1/(2 pi sigma_x sigma_y sqrt(c)))
29 exp(-((x/sigma_x)^2 + (y/sigma_y)^2 - 2 r (x/sigma_x)(y/sigma_y))/2c) dxdy
35 gsl_ran_bivariate_gaussian (const gsl_rng * r,
36 double sigma_x, double sigma_y, double rho,
39 double u, v, r2, scale;
43 /* choose x,y in uniform square (-1,-1) to (+1,+1) */
45 u = -1 + 2 * gsl_rng_uniform (r);
46 v = -1 + 2 * gsl_rng_uniform (r);
48 /* see if it is in the unit circle */
51 while (r2 > 1.0 || r2 == 0);
53 scale = sqrt (-2.0 * log (r2) / r2);
55 *x = sigma_x * u * scale;
56 *y = sigma_y * (rho * u + sqrt(1 - rho*rho) * v) * scale;
60 gsl_ran_bivariate_gaussian_pdf (const double x, const double y,
61 const double sigma_x, const double sigma_y,
64 double u = x / sigma_x ;
65 double v = y / sigma_y ;
66 double c = 1 - rho*rho ;
67 double p = (1 / (2 * M_PI * sigma_x * sigma_y * sqrt(c)))
68 * exp (-(u * u - 2 * rho * u * v + v * v) / (2 * c));