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

Getting Started

\n", "\n", "This notebook illustrates the [key concepts](https://doc.openfisca.fr/en/key-concepts.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": 79, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from openfisca_france import FranceTaxBenefitSystem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize the french [Tax and Benefit system](https://doc.openfisca.fr/en/tax_and_benefit_system.html) : it launches the entire legislation." ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tax_benefit_system = FranceTaxBenefitSystem()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a [scenario](https://doc.openfisca.fr/en/input_data.html#scenario): first step to give a familial situation for which you want to calculate taxes or benefits." ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ "scenario = tax_benefit_system.new_scenario()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize a test case (the familial situation) in the scenario:\n", "here we have one parent and two children." ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scenario.init_single_entity(\n", " period = 2015,\n", " parent1 = dict(\n", " age = 30,\n", " salaire_de_base = 20000,\n", " ),\n", " enfants = [\n", " dict(age = 12),\n", " dict(age = 18),\n", " ],\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a [simulation](https://doc.openfisca.fr/en/simulation.html#simulation--the-framework-of-computation): the OpenFisca frame for calculating taxes or benefits." ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": true }, "outputs": [], "source": [ "simulation = scenario.new_simulation()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate a variable : `\"af\"` (allocations familiales) for example." ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 129.99000549], dtype=float32)" ] }, "execution_count": 84, "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": [ "*An 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": 85, "metadata": { "collapsed": false }, "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": 86, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reform = landais_piketty_saez.landais_piketty_saez(tax_benefit_system)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the scenario you want to test." ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def init_profile(scenario):\n", " scenario.init_single_entity(\n", " period = '2013',\n", " parent1 = dict(\n", " age = 40,\n", " salaire_de_base = 50000,\n", " ),\n", " )\n", " return scenario" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the reform" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 15614.34863281], dtype=float32)" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Indicate that you want to perfom the reform on this scenario\n", "reform_scenario = init_profile(reform.new_scenario())\n", "\n", "#Simulate the reform\n", "reform_simulation = reform_scenario.new_simulation() \n", "\n", "# Choose the variable you want to calcul : here the disposable income, \"revdisp\"\n", "reform_simulation.calculate('revdisp', '2013')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without the reform: the counterfactual" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 36039.1875], dtype=float32)" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Indicate that you want to perfom the standard system on this scenario\n", "reference_scenario = init_profile(tax_benefit_system.new_scenario())\n", "\n", "#Simulate the standard scenario\n", "reference_simulation = reference_scenario.new_simulation()\n", "\n", "# Choose the variable you want to calcul\n", "reference_simulation.calculate('revdisp', '2013')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trace the calculation of a variable\n", "\n", "Thanks to the Trace tool you can see all the details of the variable calculation depending on the scenario you made." ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "http://www.openfisca.fr/tools/trace?api_url=http%3A%2F%2Fapi.openfisca.fr&simulation=%7B%22scenarios%22%3A+%5B%7B%22period%22%3A+%222015%22%2C+%22test_case%22%3A+%7B%22familles%22%3A+%5B%7B%22id%22%3A+0%2C+%22parents%22%3A+%5B%22ind0%22%5D%2C+%22enfants%22%3A+%5B%22ind2%22%2C+%22ind3%22%5D%7D%5D%2C+%22foyers_fiscaux%22%3A+%5B%7B%22id%22%3A+0%2C+%22declarants%22%3A+%5B%22ind0%22%5D%2C+%22personnes_a_charge%22%3A+%5B%22ind2%22%2C+%22ind3%22%5D%7D%5D%2C+%22individus%22%3A+%5B%7B%22id%22%3A+%22ind0%22%2C+%22age%22%3A+30%2C+%22salaire_de_base%22%3A+20000.0%7D%2C+%7B%22id%22%3A+%22ind2%22%2C+%22age%22%3A+12%7D%2C+%7B%22id%22%3A+%22ind3%22%2C+%22age%22%3A+18%7D%5D%2C+%22menages%22%3A+%5B%7B%22id%22%3A+0%2C+%22personne_de_reference%22%3A+%22ind0%22%2C+%22enfants%22%3A+%5B%22ind2%22%2C+%22ind3%22%5D%7D%5D%7D%7D%5D%2C+%22variables%22%3A+%5B%22irpp%22%5D%7D\n" ] } ], "source": [ "from openfisca_core import tools\n", "print(tools.get_trace_tool_link(scenario, variables = ['irpp'], api_url = 'http://api.openfisca.fr', trace_tool_url = 'http://www.openfisca.fr/tools/trace'))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }