Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilwoodruff committed Oct 19, 2023
1 parent 364fac9 commit 2540d27
Show file tree
Hide file tree
Showing 7 changed files with 3,041 additions and 2,934 deletions.
54 changes: 38 additions & 16 deletions docs/book/examples/income-sources.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1152,15 +1152,25 @@
"\n",
"sim = Microsimulation()\n",
"\n",
"is_pensioner_household = (sim.calculate(\"is_SP_age\", map_to=\"household\") > 0) * (sim.calculate(\"is_child\", map_to=\"household\") == 0)\n",
"is_pensioner_household = (\n",
" sim.calculate(\"is_SP_age\", map_to=\"household\") > 0\n",
") * (sim.calculate(\"is_child\", map_to=\"household\") == 0)\n",
"household_benefits = sim.calculate(\"household_benefits\")\n",
"household_pensions = sim.calculate(\"pension_income\", map_to=\"household\")\n",
"household_investment_income = sim.calculate(\"capital_income\", map_to=\"household\")\n",
"household_earnings = sim.calculate(\"employment_income\", map_to=\"household\") + sim.calculate(\"self_employment_income\", map_to=\"household\")\n",
"household_investment_income = sim.calculate(\n",
" \"capital_income\", map_to=\"household\"\n",
")\n",
"household_earnings = sim.calculate(\n",
" \"employment_income\", map_to=\"household\"\n",
") + sim.calculate(\"self_employment_income\", map_to=\"household\")\n",
"total_income = sim.calculate(\"household_market_income\") + household_benefits\n",
"\n",
"equivalised_income = sim.calculate(\"equiv_household_net_income\")[is_pensioner_household]\n",
"household_count_people = sim.calculate(\"people\", map_to=\"household\")[is_pensioner_household]\n",
"equivalised_income = sim.calculate(\"equiv_household_net_income\")[\n",
" is_pensioner_household\n",
"]\n",
"household_count_people = sim.calculate(\"people\", map_to=\"household\")[\n",
" is_pensioner_household\n",
"]\n",
"equivalised_income.weights *= household_count_people.values\n",
"household_income_decile = equivalised_income.decile_rank()\n",
"\n",
Expand All @@ -1183,22 +1193,34 @@
" income_sources.append(income_source)\n",
" income_source_values = income_source_decodes[income_source]\n",
" values.append(\n",
" income_source_values[is_pensioner_household][in_decile].sum() / total_income[is_pensioner_household][in_decile].sum()\n",
" income_source_values[is_pensioner_household][in_decile].sum()\n",
" / total_income[is_pensioner_household][in_decile].sum()\n",
" )\n",
" cumulative_income += income_source_values[is_pensioner_household][in_decile].sum()\n",
" cumulative_income += income_source_values[is_pensioner_household][\n",
" in_decile\n",
" ].sum()\n",
" # Add 'other income'\n",
" deciles.append(decile)\n",
" income_sources.append(\"Other\")\n",
" values.append(1 - cumulative_income / total_income[is_pensioner_household][in_decile].sum())\n",
" values.append(\n",
" 1\n",
" - cumulative_income\n",
" / total_income[is_pensioner_household][in_decile].sum()\n",
" )\n",
"\n",
"df = pd.DataFrame({\n",
" \"Decile\": deciles,\n",
" \"Income source\": income_sources,\n",
" \"Value\": values,\n",
"})\n",
"df = pd.DataFrame(\n",
" {\n",
" \"Decile\": deciles,\n",
" \"Income source\": income_sources,\n",
" \"Value\": values,\n",
" }\n",
")\n",
"\n",
"# Order by state support, other income, pensions, investment, earnings\n",
"df[\"Income source\"] = pd.Categorical(df[\"Income source\"], [\"State support\", \"Other\", \"Pensions\", \"Investment\", \"Earnings\"])\n",
"df[\"Income source\"] = pd.Categorical(\n",
" df[\"Income source\"],\n",
" [\"State support\", \"Other\", \"Pensions\", \"Investment\", \"Earnings\"],\n",
")\n",
"df = df.sort_values([\"Decile\", \"Income source\"], ascending=[True, False])\n",
"\n",
"import plotly.express as px\n",
Expand All @@ -1217,7 +1239,7 @@
" yaxis=dict(\n",
" tickformat=\".0%\",\n",
" title=\"Percentage of income\",\n",
" tickvals=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, .7, .8, .9, 1],\n",
" tickvals=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1],\n",
" ),\n",
" xaxis=dict(\n",
" title=\"Income decile\",\n",
Expand All @@ -1228,7 +1250,7 @@
"fig = format_fig(fig).update_layout(\n",
" title=\"Sources of income for pensioner households\",\n",
")\n",
"fig\n"
"fig"
]
}
],
Expand Down
73 changes: 48 additions & 25 deletions docs/book/programs/gov/dcms/bbc/tv-licence.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,19 @@
"dcms = system.parameters.gov.dcms\n",
"\n",
"df = pd.DataFrame()\n",
"df['Date'] = [\n",
" parameter.instant_str for parameter in dcms.bbc.tv_licence.colour.values_list\n",
"df[\"Date\"] = [\n",
" parameter.instant_str\n",
" for parameter in dcms.bbc.tv_licence.colour.values_list\n",
"]\n",
"df['Full TV Licence Fee'] = [\n",
" f'£{parameter.value:.2f}' for parameter in dcms.bbc.tv_licence.colour.values_list\n",
"df[\"Full TV Licence Fee\"] = [\n",
" f\"£{parameter.value:.2f}\"\n",
" for parameter in dcms.bbc.tv_licence.colour.values_list\n",
"]\n",
"df['Blind TV Licence Fee'] = [\n",
" f'£{0.5 * parameter.value:.2f}' for parameter in dcms.bbc.tv_licence.colour.values_list\n",
"df[\"Blind TV Licence Fee\"] = [\n",
" f\"£{0.5 * parameter.value:.2f}\"\n",
" for parameter in dcms.bbc.tv_licence.colour.values_list\n",
"]\n",
"df.sort_values('Date').set_index('Date')"
"df.sort_values(\"Date\").set_index(\"Date\")"
]
},
{
Expand Down Expand Up @@ -311,24 +314,29 @@
],
"source": [
"import pandas as pd\n",
"\n",
"# aged discount\n",
"aged_discount = {\n",
" \"Reformed value\": [0, 0.25, 0.50, 0.75],\n",
" \"Current value\": [],\n",
" \"Change against current\": [],\n",
" \"Tax revenues impact (£m)\": [91.7, 68.8, 45.9, 22.9],\n",
" \"Reference\": []\n",
" \"Reference\": [],\n",
"}\n",
"reference_list_ad = [\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27903&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27083&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=19090&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27909&region=uk&timePeriod=2023&baseline=1\"\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27909&region=uk&timePeriod=2023&baseline=1\",\n",
"]\n",
"for i in range(len(aged_discount[\"Reformed value\"])):\n",
" aged_discount[\"Current value\"] += [\"100%\"]\n",
" aged_discount[\"Change against current\"] += [f\"{aged_discount['Reformed value'][i]-1:.0%}\"]\n",
" aged_discount[\"Reformed value\"][i] = f\"{aged_discount['Reformed value'][i]:.0%}\"\n",
" aged_discount[\"Change against current\"] += [\n",
" f\"{aged_discount['Reformed value'][i]-1:.0%}\"\n",
" ]\n",
" aged_discount[\"Reformed value\"][\n",
" i\n",
" ] = f\"{aged_discount['Reformed value'][i]:.0%}\"\n",
" aged_discount[\"Reference\"] += [\n",
" f\"<a href=\\\"{reference_list_ad[i]}\\\">Budgetary impact of changing aged discount to {aged_discount['Reformed value'][i]}</a>\"\n",
" ]\n",
Expand All @@ -337,14 +345,25 @@
"df_aged_discount[\"Reformed policy\"] = \"Aged discount\"\n",
"\n",
"# minimum age\n",
"age_list = list(range(70,81))\n",
"age_list = list(range(70, 81))\n",
"age_list.remove(75)\n",
"min_age = {\n",
" \"Reformed value\": age_list,\n",
" \"Current value\": [],\n",
" \"Change against current\": [],\n",
" \"Tax revenues impact (£m)\": [-19.6, -11.4, -7.7, -2.4, -1.5, 1.3, 2.2, 3.7, 7.1, 91.7],\n",
" \"Reference\": []\n",
" \"Tax revenues impact (£m)\": [\n",
" -19.6,\n",
" -11.4,\n",
" -7.7,\n",
" -2.4,\n",
" -1.5,\n",
" 1.3,\n",
" 2.2,\n",
" 3.7,\n",
" 7.1,\n",
" 91.7,\n",
" ],\n",
" \"Reference\": [],\n",
"}\n",
"reference_list_minage = [\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27932&region=uk&timePeriod=2023&baseline=1\",\n",
Expand All @@ -356,13 +375,13 @@
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27951&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27953&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27955&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27960&region=uk&timePeriod=2023&baseline=1\"\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27960&region=uk&timePeriod=2023&baseline=1\",\n",
"]\n",
"for i in range(len(min_age[\"Reformed value\"])):\n",
" min_age[\"Current value\"] += [75]\n",
" min_age[\"Change against current\"] += [min_age[\"Reformed value\"][i] - 75]\n",
" min_age[\"Reference\"] += [\n",
" f\"<a href=\\\"{reference_list_minage[i]}\\\">Budgetary impact of changing minimum age for aged discount to {age_list[i]}</a>\"\n",
" f'<a href=\"{reference_list_minage[i]}\">Budgetary impact of changing minimum age for aged discount to {age_list[i]}</a>'\n",
" ]\n",
"\n",
"df_min_age = pd.DataFrame(min_age)\n",
Expand All @@ -372,32 +391,36 @@
"fee_list = list(range(157, 162))\n",
"fee_list.remove(159)\n",
"licence_fee = {\n",
" \"Reformed value\": fee_list,\n",
" \"Current value\": [],\n",
" \"Change against current\": [],\n",
" \"Tax revenues impact (£m)\": [-52.1, -26, 26, 52.1],\n",
" \"Reference\": []\n",
" \"Reformed value\": fee_list,\n",
" \"Current value\": [],\n",
" \"Change against current\": [],\n",
" \"Tax revenues impact (£m)\": [-52.1, -26, 26, 52.1],\n",
" \"Reference\": [],\n",
"}\n",
"reference_list_fee = [\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27089&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=27087&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=26981&region=uk&timePeriod=2023&baseline=1\",\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=26983&region=uk&timePeriod=2023&baseline=1\"\n",
" \"https://policyengine.org/uk/policy?focus=policyOutput.netIncome&reform=26983&region=uk&timePeriod=2023&baseline=1\",\n",
"]\n",
"for i in range(len(licence_fee[\"Reformed value\"])):\n",
" licence_fee[\"Current value\"] += [\"£159\"]\n",
" licence_fee[\"Change against current\"] += [f\"£{licence_fee['Reformed value'][i]-159}\"]\n",
" licence_fee[\"Change against current\"] += [\n",
" f\"£{licence_fee['Reformed value'][i]-159}\"\n",
" ]\n",
" licence_fee[\"Reformed value\"][i] = f\"£{licence_fee['Reformed value'][i]}\"\n",
" licence_fee[\"Reference\"] += [\n",
" f\"<a href=\\\"{reference_list_fee[i]}\\\">Budgetary impact of changing licence fee to {licence_fee['Reformed value'][i]}</a>\"\n",
" ]\n",
"\n",
"df_licence_fee =pd.DataFrame(licence_fee)\n",
"df_licence_fee = pd.DataFrame(licence_fee)\n",
"df_licence_fee[\"Reformed policy\"] = \"TV licence fee\"\n",
"\n",
"# concat df\n",
"df_reform = pd.concat([df_aged_discount, df_min_age, df_licence_fee])\n",
"df_reform.set_index([\"Reformed policy\", \"Current value\", \"Reformed value\"]).style.format(lambda x: x)\n"
"df_reform.set_index(\n",
" [\"Reformed policy\", \"Current value\", \"Reformed value\"]\n",
").style.format(lambda x: x)"
]
}
],
Expand Down
100 changes: 67 additions & 33 deletions docs/book/programs/gov/dwp/pension-credit.ipynb

