tor  0.4.2.1-alpha-dev
prob_distr.h
Go to the documentation of this file.
1 
8 #ifndef TOR_PROB_DISTR_H
9 #define TOR_PROB_DISTR_H
10 
11 #include "lib/cc/compat_compiler.h"
12 #include "lib/cc/torint.h"
14 
18 struct dist {
19  const struct dist_ops *ops;
20 };
21 
42 #define DIST_BASE(OPS) { .ops = (OPS) }
43 
56 #ifdef __COVERITY__
57 #define TYPE_CHECK_OBJ(OPS, OBJ, TYPE) 0
58 #else
59 #define TYPE_CHECK_OBJ(OPS, OBJ, TYPE) \
60  (0*sizeof(&(OBJ) - (const TYPE *)&(OBJ)))
61 #endif /* defined(__COVERITY__) */
62 
108 #define DIST_BASE_TYPED(OPS, OBJ, TYPE) \
109  DIST_BASE((OPS) + TYPE_CHECK_OBJ(OPS,OBJ,TYPE))
110 
116 const char *dist_name(const struct dist *);
117 double dist_sample(const struct dist *);
118 double dist_cdf(const struct dist *, double x);
119 double dist_sf(const struct dist *, double x);
120 double dist_icdf(const struct dist *, double p);
121 double dist_isf(const struct dist *, double p);
122 
128 struct dist_ops {
129  const char *name;
130  double (*sample)(const struct dist *);
131  double (*cdf)(const struct dist *, double x);
132  double (*sf)(const struct dist *, double x);
133  double (*icdf)(const struct dist *, double p);
134  double (*isf)(const struct dist *, double p);
135 };
136 
137 /* Geometric distribution on positive number of trials before first success */
138 
139 struct geometric {
140  struct dist base;
141  double p; /* success probability */
142 };
143 
144 extern const struct dist_ops geometric_ops;
145 
146 #define GEOMETRIC(OBJ) \
147  DIST_BASE_TYPED(&geometric_ops, OBJ, struct geometric)
148 
149 /* Pareto distribution */
150 
151 struct genpareto {
152  struct dist base;
153  double mu;
154  double sigma;
155  double xi;
156 };
157 
158 extern const struct dist_ops genpareto_ops;
159 
160 #define GENPARETO(OBJ) \
161  DIST_BASE_TYPED(&genpareto_ops, OBJ, struct genpareto)
162 
163 /* Weibull distribution */
164 
165 struct weibull {
166  struct dist base;
167  double lambda;
168  double k;
169 };
170 
171 extern const struct dist_ops weibull_ops;
172 
173 #define WEIBULL(OBJ) \
174  DIST_BASE_TYPED(&weibull_ops, OBJ, struct weibull)
175 
176 /* Log-logistic distribution */
177 
178 struct log_logistic {
179  struct dist base;
180  double alpha;
181  double beta;
182 };
183 
184 extern const struct dist_ops log_logistic_ops;
185 
186 #define LOG_LOGISTIC(OBJ) \
187  DIST_BASE_TYPED(&log_logistic_ops, OBJ, struct log_logistic)
188 
189 /* Logistic distribution */
190 
191 struct logistic {
192  struct dist base;
193  double mu;
194  double sigma;
195 };
196 
197 extern const struct dist_ops logistic_ops;
198 
199 #define LOGISTIC(OBJ) \
200  DIST_BASE_TYPED(&logistic_ops, OBJ, struct logistic)
201 
202 /* Uniform distribution */
203 
204 struct uniform {
205  struct dist base;
206  double a;
207  double b;
208 };
209 
210 extern const struct dist_ops uniform_ops;
211 
212 #define UNIFORM(OBJ) \
213  DIST_BASE_TYPED(&uniform_ops, OBJ, struct uniform)
214 
217 #ifdef PROB_DISTR_PRIVATE
218 
219 STATIC double logithalf(double p0);
220 STATIC double logit(double p);
221 
222 STATIC double random_uniform_01(void);
223 
224 STATIC double logistic(double x);
225 STATIC double cdf_logistic(double x, double mu, double sigma);
226 STATIC double sf_logistic(double x, double mu, double sigma);
227 STATIC double icdf_logistic(double p, double mu, double sigma);
228 STATIC double isf_logistic(double p, double mu, double sigma);
229 STATIC double sample_logistic(uint32_t s, double t, double p0);
230 
231 STATIC double cdf_log_logistic(double x, double alpha, double beta);
232 STATIC double sf_log_logistic(double x, double alpha, double beta);
233 STATIC double icdf_log_logistic(double p, double alpha, double beta);
234 STATIC double isf_log_logistic(double p, double alpha, double beta);
235 STATIC double sample_log_logistic(uint32_t s, double p0);
236 
237 STATIC double cdf_weibull(double x, double lambda, double k);
238 STATIC double sf_weibull(double x, double lambda, double k);
239 STATIC double icdf_weibull(double p, double lambda, double k);
240 STATIC double isf_weibull(double p, double lambda, double k);
241 STATIC double sample_weibull(uint32_t s, double p0, double lambda, double k);
242 
243 STATIC double sample_uniform_interval(double p0, double a, double b);
244 
245 STATIC double cdf_genpareto(double x, double mu, double sigma, double xi);
246 STATIC double sf_genpareto(double x, double mu, double sigma, double xi);
247 STATIC double icdf_genpareto(double p, double mu, double sigma, double xi);
248 STATIC double isf_genpareto(double p, double mu, double sigma, double xi);
249 STATIC double sample_genpareto(uint32_t s, double p0, double xi);
250 
251 #endif /* defined(PROB_DISTR_PRIVATE) */
252 
253 #endif /* !defined(TOR_PROB_DISTR_H) */
double dist_cdf(const struct dist *, double x)
Definition: prob_distr.c:1341
STATIC double logithalf(double p0)
Definition: prob_distr.c:358
STATIC double sf_logistic(double x, double mu, double sigma)
Definition: prob_distr.c:521
STATIC double cdf_genpareto(double x, double mu, double sigma, double xi)
Definition: prob_distr.c:790
const char * dist_name(const struct dist *)
Definition: prob_distr.c:1327
STATIC double isf_genpareto(double p, double mu, double sigma, double xi)
Definition: prob_distr.c:897
STATIC double isf_logistic(double p, double mu, double sigma)
Definition: prob_distr.c:543
STATIC double icdf_genpareto(double p, double mu, double sigma, double xi)
Definition: prob_distr.c:851
STATIC double logit(double p)
Definition: prob_distr.c:278
STATIC double logistic(double x)
Definition: prob_distr.c:191
double dist_sf(const struct dist *, double x)
Definition: prob_distr.c:1348
STATIC double sf_weibull(double x, double lambda, double k)
Definition: prob_distr.c:724
STATIC double sample_log_logistic(uint32_t s, double p0)
Definition: prob_distr.c:1158
double dist_icdf(const struct dist *, double p)
Definition: prob_distr.c:1355
Integer definitions used throughout Tor.
STATIC double icdf_log_logistic(double p, double alpha, double beta)
Definition: prob_distr.c:673
STATIC double sf_log_logistic(double x, double alpha, double beta)
Definition: prob_distr.c:657
STATIC double cdf_weibull(double x, double lambda, double k)
Definition: prob_distr.c:709
STATIC double cdf_logistic(double x, double mu, double sigma)
Definition: prob_distr.c:509
STATIC double sf_genpareto(double x, double mu, double sigma, double xi)
Definition: prob_distr.c:834
STATIC double cdf_log_logistic(double x, double alpha, double beta)
Definition: prob_distr.c:596
Utility macros to handle different features and behavior in different compilers.
STATIC double sample_uniform_interval(double p0, double a, double b)
Definition: prob_distr.c:939
STATIC double icdf_logistic(double p, double mu, double sigma)
Definition: prob_distr.c:532
STATIC double isf_weibull(double p, double lambda, double k)
Definition: prob_distr.c:750
STATIC double sample_genpareto(uint32_t s, double p0, double xi)
Definition: prob_distr.c:1229
Macros to implement mocking and selective exposure for the test code.
STATIC double random_uniform_01(void)
Definition: prob_distr.c:451
STATIC double sample_logistic(uint32_t s, double t, double p0)
Definition: prob_distr.c:1092
STATIC double isf_log_logistic(double p, double alpha, double beta)
Definition: prob_distr.c:684
double dist_isf(const struct dist *, double p)
Definition: prob_distr.c:1362
STATIC double sample_weibull(uint32_t s, double p0, double lambda, double k)
Definition: prob_distr.c:1217
STATIC double icdf_weibull(double p, double lambda, double k)
Definition: prob_distr.c:737