{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Covariates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Covariates are central objects in **respy**. They can be used to implement complex structures for payoffs, exogenous processes, and choices. Since Eckstein-Keane-Wolpin models deal with dynamic human capital accumulation, the most basic version of any model will include the variables `experience`, `period` and `lagged_choice`. These variables can always be used to define payoffs, determine transition probabilities of exogenous processes, and restrict choices. However, many exconomic applications require a richer set of variables that affect the decision problem of individuals. Additional structure can be imposed on a model using covariates.\n", "\n", "This guide provides a short overview of covariates and how they can be used to specify a model in **respy**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import respy as rp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Covariates are defined in the **respy** `options` using a nested dictionary. We will look at an example model here and demonstrate how to define covariates. The example model used is a basic Robinson Crusoe model. See the guide below for more information about this example model." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
\n", " Tutorials\n", "\n", " Find out more about the basic Robinson Crusoe economy in params, options, and simulation.\n", "
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'solution_draws': 100,\n", " 'solution_seed': 456,\n", " 'n_periods': 5,\n", " 'simulation_agents': 1000,\n", " 'simulation_seed': 132,\n", " 'estimation_draws': 100,\n", " 'estimation_seed': 100,\n", " 'estimation_tau': 0.001,\n", " 'interpolation_points': -1,\n", " 'covariates': {'constant': '1'}}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params, options = rp.get_example_model(\"robinson_crusoe_basic\", with_data=False)\n", "options" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this very basic model, the only defined covariate is `constant`, which is assigned the constant value of 1. This covariate is then used in `params` to specify the payoff." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
value
categoryname
deltadelta0.95
wage_fishingexp_fishing0.30
nonpec_fishingconstant-0.20
nonpec_hammockconstant2.00
shocks_sdcorrsd_fishing0.50
sd_hammock0.50
corr_hammock_fishing0.00
\n", "
" ], "text/plain": [ " value\n", "category name \n", "delta delta 0.95\n", "wage_fishing exp_fishing 0.30\n", "nonpec_fishing constant -0.20\n", "nonpec_hammock constant 2.00\n", "shocks_sdcorr sd_fishing 0.50\n", " sd_hammock 0.50\n", " corr_hammock_fishing 0.00" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Covariates defining payoffs\n", "\n", "The first part of this guide covers the use of covariates in complex payoff structures. As can be derived from the `params`, the non-pecuniary reward for choosing to relax in the hammock is 1 * 2 = 2. The payoff determined by a covariate is always the value of this covariate times the return, defined in the `value` column. From this very simple example, it becomes clear that covariates always need to be numbers or boolean variables, which are then treated as 0 and 1. Let us now define three more complex covariates. Note, that we always specify a new covariate using already defined ones." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Robinson gets a bonus for fishing, when he was at least three times fishing:\n", "options[\"covariates\"][\"experienced_fisher\"] = 'exp_fishing > 2'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Now we can use this covariate to specify the payoff\n", "params.loc[(\"wage_fishing\", \"experienced_fisher\"), \"value\"] = 0.1" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Now we can use the already existing covariate and define another one on top:\n", "options[\"covariates\"][\n", " \"experienced_fisher_last_period\"\n", "] = \"experienced_fisher & (lagged_choice_1 == 'fishing')\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Note that when using lagged_choice you either have to specify the value in period 0\n", "# or respy assumes a equiprobable distribution over choices. Here I specified, that all\n", "# individuals had choosen hammock in period -1.\n", "params.loc[(\"lagged_choice_1_hammock\", \"constant\"), \"value\"] = 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Now we can use this covariate to specify the payoff\n", "params.loc[(\"wage_fishing\", \"experienced_fisher_last_period\"), \"value\"] = 0.15" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
value
categoryname
deltadelta0.95
lagged_choice_1_hammockconstant1.00
nonpec_fishingconstant-0.20
nonpec_hammockconstant2.00
shocks_sdcorrsd_fishing0.50
sd_hammock0.50
corr_hammock_fishing0.00
wage_fishingexp_fishing0.30
experienced_fisher0.10
experienced_fisher_last_period0.15
\n", "
" ], "text/plain": [ " value\n", "category name \n", "delta delta 0.95\n", "lagged_choice_1_hammock constant 1.00\n", "nonpec_fishing constant -0.20\n", "nonpec_hammock constant 2.00\n", "shocks_sdcorr sd_fishing 0.50\n", " sd_hammock 0.50\n", " corr_hammock_fishing 0.00\n", "wage_fishing exp_fishing 0.30\n", " experienced_fisher 0.10\n", " experienced_fisher_last_period 0.15" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now sort the params DataFrame to group all added payoffs\n", "params.sort_values(by=\"category\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Experience_FishingLagged_Choice_1Shock_Reward_FishingMeas_Error_Wage_FishingShock_Reward_HammockMeas_Error_Wage_HammockDense_KeyCore_IndexChoiceWage...Nonpecuniary_Reward_FishingWage_FishingFlow_Utility_FishingValue_Function_FishingContinuation_Value_FishingNonpecuniary_Reward_HammockWage_HammockFlow_Utility_HammockValue_Function_HammockContinuation_Value_Hammock
IdentifierPeriod
000hammock-0.03503510.040965101fishing0.982635...-0.20.9826350.78263510.46401410.1909262NaN2.0204839.3804177.747300
11fishing0.07425411.506491111fishing1.400917...-0.21.4009171.20091710.0688619.3346772NaN2.7532459.0167416.593153
22fishing-0.35456011.185316122fishing1.526107...-0.21.5261071.3261078.7859267.8524412NaN2.5926587.6371165.309956
33fishing-0.1093971-0.785877133fishing2.990083...-0.22.9900832.7900837.2820004.7283342NaN1.6070614.5745113.123631
44fishing-1.06370511.245234143hammockNaN...-0.22.5046472.3046472.3046470.0000002NaN2.6226172.6226170.000000
.....................................................................
99900hammock0.58409911.611990101fishing1.339169...-0.21.3391691.13916910.82054910.1909262NaN2.80599510.1659307.747300
11fishing-0.39127410.371305111fishing1.110003...-0.21.1100030.9100039.7779479.3346772NaN2.1856528.4491476.593153
22fishing0.3941251-1.448981122fishing2.219013...-0.22.2190132.0190139.4788317.8524412NaN1.2755106.3199685.309956
33fishing0.5310081-0.312350133fishing4.118561...-0.24.1185613.9185618.4104784.7283342NaN1.8438254.8112743.123631
44fishing1.36730211.117095143fishing8.445646...-0.28.4456468.2456468.2456460.0000002NaN2.5585472.5585470.000000
\n", "

5000 rows × 22 columns

\n", "
" ], "text/plain": [ " Experience_Fishing Lagged_Choice_1 Shock_Reward_Fishing \\\n", "Identifier Period \n", "0 0 0 hammock -0.035035 \n", " 1 1 fishing 0.074254 \n", " 2 2 fishing -0.354560 \n", " 3 3 fishing -0.109397 \n", " 4 4 fishing -1.063705 \n", "... ... ... ... \n", "999 0 0 hammock 0.584099 \n", " 1 1 fishing -0.391274 \n", " 2 2 fishing 0.394125 \n", " 3 3 fishing 0.531008 \n", " 4 4 fishing 1.367302 \n", "\n", " Meas_Error_Wage_Fishing Shock_Reward_Hammock \\\n", "Identifier Period \n", "0 0 1 0.040965 \n", " 1 1 1.506491 \n", " 2 1 1.185316 \n", " 3 1 -0.785877 \n", " 4 1 1.245234 \n", "... ... ... \n", "999 0 1 1.611990 \n", " 1 1 0.371305 \n", " 2 1 -1.448981 \n", " 3 1 -0.312350 \n", " 4 1 1.117095 \n", "\n", " Meas_Error_Wage_Hammock Dense_Key Core_Index Choice \\\n", "Identifier Period \n", "0 0 1 0 1 fishing \n", " 1 1 1 1 fishing \n", " 2 1 2 2 fishing \n", " 3 1 3 3 fishing \n", " 4 1 4 3 hammock \n", "... ... ... ... ... \n", "999 0 1 0 1 fishing \n", " 1 1 1 1 fishing \n", " 2 1 2 2 fishing \n", " 3 1 3 3 fishing \n", " 4 1 4 3 fishing \n", "\n", " Wage ... Nonpecuniary_Reward_Fishing Wage_Fishing \\\n", "Identifier Period ... \n", "0 0 0.982635 ... -0.2 0.982635 \n", " 1 1.400917 ... -0.2 1.400917 \n", " 2 1.526107 ... -0.2 1.526107 \n", " 3 2.990083 ... -0.2 2.990083 \n", " 4 NaN ... -0.2 2.504647 \n", "... ... ... ... ... \n", "999 0 1.339169 ... -0.2 1.339169 \n", " 1 1.110003 ... -0.2 1.110003 \n", " 2 2.219013 ... -0.2 2.219013 \n", " 3 4.118561 ... -0.2 4.118561 \n", " 4 8.445646 ... -0.2 8.445646 \n", "\n", " Flow_Utility_Fishing Value_Function_Fishing \\\n", "Identifier Period \n", "0 0 0.782635 10.464014 \n", " 1 1.200917 10.068861 \n", " 2 1.326107 8.785926 \n", " 3 2.790083 7.282000 \n", " 4 2.304647 2.304647 \n", "... ... ... \n", "999 0 1.139169 10.820549 \n", " 1 0.910003 9.777947 \n", " 2 2.019013 9.478831 \n", " 3 3.918561 8.410478 \n", " 4 8.245646 8.245646 \n", "\n", " Continuation_Value_Fishing Nonpecuniary_Reward_Hammock \\\n", "Identifier Period \n", "0 0 10.190926 2 \n", " 1 9.334677 2 \n", " 2 7.852441 2 \n", " 3 4.728334 2 \n", " 4 0.000000 2 \n", "... ... ... \n", "999 0 10.190926 2 \n", " 1 9.334677 2 \n", " 2 7.852441 2 \n", " 3 4.728334 2 \n", " 4 0.000000 2 \n", "\n", " Wage_Hammock Flow_Utility_Hammock Value_Function_Hammock \\\n", "Identifier Period \n", "0 0 NaN 2.020483 9.380417 \n", " 1 NaN 2.753245 9.016741 \n", " 2 NaN 2.592658 7.637116 \n", " 3 NaN 1.607061 4.574511 \n", " 4 NaN 2.622617 2.622617 \n", "... ... ... ... \n", "999 0 NaN 2.805995 10.165930 \n", " 1 NaN 2.185652 8.449147 \n", " 2 NaN 1.275510 6.319968 \n", " 3 NaN 1.843825 4.811274 \n", " 4 NaN 2.558547 2.558547 \n", "\n", " Continuation_Value_Hammock \n", "Identifier Period \n", "0 0 7.747300 \n", " 1 6.593153 \n", " 2 5.309956 \n", " 3 3.123631 \n", " 4 0.000000 \n", "... ... \n", "999 0 7.747300 \n", " 1 6.593153 \n", " 2 5.309956 \n", " 3 3.123631 \n", " 4 0.000000 \n", "\n", "[5000 rows x 22 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulate = rp.get_simulate_func(params, options)\n", "df = simulate(params)\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Covariates defining exogenous process probabilities\n", "\n", "Covariates can also be used to define complex probabilitiy structures in respy. For more information see \"Processes with increasing characteristics\" in the tutorial for exogenous processes." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
\n", " Tutorials\n", "\n", " Find out more about exogenous processes.\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }