Skip to content

learn-co-students/dsc-arma-models-lab-seattle-ds-career-040119

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ARMA Models - Lab

Introduction

In this lab, you'll practice your knowledge the Autoregressive (AR), the Moving Average (MA) model, and the combined ARMA model.

Objectives

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

Generate an AR model of the first order with $\phi = 0.7$

#import the necessary libraries

Recall that the AR model has the following formula:

$$Y_t = \mu + \phi * Y_{t-1}+\epsilon_t$$

This means that:

$$Y_1 = \mu + \phi * Y_{0}+\epsilon_1$$ $$Y_2 = \mu + \phi * (\text{mean-centered version of } Y_1) +\epsilon_2$$

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 $\mu=5$ and $Y_0= 8$.

# 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

Look at the ACF and PACF of your model and write down your conclusions

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

Check your model with ARMA in statsmodels

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 $\phi$ parameter and $\mu$ is as you'd expect!

# 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)

Generate an MA model of the first order with $\theta = 0.9$

Recall that the MA model has the following formula:

$$Y_t = \mu +\epsilon_t + \theta * \epsilon_{t-1}$$

This means that:

$$Y_1 = \mu + \epsilon_1+ \theta * \epsilon_{0}$$ $$Y_2 = \mu + \epsilon_2+ \theta * \epsilon_{1}$$

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 $\mu=7$.

# 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

Look at the ACF and PACF of your model and write down your conclusions

# plots here

Check your model with ARMA in statsmodels

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

Create a model for the 400m data set

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

What is your final model? Why did you pick this model?

# Your comments here

Summary

Great! Now that you know the ins and outs of ARMA models and you've practiced your modeling knowledge.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published