-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[1] - added tests for winners_and_losers #32
Merged
nikhilwoodruff
merged 4 commits into
PolicyEngine:main
from
masterismail:test_winners_and_losers
Sep 25, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...gine/tests/economic_impact/winners_and_losers/test_by_income_decile/by_income_decile.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,19 @@ | ||
# Winners and Losers by income | ||
- income_impact_test: | ||
reform: | ||
gov.irs.credits.ctc.refundable.fully_refundable: | ||
"2024-01-01.2100-12-31": True | ||
country: us | ||
expected: | ||
deciles: | ||
Lose more than 5%: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] | ||
Lose less than 5%: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] | ||
No change: [82.9, 76.6, 78.3, 85.8, 89.9, 93.4, 94.7, 96.5, 96.9, 98.5] | ||
Gain less than 5%: [0.7, 6.1, 8.2, 6.2, 4.6, 4.8, 3.7, 2.6, 2.7, 1.5] | ||
Gain more than 5%: [16.4, 17.3, 13.4, 8.0, 5.5, 1.8, 1.6, 0.8, 0.5, 0.0] | ||
all: | ||
Lose more than 5%: 0.0 | ||
Lose less than 5%: 0.0 | ||
No change: 89.3 | ||
Gain less than 5%: 4.1 | ||
Gain more than 5%: 6.5 |
40 changes: 40 additions & 0 deletions
40
...e/tests/economic_impact/winners_and_losers/test_by_income_decile/test_by_income_decile.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,40 @@ | ||
import pytest | ||
import yaml | ||
import os | ||
from policyengine import EconomicImpact | ||
|
||
def assert_dict_approx_equal(actual, expected, tolerance=1e-4): | ||
if isinstance(expected, dict): | ||
for key in expected: | ||
assert_dict_approx_equal(actual[key], expected[key], tolerance) | ||
elif isinstance(expected, list): | ||
assert len(actual) == len(expected), f"List length mismatch: expected {len(expected)}, got {len(actual)}" | ||
for a, e in zip(actual, expected): | ||
assert abs(a - e) < tolerance, f"Expected {e}, got {a}" | ||
else: | ||
assert abs(actual - expected) < tolerance, f"Expected {expected}, got {actual}" | ||
|
||
yaml_file_path = "policyengine/tests/economic_impact/winners_and_losers/test_by_income_decile/by_income_decile.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] | ||
|
||
if test_name != 'income_impact_test': | ||
pytest.skip(f"Skipping non-income test: {test_name}") | ||
|
||
economic_impact = EconomicImpact(test_data['reform'], test_data['country']) | ||
result = economic_impact.calculate("winners_and_losers/by_income_decile") | ||
|
||
assert_dict_approx_equal(result['result'], test_data['expected']) | ||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
18 changes: 18 additions & 0 deletions
18
...gine/tests/economic_impact/winners_and_losers/test_by_wealth_decile/by_wealth_decile.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,18 @@ | ||
- wealth_impact_test: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
deciles: | ||
Lose more than 5%: [21.1, 31.6, 59.2, 64.3, 67.1, 80.1, 81.1, 82.6, 84.5, 86.4] | ||
Lose less than 5%: [6.5, 9.7, 2.4, 2.0, 3.8, 0.8, 0.4, 2.6, 0.2, 1.2] | ||
No change: [72.4, 58.7, 38.4, 33.6, 29.1, 19.1, 18.4, 14.8, 15.3, 12.4] | ||
Gain less than 5%: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] | ||
Gain more than 5%: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] | ||
all: | ||
Lose more than 5%: 65.8 | ||
Lose less than 5%: 3.0 | ||
No change: 31.2 | ||
Gain less than 5%: 0.0 | ||
Gain more than 5%: 0.0 |
40 changes: 40 additions & 0 deletions
40
...e/tests/economic_impact/winners_and_losers/test_by_wealth_decile/test_by_wealth_decile.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,40 @@ | ||
import pytest | ||
import yaml | ||
import os | ||
from policyengine import EconomicImpact | ||
|
||
def assert_dict_approx_equal(actual, expected, tolerance=5e-2): | ||
if isinstance(expected, dict): | ||
for key in expected: | ||
assert_dict_approx_equal(actual[key], expected[key], tolerance) | ||
elif isinstance(expected, list): | ||
assert len(actual) == len(expected), f"List length mismatch: expected {len(expected)}, got {len(actual)}" | ||
for a, e in zip(actual, expected): | ||
assert abs(a - e) < tolerance, f"Expected {e}, got {a}" | ||
else: | ||
assert abs(actual - expected) < tolerance, f"Expected {expected}, got {actual}" | ||
|
||
yaml_file_path = "policyengine/tests/economic_impact/winners_and_losers/test_by_wealth_decile/by_wealth_decile.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] | ||
|
||
if test_name != 'wealth_impact_test': | ||
pytest.skip(f"Skipping non-wealth test: {test_name}") | ||
|
||
economic_impact = EconomicImpact(test_data['reform'], test_data['country']) | ||
result = economic_impact.calculate("winners_and_losers/by_wealth_decile") | ||
|
||
assert_dict_approx_equal(result['result'], test_data['expected']) | ||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change this to a US test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific parameter you would recommend that could impact our target metrics ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one- see
reproduce in python
for the parameter name. https://policyengine.org/us/policy?reform=51488&focus=gov.irs.credits.ctc.refundable.fully_refundable®ion=us&timePeriod=2024&baseline=2There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated with the requested parameter, although we still have zeroes at some places.