| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include <esl_config.h> |
| 31 |
|
| 32 |
#include <math.h> |
| 33 |
|
| 34 |
#include "easel.h" |
| 35 |
#include "esl_normal.h" |
| 36 |
#include "esl_stats.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
double |
| 53 |
esl_normal_pdf(double x, double mu, double sigma) |
| 54 |
{ |
| 55 |
double z; |
| 56 |
|
| 57 |
z = (x - mu) / sigma; |
| 58 |
return exp(-z*z*0.5) / (sigma * sqrt(2. * eslCONST_PI)); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
double |
| 71 |
esl_normal_logpdf(double x, double mu, double sigma) |
| 72 |
{ |
| 73 |
double z; |
| 74 |
|
| 75 |
z = (x - mu) / sigma; |
| 76 |
return (-z*z*0.5) - log(sigma) - log(sqrt(2.*eslCONST_PI)); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
double |
| 89 |
esl_normal_cdf(double x, double mu, double sigma) |
| 90 |
{ |
| 91 |
double z; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
z = (x - mu) / sigma; |
| 99 |
return 0.5 * erfc(-1. * z / sqrt(2.)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
double |
| 113 |
esl_normal_surv(double x, double mu, double sigma) |
| 114 |
{ |
| 115 |
double z = (x - mu) / sigma; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
return 0.5 * erfc( z / sqrt(2.)); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
double |
| 130 |
esl_normal_generic_pdf(double x, void *params) |
| 131 |
{ |
| 132 |
double *v = (double *) params; |
| 133 |
return esl_normal_pdf(x, v[0], v[1]); |
| 134 |
} |
| 135 |
|
| 136 |
double |
| 137 |
esl_normal_generic_cdf(double x, void *params) |
| 138 |
{ |
| 139 |
double *v = (double *) params; |
| 140 |
return esl_normal_cdf(x, v[0], v[1]); |
| 141 |
} |
| 142 |
|
| 143 |
double |
| 144 |
esl_normal_generic_surv(double x, void *params) |
| 145 |
{ |
| 146 |
double *v = (double *) params; |
| 147 |
return esl_normal_surv(x, v[0], v[1]); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#ifdef eslNORMAL_TESTDRIVE |
| 155 |
static int |
| 156 |
utest_pdf(void) |
| 157 |
{ |
| 158 |
char msg[] = "gaussian PDF unit test failed"; |
| 159 |
double mu = 0.; |
| 160 |
double sigma = 1.; |
| 161 |
double delta = 0.01; |
| 162 |
double x; |
| 163 |
double newpdf, lastpdf; |
| 164 |
double cdf; |
| 165 |
|
| 166 |
|
| 167 |
for (cdf = 0., x = -40.; x < 40.; x += delta) |
| 168 |
cdf += esl_normal_pdf(x, mu, sigma) * delta; |
| 169 |
if (esl_DCompare_old(cdf, 1.0, 1e-9) != eslOK) esl_fatal(msg); |
| 170 |
|
| 171 |
|
| 172 |
x = 0.; |
| 173 |
newpdf = esl_normal_pdf(x, mu, sigma); |
| 174 |
do { |
| 175 |
x += 1.; |
| 176 |
lastpdf = newpdf; |
| 177 |
newpdf = esl_normal_pdf(x, mu, sigma); |
| 178 |
} while (newpdf > 0.); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if (lastpdf > 1e-297 || x < 38.) esl_fatal(msg); |
| 184 |
return eslOK; |
| 185 |
} |
| 186 |
|
| 187 |
static int |
| 188 |
utest_logpdf(void) |
| 189 |
{ |
| 190 |
char msg[] = "gaussian log PDF unit test failed"; |
| 191 |
double mu = 0.; |
| 192 |
double sigma = 1.; |
| 193 |
double delta = 0.01; |
| 194 |
double x; |
| 195 |
double old, new; |
| 196 |
double cdf; |
| 197 |
|
| 198 |
|
| 199 |
for (cdf = 0., x = -40.; x < 40.; x += delta) |
| 200 |
cdf += exp(esl_normal_logpdf(x, mu, sigma)) * delta; |
| 201 |
if (esl_DCompare_old(cdf, 1.0, 1e-9) != eslOK) esl_fatal(msg); |
| 202 |
|
| 203 |
|
| 204 |
for (x = -20.; x < 20.; x += delta) |
| 205 |
{ |
| 206 |
old = esl_normal_pdf (x, mu, sigma); |
| 207 |
new = exp(esl_normal_logpdf(x, mu, sigma)); |
| 208 |
if (esl_DCompare_old(old, new, 1e-9) != eslOK) esl_fatal(msg); |
| 209 |
} |
| 210 |
|
| 211 |
return eslOK; |
| 212 |
} |
| 213 |
|
| 214 |
static int |
| 215 |
utest_cdf(void) |
| 216 |
{ |
| 217 |
char msg[] = "gaussian CDF unit test failed"; |
| 218 |
double mu = 0.; |
| 219 |
double sigma = 1.; |
| 220 |
double x; |
| 221 |
|
| 222 |
x = esl_normal_cdf(mu, mu, sigma); |
| 223 |
if (esl_DCompare_old(x, 0.5, 1e-9) != eslOK) esl_fatal(msg); |
| 224 |
|
| 225 |
x = esl_normal_cdf(99., mu, sigma); |
| 226 |
if (esl_DCompare_old(x, 1.0, 1e-9) != eslOK) esl_fatal(msg); |
| 227 |
|
| 228 |
x = esl_normal_cdf(-99., mu, sigma); |
| 229 |
if (esl_DCompare_old(x, 0.0, 1e-9) != eslOK) esl_fatal(msg); |
| 230 |
|
| 231 |
x = esl_normal_cdf(-30., mu, sigma); |
| 232 |
if (x > 1e-100 || x == 0.) esl_fatal(msg); |
| 233 |
|
| 234 |
return eslOK; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
static int |
| 239 |
utest_surv(void) |
| 240 |
{ |
| 241 |
char msg[] = "gaussian survival unit test failed"; |
| 242 |
double mu = 0.; |
| 243 |
double sigma = 1.; |
| 244 |
double x; |
| 245 |
|
| 246 |
x = esl_normal_surv(mu, mu, sigma); |
| 247 |
if (esl_DCompare_old(x, 0.5, 1e-9) != eslOK) esl_fatal(msg); |
| 248 |
|
| 249 |
x = esl_normal_surv(-99., mu, sigma); |
| 250 |
if (esl_DCompare_old(x, 1.0, 1e-9) != eslOK) esl_fatal(msg); |
| 251 |
|
| 252 |
x = esl_normal_surv(99., mu, sigma); |
| 253 |
if (esl_DCompare_old(x, 0.0, 1e-9) != eslOK) esl_fatal(msg); |
| 254 |
|
| 255 |
x = esl_normal_surv(30., mu, sigma); |
| 256 |
if (x > 1e-100 || x == 0.) esl_fatal(msg); |
| 257 |
|
| 258 |
return eslOK; |
| 259 |
} |
| 260 |
#endif |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#ifdef eslNORMAL_TESTDRIVE |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#include <stdio.h> |
| 273 |
#include <math.h> |
| 274 |
#include "easel.h" |
| 275 |
#include "esl_normal.h" |
| 276 |
|
| 277 |
int |
| 278 |
main(int argc, char **argv) |
| 279 |
{ |
| 280 |
utest_pdf(); |
| 281 |
utest_logpdf(); |
| 282 |
utest_cdf(); |
| 283 |
utest_surv(); |
| 284 |
|
| 285 |
return eslOK; |
| 286 |
} |
| 287 |
#endif |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
#ifdef eslNORMAL_EXAMPLE |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#include <stdio.h> |
| 298 |
#include <math.h> |
| 299 |
|
| 300 |
#include "easel.h" |
| 301 |
#include "esl_getopts.h" |
| 302 |
#include "esl_normal.h" |
| 303 |
|
| 304 |
static ESL_OPTIONS options[] = { |
| 305 |
|
| 306 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 307 |
{ "--mean", eslARG_REAL, "0.0", NULL, NULL, NULL, NULL, NULL, "mean of normal distribution", 0 }, |
| 308 |
{ "--sd", eslARG_REAL, "1.0", NULL, NULL, NULL, NULL, NULL, "s.d. of normal distribution", 0 }, |
| 309 |
{ "--min", eslARG_REAL, "-10.0", NULL, NULL, NULL, NULL, NULL, "minimum for xaxis", 0 }, |
| 310 |
{ "--max", eslARG_REAL, "10.0", NULL, NULL, NULL, NULL, NULL, "maximum for xaxis", 0 }, |
| 311 |
{ "--step", eslARG_REAL, "1.0", NULL, NULL, NULL, NULL, NULL, "step size for xaxis", 0 }, |
| 312 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 313 |
}; |
| 314 |
static char usage[] = "[-options]"; |
| 315 |
static char banner[] = "output a Gaussian histogram"; |
| 316 |
|
| 317 |
int |
| 318 |
main(int argc, char **argv) |
| 319 |
{ |
| 320 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 321 |
double minx = esl_opt_GetReal(go, "--min"); |
| 322 |
double maxx = esl_opt_GetReal(go, "--max"); |
| 323 |
double xstep = esl_opt_GetReal(go, "--step"); |
| 324 |
double mean = esl_opt_GetReal(go, "--mean"); |
| 325 |
double sd = esl_opt_GetReal(go, "--sd"); |
| 326 |
double x; |
| 327 |
double val; |
| 328 |
|
| 329 |
for (x = minx; x < maxx; x += xstep) |
| 330 |
{ |
| 331 |
val = esl_normal_logpdf(x, mean, sd) * xstep; |
| 332 |
printf("%f %g\n", x, val); |
| 333 |
} |
| 334 |
printf("&\n"); |
| 335 |
|
| 336 |
esl_getopts_Destroy(go); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
#endif |
| 340 |
|