test_random.py 67 KB

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