1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
# mode: run
# tag: cpp, cpp11
from libcpp.random cimport mt19937, mt19937_64, random_device, uniform_int_distribution, \
uniform_real_distribution, bernoulli_distribution, binomial_distribution, \
geometric_distribution, negative_binomial_distribution, poisson_distribution, \
exponential_distribution, gamma_distribution, weibull_distribution, \
extreme_value_distribution, normal_distribution, lognormal_distribution, \
chi_squared_distribution, cauchy_distribution, fisher_f_distribution, student_t_distribution
from libc.float cimport DBL_MAX as DBL_MAX_
DBL_MAX = DBL_MAX_
def mt19937_seed_test():
"""
>>> print(mt19937_seed_test())
1608637542
"""
cdef mt19937 gen = mt19937(42)
return gen()
def mt19937_reseed_test():
"""
>>> print(mt19937_reseed_test())
1608637542
"""
cdef mt19937 gen
gen.seed(42)
return gen()
def mt19937_min_max():
"""
>>> x, y = mt19937_min_max()
>>> print(x)
0
>>> print(y) # 2 ** 32 - 1 because mt19937 is 32 bit.
4294967295
"""
cdef mt19937 gen
return gen.min(), gen.max()
def mt19937_discard(z):
"""
>>> x, y = mt19937_discard(13)
>>> print(x)
1972458954
>>> print(y)
1972458954
"""
cdef mt19937 gen = mt19937(42)
# Throw away z random numbers.
gen.discard(z)
a = gen()
# Iterate over z random numbers.
gen.seed(42)
for _ in range(z + 1):
b = gen()
return a, b
def mt19937_64_seed_test():
"""
>>> print(mt19937_64_seed_test())
13930160852258120406
"""
cdef mt19937_64 gen = mt19937_64(42)
return gen()
def mt19937_64_reseed_test():
"""
>>> print(mt19937_64_reseed_test())
13930160852258120406
"""
cdef mt19937_64 gen
gen.seed(42)
return gen()
def mt19937_64_min_max():
"""
>>> x, y = mt19937_64_min_max()
>>> print(x)
0
>>> print(y) # 2 ** 64 - 1 because mt19937_64 is 64 bit.
18446744073709551615
"""
cdef mt19937_64 gen
return gen.min(), gen.max()
def mt19937_64_discard(z):
"""
>>> x, y = mt19937_64_discard(13)
>>> print(x)
11756813601242511406
>>> print(y)
11756813601242511406
"""
cdef mt19937_64 gen = mt19937_64(42)
# Throw away z random numbers.
gen.discard(z)
a = gen()
# Iterate over z random numbers.
gen.seed(42)
for _ in range(z + 1):
b = gen()
return a, b
ctypedef fused any_dist:
uniform_int_distribution[int]
uniform_real_distribution[double]
bernoulli_distribution
binomial_distribution[int]
geometric_distribution[int]
negative_binomial_distribution[int]
poisson_distribution[int]
exponential_distribution[double]
gamma_distribution[double]
weibull_distribution[double]
extreme_value_distribution[double]
normal_distribution[double]
lognormal_distribution[double]
chi_squared_distribution[double]
cauchy_distribution[double]
fisher_f_distribution[double]
student_t_distribution[double]
cdef sample_or_range(any_dist dist, bint sample):
"""
This helper function returns a sample if `sample` is truthy and the range of the distribution
if `sample` is falsy. We use a fused type to avoid duplicating the conditional statement in each
distribution test.
"""
cdef random_device rd
if sample:
dist(mt19937(rd()))
else:
return dist.min(), dist.max()
def uniform_int_distribution_test(a, b, sample=True):
"""
>>> uniform_int_distribution_test(2, 3)
>>> uniform_int_distribution_test(5, 9, False)
(5, 9)
"""
cdef uniform_int_distribution[int] dist = uniform_int_distribution[int](a, b)
return sample_or_range[uniform_int_distribution[int]](dist, sample)
def uniform_real_distribution_test(a, b, sample=True):
"""
>>> x = uniform_real_distribution_test(4, 5)
>>> uniform_real_distribution_test(3, 8, False)
(3.0, 8.0)
"""
cdef uniform_real_distribution[double] dist = uniform_real_distribution[double](a, b)
return sample_or_range[uniform_real_distribution[double]](dist, sample)
def bernoulli_distribution_test(proba, sample=True):
"""
>>> bernoulli_distribution_test(0.2)
>>> bernoulli_distribution_test(0.7, False)
(False, True)
"""
cdef bernoulli_distribution dist = bernoulli_distribution(proba)
return sample_or_range[bernoulli_distribution](dist, sample)
def binomial_distribution_test(n, proba, sample=True):
"""
>>> binomial_distribution_test(10, 0.7)
>>> binomial_distribution_test(75, 0.3, False)
(0, 75)
"""
cdef binomial_distribution[int] dist = binomial_distribution[int](n, proba)
return sample_or_range[binomial_distribution[int]](dist, sample)
def geometric_distribution_test(proba, sample=True):
"""
>>> geometric_distribution_test(.4)
>>> geometric_distribution_test(0.2, False) # 2147483647 = 2 ** 32 - 1
(0, 2147483647)
"""
cdef geometric_distribution[int] dist = geometric_distribution[int](proba)
return sample_or_range[geometric_distribution[int]](dist, sample)
def negative_binomial_distribution_test(n, p, sample=True):
"""
>>> negative_binomial_distribution_test(5, .1)
>>> negative_binomial_distribution_test(10, 0.2, False) # 2147483647 = 2 ** 32 - 1
(0, 2147483647)
"""
cdef negative_binomial_distribution[int] dist = negative_binomial_distribution[int](n, p)
return sample_or_range[negative_binomial_distribution[int]](dist, sample)
def poisson_distribution_test(rate, sample=True):
"""
>>> poisson_distribution_test(7)
>>> poisson_distribution_test(7, False) # 2147483647 = 2 ** 32 - 1
(0, 2147483647)
"""
cdef poisson_distribution[int] dist = poisson_distribution[int](rate)
return sample_or_range[poisson_distribution[int]](dist, sample)
def exponential_distribution_test(rate, sample=True):
"""
>>> x = exponential_distribution_test(6)
>>> l, u = exponential_distribution_test(1, False)
>>> l
0.0
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef exponential_distribution[double] dist = exponential_distribution[double](rate)
return sample_or_range[exponential_distribution[double]](dist, sample)
def gamma_distribution_test(shape, scale, sample=True):
"""
>>> gamma_distribution_test(3, 4)
>>> l, u = gamma_distribution_test(1, 1, False)
>>> l
0.0
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef gamma_distribution[double] dist = gamma_distribution[double](shape, scale)
return sample_or_range[gamma_distribution[double]](dist, sample)
def weibull_distribution_test(shape, scale, sample=True):
"""
>>> weibull_distribution_test(3, 2)
>>> l, u = weibull_distribution_test(1, 1, False)
>>> l
0.0
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef weibull_distribution[double] dist = weibull_distribution[double](shape, scale)
return sample_or_range[weibull_distribution[double]](dist, sample)
def extreme_value_distribution_test(shape, scale, sample=True):
"""
>>> extreme_value_distribution_test(3, 0.1)
>>> l, u = extreme_value_distribution_test(1, 1, False)
>>> l == -DBL_MAX or l == -float("inf")
True
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef extreme_value_distribution[double] dist = extreme_value_distribution[double](shape, scale)
return sample_or_range[extreme_value_distribution[double]](dist, sample)
def normal_distribution_test(loc, scale, sample=True):
"""
>>> normal_distribution_test(3, 2)
>>> l, u = normal_distribution_test(1, 1, False)
>>> l == -DBL_MAX or l == -float("inf")
True
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef normal_distribution[double] dist = normal_distribution[double](loc, scale)
return sample_or_range[normal_distribution[double]](dist, sample)
def lognormal_distribution_test(loc, scale, sample=True):
"""
>>> lognormal_distribution_test(3, 2)
>>> l, u = lognormal_distribution_test(1, 1, False)
>>> l
0.0
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef lognormal_distribution[double] dist = lognormal_distribution[double](loc, scale)
return sample_or_range[lognormal_distribution[double]](dist, sample)
def chi_squared_distribution_test(dof, sample=True):
"""
>>> x = chi_squared_distribution_test(9)
>>> l, u = chi_squared_distribution_test(5, False)
>>> l
0.0
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef chi_squared_distribution[double] dist = chi_squared_distribution[double](dof)
return sample_or_range[chi_squared_distribution[double]](dist, sample)
def cauchy_distribution_test(loc, scale, sample=True):
"""
>>> cauchy_distribution_test(3, 9)
>>> l, u = cauchy_distribution_test(1, 1, False)
>>> l == -DBL_MAX or l == -float("inf")
True
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef cauchy_distribution[double] dist = cauchy_distribution[double](loc, scale)
return sample_or_range[cauchy_distribution[double]](dist, sample)
def fisher_f_distribution_test(m, n, sample=True):
"""
>>> x = fisher_f_distribution_test(9, 11)
>>> l, u = fisher_f_distribution_test(1, 1, False)
>>> l
0.0
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef fisher_f_distribution[double] dist = fisher_f_distribution[double](m, n)
return sample_or_range[fisher_f_distribution[double]](dist, sample)
def student_t_distribution_test(dof, sample=True):
"""
>>> x = student_t_distribution_test(13)
>>> l, u = student_t_distribution_test(1, False)
>>> l == -DBL_MAX or l == -float("inf")
True
>>> u == DBL_MAX or u == float("inf")
True
"""
cdef student_t_distribution[double] dist = student_t_distribution[double](dof)
return sample_or_range[student_t_distribution[double]](dist, sample)
|