Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Use multivariate_normal via random_state #581

Merged
merged 2 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions quantecon/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 == #
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 9 additions & 7 deletions quantecon/tests/test_lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -61,14 +64,14 @@ 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

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)
Expand All @@ -82,8 +85,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)
Expand All @@ -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)