{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "
\n", " \n", " \"QuantEcon\"\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Job Search IV: Modeling Career Choice\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Job Search IV: Modeling Career Choice](#Job-Search-IV:-Modeling-Career-Choice) \n", " - [Overview](#Overview) \n", " - [Model](#Model) \n", " - [Exercises](#Exercises) \n", " - [Solutions](#Solutions) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "Next we study a computational problem concerning career and job choices\n", "\n", "The model is originally due to Derek Neal [[Nea99]](zreferences.ipynb#neal1999)\n", "\n", "This exposition draws on the presentation in [[LS18]](zreferences.ipynb#ljungqvist2012), section 6.5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model features\n", "\n", "- Career and job within career both chosen to maximize expected discounted wage flow \n", "- Infinite horizon dynamic programming with two state variables " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": true }, "outputs": [], "source": [ "using InstantiateFromURL\n", "github_project(\"QuantEcon/quantecon-notebooks-julia\", version = \"0.2.0\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "using LinearAlgebra, Statistics, Compat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model\n", "\n", "In what follows we distinguish between a career and a job, where\n", "\n", "- a *career* is understood to be a general field encompassing many possible jobs, and \n", "- a *job* is understood to be a position with a particular firm \n", "\n", "\n", "For workers, wages can be decomposed into the contribution of job and career\n", "\n", "- $ w_t = \\theta_t + \\epsilon_t $, where \n", " \n", " - $ \\theta_t $ is contribution of career at time $ t $ \n", " - $ \\epsilon_t $ is contribution of job at time $ t $ \n", " \n", "\n", "\n", "At the start of time $ t $, a worker has the following options\n", "\n", "- retain a current (career, job) pair $ (\\theta_t, \\epsilon_t) $\n", " — referred to hereafter as “stay put” \n", "- retain a current career $ \\theta_t $ but redraw a job $ \\epsilon_t $\n", " — referred to hereafter as “new job” \n", "- redraw both a career $ \\theta_t $ and a job $ \\epsilon_t $\n", " — referred to hereafter as “new life” \n", "\n", "\n", "Draws of $ \\theta $ and $ \\epsilon $ are independent of each other and\n", "past values, with\n", "\n", "- $ \\theta_t \\sim F $ \n", "- $ \\epsilon_t \\sim G $ \n", "\n", "\n", "Notice that the worker does not have the option to retain a job but redraw\n", "a career — starting a new career always requires starting a new job\n", "\n", "A young worker aims to maximize the expected sum of discounted wages\n", "\n", "\n", "\n", "$$\n", "\\mathbb{E} \\sum_{t=0}^{\\infty} \\beta^t w_t \\tag{1}\n", "$$\n", "\n", "subject to the choice restrictions specified above\n", "\n", "Let $ V(\\theta, \\epsilon) $ denote the value function, which is the\n", "maximum of [(1)](#equation-exw) over all feasible (career, job) policies, given the\n", "initial state $ (\\theta, \\epsilon) $\n", "\n", "The value function obeys\n", "\n", "$$\n", "V(\\theta, \\epsilon) = \\max\\{I, II, III\\},\n", "$$\n", "\n", "where\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", "& I = \\theta + \\epsilon + \\beta V(\\theta, \\epsilon) \\\\\n", "& II = \\theta + \\int \\epsilon' G(d \\epsilon') + \\beta \\int V(\\theta, \\epsilon') G(d \\epsilon') \\nonumber \\\\\n", "& III = \\int \\theta' F(d \\theta') + \\int \\epsilon' G(d \\epsilon') + \\beta \\int \\int V(\\theta', \\epsilon') G(d \\epsilon') F(d \\theta') \\nonumber\n", "\\end{aligned} \\tag{2}\n", "$$\n", "\n", "Evidently $ I $, $ II $ and $ III $ correspond to “stay put”, “new job” and “new life”, respectively" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Parameterization\n", "\n", "As in [[LS18]](zreferences.ipynb#ljungqvist2012), section 6.5, we will focus on a discrete version of the model, parameterized as follows:\n", "\n", "- both $ \\theta $ and $ \\epsilon $ take values in the set `linspace(0, B, N)` — an even grid of $ N $ points between $ 0 $ and $ B $ inclusive \n", "- $ N = 50 $ \n", "- $ B = 5 $ \n", "- $ \\beta = 0.95 $ \n", "\n", "\n", "The distributions $ F $ and $ G $ are discrete distributions\n", "generating draws from the grid points `linspace(0, B, N)`\n", "\n", "A very useful family of discrete distributions is the Beta-binomial family,\n", "with probability mass function\n", "\n", "$$\n", "p(k \\,|\\, n, a, b)\n", "= {n \\choose k} \\frac{B(k + a, n - k + b)}{B(a, b)},\n", "\\qquad k = 0, \\ldots, n\n", "$$\n", "\n", "Interpretation:\n", "\n", "- draw $ q $ from a β distribution with shape parameters $ (a, b) $ \n", "- run $ n $ independent binary trials, each with success probability $ q $ \n", "- $ p(k \\,|\\, n, a, b) $ is the probability of $ k $ successes in these $ n $ trials \n", "\n", "\n", "Nice properties:\n", "\n", "- very flexible class of distributions, including uniform, symmetric unimodal, etc. \n", "- only three parameters \n", "\n", "\n", "Here’s a figure showing the effect of different shape parameters when $ n=50 $\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "using Plots, QuantEcon, Distributions\n", "gr(fmt=:png);\n", "\n", "n = 50\n", "a_vals = [0.5, 1, 100]\n", "b_vals = [0.5, 1, 100]\n", "\n", "plt = plot()\n", "for (a, b) in zip(a_vals, b_vals)\n", " ab_label = \"a = $a, b = $b\"\n", " dist = BetaBinomial(n, a, b)\n", " plot!(plt, 0:n, pdf.(dist, support(dist)), label = ab_label)\n", "end\n", "plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Implementation:\n", "\n", "The code for solving the DP problem described above is found below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "function CareerWorkerProblem(;β = 0.95,\n", " B = 5.0,\n", " N = 50,\n", " F_a = 1.0,\n", " F_b = 1.0,\n", " G_a = 1.0,\n", " G_b = 1.0)\n", " θ = range(0, B, length = N)\n", " ϵ = copy(θ)\n", " dist_F = BetaBinomial(N-1, F_a, F_b)\n", " dist_G = BetaBinomial(N-1, G_a, G_b)\n", " F_probs = pdf.(dist_F, support(dist_F))\n", " G_probs = pdf.(dist_G, support(dist_G))\n", " F_mean = sum(θ .* F_probs)\n", " G_mean = sum(ϵ .* G_probs)\n", " return (β = β, N = N, B = B, θ = θ, ϵ = ϵ,\n", " F_probs = F_probs, G_probs = G_probs,\n", " F_mean = F_mean, G_mean = G_mean)\n", "end\n", "\n", "function update_bellman!(cp, v, out; ret_policy = false)\n", "\n", " # new life. This is a function of the distribution parameters and is\n", " # always constant. No need to recompute it in the loop\n", " v3 = (cp.G_mean + cp.F_mean .+ cp.β .*\n", " cp.F_probs' * v * cp.G_probs)[1] # do not need 1 element array\n", "\n", " for j in 1:cp.N\n", " for i in 1:cp.N\n", " # stay put\n", " v1 = cp.θ[i] + cp.ϵ[j] + cp.β * v[i, j]\n", "\n", " # new job\n", " v2 = (cp.θ[i] .+ cp.G_mean .+ cp.β .*\n", " v[i, :]' * cp.G_probs)[1] # do not need a single element array\n", "\n", " if ret_policy\n", " if v1 > max(v2, v3)\n", " action = 1\n", " elseif v2 > max(v1, v3)\n", " action = 2\n", " else\n", " action = 3\n", " end\n", " out[i, j] = action\n", " else\n", " out[i, j] = max(v1, v2, v3)\n", " end\n", " end\n", " end\n", "end\n", "\n", "\n", "function update_bellman(cp, v; ret_policy = false)\n", " out = similar(v)\n", " update_bellman!(cp, v, out, ret_policy = ret_policy)\n", " return out\n", "end\n", "\n", "function get_greedy!(cp, v, out)\n", " update_bellman!(cp, v, out, ret_policy = true)\n", "end\n", "\n", "function get_greedy(cp, v)\n", " update_bellman(cp, v, ret_policy = true)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code defines\n", "\n", "- a named tuple `CareerWorkerProblem` that \n", " \n", " - encapsulates all the details of a particular parameterization \n", " - implements the Bellman operator $ T $ \n", " \n", "\n", "\n", "In this model, $ T $ is defined by $ Tv(\\theta, \\epsilon) = \\max\\{I, II, III\\} $, where\n", "$ I $, $ II $ and $ III $ are as given in [(2)](#equation-eyes), replacing $ V $ with $ v $\n", "\n", "The default probability distributions in `CareerWorkerProblem` correspond to discrete uniform distributions (see [the Beta-binomial figure](#beta-binom))\n", "\n", "In fact all our default settings correspond to the version studied in [[LS18]](zreferences.ipynb#ljungqvist2012), section 6.5.\n", "\n", "Hence we can reproduce figures 6.5.1 and 6.5.2 shown there, which exhibit the\n", "value function and optimal policy respectively\n", "\n", "Here’s the value function" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "wp = CareerWorkerProblem()\n", "v_init = fill(100.0, wp.N, wp.N)\n", "func(x) = update_bellman(wp, x)\n", "v = compute_fixed_point(func, v_init, max_iter = 500, verbose = false)\n", "\n", "plot(linetype = :surface, wp.θ, wp.ϵ, transpose(v), xlabel=\"theta\", ylabel=\"epsilon\",\n", " seriescolor=:plasma, gridalpha = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The optimal policy can be represented as follows (see [Exercise 3](#career-ex3) for code)\n", "\n", "\n", "\n", "![_static/figures/career_solutions_ex3_jl.png](_static/figures/career_solutions_ex3_jl.png) \n", "Interpretation:\n", "\n", "- If both job and career are poor or mediocre, the worker will experiment with new job and new career \n", "- If career is sufficiently good, the worker will hold it and experiment with new jobs until a sufficiently good one is found \n", "- If both job and career are good, the worker will stay put \n", "\n", "\n", "Notice that the worker will always hold on to a sufficiently good career, but not necessarily hold on to even the best paying job\n", "\n", "The reason is that high lifetime wages require both variables to be large, and\n", "the worker cannot change careers without changing jobs\n", "\n", "- Sometimes a good job must be sacrificed in order to change to a better career " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1\n", "\n", "Using the default parameterization in the `CareerWorkerProblem`,\n", "generate and plot typical sample paths for $ \\theta $ and $ \\epsilon $\n", "when the worker follows the optimal policy\n", "\n", "In particular, modulo randomness, reproduce the following figure (where the horizontal axis represents time)\n", "\n", "![_static/figures/career_solutions_ex1_jl.png](_static/figures/career_solutions_ex1_jl.png) \n", "Hint: To generate the draws from the distributions $ F $ and $ G $, use the type [DiscreteRV](https://github.com/QuantEcon/QuantEcon.jl/blob/master/src/discrete_rv.jl)\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2\n", "\n", "Let’s now consider how long it takes for the worker to settle down to a\n", "permanent job, given a starting point of $ (\\theta, \\epsilon) = (0, 0) $\n", "\n", "In other words, we want to study the distribution of the random variable\n", "\n", "$$\n", "T^* := \\text{the first point in time from which the worker's job no longer changes}\n", "$$\n", "\n", "Evidently, the worker’s job becomes permanent if and only if $ (\\theta_t, \\epsilon_t) $ enters the\n", "“stay put” region of $ (\\theta, \\epsilon) $ space\n", "\n", "Letting $ S $ denote this region, $ T^* $ can be expressed as the\n", "first passage time to $ S $ under the optimal policy:\n", "\n", "$$\n", "T^* := \\inf\\{t \\geq 0 \\,|\\, (\\theta_t, \\epsilon_t) \\in S\\}\n", "$$\n", "\n", "Collect 25,000 draws of this random variable and compute the median (which should be about 7)\n", "\n", "Repeat the exercise with $ \\beta=0.99 $ and interpret the change\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3\n", "\n", "As best you can, reproduce [the figure showing the optimal policy](#career-opt-pol)\n", "\n", "Hint: The `get_greedy()` method returns a representation of the optimal\n", "policy where values 1, 2 and 3 correspond to “stay put”, “new job” and “new life” respectively. Use this and the plots functions (e.g., `contour, contour!`) to produce the different shadings.\n", "\n", "Now set `G_a = G_b = 100` and generate a new figure with these parameters. Interpret." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "wp = CareerWorkerProblem()\n", "\n", "function solve_wp(wp)\n", " v_init = fill(100.0, wp.N, wp.N)\n", " func(x) = update_bellman(wp, x)\n", " v = compute_fixed_point(func, v_init, max_iter = 500, verbose = false)\n", " optimal_policy = get_greedy(wp, v)\n", " return v, optimal_policy\n", "end\n", "\n", "v, optimal_policy = solve_wp(wp)\n", "\n", "F = DiscreteRV(wp.F_probs)\n", "G = DiscreteRV(wp.G_probs)\n", "\n", "function gen_path(T = 20)\n", " i = j = 1\n", " θ_ind = Int[]\n", " ϵ_ind = Int[]\n", "\n", " for t=1:T\n", " # do nothing if stay put\n", " if optimal_policy[i, j] == 2 # new job\n", " j = rand(G)[1]\n", " elseif optimal_policy[i, j] == 3 # new life\n", " i, j = rand(F)[1], rand(G)[1]\n", " end\n", " push!(θ_ind, i)\n", " push!(ϵ_ind, j)\n", " end\n", " return wp.θ[θ_ind], wp.ϵ[ϵ_ind]\n", "end\n", "\n", "plot_array = Any[]\n", "for i in 1:2\n", " θ_path, ϵ_path = gen_path()\n", " plt = plot(ϵ_path, label=\"epsilon\")\n", " plot!(plt, θ_path, label=\"theta\")\n", " plot!(plt, legend=:bottomright)\n", " push!(plot_array, plt)\n", "end\n", "plot(plot_array..., layout = (2,1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2\n", "\n", "The median for the original parameterization can be computed as follows" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "function gen_first_passage_time(optimal_policy)\n", " t = 0\n", " i = j = 1\n", " while true\n", " if optimal_policy[i, j] == 1 # Stay put\n", " return t\n", " elseif optimal_policy[i, j] == 2 # New job\n", " j = rand(G)[1]\n", " else # New life\n", " i, j = rand(F)[1], rand(G)[1]\n", " end\n", " t += 1\n", " end\n", "end\n", "\n", "\n", "M = 25000\n", "samples = zeros(M)\n", "for i in 1:M\n", " samples[i] = gen_first_passage_time(optimal_policy)\n", "end\n", "print(median(samples))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To compute the median with $ \\beta=0.99 $ instead of the default value $ \\beta=0.95 $, replace `wp=CareerWorkerProblem()` with `wp=CareerWorkerProblem(β=0.99)`\n", "\n", "The medians are subject to randomness, but should be about 7 and 14 respectively. Not surprisingly, more patient workers will wait longer to settle down to their final job" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "wp2 = CareerWorkerProblem(β=0.99)\n", "\n", "v2, optimal_policy2 = solve_wp(wp2)\n", "\n", "samples2 = zeros(M)\n", "for i in 1:M\n", " samples2[i] = gen_first_passage_time(optimal_policy2)\n", "end\n", "print(median(samples2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3\n", "\n", "Here’s the code to reproduce the original figure" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "wp = CareerWorkerProblem();\n", "v, optimal_policy = solve_wp(wp)\n", "\n", "lvls = [0.5, 1.5, 2.5, 3.5]\n", "x_grid = range(0, 5, length = 50)\n", "y_grid = range(0, 5, length = 50)\n", "\n", "contour(x_grid, y_grid, optimal_policy', fill=true, levels=lvls,color = :Blues,\n", " fillalpha=1, cbar = false)\n", "contour!(xlabel=\"theta\", ylabel=\"epsilon\")\n", "annotate!([(1.8,2.5, text(\"new life\", 14, :white, :center))])\n", "annotate!([(4.5,2.5, text(\"new job\", 14, :center))])\n", "annotate!([(4.0,4.5, text(\"stay put\", 14, :center))])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we need only swap out for the new parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "wp = CareerWorkerProblem(G_a=100.0, G_b=100.0); # use new params\n", "v, optimal_policy = solve_wp(wp)\n", "\n", "lvls = [0.5, 1.5, 2.5, 3.5]\n", "x_grid = range(0, 5, length = 50)\n", "y_grid = range(0, 5, length = 50)\n", "\n", "contour(x_grid, y_grid, optimal_policy', fill=true, levels=lvls,color = :Blues,\n", " fillalpha=1, cbar = false)\n", "contour!(xlabel=\"theta\", ylabel=\"epsilon\")\n", "annotate!([(1.8,2.5, text(\"new life\", 14, :white, :center))])\n", "annotate!([(4.5,2.5, text(\"new job\", 14, :center))])\n", "annotate!([(4.0,4.5, text(\"stay put\", 14, :center))])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will see that the region for which the worker\n", "will stay put has grown because the distribution for $ \\epsilon $\n", "has become more concentrated around the mean, making high-paying jobs\n", "less realistic" ] } ], "metadata": { "filename": "career.rst", "kernelspec": { "display_name": "Julia 1.2", "language": "julia", "name": "julia-1.2" }, "title": "Job Search IV: Modeling Career Choice" }, "nbformat": 4, "nbformat_minor": 2 }