{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutorial - ``params``, ``options``, and simulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this first tutorial we will learn how to specify and simulate a simple model with **respy**. As economists love Robinsonades [1](#fn1), we will implement a simple Robinson Crusoe economy as a discrete choice dynamic programming model. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import pandas as pd\n",
"import respy as rp\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The model: a simple Robinson Crusoe economy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this basic Robinson Crusoe economy, Robinson is alone on a desert island and must choose every period $t = \\{0, 1, ... T-1\\}$ between two alternatives: fishing or relaxing on his hammock. Each of the two alternatives is associated with a certain level of instantaneous utility, determined in each period $t$ by a deterministic component and a stochastic component. As Robinson cannot foresee these stochastic components, we speak of \"choice under uncertainty\".
\n",
"\n",
"In **respy**, the instantaneous utility of an alternative, $u_t$, consists of the sum of a pecuniary reward or wage $W_t$ and a non-pecuniary reward $N_t$.
\n",
"Speaking of a wage in this context seems inappropriate since Robinson, being alone on an island, won't receive any wage for the time he spends fishing. However, this distinction is useful to keep in mind when tackling more complex models of labor supply.\n",
"\n",
"The **wage** is zero if Robinson chooses to relax on his hammock. If Robinson goes fishing his wage at time $t$ is:\n",
"\n",
"$$\n",
"W^f = exp\\{x_t \\beta^f + \\epsilon_{ft}\\}\n",
"$$\n",
"\n",
"where:\n",
"\n",
"- $f$ indicates the \"fishing\" alternative; \n",
"- $x_t$ denotes Robinson's experience in fishing in period $t$;\n",
"- $\\beta^f$ denotes the return to experience in fishing; \n",
"- $\\epsilon_{ft}$ is the random shock in period $t$.
\n",
"\n",
"In words, the deterministic component of Robinson's wage is generated each day by only one covariate, his current experience in fishing (which is 0 if Robinson never fished before, 1 if Robinson fished once before, and so on), times the return to experience in fishing. The stochastic component, $\\epsilon_{ft}$, is drawn from a normal distribution, and (may) depend on the current time $t$ and on Robinson's current choice. \n",
"\n",
"The **non-pecuniary reward** for fishing and relaxing on the hammock are simply set as constant terms, with an additional shock, also drawn from a Normal distribution, entering the latter additively: \n",
"\n",
"$$\\begin{align}\n",
"N^f = \\alpha^f \\\\\n",
"N^h = \\alpha^h + \\epsilon_{ht}\\\n",
"\\end{align}$$\n",
"\n",
"Therefore, the parameters of this simple model are $\\alpha^h$ and $\\alpha^f$ (the constants in the non-pecuniary rewards) and $\\beta^f$ (return to experience in fishing), along with the time-preference parameter (see below) and the lower triangular elements of the variance-covariance matrix which governs the distribution of the shocks. \n",
"\n",
"More generally, **respy** allows the wage and non-pecuniary rewards to be flexibly generated specifying a vector of choice- and time-dependent covariates and the associated vector of choice-dependent parameters. "
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/html"
},
"source": [
"
\n", " | \n", " | value | \n", "
---|---|---|
category | \n", "name | \n", "\n", " |
delta | \n", "delta | \n", "0.95 | \n", "
wage_fishing | \n", "exp_fishing | \n", "0.30 | \n", "
nonpec_fishing | \n", "constant | \n", "-0.20 | \n", "
nonpec_hammock | \n", "constant | \n", "2.00 | \n", "
shocks_sdcorr | \n", "sd_fishing | \n", "0.50 | \n", "
sd_hammock | \n", "0.50 | \n", "|
corr_hammock_fishing | \n", "0.00 | \n", "
\n", " | \n", " | Experience_Fishing | \n", "Shock_Reward_Fishing | \n", "Meas_Error_Wage_Fishing | \n", "Shock_Reward_Hammock | \n", "Meas_Error_Wage_Hammock | \n", "Dense_Key | \n", "Core_Index | \n", "Choice | \n", "Wage | \n", "Discount_Rate | \n", "... | \n", "Nonpecuniary_Reward_Fishing | \n", "Wage_Fishing | \n", "Flow_Utility_Fishing | \n", "Value_Function_Fishing | \n", "Continuation_Value_Fishing | \n", "Nonpecuniary_Reward_Hammock | \n", "Wage_Hammock | \n", "Flow_Utility_Hammock | \n", "Value_Function_Hammock | \n", "Continuation_Value_Hammock | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Identifier | \n", "Period | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
0 | \n", "0 | \n", "0 | \n", "-0.035035 | \n", "1 | \n", "0.040965 | \n", "1 | \n", "0 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.982635 | \n", "0.782635 | \n", "9.172605 | \n", "8.831548 | \n", "2 | \n", "NaN | \n", "2.020483 | \n", "9.339589 | \n", "7.704322 | \n", "
1 | \n", "0 | \n", "0.074254 | \n", "1 | \n", "1.506491 | \n", "1 | \n", "1 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "1.037825 | \n", "0.837825 | \n", "6.911083 | \n", "6.392903 | \n", "2 | \n", "NaN | \n", "2.753245 | \n", "8.355429 | \n", "5.897036 | \n", "|
2 | \n", "0 | \n", "-0.354560 | \n", "1 | \n", "1.185316 | \n", "1 | \n", "2 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.837545 | \n", "0.637545 | \n", "4.677616 | \n", "4.252706 | \n", "2 | \n", "NaN | \n", "2.592658 | \n", "6.398066 | \n", "4.005693 | \n", "|
3 | \n", "0 | \n", "-0.109397 | \n", "1 | \n", "-0.785877 | \n", "1 | \n", "3 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.946770 | \n", "0.746770 | \n", "2.793888 | \n", "2.154861 | \n", "2 | \n", "NaN | \n", "1.607061 | \n", "3.554221 | \n", "2.049642 | \n", "|
4 | \n", "0 | \n", "-1.063705 | \n", "1 | \n", "1.245234 | \n", "1 | \n", "4 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.587516 | \n", "0.387516 | \n", "0.387516 | \n", "0.000000 | \n", "2 | \n", "NaN | \n", "2.622617 | \n", "2.622617 | \n", "0.000000 | \n", "|
1 | \n", "0 | \n", "0 | \n", "-0.713137 | \n", "1 | \n", "1.418950 | \n", "1 | \n", "0 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.700075 | \n", "0.500075 | \n", "8.890045 | \n", "8.831548 | \n", "2 | \n", "NaN | \n", "2.709475 | \n", "10.028581 | \n", "7.704322 | \n", "
1 | \n", "0 | \n", "-0.464134 | \n", "1 | \n", "-0.384774 | \n", "1 | \n", "1 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.792893 | \n", "0.592893 | \n", "6.666151 | \n", "6.392903 | \n", "2 | \n", "NaN | \n", "1.807613 | \n", "7.409797 | \n", "5.897036 | \n", "|
2 | \n", "0 | \n", "0.066755 | \n", "1 | \n", "-0.844060 | \n", "1 | \n", "2 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "1.033941 | \n", "0.833941 | \n", "4.874012 | \n", "4.252706 | \n", "2 | \n", "NaN | \n", "1.577970 | \n", "5.383379 | \n", "4.005693 | \n", "|
3 | \n", "0 | \n", "-0.748565 | \n", "1 | \n", "-1.348821 | \n", "1 | \n", "3 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.687783 | \n", "0.487783 | \n", "2.534901 | \n", "2.154861 | \n", "2 | \n", "NaN | \n", "1.325589 | \n", "3.272749 | \n", "2.049642 | \n", "|
4 | \n", "0 | \n", "-0.130574 | \n", "1 | \n", "-0.539693 | \n", "1 | \n", "4 | \n", "0 | \n", "hammock | \n", "NaN | \n", "0.95 | \n", "... | \n", "-0.2 | \n", "0.936799 | \n", "0.736799 | \n", "0.736799 | \n", "0.000000 | \n", "2 | \n", "NaN | \n", "1.730154 | \n", "1.730154 | \n", "0.000000 | \n", "
10 rows × 21 columns
\n", "