Large diffs are not rendered by default.

44 changes: 30 additions & 14 deletions docs/book/programs/gov/dwp/universal-credit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,32 @@
"carer_amount = parameters.gov.dwp.universal_credit.elements.carer.amount\n",
"child_amount = parameters.gov.dwp.universal_credit.elements.child.amount\n",
"disabled_amount = parameters.gov.dwp.universal_credit.elements.disabled.amount\n",
"disabled_child_amount = parameters.gov.dwp.universal_credit.elements.child.disabled.amount\n",
"higher_amount = parameters.gov.dwp.universal_credit.elements.child.first.higher_amount\n",
"disabled_child_amount = (\n",
" parameters.gov.dwp.universal_credit.elements.child.disabled.amount\n",
")\n",
"higher_amount = (\n",
" parameters.gov.dwp.universal_credit.elements.child.first.higher_amount\n",
")\n",
"\n",
"elements = [carer_amount, child_amount, disabled_amount, disabled_child_amount, higher_amount] # [...]\n",
"elements = [\n",
" carer_amount,\n",
" child_amount,\n",
" disabled_amount,\n",
" disabled_child_amount,\n",
" higher_amount,\n",
"] # [...]\n",
"\n",
"dates = [\"2016-01-01\", \"2017-01-01\", \"2018-01-01\", \"2019-01-01\", \"2020-01-01\", \"2021-01-01\", \"2022-01-01\", \"2023-01-01\", \"2024-01-01\"]\n",
"dates = [\n",
" \"2016-01-01\",\n",
" \"2017-01-01\",\n",
" \"2018-01-01\",\n",
" \"2019-01-01\",\n",
" \"2020-01-01\",\n",
" \"2021-01-01\",\n",
" \"2022-01-01\",\n",
" \"2023-01-01\",\n",
" \"2024-01-01\",\n",
"]\n",
"names = [\"Carer\", \"Child\", \"Disabled\", \"Disabled child\", \"First child\"]\n",
"\n",
"import pandas as pd\n",
Expand All @@ -206,18 +226,14 @@
"for date in dates:\n",
" for element, name in zip(elements, names):\n",
" # Append to a dataframe: row = date, column = element, value = amount\n",
" new_row = {\n",
" \"date\": date,\n",
" \"element\": name,\n",
" \"amount\": element(date)\n",
" }\n",
" # Append row to the dataframe\n",
" new_row = {\"date\": date, \"element\": name, \"amount\": element(date)}\n",
" # Append row to the dataframe\n",
" df = pd.concat([df, pd.DataFrame([new_row])])\n",
"\n",
"\n",
"# merge element cells\n",
"pivot_df = df.pivot(index=\"date\", columns=\"element\", values=\"amount\")\n",
"pivot_df.fillna(\"\")\n"
"pivot_df.fillna(\"\")"
]
},
{
Expand Down Expand Up @@ -1335,9 +1351,9 @@
" yaxis_range=[0, 500],\n",
" yaxis_tickformat=\",.0f\",\n",
" yaxis_tickprefix=\"£\",\n",
" yaxis_title = \"Amount(£m)\",\n",
" xaxis_title = \"Year\",\n",
" legend_title = \"Element\"\n",
" yaxis_title=\"Amount(£m)\",\n",
" xaxis_title=\"Year\",\n",
" legend_title=\"Element\",\n",
")\n",
"\n",
"fig = format_fig(fig)\n",
Expand Down
16 changes: 9 additions & 7 deletions docs/book/programs/gov/hmrc/fuel-duty.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@
"df = pd.DataFrame()\n",
"\n",
"df[\"Date of change\"] = [\n",
" parameter.instant_str for parameter in hmrc.fuel_duty.petrol_and_diesel.values_list\n",
" parameter.instant_str\n",
" for parameter in hmrc.fuel_duty.petrol_and_diesel.values_list\n",
"]\n",
"df[\"Fuel duty rate (£/litre)\"] = [\n",
" parameter.value for parameter in hmrc.fuel_duty.petrol_and_diesel.values_list\n",
" parameter.value\n",
" for parameter in hmrc.fuel_duty.petrol_and_diesel.values_list\n",
"]\n",
"df.sort_values(\"Date of change\",inplace=True)\n",
"df.sort_values(\"Date of change\", inplace=True)\n",
"df.set_index(\"Date of change\")"
]
},
Expand Down Expand Up @@ -1054,12 +1056,13 @@
],
"source": [
"import plotly.express as px\n",
"\n",
"px.line(\n",
" df,\n",
" x=\"Date of change\",\n",
" y=\"Fuel duty rate (£/litre)\",\n",
" title=\"Fuel duty rate on petrol and giesel\",\n",
" color_discrete_sequence=[\"#2C6496\"]\n",
" color_discrete_sequence=[\"#2C6496\"],\n",
").update_layout(\n",
" yaxis_title=\"Fuel duty rate per litre\",\n",
" yaxis_tickprefix=\"£\",\n",
Expand All @@ -1068,10 +1071,9 @@
" xaxis_tickformat=\"%Y\",\n",
" height=600,\n",
" width=800,\n",
" template=\"plotly_white\"\n",
" template=\"plotly_white\",\n",
").update_xaxes(\n",
" tickangle=45,\n",
" tickfont={\"size\":10}\n",
" tickangle=45, tickfont={\"size\": 10}\n",
").update_traces(\n",
" line_shape=\"hv\"\n",
")"
Expand Down
Loading

0 comments on commit 2540d27

Please sign in to comment.