test_regression.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. args.append((2**40 - 2, 2**40 - 2, 2**40 - 2)) # Check for 64-bit systems
  27. for arg in args:
  28. assert_(np.random.hypergeometric(*arg) > 0)
  29. def test_logseries_convergence(self):
  30. # Test for ticket #923
  31. N = 1000
  32. np.random.seed(0)
  33. rvsn = np.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) / float(N)
  38. msg = "Frequency was %f, should be > 0.45" % freq
  39. assert_(freq > 0.45, msg)
  40. # theoretical large N result is 0.19882718
  41. freq = np.sum(rvsn == 2) / float(N)
  42. msg = "Frequency was %f, should be < 0.23" % freq
  43. assert_(freq < 0.23, msg)
  44. def test_permutation_longs(self):
  45. np.random.seed(1234)
  46. a = np.random.permutation(12)
  47. np.random.seed(1234)
  48. b = np.random.permutation(long(12))
  49. assert_array_equal(a, b)
  50. def test_shuffle_mixed_dimension(self):
  51. # Test for trac ticket #2074
  52. for t in [[1, 2, 3, None],
  53. [(1, 1), (2, 2), (3, 3), None],
  54. [1, (2, 2), (3, 3), None],
  55. [(1, 1), 2, 3, None]]:
  56. np.random.seed(12345)
  57. shuffled = list(t)
  58. random.shuffle(shuffled)
  59. assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]])
  60. def test_call_within_randomstate(self):
  61. # Check that custom RandomState does not call into global state
  62. m = np.random.RandomState()
  63. res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3])
  64. for i in range(3):
  65. np.random.seed(i)
  66. m.seed(4321)
  67. # If m.state is not honored, the result will change
  68. assert_array_equal(m.choice(10, size=10, p=np.ones(10)/10.), res)
  69. def test_multivariate_normal_size_types(self):
  70. # Test for multivariate_normal issue with 'size' argument.
  71. # Check that the multivariate_normal size argument can be a
  72. # numpy integer.
  73. np.random.multivariate_normal([0], [[0]], size=1)
  74. np.random.multivariate_normal([0], [[0]], size=np.int_(1))
  75. np.random.multivariate_normal([0], [[0]], size=np.int64(1))
  76. def test_beta_small_parameters(self):
  77. # Test that beta with small a and b parameters does not produce
  78. # NaNs due to roundoff errors causing 0 / 0, gh-5851
  79. np.random.seed(1234567890)
  80. x = np.random.beta(0.0001, 0.0001, size=100)
  81. assert_(not np.any(np.isnan(x)), 'Nans in np.random.beta')
  82. def test_choice_sum_of_probs_tolerance(self):
  83. # The sum of probs should be 1.0 with some tolerance.
  84. # For low precision dtypes the tolerance was too tight.
  85. # See numpy github issue 6123.
  86. np.random.seed(1234)
  87. a = [1, 2, 3]
  88. counts = [4, 4, 2]
  89. for dt in np.float16, np.float32, np.float64:
  90. probs = np.array(counts, dtype=dt) / sum(counts)
  91. c = np.random.choice(a, p=probs)
  92. assert_(c in a)
  93. assert_raises(ValueError, np.random.choice, a, p=probs*0.9)
  94. def test_shuffle_of_array_of_different_length_strings(self):
  95. # Test that permuting an array of different length strings
  96. # will not cause a segfault on garbage collection
  97. # Tests gh-7710
  98. np.random.seed(1234)
  99. a = np.array(['a', 'a' * 1000])
  100. for _ in range(100):
  101. np.random.shuffle(a)
  102. # Force Garbage Collection - should not segfault.
  103. import gc
  104. gc.collect()
  105. def test_shuffle_of_array_of_objects(self):
  106. # Test that permuting an array of objects will not cause
  107. # a segfault on garbage collection.
  108. # See gh-7719
  109. np.random.seed(1234)
  110. a = np.array([np.arange(1), np.arange(4)])
  111. for _ in range(1000):
  112. np.random.shuffle(a)
  113. # Force Garbage Collection - should not segfault.
  114. import gc
  115. gc.collect()
  116. def test_permutation_subclass(self):
  117. class N(np.ndarray):
  118. pass
  119. np.random.seed(1)
  120. orig = np.arange(3).view(N)
  121. perm = np.random.permutation(orig)
  122. assert_array_equal(perm, np.array([0, 2, 1]))
  123. assert_array_equal(orig, np.arange(3).view(N))
  124. class M(object):
  125. a = np.arange(5)
  126. def __array__(self):
  127. return self.a
  128. np.random.seed(1)
  129. m = M()
  130. perm = np.random.permutation(m)
  131. assert_array_equal(perm, np.array([2, 1, 4, 0, 3]))
  132. assert_array_equal(m.__array__(), np.arange(5))