Get Started

Simulate AR(1) Time Series

You will simulate and plot a few AR(1) time series, each with a different parameter, \(\small \phi\), using the arima_process module in statsmodels. In this exercise, you will look at an AR(1) model with a large positive \(\small \phi\) and a large negative \(\small \phi\), but feel free to play around with your own parameters.

There are a few conventions when using the arima_process module that require some explanation. First, these routines were made very generally to handle both AR and MA models. We will cover MA models next, so for now, just ignore the MA part. Second, when inputting the coefficients, you must include the zero-lag coefficient of 1, and the sign of the other coefficients is opposite what we have been using (to be consistent with the time series literature in signal processing). For example, for an AR(1) process with \(\small \phi=0.9\), the array representing the AR parameters would be ar = np.array([1, -0.9])

This is a part of the course

“Time Series Analysis in Python”

View Course

Exercise instructions

  • Import the class ArmaProcess in the arima_process module.
  • Plot the simulated AR processes:
    • Let ar1 represent an array of the AR parameters [1, \(\small -\phi\)] as explained above. For now, the MA parameter array, ma1, will contain just the lag-zero coefficient of one.
    • With parameters ar1 and ma1, create an instance of the class ArmaProcess(ar,ma) called AR_object1.
    • Simulate 1000 data points from the object you just created, AR_object1, using the method .generate_sample(). Plot the simulated data in a subplot.
  • Repeat for the other AR parameter.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# import the module for simulating data
from statsmodels.tsa.arima_process import ArmaProcess

# Plot 1: AR parameter = +0.9
plt.subplot(2,1,1)
ar1 = np.array([1, ____])
ma1 = np.array([1])
AR_object1 = ArmaProcess(____, ____)
simulated_data_1 = AR_object1.generate_sample(nsample=1000)
plt.plot(simulated_data_1)

# Plot 2: AR parameter = -0.9
plt.subplot(2,1,2)
ar2 = np.array([1, ____])
ma2 = np.array([1])
AR_object2 = ArmaProcess(____, ____)
simulated_data_2 = AR_object2.generate_sample(nsample=1000)
plt.plot(simulated_data_2)
plt.show()
Edit and Run Code

This exercise is part of the course

Time Series Analysis in Python

IntermediateSkill Level
4.5+
19 reviews

In this four-hour course, you’ll learn the basics of analyzing time series data in Python.

In this chapter you'll learn about autoregressive, or AR, models for time series. These models use past values of the series to predict the current value.

Exercise 1: Describe AR ModelExercise 2: Simulate AR(1) Time Series
Exercise 3: Compare the ACF for Several AR Time SeriesExercise 4: Match AR Model with ACFExercise 5: Estimating and Forecasting AR ModelExercise 6: Estimating an AR ModelExercise 7: Forecasting with an AR ModelExercise 8: Let's Forecast Interest RatesExercise 9: Compare AR Model with Random WalkExercise 10: Choosing the Right ModelExercise 11: Estimate Order of Model: PACFExercise 12: Estimate Order of Model: Information Criteria

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free