Get Started

Let's Forecast Interest Rates

You will now use the forecasting techniques you learned in the last exercise and apply it to real data rather than simulated data. You will revisit a dataset from the first chapter: the annual data of 10-year interest rates going back 56 years, which is in a Series called interest_rate_data. Being able to forecast interest rates is of enormous importance, not only for bond investors but also for individuals like new homeowners who must decide between fixed and floating rate mortgages.

You saw in the first chapter that there is some mean reversion in interest rates over long horizons. In other words, when interest rates are high, they tend to drop and when they are low, they tend to rise over time. Currently they are below long-term rates, so they are expected to rise, but an AR model attempts to quantify how much they are expected to rise.

The class ARIMA and the function plot_predict have already been imported.

This is a part of the course

“Time Series Analysis in Python”

View Course

Exercise instructions

  • Create an instance of the ARIMA class called mod using the annual interest rate data and choosing the order for an AR(1) model.
  • Fit the model mod using the method .fit() and save it in a results object called res.
  • Plot the data and the in-sample and out-of-sample forecasts of the data using the .plot_predict() function.
    • The first argument of plot_predict() should be the fitted model.
    • Pass the arguments start=0 to start the in-sample forecast from the beginning, and choose end to be '2027' to forecast several years in the future.
    • Note that the end argument 2027 must be in quotes here since it represents a date and not an integer position.

Hands-on interactive exercise

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

# Forecast interst rates using an AR(1) model
mod = ARIMA(interest_rate_data, order=___)
res = mod.fit()

# Plot the data and the forecast
fig, ax = plt.subplots()
interest_rate_data.plot(ax=ax)
plot_predict(___, start=___, end=___, alpha=None, ax=ax)
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 SeriesExercise 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 Rates
Exercise 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