test_random.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. from __future__ import division, absolute_import, print_function
  2. import warnings
  3. import numpy as np
  4. from numpy.testing import (
  5. assert_, assert_raises, assert_equal, assert_warns,
  6. assert_no_warnings, assert_array_equal, assert_array_almost_equal,
  7. suppress_warnings
  8. )
  9. from numpy import random
  10. import sys
  11. class TestSeed(object):
  12. def test_scalar(self):
  13. s = np.random.RandomState(0)
  14. assert_equal(s.randint(1000), 684)
  15. s = np.random.RandomState(4294967295)
  16. assert_equal(s.randint(1000), 419)
  17. def test_array(self):
  18. s = np.random.RandomState(range(10))
  19. assert_equal(s.randint(1000), 468)
  20. s = np.random.RandomState(np.arange(10))
  21. assert_equal(s.randint(1000), 468)
  22. s = np.random.RandomState([0])
  23. assert_equal(s.randint(1000), 973)
  24. s = np.random.RandomState([4294967295])
  25. assert_equal(s.randint(1000), 265)
  26. def test_invalid_scalar(self):
  27. # seed must be an unsigned 32 bit integer
  28. assert_raises(TypeError, np.random.RandomState, -0.5)
  29. assert_raises(ValueError, np.random.RandomState, -1)
  30. def test_invalid_array(self):
  31. # seed must be an unsigned 32 bit integer
  32. assert_raises(TypeError, np.random.RandomState, [-0.5])
  33. assert_raises(ValueError, np.random.RandomState, [-1])
  34. assert_raises(ValueError, np.random.RandomState, [4294967296])
  35. assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296])
  36. assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296])
  37. def test_invalid_array_shape(self):
  38. # gh-9832
  39. assert_raises(ValueError, np.random.RandomState,
  40. np.array([], dtype=np.int64))
  41. assert_raises(ValueError, np.random.RandomState, [[1, 2, 3]])
  42. assert_raises(ValueError, np.random.RandomState, [[1, 2, 3],
  43. [4, 5, 6]])
  44. class TestBinomial(object):
  45. def test_n_zero(self):
  46. # Tests the corner case of n == 0 for the binomial distribution.
  47. # binomial(0, p) should be zero for any p in [0, 1].
  48. # This test addresses issue #3480.
  49. zeros = np.zeros(2, dtype='int')
  50. for p in [0, .5, 1]:
  51. assert_(random.binomial(0, p) == 0)
  52. assert_array_equal(random.binomial(zeros, p), zeros)
  53. def test_p_is_nan(self):
  54. # Issue #4571.
  55. assert_raises(ValueError, random.binomial, 1, np.nan)
  56. class TestMultinomial(object):
  57. def test_basic(self):
  58. random.multinomial(100, [0.2, 0.8])
  59. def test_zero_probability(self):
  60. random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0])
  61. def test_int_negative_interval(self):
  62. assert_(-5 <= random.randint(-5, -1) < -1)
  63. x = random.randint(-5, -1, 5)
  64. assert_(np.all(-5 <= x))
  65. assert_(np.all(x < -1))
  66. def test_size(self):
  67. # gh-3173
  68. p = [0.5, 0.5]
  69. assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
  70. assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
  71. assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
  72. assert_equal(np.random.multinomial(1, p, [2, 2]).shape, (2, 2, 2))
  73. assert_equal(np.random.multinomial(1, p, (2, 2)).shape, (2, 2, 2))
  74. assert_equal(np.random.multinomial(1, p, np.array((2, 2))).shape,
  75. (2, 2, 2))
  76. assert_raises(TypeError, np.random.multinomial, 1, p,
  77. float(1))
  78. def test_multidimensional_pvals(self):
  79. assert_raises(ValueError, np.random.multinomial, 10, [[0, 1]])
  80. assert_raises(ValueError, np.random.multinomial, 10, [[0], [1]])
  81. assert_raises(ValueError, np.random.multinomial, 10, [[[0], [1]], [[1], [0]]])
  82. assert_raises(ValueError, np.random.multinomial, 10, np.array([[0, 1], [1, 0]]))
  83. class TestSetState(object):
  84. def setup(self):
  85. self.seed = 1234567890
  86. self.prng = random.RandomState(self.seed)
  87. self.state = self.prng.get_state()
  88. def test_basic(self):
  89. old = self.prng.tomaxint(16)
  90. self.prng.set_state(self.state)
  91. new = self.prng.tomaxint(16)
  92. assert_(np.all(old == new))
  93. def test_gaussian_reset(self):
  94. # Make sure the cached every-other-Gaussian is reset.
  95. old = self.prng.standard_normal(size=3)
  96. self.prng.set_state(self.state)
  97. new = self.prng.standard_normal(size=3)
  98. assert_(np.all(old == new))
  99. def test_gaussian_reset_in_media_res(self):
  100. # When the state is saved with a cached Gaussian, make sure the
  101. # cached Gaussian is restored.
  102. self.prng.standard_normal()
  103. state = self.prng.get_state()
  104. old = self.prng.standard_normal(size=3)
  105. self.prng.set_state(state)
  106. new = self.prng.standard_normal(size=3)
  107. assert_(np.all(old == new))
  108. def test_backwards_compatibility(self):
  109. # Make sure we can accept old state tuples that do not have the
  110. # cached Gaussian value.
  111. old_state = self.state[:-2]
  112. x1 = self.prng.standard_normal(size=16)
  113. self.prng.set_state(old_state)
  114. x2 = self.prng.standard_normal(size=16)
  115. self.prng.set_state(self.state)
  116. x3 = self.prng.standard_normal(size=16)
  117. assert_(np.all(x1 == x2))
  118. assert_(np.all(x1 == x3))
  119. def test_negative_binomial(self):
  120. # Ensure that the negative binomial results take floating point
  121. # arguments without truncation.
  122. self.prng.negative_binomial(0.5, 0.5)
  123. class TestRandint(object):
  124. rfunc = np.random.randint
  125. # valid integer/boolean types
  126. itype = [np.bool_, np.int8, np.uint8, np.int16, np.uint16,
  127. np.int32, np.uint32, np.int64, np.uint64]
  128. def test_unsupported_type(self):
  129. assert_raises(TypeError, self.rfunc, 1, dtype=float)
  130. def test_bounds_checking(self):
  131. for dt in self.itype:
  132. lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
  133. ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
  134. assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt)
  135. assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt)
  136. assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt)
  137. assert_raises(ValueError, self.rfunc, 1, 0, dtype=dt)
  138. def test_rng_zero_and_extremes(self):
  139. for dt in self.itype:
  140. lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
  141. ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
  142. tgt = ubnd - 1
  143. assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
  144. tgt = lbnd
  145. assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
  146. tgt = (lbnd + ubnd)//2
  147. assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
  148. def test_full_range(self):
  149. # Test for ticket #1690
  150. for dt in self.itype:
  151. lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
  152. ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
  153. try:
  154. self.rfunc(lbnd, ubnd, dtype=dt)
  155. except Exception as e:
  156. raise AssertionError("No error should have been raised, "
  157. "but one was with the following "
  158. "message:\n\n%s" % str(e))
  159. def test_in_bounds_fuzz(self):
  160. # Don't use fixed seed
  161. np.random.seed()
  162. for dt in self.itype[1:]:
  163. for ubnd in [4, 8, 16]:
  164. vals = self.rfunc(2, ubnd, size=2**16, dtype=dt)
  165. assert_(vals.max() < ubnd)
  166. assert_(vals.min() >= 2)
  167. vals = self.rfunc(0, 2, size=2**16, dtype=np.bool_)
  168. assert_(vals.max() < 2)
  169. assert_(vals.min() >= 0)
  170. def test_repeatability(self):
  171. import hashlib
  172. # We use a md5 hash of generated sequences of 1000 samples
  173. # in the range [0, 6) for all but bool, where the range
  174. # is [0, 2). Hashes are for little endian numbers.
  175. tgt = {'bool': '7dd3170d7aa461d201a65f8bcf3944b0',
  176. 'int16': '1b7741b80964bb190c50d541dca1cac1',
  177. 'int32': '4dc9fcc2b395577ebb51793e58ed1a05',
  178. 'int64': '17db902806f448331b5a758d7d2ee672',
  179. 'int8': '27dd30c4e08a797063dffac2490b0be6',
  180. 'uint16': '1b7741b80964bb190c50d541dca1cac1',
  181. 'uint32': '4dc9fcc2b395577ebb51793e58ed1a05',
  182. 'uint64': '17db902806f448331b5a758d7d2ee672',
  183. 'uint8': '27dd30c4e08a797063dffac2490b0be6'}
  184. for dt in self.itype[1:]:
  185. np.random.seed(1234)
  186. # view as little endian for hash
  187. if sys.byteorder == 'little':
  188. val = self.rfunc(0, 6, size=1000, dtype=dt)
  189. else:
  190. val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap()
  191. res = hashlib.md5(val.view(np.int8)).hexdigest()
  192. assert_(tgt[np.dtype(dt).name] == res)
  193. # bools do not depend on endianness
  194. np.random.seed(1234)
  195. val = self.rfunc(0, 2, size=1000, dtype=bool).view(np.int8)
  196. res = hashlib.md5(val).hexdigest()
  197. assert_(tgt[np.dtype(bool).name] == res)
  198. def test_int64_uint64_corner_case(self):
  199. # When stored in Numpy arrays, `lbnd` is casted
  200. # as np.int64, and `ubnd` is casted as np.uint64.
  201. # Checking whether `lbnd` >= `ubnd` used to be
  202. # done solely via direct comparison, which is incorrect
  203. # because when Numpy tries to compare both numbers,
  204. # it casts both to np.float64 because there is
  205. # no integer superset of np.int64 and np.uint64. However,
  206. # `ubnd` is too large to be represented in np.float64,
  207. # causing it be round down to np.iinfo(np.int64).max,
  208. # leading to a ValueError because `lbnd` now equals
  209. # the new `ubnd`.
  210. dt = np.int64
  211. tgt = np.iinfo(np.int64).max
  212. lbnd = np.int64(np.iinfo(np.int64).max)
  213. ubnd = np.uint64(np.iinfo(np.int64).max + 1)
  214. # None of these function calls should
  215. # generate a ValueError now.
  216. actual = np.random.randint(lbnd, ubnd, dtype=dt)
  217. assert_equal(actual, tgt)
  218. def test_respect_dtype_singleton(self):
  219. # See gh-7203
  220. for dt in self.itype:
  221. lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
  222. ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
  223. sample = self.rfunc(lbnd, ubnd, dtype=dt)
  224. assert_equal(sample.dtype, np.dtype(dt))
  225. for dt in (bool, int, np.compat.long):
  226. lbnd = 0 if dt is bool else np.iinfo(dt).min
  227. ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
  228. # gh-7284: Ensure that we get Python data types
  229. sample = self.rfunc(lbnd, ubnd, dtype=dt)
  230. assert_(not hasattr(sample, 'dtype'))
  231. assert_equal(type(sample), dt)
  232. class TestRandomDist(object):
  233. # Make sure the random distribution returns the correct value for a
  234. # given seed
  235. def setup(self):
  236. self.seed = 1234567890
  237. def test_rand(self):
  238. np.random.seed(self.seed)
  239. actual = np.random.rand(3, 2)
  240. desired = np.array([[0.61879477158567997, 0.59162362775974664],
  241. [0.88868358904449662, 0.89165480011560816],
  242. [0.4575674820298663, 0.7781880808593471]])
  243. assert_array_almost_equal(actual, desired, decimal=15)
  244. def test_randn(self):
  245. np.random.seed(self.seed)
  246. actual = np.random.randn(3, 2)
  247. desired = np.array([[1.34016345771863121, 1.73759122771936081],
  248. [1.498988344300628, -0.2286433324536169],
  249. [2.031033998682787, 2.17032494605655257]])
  250. assert_array_almost_equal(actual, desired, decimal=15)
  251. def test_randint(self):
  252. np.random.seed(self.seed)
  253. actual = np.random.randint(-99, 99, size=(3, 2))
  254. desired = np.array([[31, 3],
  255. [-52, 41],
  256. [-48, -66]])
  257. assert_array_equal(actual, desired)
  258. def test_random_integers(self):
  259. np.random.seed(self.seed)
  260. with suppress_warnings() as sup:
  261. w = sup.record(DeprecationWarning)
  262. actual = np.random.random_integers(-99, 99, size=(3, 2))
  263. assert_(len(w) == 1)
  264. desired = np.array([[31, 3],
  265. [-52, 41],
  266. [-48, -66]])
  267. assert_array_equal(actual, desired)
  268. def test_random_integers_max_int(self):
  269. # Tests whether random_integers can generate the
  270. # maximum allowed Python int that can be converted
  271. # into a C long. Previous implementations of this
  272. # method have thrown an OverflowError when attempting
  273. # to generate this integer.
  274. with suppress_warnings() as sup:
  275. w = sup.record(DeprecationWarning)
  276. actual = np.random.random_integers(np.iinfo('l').max,
  277. np.iinfo('l').max)
  278. assert_(len(w) == 1)
  279. desired = np.iinfo('l').max
  280. assert_equal(actual, desired)
  281. def test_random_integers_deprecated(self):
  282. with warnings.catch_warnings():
  283. warnings.simplefilter("error", DeprecationWarning)
  284. # DeprecationWarning raised with high == None
  285. assert_raises(DeprecationWarning,
  286. np.random.random_integers,
  287. np.iinfo('l').max)
  288. # DeprecationWarning raised with high != None
  289. assert_raises(DeprecationWarning,
  290. np.random.random_integers,
  291. np.iinfo('l').max, np.iinfo('l').max)
  292. def test_random(self):
  293. np.random.seed(self.seed)
  294. actual = np.random.random((3, 2))
  295. desired = np.array([[0.61879477158567997, 0.59162362775974664],
  296. [0.88868358904449662, 0.89165480011560816],
  297. [0.4575674820298663, 0.7781880808593471]])
  298. assert_array_almost_equal(actual, desired, decimal=15)
  299. def test_choice_uniform_replace(self):
  300. np.random.seed(self.seed)
  301. actual = np.random.choice(4, 4)
  302. desired = np.array([2, 3, 2, 3])
  303. assert_array_equal(actual, desired)
  304. def test_choice_nonuniform_replace(self):
  305. np.random.seed(self.seed)
  306. actual = np.random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1])
  307. desired = np.array([1, 1, 2, 2])
  308. assert_array_equal(actual, desired)
  309. def test_choice_uniform_noreplace(self):
  310. np.random.seed(self.seed)
  311. actual = np.random.choice(4, 3, replace=False)
  312. desired = np.array([0, 1, 3])
  313. assert_array_equal(actual, desired)
  314. def test_choice_nonuniform_noreplace(self):
  315. np.random.seed(self.seed)
  316. actual = np.random.choice(4, 3, replace=False,
  317. p=[0.1, 0.3, 0.5, 0.1])
  318. desired = np.array([2, 3, 1])
  319. assert_array_equal(actual, desired)
  320. def test_choice_noninteger(self):
  321. np.random.seed(self.seed)
  322. actual = np.random.choice(['a', 'b', 'c', 'd'], 4)
  323. desired = np.array(['c', 'd', 'c', 'd'])
  324. assert_array_equal(actual, desired)
  325. def test_choice_exceptions(self):
  326. sample = np.random.choice
  327. assert_raises(ValueError, sample, -1, 3)
  328. assert_raises(ValueError, sample, 3., 3)
  329. assert_raises(ValueError, sample, [[1, 2], [3, 4]], 3)
  330. assert_raises(ValueError, sample, [], 3)
  331. assert_raises(ValueError, sample, [1, 2, 3, 4], 3,
  332. p=[[0.25, 0.25], [0.25, 0.25]])
  333. assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2])
  334. assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1])
  335. assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4])
  336. assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False)
  337. # gh-13087
  338. assert_raises(ValueError, sample, [1, 2, 3], -2, replace=False)
  339. assert_raises(ValueError, sample, [1, 2, 3], (-1,), replace=False)
  340. assert_raises(ValueError, sample, [1, 2, 3], (-1, 1), replace=False)
  341. assert_raises(ValueError, sample, [1, 2, 3], 2,
  342. replace=False, p=[1, 0, 0])
  343. def test_choice_return_shape(self):
  344. p = [0.1, 0.9]
  345. # Check scalar
  346. assert_(np.isscalar(np.random.choice(2, replace=True)))
  347. assert_(np.isscalar(np.random.choice(2, replace=False)))
  348. assert_(np.isscalar(np.random.choice(2, replace=True, p=p)))
  349. assert_(np.isscalar(np.random.choice(2, replace=False, p=p)))
  350. assert_(np.isscalar(np.random.choice([1, 2], replace=True)))
  351. assert_(np.random.choice([None], replace=True) is None)
  352. a = np.array([1, 2])
  353. arr = np.empty(1, dtype=object)
  354. arr[0] = a
  355. assert_(np.random.choice(arr, replace=True) is a)
  356. # Check 0-d array
  357. s = tuple()
  358. assert_(not np.isscalar(np.random.choice(2, s, replace=True)))
  359. assert_(not np.isscalar(np.random.choice(2, s, replace=False)))
  360. assert_(not np.isscalar(np.random.choice(2, s, replace=True, p=p)))
  361. assert_(not np.isscalar(np.random.choice(2, s, replace=False, p=p)))
  362. assert_(not np.isscalar(np.random.choice([1, 2], s, replace=True)))
  363. assert_(np.random.choice([None], s, replace=True).ndim == 0)
  364. a = np.array([1, 2])
  365. arr = np.empty(1, dtype=object)
  366. arr[0] = a
  367. assert_(np.random.choice(arr, s, replace=True).item() is a)
  368. # Check multi dimensional array
  369. s = (2, 3)
  370. p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2]
  371. assert_equal(np.random.choice(6, s, replace=True).shape, s)
  372. assert_equal(np.random.choice(6, s, replace=False).shape, s)
  373. assert_equal(np.random.choice(6, s, replace=True, p=p).shape, s)
  374. assert_equal(np.random.choice(6, s, replace=False, p=p).shape, s)
  375. assert_equal(np.random.choice(np.arange(6), s, replace=True).shape, s)
  376. # Check zero-size
  377. assert_equal(np.random.randint(0, 0, size=(3, 0, 4)).shape, (3, 0, 4))
  378. assert_equal(np.random.randint(0, -10, size=0).shape, (0,))
  379. assert_equal(np.random.randint(10, 10, size=0).shape, (0,))
  380. assert_equal(np.random.choice(0, size=0).shape, (0,))
  381. assert_equal(np.random.choice([], size=(0,)).shape, (0,))
  382. assert_equal(np.random.choice(['a', 'b'], size=(3, 0, 4)).shape,
  383. (3, 0, 4))
  384. assert_raises(ValueError, np.random.choice, [], 10)
  385. def test_choice_nan_probabilities(self):
  386. a = np.array([42, 1, 2])
  387. p = [None, None, None]
  388. assert_raises(ValueError, np.random.choice, a, p=p)
  389. def test_bytes(self):
  390. np.random.seed(self.seed)
  391. actual = np.random.bytes(10)
  392. desired = b'\x82Ui\x9e\xff\x97+Wf\xa5'
  393. assert_equal(actual, desired)
  394. def test_shuffle(self):
  395. # Test lists, arrays (of various dtypes), and multidimensional versions
  396. # of both, c-contiguous or not:
  397. for conv in [lambda x: np.array([]),
  398. lambda x: x,
  399. lambda x: np.asarray(x).astype(np.int8),
  400. lambda x: np.asarray(x).astype(np.float32),
  401. lambda x: np.asarray(x).astype(np.complex64),
  402. lambda x: np.asarray(x).astype(object),
  403. lambda x: [(i, i) for i in x],
  404. lambda x: np.asarray([[i, i] for i in x]),
  405. lambda x: np.vstack([x, x]).T,
  406. # gh-11442
  407. lambda x: (np.asarray([(i, i) for i in x],
  408. [("a", int), ("b", int)])
  409. .view(np.recarray)),
  410. # gh-4270
  411. lambda x: np.asarray([(i, i) for i in x],
  412. [("a", object), ("b", np.int32)])]:
  413. np.random.seed(self.seed)
  414. alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
  415. np.random.shuffle(alist)
  416. actual = alist
  417. desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3])
  418. assert_array_equal(actual, desired)
  419. def test_shuffle_masked(self):
  420. # gh-3263
  421. a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1)
  422. b = np.ma.masked_values(np.arange(20) % 3 - 1, -1)
  423. a_orig = a.copy()
  424. b_orig = b.copy()
  425. for i in range(50):
  426. np.random.shuffle(a)
  427. assert_equal(
  428. sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask]))
  429. np.random.shuffle(b)
  430. assert_equal(
  431. sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask]))
  432. def test_beta(self):
  433. np.random.seed(self.seed)
  434. actual = np.random.beta(.1, .9, size=(3, 2))
  435. desired = np.array(
  436. [[1.45341850513746058e-02, 5.31297615662868145e-04],
  437. [1.85366619058432324e-06, 4.19214516800110563e-03],
  438. [1.58405155108498093e-04, 1.26252891949397652e-04]])
  439. assert_array_almost_equal(actual, desired, decimal=15)
  440. def test_binomial(self):
  441. np.random.seed(self.seed)
  442. actual = np.random.binomial(100, .456, size=(3, 2))
  443. desired = np.array([[37, 43],
  444. [42, 48],
  445. [46, 45]])
  446. assert_array_equal(actual, desired)
  447. def test_chisquare(self):
  448. np.random.seed(self.seed)
  449. actual = np.random.chisquare(50, size=(3, 2))
  450. desired = np.array([[63.87858175501090585, 68.68407748911370447],
  451. [65.77116116901505904, 47.09686762438974483],
  452. [72.3828403199695174, 74.18408615260374006]])
  453. assert_array_almost_equal(actual, desired, decimal=13)
  454. def test_dirichlet(self):
  455. np.random.seed(self.seed)
  456. alpha = np.array([51.72840233779265162, 39.74494232180943953])
  457. actual = np.random.mtrand.dirichlet(alpha, size=(3, 2))
  458. desired = np.array([[[0.54539444573611562, 0.45460555426388438],
  459. [0.62345816822039413, 0.37654183177960598]],
  460. [[0.55206000085785778, 0.44793999914214233],
  461. [0.58964023305154301, 0.41035976694845688]],
  462. [[0.59266909280647828, 0.40733090719352177],
  463. [0.56974431743975207, 0.43025568256024799]]])
  464. assert_array_almost_equal(actual, desired, decimal=15)
  465. def test_dirichlet_size(self):
  466. # gh-3173
  467. p = np.array([51.72840233779265162, 39.74494232180943953])
  468. assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2))
  469. assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2))
  470. assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2))
  471. assert_equal(np.random.dirichlet(p, [2, 2]).shape, (2, 2, 2))
  472. assert_equal(np.random.dirichlet(p, (2, 2)).shape, (2, 2, 2))
  473. assert_equal(np.random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2))
  474. assert_raises(TypeError, np.random.dirichlet, p, float(1))
  475. def test_dirichlet_bad_alpha(self):
  476. # gh-2089
  477. alpha = np.array([5.4e-01, -1.0e-16])
  478. assert_raises(ValueError, np.random.mtrand.dirichlet, alpha)
  479. # gh-15876
  480. assert_raises(ValueError, random.dirichlet, [[5, 1]])
  481. assert_raises(ValueError, random.dirichlet, [[5], [1]])
  482. assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]])
  483. assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]]))
  484. def test_exponential(self):
  485. np.random.seed(self.seed)
  486. actual = np.random.exponential(1.1234, size=(3, 2))
  487. desired = np.array([[1.08342649775011624, 1.00607889924557314],
  488. [2.46628830085216721, 2.49668106809923884],
  489. [0.68717433461363442, 1.69175666993575979]])
  490. assert_array_almost_equal(actual, desired, decimal=15)
  491. def test_exponential_0(self):
  492. assert_equal(np.random.exponential(scale=0), 0)
  493. assert_raises(ValueError, np.random.exponential, scale=-0.)
  494. def test_f(self):
  495. np.random.seed(self.seed)
  496. actual = np.random.f(12, 77, size=(3, 2))
  497. desired = np.array([[1.21975394418575878, 1.75135759791559775],
  498. [1.44803115017146489, 1.22108959480396262],
  499. [1.02176975757740629, 1.34431827623300415]])
  500. assert_array_almost_equal(actual, desired, decimal=15)
  501. def test_gamma(self):
  502. np.random.seed(self.seed)
  503. actual = np.random.gamma(5, 3, size=(3, 2))
  504. desired = np.array([[24.60509188649287182, 28.54993563207210627],
  505. [26.13476110204064184, 12.56988482927716078],
  506. [31.71863275789960568, 33.30143302795922011]])
  507. assert_array_almost_equal(actual, desired, decimal=14)
  508. def test_gamma_0(self):
  509. assert_equal(np.random.gamma(shape=0, scale=0), 0)
  510. assert_raises(ValueError, np.random.gamma, shape=-0., scale=-0.)
  511. def test_geometric(self):
  512. np.random.seed(self.seed)
  513. actual = np.random.geometric(.123456789, size=(3, 2))
  514. desired = np.array([[8, 7],
  515. [17, 17],
  516. [5, 12]])
  517. assert_array_equal(actual, desired)
  518. def test_gumbel(self):
  519. np.random.seed(self.seed)
  520. actual = np.random.gumbel(loc=.123456789, scale=2.0, size=(3, 2))
  521. desired = np.array([[0.19591898743416816, 0.34405539668096674],
  522. [-1.4492522252274278, -1.47374816298446865],
  523. [1.10651090478803416, -0.69535848626236174]])
  524. assert_array_almost_equal(actual, desired, decimal=15)
  525. def test_gumbel_0(self):
  526. assert_equal(np.random.gumbel(scale=0), 0)
  527. assert_raises(ValueError, np.random.gumbel, scale=-0.)
  528. def test_hypergeometric(self):
  529. np.random.seed(self.seed)
  530. actual = np.random.hypergeometric(10, 5, 14, size=(3, 2))
  531. desired = np.array([[10, 10],
  532. [10, 10],
  533. [9, 9]])
  534. assert_array_equal(actual, desired)
  535. # Test nbad = 0
  536. actual = np.random.hypergeometric(5, 0, 3, size=4)
  537. desired = np.array([3, 3, 3, 3])
  538. assert_array_equal(actual, desired)
  539. actual = np.random.hypergeometric(15, 0, 12, size=4)
  540. desired = np.array([12, 12, 12, 12])
  541. assert_array_equal(actual, desired)
  542. # Test ngood = 0
  543. actual = np.random.hypergeometric(0, 5, 3, size=4)
  544. desired = np.array([0, 0, 0, 0])
  545. assert_array_equal(actual, desired)
  546. actual = np.random.hypergeometric(0, 15, 12, size=4)
  547. desired = np.array([0, 0, 0, 0])
  548. assert_array_equal(actual, desired)
  549. def test_laplace(self):
  550. np.random.seed(self.seed)
  551. actual = np.random.laplace(loc=.123456789, scale=2.0, size=(3, 2))
  552. desired = np.array([[0.66599721112760157, 0.52829452552221945],
  553. [3.12791959514407125, 3.18202813572992005],
  554. [-0.05391065675859356, 1.74901336242837324]])
  555. assert_array_almost_equal(actual, desired, decimal=15)
  556. def test_laplace_0(self):
  557. assert_equal(np.random.laplace(scale=0), 0)
  558. assert_raises(ValueError, np.random.laplace, scale=-0.)
  559. def test_logistic(self):
  560. np.random.seed(self.seed)
  561. actual = np.random.logistic(loc=.123456789, scale=2.0, size=(3, 2))
  562. desired = np.array([[1.09232835305011444, 0.8648196662399954],
  563. [4.27818590694950185, 4.33897006346929714],
  564. [-0.21682183359214885, 2.63373365386060332]])
  565. assert_array_almost_equal(actual, desired, decimal=15)
  566. def test_lognormal(self):
  567. np.random.seed(self.seed)
  568. actual = np.random.lognormal(mean=.123456789, sigma=2.0, size=(3, 2))
  569. desired = np.array([[16.50698631688883822, 36.54846706092654784],
  570. [22.67886599981281748, 0.71617561058995771],
  571. [65.72798501792723869, 86.84341601437161273]])
  572. assert_array_almost_equal(actual, desired, decimal=13)
  573. def test_lognormal_0(self):
  574. assert_equal(np.random.lognormal(sigma=0), 1)
  575. assert_raises(ValueError, np.random.lognormal, sigma=-0.)
  576. def test_logseries(self):
  577. np.random.seed(self.seed)
  578. actual = np.random.logseries(p=.923456789, size=(3, 2))
  579. desired = np.array([[2, 2],
  580. [6, 17],
  581. [3, 6]])
  582. assert_array_equal(actual, desired)
  583. def test_multinomial(self):
  584. np.random.seed(self.seed)
  585. actual = np.random.multinomial(20, [1/6.]*6, size=(3, 2))
  586. desired = np.array([[[4, 3, 5, 4, 2, 2],
  587. [5, 2, 8, 2, 2, 1]],
  588. [[3, 4, 3, 6, 0, 4],
  589. [2, 1, 4, 3, 6, 4]],
  590. [[4, 4, 2, 5, 2, 3],
  591. [4, 3, 4, 2, 3, 4]]])
  592. assert_array_equal(actual, desired)
  593. def test_multivariate_normal(self):
  594. np.random.seed(self.seed)
  595. mean = (.123456789, 10)
  596. cov = [[1, 0], [0, 1]]
  597. size = (3, 2)
  598. actual = np.random.multivariate_normal(mean, cov, size)
  599. desired = np.array([[[1.463620246718631, 11.73759122771936],
  600. [1.622445133300628, 9.771356667546383]],
  601. [[2.154490787682787, 12.170324946056553],
  602. [1.719909438201865, 9.230548443648306]],
  603. [[0.689515026297799, 9.880729819607714],
  604. [-0.023054015651998, 9.201096623542879]]])
  605. assert_array_almost_equal(actual, desired, decimal=15)
  606. # Check for default size, was raising deprecation warning
  607. actual = np.random.multivariate_normal(mean, cov)
  608. desired = np.array([0.895289569463708, 9.17180864067987])
  609. assert_array_almost_equal(actual, desired, decimal=15)
  610. # Check that non positive-semidefinite covariance warns with
  611. # RuntimeWarning
  612. mean = [0, 0]
  613. cov = [[1, 2], [2, 1]]
  614. assert_warns(RuntimeWarning, np.random.multivariate_normal, mean, cov)
  615. # and that it doesn't warn with RuntimeWarning check_valid='ignore'
  616. assert_no_warnings(np.random.multivariate_normal, mean, cov,
  617. check_valid='ignore')
  618. # and that it raises with RuntimeWarning check_valid='raises'
  619. assert_raises(ValueError, np.random.multivariate_normal, mean, cov,
  620. check_valid='raise')
  621. cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32)
  622. with suppress_warnings() as sup:
  623. np.random.multivariate_normal(mean, cov)
  624. w = sup.record(RuntimeWarning)
  625. assert len(w) == 0
  626. def test_negative_binomial(self):
  627. np.random.seed(self.seed)
  628. actual = np.random.negative_binomial(n=100, p=.12345, size=(3, 2))
  629. desired = np.array([[848, 841],
  630. [892, 611],
  631. [779, 647]])
  632. assert_array_equal(actual, desired)
  633. def test_noncentral_chisquare(self):
  634. np.random.seed(self.seed)
  635. actual = np.random.noncentral_chisquare(df=5, nonc=5, size=(3, 2))
  636. desired = np.array([[23.91905354498517511, 13.35324692733826346],
  637. [31.22452661329736401, 16.60047399466177254],
  638. [5.03461598262724586, 17.94973089023519464]])
  639. assert_array_almost_equal(actual, desired, decimal=14)
  640. actual = np.random.noncentral_chisquare(df=.5, nonc=.2, size=(3, 2))
  641. desired = np.array([[1.47145377828516666, 0.15052899268012659],
  642. [0.00943803056963588, 1.02647251615666169],
  643. [0.332334982684171, 0.15451287602753125]])
  644. assert_array_almost_equal(actual, desired, decimal=14)
  645. np.random.seed(self.seed)
  646. actual = np.random.noncentral_chisquare(df=5, nonc=0, size=(3, 2))
  647. desired = np.array([[9.597154162763948, 11.725484450296079],
  648. [10.413711048138335, 3.694475922923986],
  649. [13.484222138963087, 14.377255424602957]])
  650. assert_array_almost_equal(actual, desired, decimal=14)
  651. def test_noncentral_f(self):
  652. np.random.seed(self.seed)
  653. actual = np.random.noncentral_f(dfnum=5, dfden=2, nonc=1,
  654. size=(3, 2))
  655. desired = np.array([[1.40598099674926669, 0.34207973179285761],
  656. [3.57715069265772545, 7.92632662577829805],
  657. [0.43741599463544162, 1.1774208752428319]])
  658. assert_array_almost_equal(actual, desired, decimal=14)
  659. def test_normal(self):
  660. np.random.seed(self.seed)
  661. actual = np.random.normal(loc=.123456789, scale=2.0, size=(3, 2))
  662. desired = np.array([[2.80378370443726244, 3.59863924443872163],
  663. [3.121433477601256, -0.33382987590723379],
  664. [4.18552478636557357, 4.46410668111310471]])
  665. assert_array_almost_equal(actual, desired, decimal=15)
  666. def test_normal_0(self):
  667. assert_equal(np.random.normal(scale=0), 0)
  668. assert_raises(ValueError, np.random.normal, scale=-0.)
  669. def test_pareto(self):
  670. np.random.seed(self.seed)
  671. actual = np.random.pareto(a=.123456789, size=(3, 2))
  672. desired = np.array(
  673. [[2.46852460439034849e+03, 1.41286880810518346e+03],
  674. [5.28287797029485181e+07, 6.57720981047328785e+07],
  675. [1.40840323350391515e+02, 1.98390255135251704e+05]])
  676. # For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this
  677. # matrix differs by 24 nulps. Discussion:
  678. # https://mail.python.org/pipermail/numpy-discussion/2012-September/063801.html
  679. # Consensus is that this is probably some gcc quirk that affects
  680. # rounding but not in any important way, so we just use a looser
  681. # tolerance on this test:
  682. np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30)
  683. def test_poisson(self):
  684. np.random.seed(self.seed)
  685. actual = np.random.poisson(lam=.123456789, size=(3, 2))
  686. desired = np.array([[0, 0],
  687. [1, 0],
  688. [0, 0]])
  689. assert_array_equal(actual, desired)
  690. def test_poisson_exceptions(self):
  691. lambig = np.iinfo('l').max
  692. lamneg = -1
  693. assert_raises(ValueError, np.random.poisson, lamneg)
  694. assert_raises(ValueError, np.random.poisson, [lamneg]*10)
  695. assert_raises(ValueError, np.random.poisson, lambig)
  696. assert_raises(ValueError, np.random.poisson, [lambig]*10)
  697. def test_power(self):
  698. np.random.seed(self.seed)
  699. actual = np.random.power(a=.123456789, size=(3, 2))
  700. desired = np.array([[0.02048932883240791, 0.01424192241128213],
  701. [0.38446073748535298, 0.39499689943484395],
  702. [0.00177699707563439, 0.13115505880863756]])
  703. assert_array_almost_equal(actual, desired, decimal=15)
  704. def test_rayleigh(self):
  705. np.random.seed(self.seed)
  706. actual = np.random.rayleigh(scale=10, size=(3, 2))
  707. desired = np.array([[13.8882496494248393, 13.383318339044731],
  708. [20.95413364294492098, 21.08285015800712614],
  709. [11.06066537006854311, 17.35468505778271009]])
  710. assert_array_almost_equal(actual, desired, decimal=14)
  711. def test_rayleigh_0(self):
  712. assert_equal(np.random.rayleigh(scale=0), 0)
  713. assert_raises(ValueError, np.random.rayleigh, scale=-0.)
  714. def test_standard_cauchy(self):
  715. np.random.seed(self.seed)
  716. actual = np.random.standard_cauchy(size=(3, 2))
  717. desired = np.array([[0.77127660196445336, -6.55601161955910605],
  718. [0.93582023391158309, -2.07479293013759447],
  719. [-4.74601644297011926, 0.18338989290760804]])
  720. assert_array_almost_equal(actual, desired, decimal=15)
  721. def test_standard_exponential(self):
  722. np.random.seed(self.seed)
  723. actual = np.random.standard_exponential(size=(3, 2))
  724. desired = np.array([[0.96441739162374596, 0.89556604882105506],
  725. [2.1953785836319808, 2.22243285392490542],
  726. [0.6116915921431676, 1.50592546727413201]])
  727. assert_array_almost_equal(actual, desired, decimal=15)
  728. def test_standard_gamma(self):
  729. np.random.seed(self.seed)
  730. actual = np.random.standard_gamma(shape=3, size=(3, 2))
  731. desired = np.array([[5.50841531318455058, 6.62953470301903103],
  732. [5.93988484943779227, 2.31044849402133989],
  733. [7.54838614231317084, 8.012756093271868]])
  734. assert_array_almost_equal(actual, desired, decimal=14)
  735. def test_standard_gamma_0(self):
  736. assert_equal(np.random.standard_gamma(shape=0), 0)
  737. assert_raises(ValueError, np.random.standard_gamma, shape=-0.)
  738. def test_standard_normal(self):
  739. np.random.seed(self.seed)
  740. actual = np.random.standard_normal(size=(3, 2))
  741. desired = np.array([[1.34016345771863121, 1.73759122771936081],
  742. [1.498988344300628, -0.2286433324536169],
  743. [2.031033998682787, 2.17032494605655257]])
  744. assert_array_almost_equal(actual, desired, decimal=15)
  745. def test_standard_t(self):
  746. np.random.seed(self.seed)
  747. actual = np.random.standard_t(df=10, size=(3, 2))
  748. desired = np.array([[0.97140611862659965, -0.08830486548450577],
  749. [1.36311143689505321, -0.55317463909867071],
  750. [-0.18473749069684214, 0.61181537341755321]])
  751. assert_array_almost_equal(actual, desired, decimal=15)
  752. def test_triangular(self):
  753. np.random.seed(self.seed)
  754. actual = np.random.triangular(left=5.12, mode=10.23, right=20.34,
  755. size=(3, 2))
  756. desired = np.array([[12.68117178949215784, 12.4129206149193152],
  757. [16.20131377335158263, 16.25692138747600524],
  758. [11.20400690911820263, 14.4978144835829923]])
  759. assert_array_almost_equal(actual, desired, decimal=14)
  760. def test_uniform(self):
  761. np.random.seed(self.seed)
  762. actual = np.random.uniform(low=1.23, high=10.54, size=(3, 2))
  763. desired = np.array([[6.99097932346268003, 6.73801597444323974],
  764. [9.50364421400426274, 9.53130618907631089],
  765. [5.48995325769805476, 8.47493103280052118]])
  766. assert_array_almost_equal(actual, desired, decimal=15)
  767. def test_uniform_range_bounds(self):
  768. fmin = np.finfo('float').min
  769. fmax = np.finfo('float').max
  770. func = np.random.uniform
  771. assert_raises(OverflowError, func, -np.inf, 0)
  772. assert_raises(OverflowError, func, 0, np.inf)
  773. assert_raises(OverflowError, func, fmin, fmax)
  774. assert_raises(OverflowError, func, [-np.inf], [0])
  775. assert_raises(OverflowError, func, [0], [np.inf])
  776. # (fmax / 1e17) - fmin is within range, so this should not throw
  777. # account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX >
  778. # DBL_MAX by increasing fmin a bit
  779. np.random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)
  780. def test_scalar_exception_propagation(self):
  781. # Tests that exceptions are correctly propagated in distributions
  782. # when called with objects that throw exceptions when converted to
  783. # scalars.
  784. #
  785. # Regression test for gh: 8865
  786. class ThrowingFloat(np.ndarray):
  787. def __float__(self):
  788. raise TypeError
  789. throwing_float = np.array(1.0).view(ThrowingFloat)
  790. assert_raises(TypeError, np.random.uniform, throwing_float,
  791. throwing_float)
  792. class ThrowingInteger(np.ndarray):
  793. def __int__(self):
  794. raise TypeError
  795. __index__ = __int__
  796. throwing_int = np.array(1).view(ThrowingInteger)
  797. assert_raises(TypeError, np.random.hypergeometric, throwing_int, 1, 1)
  798. def test_vonmises(self):
  799. np.random.seed(self.seed)
  800. actual = np.random.vonmises(mu=1.23, kappa=1.54, size=(3, 2))
  801. desired = np.array([[2.28567572673902042, 2.89163838442285037],
  802. [0.38198375564286025, 2.57638023113890746],
  803. [1.19153771588353052, 1.83509849681825354]])
  804. assert_array_almost_equal(actual, desired, decimal=15)
  805. def test_vonmises_small(self):
  806. # check infinite loop, gh-4720
  807. np.random.seed(self.seed)
  808. r = np.random.vonmises(mu=0., kappa=1.1e-8, size=10**6)
  809. np.testing.assert_(np.isfinite(r).all())
  810. def test_wald(self):
  811. np.random.seed(self.seed)
  812. actual = np.random.wald(mean=1.23, scale=1.54, size=(3, 2))
  813. desired = np.array([[3.82935265715889983, 5.13125249184285526],
  814. [0.35045403618358717, 1.50832396872003538],
  815. [0.24124319895843183, 0.22031101461955038]])
  816. assert_array_almost_equal(actual, desired, decimal=14)
  817. def test_weibull(self):
  818. np.random.seed(self.seed)
  819. actual = np.random.weibull(a=1.23, size=(3, 2))
  820. desired = np.array([[0.97097342648766727, 0.91422896443565516],
  821. [1.89517770034962929, 1.91414357960479564],
  822. [0.67057783752390987, 1.39494046635066793]])
  823. assert_array_almost_equal(actual, desired, decimal=15)
  824. def test_weibull_0(self):
  825. np.random.seed(self.seed)
  826. assert_equal(np.random.weibull(a=0, size=12), np.zeros(12))
  827. assert_raises(ValueError, np.random.weibull, a=-0.)
  828. def test_zipf(self):
  829. np.random.seed(self.seed)
  830. actual = np.random.zipf(a=1.23, size=(3, 2))
  831. desired = np.array([[66, 29],
  832. [1, 1],
  833. [3, 13]])
  834. assert_array_equal(actual, desired)
  835. class TestBroadcast(object):
  836. # tests that functions that broadcast behave
  837. # correctly when presented with non-scalar arguments
  838. def setup(self):
  839. self.seed = 123456789
  840. def setSeed(self):
  841. np.random.seed(self.seed)
  842. # TODO: Include test for randint once it can broadcast
  843. # Can steal the test written in PR #6938
  844. def test_uniform(self):
  845. low = [0]
  846. high = [1]
  847. uniform = np.random.uniform
  848. desired = np.array([0.53283302478975902,
  849. 0.53413660089041659,
  850. 0.50955303552646702])
  851. self.setSeed()
  852. actual = uniform(low * 3, high)
  853. assert_array_almost_equal(actual, desired, decimal=14)
  854. self.setSeed()
  855. actual = uniform(low, high * 3)
  856. assert_array_almost_equal(actual, desired, decimal=14)
  857. def test_normal(self):
  858. loc = [0]
  859. scale = [1]
  860. bad_scale = [-1]
  861. normal = np.random.normal
  862. desired = np.array([2.2129019979039612,
  863. 2.1283977976520019,
  864. 1.8417114045748335])
  865. self.setSeed()
  866. actual = normal(loc * 3, scale)
  867. assert_array_almost_equal(actual, desired, decimal=14)
  868. assert_raises(ValueError, normal, loc * 3, bad_scale)
  869. self.setSeed()
  870. actual = normal(loc, scale * 3)
  871. assert_array_almost_equal(actual, desired, decimal=14)
  872. assert_raises(ValueError, normal, loc, bad_scale * 3)
  873. def test_beta(self):
  874. a = [1]
  875. b = [2]
  876. bad_a = [-1]
  877. bad_b = [-2]
  878. beta = np.random.beta
  879. desired = np.array([0.19843558305989056,
  880. 0.075230336409423643,
  881. 0.24976865978980844])
  882. self.setSeed()
  883. actual = beta(a * 3, b)
  884. assert_array_almost_equal(actual, desired, decimal=14)
  885. assert_raises(ValueError, beta, bad_a * 3, b)
  886. assert_raises(ValueError, beta, a * 3, bad_b)
  887. self.setSeed()
  888. actual = beta(a, b * 3)
  889. assert_array_almost_equal(actual, desired, decimal=14)
  890. assert_raises(ValueError, beta, bad_a, b * 3)
  891. assert_raises(ValueError, beta, a, bad_b * 3)
  892. def test_exponential(self):
  893. scale = [1]
  894. bad_scale = [-1]
  895. exponential = np.random.exponential
  896. desired = np.array([0.76106853658845242,
  897. 0.76386282278691653,
  898. 0.71243813125891797])
  899. self.setSeed()
  900. actual = exponential(scale * 3)
  901. assert_array_almost_equal(actual, desired, decimal=14)
  902. assert_raises(ValueError, exponential, bad_scale * 3)
  903. def test_standard_gamma(self):
  904. shape = [1]
  905. bad_shape = [-1]
  906. std_gamma = np.random.standard_gamma
  907. desired = np.array([0.76106853658845242,
  908. 0.76386282278691653,
  909. 0.71243813125891797])
  910. self.setSeed()
  911. actual = std_gamma(shape * 3)
  912. assert_array_almost_equal(actual, desired, decimal=14)
  913. assert_raises(ValueError, std_gamma, bad_shape * 3)
  914. def test_gamma(self):
  915. shape = [1]
  916. scale = [2]
  917. bad_shape = [-1]
  918. bad_scale = [-2]
  919. gamma = np.random.gamma
  920. desired = np.array([1.5221370731769048,
  921. 1.5277256455738331,
  922. 1.4248762625178359])
  923. self.setSeed()
  924. actual = gamma(shape * 3, scale)
  925. assert_array_almost_equal(actual, desired, decimal=14)
  926. assert_raises(ValueError, gamma, bad_shape * 3, scale)
  927. assert_raises(ValueError, gamma, shape * 3, bad_scale)
  928. self.setSeed()
  929. actual = gamma(shape, scale * 3)
  930. assert_array_almost_equal(actual, desired, decimal=14)
  931. assert_raises(ValueError, gamma, bad_shape, scale * 3)
  932. assert_raises(ValueError, gamma, shape, bad_scale * 3)
  933. def test_f(self):
  934. dfnum = [1]
  935. dfden = [2]
  936. bad_dfnum = [-1]
  937. bad_dfden = [-2]
  938. f = np.random.f
  939. desired = np.array([0.80038951638264799,
  940. 0.86768719635363512,
  941. 2.7251095168386801])
  942. self.setSeed()
  943. actual = f(dfnum * 3, dfden)
  944. assert_array_almost_equal(actual, desired, decimal=14)
  945. assert_raises(ValueError, f, bad_dfnum * 3, dfden)
  946. assert_raises(ValueError, f, dfnum * 3, bad_dfden)
  947. self.setSeed()
  948. actual = f(dfnum, dfden * 3)
  949. assert_array_almost_equal(actual, desired, decimal=14)
  950. assert_raises(ValueError, f, bad_dfnum, dfden * 3)
  951. assert_raises(ValueError, f, dfnum, bad_dfden * 3)
  952. def test_noncentral_f(self):
  953. dfnum = [2]
  954. dfden = [3]
  955. nonc = [4]
  956. bad_dfnum = [0]
  957. bad_dfden = [-1]
  958. bad_nonc = [-2]
  959. nonc_f = np.random.noncentral_f
  960. desired = np.array([9.1393943263705211,
  961. 13.025456344595602,
  962. 8.8018098359100545])
  963. self.setSeed()
  964. actual = nonc_f(dfnum * 3, dfden, nonc)
  965. assert_array_almost_equal(actual, desired, decimal=14)
  966. assert_raises(ValueError, nonc_f, bad_dfnum * 3, dfden, nonc)
  967. assert_raises(ValueError, nonc_f, dfnum * 3, bad_dfden, nonc)
  968. assert_raises(ValueError, nonc_f, dfnum * 3, dfden, bad_nonc)
  969. self.setSeed()
  970. actual = nonc_f(dfnum, dfden * 3, nonc)
  971. assert_array_almost_equal(actual, desired, decimal=14)
  972. assert_raises(ValueError, nonc_f, bad_dfnum, dfden * 3, nonc)
  973. assert_raises(ValueError, nonc_f, dfnum, bad_dfden * 3, nonc)
  974. assert_raises(ValueError, nonc_f, dfnum, dfden * 3, bad_nonc)
  975. self.setSeed()
  976. actual = nonc_f(dfnum, dfden, nonc * 3)
  977. assert_array_almost_equal(actual, desired, decimal=14)
  978. assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3)
  979. assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3)
  980. assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3)
  981. def test_noncentral_f_small_df(self):
  982. self.setSeed()
  983. desired = np.array([6.869638627492048, 0.785880199263955])
  984. actual = np.random.noncentral_f(0.9, 0.9, 2, size=2)
  985. assert_array_almost_equal(actual, desired, decimal=14)
  986. def test_chisquare(self):
  987. df = [1]
  988. bad_df = [-1]
  989. chisquare = np.random.chisquare
  990. desired = np.array([0.57022801133088286,
  991. 0.51947702108840776,
  992. 0.1320969254923558])
  993. self.setSeed()
  994. actual = chisquare(df * 3)
  995. assert_array_almost_equal(actual, desired, decimal=14)
  996. assert_raises(ValueError, chisquare, bad_df * 3)
  997. def test_noncentral_chisquare(self):
  998. df = [1]
  999. nonc = [2]
  1000. bad_df = [-1]
  1001. bad_nonc = [-2]
  1002. nonc_chi = np.random.noncentral_chisquare
  1003. desired = np.array([9.0015599467913763,
  1004. 4.5804135049718742,
  1005. 6.0872302432834564])
  1006. self.setSeed()
  1007. actual = nonc_chi(df * 3, nonc)
  1008. assert_array_almost_equal(actual, desired, decimal=14)
  1009. assert_raises(ValueError, nonc_chi, bad_df * 3, nonc)
  1010. assert_raises(ValueError, nonc_chi, df * 3, bad_nonc)
  1011. self.setSeed()
  1012. actual = nonc_chi(df, nonc * 3)
  1013. assert_array_almost_equal(actual, desired, decimal=14)
  1014. assert_raises(ValueError, nonc_chi, bad_df, nonc * 3)
  1015. assert_raises(ValueError, nonc_chi, df, bad_nonc * 3)
  1016. def test_standard_t(self):
  1017. df = [1]
  1018. bad_df = [-1]
  1019. t = np.random.standard_t
  1020. desired = np.array([3.0702872575217643,
  1021. 5.8560725167361607,
  1022. 1.0274791436474273])
  1023. self.setSeed()
  1024. actual = t(df * 3)
  1025. assert_array_almost_equal(actual, desired, decimal=14)
  1026. assert_raises(ValueError, t, bad_df * 3)
  1027. def test_vonmises(self):
  1028. mu = [2]
  1029. kappa = [1]
  1030. bad_kappa = [-1]
  1031. vonmises = np.random.vonmises
  1032. desired = np.array([2.9883443664201312,
  1033. -2.7064099483995943,
  1034. -1.8672476700665914])
  1035. self.setSeed()
  1036. actual = vonmises(mu * 3, kappa)
  1037. assert_array_almost_equal(actual, desired, decimal=14)
  1038. assert_raises(ValueError, vonmises, mu * 3, bad_kappa)
  1039. self.setSeed()
  1040. actual = vonmises(mu, kappa * 3)
  1041. assert_array_almost_equal(actual, desired, decimal=14)
  1042. assert_raises(ValueError, vonmises, mu, bad_kappa * 3)
  1043. def test_pareto(self):
  1044. a = [1]
  1045. bad_a = [-1]
  1046. pareto = np.random.pareto
  1047. desired = np.array([1.1405622680198362,
  1048. 1.1465519762044529,
  1049. 1.0389564467453547])
  1050. self.setSeed()
  1051. actual = pareto(a * 3)
  1052. assert_array_almost_equal(actual, desired, decimal=14)
  1053. assert_raises(ValueError, pareto, bad_a * 3)
  1054. def test_weibull(self):
  1055. a = [1]
  1056. bad_a = [-1]
  1057. weibull = np.random.weibull
  1058. desired = np.array([0.76106853658845242,
  1059. 0.76386282278691653,
  1060. 0.71243813125891797])
  1061. self.setSeed()
  1062. actual = weibull(a * 3)
  1063. assert_array_almost_equal(actual, desired, decimal=14)
  1064. assert_raises(ValueError, weibull, bad_a * 3)
  1065. def test_power(self):
  1066. a = [1]
  1067. bad_a = [-1]
  1068. power = np.random.power
  1069. desired = np.array([0.53283302478975902,
  1070. 0.53413660089041659,
  1071. 0.50955303552646702])
  1072. self.setSeed()
  1073. actual = power(a * 3)
  1074. assert_array_almost_equal(actual, desired, decimal=14)
  1075. assert_raises(ValueError, power, bad_a * 3)
  1076. def test_laplace(self):
  1077. loc = [0]
  1078. scale = [1]
  1079. bad_scale = [-1]
  1080. laplace = np.random.laplace
  1081. desired = np.array([0.067921356028507157,
  1082. 0.070715642226971326,
  1083. 0.019290950698972624])
  1084. self.setSeed()
  1085. actual = laplace(loc * 3, scale)
  1086. assert_array_almost_equal(actual, desired, decimal=14)
  1087. assert_raises(ValueError, laplace, loc * 3, bad_scale)
  1088. self.setSeed()
  1089. actual = laplace(loc, scale * 3)
  1090. assert_array_almost_equal(actual, desired, decimal=14)
  1091. assert_raises(ValueError, laplace, loc, bad_scale * 3)
  1092. def test_gumbel(self):
  1093. loc = [0]
  1094. scale = [1]
  1095. bad_scale = [-1]
  1096. gumbel = np.random.gumbel
  1097. desired = np.array([0.2730318639556768,
  1098. 0.26936705726291116,
  1099. 0.33906220393037939])
  1100. self.setSeed()
  1101. actual = gumbel(loc * 3, scale)
  1102. assert_array_almost_equal(actual, desired, decimal=14)
  1103. assert_raises(ValueError, gumbel, loc * 3, bad_scale)
  1104. self.setSeed()
  1105. actual = gumbel(loc, scale * 3)
  1106. assert_array_almost_equal(actual, desired, decimal=14)
  1107. assert_raises(ValueError, gumbel, loc, bad_scale * 3)
  1108. def test_logistic(self):
  1109. loc = [0]
  1110. scale = [1]
  1111. bad_scale = [-1]
  1112. logistic = np.random.logistic
  1113. desired = np.array([0.13152135837586171,
  1114. 0.13675915696285773,
  1115. 0.038216792802833396])
  1116. self.setSeed()
  1117. actual = logistic(loc * 3, scale)
  1118. assert_array_almost_equal(actual, desired, decimal=14)
  1119. assert_raises(ValueError, logistic, loc * 3, bad_scale)
  1120. self.setSeed()
  1121. actual = logistic(loc, scale * 3)
  1122. assert_array_almost_equal(actual, desired, decimal=14)
  1123. assert_raises(ValueError, logistic, loc, bad_scale * 3)
  1124. def test_lognormal(self):
  1125. mean = [0]
  1126. sigma = [1]
  1127. bad_sigma = [-1]
  1128. lognormal = np.random.lognormal
  1129. desired = np.array([9.1422086044848427,
  1130. 8.4013952870126261,
  1131. 6.3073234116578671])
  1132. self.setSeed()
  1133. actual = lognormal(mean * 3, sigma)
  1134. assert_array_almost_equal(actual, desired, decimal=14)
  1135. assert_raises(ValueError, lognormal, mean * 3, bad_sigma)
  1136. self.setSeed()
  1137. actual = lognormal(mean, sigma * 3)
  1138. assert_array_almost_equal(actual, desired, decimal=14)
  1139. assert_raises(ValueError, lognormal, mean, bad_sigma * 3)
  1140. def test_rayleigh(self):
  1141. scale = [1]
  1142. bad_scale = [-1]
  1143. rayleigh = np.random.rayleigh
  1144. desired = np.array([1.2337491937897689,
  1145. 1.2360119924878694,
  1146. 1.1936818095781789])
  1147. self.setSeed()
  1148. actual = rayleigh(scale * 3)
  1149. assert_array_almost_equal(actual, desired, decimal=14)
  1150. assert_raises(ValueError, rayleigh, bad_scale * 3)
  1151. def test_wald(self):
  1152. mean = [0.5]
  1153. scale = [1]
  1154. bad_mean = [0]
  1155. bad_scale = [-2]
  1156. wald = np.random.wald
  1157. desired = np.array([0.11873681120271318,
  1158. 0.12450084820795027,
  1159. 0.9096122728408238])
  1160. self.setSeed()
  1161. actual = wald(mean * 3, scale)
  1162. assert_array_almost_equal(actual, desired, decimal=14)
  1163. assert_raises(ValueError, wald, bad_mean * 3, scale)
  1164. assert_raises(ValueError, wald, mean * 3, bad_scale)
  1165. self.setSeed()
  1166. actual = wald(mean, scale * 3)
  1167. assert_array_almost_equal(actual, desired, decimal=14)
  1168. assert_raises(ValueError, wald, bad_mean, scale * 3)
  1169. assert_raises(ValueError, wald, mean, bad_scale * 3)
  1170. assert_raises(ValueError, wald, 0.0, 1)
  1171. assert_raises(ValueError, wald, 0.5, 0.0)
  1172. def test_triangular(self):
  1173. left = [1]
  1174. right = [3]
  1175. mode = [2]
  1176. bad_left_one = [3]
  1177. bad_mode_one = [4]
  1178. bad_left_two, bad_mode_two = right * 2
  1179. triangular = np.random.triangular
  1180. desired = np.array([2.03339048710429,
  1181. 2.0347400359389356,
  1182. 2.0095991069536208])
  1183. self.setSeed()
  1184. actual = triangular(left * 3, mode, right)
  1185. assert_array_almost_equal(actual, desired, decimal=14)
  1186. assert_raises(ValueError, triangular, bad_left_one * 3, mode, right)
  1187. assert_raises(ValueError, triangular, left * 3, bad_mode_one, right)
  1188. assert_raises(ValueError, triangular, bad_left_two * 3, bad_mode_two,
  1189. right)
  1190. self.setSeed()
  1191. actual = triangular(left, mode * 3, right)
  1192. assert_array_almost_equal(actual, desired, decimal=14)
  1193. assert_raises(ValueError, triangular, bad_left_one, mode * 3, right)
  1194. assert_raises(ValueError, triangular, left, bad_mode_one * 3, right)
  1195. assert_raises(ValueError, triangular, bad_left_two, bad_mode_two * 3,
  1196. right)
  1197. self.setSeed()
  1198. actual = triangular(left, mode, right * 3)
  1199. assert_array_almost_equal(actual, desired, decimal=14)
  1200. assert_raises(ValueError, triangular, bad_left_one, mode, right * 3)
  1201. assert_raises(ValueError, triangular, left, bad_mode_one, right * 3)
  1202. assert_raises(ValueError, triangular, bad_left_two, bad_mode_two,
  1203. right * 3)
  1204. def test_binomial(self):
  1205. n = [1]
  1206. p = [0.5]
  1207. bad_n = [-1]
  1208. bad_p_one = [-1]
  1209. bad_p_two = [1.5]
  1210. binom = np.random.binomial
  1211. desired = np.array([1, 1, 1])
  1212. self.setSeed()
  1213. actual = binom(n * 3, p)
  1214. assert_array_equal(actual, desired)
  1215. assert_raises(ValueError, binom, bad_n * 3, p)
  1216. assert_raises(ValueError, binom, n * 3, bad_p_one)
  1217. assert_raises(ValueError, binom, n * 3, bad_p_two)
  1218. self.setSeed()
  1219. actual = binom(n, p * 3)
  1220. assert_array_equal(actual, desired)
  1221. assert_raises(ValueError, binom, bad_n, p * 3)
  1222. assert_raises(ValueError, binom, n, bad_p_one * 3)
  1223. assert_raises(ValueError, binom, n, bad_p_two * 3)
  1224. def test_negative_binomial(self):
  1225. n = [1]
  1226. p = [0.5]
  1227. bad_n = [-1]
  1228. bad_p_one = [-1]
  1229. bad_p_two = [1.5]
  1230. neg_binom = np.random.negative_binomial
  1231. desired = np.array([1, 0, 1])
  1232. self.setSeed()
  1233. actual = neg_binom(n * 3, p)
  1234. assert_array_equal(actual, desired)
  1235. assert_raises(ValueError, neg_binom, bad_n * 3, p)
  1236. assert_raises(ValueError, neg_binom, n * 3, bad_p_one)
  1237. assert_raises(ValueError, neg_binom, n * 3, bad_p_two)
  1238. self.setSeed()
  1239. actual = neg_binom(n, p * 3)
  1240. assert_array_equal(actual, desired)
  1241. assert_raises(ValueError, neg_binom, bad_n, p * 3)
  1242. assert_raises(ValueError, neg_binom, n, bad_p_one * 3)
  1243. assert_raises(ValueError, neg_binom, n, bad_p_two * 3)
  1244. def test_poisson(self):
  1245. max_lam = np.random.RandomState()._poisson_lam_max
  1246. lam = [1]
  1247. bad_lam_one = [-1]
  1248. bad_lam_two = [max_lam * 2]
  1249. poisson = np.random.poisson
  1250. desired = np.array([1, 1, 0])
  1251. self.setSeed()
  1252. actual = poisson(lam * 3)
  1253. assert_array_equal(actual, desired)
  1254. assert_raises(ValueError, poisson, bad_lam_one * 3)
  1255. assert_raises(ValueError, poisson, bad_lam_two * 3)
  1256. def test_zipf(self):
  1257. a = [2]
  1258. bad_a = [0]
  1259. zipf = np.random.zipf
  1260. desired = np.array([2, 2, 1])
  1261. self.setSeed()
  1262. actual = zipf(a * 3)
  1263. assert_array_equal(actual, desired)
  1264. assert_raises(ValueError, zipf, bad_a * 3)
  1265. with np.errstate(invalid='ignore'):
  1266. assert_raises(ValueError, zipf, np.nan)
  1267. assert_raises(ValueError, zipf, [0, 0, np.nan])
  1268. def test_geometric(self):
  1269. p = [0.5]
  1270. bad_p_one = [-1]
  1271. bad_p_two = [1.5]
  1272. geom = np.random.geometric
  1273. desired = np.array([2, 2, 2])
  1274. self.setSeed()
  1275. actual = geom(p * 3)
  1276. assert_array_equal(actual, desired)
  1277. assert_raises(ValueError, geom, bad_p_one * 3)
  1278. assert_raises(ValueError, geom, bad_p_two * 3)
  1279. def test_hypergeometric(self):
  1280. ngood = [1]
  1281. nbad = [2]
  1282. nsample = [2]
  1283. bad_ngood = [-1]
  1284. bad_nbad = [-2]
  1285. bad_nsample_one = [0]
  1286. bad_nsample_two = [4]
  1287. hypergeom = np.random.hypergeometric
  1288. desired = np.array([1, 1, 1])
  1289. self.setSeed()
  1290. actual = hypergeom(ngood * 3, nbad, nsample)
  1291. assert_array_equal(actual, desired)
  1292. assert_raises(ValueError, hypergeom, bad_ngood * 3, nbad, nsample)
  1293. assert_raises(ValueError, hypergeom, ngood * 3, bad_nbad, nsample)
  1294. assert_raises(ValueError, hypergeom, ngood * 3, nbad, bad_nsample_one)
  1295. assert_raises(ValueError, hypergeom, ngood * 3, nbad, bad_nsample_two)
  1296. self.setSeed()
  1297. actual = hypergeom(ngood, nbad * 3, nsample)
  1298. assert_array_equal(actual, desired)
  1299. assert_raises(ValueError, hypergeom, bad_ngood, nbad * 3, nsample)
  1300. assert_raises(ValueError, hypergeom, ngood, bad_nbad * 3, nsample)
  1301. assert_raises(ValueError, hypergeom, ngood, nbad * 3, bad_nsample_one)
  1302. assert_raises(ValueError, hypergeom, ngood, nbad * 3, bad_nsample_two)
  1303. self.setSeed()
  1304. actual = hypergeom(ngood, nbad, nsample * 3)
  1305. assert_array_equal(actual, desired)
  1306. assert_raises(ValueError, hypergeom, bad_ngood, nbad, nsample * 3)
  1307. assert_raises(ValueError, hypergeom, ngood, bad_nbad, nsample * 3)
  1308. assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_one * 3)
  1309. assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_two * 3)
  1310. def test_logseries(self):
  1311. p = [0.5]
  1312. bad_p_one = [2]
  1313. bad_p_two = [-1]
  1314. logseries = np.random.logseries
  1315. desired = np.array([1, 1, 1])
  1316. self.setSeed()
  1317. actual = logseries(p * 3)
  1318. assert_array_equal(actual, desired)
  1319. assert_raises(ValueError, logseries, bad_p_one * 3)
  1320. assert_raises(ValueError, logseries, bad_p_two * 3)
  1321. class TestThread(object):
  1322. # make sure each state produces the same sequence even in threads
  1323. def setup(self):
  1324. self.seeds = range(4)
  1325. def check_function(self, function, sz):
  1326. from threading import Thread
  1327. out1 = np.empty((len(self.seeds),) + sz)
  1328. out2 = np.empty((len(self.seeds),) + sz)
  1329. # threaded generation
  1330. t = [Thread(target=function, args=(np.random.RandomState(s), o))
  1331. for s, o in zip(self.seeds, out1)]
  1332. [x.start() for x in t]
  1333. [x.join() for x in t]
  1334. # the same serial
  1335. for s, o in zip(self.seeds, out2):
  1336. function(np.random.RandomState(s), o)
  1337. # these platforms change x87 fpu precision mode in threads
  1338. if np.intp().dtype.itemsize == 4 and sys.platform == "win32":
  1339. assert_array_almost_equal(out1, out2)
  1340. else:
  1341. assert_array_equal(out1, out2)
  1342. def test_normal(self):
  1343. def gen_random(state, out):
  1344. out[...] = state.normal(size=10000)
  1345. self.check_function(gen_random, sz=(10000,))
  1346. def test_exp(self):
  1347. def gen_random(state, out):
  1348. out[...] = state.exponential(scale=np.ones((100, 1000)))
  1349. self.check_function(gen_random, sz=(100, 1000))
  1350. def test_multinomial(self):
  1351. def gen_random(state, out):
  1352. out[...] = state.multinomial(10, [1/6.]*6, size=10000)
  1353. self.check_function(gen_random, sz=(10000, 6))
  1354. # See Issue #4263
  1355. class TestSingleEltArrayInput(object):
  1356. def setup(self):
  1357. self.argOne = np.array([2])
  1358. self.argTwo = np.array([3])
  1359. self.argThree = np.array([4])
  1360. self.tgtShape = (1,)
  1361. def test_one_arg_funcs(self):
  1362. funcs = (np.random.exponential, np.random.standard_gamma,
  1363. np.random.chisquare, np.random.standard_t,
  1364. np.random.pareto, np.random.weibull,
  1365. np.random.power, np.random.rayleigh,
  1366. np.random.poisson, np.random.zipf,
  1367. np.random.geometric, np.random.logseries)
  1368. probfuncs = (np.random.geometric, np.random.logseries)
  1369. for func in funcs:
  1370. if func in probfuncs: # p < 1.0
  1371. out = func(np.array([0.5]))
  1372. else:
  1373. out = func(self.argOne)
  1374. assert_equal(out.shape, self.tgtShape)
  1375. def test_two_arg_funcs(self):
  1376. funcs = (np.random.uniform, np.random.normal,
  1377. np.random.beta, np.random.gamma,
  1378. np.random.f, np.random.noncentral_chisquare,
  1379. np.random.vonmises, np.random.laplace,
  1380. np.random.gumbel, np.random.logistic,
  1381. np.random.lognormal, np.random.wald,
  1382. np.random.binomial, np.random.negative_binomial)
  1383. probfuncs = (np.random.binomial, np.random.negative_binomial)
  1384. for func in funcs:
  1385. if func in probfuncs: # p <= 1
  1386. argTwo = np.array([0.5])
  1387. else:
  1388. argTwo = self.argTwo
  1389. out = func(self.argOne, argTwo)
  1390. assert_equal(out.shape, self.tgtShape)
  1391. out = func(self.argOne[0], argTwo)
  1392. assert_equal(out.shape, self.tgtShape)
  1393. out = func(self.argOne, argTwo[0])
  1394. assert_equal(out.shape, self.tgtShape)
  1395. # TODO: Uncomment once randint can broadcast arguments
  1396. # def test_randint(self):
  1397. # itype = [bool, np.int8, np.uint8, np.int16, np.uint16,
  1398. # np.int32, np.uint32, np.int64, np.uint64]
  1399. # func = np.random.randint
  1400. # high = np.array([1])
  1401. # low = np.array([0])
  1402. #
  1403. # for dt in itype:
  1404. # out = func(low, high, dtype=dt)
  1405. # self.assert_equal(out.shape, self.tgtShape)
  1406. #
  1407. # out = func(low[0], high, dtype=dt)
  1408. # self.assert_equal(out.shape, self.tgtShape)
  1409. #
  1410. # out = func(low, high[0], dtype=dt)
  1411. # self.assert_equal(out.shape, self.tgtShape)
  1412. def test_three_arg_funcs(self):
  1413. funcs = [np.random.noncentral_f, np.random.triangular,
  1414. np.random.hypergeometric]
  1415. for func in funcs:
  1416. out = func(self.argOne, self.argTwo, self.argThree)
  1417. assert_equal(out.shape, self.tgtShape)
  1418. out = func(self.argOne[0], self.argTwo, self.argThree)
  1419. assert_equal(out.shape, self.tgtShape)
  1420. out = func(self.argOne, self.argTwo[0], self.argThree)
  1421. assert_equal(out.shape, self.tgtShape)