-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from masterismail/povertyImpact
Added Regular Poverty Impact
- Loading branch information
Showing
12 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from policyengine_core.reforms import Reform | ||
from typing import Dict, Union | ||
|
||
class BaseMetricCalculator: | ||
""" | ||
Base class for calculating metrics based on baseline and reformed data. | ||
Attributes: | ||
baseline (object): Object representing baseline data. | ||
reformed (object): Object representing reformed data. | ||
""" | ||
def __init__(self, baseline: object, reformed: object, default_period: int = 2024) -> None: | ||
""" | ||
Initialize with baseline and reformed data objects. | ||
Args: | ||
baseline (object): Object representing baseline data. | ||
reformed (object): Object representing reformed data. | ||
""" | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
self.default_period = default_period | ||
self.baseline.default_calculation_period = default_period | ||
self.reformed.default_calculation_period = default_period | ||
|
||
def calculate_baseline(self, variable: str, period: int = None) -> Union[float, int]: | ||
""" | ||
Calculate baseline metric value. | ||
Args: | ||
variable (str): Variable to calculate. | ||
period (int): Year or period for calculation (default: 2024). | ||
Returns: | ||
Union[float, int]: Calculated metric value. | ||
""" | ||
if period is None: | ||
period = self.default_period | ||
return self.baseline.calculate(variable, period=period) | ||
|
||
def calculate_reformed(self, variable: str, period: int = None) -> Union[float, int]: | ||
""" | ||
Calculate reformed metric value. | ||
Args: | ||
variable (str): Variable to calculate. | ||
period (int): Year or period for calculation (default: 2024). | ||
Returns: | ||
Union[float, int]: Calculated metric value. | ||
""" | ||
if period is None: | ||
period = self.default_period | ||
return self.reformed.calculate(variable, period=period) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
89 changes: 89 additions & 0 deletions
89
policyengine/economic_impact/poverty_impact/regular_poverty/by_age/by_age.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from policyengine.economic_impact.base_metric_calculator import BaseMetricCalculator | ||
from policyengine_uk import Microsimulation | ||
|
||
class ChildPoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
age = self.baseline.calculate("age") | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty[age < 18].mean()) | ||
reform = float(reform_poverty[age < 18].mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} | ||
|
||
class AdultPoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
age = self.baseline.calculate("age") | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty[(age >= 18) & (age < 65)].mean()) | ||
reform = float(reform_poverty[(age >= 18) & (age < 65)].mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} | ||
|
||
class SeniorPoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
age = self.baseline.calculate("age") | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty[age >= 65].mean()) | ||
reform = float(reform_poverty[age >= 65].mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} | ||
|
||
class AllPoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty.mean()) | ||
reform = float(reform_poverty.mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} |
Empty file.
67 changes: 67 additions & 0 deletions
67
policyengine/economic_impact/poverty_impact/regular_poverty/by_gender/by_gender.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from policyengine.economic_impact.inequality_impact.inequality_impact import BaseMetricCalculator | ||
from policyengine_uk import Microsimulation | ||
|
||
class MalePoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
is_male = self.baseline.calculate("is_male") | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty[is_male].mean()) | ||
reform = float(reform_poverty[is_male].mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} | ||
|
||
class FemalePoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
is_male = self.baseline.calculate("is_male") | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty[~is_male].mean()) | ||
reform = float(reform_poverty[~is_male].mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} | ||
|
||
class AllPoverty(BaseMetricCalculator): | ||
def __init__(self, baseline: Microsimulation, reformed: Microsimulation, default_period: int = 2024) -> None: | ||
super().__init__(baseline, reformed, default_period) | ||
self.baseline = baseline | ||
self.reformed = reformed | ||
|
||
def calculate(self): | ||
|
||
baseline_poverty = self.baseline.calculate("in_poverty", map_to="person") | ||
reform_poverty = self.reformed.calculate("in_poverty", map_to="person") | ||
|
||
baseline = float(baseline_poverty.mean()) | ||
reform = float(reform_poverty.mean()) | ||
change = ((reform - baseline) / baseline) * 100 | ||
|
||
return { | ||
"baseline": round(baseline,2), | ||
"reform": round(reform,2), | ||
"change": round(change,1) | ||
} |
40 changes: 40 additions & 0 deletions
40
policyengine/tests/economic_impact/poverty_impact/regular_poverty/by_age/by_age.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Regular poverty by age | ||
- test_child_poverty: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.32 | ||
reform: 0.36 | ||
change: 10.1 | ||
|
||
- test_adult_poverty: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.17 | ||
reform: 0.19 | ||
change: 8.4 | ||
|
||
- test_senior_poverty: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.13 | ||
reform: 0.17 | ||
change: 30.5 | ||
|
||
- test_all_poverty: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.2 | ||
reform: 0.22 | ||
change: 11.7 |
41 changes: 41 additions & 0 deletions
41
policyengine/tests/economic_impact/poverty_impact/regular_poverty/by_age/test_by_age.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
import yaml | ||
import os | ||
from policyengine import EconomicImpact | ||
|
||
def assert_dict_approx_equal(actual, expected, tolerance=1e-4): | ||
for key in expected: | ||
assert abs(actual[key] - expected[key]) < tolerance, f"Key {key}: expected {expected[key]}, got {actual[key]}" | ||
|
||
|
||
yaml_file_path = "policyengine/tests/economic_impact/poverty_impact/regular_poverty/by_age/by_age.yaml" | ||
|
||
# Check if the file exists | ||
if not os.path.exists(yaml_file_path): | ||
raise FileNotFoundError(f"The YAML file does not exist at: {yaml_file_path}") | ||
|
||
with open(yaml_file_path, 'r') as file: | ||
test_cases = yaml.safe_load(file) | ||
|
||
@pytest.mark.parametrize("test_case", test_cases) | ||
def test_economic_impact(test_case): | ||
test_name = list(test_case.keys())[0] | ||
test_data = test_case[test_name] | ||
|
||
economic_impact = EconomicImpact(test_data['reform'], test_data['country']) | ||
|
||
if 'child' in test_name: | ||
result = economic_impact.calculate("poverty/regular/child") | ||
elif 'adult' in test_name: | ||
result = economic_impact.calculate("poverty/regular/adult") | ||
elif 'senior' in test_name: | ||
result = economic_impact.calculate("poverty/regular/senior") | ||
elif 'all' in test_name: | ||
result = economic_impact.calculate("poverty/regular/age/all") | ||
else: | ||
pytest.fail(f"Unknown test case: {test_name}") | ||
|
||
assert_dict_approx_equal(result, test_data['expected']) | ||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
30 changes: 30 additions & 0 deletions
30
policyengine/tests/economic_impact/poverty_impact/regular_poverty/by_gender/by_gender.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Regular poverty by age | ||
- male_poverty_test: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.18 | ||
reform: 0.21 | ||
change: 12.9 | ||
|
||
- female_poverty_test: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.21 | ||
reform: 0.23 | ||
change: 10.7 | ||
|
||
- all_poverty_test: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.2 | ||
reform: 0.22 | ||
change: 11.7 |
Oops, something went wrong.