test_randomstate_regression.py 7.7 KB

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