Skip to content

Consolidate economic impact analysis into shared compute functions #263

Description

@anth-volk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions