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 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
85 changes: 85 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,91 @@ 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_arma_process(ar, ma=0, samples=100000):
"""
Generates a realization of an ARMA(ar, ma) process.
"""
# a small noise so that the coefficients are well determined
noise = np.random.normal(scale=0.01, size=samples)

if ar > 0:
# when sum of coefficients is smaller than 1 the process is stationary
coefficients_ar = 0.99 * np.ones(shape=ar) / ar
else:
coefficients_ar = []

if ma > 0:
coefficients_ma = 0.5 * np.ones(shape=ma)
else:
coefficients_ma = []

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

for i in range(max(ar, ma), samples):
x[i] = np.sum(coefficients_ar[d] * x[i - 1 - d] for d in range(0, ar)) + \
np.sum(coefficients_ma[d] * noise[i - 1 - d] for d in range(0, ma)) + noise[i]

return x[max(ar, ma):]


def _test_ARMA(ar, ma):
"""
Tests that a ARMA(ar, ma) model fits an ARMA(ar, ma) process.
"""
data = get_arma_process(ar=ar, ma=ma)

model = ARIMA(data=data, ar=ar, ma=ma)
coefficients = model.fit().z.get_z_values(transformed=True)

assert (len(coefficients) == ar + ma + 2) # 1 constant + 1 for the noise scale

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

# AR coefficients within 10%
if ar > 0:
expected_ar = 0.99 / ar # same as used in `get_arma_process`
for ar_i in range(ar):
assert (np.abs(coefficients[1 + ar_i] - expected_ar) / expected_ar < 0.1)

# MA coefficients within 10%
if ma > 0:
expected_ma = 0.5 # same as used in `get_arma_process`
for ma_i in range(ar, ma):
assert (np.abs(coefficients[1 + ma_i] - expected_ma) / expected_ma < 0.1)

# Normal scale coefficient within 10%
expected_noise = 0.01 # same as used in `get_arma_process`
assert (np.abs(coefficients[-1] - expected_noise) / expected_noise < 0.1)


def test_AR1():
"""
Tests that an AR(1) model fits an AR(1) process.
"""
_test_ARMA(1, 0)


def test_AR2():
"""
Tests that an AR(2) model fits an AR(2) process.
"""
_test_ARMA(2, 0)


def test_AR3():
"""
Tests that an AR(3) model fits an AR(3) process.
"""
_test_ARMA(3, 0)


def test_MA1():
_test_ARMA(0, 1)


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