From 91225e4c8120018b46c2c733855aae1ec41dce4b Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 2 May 2021 16:21:26 +0900 Subject: [PATCH 1/2] MAINT: Use multivariate_normal via random_state --- quantecon/lss.py | 4 ++-- quantecon/tests/test_lss.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/quantecon/lss.py b/quantecon/lss.py index 7e24ee000..9ac750709 100644 --- a/quantecon/lss.py +++ b/quantecon/lss.py @@ -10,7 +10,6 @@ from textwrap import dedent import numpy as np -from numpy.random import multivariate_normal from scipy.linalg import solve from .matrix_eqn import solve_discrete_lyapunov from numba import jit @@ -186,7 +185,8 @@ def simulate(self, ts_length=100, random_state=None): """ random_state = check_random_state(random_state) - x0 = multivariate_normal(self.mu_0.flatten(), self.Sigma_0) + x0 = random_state.multivariate_normal(self.mu_0.flatten(), + self.Sigma_0) w = random_state.randn(self.m, ts_length-1) v = self.C.dot(w) # Multiply each w_t by C to get v_t = C w_t # == simulate time series == # diff --git a/quantecon/tests/test_lss.py b/quantecon/tests/test_lss.py index 81acc8dab..0d392fafb 100644 --- a/quantecon/tests/test_lss.py +++ b/quantecon/tests/test_lss.py @@ -67,8 +67,8 @@ def test_simulate_with_seed(self): ss = self.ss1 xval, yval = ss.simulate(ts_length=5, random_state=5) - expected_output = np.array([0.75 , 0.73456137, 0.6812898, 0.76876387, - .71772107]) + expected_output = np.array([0.75, 0.69595649, 0.78269723, 0.73095776, + 0.69989036]) assert_allclose(xval[0], expected_output) assert_allclose(yval[0], expected_output) @@ -82,8 +82,8 @@ def test_replicate(self): def test_replicate_with_seed(self): xval, yval = self.ss1.replicate(T=100, num_reps=5, random_state=5) - expected_output = np.array([0.06871204, 0.06937119, -0.1478022, - 0.23841252, -0.06823762]) + expected_output = np.array([0.10498898, 0.02892168, 0.04915998, + 0.18568489, 0.04541764]) assert_allclose(xval[0], expected_output) assert_allclose(yval[0], expected_output) From ef777d6429b1ae26abfe29b086db6f47c4dafc89 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 2 May 2021 16:27:33 +0900 Subject: [PATCH 2/2] MAINT: PEP8 compliance --- quantecon/lss.py | 4 ++-- quantecon/tests/test_lss.py | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/quantecon/lss.py b/quantecon/lss.py index 9ac750709..e2851409e 100644 --- a/quantecon/lss.py +++ b/quantecon/lss.py @@ -447,8 +447,8 @@ def __partition(self): A_diag = np.diag(A) num_const = 0 for idx in range(n): - if (A_diag[idx] == 1) and (C[idx, :] == 0).all() \ - and np.linalg.norm(A[idx, :]) == 1: + if (A_diag[idx] == 1) and (C[idx, :] == 0).all() and \ + np.linalg.norm(A[idx, :]) == 1: sorted_idx.insert(0, idx) num_const += 1 else: diff --git a/quantecon/tests/test_lss.py b/quantecon/tests/test_lss.py index 0d392fafb..8b58f83fc 100644 --- a/quantecon/tests/test_lss.py +++ b/quantecon/tests/test_lss.py @@ -52,7 +52,10 @@ def test_stationarity(self): assert_allclose(ssmux.flatten(), np.array([2.5, 2.5, 1])) assert_allclose(ssmuy.flatten(), np.array([2.5])) - assert_allclose(sssigx, self.ss2.A @ sssigx @ self.ss2.A.T + self.ss2.C @ self.ss2.C.T) + assert_allclose( + sssigx, + self.ss2.A @ sssigx @ self.ss2.A.T + self.ss2.C @ self.ss2.C.T + ) assert_allclose(sssigy, self.ss2.G @ sssigx @ self.ss2.G.T) assert_allclose(sssigyx, self.ss2.G @ sssigx) @@ -61,7 +64,7 @@ def test_simulate(self): sim = ss.simulate(ts_length=250) for arr in sim: - self.assertTrue(len(arr[0])==250) + self.assertTrue(len(arr[0]) == 250) def test_simulate_with_seed(self): ss = self.ss1 @@ -101,4 +104,3 @@ def test_non_square_A(): if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(TestLinearStateSpace) unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite) -