test_regression.py 5.3 KB

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