# Chapter 38: AR, MA, ARMA, ARIMA, and Forecasting
# Fundamentals of Python for Financial Econometrics - AR, MA, ARMA, ARIMA, and Forecasting
# Dataset: Ceteris Lab teaching sample

# AR, MA, ARMA, ARIMA, and Forecasting
#
# **Opening question:** How can past values and past shocks be organized into a model that produces honest forecasts and uncertainty?

# %% Cell 2
import numpy as np
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.tsa.arima.model import ARIMA

rng = np.random.default_rng(19)
process = ArmaProcess(ar=[1, -0.65], ma=[1, 0.35])
x = process.generate_sample(500, distrvs=rng.standard_normal)
fit = ARIMA(x, order=(1, 0, 1), trend="c").fit()
print(fit.params.round(3))

# **Interpretation check:** Interpretation. The estimates recover the simulated AR and MA structure approximately. Sampling variation and likelihood conventions prevent exact equality.

# %% Cell 4
forecast = fit.get_forecast(steps=5)
mean = forecast.predicted_mean
interval = forecast.conf_int(alpha=0.05)
print(np.round(mean, 3))
print(np.round(interval[0], 3))

# **Interpretation check:** Interpretation. The forecast returns toward the estimated mean while uncertainty expands with horizon.

# %% Cell 6
import numpy as np
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.tsa.arima.model import ARIMA
rng = np.random.default_rng(19)
process = ArmaProcess(ar=[1, -0.65], ma=[1, 0.35])
x = process.generate_sample(500, distrvs=rng.standard_normal)

# Verified source output
#
# ```text
# [-0.055 0.619 0.394 0.932]
# ```
#
# ```text
# [-0.603 -0.394 -0.265 -0.185 -0.136] [-2.495 1.29 ]
# ```
#
# ```text
# [-0.055  0.619  0.394  0.932]
# ```
#
# ```text
# [-0.603 -0.394 -0.265 -0.185 -0.136]
# [-2.495  1.29 ]
# ```
