-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Validate NI against UKMOD * Versioning * Skip tests online * Format * Fix UKMOD skip * Import pytest * Fix test
- Loading branch information
1 parent
4954c75
commit 3dbe3da
Showing
44 changed files
with
610 additions
and
249 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
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,4 @@ | ||
- bump: minor | ||
changes: | ||
changed: | ||
- Validated and standardised National Insurance variables. |
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
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
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,63 @@ | ||
import pandas as pd | ||
from policyengine_uk.data.storage import STORAGE_FOLDER | ||
import numpy as np | ||
from policyengine_core.data import Dataset | ||
|
||
|
||
class UKMOD_FRS_2018(Dataset): | ||
name = "ukmod_frs_2018" | ||
label = "UKMOD (2018-19 FRS)" | ||
data_format = Dataset.TIME_PERIOD_ARRAYS | ||
file_path = STORAGE_FOLDER / "ukmod_frs_2018.h5" | ||
time_period = "2018" | ||
|
||
def generate(self): | ||
data = {} | ||
ukmod_output = pd.read_csv( | ||
STORAGE_FOLDER / "uk_2018_std.txt", delimiter="\t" | ||
) | ||
ukmod_input = pd.read_csv( | ||
STORAGE_FOLDER / "uk_2018_a4.txt", delimiter="\t" | ||
) | ||
output_columns = [ | ||
column | ||
for column in ukmod_output.columns | ||
if column not in ukmod_input.columns | ||
] | ||
ukmod = pd.merge( | ||
ukmod_output[output_columns + ["idperson"]], | ||
ukmod_input, | ||
on="idperson", | ||
how="right", | ||
) | ||
# Add ID variables first | ||
data["person_id"] = ukmod.idperson | ||
data["person_benunit_id"] = person_benunit_id = ( | ||
ukmod.idorigbenunit * 10 + ukmod.idorighh | ||
) | ||
data["person_household_id"] = person_household_id = ( | ||
ukmod.idorighh * 100 | ||
) | ||
data["person_state_id"] = np.ones_like(ukmod.idperson) | ||
|
||
data["benunit_id"] = person_benunit_id.unique() | ||
data["household_id"] = person_household_id.unique() | ||
data["state_id"] = np.array([1]) | ||
|
||
data["age"] = ukmod.dag.values | ||
data["gender"] = np.where( | ||
ukmod.dgn == 0, | ||
"FEMALE", | ||
"MALE", | ||
).astype("S") | ||
data["employment_income"] = ukmod.yem.values * 12 | ||
data["self_employment_income"] = ukmod.yse.values * 12 | ||
data["pension_income"] = ukmod.ypp.values * 12 | ||
data["statutory_sick_pay"] = ukmod.bhlwk.values * 12 | ||
data["statutory_maternity_pay"] = ukmod.bmact_s.values * 12 | ||
data["statutory_paternity_pay"] = ukmod.bpact_s.values * 12 | ||
|
||
for variable in data: | ||
data[variable] = {"2018": data[variable]} | ||
|
||
self.save_dataset(data) |
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,3 @@ | ||
from pathlib import Path | ||
|
||
STORAGE_FOLDER = Path(__file__).parent |
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
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
66 changes: 66 additions & 0 deletions
66
policyengine_uk/tests/microsimulation/test_against_ukmod.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,66 @@ | ||
from policyengine_uk import Microsimulation | ||
from policyengine_uk.data.datasets import UKMOD_FRS_2018 | ||
from policyengine_uk.data.storage import STORAGE_FOLDER | ||
import pandas as pd | ||
import numpy as np | ||
import pytest | ||
|
||
SKIP_UKMOD_TESTS = True | ||
|
||
if not SKIP_UKMOD_TESTS: | ||
ukmod_output = pd.read_csv( | ||
STORAGE_FOLDER / "uk_2018_std.txt", delimiter="\t" | ||
) | ||
ukmod_input = pd.read_csv( | ||
STORAGE_FOLDER / "uk_2018_a4.txt", delimiter="\t" | ||
) | ||
output_columns = [ | ||
column | ||
for column in ukmod_output.columns | ||
if column not in ukmod_input.columns | ||
] | ||
ukmod = pd.merge( | ||
ukmod_output[output_columns + ["idperson"]], | ||
ukmod_input, | ||
on="idperson", | ||
how="right", | ||
) | ||
|
||
UKMOD_FRS_2018().generate() | ||
sim = Microsimulation(dataset="ukmod_frs_2018") | ||
|
||
|
||
@pytest.mark.skip(reason="UKMOD data not publicly shareable") | ||
def test_ni_class_1(): | ||
# NI Class 1 income matches. | ||
assert np.allclose( | ||
sim.calculate("ni_class_1_income").values, | ||
ukmod.il_empniearns.values * 12, | ||
atol=1, | ||
) | ||
|
||
|
||
@pytest.mark.skip(reason="UKMOD data not publicly shareable") | ||
def test_ni_class_1_employee(): | ||
# NI contributions are off by more because the thresholds change mid-year, | ||
# and PolicyEngine simulates over the full year while UKMOD simulates one | ||
# month. | ||
assert np.allclose( | ||
sim.calculate("ni_class_1_employee").values, | ||
ukmod.tscee_s.values * 12, | ||
atol=50, | ||
) | ||
|
||
|
||
@pytest.mark.skip(reason="UKMOD data not publicly shareable") | ||
def test_ni_self_employed(): | ||
# NI self-employed contributions don't match entirely for people with both | ||
# self-employed and employment income. This might be due to a different | ||
# interpretation of the rules around capped NI contributions (our Class 4 | ||
# maximum uses the legislation as a reference). | ||
|
||
error = np.abs( | ||
sim.calculate("ni_self_employed").values - ukmod.tscse_s.values * 12 | ||
) | ||
|
||
assert (error < 50).mean() > 0.99 |
22 changes: 0 additions & 22 deletions
22
policyengine_uk/tests/policy/baseline/finance/tax/national_insurance/class_1.yaml
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
policyengine_uk/tests/policy/baseline/finance/tax/national_insurance/class_2.yaml
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
policyengine_uk/tests/policy/baseline/finance/tax/national_insurance/class_4.yaml
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
policyengine_uk/tests/policy/baseline/finance/tax/national_insurance/national_insurance.yaml
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...ine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_1/ni_class_1_employee.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,22 @@ | ||
- name: NI Class 1 employee contributions - below PT | ||
period: 2023 | ||
input: | ||
ni_class_1_income: 11_000 | ||
output: | ||
ni_class_1_employee: 0 | ||
|
||
- name: NI Class 1 employee contributions - between PT and UEL | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
ni_class_1_income: 30_000 | ||
output: | ||
ni_class_1_employee: (30_000 - 12_570) * 0.12 | ||
|
||
- name: NI Class 1 employee contributions - above UEL | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
ni_class_1_income: 70000 | ||
output: | ||
ni_class_1_employee: (50_270 - 12_570) * 0.12 + (70_000 - 50_270) * 0.0325 |
15 changes: 15 additions & 0 deletions
15
...s/policy/baseline/gov/hmrc/national_insurance/class_1/ni_class_1_employee_additional.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,15 @@ | ||
- name: NI Class 1 employee additional contributions - income below UEL | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
ni_class_1_income: 30_000 | ||
output: | ||
ni_class_1_employee_additional: 0 | ||
|
||
- name: NI Class 1 employee additional contributions - income above UEL | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
ni_class_1_income: 100_000 | ||
output: | ||
ni_class_1_employee_additional: (100_000 - 50_270) * 0.0325 |
7 changes: 7 additions & 0 deletions
7
...ests/policy/baseline/gov/hmrc/national_insurance/class_1/ni_class_1_employee_primary.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,7 @@ | ||
- name: £20k income has NI Class 1 liability | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
employment_income: 20_000 | ||
output: | ||
ni_class_1_employee_primary: (20_000 - 12_570) * 0.12 |
15 changes: 15 additions & 0 deletions
15
...ine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_1/ni_class_1_employer.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,15 @@ | ||
- name: NI Class 1 employer contributions for low income in 2023 | ||
period: 2023 | ||
absolute_error_margin: 0.01 | ||
input: | ||
ni_class_1_income: 8_000 # Annual income below secondary threshold | ||
output: | ||
ni_class_1_employer: 0.00 # Expected employer contributions | ||
|
||
- name: NI Class 1 employer contributions for moderate income in 2023 | ||
period: 2023 | ||
absolute_error_margin: 0.01 | ||
input: | ||
ni_class_1_income: 30_000 # Annual income above secondary threshold but below upper limit | ||
output: | ||
ni_class_1_employer: (30_000 - 175 * 52) * 0.138 # Expected employer contributions |
9 changes: 9 additions & 0 deletions
9
...ngine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_1/ni_class_1_income.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,9 @@ | ||
- name: NI Class 1 income sums income components. | ||
period: 2023 | ||
input: | ||
employment_income: 1 | ||
statutory_sick_pay: 2 | ||
statutory_maternity_pay: 4 | ||
statutory_paternity_pay: 8 | ||
output: | ||
ni_class_1_income: 15 |
20 changes: 20 additions & 0 deletions
20
policyengine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_1/ni_liable.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,20 @@ | ||
- name: Child isn't liable for NI. | ||
period: 2023 | ||
input: | ||
age: 15 | ||
output: | ||
ni_liable: false | ||
|
||
- name: Working-age adult is liable for NI. | ||
period: 2023 | ||
input: | ||
age: 35 | ||
output: | ||
ni_liable: true | ||
|
||
- name: Retired adult isn't liable for NI. | ||
period: 2023 | ||
input: | ||
age: 70 | ||
output: | ||
ni_liable: false |
13 changes: 13 additions & 0 deletions
13
policyengine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_2/ni_class_2.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,13 @@ | ||
- name: NI Class 2 - under LPL. | ||
period: 2023 | ||
input: | ||
self_employment_income: 5_000 | ||
output: | ||
ni_class_2: 0 | ||
|
||
- name: NI Class 2 - over LPL. | ||
period: 2023 | ||
input: | ||
self_employment_income: 15_000 | ||
output: | ||
ni_class_2: 3.15 * 52 |
7 changes: 7 additions & 0 deletions
7
policyengine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_4/ni_class_4.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,7 @@ | ||
- name: NI Class 4 - under UPL. | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
self_employment_income: 30_000 | ||
output: | ||
ni_class_4: 1628 |
7 changes: 7 additions & 0 deletions
7
...yengine_uk/tests/policy/baseline/gov/hmrc/national_insurance/class_4/ni_class_4_main.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,7 @@ | ||
- name: NI Class 4 - over UPL has maximum capped. | ||
period: 2023 | ||
absolute_error_margin: 1 | ||
input: | ||
self_employment_income: 100_000 | ||
output: | ||
ni_class_4_main: 3452 |
Oops, something went wrong.