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

Added failing test demonstrating that MA model does not fit MA process. #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Added tests to outcome of AR model against an AR process.
  • Loading branch information
jorgecarleitao committed Feb 1, 2018
commit 103d10ecc9b3bac563a0f7a0fd848127c8bacd23
67 changes: 67 additions & 0 deletions pyflux/arma/tests/test_arima_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,73 @@ def test_no_terms():
lvs = np.array([i.value for i in model.latent_variables.z_list])
assert(len(lvs[np.isnan(lvs)]) == 0)


def get_ar_process(ar, samples=10000):
"""
Generates a realization of an AR(ar) process.
"""
# a small noise so that the coefficients are well determined
noise = np.random.normal(scale=0.001, size=samples)

# sum of coefficients must be smaller than 1 for the process to be non-stationary
coefficients = 0.99*np.ones(shape=ar)/ar

# start is 1
x = np.ones(samples)

for i in range(ar, len(x)):
x[i] = np.sum(coefficients[d] * x[i - d - 1] for d in range(0, ar)) + noise[i]

return x


def _test_AR(ar):
"""
Tests that a AR(ar) model fits an AR(ar) process.
"""
data = get_ar_process(ar=ar)

model = ARIMA(data=data, ar=ar, ma=0)
x = model.fit()
lvs = np.array([i.value for i in model.latent_variables.z_list])
assert (len(lvs) == ar + 2)

coefficients = x.z.get_z_values(transformed=True)

# Constant coefficient within 10%
assert (np.abs(coefficients[0]) < 0.1)

expected = 0.99/ar

# AR coefficients within 10%
for ar_i in range(ar):
assert (np.abs(coefficients[1 + ar_i] - expected) / expected < 0.1)

# Normal scale coefficient within 10%
assert (np.abs(coefficients[-1] - 0.001) / 0.001 < 0.1)


def test_AR1():
"""
Tests that an AR(1) model fits an AR(1) process.
"""
_test_AR(1)


def test_AR2():
"""
Tests that an AR(2) model fits an AR(2) process.
"""
_test_AR(2)


def test_AR3():
"""
Tests that an AR(3) model fits an AR(3) process.
"""
_test_AR(3)


def test_couple_terms():
"""
Tests an ARIMA model with 1 AR and 1 MA term and that
Expand Down