In this lab, you'll practice your knowledge the Autoregressive (AR), the Moving Average (MA) model, and the combined ARMA model.
You will be able to:
- Understand and explain what a Autoregressive model is
- Understand and explain what a Moving Average model is
- Understand and apply the mathematical formulations for Autoregressive and Moving Average models
- Understand how AR and MA can be combined in ARMA models
#import the necessary libraries
Recall that the AR model has the following formula:
This means that:
and so on.
Assume a mean-zero white noise with a standard deviation of 2. Make sure you have a daily datetime index ranging from January 2017 until the end of March 2018. Assume that
# keep the random seed
np.random.seed(11225)
# create a series with the specified dates
# store the parameters
# generate the time series according to the formula
Plot the time series and verify what you see
# plot here
We recommend to use plot_acf
in statsmodels instead of the pandas ACF variant.
from statsmodels.graphics.tsaplots import plot_pacf
from statsmodels.graphics.tsaplots import plot_acf
Statsmodels also has a tool that fits ARMA models on time series. The only thing you have to do is provide the number of orders for AR vs MA. Have a look at the code below, and the output of the code. Make sure that the output for the
# assuming your time series are stored in an object "series"
# Import the ARMA module from statsmodels
from statsmodels.tsa.arima_model import ARMA
import statsmodels.api as sm
# Fit an MA(1) model to the first simulated data
mod_arma = ARMA(series, order=(1,0))
res_arma = mod_arma.fit()
# Print out summary information on the fit
print(res_arma.summary())
# Print out the estimate for the constant and for theta
print(res_arma.params)
Recall that the MA model has the following formula:
This means that:
and so on.
Assume a mean-zero white noise with a standard deviation of 4. Make sure you have a daily datetime index is ranging from April 2015 until the end of August 2015. Assume that
# keep the random seed
np.random.seed(1234)
# create a series with the specified dates
# store the parameters
#generate the time series
# Plot the time series
# plots here
Repeat what you did for your AR model but now for your MA model to verify the parameters are estimated correctly.
# Fit an AR(1) model to the first simulated data
# Print out summary information on the fit
Import the data set containing the historical running times for the men's 400m on the Olympic games.
# the data is in "winning_400m.csv"
Plot the data
# your code here
Difference the data to get a stationary time series. Make sure to remove the first NaN value.
# your code here
# Look at ACF and PACF
Based on the ACF and PACF, fit an arma model with the right orders for AR and MA. Feel free to try different models and compare AIC and BIC values, as well as significance values for the parameter estimates.
# your code here
# Try another one
Great! Now that you know the ins and outs of ARMA models and you've practiced your modeling knowledge.