Implement and test probability distributions used by WTF-PAD.
[tor.git] / src / lib / math / prob_distr.h
blobc2fd6c74b30ad4e380c731bbb52795e89e24d88c
2 /**
3 * \file prob_distr.h
5 * \brief Header for prob_distr.c
6 **/
8 #ifndef TOR_PROB_DISTR_H
9 #define TOR_PROB_DISTR_H
11 #include "lib/cc/compat_compiler.h"
12 #include "lib/cc/torint.h"
13 #include "lib/testsupport/testsupport.h"
15 /**
16 * Container for distribution parameters for sampling, CDF, &c.
18 struct dist {
19 const struct dist_ops *ops;
22 #define DIST_BASE(OPS) { .ops = (OPS) }
24 struct dist_ops {
25 const char *name;
26 double (*sample)(const struct dist *);
27 double (*cdf)(const struct dist *, double x);
28 double (*sf)(const struct dist *, double x);
29 double (*icdf)(const struct dist *, double p);
30 double (*isf)(const struct dist *, double p);
33 /* Geometric distribution */
35 double geometric_sample(double p);
37 /* Pareto distribution */
39 struct genpareto {
40 struct dist base;
41 double mu;
42 double sigma;
43 double xi;
46 double genpareto_sample(const struct dist *dist);
47 double genpareto_cdf(const struct dist *dist, double x);
48 double genpareto_sf(const struct dist *dist, double x);
49 double genpareto_icdf(const struct dist *dist, double p);
50 double genpareto_isf(const struct dist *dist, double p);
52 extern const struct dist_ops genpareto_ops;
54 /* Weibull distribution */
56 struct weibull {
57 struct dist base;
58 double lambda;
59 double k;
62 double weibull_sample(const struct dist *dist);
63 double weibull_cdf(const struct dist *dist, double x);
64 double weibull_sf(const struct dist *dist, double x);
65 double weibull_icdf(const struct dist *dist, double p);
66 double weibull_isf(const struct dist *dist, double p);
68 extern const struct dist_ops weibull_ops;
70 /* Log-logistic distribution */
72 struct log_logistic {
73 struct dist base;
74 double alpha;
75 double beta;
78 double log_logistic_sample(const struct dist *dist);
79 double log_logistic_cdf(const struct dist *dist, double x);
80 double log_logistic_sf(const struct dist *dist, double x);
81 double log_logistic_icdf(const struct dist *dist, double p);
82 double log_logistic_isf(const struct dist *dist, double p);
84 extern const struct dist_ops log_logistic_ops;
86 /* Logistic distribution */
88 struct logistic {
89 struct dist base;
90 double mu;
91 double sigma;
94 double logistic_sample(const struct dist *dist);
95 double logistic_cdf(const struct dist *dist, double x);
96 double logistic_sf(const struct dist *dist, double x);
97 double logistic_icdf(const struct dist *dist, double p);
98 double logistic_isf(const struct dist *dist, double p);
100 extern const struct dist_ops logistic_ops;
102 /* Uniform distribution */
104 struct uniform {
105 struct dist base;
106 double a;
107 double b;
110 double uniform_sample(const struct dist *dist);
111 double uniform_cdf(const struct dist *dist, double x);
112 double uniform_sf(const struct dist *dist, double x);
113 double uniform_icdf(const struct dist *dist, double p);
114 double uniform_isf(const struct dist *dist, double p);
116 extern const struct dist_ops uniform_ops;
118 /** Only by unittests */
120 #ifdef PROB_DISTR_PRIVATE
122 STATIC double logithalf(double p0);
123 STATIC double logit(double p);
125 STATIC double random_uniform_01(void);
127 STATIC double logistic(double x);
128 STATIC double cdf_logistic(double x, double mu, double sigma);
129 STATIC double sf_logistic(double x, double mu, double sigma);
130 STATIC double icdf_logistic(double p, double mu, double sigma);
131 STATIC double isf_logistic(double p, double mu, double sigma);
132 STATIC double sample_logistic(uint32_t s, double t, double p0);
134 STATIC double cdf_log_logistic(double x, double alpha, double beta);
135 STATIC double sf_log_logistic(double x, double alpha, double beta);
136 STATIC double icdf_log_logistic(double p, double alpha, double beta);
137 STATIC double isf_log_logistic(double p, double alpha, double beta);
138 STATIC double sample_log_logistic(uint32_t s, double p0);
140 STATIC double cdf_weibull(double x, double lambda, double k);
141 STATIC double sf_weibull(double x, double lambda, double k);
142 STATIC double icdf_weibull(double p, double lambda, double k);
143 STATIC double isf_weibull(double p, double lambda, double k);
144 STATIC double sample_weibull(uint32_t s, double p0, double lambda, double k);
146 STATIC double sample_uniform_interval(double p0, double a, double b);
148 STATIC double cdf_genpareto(double x, double mu, double sigma, double xi);
149 STATIC double sf_genpareto(double x, double mu, double sigma, double xi);
150 STATIC double icdf_genpareto(double p, double mu, double sigma, double xi);
151 STATIC double isf_genpareto(double p, double mu, double sigma, double xi);
152 STATIC double sample_genpareto(uint32_t s, double p0, double xi);
154 #endif
156 #endif