prob_distr.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * \file prob_distr.h
  3. *
  4. * \brief Header for prob_distr.c
  5. **/
  6. #ifndef TOR_PROB_DISTR_H
  7. #define TOR_PROB_DISTR_H
  8. #include "lib/cc/compat_compiler.h"
  9. #include "lib/cc/torint.h"
  10. #include "lib/testsupport/testsupport.h"
  11. /**
  12. * Container for distribution parameters for sampling, CDF, &c.
  13. */
  14. struct dist {
  15. const struct dist_ops *ops;
  16. };
  17. #define DIST_BASE(OPS) { .ops = (OPS) }
  18. #define DIST_BASE_TYPED(OPS, OBJ, TYPE) \
  19. DIST_BASE((OPS) + 0*sizeof(&(OBJ) - (const TYPE *)&(OBJ)))
  20. const char *dist_name(const struct dist *);
  21. double dist_sample(const struct dist *);
  22. double dist_cdf(const struct dist *, double x);
  23. double dist_sf(const struct dist *, double x);
  24. double dist_icdf(const struct dist *, double p);
  25. double dist_isf(const struct dist *, double p);
  26. struct dist_ops {
  27. const char *name;
  28. double (*sample)(const struct dist *);
  29. double (*cdf)(const struct dist *, double x);
  30. double (*sf)(const struct dist *, double x);
  31. double (*icdf)(const struct dist *, double p);
  32. double (*isf)(const struct dist *, double p);
  33. };
  34. /* Geometric distribution on positive number of trials before first success */
  35. struct geometric {
  36. struct dist base;
  37. double p; /* success probability */
  38. };
  39. extern const struct dist_ops geometric_ops;
  40. #define GEOMETRIC(OBJ) \
  41. DIST_BASE_TYPED(&geometric_ops, OBJ, struct geometric)
  42. /* Pareto distribution */
  43. struct genpareto {
  44. struct dist base;
  45. double mu;
  46. double sigma;
  47. double xi;
  48. };
  49. extern const struct dist_ops genpareto_ops;
  50. #define GENPARETO(OBJ) \
  51. DIST_BASE_TYPED(&genpareto_ops, OBJ, struct genpareto)
  52. /* Weibull distribution */
  53. struct weibull {
  54. struct dist base;
  55. double lambda;
  56. double k;
  57. };
  58. extern const struct dist_ops weibull_ops;
  59. #define WEIBULL(OBJ) \
  60. DIST_BASE_TYPED(&weibull_ops, OBJ, struct weibull)
  61. /* Log-logistic distribution */
  62. struct log_logistic {
  63. struct dist base;
  64. double alpha;
  65. double beta;
  66. };
  67. extern const struct dist_ops log_logistic_ops;
  68. #define LOG_LOGISTIC(OBJ) \
  69. DIST_BASE_TYPED(&log_logistic_ops, OBJ, struct log_logistic)
  70. /* Logistic distribution */
  71. struct logistic {
  72. struct dist base;
  73. double mu;
  74. double sigma;
  75. };
  76. extern const struct dist_ops logistic_ops;
  77. #define LOGISTIC(OBJ) \
  78. DIST_BASE_TYPED(&logistic_ops, OBJ, struct logistic)
  79. /* Uniform distribution */
  80. struct uniform {
  81. struct dist base;
  82. double a;
  83. double b;
  84. };
  85. extern const struct dist_ops uniform_ops;
  86. #define UNIFORM(OBJ) \
  87. DIST_BASE_TYPED(&uniform_ops, OBJ, struct uniform)
  88. /** Only by unittests */
  89. #ifdef PROB_DISTR_PRIVATE
  90. STATIC double logithalf(double p0);
  91. STATIC double logit(double p);
  92. STATIC double random_uniform_01(void);
  93. STATIC double logistic(double x);
  94. STATIC double cdf_logistic(double x, double mu, double sigma);
  95. STATIC double sf_logistic(double x, double mu, double sigma);
  96. STATIC double icdf_logistic(double p, double mu, double sigma);
  97. STATIC double isf_logistic(double p, double mu, double sigma);
  98. STATIC double sample_logistic(uint32_t s, double t, double p0);
  99. STATIC double cdf_log_logistic(double x, double alpha, double beta);
  100. STATIC double sf_log_logistic(double x, double alpha, double beta);
  101. STATIC double icdf_log_logistic(double p, double alpha, double beta);
  102. STATIC double isf_log_logistic(double p, double alpha, double beta);
  103. STATIC double sample_log_logistic(uint32_t s, double p0);
  104. STATIC double cdf_weibull(double x, double lambda, double k);
  105. STATIC double sf_weibull(double x, double lambda, double k);
  106. STATIC double icdf_weibull(double p, double lambda, double k);
  107. STATIC double isf_weibull(double p, double lambda, double k);
  108. STATIC double sample_weibull(uint32_t s, double p0, double lambda, double k);
  109. STATIC double sample_uniform_interval(double p0, double a, double b);
  110. STATIC double cdf_genpareto(double x, double mu, double sigma, double xi);
  111. STATIC double sf_genpareto(double x, double mu, double sigma, double xi);
  112. STATIC double icdf_genpareto(double p, double mu, double sigma, double xi);
  113. STATIC double isf_genpareto(double p, double mu, double sigma, double xi);
  114. STATIC double sample_genpareto(uint32_t s, double p0, double xi);
  115. #endif
  116. #endif