annotate DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/random/tests/test_random.py @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2a2c65a20a8b
children
rev   line source
Chris@87 1 from __future__ import division, absolute_import, print_function
Chris@87 2
Chris@87 3 import numpy as np
Chris@87 4 from numpy.testing import (
Chris@87 5 TestCase, run_module_suite, assert_, assert_raises, assert_equal,
Chris@87 6 assert_warns)
Chris@87 7 from numpy import random
Chris@87 8 from numpy.compat import asbytes
Chris@87 9 import sys
Chris@87 10
Chris@87 11 class TestSeed(TestCase):
Chris@87 12 def test_scalar(self):
Chris@87 13 s = np.random.RandomState(0)
Chris@87 14 assert_equal(s.randint(1000), 684)
Chris@87 15 s = np.random.RandomState(4294967295)
Chris@87 16 assert_equal(s.randint(1000), 419)
Chris@87 17
Chris@87 18 def test_array(self):
Chris@87 19 s = np.random.RandomState(range(10))
Chris@87 20 assert_equal(s.randint(1000), 468)
Chris@87 21 s = np.random.RandomState(np.arange(10))
Chris@87 22 assert_equal(s.randint(1000), 468)
Chris@87 23 s = np.random.RandomState([0])
Chris@87 24 assert_equal(s.randint(1000), 973)
Chris@87 25 s = np.random.RandomState([4294967295])
Chris@87 26 assert_equal(s.randint(1000), 265)
Chris@87 27
Chris@87 28 def test_invalid_scalar(self):
Chris@87 29 # seed must be a unsigned 32 bit integers
Chris@87 30 assert_raises(TypeError, np.random.RandomState, -0.5)
Chris@87 31 assert_raises(ValueError, np.random.RandomState, -1)
Chris@87 32
Chris@87 33 def test_invalid_array(self):
Chris@87 34 # seed must be a unsigned 32 bit integers
Chris@87 35 assert_raises(TypeError, np.random.RandomState, [-0.5])
Chris@87 36 assert_raises(ValueError, np.random.RandomState, [-1])
Chris@87 37 assert_raises(ValueError, np.random.RandomState, [4294967296])
Chris@87 38 assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296])
Chris@87 39 assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296])
Chris@87 40
Chris@87 41 class TestBinomial(TestCase):
Chris@87 42 def test_n_zero(self):
Chris@87 43 # Tests the corner case of n == 0 for the binomial distribution.
Chris@87 44 # binomial(0, p) should be zero for any p in [0, 1].
Chris@87 45 # This test addresses issue #3480.
Chris@87 46 zeros = np.zeros(2, dtype='int')
Chris@87 47 for p in [0, .5, 1]:
Chris@87 48 assert_(random.binomial(0, p) == 0)
Chris@87 49 np.testing.assert_array_equal(random.binomial(zeros, p), zeros)
Chris@87 50
Chris@87 51 def test_p_is_nan(self):
Chris@87 52 # Issue #4571.
Chris@87 53 assert_raises(ValueError, random.binomial, 1, np.nan)
Chris@87 54
Chris@87 55
Chris@87 56 class TestMultinomial(TestCase):
Chris@87 57 def test_basic(self):
Chris@87 58 random.multinomial(100, [0.2, 0.8])
Chris@87 59
Chris@87 60 def test_zero_probability(self):
Chris@87 61 random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0])
Chris@87 62
Chris@87 63 def test_int_negative_interval(self):
Chris@87 64 assert_(-5 <= random.randint(-5, -1) < -1)
Chris@87 65 x = random.randint(-5, -1, 5)
Chris@87 66 assert_(np.all(-5 <= x))
Chris@87 67 assert_(np.all(x < -1))
Chris@87 68
Chris@87 69 def test_size(self):
Chris@87 70 # gh-3173
Chris@87 71 p = [0.5, 0.5]
Chris@87 72 assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
Chris@87 73 assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
Chris@87 74 assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
Chris@87 75 assert_equal(np.random.multinomial(1, p, [2, 2]).shape, (2, 2, 2))
Chris@87 76 assert_equal(np.random.multinomial(1, p, (2, 2)).shape, (2, 2, 2))
Chris@87 77 assert_equal(np.random.multinomial(1, p, np.array((2, 2))).shape,
Chris@87 78 (2, 2, 2))
Chris@87 79
Chris@87 80 assert_raises(TypeError, np.random.multinomial, 1, p,
Chris@87 81 np.float(1))
Chris@87 82
Chris@87 83
Chris@87 84 class TestSetState(TestCase):
Chris@87 85 def setUp(self):
Chris@87 86 self.seed = 1234567890
Chris@87 87 self.prng = random.RandomState(self.seed)
Chris@87 88 self.state = self.prng.get_state()
Chris@87 89
Chris@87 90 def test_basic(self):
Chris@87 91 old = self.prng.tomaxint(16)
Chris@87 92 self.prng.set_state(self.state)
Chris@87 93 new = self.prng.tomaxint(16)
Chris@87 94 assert_(np.all(old == new))
Chris@87 95
Chris@87 96 def test_gaussian_reset(self):
Chris@87 97 # Make sure the cached every-other-Gaussian is reset.
Chris@87 98 old = self.prng.standard_normal(size=3)
Chris@87 99 self.prng.set_state(self.state)
Chris@87 100 new = self.prng.standard_normal(size=3)
Chris@87 101 assert_(np.all(old == new))
Chris@87 102
Chris@87 103 def test_gaussian_reset_in_media_res(self):
Chris@87 104 # When the state is saved with a cached Gaussian, make sure the
Chris@87 105 # cached Gaussian is restored.
Chris@87 106
Chris@87 107 self.prng.standard_normal()
Chris@87 108 state = self.prng.get_state()
Chris@87 109 old = self.prng.standard_normal(size=3)
Chris@87 110 self.prng.set_state(state)
Chris@87 111 new = self.prng.standard_normal(size=3)
Chris@87 112 assert_(np.all(old == new))
Chris@87 113
Chris@87 114 def test_backwards_compatibility(self):
Chris@87 115 # Make sure we can accept old state tuples that do not have the
Chris@87 116 # cached Gaussian value.
Chris@87 117 old_state = self.state[:-2]
Chris@87 118 x1 = self.prng.standard_normal(size=16)
Chris@87 119 self.prng.set_state(old_state)
Chris@87 120 x2 = self.prng.standard_normal(size=16)
Chris@87 121 self.prng.set_state(self.state)
Chris@87 122 x3 = self.prng.standard_normal(size=16)
Chris@87 123 assert_(np.all(x1 == x2))
Chris@87 124 assert_(np.all(x1 == x3))
Chris@87 125
Chris@87 126 def test_negative_binomial(self):
Chris@87 127 # Ensure that the negative binomial results take floating point
Chris@87 128 # arguments without truncation.
Chris@87 129 self.prng.negative_binomial(0.5, 0.5)
Chris@87 130
Chris@87 131 class TestRandomDist(TestCase):
Chris@87 132 # Make sure the random distrobution return the correct value for a
Chris@87 133 # given seed
Chris@87 134
Chris@87 135 def setUp(self):
Chris@87 136 self.seed = 1234567890
Chris@87 137
Chris@87 138 def test_rand(self):
Chris@87 139 np.random.seed(self.seed)
Chris@87 140 actual = np.random.rand(3, 2)
Chris@87 141 desired = np.array([[0.61879477158567997, 0.59162362775974664],
Chris@87 142 [0.88868358904449662, 0.89165480011560816],
Chris@87 143 [0.4575674820298663, 0.7781880808593471]])
Chris@87 144 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 145
Chris@87 146 def test_randn(self):
Chris@87 147 np.random.seed(self.seed)
Chris@87 148 actual = np.random.randn(3, 2)
Chris@87 149 desired = np.array([[1.34016345771863121, 1.73759122771936081],
Chris@87 150 [1.498988344300628, -0.2286433324536169],
Chris@87 151 [2.031033998682787, 2.17032494605655257]])
Chris@87 152 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 153
Chris@87 154 def test_randint(self):
Chris@87 155 np.random.seed(self.seed)
Chris@87 156 actual = np.random.randint(-99, 99, size=(3, 2))
Chris@87 157 desired = np.array([[31, 3],
Chris@87 158 [-52, 41],
Chris@87 159 [-48, -66]])
Chris@87 160 np.testing.assert_array_equal(actual, desired)
Chris@87 161
Chris@87 162 def test_random_integers(self):
Chris@87 163 np.random.seed(self.seed)
Chris@87 164 actual = np.random.random_integers(-99, 99, size=(3, 2))
Chris@87 165 desired = np.array([[31, 3],
Chris@87 166 [-52, 41],
Chris@87 167 [-48, -66]])
Chris@87 168 np.testing.assert_array_equal(actual, desired)
Chris@87 169
Chris@87 170 def test_random_sample(self):
Chris@87 171 np.random.seed(self.seed)
Chris@87 172 actual = np.random.random_sample((3, 2))
Chris@87 173 desired = np.array([[0.61879477158567997, 0.59162362775974664],
Chris@87 174 [0.88868358904449662, 0.89165480011560816],
Chris@87 175 [0.4575674820298663, 0.7781880808593471]])
Chris@87 176 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 177
Chris@87 178 def test_choice_uniform_replace(self):
Chris@87 179 np.random.seed(self.seed)
Chris@87 180 actual = np.random.choice(4, 4)
Chris@87 181 desired = np.array([2, 3, 2, 3])
Chris@87 182 np.testing.assert_array_equal(actual, desired)
Chris@87 183
Chris@87 184 def test_choice_nonuniform_replace(self):
Chris@87 185 np.random.seed(self.seed)
Chris@87 186 actual = np.random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1])
Chris@87 187 desired = np.array([1, 1, 2, 2])
Chris@87 188 np.testing.assert_array_equal(actual, desired)
Chris@87 189
Chris@87 190 def test_choice_uniform_noreplace(self):
Chris@87 191 np.random.seed(self.seed)
Chris@87 192 actual = np.random.choice(4, 3, replace=False)
Chris@87 193 desired = np.array([0, 1, 3])
Chris@87 194 np.testing.assert_array_equal(actual, desired)
Chris@87 195
Chris@87 196 def test_choice_nonuniform_noreplace(self):
Chris@87 197 np.random.seed(self.seed)
Chris@87 198 actual = np.random.choice(4, 3, replace=False,
Chris@87 199 p=[0.1, 0.3, 0.5, 0.1])
Chris@87 200 desired = np.array([2, 3, 1])
Chris@87 201 np.testing.assert_array_equal(actual, desired)
Chris@87 202
Chris@87 203 def test_choice_noninteger(self):
Chris@87 204 np.random.seed(self.seed)
Chris@87 205 actual = np.random.choice(['a', 'b', 'c', 'd'], 4)
Chris@87 206 desired = np.array(['c', 'd', 'c', 'd'])
Chris@87 207 np.testing.assert_array_equal(actual, desired)
Chris@87 208
Chris@87 209 def test_choice_exceptions(self):
Chris@87 210 sample = np.random.choice
Chris@87 211 assert_raises(ValueError, sample, -1, 3)
Chris@87 212 assert_raises(ValueError, sample, 3., 3)
Chris@87 213 assert_raises(ValueError, sample, [[1, 2], [3, 4]], 3)
Chris@87 214 assert_raises(ValueError, sample, [], 3)
Chris@87 215 assert_raises(ValueError, sample, [1, 2, 3, 4], 3,
Chris@87 216 p=[[0.25, 0.25], [0.25, 0.25]])
Chris@87 217 assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2])
Chris@87 218 assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1])
Chris@87 219 assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4])
Chris@87 220 assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False)
Chris@87 221 assert_raises(ValueError, sample, [1, 2, 3], 2, replace=False,
Chris@87 222 p=[1, 0, 0])
Chris@87 223
Chris@87 224 def test_choice_return_shape(self):
Chris@87 225 p = [0.1, 0.9]
Chris@87 226 # Check scalar
Chris@87 227 assert_(np.isscalar(np.random.choice(2, replace=True)))
Chris@87 228 assert_(np.isscalar(np.random.choice(2, replace=False)))
Chris@87 229 assert_(np.isscalar(np.random.choice(2, replace=True, p=p)))
Chris@87 230 assert_(np.isscalar(np.random.choice(2, replace=False, p=p)))
Chris@87 231 assert_(np.isscalar(np.random.choice([1, 2], replace=True)))
Chris@87 232 assert_(np.random.choice([None], replace=True) is None)
Chris@87 233 a = np.array([1, 2])
Chris@87 234 arr = np.empty(1, dtype=object)
Chris@87 235 arr[0] = a
Chris@87 236 assert_(np.random.choice(arr, replace=True) is a)
Chris@87 237
Chris@87 238 # Check 0-d array
Chris@87 239 s = tuple()
Chris@87 240 assert_(not np.isscalar(np.random.choice(2, s, replace=True)))
Chris@87 241 assert_(not np.isscalar(np.random.choice(2, s, replace=False)))
Chris@87 242 assert_(not np.isscalar(np.random.choice(2, s, replace=True, p=p)))
Chris@87 243 assert_(not np.isscalar(np.random.choice(2, s, replace=False, p=p)))
Chris@87 244 assert_(not np.isscalar(np.random.choice([1, 2], s, replace=True)))
Chris@87 245 assert_(np.random.choice([None], s, replace=True).ndim == 0)
Chris@87 246 a = np.array([1, 2])
Chris@87 247 arr = np.empty(1, dtype=object)
Chris@87 248 arr[0] = a
Chris@87 249 assert_(np.random.choice(arr, s, replace=True).item() is a)
Chris@87 250
Chris@87 251 # Check multi dimensional array
Chris@87 252 s = (2, 3)
Chris@87 253 p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2]
Chris@87 254 assert_(np.random.choice(6, s, replace=True).shape, s)
Chris@87 255 assert_(np.random.choice(6, s, replace=False).shape, s)
Chris@87 256 assert_(np.random.choice(6, s, replace=True, p=p).shape, s)
Chris@87 257 assert_(np.random.choice(6, s, replace=False, p=p).shape, s)
Chris@87 258 assert_(np.random.choice(np.arange(6), s, replace=True).shape, s)
Chris@87 259
Chris@87 260 def test_bytes(self):
Chris@87 261 np.random.seed(self.seed)
Chris@87 262 actual = np.random.bytes(10)
Chris@87 263 desired = asbytes('\x82Ui\x9e\xff\x97+Wf\xa5')
Chris@87 264 np.testing.assert_equal(actual, desired)
Chris@87 265
Chris@87 266 def test_shuffle(self):
Chris@87 267 # Test lists, arrays, and multidimensional versions of both:
Chris@87 268 for conv in [lambda x: x,
Chris@87 269 np.asarray,
Chris@87 270 lambda x: [(i, i) for i in x],
Chris@87 271 lambda x: np.asarray([(i, i) for i in x])]:
Chris@87 272 np.random.seed(self.seed)
Chris@87 273 alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
Chris@87 274 np.random.shuffle(alist)
Chris@87 275 actual = alist
Chris@87 276 desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3])
Chris@87 277 np.testing.assert_array_equal(actual, desired)
Chris@87 278
Chris@87 279 def test_shuffle_flexible(self):
Chris@87 280 # gh-4270
Chris@87 281 arr = [(0, 1), (2, 3)]
Chris@87 282 dt = np.dtype([('a', np.int32, 1), ('b', np.int32, 1)])
Chris@87 283 nparr = np.array(arr, dtype=dt)
Chris@87 284 a, b = nparr[0].copy(), nparr[1].copy()
Chris@87 285 for i in range(50):
Chris@87 286 np.random.shuffle(nparr)
Chris@87 287 assert_(a in nparr)
Chris@87 288 assert_(b in nparr)
Chris@87 289
Chris@87 290 def test_shuffle_masked(self):
Chris@87 291 # gh-3263
Chris@87 292 a = np.ma.masked_values(np.reshape(range(20), (5,4)) % 3 - 1, -1)
Chris@87 293 b = np.ma.masked_values(np.arange(20) % 3 - 1, -1)
Chris@87 294 ma = np.ma.count_masked(a)
Chris@87 295 mb = np.ma.count_masked(b)
Chris@87 296 for i in range(50):
Chris@87 297 np.random.shuffle(a)
Chris@87 298 self.assertEqual(ma, np.ma.count_masked(a))
Chris@87 299 np.random.shuffle(b)
Chris@87 300 self.assertEqual(mb, np.ma.count_masked(b))
Chris@87 301
Chris@87 302 def test_beta(self):
Chris@87 303 np.random.seed(self.seed)
Chris@87 304 actual = np.random.beta(.1, .9, size=(3, 2))
Chris@87 305 desired = np.array(
Chris@87 306 [[1.45341850513746058e-02, 5.31297615662868145e-04],
Chris@87 307 [1.85366619058432324e-06, 4.19214516800110563e-03],
Chris@87 308 [1.58405155108498093e-04, 1.26252891949397652e-04]])
Chris@87 309 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 310
Chris@87 311 def test_binomial(self):
Chris@87 312 np.random.seed(self.seed)
Chris@87 313 actual = np.random.binomial(100.123, .456, size=(3, 2))
Chris@87 314 desired = np.array([[37, 43],
Chris@87 315 [42, 48],
Chris@87 316 [46, 45]])
Chris@87 317 np.testing.assert_array_equal(actual, desired)
Chris@87 318
Chris@87 319 def test_chisquare(self):
Chris@87 320 np.random.seed(self.seed)
Chris@87 321 actual = np.random.chisquare(50, size=(3, 2))
Chris@87 322 desired = np.array([[63.87858175501090585, 68.68407748911370447],
Chris@87 323 [65.77116116901505904, 47.09686762438974483],
Chris@87 324 [72.3828403199695174, 74.18408615260374006]])
Chris@87 325 np.testing.assert_array_almost_equal(actual, desired, decimal=13)
Chris@87 326
Chris@87 327 def test_dirichlet(self):
Chris@87 328 np.random.seed(self.seed)
Chris@87 329 alpha = np.array([51.72840233779265162, 39.74494232180943953])
Chris@87 330 actual = np.random.mtrand.dirichlet(alpha, size=(3, 2))
Chris@87 331 desired = np.array([[[0.54539444573611562, 0.45460555426388438],
Chris@87 332 [0.62345816822039413, 0.37654183177960598]],
Chris@87 333 [[0.55206000085785778, 0.44793999914214233],
Chris@87 334 [0.58964023305154301, 0.41035976694845688]],
Chris@87 335 [[0.59266909280647828, 0.40733090719352177],
Chris@87 336 [0.56974431743975207, 0.43025568256024799]]])
Chris@87 337 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 338
Chris@87 339 def test_dirichlet_size(self):
Chris@87 340 # gh-3173
Chris@87 341 p = np.array([51.72840233779265162, 39.74494232180943953])
Chris@87 342 assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2))
Chris@87 343 assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2))
Chris@87 344 assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2))
Chris@87 345 assert_equal(np.random.dirichlet(p, [2, 2]).shape, (2, 2, 2))
Chris@87 346 assert_equal(np.random.dirichlet(p, (2, 2)).shape, (2, 2, 2))
Chris@87 347 assert_equal(np.random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2))
Chris@87 348
Chris@87 349 assert_raises(TypeError, np.random.dirichlet, p, np.float(1))
Chris@87 350
Chris@87 351 def test_exponential(self):
Chris@87 352 np.random.seed(self.seed)
Chris@87 353 actual = np.random.exponential(1.1234, size=(3, 2))
Chris@87 354 desired = np.array([[1.08342649775011624, 1.00607889924557314],
Chris@87 355 [2.46628830085216721, 2.49668106809923884],
Chris@87 356 [0.68717433461363442, 1.69175666993575979]])
Chris@87 357 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 358
Chris@87 359 def test_f(self):
Chris@87 360 np.random.seed(self.seed)
Chris@87 361 actual = np.random.f(12, 77, size=(3, 2))
Chris@87 362 desired = np.array([[1.21975394418575878, 1.75135759791559775],
Chris@87 363 [1.44803115017146489, 1.22108959480396262],
Chris@87 364 [1.02176975757740629, 1.34431827623300415]])
Chris@87 365 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 366
Chris@87 367 def test_gamma(self):
Chris@87 368 np.random.seed(self.seed)
Chris@87 369 actual = np.random.gamma(5, 3, size=(3, 2))
Chris@87 370 desired = np.array([[24.60509188649287182, 28.54993563207210627],
Chris@87 371 [26.13476110204064184, 12.56988482927716078],
Chris@87 372 [31.71863275789960568, 33.30143302795922011]])
Chris@87 373 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 374
Chris@87 375 def test_geometric(self):
Chris@87 376 np.random.seed(self.seed)
Chris@87 377 actual = np.random.geometric(.123456789, size=(3, 2))
Chris@87 378 desired = np.array([[8, 7],
Chris@87 379 [17, 17],
Chris@87 380 [5, 12]])
Chris@87 381 np.testing.assert_array_equal(actual, desired)
Chris@87 382
Chris@87 383 def test_gumbel(self):
Chris@87 384 np.random.seed(self.seed)
Chris@87 385 actual = np.random.gumbel(loc=.123456789, scale=2.0, size=(3, 2))
Chris@87 386 desired = np.array([[0.19591898743416816, 0.34405539668096674],
Chris@87 387 [-1.4492522252274278, -1.47374816298446865],
Chris@87 388 [1.10651090478803416, -0.69535848626236174]])
Chris@87 389 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 390
Chris@87 391 def test_hypergeometric(self):
Chris@87 392 np.random.seed(self.seed)
Chris@87 393 actual = np.random.hypergeometric(10.1, 5.5, 14, size=(3, 2))
Chris@87 394 desired = np.array([[10, 10],
Chris@87 395 [10, 10],
Chris@87 396 [9, 9]])
Chris@87 397 np.testing.assert_array_equal(actual, desired)
Chris@87 398
Chris@87 399 # Test nbad = 0
Chris@87 400 actual = np.random.hypergeometric(5, 0, 3, size=4)
Chris@87 401 desired = np.array([3, 3, 3, 3])
Chris@87 402 np.testing.assert_array_equal(actual, desired)
Chris@87 403
Chris@87 404 actual = np.random.hypergeometric(15, 0, 12, size=4)
Chris@87 405 desired = np.array([12, 12, 12, 12])
Chris@87 406 np.testing.assert_array_equal(actual, desired)
Chris@87 407
Chris@87 408 # Test ngood = 0
Chris@87 409 actual = np.random.hypergeometric(0, 5, 3, size=4)
Chris@87 410 desired = np.array([0, 0, 0, 0])
Chris@87 411 np.testing.assert_array_equal(actual, desired)
Chris@87 412
Chris@87 413 actual = np.random.hypergeometric(0, 15, 12, size=4)
Chris@87 414 desired = np.array([0, 0, 0, 0])
Chris@87 415 np.testing.assert_array_equal(actual, desired)
Chris@87 416
Chris@87 417 def test_laplace(self):
Chris@87 418 np.random.seed(self.seed)
Chris@87 419 actual = np.random.laplace(loc=.123456789, scale=2.0, size=(3, 2))
Chris@87 420 desired = np.array([[0.66599721112760157, 0.52829452552221945],
Chris@87 421 [3.12791959514407125, 3.18202813572992005],
Chris@87 422 [-0.05391065675859356, 1.74901336242837324]])
Chris@87 423 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 424
Chris@87 425 def test_logistic(self):
Chris@87 426 np.random.seed(self.seed)
Chris@87 427 actual = np.random.logistic(loc=.123456789, scale=2.0, size=(3, 2))
Chris@87 428 desired = np.array([[1.09232835305011444, 0.8648196662399954],
Chris@87 429 [4.27818590694950185, 4.33897006346929714],
Chris@87 430 [-0.21682183359214885, 2.63373365386060332]])
Chris@87 431 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 432
Chris@87 433 def test_lognormal(self):
Chris@87 434 np.random.seed(self.seed)
Chris@87 435 actual = np.random.lognormal(mean=.123456789, sigma=2.0, size=(3, 2))
Chris@87 436 desired = np.array([[16.50698631688883822, 36.54846706092654784],
Chris@87 437 [22.67886599981281748, 0.71617561058995771],
Chris@87 438 [65.72798501792723869, 86.84341601437161273]])
Chris@87 439 np.testing.assert_array_almost_equal(actual, desired, decimal=13)
Chris@87 440
Chris@87 441 def test_logseries(self):
Chris@87 442 np.random.seed(self.seed)
Chris@87 443 actual = np.random.logseries(p=.923456789, size=(3, 2))
Chris@87 444 desired = np.array([[2, 2],
Chris@87 445 [6, 17],
Chris@87 446 [3, 6]])
Chris@87 447 np.testing.assert_array_equal(actual, desired)
Chris@87 448
Chris@87 449 def test_multinomial(self):
Chris@87 450 np.random.seed(self.seed)
Chris@87 451 actual = np.random.multinomial(20, [1/6.]*6, size=(3, 2))
Chris@87 452 desired = np.array([[[4, 3, 5, 4, 2, 2],
Chris@87 453 [5, 2, 8, 2, 2, 1]],
Chris@87 454 [[3, 4, 3, 6, 0, 4],
Chris@87 455 [2, 1, 4, 3, 6, 4]],
Chris@87 456 [[4, 4, 2, 5, 2, 3],
Chris@87 457 [4, 3, 4, 2, 3, 4]]])
Chris@87 458 np.testing.assert_array_equal(actual, desired)
Chris@87 459
Chris@87 460 def test_multivariate_normal(self):
Chris@87 461 np.random.seed(self.seed)
Chris@87 462 mean = (.123456789, 10)
Chris@87 463 # Hmm... not even symmetric.
Chris@87 464 cov = [[1, 0], [1, 0]]
Chris@87 465 size = (3, 2)
Chris@87 466 actual = np.random.multivariate_normal(mean, cov, size)
Chris@87 467 desired = np.array([[[-1.47027513018564449, 10.],
Chris@87 468 [-1.65915081534845532, 10.]],
Chris@87 469 [[-2.29186329304599745, 10.],
Chris@87 470 [-1.77505606019580053, 10.]],
Chris@87 471 [[-0.54970369430044119, 10.],
Chris@87 472 [0.29768848031692957, 10.]]])
Chris@87 473 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 474
Chris@87 475 # Check for default size, was raising deprecation warning
Chris@87 476 actual = np.random.multivariate_normal(mean, cov)
Chris@87 477 desired = np.array([-0.79441224511977482, 10.])
Chris@87 478 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 479
Chris@87 480 # Check that non positive-semidefinite covariance raises warning
Chris@87 481 mean = [0, 0]
Chris@87 482 cov = [[1, 1 + 1e-10], [1 + 1e-10, 1]]
Chris@87 483 assert_warns(RuntimeWarning, np.random.multivariate_normal, mean, cov)
Chris@87 484
Chris@87 485 def test_negative_binomial(self):
Chris@87 486 np.random.seed(self.seed)
Chris@87 487 actual = np.random.negative_binomial(n=100, p=.12345, size=(3, 2))
Chris@87 488 desired = np.array([[848, 841],
Chris@87 489 [892, 611],
Chris@87 490 [779, 647]])
Chris@87 491 np.testing.assert_array_equal(actual, desired)
Chris@87 492
Chris@87 493 def test_noncentral_chisquare(self):
Chris@87 494 np.random.seed(self.seed)
Chris@87 495 actual = np.random.noncentral_chisquare(df=5, nonc=5, size=(3, 2))
Chris@87 496 desired = np.array([[23.91905354498517511, 13.35324692733826346],
Chris@87 497 [31.22452661329736401, 16.60047399466177254],
Chris@87 498 [5.03461598262724586, 17.94973089023519464]])
Chris@87 499 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 500
Chris@87 501 def test_noncentral_f(self):
Chris@87 502 np.random.seed(self.seed)
Chris@87 503 actual = np.random.noncentral_f(dfnum=5, dfden=2, nonc=1,
Chris@87 504 size=(3, 2))
Chris@87 505 desired = np.array([[1.40598099674926669, 0.34207973179285761],
Chris@87 506 [3.57715069265772545, 7.92632662577829805],
Chris@87 507 [0.43741599463544162, 1.1774208752428319]])
Chris@87 508 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 509
Chris@87 510 def test_normal(self):
Chris@87 511 np.random.seed(self.seed)
Chris@87 512 actual = np.random.normal(loc=.123456789, scale=2.0, size=(3, 2))
Chris@87 513 desired = np.array([[2.80378370443726244, 3.59863924443872163],
Chris@87 514 [3.121433477601256, -0.33382987590723379],
Chris@87 515 [4.18552478636557357, 4.46410668111310471]])
Chris@87 516 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 517
Chris@87 518 def test_pareto(self):
Chris@87 519 np.random.seed(self.seed)
Chris@87 520 actual = np.random.pareto(a=.123456789, size=(3, 2))
Chris@87 521 desired = np.array(
Chris@87 522 [[2.46852460439034849e+03, 1.41286880810518346e+03],
Chris@87 523 [5.28287797029485181e+07, 6.57720981047328785e+07],
Chris@87 524 [1.40840323350391515e+02, 1.98390255135251704e+05]])
Chris@87 525 # For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this
Chris@87 526 # matrix differs by 24 nulps. Discussion:
Chris@87 527 # http://mail.scipy.org/pipermail/numpy-discussion/2012-September/063801.html
Chris@87 528 # Consensus is that this is probably some gcc quirk that affects
Chris@87 529 # rounding but not in any important way, so we just use a looser
Chris@87 530 # tolerance on this test:
Chris@87 531 np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30)
Chris@87 532
Chris@87 533 def test_poisson(self):
Chris@87 534 np.random.seed(self.seed)
Chris@87 535 actual = np.random.poisson(lam=.123456789, size=(3, 2))
Chris@87 536 desired = np.array([[0, 0],
Chris@87 537 [1, 0],
Chris@87 538 [0, 0]])
Chris@87 539 np.testing.assert_array_equal(actual, desired)
Chris@87 540
Chris@87 541 def test_poisson_exceptions(self):
Chris@87 542 lambig = np.iinfo('l').max
Chris@87 543 lamneg = -1
Chris@87 544 assert_raises(ValueError, np.random.poisson, lamneg)
Chris@87 545 assert_raises(ValueError, np.random.poisson, [lamneg]*10)
Chris@87 546 assert_raises(ValueError, np.random.poisson, lambig)
Chris@87 547 assert_raises(ValueError, np.random.poisson, [lambig]*10)
Chris@87 548
Chris@87 549 def test_power(self):
Chris@87 550 np.random.seed(self.seed)
Chris@87 551 actual = np.random.power(a=.123456789, size=(3, 2))
Chris@87 552 desired = np.array([[0.02048932883240791, 0.01424192241128213],
Chris@87 553 [0.38446073748535298, 0.39499689943484395],
Chris@87 554 [0.00177699707563439, 0.13115505880863756]])
Chris@87 555 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 556
Chris@87 557 def test_rayleigh(self):
Chris@87 558 np.random.seed(self.seed)
Chris@87 559 actual = np.random.rayleigh(scale=10, size=(3, 2))
Chris@87 560 desired = np.array([[13.8882496494248393, 13.383318339044731],
Chris@87 561 [20.95413364294492098, 21.08285015800712614],
Chris@87 562 [11.06066537006854311, 17.35468505778271009]])
Chris@87 563 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 564
Chris@87 565 def test_standard_cauchy(self):
Chris@87 566 np.random.seed(self.seed)
Chris@87 567 actual = np.random.standard_cauchy(size=(3, 2))
Chris@87 568 desired = np.array([[0.77127660196445336, -6.55601161955910605],
Chris@87 569 [0.93582023391158309, -2.07479293013759447],
Chris@87 570 [-4.74601644297011926, 0.18338989290760804]])
Chris@87 571 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 572
Chris@87 573 def test_standard_exponential(self):
Chris@87 574 np.random.seed(self.seed)
Chris@87 575 actual = np.random.standard_exponential(size=(3, 2))
Chris@87 576 desired = np.array([[0.96441739162374596, 0.89556604882105506],
Chris@87 577 [2.1953785836319808, 2.22243285392490542],
Chris@87 578 [0.6116915921431676, 1.50592546727413201]])
Chris@87 579 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 580
Chris@87 581 def test_standard_gamma(self):
Chris@87 582 np.random.seed(self.seed)
Chris@87 583 actual = np.random.standard_gamma(shape=3, size=(3, 2))
Chris@87 584 desired = np.array([[5.50841531318455058, 6.62953470301903103],
Chris@87 585 [5.93988484943779227, 2.31044849402133989],
Chris@87 586 [7.54838614231317084, 8.012756093271868]])
Chris@87 587 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 588
Chris@87 589 def test_standard_normal(self):
Chris@87 590 np.random.seed(self.seed)
Chris@87 591 actual = np.random.standard_normal(size=(3, 2))
Chris@87 592 desired = np.array([[1.34016345771863121, 1.73759122771936081],
Chris@87 593 [1.498988344300628, -0.2286433324536169],
Chris@87 594 [2.031033998682787, 2.17032494605655257]])
Chris@87 595 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 596
Chris@87 597 def test_standard_t(self):
Chris@87 598 np.random.seed(self.seed)
Chris@87 599 actual = np.random.standard_t(df=10, size=(3, 2))
Chris@87 600 desired = np.array([[0.97140611862659965, -0.08830486548450577],
Chris@87 601 [1.36311143689505321, -0.55317463909867071],
Chris@87 602 [-0.18473749069684214, 0.61181537341755321]])
Chris@87 603 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 604
Chris@87 605 def test_triangular(self):
Chris@87 606 np.random.seed(self.seed)
Chris@87 607 actual = np.random.triangular(left=5.12, mode=10.23, right=20.34,
Chris@87 608 size=(3, 2))
Chris@87 609 desired = np.array([[12.68117178949215784, 12.4129206149193152],
Chris@87 610 [16.20131377335158263, 16.25692138747600524],
Chris@87 611 [11.20400690911820263, 14.4978144835829923]])
Chris@87 612 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 613
Chris@87 614 def test_uniform(self):
Chris@87 615 np.random.seed(self.seed)
Chris@87 616 actual = np.random.uniform(low=1.23, high=10.54, size=(3, 2))
Chris@87 617 desired = np.array([[6.99097932346268003, 6.73801597444323974],
Chris@87 618 [9.50364421400426274, 9.53130618907631089],
Chris@87 619 [5.48995325769805476, 8.47493103280052118]])
Chris@87 620 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 621
Chris@87 622 def test_vonmises(self):
Chris@87 623 np.random.seed(self.seed)
Chris@87 624 actual = np.random.vonmises(mu=1.23, kappa=1.54, size=(3, 2))
Chris@87 625 desired = np.array([[2.28567572673902042, 2.89163838442285037],
Chris@87 626 [0.38198375564286025, 2.57638023113890746],
Chris@87 627 [1.19153771588353052, 1.83509849681825354]])
Chris@87 628 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 629
Chris@87 630 def test_vonmises_small(self):
Chris@87 631 # check infinite loop, gh-4720
Chris@87 632 np.random.seed(self.seed)
Chris@87 633 r = np.random.vonmises(mu=0., kappa=1.1e-8, size=10**6)
Chris@87 634 np.testing.assert_(np.isfinite(r).all())
Chris@87 635
Chris@87 636 def test_wald(self):
Chris@87 637 np.random.seed(self.seed)
Chris@87 638 actual = np.random.wald(mean=1.23, scale=1.54, size=(3, 2))
Chris@87 639 desired = np.array([[3.82935265715889983, 5.13125249184285526],
Chris@87 640 [0.35045403618358717, 1.50832396872003538],
Chris@87 641 [0.24124319895843183, 0.22031101461955038]])
Chris@87 642 np.testing.assert_array_almost_equal(actual, desired, decimal=14)
Chris@87 643
Chris@87 644 def test_weibull(self):
Chris@87 645 np.random.seed(self.seed)
Chris@87 646 actual = np.random.weibull(a=1.23, size=(3, 2))
Chris@87 647 desired = np.array([[0.97097342648766727, 0.91422896443565516],
Chris@87 648 [1.89517770034962929, 1.91414357960479564],
Chris@87 649 [0.67057783752390987, 1.39494046635066793]])
Chris@87 650 np.testing.assert_array_almost_equal(actual, desired, decimal=15)
Chris@87 651
Chris@87 652 def test_zipf(self):
Chris@87 653 np.random.seed(self.seed)
Chris@87 654 actual = np.random.zipf(a=1.23, size=(3, 2))
Chris@87 655 desired = np.array([[66, 29],
Chris@87 656 [1, 1],
Chris@87 657 [3, 13]])
Chris@87 658 np.testing.assert_array_equal(actual, desired)
Chris@87 659
Chris@87 660
Chris@87 661 class TestThread(object):
Chris@87 662 # make sure each state produces the same sequence even in threads
Chris@87 663 def setUp(self):
Chris@87 664 self.seeds = range(4)
Chris@87 665
Chris@87 666 def check_function(self, function, sz):
Chris@87 667 from threading import Thread
Chris@87 668
Chris@87 669 out1 = np.empty((len(self.seeds),) + sz)
Chris@87 670 out2 = np.empty((len(self.seeds),) + sz)
Chris@87 671
Chris@87 672 # threaded generation
Chris@87 673 t = [Thread(target=function, args=(np.random.RandomState(s), o))
Chris@87 674 for s, o in zip(self.seeds, out1)]
Chris@87 675 [x.start() for x in t]
Chris@87 676 [x.join() for x in t]
Chris@87 677
Chris@87 678 # the same serial
Chris@87 679 for s, o in zip(self.seeds, out2):
Chris@87 680 function(np.random.RandomState(s), o)
Chris@87 681
Chris@87 682 # these platforms change x87 fpu precision mode in threads
Chris@87 683 if (np.intp().dtype.itemsize == 4 and
Chris@87 684 (sys.platform == "win32" or
Chris@87 685 sys.platform.startswith("gnukfreebsd"))):
Chris@87 686 np.testing.assert_array_almost_equal(out1, out2)
Chris@87 687 else:
Chris@87 688 np.testing.assert_array_equal(out1, out2)
Chris@87 689
Chris@87 690 def test_normal(self):
Chris@87 691 def gen_random(state, out):
Chris@87 692 out[...] = state.normal(size=10000)
Chris@87 693 self.check_function(gen_random, sz=(10000,))
Chris@87 694
Chris@87 695 def test_exp(self):
Chris@87 696 def gen_random(state, out):
Chris@87 697 out[...] = state.exponential(scale=np.ones((100, 1000)))
Chris@87 698 self.check_function(gen_random, sz=(100, 1000))
Chris@87 699
Chris@87 700 def test_multinomial(self):
Chris@87 701 def gen_random(state, out):
Chris@87 702 out[...] = state.multinomial(10, [1/6.]*6, size=10000)
Chris@87 703 self.check_function(gen_random, sz=(10000,6))
Chris@87 704
Chris@87 705
Chris@87 706 if __name__ == "__main__":
Chris@87 707 run_module_suite()