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

Add AR(p) and MA(q) models #117

Merged
merged 9 commits into from
Apr 2, 2024
Merged

Conversation

northern-64bit
Copy link
Contributor

@northern-64bit northern-64bit commented Mar 6, 2024

Here is the validation of the implementations working:

The AR model:

Implementation

    import numpy as np
    import pandas as pd
    import statsmodels.api as sm
    import time
    
    # Simulate an AR(1) process
    np.random.seed(0)  # For reproducibility
    n = 1000
    alpha = 0.5  # AR(1) coefficient
    sigma = 1  # Noise standard deviation
    ar_process = np.random.normal(0, sigma, n)
    for t in range(1, n):
        ar_process[t] += alpha * ar_process[t-1]
    
    # Fit models
    start_lsm = time.time()
    phi_lsm, c_lsm, sigma2_lsm = get_ar_weights_lsm(ar_process, 1)
    end_lsm = time.time()
    
    start_yw = time.time()
    phi_yw, c_yk, sigma2_yw = estimate_ar_weights_yule_walker(ar_process, 1)
    end_yw = time.time()
    
    start_sm_arima = time.time()
    model_sm_arima = sm.tsa.ARIMA(ar_process, order=(1, 0, 0)).fit()
    sigma2_sm_arima = model_sm_arima.resid.var()
    end_sm_arima = time.time()
    
    # Results comparison
    results_comparison = {
        "Custom LSM": {"Phi": phi_lsm, "Sigma2": sigma2_lsm, "Time": end_lsm - start_lsm},
        "Custom Yule-Walker": {"Phi": phi_yw, "Sigma2": sigma2_yw, "Time": end_yw - start_yw},
        "Statsmodels ARIMA": {"Phi": model_sm_arima.params[1], "Sigma2": sigma2_sm_arima, "Time": end_sm_arima - start_sm_arima}
    }
    
    results_comparison

Result

{'Custom LSM': {'Phi': 0.4907072990615945,
  'Sigma2': 0.9718186552765827,
  'Time': 0.0009710788726806641},
 'Custom Yule-Walker': {'Phi': 0.49369116106281674,
  'Sigma2': 0.9764331694286648,
  'Time': 0.002042531967163086},
 'Statsmodels ARIMA': {'Phi': 0.49153504493181266,
  'Sigma2': 0.9742868834709236,
  'Time': 0.07370138168334961}}

The MA model:

Implementation

np.random.seed(42)  # For reproducibility

# Parameters for the MA(1) process
theta_true = 0.6
sigma2_true = 1
n = 100

errors = np.random.normal(0, np.sqrt(sigma2_true), n)

# Generate the MA(1) process
data_ma = np.zeros(n)
for t in range(1, n):
    data_ma[t] = errors[t] + theta_true * errors[t-1]

theta_custom, sigma2_custom, errors_custom = fit_ma_model(data_ma, 1)

model_sm = sm.tsa.ARIMA(data_ma, order=(0,0,1))
results_sm = model_sm.fit()
theta_sm = results_sm.maparams
sigma2_sm = results_sm.params['sigma2']

theta_custom, sigma2_custom, theta_sm, sigma2_sm

Result

(array([0.63490588]),
 0.8143169190774796,
 array([0.62839426]),
 0.8133570727952485)

@JerBouma JerBouma marked this pull request as ready for review March 6, 2024 22:32
@JerBouma
Copy link
Owner

JerBouma commented Mar 7, 2024

Thank you! Will review this around the 18th of March!

northern-64bit and others added 6 commits March 9, 2024 17:12
The previous implementation had an incorrect way to get the errors for the prediction. This has now been fixed and compared with statsmodels.
@JerBouma JerBouma merged commit e461450 into JerBouma:main Apr 2, 2024
2 checks passed
@JerBouma
Copy link
Owner

JerBouma commented Apr 2, 2024

Thank you 🫡

@northern-64bit northern-64bit deleted the ar-model branch April 2, 2024 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants