@@ -707,6 +707,8 @@ def economy_comparison_uk(job_id: str, traceparent: str | None = None) -> None:
707707 from policyengine_api .models import (
708708 Dataset ,
709709 DecileImpact ,
710+ Inequality ,
711+ Poverty ,
710712 ProgramStatistics ,
711713 Report ,
712714 ReportStatus ,
@@ -748,6 +750,12 @@ def economy_comparison_uk(job_id: str, traceparent: str | None = None) -> None:
748750 # Import policyengine
749751 from policyengine .core import Simulation as PESimulation
750752 from policyengine .outputs import DecileImpact as PEDecileImpact
753+ from policyengine .outputs .inequality import (
754+ calculate_uk_inequality ,
755+ )
756+ from policyengine .outputs .poverty import (
757+ calculate_uk_poverty_rates ,
758+ )
751759 from policyengine .tax_benefit_models .uk import uk_latest
752760 from policyengine .tax_benefit_models .uk .datasets import (
753761 PolicyEngineUKDataset ,
@@ -881,6 +889,45 @@ def economy_comparison_uk(job_id: str, traceparent: str | None = None) -> None:
881889 except KeyError :
882890 pass # Variable not in model, skip silently
883891
892+ # Calculate poverty rates
893+ with logfire .span ("calculate_poverty" ):
894+ for sim , sim_id in [
895+ (pe_baseline_sim , baseline_sim .id ),
896+ (pe_reform_sim , reform_sim .id ),
897+ ]:
898+ poverty_collection = calculate_uk_poverty_rates (sim )
899+ for pov in poverty_collection .outputs :
900+ poverty_record = Poverty (
901+ simulation_id = sim_id ,
902+ report_id = report .id ,
903+ poverty_type = pov .poverty_type ,
904+ entity = pov .entity ,
905+ filter_variable = pov .filter_variable ,
906+ headcount = pov .headcount ,
907+ total_population = pov .total_population ,
908+ rate = pov .rate ,
909+ )
910+ session .add (poverty_record )
911+
912+ # Calculate inequality
913+ with logfire .span ("calculate_inequality" ):
914+ for sim , sim_id in [
915+ (pe_baseline_sim , baseline_sim .id ),
916+ (pe_reform_sim , reform_sim .id ),
917+ ]:
918+ ineq = calculate_uk_inequality (sim )
919+ inequality_record = Inequality (
920+ simulation_id = sim_id ,
921+ report_id = report .id ,
922+ income_variable = ineq .income_variable ,
923+ entity = ineq .entity ,
924+ gini = ineq .gini ,
925+ top_10_share = ineq .top_10_share ,
926+ top_1_share = ineq .top_1_share ,
927+ bottom_50_share = ineq .bottom_50_share ,
928+ )
929+ session .add (inequality_record )
930+
884931 # Mark simulations and report as completed
885932 baseline_sim .status = SimulationStatus .COMPLETED
886933 baseline_sim .completed_at = datetime .now (timezone .utc )
@@ -949,6 +996,8 @@ def economy_comparison_us(job_id: str, traceparent: str | None = None) -> None:
949996 from policyengine_api .models import (
950997 Dataset ,
951998 DecileImpact ,
999+ Inequality ,
1000+ Poverty ,
9521001 ProgramStatistics ,
9531002 Report ,
9541003 ReportStatus ,
@@ -983,6 +1032,12 @@ def economy_comparison_us(job_id: str, traceparent: str | None = None) -> None:
9831032 # Import policyengine
9841033 from policyengine .core import Simulation as PESimulation
9851034 from policyengine .outputs import DecileImpact as PEDecileImpact
1035+ from policyengine .outputs .inequality import (
1036+ calculate_us_inequality ,
1037+ )
1038+ from policyengine .outputs .poverty import (
1039+ calculate_us_poverty_rates ,
1040+ )
9861041 from policyengine .tax_benefit_models .us import us_latest
9871042 from policyengine .tax_benefit_models .us .datasets import (
9881043 PolicyEngineUSDataset ,
@@ -1113,6 +1168,45 @@ def economy_comparison_us(job_id: str, traceparent: str | None = None) -> None:
11131168 except KeyError :
11141169 pass # Variable not in model, skip silently
11151170
1171+ # Calculate poverty rates
1172+ with logfire .span ("calculate_poverty" ):
1173+ for sim , sim_id in [
1174+ (pe_baseline_sim , baseline_sim .id ),
1175+ (pe_reform_sim , reform_sim .id ),
1176+ ]:
1177+ poverty_collection = calculate_us_poverty_rates (sim )
1178+ for pov in poverty_collection .outputs :
1179+ poverty_record = Poverty (
1180+ simulation_id = sim_id ,
1181+ report_id = report .id ,
1182+ poverty_type = pov .poverty_type ,
1183+ entity = pov .entity ,
1184+ filter_variable = pov .filter_variable ,
1185+ headcount = pov .headcount ,
1186+ total_population = pov .total_population ,
1187+ rate = pov .rate ,
1188+ )
1189+ session .add (poverty_record )
1190+
1191+ # Calculate inequality
1192+ with logfire .span ("calculate_inequality" ):
1193+ for sim , sim_id in [
1194+ (pe_baseline_sim , baseline_sim .id ),
1195+ (pe_reform_sim , reform_sim .id ),
1196+ ]:
1197+ ineq = calculate_us_inequality (sim )
1198+ inequality_record = Inequality (
1199+ simulation_id = sim_id ,
1200+ report_id = report .id ,
1201+ income_variable = ineq .income_variable ,
1202+ entity = ineq .entity ,
1203+ gini = ineq .gini ,
1204+ top_10_share = ineq .top_10_share ,
1205+ top_1_share = ineq .top_1_share ,
1206+ bottom_50_share = ineq .bottom_50_share ,
1207+ )
1208+ session .add (inequality_record )
1209+
11161210 # Mark simulations and report as completed
11171211 baseline_sim .status = SimulationStatus .COMPLETED
11181212 baseline_sim .completed_at = datetime .now (timezone .utc )
0 commit comments