def test_laplace(self): rnd.seed(self.seed, self.brng) actual = rnd.laplace(loc=.123456789, scale=2.0, size=(3, 2)) desired = np.array([[0.15598087210935016, -3.3424589282252994], [-1.189978401356375, 3.0607925598732253], [0.0030946589024587745, 3.14795824463997]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_logistic(self): rnd.seed(self.seed, self.brng) actual = rnd.logistic(loc=.123456789, scale=2.0, size=(3, 2)) desired = np.array([[8.345015961402696, -7.749557532940552], [-2.9534419690278444, 1.910964962531448], [0.2719300361499433, 8.913100396613983]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_uniform(self): rnd.seed(self.seed, self.brng) actual = rnd.uniform(low=1.23, high=10.54, size=(3, 2)) desired = np.array([[10.38982478047721, 1.408218254214153], [2.8756430713785814, 7.836974412682466], [6.057706432128325, 10.426505200380925]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_wald(self): rnd.seed(self.seed, self.brng) actual = rnd.wald(mean=1.23, scale=1.54, size=(3, 2)) desired = np.array([[0.3465678392232347, 0.3594497155916536], [2.192908727996422, 1.7408141717513501], [1.1943709105062374, 0.3273455943060196]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_standard_cauchy(self): rnd.seed(self.seed, self.brng) actual = rnd.standard_cauchy(size=(3, 2)) desired = np.array([[19.716487700629912, -16.608240276131227], [-1.6117703817332278, 0.7739915895826882], [0.058344614106131, 26.09825325697747]]) np.testing.assert_array_almost_equal(actual, desired, decimal=9)
def test_standard_gamma(self): rnd.seed(self.seed, self.brng) actual = rnd.standard_gamma(shape=3, size=(3, 2)) desired = np.array([[2.939330965027084, 2.799606052259993], [4.988193705918075, 2.905305108691164], [3.2630929395548147, 2.772756340265377]]) np.testing.assert_array_almost_equal(actual, desired, decimal=7)
def test_beta(self): rnd.seed(self.seed, self.brng) actual = rnd.beta(.1, .9, size=(3, 2)) desired = np.array([[0.9856952034381025, 4.35869375658114e-08], [0.0014230232791189966, 1.4981856288121975e-06], [1.426135763875603e-06, 4.5801786040477326e-07]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_power(self): rnd.seed(self.seed, self.brng) actual = rnd.power(a=.123456789, size=(3, 2)) desired = np.array([[0.8765841803224415, 1.2140041091640163e-14], [8.013574117268635e-07, 0.06216255187464781], [0.004895628723087296, 0.9054248959192386]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def symmetric_random_walk(size, seed=None, scale=1.0, normalize=True): # 0. Preparation if seed != None: rnd.seed(seed) else: rnd.seed() time_steps = size[0] num_dims = len(size) - 1 # 1. Generate random walk noise levels (integer) random_walk = 2 * rnd.randint(2, size=time_steps) - 1 random_walk = np.cumsum(random_walk) # 2. Normalize random walk noise levels to the range [0,1] if normalize == True: random_walk += np.abs(random_walk.min()) random_walk = random_walk / random_walk.max() # 3. Scale random walk noise levels to max_level random_walk *= scale # 4. Generate noise noise = rnd.uniform(low=-np.sqrt(12) / 2, high=np.sqrt(12) / 2, size=size) # 5. Scale noise to desired std over time_steps random_walk = random_walk.reshape((-1, ) + (1, ) * num_dims) noise *= random_walk return noise.astype(np.float32), random_walk.astype(np.float32)
def test_random_sample(self): rnd.seed(self.seed, self.brng) actual = rnd.random_sample((3, 2)) desired = np.array([[0.9838694715872407, 0.01914266962558031], [0.1767608025111258, 0.7096642763353884], [0.518550637178123, 0.9878093663137406]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_randn(self): rnd.seed(self.seed, self.brng) actual = rnd.randn(3, 2) desired = np.array([[2.1411609928913298, -2.0717866791744819], [-0.92778018318550248, 0.55240420724917727], [0.04651632135517459, 2.2510674226058036]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_repeatability(self): import hashlib # We use a md5 hash of generated sequences of 1000 samples # in the range [0, 6) for all but np.bool, where the range # is [0, 2). Hashes are for little endian numbers. tgt = { 'bool': '4fee98a6885457da67c39331a9ec336f', 'int16': '80a5ff69c315ab6f80b03da1d570b656', 'int32': '15a3c379b6c7b0f296b162194eab68bc', 'int64': 'ea9875f9334c2775b00d4976b85a1458', 'int8': '0f56333af47de94930c799806158a274', 'uint16': '80a5ff69c315ab6f80b03da1d570b656', 'uint32': '15a3c379b6c7b0f296b162194eab68bc', 'uint64': 'ea9875f9334c2775b00d4976b85a1458', 'uint8': '0f56333af47de94930c799806158a274' } for dt in self.itype[1:]: rnd.seed(1234, brng='MT19937') # view as little endian for hash if sys.byteorder == 'little': val = self.rfunc(0, 6, size=1000, dtype=dt) else: val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap() res = hashlib.md5(val.view(np.int8)).hexdigest() print("") assert_(tgt[np.dtype(dt).name] == res) # bools do not depend on endianess rnd.seed(1234, brng='MT19937') val = self.rfunc(0, 2, size=1000, dtype=np.bool).view(np.int8) res = hashlib.md5(val).hexdigest() assert_(tgt[np.dtype(np.bool).name] == res)
def test_shuffle(self): # Test lists, arrays (of various dtypes), and multidimensional versions # of both, c-contiguous or not: for conv in [ lambda x: np.array([]), lambda x: x, lambda x: np.asarray(x).astype(np.int8), lambda x: np.asarray(x).astype(np.float32), lambda x: np.asarray(x).astype(np.complex64), lambda x: np.asarray(x).astype(object), lambda x: [(i, i) for i in x], lambda x: np.asarray([[i, i] for i in x]), lambda x: np.vstack([x, x]).T, # gh-4270 lambda x: np.asarray([(i, i) for i in x], [("a", object, (1, )), ("b", np.int32, (1, ))]) ]: rnd.seed(self.seed, self.brng) alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) rnd.shuffle(alist) actual = alist desired = conv([9, 8, 5, 1, 6, 4, 7, 2, 3, 0]) np.testing.assert_array_equal(actual, desired)
def test_weibull(self): rnd.seed(self.seed, self.brng) actual = rnd.weibull(a=1.23, size=(3, 2)) desired = np.array([[0.035129404330214734, 3.058859465984936], [1.5636393343788513, 0.4189406773709585], [0.710439924774508, 0.02793103204502023]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_chisquare(self): rnd.seed(self.seed, self.brng) actual = rnd.chisquare(50, size=(3, 2)) desired = np.array([[50.955833609920589, 50.133178918244099], [61.513615847062013, 50.757127871422448], [52.79816819717081, 49.973023331993552]]) np.testing.assert_array_almost_equal(actual, desired, decimal=7)
def test_noncentral_f(self): rnd.seed(self.seed, self.brng) actual = rnd.noncentral_f(dfnum=5, dfden=2, nonc=1, size=(3, 2)) desired = np.array([[0.2216297348371284, 0.7632696724492449], [98.67664232828238, 0.9500319825372799], [0.3489618249246971, 1.5035633972571092]]) np.testing.assert_array_almost_equal(actual, desired, decimal=7)
def test_exponential(self): rnd.seed(self.seed, self.brng) actual = rnd.exponential(1.1234, size=(3, 2)) desired = np.array([[0.01826877748252199, 4.4439855151117005], [1.9468048583654507, 0.38528493864979607], [0.7377565464231758, 0.013779117663987912]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_rayleigh(self): rnd.seed(self.seed, self.brng) actual = rnd.rayleigh(scale=10, size=(3, 2)) desired = np.array([[1.80344345931194, 28.127692489122378], [18.6169699930609, 8.282068232120208], [11.460520015934597, 1.5662406536967712]]) np.testing.assert_array_almost_equal(actual, desired, decimal=7)
def test_f(self): rnd.seed(self.seed, self.brng) actual = rnd.f(12, 77, size=(3, 2)) desired = np.array([[1.325076177478387, 0.8670927327120197], [2.1190792007836827, 0.9095296301824258], [1.4953697422236187, 0.9547125618834837]]) np.testing.assert_array_almost_equal(actual, desired, decimal=9)
def test_standard_exponential(self): rnd.seed(self.seed, self.brng) actual = rnd.standard_exponential(size=(3, 2)) desired = np.array([[0.016262041554675085, 3.955835423813157], [1.7329578586126497, 0.3429632710074738], [0.6567175951781875, 0.012265548926462446]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_gamma(self): rnd.seed(self.seed, self.brng) actual = rnd.gamma(5, 3, size=(3, 2)) desired = np.array([[15.073510060334929, 14.525495858042685], [22.73897210140115, 14.94044782480266], [16.327929995271095, 14.419692564592896]]) np.testing.assert_array_almost_equal(actual, desired, decimal=7)
def test_standard_t(self): rnd.seed(self.seed, self.brng) actual = rnd.standard_t(df=10, size=(3, 2)) desired = np.array([[-0.783927044239963, 0.04762883516531178], [0.7624597987725193, -1.8045540288955506], [-1.2657694296239195, 0.307870906117017]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_gumbel(self): rnd.seed(self.seed, self.brng) actual = rnd.gumbel(loc=.123456789, scale=2.0, size=(3, 2)) desired = np.array([[-8.114386462751979, 2.873840411460178], [1.2231161758452016, -2.0168070493213532], [-0.7175455966332102, -8.678464904504784]]) np.testing.assert_array_almost_equal(actual, desired, decimal=7)
def test_vonmises(self): rnd.seed(self.seed, self.brng) actual = rnd.vonmises(mu=1.23, kappa=1.54, size=(3, 2)) desired = np.array([[1.1027657269593822, 1.2539311427727782], [2.0281801137277764, 1.3262040229028056], [0.9510301598100863, 2.0284972823322818]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_multinomial(self): rnd.seed(self.seed, self.brng) actual = rnd.multinomial(20, [1 / 6.] * 6, size=(3, 2)) desired = np.array([[[7, 0, 2, 4, 4, 3], [7, 1, 2, 6, 4, 0]], [[2, 2, 3, 4, 6, 3], [1, 2, 5, 5, 6, 1]], [[2, 7, 4, 1, 2, 4], [3, 5, 2, 5, 4, 1]]]) np.testing.assert_array_equal(actual, desired)
def test_triangular(self): rnd.seed(self.seed, brng=self.brng) actual = rnd.triangular(left=5.12, mode=10.23, right=20.34, size=(3, 2)) desired = np.array([[18.764540652669638, 6.340166306695037], [8.827752689522429, 13.65605077739865], [11.732872979633328, 18.970392754850423]]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)
def test_shuffle_mixed_dimension(self): # Test for trac ticket #2074 for t in [[1, 2, 3, None], [(1, 1), (2, 2), (3, 3), None], [1, (2, 2), (3, 3), None], [(1, 1), 2, 3, None]]: rnd.seed(12345, brng='MT2203') shuffled = list(t) rnd.shuffle(shuffled) assert_array_equal(shuffled, [t[0], t[2], t[1], t[3]])
def test_call_within_randomstate(self): # Check that custom RandomState does not call into global state m = rnd.RandomState() res = np.array([5, 7, 5, 4, 5, 5, 6, 9, 6, 1]) for i in range(3): rnd.seed(i) m.seed(4321, brng='SFMT19937') # If m.state is not honored, the result will change assert_array_equal(m.choice(10, size=10, p=np.ones(10) / 10.), res)
def test_random_integers(self): rnd.seed(self.seed, self.brng) with suppress_warnings() as sup: w = sup.record(DeprecationWarning) actual = rnd.random_integers(-99, 99, size=(3, 2)) assert_(len(w) == 1) desired = np.array([[96, -96], [-64, 42], [4, 97]]) np.testing.assert_array_equal(actual, desired)
def test_wald(self): rnd.seed(self.seed, brng=self.brng) actual = rnd.wald(mean=1.23, scale=1.54, size=(3, 2)) desired = np.array( [[0.22448558337033758, 0.23485255518098838], [2.756850184899666, 2.005347850108636], [1.179918636588408, 0.20928649815442452] ]) np.testing.assert_array_almost_equal(actual, desired, decimal=10)