-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbudget_by_program.py
More file actions
96 lines (80 loc) · 2.73 KB
/
budget_by_program.py
File metadata and controls
96 lines (80 loc) · 2.73 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
import plotly.express as px
import plotly.graph_objects as go
import typing
from policyengine import PolicyEngine
from pydantic import BaseModel
from policyengine.utils.charts import *
def create_budget_program_comparison_chart(
engine: PolicyEngine,
) -> go.Figure:
"""Create a budget comparison chart."""
if not simulation.is_comparison:
raise ValueError("Simulation must be a comparison simulation.")
if not simulation.options.country == "uk":
raise ValueError("This chart is only available for the UK.")
economy = simulation.calculate_economy_comparison()
change_programs = economy.fiscal.change.tax_benefit_programs
change_programs = {
program: change_programs[program]
for program in change_programs
if round(change_programs[program] / 1e9, 1) != 0
}
labels = [
simulation.baseline_simulation.tax_benefit_system.variables.get(
program
).label
for program in change_programs
]
x_values = labels
y_values = [
round(change_programs[program] / 1e9, 1) for program in change_programs
]
total_from_programs = round(sum(y_values))
total_overall = round(economy.fiscal.change.federal_balance / 1e9)
if total_from_programs != total_overall:
x_values.append("Other")
y_values.append(total_overall - total_from_programs)
x_values.append("Combined")
y_values.append(total_overall)
if total_overall > 0:
description = f"raise ${total_overall}bn"
elif total_overall < 0:
description = f"cost ${-total_overall}bn"
else:
description = "have no effect on government finances"
chart = go.Figure(
data=[
go.Waterfall(
x=x_values,
y=y_values,
measure=["relative"] * (len(x_values) - 1) + ["total"],
textposition="inside",
text=[f"${value:.1f}bn" for value in y_values],
increasing=dict(
marker=dict(
color=BLUE,
)
),
decreasing=dict(
marker=dict(
color=DARK_GRAY,
)
),
totals=dict(
marker=dict(
color=BLUE if total_overall > 0 else DARK_GRAY,
)
),
),
]
).update_layout(
title=f"{simulation.options.title} would {description}",
yaxis_title="Budgetary impact (bn)",
uniformtext=dict(
mode="hide",
minsize=12,
),
)
return format_fig(
chart, country=simulation.options.country, add_zero_line=True
)