prob_distr.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. struct dist_ops {
  19. const char *name;
  20. double (*sample)(const struct dist *);
  21. double (*cdf)(const struct dist *, double x);
  22. double (*sf)(const struct dist *, double x);
  23. double (*icdf)(const struct dist *, double p);
  24. double (*isf)(const struct dist *, double p);
  25. };
  26. /* Geometric distribution */
  27. double geometric_sample(double p);
  28. /* Pareto distribution */
  29. struct genpareto {
  30. struct dist base;
  31. double mu;
  32. double sigma;
  33. double xi;
  34. };
  35. double genpareto_sample(const struct dist *dist);
  36. double genpareto_cdf(const struct dist *dist, double x);
  37. double genpareto_sf(const struct dist *dist, double x);
  38. double genpareto_icdf(const struct dist *dist, double p);
  39. double genpareto_isf(const struct dist *dist, double p);
  40. extern const struct dist_ops genpareto_ops;
  41. /* Weibull distribution */
  42. struct weibull {
  43. struct dist base;
  44. double lambda;
  45. double k;
  46. };
  47. double weibull_sample(const struct dist *dist);
  48. double weibull_cdf(const struct dist *dist, double x);
  49. double weibull_sf(const struct dist *dist, double x);
  50. double weibull_icdf(const struct dist *dist, double p);
  51. double weibull_isf(const struct dist *dist, double p);
  52. extern const struct dist_ops weibull_ops;
  53. /* Log-logistic distribution */
  54. struct log_logistic {
  55. struct dist base;
  56. double alpha;
  57. double beta;
  58. };
  59. double log_logistic_sample(const struct dist *dist);
  60. double log_logistic_cdf(const struct dist *dist, double x);
  61. double log_logistic_sf(const struct dist *dist, double x);
  62. double log_logistic_icdf(const struct dist *dist, double p);
  63. double log_logistic_isf(const struct dist *dist, double p);
  64. extern const struct dist_ops log_logistic_ops;
  65. /* Logistic distribution */
  66. struct logistic {
  67. struct dist base;
  68. double mu;
  69. double sigma;
  70. };
  71. double logistic_sample(const struct dist *dist);
  72. double logistic_cdf(const struct dist *dist, double x);
  73. double logistic_sf(const struct dist *dist, double x);
  74. double logistic_icdf(const struct dist *dist, double p);
  75. double logistic_isf(const struct dist *dist, double p);
  76. extern const struct dist_ops logistic_ops;
  77. /* Uniform distribution */
  78. struct uniform {
  79. struct dist base;
  80. double a;
  81. double b;
  82. };
  83. double uniform_sample(const struct dist *dist);
  84. double uniform_cdf(const struct dist *dist, double x);
  85. double uniform_sf(const struct dist *dist, double x);
  86. double uniform_icdf(const struct dist *dist, double p);
  87. double uniform_isf(const struct dist *dist, double p);
  88. extern const struct dist_ops uniform_ops;
  89. /** Only by unittests */
  90. #ifdef PROB_DISTR_PRIVATE
  91. STATIC double logithalf(double p0);
  92. STATIC double logit(double p);
  93. STATIC double random_uniform_01(void);
  94. STATIC double logistic(double x);
  95. STATIC double cdf_logistic(double x, double mu, double sigma);
  96. STATIC double sf_logistic(double x, double mu, double sigma);
  97. STATIC double icdf_logistic(double p, double mu, double sigma);
  98. STATIC double isf_logistic(double p, double mu, double sigma);
  99. STATIC double sample_logistic(uint32_t s, double t, double p0);
  100. STATIC double cdf_log_logistic(double x, double alpha, double beta);
  101. STATIC double sf_log_logistic(double x, double alpha, double beta);
  102. STATIC double icdf_log_logistic(double p, double alpha, double beta);
  103. STATIC double isf_log_logistic(double p, double alpha, double beta);
  104. STATIC double sample_log_logistic(uint32_t s, double p0);
  105. STATIC double cdf_weibull(double x, double lambda, double k);
  106. STATIC double sf_weibull(double x, double lambda, double k);
  107. STATIC double icdf_weibull(double p, double lambda, double k);
  108. STATIC double isf_weibull(double p, double lambda, double k);
  109. STATIC double sample_weibull(uint32_t s, double p0, double lambda, double k);
  110. STATIC double sample_uniform_interval(double p0, double a, double b);
  111. STATIC double cdf_genpareto(double x, double mu, double sigma, double xi);
  112. STATIC double sf_genpareto(double x, double mu, double sigma, double xi);
  113. STATIC double icdf_genpareto(double p, double mu, double sigma, double xi);
  114. STATIC double isf_genpareto(double p, double mu, double sigma, double xi);
  115. STATIC double sample_genpareto(uint32_t s, double p0, double xi);
  116. #endif
  117. #endif