test_randomstate_regression.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import sys
  2. import pytest
  3. from numpy.testing import (
  4. assert_, assert_array_equal, assert_raises,
  5. )
  6. from numpy.compat import long
  7. import numpy as np
  8. from numpy import random
  9. class TestRegression(object):
  10. def test_VonMises_range(self):
  11. # Make sure generated random variables are in [-pi, pi].
  12. # Regression test for ticket #986.
  13. for mu in np.linspace(-7., 7., 5):
  14. r = random.vonmises(mu, 1, 50)
  15. assert_(np.all(r > -np.pi) and np.all(r <= np.pi))
  16. def test_hypergeometric_range(self):
  17. # Test for ticket #921
  18. assert_(np.all(random.hypergeometric(3, 18, 11, size=10) < 4))
  19. assert_(np.all(random.hypergeometric(18, 3, 11, size=10) > 0))
  20. # Test for ticket #5623
  21. args = [
  22. (2**20 - 2, 2**20 - 2, 2**20 - 2), # Check for 32-bit systems
  23. ]
  24. is_64bits = sys.maxsize > 2**32
  25. if is_64bits and sys.platform != 'win32':
  26. # Check for 64-bit systems
  27. args.append((2**40 - 2, 2**40 - 2, 2**40 - 2))
  28. for arg in args:
  29. assert_(random.hypergeometric(*arg) > 0)
  30. def test_logseries_convergence(self):
  31. # Test for ticket #923
  32. N = 1000
  33. random.seed(0)
  34. rvsn = random.logseries(0.8, size=N)
  35. # these two frequency counts should be close to theoretical
  36. # numbers with this large sample
  37. # theoretical large N result is 0.49706795
  38. freq = np.sum(rvsn == 1) / float(N)
  39. msg = "Frequency was %f, should be > 0.45" % freq
  40. assert_(freq > 0.45, msg)
  41. # theoretical large N result is 0.19882718
  42. freq = np.sum(rvsn == 2) / float(N)
  43. msg = "Frequency was %f, should be < 0.23" % freq
  44. assert_(freq < 0.23, msg)
  45. def test_permutation_longs(self):
  46. random.seed(1234)
  47. a = random.permutation(12)
  48. random.seed(1234)
  49. b = random.permutation(long(12))
  50. assert_array_equal(a, b)
  51. def test_shuffle_mixed_dimension(self):
  52. # Test for trac ticket #2074
  53. for t in [[1, 2, 3, None],
  54. [(1, 1), (2, 2), (3, 3), None],
  55. [1, (2, 2), (3, 3), None],
  56. [(1, 1), 2, 3, None]]:
  57. random.seed(12345)
  58. shuffled = list(t)
  59. random.shuffle(shuffled)
  60. assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]])
  61. def test_call_within_randomstate(self):
  62. # Check that custom RandomState does not call into global state
  63. m = random.RandomState()
  64. res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3])
  65. for i in range(3):
  66. random.seed(i)
  67. m.seed(4321)
  68. # If m.state is not honored, the result will change
  69. assert_array_equal(m.choice(10, size=10, p=np.ones(10)/10.), res)
  70. def test_multivariate_normal_size_types(self):
  71. # Test for multivariate_normal issue with 'size' argument.
  72. # Check that the multivariate_normal size argument can be a
  73. # numpy integer.
  74. random.multivariate_normal([0], [[0]], size=1)
  75. random.multivariate_normal([0], [[0]], size=np.int_(1))
  76. random.multivariate_normal([0], [[0]], size=np.int64(1))
  77. def test_beta_small_parameters(self):
  78. # Test that beta with small a and b parameters does not produce
  79. # NaNs due to roundoff errors causing 0 / 0, gh-5851
  80. random.seed(1234567890)
  81. x = random.beta(0.0001, 0.0001, size=100)
  82. assert_(not np.any(np.isnan(x)), 'Nans in random.beta')
  83. def test_choice_sum_of_probs_tolerance(self):
  84. # The sum of probs should be 1.0 with some tolerance.
  85. # For low precision dtypes the tolerance was too tight.
  86. # See numpy github issue 6123.
  87. random.seed(1234)
  88. a = [1, 2, 3]
  89. counts = [4, 4, 2]
  90. for dt in np.float16, np.float32, np.float64:
  91. probs = np.array(counts, dtype=dt) / sum(counts)
  92. c = random.choice(a, p=probs)
  93. assert_(c in a)
  94. assert_raises(ValueError, random.choice, a, p=probs*0.9)
  95. def test_shuffle_of_array_of_different_length_strings(self):
  96. # Test that permuting an array of different length strings
  97. # will not cause a segfault on garbage collection
  98. # Tests gh-7710
  99. random.seed(1234)
  100. a = np.array(['a', 'a' * 1000])
  101. for _ in range(100):
  102. random.shuffle(a)
  103. # Force Garbage Collection - should not segfault.
  104. import gc
  105. gc.collect()
  106. def test_shuffle_of_array_of_objects(self):
  107. # Test that permuting an array of objects will not cause
  108. # a segfault on garbage collection.
  109. # See gh-7719
  110. random.seed(1234)
  111. a = np.array([np.arange(1), np.arange(4)])
  112. for _ in range(1000):
  113. random.shuffle(a)
  114. # Force Garbage Collection - should not segfault.
  115. import gc
  116. gc.collect()
  117. def test_permutation_subclass(self):
  118. class N(np.ndarray):
  119. pass
  120. random.seed(1)
  121. orig = np.arange(3).view(N)
  122. perm = random.permutation(orig)
  123. assert_array_equal(perm, np.array([0, 2, 1]))
  124. assert_array_equal(orig, np.arange(3).view(N))
  125. class M(object):
  126. a = np.arange(5)
  127. def __array__(self):
  128. return self.a
  129. random.seed(1)
  130. m = M()
  131. perm = random.permutation(m)
  132. assert_array_equal(perm, np.array([2, 1, 4, 0, 3]))
  133. assert_array_equal(m.__array__(), np.arange(5))
  134. def test_warns_byteorder(self):
  135. # GH 13159
  136. other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
  137. with pytest.deprecated_call(match='non-native byteorder is not'):
  138. random.randint(0, 200, size=10, dtype=other_byteord_dt)
  139. def test_named_argument_initialization(self):
  140. # GH 13669
  141. rs1 = np.random.RandomState(123456789)
  142. rs2 = np.random.RandomState(seed=123456789)
  143. assert rs1.randint(0, 100) == rs2.randint(0, 100)
  144. def test_choice_retun_dtype(self):
  145. # GH 9867
  146. c = np.random.choice(10, p=[.1]*10, size=2)
  147. assert c.dtype == np.dtype(int)
  148. c = np.random.choice(10, p=[.1]*10, replace=False, size=2)
  149. assert c.dtype == np.dtype(int)
  150. c = np.random.choice(10, size=2)
  151. assert c.dtype == np.dtype(int)
  152. c = np.random.choice(10, replace=False, size=2)
  153. assert c.dtype == np.dtype(int)
  154. @pytest.mark.skipif(np.iinfo('l').max < 2**32,
  155. reason='Cannot test with 32-bit C long')
  156. def test_randint_117(self):
  157. # GH 14189
  158. random.seed(0)
  159. expected = np.array([2357136044, 2546248239, 3071714933, 3626093760,
  160. 2588848963, 3684848379, 2340255427, 3638918503,
  161. 1819583497, 2678185683], dtype='int64')
  162. actual = random.randint(2**32, size=10)
  163. assert_array_equal(actual, expected)
  164. def test_p_zero_stream(self):
  165. # Regression test for gh-14522. Ensure that future versions
  166. # generate the same variates as version 1.16.
  167. np.random.seed(12345)
  168. assert_array_equal(random.binomial(1, [0, 0.25, 0.5, 0.75, 1]),
  169. [0, 0, 0, 1, 1])
  170. def test_n_zero_stream(self):
  171. # Regression test for gh-14522. Ensure that future versions
  172. # generate the same variates as version 1.16.
  173. np.random.seed(8675309)
  174. expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  175. [3, 4, 2, 3, 3, 1, 5, 3, 1, 3]])
  176. assert_array_equal(random.binomial([[0], [10]], 0.25, size=(2, 10)),
  177. expected)