prob_distr.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /** Assign the right ops to dist.dist_ops */
  18. #define DIST_BASE(OPS) { .ops = (OPS) }
  19. /** A compile-time type-checking macro for use with DIST_BASE_TYPED. */
  20. #ifdef __COVERITY___
  21. /* Disable type-checking if coverity is enabled, since they don't like it */
  22. #define TYPE_CHECK_OBJ(OPS, OBJ, TYPE) 0
  23. #else
  24. #define TYPE_CHECK_OBJ(OPS, OBJ, TYPE) \
  25. (0*sizeof(&(OBJ) - (const TYPE *)&(OBJ)))
  26. #endif
  27. /** Macro to initialize a distribution with the right OPS, while making sure
  28. * that OBJ is of the right TYPE */
  29. #define DIST_BASE_TYPED(OPS, OBJ, TYPE) \
  30. DIST_BASE((OPS) + TYPE_CHECK_OBJ(OPS,OBJ,TYPE))
  31. const char *dist_name(const struct dist *);
  32. double dist_sample(const struct dist *);
  33. double dist_cdf(const struct dist *, double x);
  34. double dist_sf(const struct dist *, double x);
  35. double dist_icdf(const struct dist *, double p);
  36. double dist_isf(const struct dist *, double p);
  37. struct dist_ops {
  38. const char *name;
  39. double (*sample)(const struct dist *);
  40. double (*cdf)(const struct dist *, double x);
  41. double (*sf)(const struct dist *, double x);
  42. double (*icdf)(const struct dist *, double p);
  43. double (*isf)(const struct dist *, double p);
  44. };
  45. /* Geometric distribution on positive number of trials before first success */
  46. struct geometric {
  47. struct dist base;
  48. double p; /* success probability */
  49. };
  50. extern const struct dist_ops geometric_ops;
  51. #define GEOMETRIC(OBJ) \
  52. DIST_BASE_TYPED(&geometric_ops, OBJ, struct geometric)
  53. /* Pareto distribution */
  54. struct genpareto {
  55. struct dist base;
  56. double mu;
  57. double sigma;
  58. double xi;
  59. };
  60. extern const struct dist_ops genpareto_ops;
  61. #define GENPARETO(OBJ) \
  62. DIST_BASE_TYPED(&genpareto_ops, OBJ, struct genpareto)
  63. /* Weibull distribution */
  64. struct weibull {
  65. struct dist base;
  66. double lambda;
  67. double k;
  68. };
  69. extern const struct dist_ops weibull_ops;
  70. #define WEIBULL(OBJ) \
  71. DIST_BASE_TYPED(&weibull_ops, OBJ, struct weibull)
  72. /* Log-logistic distribution */
  73. struct log_logistic {
  74. struct dist base;
  75. double alpha;
  76. double beta;
  77. };
  78. extern const struct dist_ops log_logistic_ops;
  79. #define LOG_LOGISTIC(OBJ) \
  80. DIST_BASE_TYPED(&log_logistic_ops, OBJ, struct log_logistic)
  81. /* Logistic distribution */
  82. struct logistic {
  83. struct dist base;
  84. double mu;
  85. double sigma;
  86. };
  87. extern const struct dist_ops logistic_ops;
  88. #define LOGISTIC(OBJ) \
  89. DIST_BASE_TYPED(&logistic_ops, OBJ, struct logistic)
  90. /* Uniform distribution */
  91. struct uniform {
  92. struct dist base;
  93. double a;
  94. double b;
  95. };
  96. extern const struct dist_ops uniform_ops;
  97. #define UNIFORM(OBJ) \
  98. DIST_BASE_TYPED(&uniform_ops, OBJ, struct uniform)
  99. /** Only by unittests */
  100. #ifdef PROB_DISTR_PRIVATE
  101. STATIC double logithalf(double p0);
  102. STATIC double logit(double p);
  103. STATIC double random_uniform_01(void);
  104. STATIC double logistic(double x);
  105. STATIC double cdf_logistic(double x, double mu, double sigma);
  106. STATIC double sf_logistic(double x, double mu, double sigma);
  107. STATIC double icdf_logistic(double p, double mu, double sigma);
  108. STATIC double isf_logistic(double p, double mu, double sigma);
  109. STATIC double sample_logistic(uint32_t s, double t, double p0);
  110. STATIC double cdf_log_logistic(double x, double alpha, double beta);
  111. STATIC double sf_log_logistic(double x, double alpha, double beta);
  112. STATIC double icdf_log_logistic(double p, double alpha, double beta);
  113. STATIC double isf_log_logistic(double p, double alpha, double beta);
  114. STATIC double sample_log_logistic(uint32_t s, double p0);
  115. STATIC double cdf_weibull(double x, double lambda, double k);
  116. STATIC double sf_weibull(double x, double lambda, double k);
  117. STATIC double icdf_weibull(double p, double lambda, double k);
  118. STATIC double isf_weibull(double p, double lambda, double k);
  119. STATIC double sample_weibull(uint32_t s, double p0, double lambda, double k);
  120. STATIC double sample_uniform_interval(double p0, double a, double b);
  121. STATIC double cdf_genpareto(double x, double mu, double sigma, double xi);
  122. STATIC double sf_genpareto(double x, double mu, double sigma, double xi);
  123. STATIC double icdf_genpareto(double p, double mu, double sigma, double xi);
  124. STATIC double isf_genpareto(double p, double mu, double sigma, double xi);
  125. STATIC double sample_genpareto(uint32_t s, double p0, double xi);
  126. #endif
  127. #endif