Calculate Composite Rate for Series I Savings Bonds (I Bonds) using CPI¶
Install Packages¶
import numpy as np
import pandas as pd
from pandas_datareader.fred import FredReader
end_date = pd.to_datetime('today').date()
start_date = (end_date + pd.DateOffset(years=-5)).date()
print(f'start_date: {start_date} to end_date: {end_date}')
df = FredReader('CPIAUCNS', start=start_date).read()
df.tail(12)
Plot CPI Data¶
df.plot(title='CPI - CPIAUCNS', figsize=(15,10));
Calculate Percent Change¶
df['CPIAUCNS-1m'] = df['CPIAUCNS'].shift(periods=1)
df['CPIAUCNS-6m'] = df['CPIAUCNS'].shift(periods=6)
df['CPIAUCNS-12m'] = df['CPIAUCNS'].shift(periods=12)
df.tail(12)
Composite Rate is 6 months percentage change multiplied by 2 (annualized).
df['percent_change_1m'] = (df['CPIAUCNS'] - df['CPIAUCNS-1m'])*100 / df['CPIAUCNS-1m']
df['percent_change_6m'] = (df['CPIAUCNS'] - df['CPIAUCNS-6m'])*100 / df['CPIAUCNS-6m']
df['percent_change_12m'] = (df['CPIAUCNS'] - df['CPIAUCNS-12m'])*100 / df['CPIAUCNS-12m']
# Composite Rate
df['percent_change_6m_annualized'] = df['percent_change_6m'] *2
cols = ['percent_change_1m', 'percent_change_6m', 'percent_change_12m', 'percent_change_6m_annualized']
df[cols].tail(12)
Composite Rate¶
Following we can see the composite rate based on the latest CPI data.
composite_rate = df.iloc[-1]['percent_change_6m_annualized'].round(2)
inflation_rate = df.iloc[-1]['percent_change_12m'].round(2)
latest_CPI_date = df.iloc[-1].name.date()
print(f'Latest CPI Date: {latest_CPI_date}, Inflation Rate: {inflation_rate}%, Composite Rate: {composite_rate}%')
Plot Percent Changes over Time¶
# pct_columns = ['percent_change_1m', 'percent_change_6m', 'percent_change_12m', 'percent_change_6m_annualized']
pct_columns = ['percent_change_6m_annualized']
df[pct_columns].tail(12)
df[pct_columns].plot(title='Calculated Inflation Rate', figsize=(15, 10));
print(f"Last Updated {pd.to_datetime('today')}")