{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# User Guide\n", "\n", "The Euromod Conector is a Python library providing tools for running simulations and interacting with the tax-benefit microsimulation model [EUROMOD](https://euromod-web.jrc.ec.europa.eu \"https://euromod-web.jrc.ec.europa.eu\"). \n", "\n", "## Installation\n", "The Euromod Connector can be installed from [PyPi](https://test.pypi.org/project/euromod/) using _pip_:\n", "```bash\n", "$ pip install euromod\n", "```\n", "\n", "### Requirements\n", "In order to run the model, we require two components: 1) the model (coded policy rules), and 2) the input microdata with the variables that respect the EUROMOD naming conventions.\n", "For more information, please, read the sections \"Model\" and \"Input microdata\" on the [Download Euromod](https://euromod-web.jrc.ec.europa.eu/download-euromod \"https://euromod-web.jrc.ec.europa.eu/download-euromod\") web page." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running and navigating the model\n", "The euromod package is object oriented and evolves around using the Model class that loads a representation of the EUROMOD model. This can be imported as follows:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from euromod import Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an object of the `Model` class by passing the path to the EUROMOD project: " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "------------------------------\n", "Model\n", "------------------------------\n", "\t countries: 28 elements\n", "\t extensions: 11 elements\n", "\t model_path: 'C:\\\\EUROMOD_RELEASES_I6.0+'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod=Model(r\"C:\\EUROMOD_RELEASES_I6.0+\")\n", "mod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that every object that is related to the EUROMOD project comes with an informative description. Here we can see that the model has 3 relevant attributes to the user:\n", "- countries\n", "- extensions\n", "- model_path\n", " \n", "The countries and extensions attributes contain elements of the respective objects. If we take a look at `countries`:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0: AT \n", "1: BE \n", "2: BG \n", "3: CY \n", "4: CZ \n", "5: DE \n", "6: DK \n", "7: EE \n", "8: EL \n", "9: ES \n", "10: FI\n", "11: FR\n", "12: HR\n", "13: HU\n", "14: IE\n", "15: IT\n", "16: LT\n", "17: LU\n", "18: LV\n", "19: MT\n", "20: NL\n", "21: PL\n", "22: PT\n", "23: RO\n", "24: SE\n", "25: SI\n", "26: SK\n", "27: SL" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod.countries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `countries` attirbute is a container storing the Country objects nesting the country-specific tax-benefit `policies` and `systems`. We see indeed that the euromod model contains 28 countries. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} \n", "The elements in an attribute of type Container can be accessed by indexing the attribute with the element position.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a similar fashion we can look what kind of extensions are stored in the model. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{tip} \n", "The countries container can be indexed by both the number of the element and the country shortcode. \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us take a look at Sweden:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "------------------------------\n", "Country\n", "------------------------------\n", "\t datasets: 27 elements\n", "\t extensions: 12 elements\n", "\t local_extensions: COVID\n", "\t name: 'SE'\n", "\t policies: 26 elements\n", "\t systems: 18 elements" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod.countries[\"SE\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we see again an informative representation of the `Country` object, which contains several attributes that can be accessed. In order to simulate a system we run a specific System object. We can obtain the systems for Sweden as follows:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0: SE_2006 \n", "1: SE_2007 \n", "2: SE_2008 \n", "3: SE_2009 \n", "4: SE_2010 \n", "5: SE_2011 \n", "6: SE_2012 \n", "7: SE_2013 \n", "8: SE_2014 \n", "9: SE_2015 \n", "10: SE_2016\n", "11: SE_2017\n", "12: SE_2018\n", "13: SE_2019\n", "14: SE_2020\n", "15: SE_2021\n", "16: SE_2022\n", "17: SE_2023" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod.countries[\"SE\"].systems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{tip} \n", "The systems container can be indexed by both the number of the element and the system name. \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running a simulation\n", "\n", "In order to run the tax system we need a dataset that fits the requirement to use. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{seealso} \n", "See Other important euromod objects on what datasets are configured and how.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you know already which dataset to use you can simply load the data and run the model as follows:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Simulation for system SE_2021 with dataset SE_2021_b1 finished.\n" ] }, { "data": { "text/plain": [ "------------------------------\n", "Simulation\n", "------------------------------\n", "\t constantsToOverwrite: {}\n", "\t errors: []\n", "\t output_filenames: ['se_2021_std.txt']\n", "\t outputs: Pandas DataFrame of 240 variables and 21671 observations." ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "data=pd.read_csv(r\"C:\\EUROMOD_RELEASES_I6.0+\\Input\\SE_2021_b1.txt\",sep=\"\\t\")\n", "out_baseline = mod.countries[\"SE\"].systems[\"SE_2021\"].run(data,\"SE_2021_b1\")\n", "out_baseline" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "pd.set_option('display.max_rows', 11, 'display.max_columns',7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the run function here takes the mandatory argument `dataset_id`, which in our case is SE_2021_b1. This is necessary such that EUROMOD can apply the dataset specific logic with respect to setting default values and uprating. This returned us a Simulation object with multiple attributes. The one of interest here is outputs, which contains the outputdataset(s) returned by the microsimulation model:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | idhh | \n", "idperson | \n", "idmother | \n", "... | \n", "tu_bho_se_IsDependentChild | \n", "tu_bho_se_IsLoneParent | \n", "tu_bho_se_IsPartner | \n", "
|---|---|---|---|---|---|---|---|
| 0 | \n", "200.0 | \n", "20001.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
| 1 | \n", "300.0 | \n", "30001.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
| 2 | \n", "300.0 | \n", "30002.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "1.0 | \n", "
| 3 | \n", "500.0 | \n", "50001.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
| 4 | \n", "500.0 | \n", "50002.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "1.0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 21666 | \n", "1936500.0 | \n", "193650002.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "1.0 | \n", "
| 21667 | \n", "1936800.0 | \n", "193680001.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "1.0 | \n", "
| 21668 | \n", "1936800.0 | \n", "193680002.0 | \n", "0.0 | \n", "... | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
| 21669 | \n", "1936800.0 | \n", "193680003.0 | \n", "193680002.0 | \n", "... | \n", "1.0 | \n", "0.0 | \n", "0.0 | \n", "
| 21670 | \n", "1936800.0 | \n", "193680004.0 | \n", "193680002.0 | \n", "... | \n", "1.0 | \n", "0.0 | \n", "0.0 | \n", "
21671 rows × 240 columns
\n", "