test_generator_mt19937_regressions.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from numpy.testing import (assert_, assert_array_equal)
  2. import numpy as np
  3. import pytest
  4. from numpy.random import Generator, MT19937
  5. mt19937 = Generator(MT19937())
  6. class TestRegression:
  7. def test_vonmises_range(self):
  8. # Make sure generated random variables are in [-pi, pi].
  9. # Regression test for ticket #986.
  10. for mu in np.linspace(-7., 7., 5):
  11. r = mt19937.vonmises(mu, 1, 50)
  12. assert_(np.all(r > -np.pi) and np.all(r <= np.pi))
  13. def test_hypergeometric_range(self):
  14. # Test for ticket #921
  15. assert_(np.all(mt19937.hypergeometric(3, 18, 11, size=10) < 4))
  16. assert_(np.all(mt19937.hypergeometric(18, 3, 11, size=10) > 0))
  17. # Test for ticket #5623
  18. args = (2**20 - 2, 2**20 - 2, 2**20 - 2) # Check for 32-bit systems
  19. assert_(mt19937.hypergeometric(*args) > 0)
  20. def test_logseries_convergence(self):
  21. # Test for ticket #923
  22. N = 1000
  23. mt19937 = Generator(MT19937(0))
  24. rvsn = mt19937.logseries(0.8, size=N)
  25. # these two frequency counts should be close to theoretical
  26. # numbers with this large sample
  27. # theoretical large N result is 0.49706795
  28. freq = np.sum(rvsn == 1) / N
  29. msg = f'Frequency was {freq:f}, should be > 0.45'
  30. assert_(freq > 0.45, msg)
  31. # theoretical large N result is 0.19882718
  32. freq = np.sum(rvsn == 2) / N
  33. msg = f'Frequency was {freq:f}, should be < 0.23'
  34. assert_(freq < 0.23, msg)
  35. def test_shuffle_mixed_dimension(self):
  36. # Test for trac ticket #2074
  37. for t in [[1, 2, 3, None],
  38. [(1, 1), (2, 2), (3, 3), None],
  39. [1, (2, 2), (3, 3), None],
  40. [(1, 1), 2, 3, None]]:
  41. mt19937 = Generator(MT19937(12345))
  42. shuffled = np.array(t, dtype=object)
  43. mt19937.shuffle(shuffled)
  44. expected = np.array([t[2], t[0], t[3], t[1]], dtype=object)
  45. assert_array_equal(np.array(shuffled, dtype=object), expected)
  46. def test_call_within_randomstate(self):
  47. # Check that custom BitGenerator does not call into global state
  48. res = np.array([1, 8, 0, 1, 5, 3, 3, 8, 1, 4])
  49. for i in range(3):
  50. mt19937 = Generator(MT19937(i))
  51. m = Generator(MT19937(4321))
  52. # If m.state is not honored, the result will change
  53. assert_array_equal(m.choice(10, size=10, p=np.ones(10)/10.), res)
  54. def test_multivariate_normal_size_types(self):
  55. # Test for multivariate_normal issue with 'size' argument.
  56. # Check that the multivariate_normal size argument can be a
  57. # numpy integer.
  58. mt19937.multivariate_normal([0], [[0]], size=1)
  59. mt19937.multivariate_normal([0], [[0]], size=np.int_(1))
  60. mt19937.multivariate_normal([0], [[0]], size=np.int64(1))
  61. def test_beta_small_parameters(self):
  62. # Test that beta with small a and b parameters does not produce
  63. # NaNs due to roundoff errors causing 0 / 0, gh-5851
  64. mt19937 = Generator(MT19937(1234567890))
  65. x = mt19937.beta(0.0001, 0.0001, size=100)
  66. assert_(not np.any(np.isnan(x)), 'Nans in mt19937.beta')
  67. def test_choice_sum_of_probs_tolerance(self):
  68. # The sum of probs should be 1.0 with some tolerance.
  69. # For low precision dtypes the tolerance was too tight.
  70. # See numpy github issue 6123.
  71. mt19937 = Generator(MT19937(1234))
  72. a = [1, 2, 3]
  73. counts = [4, 4, 2]
  74. for dt in np.float16, np.float32, np.float64:
  75. probs = np.array(counts, dtype=dt) / sum(counts)
  76. c = mt19937.choice(a, p=probs)
  77. assert_(c in a)
  78. with pytest.raises(ValueError):
  79. mt19937.choice(a, p=probs*0.9)
  80. def test_shuffle_of_array_of_different_length_strings(self):
  81. # Test that permuting an array of different length strings
  82. # will not cause a segfault on garbage collection
  83. # Tests gh-7710
  84. mt19937 = Generator(MT19937(1234))
  85. a = np.array(['a', 'a' * 1000])
  86. for _ in range(100):
  87. mt19937.shuffle(a)
  88. # Force Garbage Collection - should not segfault.
  89. import gc
  90. gc.collect()
  91. def test_shuffle_of_array_of_objects(self):
  92. # Test that permuting an array of objects will not cause
  93. # a segfault on garbage collection.
  94. # See gh-7719
  95. mt19937 = Generator(MT19937(1234))
  96. a = np.array([np.arange(1), np.arange(4)], dtype=object)
  97. for _ in range(1000):
  98. mt19937.shuffle(a)
  99. # Force Garbage Collection - should not segfault.
  100. import gc
  101. gc.collect()
  102. def test_permutation_subclass(self):
  103. class N(np.ndarray):
  104. pass
  105. mt19937 = Generator(MT19937(1))
  106. orig = np.arange(3).view(N)
  107. perm = mt19937.permutation(orig)
  108. assert_array_equal(perm, np.array([2, 0, 1]))
  109. assert_array_equal(orig, np.arange(3).view(N))
  110. class M:
  111. a = np.arange(5)
  112. def __array__(self):
  113. return self.a
  114. mt19937 = Generator(MT19937(1))
  115. m = M()
  116. perm = mt19937.permutation(m)
  117. assert_array_equal(perm, np.array([4, 1, 3, 0, 2]))
  118. assert_array_equal(m.__array__(), np.arange(5))
  119. def test_gamma_0(self):
  120. assert mt19937.standard_gamma(0.0) == 0.0
  121. assert_array_equal(mt19937.standard_gamma([0.0]), 0.0)
  122. actual = mt19937.standard_gamma([0.0], dtype='float')
  123. expected = np.array([0.], dtype=np.float32)
  124. assert_array_equal(actual, expected)