distributions.odin 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package rand
  2. import "core:math"
  3. float64_uniform :: float64_range
  4. float32_uniform :: float32_range
  5. // Triangular Distribution
  6. // See: http://wikipedia.org/wiki/Triangular_distribution
  7. float64_triangular :: proc(lo, hi: f64, mode: Maybe(f64), r: ^Rand = nil) -> f64 {
  8. if hi-lo == 0 {
  9. return lo
  10. }
  11. lo, hi := lo, hi
  12. u := float64(r)
  13. c := f64(0.5) if mode == nil else clamp((mode.?-lo) / (hi-lo), 0, 1)
  14. if u > c {
  15. u = 1-u
  16. c = 1-c
  17. lo, hi = hi, lo
  18. }
  19. return lo + (hi - lo) * math.sqrt(u * c)
  20. }
  21. // Triangular Distribution
  22. // See: http://wikipedia.org/wiki/Triangular_distribution
  23. float32_triangular :: proc(lo, hi: f32, mode: Maybe(f32), r: ^Rand = nil) -> f32 {
  24. if hi-lo == 0 {
  25. return lo
  26. }
  27. lo, hi := lo, hi
  28. u := float32(r)
  29. c := f32(0.5) if mode == nil else clamp((mode.?-lo) / (hi-lo), 0, 1)
  30. if u > c {
  31. u = 1-u
  32. c = 1-c
  33. lo, hi = hi, lo
  34. }
  35. return lo + (hi - lo) * math.sqrt(u * c)
  36. }
  37. // Normal/Gaussian Distribution
  38. float64_normal :: proc(mean, stddev: f64, r: ^Rand = nil) -> f64 {
  39. return norm_float64(r) * stddev + mean
  40. }
  41. // Normal/Gaussian Distribution
  42. float32_normal :: proc(mean, stddev: f32, r: ^Rand = nil) -> f32 {
  43. return f32(float64_normal(f64(mean), f64(stddev), r))
  44. }
  45. // Log Normal Distribution
  46. float64_log_normal :: proc(mean, stddev: f64, r: ^Rand = nil) -> f64 {
  47. return math.exp(float64_normal(mean, stddev, r))
  48. }
  49. // Log Normal Distribution
  50. float32_log_normal :: proc(mean, stddev: f32, r: ^Rand = nil) -> f32 {
  51. return f32(float64_log_normal(f64(mean), f64(stddev), r))
  52. }
  53. // Exponential Distribution
  54. // `lambda` is 1.0/(desired mean). It should be non-zero.
  55. // Return values range from
  56. // 0 to positive infinity if lambda > 0
  57. // negative infinity to 0 if lambda <= 0
  58. float64_exponential :: proc(lambda: f64, r: ^Rand = nil) -> f64 {
  59. return - math.ln(1 - float64(r)) / lambda
  60. }
  61. // Exponential Distribution
  62. // `lambda` is 1.0/(desired mean). It should be non-zero.
  63. // Return values range from
  64. // 0 to positive infinity if lambda > 0
  65. // negative infinity to 0 if lambda <= 0
  66. float32_exponential :: proc(lambda: f32, r: ^Rand = nil) -> f32 {
  67. return f32(float64_exponential(f64(lambda), r))
  68. }
  69. // Gamma Distribution (NOT THE GAMMA FUNCTION)
  70. //
  71. // Required: alpha > 0 and beta > 0
  72. //
  73. // math.pow(x, alpha-1) * math.exp(-x / beta)
  74. // pdf(x) = --------------------------------------------
  75. // math.gamma(alpha) * math.pow(beta, alpha)
  76. //
  77. // mean is alpha*beta, variance is math.pow(alpha*beta, 2)
  78. float64_gamma :: proc(alpha, beta: f64, r: ^Rand = nil) -> f64 {
  79. if alpha <= 0 || beta <= 0 {
  80. panic(#procedure + ": alpha and beta must be > 0.0")
  81. }
  82. LOG4 :: 1.3862943611198906188344642429163531361510002687205105082413600189
  83. SG_MAGIC_CONST :: 2.5040773967762740733732583523868748412194809812852436493487
  84. switch {
  85. case alpha > 1:
  86. // R.C.H. Cheng, "The generation of Gamma variables with non-integral shape parameters", Applied Statistics, (1977), 26, No. 1, p71-74
  87. ainv := math.sqrt(2 * alpha - 1)
  88. bbb := alpha - LOG4
  89. ccc := alpha + ainv
  90. for {
  91. u1 := float64(r)
  92. if !(1e-7 < u1 && u1 < 0.9999999) {
  93. continue
  94. }
  95. u2 := 1 - float64(r)
  96. v := math.ln(u1 / (1 - u1)) / ainv
  97. x := alpha * math.exp(v)
  98. z := u1 * u1 * u2
  99. t := bbb + ccc*v - x
  100. if t + SG_MAGIC_CONST - 4.5 * z >= 0 || t >= math.ln(z) {
  101. return x * beta
  102. }
  103. }
  104. case alpha == 1:
  105. // float64_exponential(1/beta)
  106. return -math.ln(1 - float64(r)) * beta
  107. case:
  108. // ALGORITHM GS of Statistical Computing - Kennedy & Gentle
  109. x: f64
  110. for {
  111. u := float64(r)
  112. b := (math.e + alpha) / math.e
  113. p := b * u
  114. if p <= 1 {
  115. x = math.pow(p, 1/alpha)
  116. } else {
  117. x = -math.ln((b - p) / alpha)
  118. }
  119. u1 := float64(r)
  120. if p > 1 {
  121. if u1 <= math.pow(x, alpha-1) {
  122. break
  123. }
  124. } else if u1 <= math.exp(-x) {
  125. break
  126. }
  127. }
  128. return x * beta
  129. }
  130. }
  131. // Gamma Distribution (NOT THE GAMMA FUNCTION)
  132. //
  133. // Required: alpha > 0 and beta > 0
  134. //
  135. // math.pow(x, alpha-1) * math.exp(-x / beta)
  136. // pdf(x) = --------------------------------------------
  137. // math.gamma(alpha) * math.pow(beta, alpha)
  138. //
  139. // mean is alpha*beta, variance is math.pow(alpha*beta, 2)
  140. float32_gamma :: proc(alpha, beta: f32, r: ^Rand = nil) -> f32 {
  141. return f32(float64_gamma(f64(alpha), f64(beta), r))
  142. }
  143. // Beta Distribution
  144. //
  145. // Required: alpha > 0 and beta > 0
  146. //
  147. // Return values range between 0 and 1
  148. float64_beta :: proc(alpha, beta: f64, r: ^Rand = nil) -> f64 {
  149. if alpha <= 0 || beta <= 0 {
  150. panic(#procedure + ": alpha and beta must be > 0.0")
  151. }
  152. // Knuth Vol 2 Ed 3 pg 134 "the beta distribution"
  153. y := float64_gamma(alpha, 1.0, r)
  154. if y != 0 {
  155. return y / (y + float64_gamma(beta, 1.0, r))
  156. }
  157. return 0
  158. }
  159. // Beta Distribution
  160. //
  161. // Required: alpha > 0 and beta > 0
  162. //
  163. // Return values range between 0 and 1
  164. float32_beta :: proc(alpha, beta: f32, r: ^Rand = nil) -> f32 {
  165. return f32(float64_beta(f64(alpha), f64(beta), r))
  166. }
  167. // Pareto distribution, `alpha` is the shape parameter.
  168. // https://wikipedia.org/wiki/Pareto_distribution
  169. float64_pareto :: proc(alpha: f64, r: ^Rand = nil) -> f64 {
  170. return math.pow(1 - float64(r), -1.0 / alpha)
  171. }
  172. // Pareto distribution, `alpha` is the shape parameter.
  173. // https://wikipedia.org/wiki/Pareto_distribution
  174. float32_pareto :: proc(alpha, beta: f32, r: ^Rand = nil) -> f32 {
  175. return f32(float64_pareto(f64(alpha), r))
  176. }
  177. // Weibull distribution, `alpha` is the scale parameter, `beta` is the shape parameter.
  178. float64_weibull :: proc(alpha, beta: f64, r: ^Rand = nil) -> f64 {
  179. u := 1 - float64(r)
  180. return alpha * math.pow(-math.ln(u), 1.0/beta)
  181. }
  182. // Weibull distribution, `alpha` is the scale parameter, `beta` is the shape parameter.
  183. float32_weibull :: proc(alpha, beta: f32, r: ^Rand = nil) -> f32 {
  184. return f32(float64_weibull(f64(alpha), f64(beta), r))
  185. }
  186. // Circular Data (von Mises) Distribution
  187. // `mean_angle` is the in mean angle between 0 and 2pi radians
  188. // `kappa` is the concentration parameter which must be >= 0
  189. // When `kappa` is zero, the Distribution is a uniform Distribution over the range 0 to 2pi
  190. float64_von_mises :: proc(mean_angle, kappa: f64, r: ^Rand = nil) -> f64 {
  191. // Fisher, N.I., "Statistical Analysis of Circular Data", Cambridge University Press, 1993.
  192. mu := mean_angle
  193. if kappa <= 1e-6 {
  194. return math.TAU * float64(r)
  195. }
  196. s := 0.5 / kappa
  197. t := s + math.sqrt(1 + s*s)
  198. z: f64
  199. for {
  200. u1 := float64(r)
  201. z = math.cos(math.TAU * 0.5 * u1)
  202. d := z / (t + z)
  203. u2 := float64(r)
  204. if u2 < 1 - d*d || u2 <= (1-d)*math.exp(d) {
  205. break
  206. }
  207. }
  208. q := 1.0 / t
  209. f := (q + z) / (1 + q*z)
  210. u3 := float64(r)
  211. if u3 > 0.5 {
  212. return math.mod(mu + math.acos(f), math.TAU)
  213. } else {
  214. return math.mod(mu - math.acos(f), math.TAU)
  215. }
  216. }
  217. // Circular Data (von Mises) Distribution
  218. // `mean_angle` is the in mean angle between 0 and 2pi radians
  219. // `kappa` is the concentration parameter which must be >= 0
  220. // When `kappa` is zero, the Distribution is a uniform Distribution over the range 0 to 2pi
  221. float32_von_mises :: proc(mean_angle, kappa: f32, r: ^Rand = nil) -> f32 {
  222. return f32(float64_von_mises(f64(mean_angle), f64(kappa), r))
  223. }
  224. // Cauchy-Lorentz Distribution
  225. // `x_0` is the location, `gamma` is the scale where `gamma` > 0
  226. float64_cauchy_lorentz :: proc(x_0, gamma: f64, r: ^Rand = nil) -> f64 {
  227. assert(gamma > 0)
  228. // Calculated from the inverse CDF
  229. return math.tan(math.PI * (float64(r) - 0.5))*gamma + x_0
  230. }
  231. // Cauchy-Lorentz Distribution
  232. // `x_0` is the location, `gamma` is the scale where `gamma` > 0
  233. float32_cauchy_lorentz :: proc(x_0, gamma: f32, r: ^Rand = nil) -> f32 {
  234. return f32(float64_cauchy_lorentz(f64(x_0), f64(gamma), r))
  235. }
  236. // Log Cauchy-Lorentz Distribution
  237. // `x_0` is the location, `gamma` is the scale where `gamma` > 0
  238. float64_log_cauchy_lorentz :: proc(x_0, gamma: f64, r: ^Rand = nil) -> f64 {
  239. assert(gamma > 0)
  240. return math.exp(math.tan(math.PI * (float64(r) - 0.5))*gamma + x_0)
  241. }
  242. // Log Cauchy-Lorentz Distribution
  243. // `x_0` is the location, `gamma` is the scale where `gamma` > 0
  244. float32_log_cauchy_lorentz :: proc(x_0, gamma: f32, r: ^Rand = nil) -> f32 {
  245. return f32(float64_log_cauchy_lorentz(f64(x_0), f64(gamma), r))
  246. }
  247. // Laplace Distribution
  248. // `b` is the scale where `b` > 0
  249. float64_laplace :: proc(mean, b: f64, r: ^Rand = nil) -> f64 {
  250. assert(b > 0)
  251. p := float64(r)-0.5
  252. return -math.sign(p)*math.ln(1 - 2*abs(p))*b + mean
  253. }
  254. // Laplace Distribution
  255. // `b` is the scale where `b` > 0
  256. float32_laplace :: proc(mean, b: f32, r: ^Rand = nil) -> f32 {
  257. return f32(float64_laplace(f64(mean), f64(b), r))
  258. }
  259. // Gompertz Distribution
  260. // `eta` is the shape, `b` is the scale
  261. // Both `eta` and `b` must be > 0
  262. float64_gompertz :: proc(eta, b: f64, r: ^Rand = nil) -> f64 {
  263. if eta <= 0 || b <= 0 {
  264. panic(#procedure + ": eta and b must be > 0.0")
  265. }
  266. p := float64(r)
  267. return math.ln(1 - math.ln(1 - p)/eta)/b
  268. }
  269. // Gompertz Distribution
  270. // `eta` is the shape, `b` is the scale
  271. // Both `eta` and `b` must be > 0
  272. float32_gompertz :: proc(eta, b: f32, r: ^Rand = nil) -> f32 {
  273. return f32(float64_gompertz(f64(eta), f64(b), r))
  274. }