!pip install openpyxl
import pandas as pd
import numpy as np
import plotly.express as px
psid_df = pd.read_csv('psid_task_data.txt', sep='\t')
psid_df.head()
cpi_df = pd.read_excel('bls_cpi_data.xlsx')
cpi_df.columns = cpi_df.iloc[10] # fix excel parsing errors
cpi_df = cpi_df.iloc[11:]
cpi_df = cpi_df.set_index('Year')
cpi_df.head()
psid_df['birth_year'] = psid_df['year'] - psid_df['age']
basket_2010 = cpi_df.loc[2010]['Annual'] # 2010 annual average
adj_cpi_df = cpi_df.divide(basket_2010).multiply(100)
adj_cpi_df.head()
stable_psid_df = psid_df[psid_df['change_in_fam_comp']==0].drop('change_in_fam_comp', axis=1)
stable_psid_df.head()
adj_psid_df = stable_psid_df.join(adj_cpi_df['Annual'],on='year',how='left').rename({'Annual':'CPI'}, axis=1)
adj_psid_df['full_income'] = adj_psid_df['full_income']/adj_psid_df['CPI']*100 # adjust by CPI
adj_psid_df.head()
weighted_avg = lambda x: np.average(list(x), weights=list(adj_psid_df.loc[x.index, 'full_weights']))
average_income = adj_psid_df[['year','full_income']].groupby(['year']).aggregate(weighted_avg)
average_income.to_csv('average_incomes.csv')
average_income.head()
fig = px.line(data_frame=average_income,
x=average_income.index,
y='full_income',
title='Inflation Adjusted Income 1982-2015 for Stable Households',
labels=dict(year="Year", full_income='Annual Income in 2010 Dollars'))
fig.show()
infl = adj_cpi_df['Annual'].pct_change()[1:]
infl.head()
"""
For a given year `t`, a
birth year `birth`,
and lambda `lmbda`, computes
the experienced inflation.
"""
def compute_exp_infl(year, lmbda, birth=min(infl.index)):
age = year-birth # consider the current age of the agent
def weights(age, lag): # weight experiences in terms of ages
return (age-lag)**lmbda/np.sum((np.arange(1, age+1))**lmbda)
def wght_inf(lag): # compute weighted inflation for given year lag
return weights(age, lag)*infl.loc[year-lag]
return sum(map(wght_inf, np.arange(0, age))) # sum weighted inflation experiences
lmbda = 4 # set lambda
exp_infl = pd.Series(map(lambda x: compute_exp_infl(x, lmbda), infl.index)) # apply to inflation series
exp_infl.index = infl.index # adjust index
infl_df = pd.DataFrame({'current':infl, 'experienced':exp_infl}).iloc[1:,].reset_index() # drop first year
infl_df = pd.melt(infl_df, id_vars=['Year'], value_vars=['current','experienced']).rename({'variable':'Inflation Type', 'value':'Inflation Level'}, axis=1)
px.line(infl_df,
x='Year',
y='Inflation Level',
color='Inflation Type',
title=f'Current vs. Experienced Inflation for Lambda={lmbda}')
print("{:.5f}%!".format(compute_exp_infl(year=2017, lmbda=3, birth=1949)*100))