test_random.py 65 KB

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