Getting Started

This notebook illustrates the key concepts of OpenFisca that you will find in the documentation.

Table of Contents

Calculate a variable

Do some imports.

In [1]:
from pprint import pprint  # data pretty printer

from openfisca_core.simulation_builder import SimulationBuilder
from openfisca_france import FranceTaxBenefitSystem
libyaml is not installed in your environment. This can make OpenFisca slower to start. Once you have installed libyaml, run 'pip uninstall pyyaml && pip install pyyaml --no-cache-dir' so that it is used in your Python environment.

Initialize the French Tax and Benefit system: it launches the entire legislation.

In [2]:
tax_benefit_system = FranceTaxBenefitSystem()

Initialize a test case: first step to give a persons situations for which you want to calculate taxes or benefits.
Here we start with the persons: we have one parent and two children.

In [3]:
TEST_CASE = {
    'individus': {
        'parent1': {
            'age': {'2015-01': 30},
            'salaire_de_base': {'2015': 20000}
        },
        'enfant1': {
            'age': {'2015-01': 12}
        },
        'enfant2': {
            'age': {'2015-01': 18}
        }
    }
}

Define the role of those persons as members of foyers_fiscaux (tax households), menages (social households) and familles (families) groups.
These groups come with the tax and benefit system.

In [4]:
TEST_CASE['foyers_fiscaux'] = {
    'foyer_fiscal1': {
        'declarants': ['parent1'],
        'personnes_a_charge': ['enfant1', 'enfant2']
    }
}

TEST_CASE['menages'] = {
    'menage1': {
        'personne_de_reference': ['parent1'],
        'enfants': ['enfant1', 'enfant2']
    }
}

TEST_CASE['familles'] = {
    'famille1': {
        'parents': ['parent1'],
        'enfants': ['enfant1', 'enfant2']
    }
}

# display full test case
pprint(TEST_CASE)
{'familles': {'famille1': {'enfants': ['enfant1', 'enfant2'],
                           'parents': ['parent1']}},
 'foyers_fiscaux': {'foyer_fiscal1': {'declarants': ['parent1'],
                                      'personnes_a_charge': ['enfant1',
                                                             'enfant2']}},
 'individus': {'enfant1': {'age': {'2015-01': 12}},
               'enfant2': {'age': {'2015-01': 18}},
               'parent1': {'age': {'2015-01': 30},
                           'salaire_de_base': {'2015': 20000}}},
 'menages': {'menage1': {'enfants': ['enfant1', 'enfant2'],
                         'personne_de_reference': ['parent1']}}}

And use this test case to create a simulation: the OpenFisca frame for calculating taxes or benefits.

In [5]:
simulation_builder = SimulationBuilder()
simulation = simulation_builder.build_from_entities(tax_benefit_system, TEST_CASE)

Calculate a variable: af (family allowance) for example.

In [6]:
simulation.calculate('af', '2015-01')
Out[6]:
array([129.99], dtype=float32)

Test the impact of a reform

A reform represents a modified version of the real Tax and Benefit legislation*

Reforms are gather in the module Reform of OpenFisca.

You have then to import the module extension corresponding to the reform of your concern.

Here we choose the fiscal reform of Landais, Piketty and Saez on the income tax ( http://www.revolution-fiscale.fr/la-reforme-proposee ).

It is already implemented in OpenFisca under the name landais_piketty_saez .

In [7]:
from openfisca_france.reforms import landais_piketty_saez

Create a modified version of the tax and benefit system, affected by the changes introduced by the reform

In [8]:
reform = landais_piketty_saez.landais_piketty_saez(tax_benefit_system)

Create the test case you want to test.

In [9]:
REFORM_TEST_CASE = {
    'individus': {
        'parent1': {
            'age': {'2013-01': 40},
            'salaire_de_base': {'2013': 50000}
        }
    },
    'foyers_fiscaux': {
        'foyer_fiscal1': {
            'declarants': ['parent1']
        }
    },
    'menages': {    
        'menage1': {
            'personne_de_reference': ['parent1'],
        }
    },    
    'familles': {
        'famille1': {
            'parents': ['parent1'],
        }
    }
}

With the reform:

In [10]:
# Indicate that you want to perfom the reform on this test case
simulation_builder = SimulationBuilder()

# Simulate the reform
reform_simulation = simulation_builder.build_from_entities(reform, REFORM_TEST_CASE)

# Choose the variable you want to calcul : here the disposable income, "revenu_disponible"
reform_simulation.calculate('revenu_disponible', '2013')
Out[10]:
array([14256.885], dtype=float32)

Without the reform: the baseline (counterfactual)

In [11]:
# Indicate that you want to perfom the standard system on the same test case
simulation_builder = SimulationBuilder()
baseline_simulation = simulation_builder.build_from_entities(tax_benefit_system, REFORM_TEST_CASE)

# Choose the variable you want to calcul
baseline_simulation.calculate('revenu_disponible', '2013')
Out[11]:
array([34682.22], dtype=float32)
In [12]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
In [ ]: