Skip to content

Commit

Permalink
Merge pull request PolicyEngine#188 from MaxGhenis/val
Browse files Browse the repository at this point in the history
Refactor validation notebook and display dataframe
  • Loading branch information
MaxGhenis authored Aug 31, 2021
2 parents a522dd9 + 7d55313 commit 86065f4
Showing 1 changed file with 176 additions and 63 deletions.
239 changes: 176 additions & 63 deletions docs/book/validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,201 @@
"source": [
"# Validation\n",
"\n",
"OpenFisca-UK runs unit and integration tests on each new version (see [here](https://github.com/PSLmodels/openfisca-uk/tree/master/tests)). In addition, the table below shows the aggregates produced by the model for the major taxes and benefits, and comparisons with UKMOD (latest [country report](https://www.iser.essex.ac.uk/research/publications/working-papers/cempa/cempa7-20.pdf)) and official sources[^1]. UKMOD and administrative sources refer to 2018, and OpenFisca-UK is simulated on policy at the end of 2018.\n",
"OpenFisca-UK runs unit and integration tests on each new version (see [here](https://github.com/PSLmodels/openfisca-uk/tree/master/tests)).\n",
"In addition, the table below shows the aggregates produced by the model for the major taxes and benefits, and comparisons with UKMOD (latest [country report](https://www.iser.essex.ac.uk/research/publications/working-papers/cempa/cempa7-20.pdf)) and official sources.[^1]\n",
"UKMOD and administrative sources refer to 2018, and OpenFisca-UK is simulated on policy at the end of 2018.\n",
"Numbers are in billions of pounds.\n",
"\n",
"[^1]: From the UKMOD country report: unless otherwise specified: Department for Work and Pensions https://www.gov.uk/government/publications/benefit-expenditure-and-caseload-tables-2018 ; Best Start Grant: https://www2.gov.scot/Topics/Statistics/Browse/Social-Welfare/SocialSecurityforScotland/BSGJune2019; Child tax credit and working tax credit: HMRC statistics \n",
"https://www.gov.uk/government/statistics/child-and-working-tax-credits-statistics-finalised-annual-awards-2016-to-2017; Scottish Child Payment: Scottish Fiscal Commission https://www.fiscalcommission.scot/forecast/supplementary-costing-scottish-child-payment; Scottish Child Winter Heating Assistance: Scottish Fiscal Commission \n",
"https://www.fiscalcommission.scot/forecast/supplementary-costing-child-winter-heating-assistance; Income tax: HMRC statistics https://www.gov.uk/government/statistics/income-tax-liabilities-statistics-tax-year-2014-to-2015-to-tax-year-2017-to-2018; National Insurance Contributions: ONS Blue Book Table 5.2.4s \n"
"https://www.fiscalcommission.scot/forecast/supplementary-costing-child-winter-heating-assistance; Income tax: HMRC statistics https://www.gov.uk/government/statistics/income-tax-liabilities-statistics-tax-year-2014-to-2015-to-tax-year-2017-to-2018; National Insurance Contributions: ONS Blue Book Table 5.2.4s "
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"source": [
"from openfisca_uk import Microsimulation\n",
"from openfisca_uk_data import FRS_SPI_Adjusted\n",
"import pandas as pd\n",
"\n",
"sim = Microsimulation()\n",
"\n",
"VARIABLES = (\n",
" \"income_tax\",\n",
" \"national_insurance\",\n",
" \"employee_NI_class_1\",\n",
" \"employer_NI_class_1\",\n",
" \"NI_class_4\",\n",
" \"universal_credit\",\n",
" \"tax_credits\",\n",
" \"housing_benefit\",\n",
" \"state_pension\",\n",
"VARIABLES = dict(\n",
" income_tax=\"Income Tax\",\n",
" national_insurance=\"Non-employer National Insurance\",\n",
" employee_NI_class_1=\"Employee Class 1 NI\",\n",
" employer_NI_class_1=\"Employer Class 1 NI\",\n",
" NI_class_4=\"Class 2 & 4 NI\",\n",
" universal_credit=\"Universal Credit\",\n",
" tax_credits=\"Tax Credits\",\n",
" housing_benefit=\"Housing Benefit\",\n",
" state_pension=\"State Pension\",\n",
")\n",
"\n",
"df = pd.DataFrame({\"OpenFisca-UK\": sim.df(VARIABLES, map_to=\"household\", period=2019).sum().apply(lambda x: round(x / 1e+9, 1))})\n",
"df[\"Variable\"] = (\n",
" \"Income Tax\",\n",
" \"Non-employer National Insurance\",\n",
" \"Employee Class 1 NI\",\n",
" \"Employer Class 1 NI\",\n",
" \"Class 2 & 4 NI\",\n",
" \"Universal Credit\",\n",
" \"Tax Credits\",\n",
" \"Housing Benefit\",\n",
" \"State Pension\",\n",
")\n",
"\n",
"_ = \"\"\n",
"v = pd.DataFrame(columns=[\"ukmod\", \"external\"])\n",
"v.loc[\"income_tax\"] = 163.7, 190.0\n",
"v.loc[\"national_insurance\"] = 64.0, 55.7\n",
"v.loc[\"employee_NI_class_1\"] = 55.7, 52.0\n",
"v.loc[\"employer_NI_class_1\"] = 77.8, 79.2\n",
"v.loc[\"NI_class_4\"] = 5.0, 3.7\n",
"v.loc[\"universal_credit\"] = 11.7, 8.1\n",
"v.loc[\"tax_credits\"] = 13.9, 22.8\n",
"v.loc[\"housing_benefit\"] = 15.1, 20.7\n",
"v.loc[\"state_pension\"] = 86.4, 85.5\n",
"\n",
"df[\"UKMOD\"] = (\n",
" 163.7,\n",
" 64.0,\n",
" 55.7,\n",
" 77.8,\n",
" 5.0,\n",
" 11.7,\n",
" 13.9,\n",
" 15.1,\n",
" 86.4,\n",
"v[\"of\"] = (\n",
" sim.df(list(VARIABLES.keys()), map_to=\"household\", period=2019).sum() / 1e9\n",
")\n",
"\n",
"df[\"External\"] = (\n",
" 190.0,\n",
" 55.7,\n",
" 52.0,\n",
" 79.2,\n",
" 3.7,\n",
" 8.1,\n",
" 22.8,\n",
" 20.7,\n",
" 85.5,\n",
"v[\"of_ukmod\"] = round(v.of / v.ukmod, 2)\n",
"v[\"of_external\"] = round(v.of / v.external, 2)\n",
"v.of = v.of.round(1)\n",
"COLUMN_RENAMES = dict(\n",
" of=\"OpenFisca-UK\",\n",
" ukmod=\"UKMOD\",\n",
" external=\"External\",\n",
" of_ukmod=\"OpenFisca-UK / UKMOD\",\n",
" of_external=\"OpenFisca-UK / External\",\n",
")\n",
"df.set_index(\"Variable\", drop=True, inplace=True)\n",
"\n",
"def safe_ratio(x, y):\n",
" try:\n",
" return round(x / y, 2)\n",
" except:\n",
" return _\n",
"\n",
"df[\"OpenFisca-UK / UKMOD\"] = [safe_ratio(x, y) for x, y in zip(df[\"OpenFisca-UK\"], df[\"UKMOD\"])]\n",
"df[\"OpenFisca-UK / External\"] = [safe_ratio(x, y) for x, y in zip(df[\"OpenFisca-UK\"], df[\"External\"])]\n",
"\n",
"df"
"v.rename(index=VARIABLES, columns=COLUMN_RENAMES)\n"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" UKMOD External OpenFisca-UK \\\n",
"Income Tax 163.7 190.0 170.1 \n",
"Non-employer National Insurance 64.0 55.7 55.6 \n",
"Employee Class 1 NI 55.7 52.0 51.7 \n",
"Employer Class 1 NI 77.8 79.2 56.8 \n",
"Class 2 & 4 NI 5.0 3.7 3.5 \n",
"Universal Credit 11.7 8.1 13.5 \n",
"Tax Credits 13.9 22.8 3.8 \n",
"Housing Benefit 15.1 20.7 15.9 \n",
"State Pension 86.4 85.5 103.0 \n",
"\n",
" OpenFisca-UK / UKMOD OpenFisca-UK / External \n",
"Income Tax 1.04 0.90 \n",
"Non-employer National Insurance 0.87 1.00 \n",
"Employee Class 1 NI 0.93 0.99 \n",
"Employer Class 1 NI 0.73 0.72 \n",
"Class 2 & 4 NI 0.70 0.95 \n",
"Universal Credit 1.15 1.67 \n",
"Tax Credits 0.27 0.17 \n",
"Housing Benefit 1.05 0.77 \n",
"State Pension 1.19 1.20 "
],
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>UKMOD</th>\n",
" <th>External</th>\n",
" <th>OpenFisca-UK</th>\n",
" <th>OpenFisca-UK / UKMOD</th>\n",
" <th>OpenFisca-UK / External</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Income Tax</th>\n",
" <td>163.7</td>\n",
" <td>190.0</td>\n",
" <td>170.1</td>\n",
" <td>1.04</td>\n",
" <td>0.90</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Non-employer National Insurance</th>\n",
" <td>64.0</td>\n",
" <td>55.7</td>\n",
" <td>55.6</td>\n",
" <td>0.87</td>\n",
" <td>1.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Employee Class 1 NI</th>\n",
" <td>55.7</td>\n",
" <td>52.0</td>\n",
" <td>51.7</td>\n",
" <td>0.93</td>\n",
" <td>0.99</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Employer Class 1 NI</th>\n",
" <td>77.8</td>\n",
" <td>79.2</td>\n",
" <td>56.8</td>\n",
" <td>0.73</td>\n",
" <td>0.72</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Class 2 &amp; 4 NI</th>\n",
" <td>5.0</td>\n",
" <td>3.7</td>\n",
" <td>3.5</td>\n",
" <td>0.70</td>\n",
" <td>0.95</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Universal Credit</th>\n",
" <td>11.7</td>\n",
" <td>8.1</td>\n",
" <td>13.5</td>\n",
" <td>1.15</td>\n",
" <td>1.67</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tax Credits</th>\n",
" <td>13.9</td>\n",
" <td>22.8</td>\n",
" <td>3.8</td>\n",
" <td>0.27</td>\n",
" <td>0.17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Housing Benefit</th>\n",
" <td>15.1</td>\n",
" <td>20.7</td>\n",
" <td>15.9</td>\n",
" <td>1.05</td>\n",
" <td>0.77</td>\n",
" </tr>\n",
" <tr>\n",
" <th>State Pension</th>\n",
" <td>86.4</td>\n",
" <td>85.5</td>\n",
" <td>103.0</td>\n",
" <td>1.19</td>\n",
" <td>1.20</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
]
},
"metadata": {},
"execution_count": 3
}
],
"outputs": [],
"metadata": {
"tags": [
"hide-input"
Expand All @@ -97,7 +210,7 @@
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.8 64-bit ('base': conda)"
"display_name": "Python 3.8.11 64-bit ('base': conda)"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -109,10 +222,10 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.8.11"
},
"interpreter": {
"hash": "d8fe82497dc3af1dafdfcaf67c3f347e622d9ec55d37e96a4812404db83e4772"
"hash": "4a68920d8e8856d089b03c157a7384bba62e0986489c1fa381bbe538cda0922c"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 86065f4

Please sign in to comment.