Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / randist / bigauss.c
1 /* randist/bigauss.c
2  * 
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19
20 #include <config.h>
21 #include <math.h>
22 #include <gsl/gsl_math.h>
23 #include <gsl/gsl_rng.h>
24 #include <gsl/gsl_randist.h>
25
26 /* The Bivariate Gaussian probability distribution is 
27
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 
30
31    where c = 1-r^2
32 */
33
34 void
35 gsl_ran_bivariate_gaussian (const gsl_rng * r, 
36                             double sigma_x, double sigma_y, double rho,
37                             double *x, double *y)
38 {
39   double u, v, r2, scale;
40
41   do
42     {
43       /* choose x,y in uniform square (-1,-1) to (+1,+1) */
44
45       u = -1 + 2 * gsl_rng_uniform (r);
46       v = -1 + 2 * gsl_rng_uniform (r);
47
48       /* see if it is in the unit circle */
49       r2 = u * u + v * v;
50     }
51   while (r2 > 1.0 || r2 == 0);
52
53   scale = sqrt (-2.0 * log (r2) / r2);
54
55   *x = sigma_x * u * scale;
56   *y = sigma_y * (rho * u + sqrt(1 - rho*rho) * v) * scale;
57 }
58
59 double
60 gsl_ran_bivariate_gaussian_pdf (const double x, const double y, 
61                                 const double sigma_x, const double sigma_y,
62                                 const double rho)
63 {
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));
69   return p;
70 }