How to do it...

Execute the following steps to transform the series from non-stationary to stationary.

  1. Import the libraries and update the inflation data:
import cpiimport pandas as pdfrom datetime import datefrom statsmodels.graphics.tsaplots import plot_acf, plot_pacffrom statsmodels.tsa.stattools import adfuller, kpssfrom chapter_3_utils import test_autocorrelation# update the CPI data (if needed)# cpi.update()
  1. Deflate the gold prices (to 2011-12-31 USD values) and plot the results:
DEFL_DATE = date(2011, 12, 31)df['dt_index'] = df.index.map(lambda x: x.to_pydatetime().date())df['price_deflated'] = df.apply(lambda x:                                      cpi.inflate(x.price,                                                 x.dt_index,                                                  DEFL_DATE),                                 axis=1)df[['price', 'price_deflated']].plot(title='Gold Price (deflated)'); ...

Get Python for Finance Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.