Distributional Analysis using Pandas (g14)

# etr.py

"""
etr.py
Spring 2022 PJW

Computing tax incidence using Pandas.
"""

import pandas as pd

#
#  Set up a function to print median ETR for a given set of
#  grouping variables
#

def print_groups(hh,group_vars):
grouped = hh.groupby(group_vars)
med = grouped['etr'].median().round(2)
print( med )
return med

#
#

#%%
#
#  Compute the quintile breaks
#

hh['quint'] = pd.qcut( hh['inc'], 5, labels=[1,2,3,4,5] )

#
#  Use the equilibrium prices to compute ETRs
#

pd1 = 53.35
pd2 = 55.27
dp  = pd2-pd1

hh['etr'] = 100*dp*q['qd2']/hh['inc']

#%%
#
#  Use the print_groups() function to print median ETRs
#  by quintile, by type, and by type and quintile.
#

print('\nMedian ETRs by income quintile alone:')
med_q = print_groups( hh, ['quint'] )

print('\nMedian ETRs by type alone:')
med_t = print_groups( hh, ['type'] )

print('\nMedian ETRs by type and income quintile:')
med_b = print_groups( hh, ['type','quint'] )

#
#

print( '\nIndex of med_b:')
print( med_b.index )

print( '\nMedians for type 3:')
print( med_b.xs(3,level='type') )

print( '\nMedians for quintile 5:')
print( med_b.xs(5,level='quint') )

#
#  Find the difference between each group's ETR and
#  that of the lowest quintile in the same group.
#

etr_lowest = med_b.xs(1,level='quint')
print( '\nETR of lowest quintile for each type:' )
print( etr_lowest )

etr_change = med_b - etr_lowest

print( "\nDifference of quintile's ETR from lowest quintle:" )
print( etr_change )

URL: https://wilcoxen.maxwell.insightworks.com/pages/5266.html
Peter J Wilcoxen, The Maxwell School, Syracuse University
Revised 03/27/2022