/* This file contains functions to
 * compute the Moyal distribution
 * PDF, CDF and QDF functions.
 */

#include <gsl/gsl_cdf.h>
#include <gsl/gsl_sf.h>
#include <math.h>


/* The Moyal PDF.
 *
 * This is a wrapper needed by `moyal_cdf` and
 * other optimisation functions because the GSL
 * routines expect a function with parameters.
 */
double moyal_pdf(double x, double* params) {
  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) {
  double mu = params[0];
  double sigma = params[1];
  double z = (x - mu)/sigma;
  return 2 * (1 - gsl_cdf_gaussian_P(exp(-z/2), 1));
}


/* The quantile function (inverse CDF) of the Moyal
 * distribution.
 */
double moyal_qdf(double p0, double* params) {
  double mu = params[0];
  double sigma = params[1];
  double z = -2 * log(gsl_cdf_gaussian_Pinv(1 - p0/2, 1));
  return z*sigma + mu;
}
