test_regression.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from __future__ import division, absolute_import, print_function
  2. import sys
  3. from numpy.testing import (
  4. assert_, assert_array_equal, assert_raises,
  5. )
  6. from numpy import random
  7. from numpy.compat import long
  8. import numpy as np
  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.mtrand.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(np.random.hypergeometric(3, 18, 11, size=10) < 4))
  19. assert_(np.all(np.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_(np.random.hypergeometric(*arg) > 0)
  30. def test_logseries_convergence(self):
  31. # Test for ticket #923
  32. N = 1000
  33. np.random.seed(0)
  34. rvsn = np.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. np.random.seed(1234)
  47. a = np.random.permutation(12)
  48. np.random.seed(1234)
  49. b = np.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. np.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 = np.random.RandomState()
  64. res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3])
  65. for i in range(3):
  66. np.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. np.random.multivariate_normal([0], [[0]], size=1)
  75. np.random.multivariate_normal([0], [[0]], size=np.int_(1))
  76. np.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. np.random.seed(1234567890)
  81. x = np.random.beta(0.0001, 0.0001, size=100)
  82. assert_(not np.any(np.isnan(x)), 'Nans in np.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. np.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 = np.random.choice(a, p=probs)
  93. assert_(c in a)
  94. assert_raises(ValueError, np.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. np.random.seed(1234)
  100. a = np.array(['a', 'a' * 1000])
  101. for _ in range(100):
  102. np.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. np.random.seed(1234)
  111. a = np.array([np.arange(1), np.arange(4)])
  112. for _ in range(1000):
  113. np.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. np.random.seed(1)
  121. orig = np.arange(3).view(N)
  122. perm = np.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. np.random.seed(1)
  130. m = M()
  131. perm = np.random.permutation(m)
  132. assert_array_equal(perm, np.array([2, 1, 4, 0, 3]))
  133. assert_array_equal(m.__array__(), np.arange(5))