{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples\n", "\n", "## Loading and navigating the model\n", "We start with importing the euromod package and creating a Model object from a EUROMOD model. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{seealso} \n", "See the installation guide on how to install the package and its dependencies.\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "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": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from euromod import Model\n", "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": 2, "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod.countries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see indeed that the euromod model contains 28 countries. In a similar fashion we can look what kind of extensions are stored in the model. The countries container can be indexed by both the number of the element and the country shortcode. Let us take a look at Sweden." ] }, { "cell_type": "code", "execution_count": 3, "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": 3, "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. We can for example take a look at the first 10 policies that are stored in the country." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0: setdefault_se | | DEF: SET DEFAULT \n", "1: uprate_se | | DEF: UPRATING FACTORS \n", "2: ConstDef_se | | DEF: CONSTANTS \n", "3: IlsDef_se | | DEF: INCOME CONCEPTS (standardized) \n", "4: IlsUDBdef_se | | DEF: INCOME CONCEPTS (UDB) \n", "5: ildef_se | | DEF: INCOME CONCEPTS (non-standardized) \n", "6: random_se | | DEF: Random assignment \n", "7: TransLMA_se | | DEF: Modelling labour market transitions (DO NOT S ... \n", "8: tudef_se | | DEF: ASSESSMENT UNITS \n", "9: yem_se | (with switch set for MWA) | DEF: minimum wage " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod.countries[\"SE\"].policies[0:10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running a system with default configuration\n", "Say that we are interested in running the tax system for the year 2021 of Sweden. Building further on the previous example we can look at the tax-systems contained in the model for Sweden." ] }, { "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": [ "In order to run the tax system we need a dataset that fits the requirement to use. The model however provides us with a list of datasets that are configured already." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0: training_data | \n", "1: SE_2019_a1 | \n", "2: SE_2020_b1 | \n", "3: SE_2021_hhot | \n", "4: SE_2021_b1 | best match " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mod.countries[\"SE\"].systems[\"SE_2021\"].datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we see that there are multiple datasets configured, but that PL_2021_b1 is set as the best match dataset for this taxsystem. Provided that you have the microdata stored somewhere, you can then load it as a `pandas.DataFrame` and run the model in the following way:" ] }, { "cell_type": "code", "execution_count": 7, "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": 7, "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 = mod.countries[\"SE\"].systems[\"SE_2021\"].run(data,\"SE_2021_b1\")\n", "out" ] }, { "cell_type": "code", "execution_count": 8, "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": [ "This returns 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": 9, "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", "