From 2aca14f0c9c9501a09d51256104146f9707ea2f0 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Mon, 8 Jun 2020 00:48:28 +0200 Subject: [PATCH] ex-1: add Moyal functions --- ex-1/moyal.c | 39 +++++++++++++++++++++++++++++++++++++++ ex-1/moyal.h | 23 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 ex-1/moyal.c create mode 100644 ex-1/moyal.h diff --git a/ex-1/moyal.c b/ex-1/moyal.c new file mode 100644 index 0000000..f478ec7 --- /dev/null +++ b/ex-1/moyal.c @@ -0,0 +1,39 @@ +/* This file contains functions to + * compute the Moyal distribution + * PDF, CDF and QDF functions. + */ + +#include + + +/* 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, void* 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, void* params) { + double mu = params[0]; + double sigma = params[1]; + double z = (x - mu)/sigma; + return 2*(1 - gsl_cdf_gaussian_P(exp(-x/2))); +} + + +/* The quantile function (inverse CDF) of the Moyal + * distribution. + */ +double moyal_qdf(double p0, void* params) { + double mu = params[0]; + double sigma = params[1]; + double z = -2*log(gsl_cdf_gaussian_Pinv(1 - p/2)); + return z*sigma + mu; +} diff --git a/ex-1/moyal.h b/ex-1/moyal.h new file mode 100644 index 0000000..44774fe --- /dev/null +++ b/ex-1/moyal.h @@ -0,0 +1,23 @@ +/* This file contains functions to + * compute the Moyal distribution + * PDF, CDF and QDF functions. + */ + +#pragma once + +/* 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, void* params); + + +/* The cumulative function of the Moyal distribution. + */ +double moyal_cdf(double x, void* params); + + +/* The quantile function (inverse CDF) of the Moyal + * distribution. + */ +double moyal_qdf(double p0, void* params);