Problem
The economic impact analysis logic (decile impacts, budget summary, program statistics, poverty, inequality) is currently duplicated between the US and UK analysis.py files, and depends on country-specific string checks scattered throughout the code. This makes it hard to maintain, test, and extend to new countries.
Solution
Introduce shared, country-agnostic compute functions and a CountryConfig frozen dataclass that encapsulates all country-specific parameters:
CountryConfig (outputs/country_config.py): Frozen dataclass with US_CONFIG and UK_CONFIG singletons holding income variables, program definitions, budget variables, poverty variables, and inequality settings.
BudgetSummaryItem + compute_budget_summary() (outputs/budget_summary.py): Computes baseline/reform/change totals for budget variables using Aggregate.
compute_program_statistics() (outputs/program_statistics.py): Computes per-program statistics (totals, counts, winners, losers) using ProgramStatistics/ProgrammeStatistics.
compute_decile_impacts() (outputs/decile_impact.py): Computes decile-by-decile impacts from already-run simulations.
PolicyReformAnalysis (outputs/policy_reform_analysis.py): Unified result container assembling all analysis outputs.
Each country's analysis.py calls these shared functions with its config, eliminating duplication.
Additional fixes included
- Fix
DecileImpact.run() bare next() → next(..., None) + ValueError (prevents StopIteration crash)
- Fix
Aggregate and ChangeAggregate with same pattern (already done in prior commits)
- Move inline imports to module top-level in
us/analysis.py
- Improve exception logging in
compute_program_statistics with exc_info=True
- Add
model_rebuild() calls for ProgramStatistics, BudgetSummaryItem, and PolicyReformAnalysis
- Add 11 new tests covering
CountryConfig, compute_decile_impacts, DecileImpact error handling, and ChangeAggregate error handling
Problem
The economic impact analysis logic (decile impacts, budget summary, program statistics, poverty, inequality) is currently duplicated between the US and UK
analysis.pyfiles, and depends on country-specific string checks scattered throughout the code. This makes it hard to maintain, test, and extend to new countries.Solution
Introduce shared, country-agnostic compute functions and a
CountryConfigfrozen dataclass that encapsulates all country-specific parameters:CountryConfig(outputs/country_config.py): Frozen dataclass withUS_CONFIGandUK_CONFIGsingletons holding income variables, program definitions, budget variables, poverty variables, and inequality settings.BudgetSummaryItem+compute_budget_summary()(outputs/budget_summary.py): Computes baseline/reform/change totals for budget variables usingAggregate.compute_program_statistics()(outputs/program_statistics.py): Computes per-program statistics (totals, counts, winners, losers) usingProgramStatistics/ProgrammeStatistics.compute_decile_impacts()(outputs/decile_impact.py): Computes decile-by-decile impacts from already-run simulations.PolicyReformAnalysis(outputs/policy_reform_analysis.py): Unified result container assembling all analysis outputs.Each country's
analysis.pycalls these shared functions with its config, eliminating duplication.Additional fixes included
DecileImpact.run()barenext()→next(..., None)+ValueError(preventsStopIterationcrash)AggregateandChangeAggregatewith same pattern (already done in prior commits)us/analysis.pycompute_program_statisticswithexc_info=Truemodel_rebuild()calls forProgramStatistics,BudgetSummaryItem, andPolicyReformAnalysisCountryConfig,compute_decile_impacts,DecileImpacterror handling, andChangeAggregateerror handling