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

Table of Contents

\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Importance of giving a convenient period" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook aim to show easily how periods work in OpenFisca. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need to initialize a **simulation**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from openfisca_france import FranceTaxBenefitSystem\n", "tax_benefit_system = FranceTaxBenefitSystem()\n", "scenario = tax_benefit_system.new_scenario()\n", "scenario.init_single_entity(\n", " period = 2015,\n", " parent1 = dict(\n", " age = 30,\n", " salaire_de_base = 50000,\n", " ),\n", " enfants = [\n", " dict(age = 12),\n", " dict(age = 18),\n", " ],\n", " )\n", "simulation = scenario.new_simulation()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simulation's variable is calculated for a specific period. \n", "This specific time interval have to be given when calling a variable to avoid computation problem.\n", "\n", "### Variables with monthly formulas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some variables are computed over the month. \n", "For exemple the variable `allocation familiales`. \n", "If called on an annual basis, an error is displayed : " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AssertionError", "evalue": "Requested period 2015 differs from 2015-01 returned by variable af", "output_type": "error", "traceback": [ "\u001b[0;31m\u001b[0m", "\u001b[0;31mAssertionError\u001b[0mTraceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msimulation\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcalculate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'af'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'2015'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/home/notebook/virtualenvs/openfisca/local/lib/python2.7/site-packages/openfisca_core/simulations.pyc\u001b[0m in \u001b[0;36mcalculate\u001b[0;34m(self, column_name, period, **parameters)\u001b[0m\n\u001b[1;32m 72\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mperiod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 74\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumn_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 75\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcalculate_add\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumn_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/notebook/virtualenvs/openfisca/local/lib/python2.7/site-packages/openfisca_core/simulations.pyc\u001b[0m in \u001b[0;36mcompute\u001b[0;34m(self, column_name, period, **parameters)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[0mcaller_input_variables_infos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvariable_infos\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 158\u001b[0m \u001b[0mholder\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_or_new_holder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumn_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 159\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mholder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 160\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mprint_trace\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_trace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvariable_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolumn_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mprint_trace_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/notebook/virtualenvs/openfisca/local/lib/python2.7/site-packages/openfisca_core/holders.pyc\u001b[0m in \u001b[0;36mcompute\u001b[0;34m(self, period, **parameters)\u001b[0m\n\u001b[1;32m 140\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0maccept_other_period\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mformula_dated_holder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mperiod\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 141\u001b[0m \"Requested period {} differs from {} returned by variable {}\".format(period,\n\u001b[0;32m--> 142\u001b[0;31m formula_dated_holder.period, column.name)\n\u001b[0m\u001b[1;32m 143\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mformula_dated_holder\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[0marray\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mempty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mentity\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcolumn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: Requested period 2015 differs from 2015-01 returned by variable af" ] } ], "source": [ "simulation.calculate('af', '2015')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This assertion error : \n", " \"`Requested period 2015 differs from 2015-01 returned by variable af`\" \n", "means exactly the fact that `af` are computed for a month, therefore the variable returned value for `2015-01`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But when called on its period (monthly), the result of the variable's formula will be returned" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 129.99000549], dtype=float32)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate('af', '2015-01') #calculate variable af for January 2015" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variable with annual formulas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other formulas only works on a annual basis, thus a monthly call will not work, e.g irpp (impôt sur le revenu) :" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "ename": "AssertionError", "evalue": "Requested period 2015-01 differs from 2015 returned by variable irpp", "output_type": "error", "traceback": [ "\u001b[0;31m\u001b[0m", "\u001b[0;31mAssertionError\u001b[0mTraceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msimulation\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcalculate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'irpp'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'2015-01'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/home/notebook/virtualenvs/openfisca/local/lib/python2.7/site-packages/openfisca_core/simulations.pyc\u001b[0m in \u001b[0;36mcalculate\u001b[0;34m(self, column_name, period, **parameters)\u001b[0m\n\u001b[1;32m 72\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mperiod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 74\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumn_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 75\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcalculate_add\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumn_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/notebook/virtualenvs/openfisca/local/lib/python2.7/site-packages/openfisca_core/simulations.pyc\u001b[0m in \u001b[0;36mcompute\u001b[0;34m(self, column_name, period, **parameters)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[0mcaller_input_variables_infos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvariable_infos\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 158\u001b[0m \u001b[0mholder\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_or_new_holder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumn_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 159\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mholder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mperiod\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 160\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mprint_trace\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_trace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvariable_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolumn_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mprint_trace_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/notebook/virtualenvs/openfisca/local/lib/python2.7/site-packages/openfisca_core/holders.pyc\u001b[0m in \u001b[0;36mcompute\u001b[0;34m(self, period, **parameters)\u001b[0m\n\u001b[1;32m 140\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0maccept_other_period\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mformula_dated_holder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mperiod\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 141\u001b[0m \"Requested period {} differs from {} returned by variable {}\".format(period,\n\u001b[0;32m--> 142\u001b[0;31m formula_dated_holder.period, column.name)\n\u001b[0m\u001b[1;32m 143\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mformula_dated_holder\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[0marray\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mempty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mentity\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcolumn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: Requested period 2015-01 differs from 2015 returned by variable irpp" ] } ], "source": [ "simulation.calculate('irpp', '2015-01')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It must be called on an annual basis :" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-2400.52807617], dtype=float32)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate('irpp', '2015') #calculate variable irpp for 2015" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Default period" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When a variable is called without period, the simulation's default period is used." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Period((u'year', Instant((2015, 1, 1)), 1))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.period" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here default simulation period is a year. Thus the variable `irpp` can be computed but not the variable `af`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-2400.52807617]\n" ] } ], "source": [ "print(simulation.calculate('irpp'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case simulation.calculate('irpp') is equivalent to simulation.calculate('irpp', '2015')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ True]\n" ] } ], "source": [ "print(simulation.calculate('irpp') == simulation.calculate('irpp', '2015'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**WARNING** : this demonstration shows the necessity of being aware over which kind of period each measure you want to compute is based on.\n", "\n", "### How to know the definition period of a variable\n", "\n", "If you've forgotten over which kind of period (year or month) your variable is defined in the legislation, you may check in the [legislation explorer](https://legislation.openfisca.fr/variables).\n", "\n", "The information is located in the code source.\n", "\n", "Example for the  [irpp](https://legislation.openfisca.fr/variables/irpp), a tax with annual definition, you will find the period at the end of the website page:\n", "\n", "![](irpp_period.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Specific period calls insure that no errors are made by the user. \n", "If a annual based variable is asked to be computed monthly, the software returns an error.\n", "\n", "But there is solutions to get the result of a given variable on another kind of period, a annual based variable over a month for example." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Calculate_add : small to larger period" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The calculate_add method has the same behavior as calculate, except that it sum all variables given their instant of calculus over a period lenght." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1559.88000488]\n" ] } ], "source": [ "print simulation.calculate_add(\"af\", \"2015\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "This result is equivalent to do sum all monthly calculate over the period." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "annual_af = 0 #create annual_af equals to 0\n", "for month in range(1,13): # [1,2,...,11,12]\n", " annual_af += simulation.calculate(\"af\", '2015-{}'.format(month)) #add recursively af for all month in 2014" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1559.88000488]\n" ] } ], "source": [ "print annual_af" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We thus see that annual_af is equal to simulation.calculate_add('af', \"2014\").\n", "\n", "We can test that :" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ True]\n" ] } ], "source": [ "print simulation.calculate_add('af','2015') == annual_af" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate_divide : large to smaller period" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-200.04400635], dtype=float32)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate_divide(\"irpp\", \"2015-01\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-200.04400635], dtype=float32)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate(\"irpp\", \"2015\")/12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be warned, when a variable is calculated on a monthly basis with calculate_divide (or add), the result is stored in cache.\n", "\n", "Now you can do :" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-200.04400635], dtype=float32)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate('irpp', '2015-01')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate_add_divide \n", "Some variables are not constant over the year but you might want it as an average value for each month.\n", "\n", "For exemple variable `'ars'` (allocation de rentrée scolaire), is computed only in september. To smooth it over the year, we must use calculate_add_divide. It will simply sum it over a given period, and then divide it over sub-periods of that period." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 65.20347595], dtype=float32)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate('ars', '2014')/12" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 49.92318344], dtype=float32)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation.calculate_add_divide('ars', \"2014-08\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Que fait add_divide ?" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [], "source": [ "simulation.compute_add_divide??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The Concept of Period" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use more exotic period by using the class periods. \n", "\n", "Actually when we do : `simulation.calculate('irpp','2015')`, the '2015' string is converted into an object period. \n", "\n", "A more explicit way to do this, is : `simulation.calculate('irpp', periods.period('2015'))`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Period((u'year', Instant((2015, 1, 1)), 1))" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from openfisca_core import periods # OpenFisca_core is the architecture of OpenFisca\n", "periods.period('2015')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-2400.52807617]\n", "[ True]\n" ] } ], "source": [ "print(simulation.calculate('irpp', periods.period('2015')))\n", "print(simulation.calculate('irpp', periods.period('2015'))== simulation.calculate('irpp', 2015))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Definition\n", "We can look at periods docstring to understand how it works :" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Return a new period, aka a triple (unit, start_instant, size).\n", "\n", " >>> period(u'2014')\n", " Period((u'year', Instant((2014, 1, 1)), 1))\n", " >>> period(u'2014:2')\n", " Period((u'year', Instant((2014, 1, 1)), 2))\n", " >>> period(u'2014-2')\n", " Period((u'month', Instant((2014, 2, 1)),\n" ] } ], "source": [ "print periods.period.__doc__[: 280] #periods.period? would also display the help, suppress the brackets to get full doc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A *period* is constituted of three parameters:\n", "- A unit : day, month and year\n", "- A start instant : the date at which it starts\n", "- And a size : the number of unit \n", "\n", "periods.period('day','2014-03-01', 32) would be a period going from the first of March to the first of april" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Computing over several months" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to calculate the 'allocation familiales' from the April to July." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 519.96002197]\n" ] } ], "source": [ "af_march_to_july = simulation.calculate_add('af' ,periods.period('month', '2015-04', 4))\n", "print af_march_to_july" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simplificated version of period declaration exists with the symbol: `\":\"`.\n", "\n", "`year-month:n` means n months beginning at the month, year.\n", "\n", "Example with `af_march_to_july`:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 519.96002197]\n" ] } ], "source": [ "af_march_to_july = simulation.calculate_add('af' ,'2015-04:4')\n", "print af_march_to_july" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Computing over several years with identique scenario\n", "\n", "Computing over several years needs to rethink how we've declared the scenario: \n", "the starting scenario has, as `period`, one year, `2015`, so it won't be able to compute anything outside this time interval.\n", "\n", "The idea is to change the period over which the scenario applies, using the syntax shown previously. \n", "\n", "**WARNING : handling the stretching of values ** \n", "Doing that defines the other variables for the entire time interval including their values." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#we have to give a new scenario\n", "scenario_over_years = tax_benefit_system.new_scenario()\n", "scenario_over_years.init_single_entity(\n", " period = '2014:3', # it means three years starting in 2014\n", " parent1 = dict(\n", " date_naissance = 1975,\n", " salaire_de_base = 50000 * 3 , # if we want 50000 for each of the three years\n", " ),\n", " enfants = [\n", " dict(date_naissance = 2001),\n", " dict(date_naissance = 1999),\n", " ],\n", " )\n", "simulation_over_years = scenario_over_years.new_simulation()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**WARNING : handling the age** \n", "We've changed the variable `age` to `date_naissance` in order that the individuals get older. If we've kept the `age` they would have the same age for the entire period.\n", "\n", "Now we can compute the `irpp` with the function previously used : `calculate_add`" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-4813.68652344], dtype=float32)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation_over_years.calculate_add('irpp', '2014:2') # for 2014 and 2015" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It takes in consideration the legislation change between the two years. \n", "Therefore it equals the sum of `irpp` for each year but not the double of `irpp` for one year." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-4813.68652344]\n", "[-4826.31738281]\n" ] } ], "source": [ "print(simulation_over_years.calculate('irpp', '2015') + simulation_over_years.calculate('irpp', '2014'))\n", "print(simulation_over_years.calculate_add('irpp', '2014')*2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These formulas are still working for variables defined on a monthly basis." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 4616.37402344], dtype=float32)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation_over_years.calculate_add('af', '2015:2')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Computing over several years changing scenario\n", "\n", "You might want to make evolve some given values over the years.\n", "\n", "The tool for it will be to use a Python dictionnary.\n", "\n", "For example, if you want to give a wage evolution :" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scenario_over_years = tax_benefit_system.new_scenario()\n", "scenario_over_years.init_single_entity(\n", " period = '2014:3',\n", " parent1 = dict(\n", " date_naissance = 1975,\n", " salaire_de_base = {'2014':50000, '2015': 50500, '2016':51000},\n", " ),\n", " enfants = [\n", " dict(date_naissance = 2001),\n", " dict(date_naissance = 1999),\n", " ],\n", " )\n", "simulation_over_years = scenario_over_years.new_simulation()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-7376.67675781], dtype=float32)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulation_over_years.calculate_add('irpp', '2014:3') # for 2014, 2015 and 2016" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "*Command line to get the Notebook's Table of Contents:*" ] }, { "cell_type": "code", "execution_count": 31, "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 }