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

FEAT: Add random_state option (issue #153) #329

Merged
merged 2 commits into from
Aug 30, 2017
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
13 changes: 11 additions & 2 deletions quantecon/arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
from numpy import conj, pi
from scipy.signal import dimpulse, freqz, dlsim
from .util import check_random_state


class ARMA:
Expand Down Expand Up @@ -230,7 +231,7 @@ def autocovariance(self, num_autocov=16):
# num_autocov should be <= len(acov) / 2
return acov[:num_autocov]

def simulation(self, ts_length=90):
def simulation(self, ts_length=90, random_state=None):
"""
Compute a simulated sample path assuming Gaussian shocks.

Expand All @@ -239,14 +240,22 @@ def simulation(self, ts_length=90):
ts_length : scalar(int), optional(default=90)
Number of periods to simulate for

random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.

Returns
-------
vals : array_like(float)
A simulation of the model that corresponds to this class

"""
random_state = check_random_state(random_state)

sys = self.ma_poly, self.ar_poly, 1
u = np.random.randn(ts_length, 1) * self.sigma
u = random_state.randn(ts_length, 1) * self.sigma
vals = dlsim(sys, u)[1]

return vals.flatten()
13 changes: 10 additions & 3 deletions quantecon/tests/test_arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
import os
import unittest
import numpy as np
from numpy.testing import assert_allclose
from numpy.testing import assert_array_equal
from quantecon.arma import ARMA


class TestARMA(unittest.TestCase):

def setUp(self):
# Initial Values
phi = np.array([.95, -.4, -.4])
Expand All @@ -26,7 +25,6 @@ def setUp(self):

self.lp = ARMA(phi, theta, sigma)


def tearDown(self):
del self.lp

Expand All @@ -37,13 +35,22 @@ def test_simulate(self):

self.assertTrue(sim.size==250)

def test_simulate_with_seed(self):
lp = self.lp
seed = 5
sim0 = lp.simulation(ts_length=10, random_state=seed)
sim1 = lp.simulation(ts_length=10, random_state=seed)

assert_array_equal(sim0, sim1)

def test_impulse_response(self):
lp = self.lp

imp_resp = lp.impulse_response(impulse_length=75)

self.assertTrue(imp_resp.size==75)


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestARMA)
unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)
Expand Down