{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

Getting Started

\n", "\n", "This notebook illustrates the [key concepts](https://openfisca.org/doc/key-concepts/index.html) of OpenFisca that you will find in the documentation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Table of Contents

\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate a variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do some imports." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pprint import pprint # data pretty printer\n", "\n", "from openfisca_core.simulation_builder import SimulationBuilder\n", "from openfisca_france import FranceTaxBenefitSystem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize the French [Tax and Benefit system](https://openfisca.org/doc/key-concepts/tax_and_benefit_system.html): it launches the entire legislation." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "tax_benefit_system = FranceTaxBenefitSystem()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize a test case: first step to give a persons situations for which you want to calculate taxes or benefits. \n", "Here we start with the persons: we have one parent and two children." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "TEST_CASE = {\n", " 'individus': {\n", " 'parent1': {\n", " 'age': {'2015-01': 30},\n", " 'salaire_de_base': {'2015': 20000}\n", " },\n", " 'enfant1': {\n", " 'age': {'2015-01': 12}\n", " },\n", " 'enfant2': {\n", " 'age': {'2015-01': 18}\n", " }\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the role of those persons as members of `foyers_fiscaux` (tax households), `menages` (social households) and `familles` (families) groups. \n", "These groups come with the tax and benefit system." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'familles': {'famille1': {'enfants': ['enfant1', 'enfant2'],\n", " 'parents': ['parent1']}},\n", " 'foyers_fiscaux': {'foyer_fiscal1': {'declarants': ['parent1'],\n", " 'personnes_a_charge': ['enfant1',\n", " 'enfant2']}},\n", " 'individus': {'enfant1': {'age': {'2015-01': 12}},\n", " 'enfant2': {'age': {'2015-01': 18}},\n", " 'parent1': {'age': {'2015-01': 30},\n", " 'salaire_de_base': {'2015': 20000}}},\n", " 'menages': {'menage1': {'enfants': ['enfant1', 'enfant2'],\n", " 'personne_de_reference': ['parent1']}}}\n" ] } ], "source": [ "TEST_CASE['foyers_fiscaux'] = {\n", " 'foyer_fiscal1': {\n", " 'declarants': ['parent1'],\n", " 'personnes_a_charge': ['enfant1', 'enfant2']\n", " }\n", "}\n", "\n", "TEST_CASE['menages'] = {\n", " 'menage1': {\n", " 'personne_de_reference': ['parent1'],\n", " 'enfants': ['enfant1', 'enfant2']\n", " }\n", "}\n", "\n", "TEST_CASE['familles'] = {\n", " 'famille1': {\n", " 'parents': ['parent1'],\n", " 'enfants': ['enfant1', 'enfant2']\n", " }\n", "}\n", "\n", "# display full test case\n", "pprint(TEST_CASE)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And use this test case to create a [simulation](https://openfisca.org/doc/key-concepts/simulation.html#simulation-the-framework-of-computation): the OpenFisca frame for calculating taxes or benefits." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "simulation_builder = SimulationBuilder()\n", "simulation = simulation_builder.build_from_entities(tax_benefit_system, TEST_CASE)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate a variable: `af` (family allowance) for example." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([129.99], dtype=float32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate('af', '2015-01')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test the impact of a reform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A reform represents a modified version of the real Tax and Benefit legislation* \n", "\n", "Reforms are gather in the module [`Reform`](https://github.com/openfisca/openfisca-france/tree/master/openfisca_france/reforms) of OpenFisca.\n", "\n", "You have then to import the module extension corresponding to the reform of your concern.\n", "\n", "Here we choose the fiscal reform of Landais, Piketty and Saez on the income tax ( http://www.revolution-fiscale.fr/la-reforme-proposee ). \n", "\n", "It is already implemented in OpenFisca under the name `landais_piketty_saez` ." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from openfisca_france.reforms import landais_piketty_saez" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a modified version of the tax and benefit system, affected by the changes introduced by the reform" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "reform = landais_piketty_saez.landais_piketty_saez(tax_benefit_system)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the test case you want to test." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "REFORM_TEST_CASE = {\n", " 'individus': {\n", " 'parent1': {\n", " 'age': {'2013-01': 40},\n", " 'salaire_de_base': {'2013': 50000}\n", " }\n", " },\n", " 'foyers_fiscaux': {\n", " 'foyer_fiscal1': {\n", " 'declarants': ['parent1']\n", " }\n", " },\n", " 'menages': { \n", " 'menage1': {\n", " 'personne_de_reference': ['parent1'],\n", " }\n", " }, \n", " 'familles': {\n", " 'famille1': {\n", " 'parents': ['parent1'],\n", " }\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the reform:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([14256.885], dtype=float32)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Indicate that you want to perfom the reform on this test case\n", "simulation_builder = SimulationBuilder()\n", "\n", "# Simulate the reform\n", "reform_simulation = simulation_builder.build_from_entities(reform, REFORM_TEST_CASE)\n", "\n", "# Choose the variable you want to calcul : here the disposable income, \"revenu_disponible\"\n", "reform_simulation.calculate('revenu_disponible', '2013')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without the reform: the baseline (counterfactual)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([34682.22], dtype=float32)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Indicate that you want to perfom the standard system on the same test case\n", "simulation_builder = SimulationBuilder()\n", "baseline_simulation = simulation_builder.build_from_entities(tax_benefit_system, REFORM_TEST_CASE)\n", "\n", "# Choose the variable you want to calcul\n", "baseline_simulation.calculate('revenu_disponible', '2013')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }