-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
262 lines (196 loc) · 8.74 KB
/
Copy pathapp.py
File metadata and controls
262 lines (196 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
U.S. Federal Tax Dashboard
Interactive dashboard exploring federal tax revenue and effective rates using data from OMB, CBO/TPC, and FRED
"""
import streamlit as st
import numpy as np
st.set_page_config(
page_title='U.S. Federal Tax Dashboard',
layout='wide',
initial_sidebar_state='expanded'
)
#---------------------------------------------------------------------
# Imports (after page config)
#---------------------------------------------------------------------
from src.data_pipeline import load_all_data
from src.charts import(
effective_rates_chart,
revenue_pie_chart,
revenue_share_history,
revenue_history,
effective_rates_over_time
)
#--------------------------------------------------------------------
# Load data, chached (only download once per session day)
#--------------------------------------------------------------------
data = load_all_data()
receipts_nom = data['receipts_nominal']
receipts_real = data['receipts_real']
receipts_pct = data['receipts_real']
eff_rates = data['effective_rates']
# year ranges
eff_rate_years = sorted(eff_rates['Year'].unique())
revenue_years = sorted(receipts_nom['Fiscal Year'].unique())
#--------------------------------------------------------------------
# Sidebar
#--------------------------------------------------------------------
with st.sidebar:
st.markdown('## Federal Tax Dashboard')
st.caption('Explore U.S. federal tax revenue sources and effective tax rates across income groups.')
st.divider()
view = st.radio(
"**Select a view**",
[
'Effective Tax Rates',
'Tax Revenue Sources',
'Tax Revenue Over Time',
'Effective Rates Over Time'
],
index=0
)
st.divider()
st.markdown('### Data Sources')
st.markdown(
"""
- [OMB Historical Tables](https://www.whitehouse.gov/omb/budget/historical-tables/)
- [CBO Household Income Report](https://www.cbo.gov/publication/58353)
- [Tax Policy Center](https://taxpolicycenter.org/)
- [FRED CPI Data](https://fred.stlouisfed.org/series/CPIAUCSL)
"""
)
st.caption('Revenue is adjusted to 2017 dollars by default using CPI')
#---------------------------------------------------------------
# Header
#---------------------------------------------------------------
st.markdown(
"""
<h1 style='margin-bottom: 0;'>U.S. Federal Tax Dashboard</h1>
<p style='color: #64748B; font-size: 1.1rem; margin-top: 0.25rem;'>
Exploring federal tax revenue sources and effective rates across income groups
</p>
""",
unsafe_allow_html=True
)
#-----------------------------------------------------------------
# View 1: Effective Tax Rates (single year, stacked bar)
#-----------------------------------------------------------------
if 'Effective Tax Rates' in view and 'Over Time' not in view:
col_ctrl1, col_ctrl2 = st.columns([1, 3])
with col_ctrl1:
fy = st.select_slider(
'Fiscal Year',
options=eff_rate_years,
value=max(eff_rate_years)
)
fig = effective_rates_chart(eff_rates, fy)
st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False})
# Key stats row
df_fy = eff_rates[(eff_rates['Year'] == fy) & (eff_rates['Tax Type'] == 'Total Federal Tax Rate')]
if not df_fy.empty:
row = df_fy.iloc[0]
c1, c2, c3, c4 = st.columns(4)
c1.metric("Lowest Quintile", f"{row.get('Lowest Quintile', 0):.1f}%")
c2.metric("Middle Quintile", f"{row.get('Middle Quintile', 0):.1f}%")
c3.metric("Highest Quintile", f"{row.get('Highest Quintile', 0):.1f}%")
c4.metric("Top 1%", f"{row.get('Top 1%', 0):.1f}%")
with st.expander('About this chart'):
st.markdown(
"""
Effective tax rates represent the share of income each group actually pays in federal taxes, broken down by the type of tax (individual income taxes, corporate income taxes, payroll taxes, and excise taxes).
A Congressional Budget Office (CBO) analysis allocated all federal taxes to households based on the income group the household fell under (based on percentile), and data from this report is summarized by the Tax Policy Center.
**Source:** Congression Budget Office via the Tax Policy Center
"""
)
df_fy = eff_rates[eff_rates['Year'] == 2019].copy()
df_fy = df_fy.set_index('Tax Type')
print("Tax Types in index:")
for t in df_fy.index.unique():
print(f" '{t}'")
print("Individual Income Tax Rate, Lowest Quintile:",
df_fy.loc['Individual Income Tax Rate', 'Lowest Quintile'])
print("Payroll Tax Rate, Lowest Quintile:",
df_fy.loc['Payroll Tax Rate', 'Lowest Quintile'])
print("\nFull row for Individual Income Tax Rate:")
print(df_fy.loc['Individual Income Tax Rate'])
#---------------------------------------------------------------------------------------------
# View 2: Revenue Composition (Revenue by Source), single year pie + time series share
#---------------------------------------------------------------------------------------------
elif 'Tax Revenue Sources' in view:
col_ctrl1, _ = st.columns([1, 3])
with col_ctrl1:
fy_rev = st.select_slider(
'Fiscal Year',
options=revenue_years,
value=max(revenue_years),
key='fy_rev',
)
fig_pie = revenue_pie_chart(receipts_nom, fy_rev)
st.plotly_chart(fig_pie, use_container_width=True, config={"displayModeBar": False})
with st.expander('About this chart'):
st.markdown(
"""
The pie chart shows the composition of federal revenue for a single fiscal year.
Key trend: Corporate income taxes have shrunk from ~30% of revenue in the 1950s to
roughly 9–15% today, while payroll taxes (Social Insurance) have grown substantially.
**Source:** OMB Historical Table 2.1.
"""
)
#-------------------------------------------------------------------------------------------
# View 3: Revenue Over Time (stacked area, nominal vs real)
#-------------------------------------------------------------------------------------------
elif 'Revenue Over Time' in view:
col_ctrl1, col_ctrl2, _ = st.columns([1, 1, 2])
with col_ctrl1:
start = st.select_slider(
'Start Year',
options=revenue_years,
value=1950,
key="start_rev",
)
with col_ctrl2:
use_real = st.toggle('Inflation-Adjusted (2017 $)', value=False)
if use_real:
fig = revenue_history(receipts_real, start_year=start, real=True)
else:
fig = revenue_history(receipts_nom, start_year=start, real=False)
st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False})
with st.expander('About this chart'):
st.markdown(
"""
Total federal revenue broken down by source. Toggle between nominal dollars and
inflation-adjusted (2017) dollars to see real growth versus price-level effects.
Inflation adjustment uses CPI-U annual averages from FRED (Federal Reserve
Economic Data, originally Bureau of Labor Statistics).
**Source:** OMB Historical Table 2.1; CPI from FRED/BLS.
"""
)
#--------------------------------------------------------------------------------------------
# View 4: Effective Rates Over Time (line chart)
#--------------------------------------------------------------------------------------------
elif 'Effective Rates Over Time' in view:
col_ctrl1, _ = st.columns([1, 3])
with col_ctrl1:
group_view = st.selectbox(
'Income Group',
['All Quintiles', 'Top Earners'],
index=0
)
fig = effective_rates_over_time(eff_rates, income_group=group_view)
st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False})
with st.expander('About this chart'):
st.markdown(
"""
Total effective federal tax rate over time for different income groups.
"All Quintiles" shows the five quintiles of the income distribution.
"Top Earners" breaks out the top income quintile into smaller groups.
**Source:** Congressional Budget Office via the Tax Policy Center
"""
)
#----------------------------------------------------------------------------------------------
# Footer
#----------------------------------------------------------------------------------------------
st.divider()
st.caption(
'Built with streamlit & Plotly, Data: OMB, CBO/TPC, FRED'
'[GitHub](https://github.com/ellie-cothren/federal_tax_figures)'
)