analistica/ex-1/moyal.c

44 lines
1023 B
C
Raw Normal View History

2020-06-08 00:48:28 +02:00
/* This file contains functions to
* compute the Moyal distribution
* PDF, CDF and QDF functions.
*/
#include <gsl/gsl_cdf.h>
2020-06-08 00:48:28 +02:00
#include <gsl/gsl_sf.h>
#include <math.h>
2020-06-08 00:48:28 +02:00
/* The Moyal PDF.
*
* This is a wrapper needed by `moyal_cdf` and
2020-06-08 00:48:28 +02:00
* other optimisation functions because the GSL
* routines expect a function with parameters.
*/
double moyal_pdf(double x, double* params) {
2020-06-08 00:48:28 +02:00
double mu = params[0];
double sigma = params[1];
double z = (x - mu)/sigma;
return exp(-(z + exp(-z))/2) /sqrt(2*M_PI) /sigma;
}
/* The cumulative function of the Moyal distribution.
*/
double moyal_cdf(double x, double* params) {
2020-06-08 00:48:28 +02:00
double mu = params[0];
double sigma = params[1];
double z = (x - mu)/sigma;
return 2 * (1 - gsl_cdf_gaussian_P(exp(-z/2), 1));
2020-06-08 00:48:28 +02:00
}
/* The quantile function (inverse CDF) of the Moyal
* distribution.
*/
double moyal_qdf(double p0, double* params) {
2020-06-08 00:48:28 +02:00
double mu = params[0];
double sigma = params[1];
double z = -2 * log(gsl_cdf_gaussian_Pinv(1 - p0/2, 1));
2020-06-08 00:48:28 +02:00
return z*sigma + mu;
}