├── .gitignore ├── LICENSE ├── README.md ├── homework ├── homework1.pdf ├── homework1.tex ├── homework2.pdf ├── homework2.tex ├── homework3.pdf ├── homework3.tex ├── homework4.pdf ├── homework4.tex ├── homework5.pdf └── homework5.tex ├── homework_coding ├── homework_coding_week1.pdf ├── homework_coding_week3.pdf ├── homework_coding_week4.pdf ├── homework_coding_week5.pdf ├── honework_coding_week2.pdf └── solutions │ ├── homework_coding_week1_sol.pdf │ ├── homework_coding_week2_sol.pdf │ ├── homework_coding_week3_sol.pdf │ └── homework_coding_week4_sol.pdf ├── homework_solutions ├── Homework_Solutions_1.pdf ├── Homework_Solutions_2.pdf ├── Homework_Solutions_3.pdf ├── Homework_Solutions_4.pdf └── Homework_Solutions_5.pdf ├── lectures ├── Brownian1.png ├── Brownian2.png ├── Brownian3.png ├── Brownian4.png ├── Brownian5.png ├── Brownian6.png ├── Brownian7.png ├── figures │ ├── HACT_MPC.png │ ├── HACT_accuracy_consumption.png │ ├── HACT_accuracy_error1000.png │ ├── HACT_accuracy_error30.png │ ├── HACT_consumption.png │ ├── HACT_diffusion_distribution.png │ ├── HACT_diffusion_savings.png │ ├── HACT_distribution.png │ ├── HACT_distribution2.png │ ├── HACT_example1.png │ ├── HACT_example2.png │ ├── HACT_existence.png │ ├── HACT_savings.png │ ├── HACT_savings2.png │ ├── HACT_spyA.png │ ├── HACT_transition.png │ ├── candlerpic.eps │ ├── density_initial.eps │ ├── density_steadystate.eps │ ├── ikc1.png │ ├── ikc2.png │ ├── inequality_consumption_0.png │ ├── inequality_consumption_1.png │ ├── inequality_consumption_2.png │ ├── inequality_consumption_3.png │ ├── inequality_earnings_1.png │ ├── inequality_earnings_2.png │ ├── inequality_earnings_3.png │ ├── inequality_earnings_4.png │ ├── inequality_earnings_5.png │ ├── inequality_earnings_6.png │ ├── inequality_earnings_7.png │ ├── inequality_government_1.png │ ├── inequality_government_2.png │ ├── inequality_government_3.png │ ├── inequality_government_4.png │ ├── inequality_household_1.png │ ├── inequality_household_2.png │ ├── inequality_household_3.png │ ├── inequality_polarization_1.png │ ├── inequality_polarization_2.png │ ├── inequality_polarization_3.png │ ├── inequality_wage_1.png │ ├── inequality_wage_10.png │ ├── inequality_wage_11.png │ ├── inequality_wage_12.png │ ├── inequality_wage_2.png │ ├── inequality_wage_3.png │ ├── inequality_wage_4.png │ ├── inequality_wage_5.png │ ├── inequality_wealth_1.png │ ├── inequality_wealth_2.png │ ├── inequality_wealth_3.png │ ├── liquidity_constraint.png │ ├── precautionary.png │ ├── skiba.png │ ├── skiba1978.png │ ├── skiba_savings.png │ ├── skiba_spyA.png │ ├── skiba_value.png │ └── spyA.png ├── lecture1.pdf ├── lecture1.tex ├── lecture10.pdf ├── lecture10.tex ├── lecture11.pdf ├── lecture11.tex ├── lecture2.pdf ├── lecture2.tex ├── lecture3.pdf ├── lecture3.tex ├── lecture5.pdf ├── lecture5.tex ├── lecture7.pdf ├── lecture7.tex ├── lecture9.pdf └── lecture9.tex ├── sections ├── .gitkeep ├── Section 1.pdf ├── Section 2.pdf ├── Section 3.pdf ├── Section_4.pdf ├── Section_5.pdf ├── Syllabus.pdf └── codes │ ├── .gitkeep │ ├── week1 │ ├── .gitkeep │ ├── capital_accumulation.m │ └── define_parameters.m │ ├── week2 │ ├── ramsey │ │ ├── define_parameters.m │ │ ├── forward_simulate.m │ │ ├── ramsey.m │ │ └── terminal_condition.m │ └── ramsey_HJB │ │ ├── HJB_ramsey_implicit_demo.m │ │ └── define_parameters.m │ ├── week3 │ ├── HJB_ramsey_implicit_mixed.m │ ├── HJB_ramsey_implicit_upwind.m │ └── define_parameters.m │ ├── week4 │ ├── Asset Supply │ │ ├── Huggett_assetsupply.m │ │ └── define_parameters_assetsupply.m │ ├── GE │ │ ├── Huggett_GE.m │ │ └── define_parameters_GE.m │ ├── HJB │ │ ├── HJB_Huggett.m │ │ └── define_parameters.m │ └── KF │ │ ├── Huggett_PE.m │ │ └── define_parameters.m │ └── week5 │ ├── Andreas's Repository │ ├── GE │ │ ├── HJB.m │ │ ├── KF.m │ │ ├── define_parameters.m │ │ ├── main_GE.m │ │ └── stationary.m │ └── PE │ │ ├── define_parameters.m │ │ └── main_PE.m │ └── Transition Dynamics │ ├── Huggett_transition.m │ ├── Huggett_transition_demo.m │ ├── define_parameters_initial.m │ ├── define_parameters_terminal.m │ └── huggett_GE.m └── syllabus └── syllabus2024.docx /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # MATLAB 4 | *.asv 5 | *.mat 6 | 7 | ## Core latex/pdflatex auxiliary files: 8 | *.aux 9 | *.lof 10 | *.log 11 | *.lot 12 | *.fls 13 | *.out 14 | *.toc 15 | *.fmt 16 | *.fot 17 | *.cb 18 | *.cb2 19 | .*.lb 20 | *.swp 21 | 22 | ## Intermediate documents: 23 | *.dvi 24 | *.xdv 25 | *-converted-to.* 26 | # *.ps 27 | # *.eps 28 | # *.pdf 29 | 30 | ## Generated if empty string is given at "Please type another file name for output:" 31 | .pdf 32 | 33 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 34 | *.bbl 35 | *.bcf 36 | *.blg 37 | *-blx.aux 38 | *-blx.bib 39 | *.run.xml 40 | 41 | ## Build tool auxiliary files: 42 | *.fdb_latexmk 43 | *.synctex 44 | *.synctex(busy) 45 | *.synctex.gz 46 | *.synctex.gz(busy) 47 | *.pdfsync 48 | 49 | ## Build tool directories for auxiliary files 50 | # latexrun 51 | latex.out/ 52 | 53 | ## Auxiliary and intermediate files from other packages: 54 | # algorithms 55 | *.alg 56 | *.loa 57 | 58 | # achemso 59 | acs-*.bib 60 | 61 | # amsthm 62 | *.thm 63 | 64 | # beamer 65 | *.nav 66 | *.pre 67 | *.snm 68 | *.vrb 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 schaab-teaching 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DynamicProgramming2024 -------------------------------------------------------------------------------- /homework/homework1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework/homework1.pdf -------------------------------------------------------------------------------- /homework/homework1.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{extarticle} 2 | \usepackage{fullpage,amsmath,amsfonts,microtype,nicefrac,amssymb, amsthm} 3 | \usepackage[left=1in, bottom=1in, top=1in, right = 1in]{geometry} 4 | \usepackage{textcomp} 5 | \usepackage{mathpazo} 6 | \usepackage{mathrsfs} 7 | \usepackage[T1]{fontenc} 8 | \usepackage[utf8]{inputenc} 9 | \usepackage[english]{babel} 10 | \usepackage{graphicx} 11 | 12 | \usepackage{microtype} 13 | 14 | \usepackage{bm} 15 | \usepackage{dsfont} 16 | \usepackage{enumerate} 17 | \usepackage{ragged2e} 18 | 19 | \setlength{\parindent}{24pt} 20 | \setlength{\jot}{8pt} 21 | 22 | 23 | \usepackage[shortlabels]{enumitem} 24 | 25 | 26 | %% FOOTNOTES 27 | \usepackage[bottom]{footmisc} 28 | \usepackage{footnotebackref} 29 | 30 | 31 | %% FIGURE ENVIRONMENT 32 | %\graphicspath{{}} 33 | \usepackage[margin=15pt, font=small, labelfont={bf}, labelsep=period]{caption} 34 | \usepackage{subcaption} 35 | \captionsetup[figure]{name={Figure}, position=above} 36 | \usepackage{float} 37 | \usepackage{epstopdf} 38 | 39 | 40 | %% NEW COMMANDS 41 | \renewcommand{\baselinestretch}{1.25} 42 | \renewcommand{\qedsymbol}{$\blacksquare$} 43 | \newcommand{\R}{\mathbb{R}} 44 | \newcommand{\indep}{\mathrel{\text{\scalebox{1.07}{$\perp\mkern-10mu\perp$}}}} 45 | \renewcommand{\b}{\begin} 46 | \newcommand{\e}{\end} 47 | 48 | %% NEWTHEOREM 49 | \theoremstyle{plain} 50 | \newtheorem{thm}{Theorem} 51 | \newtheorem{lem}[thm]{Lemma} 52 | \newtheorem{prop}[thm]{Proposition} 53 | 54 | \theoremstyle{definition} 55 | \newtheorem{defn}[thm]{Definition} 56 | \newtheorem{ex}[thm]{Example} 57 | \newtheorem{remark}[thm]{Remark} 58 | \newtheorem{cor}[thm]{Corollary} 59 | 60 | %% LINKS and COLORS 61 | \usepackage[dvipsnames]{xcolor} 62 | \usepackage{hyperref} 63 | \definecolor{myred}{RGB}{163, 32, 45} 64 | \hypersetup{ 65 | %backref=true, 66 | %pagebackref=true, 67 | colorlinks=true, 68 | urlcolor=myred, 69 | citecolor=myred, 70 | linktoc=all, 71 | linkcolor=myred, 72 | } 73 | 74 | %% TABLE OF CONTENTS 75 | \addto\captionsenglish{ 76 | \renewcommand{\contentsname} 77 | {}% This removes the heading over the table of contents. 78 | } 79 | 80 | 81 | 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | %%%%%%%%%%%%%%%%%%%%%% END PREAMBLE %%%%%%%%%%%%%%%%%%%%%%%% 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 87 | 88 | \title{202A: Dynamic Programming and Applications\\[5pt] {\Large \textbf{Homework \#1}}} 89 | 90 | \author{Andreas Schaab\footnote{ 91 | UC Berkeley. Email: schaab@berkeley.edu.\\ 92 | This course builds on the excellent teaching material developed, in particular, by David Laibson and Benjamin Moll. I would also like to thank Gabriel Chodorow-Reich, Pablo Kurlat, J\'on Steinsson, and Gianluca Violante, as well as QuantEcon, on whose teaching material I have drawn. 93 | }} 94 | 95 | \date{} 96 | 97 | 98 | \begin{document} 99 | 100 | \maketitle 101 | 102 | 103 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 104 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 105 | \section*{Problem 1: Discrete Time Markov Chains} 106 | 107 | Time is discrete, with $t \in \{0, 1, ...\}$. Consider the stochastic process $\{ y_t \}$ which follows a Markov chain. Let $y_t$ denote the employment / earnings state of an individual in periods $t$. Consider the state space $y_t \in Y = \{ y^U, y^E \}$, where $y^U$ corresponds to unemployment and $y^E$ corresponds to employment. Let $\bm y$ denote the column vector $(y^U, y^E)'$ representing this state space (this is the grid you would construct on a computer). Suppose the employment dynamics of the individual are characterized by the invariant transition matrix 108 | \begin{equation*} 109 | P = \begin{pmatrix} \alpha & 1 - \alpha \\ 1 - \beta & \beta \end{pmatrix}. 110 | \end{equation*} 111 | We interpret a time period as a quarter. 112 | 113 | \vspace{5mm} 114 | \noindent 115 | \begin{enumerate}[(a)] 116 | \item Give economic interpretations of $\alpha = P_{11}$ and $\beta = P_{22}$ 117 | 118 | \item Why do the rows of $P$ sum to $1$? 119 | 120 | \item Is there an absorbing state in this model? 121 | 122 | \item Compute the probability of being unemployed two quarters after being employed. 123 | 124 | \item Denote the \textit{marginal (probability) distribution} of $y_t$ at time $t$ by $\psi_t$. $\psi_t(y^L)$ is the probability that process $y_t$ is in state $y^L$ at time $t$. It is easiest to think of $\psi_t$ as a time-varying row vector. Use the law of total probability to decompose $y_{t+1} = y^L$, accounting for all the possible ways in which state $y^L$ can be reached at time $t+1$. 125 | 126 | \item Show that the resulting equation can be written as the vector-matrix product 127 | \begin{equation*} 128 | \psi_{t+1} = \psi_t P. 129 | \end{equation*} 130 | Therefore: The evolution of the marginal distribution of a Markov chain is obtained by post-multiplying by the transition matrix. 131 | 132 | \item Show that 133 | \begin{equation*} 134 | X_0 \sim \psi_0 \implies X_t \sim \psi_0 P^t, 135 | \end{equation*} 136 | where $\sim$ reads ``is distributed according to''. 137 | 138 | \item We call $\psi^*$ a \textit{stationary distribution} of the Markov chain if it satisfies 139 | \begin{equation*} 140 | \psi^* = \psi^* P. 141 | \end{equation*} 142 | Compute the probability of being unemployed $n$ quarters after being employed. Take $n \to \infty$ and find the stationary distribution of this Markov chain. Find the stationary distribution by alternatively plugging into the above equation for $\psi^*$. 143 | 144 | \item Suppose $y_0 = y^H$. Solve for $\mathbb E_0 (y_t)$. Use the law of total / iterated expectation to relate expectation to probabilities. Then use the formulas for marginal (probability) distributions derived above. 145 | \end{enumerate} 146 | 147 | 148 | 149 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 150 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 151 | \vspace{5mm} 152 | \section*{Problem 2: Proof of the Contraction Mapping Theorem} 153 | 154 | In class, we defined the Bellman operator $B$, which operates on functions $w$, and is defined by 155 | \begin{equation*} 156 | (Bw)(x) \equiv \max_{x' \in \Gamma(x)} \bigg\{ F(x, x') + \beta w(x') \bigg\} 157 | \end{equation*} 158 | for all $x \in \mathcal X$ in the state space, where $\Gamma(x)$ is some constraint set---in our case, this was the budget constraint. The definition is expressed pointwise, but it applies to all possible values in the state space. We call $B$ an operator because it maps a function $w$ to a new function $Bw$. So both $w$ and $Bw$ map $\mathcal X$ into $\mathbb R$. Operator $B$ maps \textit{functions} and is therefore called a functional operator. In class, we showed that the solution of the Bellman equation---the value function---is a fixed point of the Bellman operator. 159 | 160 | What does it mean to \textit{iterate} $B^n w$? 161 | \begin{align*} 162 | (Bw) (x) &= \max_{x' \in \Gamma(x)} \bigg\{ F(x, x') + \beta w(x') \bigg\} \\ 163 | (B(Bw)) (x) &= \max_{x' \in \Gamma(x)} \bigg\{ F(x, x') + \beta (Bw)(x') \bigg\} \\ 164 | (B(B^2w)) (x) &= \max_{x' \in \Gamma(x)} \bigg\{ F(x, x') + \beta (B^2w)(x') \bigg\} \\ 165 | \vdots & \vdots \\ 166 | (B(B^nw)) (x) &= \max_{x' \in \Gamma(x)} \bigg\{ F(x, x') + \beta (B^nw)(x') \bigg\}. 167 | \end{align*} 168 | What does it mean for functions to converge to a limiting function? Let $v_0$ be some guess for the value function, then convergence would mean 169 | \begin{equation*} 170 | \lim_{n \to \infty} B^n v_0 = v. 171 | \end{equation*} 172 | And why might $B^n w$ converge as $n \to \infty$? The answer is that $B$ is a \textit{contraction mapping}. 173 | 174 | 175 | \vspace{5mm} 176 | \begin{defn} 177 | 178 | Let $(S, d)$ be a metric space and $B: S \to S$ be a function that maps $S$ into intself. $B$ is a contraction mapping if for some $\beta \in (0, 1)$, $d(Bf, Bg) \leq \beta d(f, g)$, for any two functions $f$ and $g$.\footnote{ 179 | A metric $d$ is a way of representing the distance between two functions, or two members of (metric) space $S$. One example: the supremum pointwise gap. 180 | } 181 | 182 | \end{defn} 183 | 184 | 185 | \vspace{5mm} 186 | \noindent 187 | Intuitively, $B$ is a contraction mapping if applying the operator $B$ to any two functions $f$ and $g$ (that are not the same) moves them strictly closer together. $Bf$ and $Bg$ are strictly closer together than $f$ and $g$. We can now state the contraction mapping theorem. 188 | 189 | 190 | \vspace{5mm} 191 | \begin{thm} 192 | 193 | If $(S, d)$ is a complete metric space and $B: S \to S$ is a contraction mapping, then: 194 | \begin{enumerate}[(i)] 195 | \item $B$ has exactly one fixed point $v \in S$ 196 | \item For any $v_0 \in S$, $\lim_{n \to \infty} B^n v_0 = v$ 197 | \item $B^n v_0$ has an exponential convergence rate at least as great as $- \ln(\beta)$ 198 | \end{enumerate} 199 | 200 | \end{thm} 201 | 202 | 203 | \vspace{5mm} 204 | \noindent 205 | In this problem, we will illustrate and prove the contraction mapping theorem. 206 | 207 | \begin{enumerate}[(a)] 208 | \item Consider the contraction mapping $(Bw)(x) \equiv h(x) + \alpha w(x)$ with $\alpha \in (0, 1)$. Iteratively apply the operator $B$ and show that 209 | \begin{equation*} 210 | \lim_{n \to \infty} (B^n f)(x) = \frac{h(x)}{1-\alpha} 211 | \end{equation*} 212 | Argue that this shows that the fixed point of this operator $B$ is consequently the function $v(x) = \frac{1}{1-\alpha} h(x)$. Show that $(Bv)(x) = v(x)$. 213 | 214 | \item \textbf{Optional:} Now we will prove the contraction mapping theorem in 3 steps (we will not prove the convergence rate). Show that $\{ B^n f_0\}_{n=0}^\infty$ is a Cauchy sequence. (Cauchy sequence definition: Fix any $\epsilon > 0$. Then there exists $N$ such that $d(B^m f_0, B^n f_0) \leq \epsilon$ for all $m, n \leq N$.) 215 | 216 | \item \textbf{Optional:} Show that the limit point $v$ is a fixed point of $B$. 217 | 218 | \item \textbf{Optional:} Show that only one fixed point exists. 219 | 220 | \end{enumerate} 221 | 222 | 223 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 224 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 225 | \vspace{5mm} 226 | \section*{Problem 3: Blackwell's Sufficiency Conditions} 227 | 228 | We now show that there are in fact sufficient conditions for an operator to be contraction mapping. 229 | 230 | \begin{thm}[Blackwell's Sufficient Conditions] 231 | 232 | Let $X \subset \mathbb R^l$ and let $C(X)$ be a space of bounded functions $f:X \to \mathbb R$, with the sup-metric. Let $B : C(X) \to C(X)$ be an operator satisfying two conditions: 233 | \begin{enumerate}[1.] 234 | \item monotonicity: if $f,g\in C(X)$ and $f(x)\leq g(x)$ $\forall x\in X$,% 235 | \newline 236 | then $(Bf)(x)\leq (Bg)(x),$ $\forall x\in X$\newline 237 | 238 | \item discounting: there exists some $\delta \in (0,1)$ such that 239 | \[ 240 | \lbrack B(f+a)](x)\leq (Bf)(x)+\delta a\text{ \ }\forall \text{ }f\in C(X),\ 241 | a\geq 0,\ x\in X. 242 | \]% 243 | \end{enumerate} 244 | 245 | \noindent 246 | Then, $B$ is a contraction with modulus $\delta$. 247 | 248 | \end{thm} 249 | 250 | \noindent 251 | Note that $a$ is a constant and $(f+a)$ is the function generated by adding $a$ to the function $f$. Blackwell's conditions are sufficient but not necessary for $B$ to be a contraction. 252 | 253 | \vspace{5mm} 254 | \noindent 255 | In this problem, we will prove these sufficient conditions. 256 | \begin{enumerate}[(a)] 257 | \item Let $d$ be the sup-metric and show that, for any $f, g \in C(X)$, we have $f(x) \leq g(x) + d(f, g)$ for all $x$ 258 | 259 | \item Use monotonicity and discounting to show that, for any $f, g \in C(X)$, we have $(Bf)(x) \leq (Bg)(x) + \delta d(f, g)$ and $(Bg)(x) \leq (Bf)(x) + \delta d(f, g)$. 260 | 261 | \item Combine these to show that $d(Bf, Bg) \leq \delta d(f, g)$. 262 | \end{enumerate} 263 | 264 | 265 | 266 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 267 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 268 | \vspace{5mm} 269 | \section*{Problem 4: Example of Blackwell's Conditions} 270 | 271 | We will now work out a simple example to illustrate these sufficient conditions. In particular, consider the Bellman operator in a consumption problem with stochastic asset returns, stochastic labor income, and a liquidity constraint: 272 | \begin{equation*} 273 | (Bf)(x)=\sup_{c\in \lbrack 0,x]}\left\{ u(c)+\delta \mathbb Ef(\tilde{R}_{+1}(x-c)+ 274 | \tilde{y}_{+1})\right\} \text{ \ }\forall x\text{\ } 275 | \end{equation*} 276 | Notionally, $\tilde R$ and $\tilde y$ just underscore that these are random variables. The $_{+1}$ subscript underscores that these random variables are realized next period (in class, we used $'$ for this). The liquidity constraint is encoded in $c \in [0, x]$. (Why?) 277 | 278 | 279 | \vspace{5mm} 280 | \noindent 281 | \begin{enumerate}[(a)] 282 | \item Interpret each term in the definition of this Bellman operator. 283 | 284 | \item Explicitly write out the budget constraint that is used here. 285 | 286 | \item Check the first of Blackwell's conditions: monotonicity. 287 | 288 | \item Check the second of Blackwell's conditions: discounting. 289 | \end{enumerate} 290 | 291 | 292 | 293 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 294 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 295 | \vspace{5mm} 296 | \section*{Problem 5: Neoclassical Growth Model with Log Utility} 297 | 298 | Recall the neoclassical growth model we discussed in class. Assuming log utility, full depreciation, and a decreasing-returns production function, preferences can be written as 299 | \begin{equation*} 300 | V(k_0) = \max_{ \{ k_{t+1} \}_{t=0}^\infty} \sum_{t=0}^\infty \beta^t \log (k_t^\alpha - k_{t+1}) 301 | \end{equation*} 302 | where $0 < \alpha < 1$, subject to the constraint 303 | \begin{equation*} 304 | k_{t+1} \in [0, k_t^\alpha] \equiv \Gamma(k_t). 305 | \end{equation*} 306 | Think of $k_t^\alpha$ as the resources you have available, and so the most you would be allowed to save is $k_t^\alpha$. We represent this constraint by the \textit{feasibility set} $\Gamma(k_t)$. (This is the more general notation you will find in Stokey-Lucas, for example.) 307 | 308 | Consider the associated Bellman equation 309 | \begin{equation*} 310 | V(k) = \max_{k' \in \Gamma(k)} \bigg\{ \ln (k^\alpha - k') + \beta V(k') \bigg\}. 311 | \end{equation*} 312 | 313 | \begin{enumerate}[(a)] 314 | \item Try to solve the Bellman equation by \textit{guessing} a solution. Specifically, start by guessing that the form of the solution is 315 | \begin{equation*} 316 | V(k) = \psi + \phi \log k. 317 | \end{equation*} 318 | We will solve for the coefficients $\psi$ and $\phi$, and show that $V(k)$ solves the functional equation. Rewrite the functional equation substituting in $V(k) = \psi + \phi \log k$. Use the Envelope Theorem (ET) and the First Order Condition (FOC) to show 319 | \begin{equation*} 320 | \phi = \frac{\alpha}{1 - \alpha \beta}. 321 | \end{equation*} 322 | Now use the FOC to show 323 | \begin{equation*} 324 | k'(k) = \alpha \beta k^\alpha. 325 | \end{equation*} 326 | Finally, show that the functional equation is satisfied at all feasible values of $k_0$ if 327 | \begin{equation*} 328 | \psi =\frac{1}{1-\beta }\bigg[ \log (1-\alpha \beta)+\frac{\alpha \beta}{1-\alpha \beta}\log (\alpha \beta )\bigg] . 329 | \end{equation*} 330 | You have now solved the functional equation by using the guess and verify method. 331 | 332 | 333 | \item We have derived the policy function 334 | \begin{equation*} 335 | k'(k) = g(k) = \alpha \beta k^\alpha. 336 | \end{equation*} 337 | Derive the optimal sequence of state variables $\{k_t^*\}_{t=0}^\infty$ which would be generated by this policy function. Show that 338 | \begin{equation*} 339 | V(k_0) = \sum_{t=0}^\infty \beta^t \log \bigg( (k_t^*)^\alpha - k_{t+1}^* \bigg), 340 | \end{equation*} 341 | thereby confirming that this policy function is optimal. 342 | 343 | 344 | \item Consider the Bellman (functional) operator $B$ defined by 345 | \begin{equation*} 346 | (B f)(k) = \sup_{k' \in \Gamma(k)} \Big\{ \log (k^\alpha - k') + \beta f(k') \Big\}. 347 | \end{equation*} 348 | Let $\hat V(k) = \frac{\alpha \log k}{1 - \alpha \beta}$. Show that 349 | \begin{equation*} 350 | (B^n \hat V)(k) = \frac{1-\beta^n}{1-\beta} \bigg[\log (1-\alpha \beta) + \frac{\alpha \beta}{1 - \alpha \beta} \log (\alpha \beta) \bigg] + \frac{\alpha \log k}{1 - \alpha \beta}. 351 | \end{equation*} 352 | To prove this you'll need to show that $k' = \alpha \beta k^\alpha$, and substitute this expression into the functional operator. Let, 353 | \begin{equation*} 354 | \lim_{n \rightarrow \infty} (B^n \hat V)(k) = V(k) 355 | \end{equation*} 356 | Confirm that $V(k)$ is the same solution to the the functional equation that you derived in part (a). You have now solved the functional equation by iterating the operator $T$ on a starting guess. 357 | 358 | \end{enumerate} 359 | 360 | 361 | 362 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 363 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 364 | \vspace{10mm} 365 | \section*{Problem 6: A Model with Equity} 366 | 367 | \vspace{5mm} 368 | \noindent 369 | Assume that a consumer with only equity wealth must choose period by period consumption in a discrete-time dynamic optimization problem. Specifically, consider the sequence problem 370 | \begin{equation*} 371 | V(x_0) = \max_{ \{ c_t \}_{t=0}^\infty } \mathbb E_0 \sum_{t=0}^\infty e^{- \rho t} u(c_t) 372 | \end{equation*} 373 | subject to the constraints 374 | \begin{equation*} 375 | x_{t+1} = e^{r + \sigma u_t - \frac{\sigma^2}{2}} (x_t - c_t) 376 | \end{equation*} 377 | where $u_t$ is iid and $u_t \sim \mathcal N(0, 1)$. There is a feasibility constraint $c_t \in [0, x_t]$. And we assume an endowment $x_0 > 0$. Here, $x_t$ represents equity wealth at period $t$ and $c_t$ is consumption in period $t$. The consumer has discount rate $\rho$. The consumer can only invest in a risky asset with expected return $e^r = \mathbb E e^{r + \sigma u_t - \frac{\sigma^2}{2}}$. And we assume CRRA preferences with $u(c) = \frac{1}{1-\gamma} c^{1-\gamma}$, with $\gamma \in [0, \infty]$. We call this \textit{constant} relative risk aversion because the relative risk aversion coefficient 378 | \begin{equation*} 379 | - \frac{c u''(c)}{u'(c)} = \gamma 380 | \end{equation*} 381 | is constant. Notice that CRRA consumption preferences are \textit{homothetic}, and this is what allows us to analytically solve this problem. 382 | 383 | The associated Bellman equation is 384 | \begin{equation*} 385 | V(x) = \max_{x' \in [0, x]} \bigg\{ u(x - x') + \mathbb E e^{- \rho} V \bigg( e^{r + \sigma u - \frac{\sigma^2}{2}} x' \bigg) \bigg\}. 386 | \end{equation*} 387 | 388 | 389 | \begin{enumerate}[(a)] 390 | \item Explain all terms in this Bellman equation. Why is $u$ not a state variable, i.e., why don't we have $V(x, u)$? Make sure that this equation makes sense to you. 391 | 392 | 393 | \item Now guess that the value function takes the special form 394 | \begin{equation*} 395 | V(x) = \phi \frac{x^{1-\gamma}}{1-\gamma} . 396 | \end{equation*} 397 | Note the close similarity between this functional form and the functional form of the utility function. Assuming that the value function guess is correct, use the Envelope Theorem to derive the consumption function: 398 | \begin{equation*} 399 | c=\phi^{-\frac{1}{\gamma}} x . 400 | \end{equation*} 401 | Now verify that the Bellman Equation is satisfied for a particular value of $\phi$. Do not solve for $\phi$ (it's a very nasty expression). Instead, show that 402 | \begin{equation*} 403 | \log (1 - \phi^{-\frac{1}{\gamma}}) = \frac{1}{\gamma} \Big[ (1-\gamma) r - \rho \Big] + \frac{1}{2}(\gamma-1) \sigma^2. 404 | \end{equation*} 405 | 406 | 407 | \item Now consider the $\log$ of the ratio of $c_{t+1}$ and $c_t$. Show that 408 | \begin{equation*} 409 | \mathbb E \log \bigg(\frac{c_{t+1}}{c_t}\bigg) = \frac{1}{\gamma}(r-\rho) + \frac{\gamma}{2} \sigma^2 - \sigma^2. 410 | \end{equation*} 411 | 412 | 413 | \item Interpret the previous equation for the certainty case $\sigma = 0$. Note that $\log (\frac{c_{t+1}}{c_t}) = \log c_{t+1} - \log c_t$ is the growth rate of consumption. Explain why $\log c_{t+1} - \log c_t$ increases in $r$ and decreases in $\rho$. Why does the coefficient of relative risk aversion $\gamma$ appear in the denominator of the expression? Why does the coefficient of relative risk aversion regulate the consumer's willingness to substitute consumption between periods? 414 | 415 | \end{enumerate} 416 | 417 | \end{document} 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | -------------------------------------------------------------------------------- /homework/homework2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework/homework2.pdf -------------------------------------------------------------------------------- /homework/homework2.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{extarticle} 2 | \usepackage{fullpage,amsmath,amsfonts,microtype,nicefrac,amssymb, amsthm} 3 | \usepackage[left=1in, bottom=1in, top=1in, right = 1in]{geometry} 4 | \usepackage{textcomp} 5 | \usepackage{mathpazo} 6 | \usepackage{mathrsfs} 7 | \usepackage[T1]{fontenc} 8 | \usepackage[utf8]{inputenc} 9 | \usepackage[english]{babel} 10 | \usepackage{graphicx} 11 | 12 | \usepackage{microtype} 13 | 14 | \usepackage{bm} 15 | \usepackage{dsfont} 16 | \usepackage{enumerate} 17 | \usepackage{ragged2e} 18 | 19 | \setlength{\parindent}{24pt} 20 | \setlength{\jot}{8pt} 21 | 22 | 23 | \usepackage[shortlabels]{enumitem} 24 | 25 | 26 | %% FOOTNOTES 27 | \usepackage[bottom]{footmisc} 28 | \usepackage{footnotebackref} 29 | 30 | 31 | %% FIGURE ENVIRONMENT 32 | %\graphicspath{{}} 33 | \usepackage[margin=15pt, font=small, labelfont={bf}, labelsep=period]{caption} 34 | \usepackage{subcaption} 35 | \captionsetup[figure]{name={Figure}, position=above} 36 | \usepackage{float} 37 | \usepackage{epstopdf} 38 | 39 | 40 | %% NEW COMMANDS 41 | \renewcommand{\baselinestretch}{1.25} 42 | \renewcommand{\qedsymbol}{$\blacksquare$} 43 | \newcommand{\R}{\mathbb{R}} 44 | \newcommand{\indep}{\mathrel{\text{\scalebox{1.07}{$\perp\mkern-10mu\perp$}}}} 45 | \renewcommand{\b}{\begin} 46 | \newcommand{\e}{\end} 47 | 48 | %% NEWTHEOREM 49 | \theoremstyle{plain} 50 | \newtheorem{thm}{Theorem} 51 | \newtheorem{lem}[thm]{Lemma} 52 | \newtheorem{prop}[thm]{Proposition} 53 | 54 | \theoremstyle{definition} 55 | \newtheorem{defn}[thm]{Definition} 56 | \newtheorem{ex}[thm]{Example} 57 | \newtheorem{remark}[thm]{Remark} 58 | \newtheorem{cor}[thm]{Corollary} 59 | 60 | %% LINKS and COLORS 61 | \usepackage[dvipsnames]{xcolor} 62 | \usepackage{hyperref} 63 | \definecolor{myred}{RGB}{163, 32, 45} 64 | \hypersetup{ 65 | %backref=true, 66 | %pagebackref=true, 67 | colorlinks=true, 68 | urlcolor=myred, 69 | citecolor=myred, 70 | linktoc=all, 71 | linkcolor=myred, 72 | } 73 | 74 | %% TABLE OF CONTENTS 75 | \addto\captionsenglish{ 76 | \renewcommand{\contentsname} 77 | {}% This removes the heading over the table of contents. 78 | } 79 | 80 | 81 | 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | %%%%%%%%%%%%%%%%%%%%%% END PREAMBLE %%%%%%%%%%%%%%%%%%%%%%%% 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 87 | 88 | \title{202A: Dynamic Programming and Applications\\[5pt] {\Large \textbf{Homework \#2}}} 89 | 90 | \author{Andreas Schaab\footnote{ 91 | UC Berkeley. Email: schaab@berkeley.edu.\\ 92 | This course builds on the excellent teaching material developed, in particular, by David Laibson and Benjamin Moll. I would also like to thank Gabriel Chodorow-Reich, Pablo Kurlat, J\'on Steinsson, and Gianluca Violante, as well as QuantEcon, on whose teaching material I have drawn. 93 | }} 94 | 95 | \date{} 96 | 97 | 98 | \begin{document} 99 | 100 | \maketitle 101 | 102 | 103 | 104 | 105 | 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 108 | \vspace{5mm} 109 | \section*{Problem 1: Optimal Stopping} 110 | 111 | Consider the optimal stopping application from class: Each period $t = 0, 1, \ldots$ the consumer draws a job offer from a uniform distribution with support in the unit interval: $x \sim \text{unif}[0, 1]$. The consumer can either accept the offer and realize net present value $x$, or the consumer can wait another period and draw again. Once you accept an offer the game ends. Waiting to accept an offer is costly because the value of the remaining offers declines at rate $\rho = - \log(\beta)$ between periods. The Bellman equation for this problem is: 112 | \begin{equation*} 113 | V(x) = \max \bigg\{ x, \; \beta \mathbb E V(x') \bigg\} 114 | \end{equation*} 115 | where $x'$ is your next draw, which is a random variable. 116 | 117 | 118 | \vspace{2mm} 119 | \begin{enumerate}[(a)] 120 | \item Explain the intuition behind this Bellman equation. Explain every term. 121 | 122 | \item Briefly describe one decision problem you have faced in your own life that could be modeled using the above Bellman equation. 123 | 124 | \item Consider the associated functional operator: 125 | \begin{equation*} 126 | (Bw)(x) = \max \bigg\{ x, \; \beta \mathbb E w(x') \bigg\} 127 | \end{equation*} 128 | for all $x$. Using Blackwell's conditions, show that this Bellman operator is a contraction mapping. 129 | 130 | \item What does the contraction mapping property imply about $\lim_{n \to \infty} B^n w$, where $w$ is \textit{any} arbitrary function? 131 | 132 | \item Suppose we make a (bad?) guess $w(x) = 1$ for all $x$. Analytically iterate on $B^n w$ and show that 133 | \begin{equation*} 134 | \lim_{n \to \infty} (B^n w) (x) = V(x) = \begin{cases} 135 | x^* & \text { if } x \leq x^* \\ 136 | x & \text { if } x > x^* 137 | \end{cases} 138 | \end{equation*} 139 | where 140 | \begin{equation*} 141 | x^* = e^\rho \bigg( 1 - \Big[ 1 - e^{- 2 \rho} \Big]^\frac{1}{2} \bigg). 142 | \end{equation*} 143 | \end{enumerate} 144 | 145 | 146 | 147 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 148 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 149 | \vspace{5mm} 150 | \section*{Problem 2: Optimal Project Completion} 151 | 152 | Every period you draw a cost $c$ distributed uniformly between $0$ and $1$ for completing a project. If you undertake the project, you pay $c$, and complete the project with probability $1-p$. Each period in which the project remains uncompleted, you pay a late fee of $l$. The game continues until you complete the project. 153 | 154 | 155 | \vspace{2mm} 156 | \begin{enumerate}[(a)] 157 | \item Write down the Bellman Equation assuming no discounting. Why is it ok to assume no discounting in this problem? 158 | 159 | \item Derive the optimal threshold: $c^* = \sqrt{2l}$. Explain intuitively, why this threshold does not depend on the probability of failing to complete the project, $p$. 160 | 161 | \item How would these results change if we added discounting to the framework? Redo steps a and b, assuming that the agent discounts the future with discount factor $0 < \beta < 1$ and assuming that $p = 0$. Show that the optimal threshold is given by 162 | \begin{equation*} 163 | c^* = \frac{1}{\beta} \bigg( \beta - 1 + \sqrt{ (1-\beta)^2 + 2 \beta^2 l } \bigg) 164 | \end{equation*} 165 | 166 | \item When $0 < \beta < 1$, is the optimal value of $c^*$ still independent of the value of $p$? If not, how does $c^*$ qualitatively vary with $p$? Provide an intuitive argument. 167 | 168 | \item Take the perspective of an agent who has not yet observed the current period's draw of $c$. Prove that the expected delay until completion is given by: 169 | \begin{equation*} 170 | \frac{1}{c^*(1-p)} - 1 171 | \end{equation*} 172 | 173 | \end{enumerate} 174 | 175 | 176 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 177 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 178 | \newpage 179 | \section*{Problem 3: Consumption-Savings with Deterministic Income} 180 | 181 | Consider an economy populated by a continuum of infinitely lived households. There is no uncertainty in this economy for now. Households' preferences are given by 182 | \begin{equation*} 183 | \max \int_0^\infty e^{-\rho t} u(c_t) dt. 184 | \end{equation*} 185 | That is, households discount future consumption $c_t$ at a rate $\rho$. Oftentimes, we will use constant relative risk aversion (CRRA) preferences, given by 186 | \begin{equation*} 187 | u(c_t) = \frac{c_t^{1-\gamma}}{1-\gamma}. 188 | \end{equation*} 189 | A special form of these preferences are log preferences, 190 | \begin{equation*} 191 | u(c_t) = \log(c_t). 192 | \end{equation*} 193 | 194 | 195 | The household's flow budget constraint in this economy is given by 196 | \begin{equation*} 197 | \frac{d}{dt}(P_t a_t) = i_t (P_t a_t) - P_t c_t + P_t y_t, 198 | \end{equation*} 199 | where $P_t$ is the nominal price level, $c_t$ is real consumption expenditures, $a_t$ is the real wealth of the household and $\{y_t\}$ is an \textbf{exogenous} stream of income whose future path the household knows at any time point with certainty (because there is no uncertainty or risk for now). 200 | 201 | 202 | \vspace{5mm} 203 | \begin{enumerate} 204 | \item [(a)] Derive the budget constraint for real wealth, i.e., $\frac{d}{dt} a_t = \dot a_t$. Define the real interest rate as $r_t = i_t - \pi_t$, where $\pi_t \equiv \frac{\dot P_t}{P_t}$ is price inflation. 205 | 206 | \item [(b)] Derive the lifetime budget constraint 207 | 208 | \item [(c)] In class, we have so far always worked with the flow budget constraint as our constraint. And then we used either calculus of variations or optimal control theory. Alterantively, we can use the lifetime budget constraint as our constraint in this setting. (Why? When would you not be able to work with a lifetime budget constraint?) Set up the optimization problem with the lifetime budget constraint (i.e., write down the Lagrangian and introduce a multiplier) and take the first-order conditions. Solve for a consumption Euler equation. 209 | 210 | \item [(d)] Consider the two functional forms given earlier for utility, $u(c_t)$. Plug them into the Euler equation and solve for the term $\frac{u'(c_t)}{ u''(c_t) c_t}$. 211 | \end{enumerate} 212 | 213 | 214 | \vspace{6mm} 215 | \noindent 216 | We will now derive the simple Euler equation using two different approaches. The first approach will be using optimal control theory. In Problem 2, we will then use dynamic programming and confirm that the two approaches are equivalent. 217 | 218 | \vspace{5mm} 219 | \begin{enumerate} 220 | \item [(e)] Write down the optimal control problem. Identify the state, control variables and multipliers. 221 | 222 | \item [(f)] Write down the (current-value) Hamiltonian. 223 | 224 | \item [(g)] Find the FOCs. Rearrange and again find the consumption Euler equation. Confirm that it's the same equation we derived above. 225 | \end{enumerate} 226 | 227 | 228 | 229 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 230 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 231 | \vspace{5mm} 232 | \section*{Problem 4: Consumption-Savings using Dynamic Programming} 233 | 234 | We concluded Problem 1 by deriving the household's consumption Euler equation using optimal control theory. We will now do the same using dynamic programming. 235 | 236 | 237 | Consider again the preferences of our household, given by 238 | \begin{equation*} 239 | V_0 = \max_{\{c_t\}_{t>0}} \int_0^\infty e^{-\rho t} u(c_t) dt, 240 | \end{equation*} 241 | as well as the flow budget constraint 242 | \begin{equation*} 243 | \dot a_t = r_t a_t + y_t - c_t, 244 | \end{equation*} 245 | where the household takes the (deterministic) paths of real interest rates $\{r_t\}$ and income $\{y_t\}$ as given. 246 | 247 | 248 | We will now derive a recursive representation of the household's value function, and use the resulting Hamilton-Jacobi-Bellman (HJB) equation to derive the consumption Euler equation. 249 | 250 | 251 | \vspace{2mm} 252 | \begin{enumerate}[(a)] 253 | \item We will derive a recursive representation for the household value function $V_t(a) = V(t, a)$. Why does the value function take this form in this context? I.e., why does $V$ depend explicitly on calendar time, and why are we working with a representation of the problem where $a$ is the only state variable? 254 | 255 | \item In class, we derived the HJB heuristically, starting with a discrete time Bellman equation and taking the continuous time limit. We will now derive the HJB equation from the sequence problem. Recall in Lecture 1 we derived the Bellman equation from the sequence problem in discrete time. Please follow the same general proof strategy and derive the HJB in continuous time. You should arrive at the following HJB equation: 256 | \begin{equation*} 257 | \rho V_t(a) = \partial_t V_t(a) + \max_c \bigg\{ u(c) + \Big[ r_t a + y_t - c \Big] \partial_a V_t(a) \bigg\} 258 | \end{equation*} 259 | where $\partial_x$ denotes the partial derivative with respect to $x$. 260 | 261 | \item Why is there a $\partial_t V_t(a)$ term? 262 | 263 | \item Write down the FOC for consumption and interpret every term. Define and discuss the consumption policy function. 264 | 265 | \item Plug the consumption policy function back into the HJB. Discuss why this is now a non-linear partial differential equation. 266 | 267 | \item Take the envelope condition by differentiating the HJB with respect to $a$. 268 | 269 | \item Use the chain rule to characterize $\frac{d}{dt} V(t, a_t)$, taking into account explicitly that $a_t$ is also a function of time. 270 | 271 | \item You will now arrive at the consumption Euler equation by combining 3 equations: the characterization of $\frac{d}{dt} V_t(a_t)$ from the previous part, the FOC, and the envelope condition. 272 | 273 | \end{enumerate} 274 | 275 | 276 | 277 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 278 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 279 | \vspace{5mm} 280 | \section*{Problem 5: Oil Extraction} 281 | 282 | Time is continuous and indexed by $t \in [0, \infty)$. At time $t=0$, there is a finite amount of oil $x_0$. Denote by $c_t$ the rate of oil consumption at date $t$. Oil is non-renewable, so the remaining amount of oil at date $t$ is 283 | \begin{equation*} 284 | x_t = x_0 - \int_0^t c_s ds. 285 | \end{equation*} 286 | A social planner (the government) wants to set the rate of oil consumption to maximize utility of the representative household given by 287 | \begin{equation*} 288 | \int_0^\infty e^{- \rho t} u(c_t) dt, 289 | \end{equation*} 290 | with $u(c) = \log(c)$. 291 | 292 | \vspace{2mm} 293 | \begin{enumerate}[(a)] 294 | \item Explain the expression for the remaining amount of oil: $x_t = x_0 - \int_0^t c_s ds$ 295 | 296 | \item Set up the (present-value) Hamiltonian for this problem. List all state variables, control variables, and multipliers 297 | 298 | \item Write down the first-order necessary conditions. Solve for the optimal policy $c_t$ 299 | 300 | \item Write down the HJB equation for this problem 301 | 302 | \item Guess and verify that the value function is $V(x) = a + b \log(x)$. Solve for $a$ and $b$ 303 | \end{enumerate} 304 | 305 | 306 | 307 | 308 | \end{document} 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | -------------------------------------------------------------------------------- /homework/homework3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework/homework3.pdf -------------------------------------------------------------------------------- /homework/homework3.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{extarticle} 2 | \usepackage{fullpage,amsmath,amsfonts,microtype,nicefrac,amssymb, amsthm} 3 | \usepackage[left=1in, bottom=1in, top=1in, right = 1in]{geometry} 4 | \usepackage{textcomp} 5 | \usepackage{mathpazo} 6 | \usepackage{mathrsfs} 7 | \usepackage[T1]{fontenc} 8 | \usepackage[utf8]{inputenc} 9 | \usepackage[english]{babel} 10 | \usepackage{graphicx} 11 | 12 | \usepackage{microtype} 13 | 14 | \usepackage{bm} 15 | \usepackage{dsfont} 16 | \usepackage{enumerate} 17 | \usepackage{ragged2e} 18 | 19 | \setlength{\parindent}{24pt} 20 | \setlength{\jot}{8pt} 21 | 22 | 23 | \usepackage[shortlabels]{enumitem} 24 | 25 | 26 | %% FOOTNOTES 27 | \usepackage[bottom]{footmisc} 28 | \usepackage{footnotebackref} 29 | 30 | 31 | %% FIGURE ENVIRONMENT 32 | %\graphicspath{{}} 33 | \usepackage[margin=15pt, font=small, labelfont={bf}, labelsep=period]{caption} 34 | \usepackage{subcaption} 35 | \captionsetup[figure]{name={Figure}, position=above} 36 | \usepackage{float} 37 | \usepackage{epstopdf} 38 | 39 | 40 | %% NEW COMMANDS 41 | \renewcommand{\baselinestretch}{1.25} 42 | \renewcommand{\qedsymbol}{$\blacksquare$} 43 | \newcommand{\R}{\mathbb{R}} 44 | \newcommand{\indep}{\mathrel{\text{\scalebox{1.07}{$\perp\mkern-10mu\perp$}}}} 45 | \renewcommand{\b}{\begin} 46 | \newcommand{\e}{\end} 47 | 48 | %% NEWTHEOREM 49 | \theoremstyle{plain} 50 | \newtheorem{thm}{Theorem} 51 | \newtheorem{lem}[thm]{Lemma} 52 | \newtheorem{prop}[thm]{Proposition} 53 | 54 | \theoremstyle{definition} 55 | \newtheorem{defn}[thm]{Definition} 56 | \newtheorem{ex}[thm]{Example} 57 | \newtheorem{remark}[thm]{Remark} 58 | \newtheorem{cor}[thm]{Corollary} 59 | 60 | %% LINKS and COLORS 61 | \usepackage[dvipsnames]{xcolor} 62 | \usepackage{hyperref} 63 | \definecolor{myred}{RGB}{163, 32, 45} 64 | \hypersetup{ 65 | %backref=true, 66 | %pagebackref=true, 67 | colorlinks=true, 68 | urlcolor=myred, 69 | citecolor=myred, 70 | linktoc=all, 71 | linkcolor=myred, 72 | } 73 | 74 | %% TABLE OF CONTENTS 75 | \addto\captionsenglish{ 76 | \renewcommand{\contentsname} 77 | {}% This removes the heading over the table of contents. 78 | } 79 | 80 | 81 | 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | %%%%%%%%%%%%%%%%%%%%%% END PREAMBLE %%%%%%%%%%%%%%%%%%%%%%%% 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 87 | 88 | \title{202A: Dynamic Programming and Applications\\[5pt] {\Large \textbf{Homework \#3}}} 89 | 90 | \author{Andreas Schaab} 91 | 92 | \date{} 93 | 94 | 95 | \begin{document} 96 | 97 | \maketitle 98 | 99 | 100 | 101 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 102 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103 | \section*{Problem 1: Brownian Motion} 104 | 105 | This problem collects several exercises on Brownian motion and stochastic calculus. We denote standard Brownian motion by $B_t$. 106 | 107 | \begin{enumerate}[(a)] 108 | 109 | \item Show that $\mathbb Cov(B_s, B_t) = \min\{s, t\}$ for two times $0 \leq s < t$. Use the following tricks: Use the covariance formula $\mathbb Cov(A, B) = \mathbb E (AB) - \mathbb E(A) \mathbb E(B)$. Use $B_t \sim \mathcal N(0, t)$ as well as $B_t - B_s \sim \mathcal N(0, t-s)$. And use $B_t = B_s + (B_t - B_s)$. 110 | 111 | 112 | \item Let $X_t = B_t^2$. What is $\mathbb E X_t$? What is $\mathbb Cov (X_t, X_s)$? 113 | 114 | 115 | \item Let $X_t = B_{t+s} - B_s$ for some fixed $s > 0$. Let $Y_t = \frac{1}{\sqrt \lambda} B_{\lambda t}$. Show that both $X_t$ and $Y_t$ are standard Brownian motion; that is, show the following 5 properties: 116 | \begin{enumerate}[(i)] 117 | \item $X_0 = Y_0 = 0$ 118 | \item $X_t, Y_t \sim \mathcal N(0, t)$ 119 | \item Stationarity 120 | \item Independent increments 121 | \item Continuous 122 | \end{enumerate} 123 | 124 | 125 | \item Geometric Brownian motion evolves as: $dX_t = \mu X_t dt + \sigma X_t dB_t$ given an initial value $X_0$. Show that $X_t = X_0 e^{\mu t - \frac{\sigma^2}{2} t + \sigma B_t}$. Also show that $\mathbb E = X_0 e^{\mu t}$. 126 | 127 | \item The Ornstein-Uhlenbeck (OU) process is like a continuous-time variant of the AR(1) process. It evolves as $dX_t = - \mu X_t dt + \sigma dB_t$ for drift parameter $\mu$, diffusion parameter $\sigma$, and some $X$. Show that it solves $X_t = X_0 e^{- \mu t} + \sigma \int_0^t e^{-\mu(t - s)} dB_s$. 128 | 129 | \item Let $X_t = \int_0^t B_s ds$ and $Y_t = \int_0^t B_s^2 ds$. Compute $\mathbb E(X_t)$ and $\mathbb E(Y_t)$, as well as $\mathbb Var(X_t)$ and $\mathbb Var(Y_t)$. 130 | \end{enumerate} 131 | 132 | 133 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 134 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 135 | \vspace{5mm} 136 | \section*{Problem 2: Poisson Process} 137 | 138 | \begin{enumerate}[(a)] 139 | \item Consider a two-state Markov chain in continuous time denoted $Y_t$, which can take on values in $\{Y^1, Y^2\}$. The transition rate is given by $\lambda$ regardless of the current state. Suppose we are in state $1$ at $t = 0$. Compute the expected time until the process switches to state $2$. 140 | 141 | \item Now suppose that the transition rates differ depending on which state we are in. That is, if we're in state $1$ we transition to rate $2$ at rate $\lambda_1$, and vice versa at rate $\lambda_2$. Show that the fraction of time the process spends in states $1$ and $2$ converges in the long run to $\frac{\lambda_2}{\lambda_1 + \lambda_2}$ and $\frac{\lambda_1}{\lambda_1 + \lambda_2}$. 142 | 143 | \item What is $\mathbb E (Y_t \mid Y_0 = Y^1)$? 144 | 145 | \end{enumerate} 146 | 147 | 148 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 149 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 150 | \vspace{5mm} 151 | \section*{Problem 3: Ito's Lemma} 152 | 153 | \begin{enumerate}[(a)] 154 | \item Let $dX_t = - \alpha X_t dt + \sigma dB_t$, and $f(t, X_t) = e^{\alpha t} X_t$. Show that $df = \sigma e^{\alpha t} d B$. 155 | 156 | \item Consider the capital accumulation equation $dK_t = (\iota - \delta) K_t dt + \sigma K_t dB_t$, where $\iota$ is the investment rate. Suppose our value function is $V(K_t)$. Use Ito's lemma to solve for $dV_t$. 157 | 158 | \item Suppose $X_t = f(t, B_t)$ for some function $f$. In this problem, we will solve for $f$. The only information we have is that 159 | \begin{equation*} 160 | dX_t = X_t dB_t. 161 | \end{equation*} 162 | Use Ito's lemma to derive two conditions on the partial derivatives of $f(\cdot)$. (That is, group the $dt$ and $dB$ terms and reason via coefficient-matching.) Show that the function $f(t, x) = e^{x - \frac{1}{2}t}$ satisfies these conditions. 163 | \end{enumerate} 164 | 165 | 166 | 167 | 168 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 169 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 170 | \vspace{5mm} 171 | \section*{Problem 4: Generator} 172 | 173 | We defined the generator of a stochastic process in class. The generator is extremely useful when we want to quickly write down the HJB associated with a dynamic optimization problem. 174 | 175 | \vspace{4mm} 176 | \begin{enumerate}[(a)] 177 | \item Consider the wealth and income processes 178 | \begin{align*} 179 | da_t &= (ra_t + y_t - c_t)dt + \sigma dB_t \\ 180 | dy_t &= \theta (\bar y - y_t) dt + \nu dW_t 181 | \end{align*} 182 | where $B_t$ and $W_t$ are independent standard Brownian motion. Write down the generator $\mathcal A$ for the two-dimensional process 183 | \begin{equation*} 184 | \begin{pmatrix} 185 | d a_t \\ 186 | d y_t 187 | \end{pmatrix} 188 | \end{equation*} 189 | That is, what is $\mathcal A V$ for a given (smooth) function $V(a_t, y_t)$? 190 | 191 | \item Consider the capital accumulation process 192 | \begin{align*} 193 | dk_t &= (\iota - \delta) k_t dt. 194 | \end{align*} 195 | Also suppose that firm technology $A_t$ follows a two-state Markov chain (Poisson process) with transition rates $\lambda$. That is, $A_t$ can take on values in $\{A^1, A^2\}$. Suppose that the enterprise value of the firm is given by some function $V(k_t, A_t)$. Characterize the generator of the process 196 | \begin{equation*} 197 | \begin{pmatrix} 198 | d k_t \\ 199 | d A_t 200 | \end{pmatrix} 201 | \end{equation*} 202 | That is, what is $\mathcal A V$ for a given (smooth) function $V(k_t, A_t)$? (Recall that $\mathcal A V = \mathbb E[d V]$, so the expression you just solved for tells us how the firm's enterprise value evolves in expectation.) 203 | 204 | \end{enumerate} 205 | 206 | 207 | 208 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 209 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 210 | \vspace{5mm} 211 | \section*{Problem 5: Consumption with Income Uncertainty} 212 | 213 | Consider a household whose lifetime utility is given by 214 | \begin{equation*} 215 | V_0 = \max_{ \{c_t\} } \, \mathbb E_0 \int_0^\infty e^{- \rho t} u(c_t) dt. 216 | \end{equation*} 217 | The household makes consumption-savings decisions facing the budget constraint 218 | \begin{equation*} 219 | da_t = (r a_t + y_t - c_t)dt. 220 | \end{equation*} 221 | Here, $y_t$ is the household's income process. We cannot perfectly forecast our future income, so we will assume that $y_t$ is a stochastic process. Below, you will be asked to write down the HJB associated with this problem for the two canonical models of income uncertainty. 222 | 223 | \vspace{4mm} 224 | \begin{enumerate}[(a)] 225 | \item Suppose that income follows the diffusion (Ornstein-Uhlenbeck / AR1) process 226 | \begin{equation*} 227 | dy_t = \theta(\bar y - y_t) dt + \sigma dB_t. 228 | \end{equation*} 229 | Write down the HJB for this problem that characterizes the household value function $V(a, y)$. 230 | 231 | \item Now suppose that income follows a two-state Markov chain (Poisson process). Income can be high or low, $\in \{y^L, y^H\}$. The transition rate from high to low is $\lambda^H$ and the transition rate from low to high is $\lambda^L$. Write down the HJB for this problem that characterizes the household value function $V(a, y)$. 232 | 233 | \item Suppose that the interest rate is not constant but varies over time. We know with certainty, however, how the interest rate evolves. So $\{r_t\}$ is a deterministic sequence that is exogenously given to us. (In other words, we are characterizing the household problem in partial equilibrium; the household takes the interest rate as given.) Write down the HJB for this problem that characterizes the household value function $V(t, a, y)$. Why is this value function no longer stationary? 234 | 235 | \end{enumerate} 236 | 237 | 238 | 239 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 240 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 241 | % \vspace{5mm} 242 | % \section*{Problem 6: Job Market Paper} 243 | % 244 | % Imagine you were a graduate student in economics, and you are currently searching for and working on what will hopefully become your job market paper. You have to go on the job market at date $T$ (it is currently date $t = 0$). Your objective is to maximize 245 | % \begin{equation*} 246 | % V_0 = \max \, \mathbb E_0 \bigg[ - \int_0^T v(\ell_t) dt + x_T \bigg]. 247 | % \end{equation*} 248 | % Here, $\ell_t$ is your rate of work (think: total hours you work in a day), and $v(\cdot)$ encodes the disutility you get from working. You can spend your working time on one of two activities: either you search for a new project to work on ($s_t$) or you continue working on your current project ($w_t$), facing the budget constraint 249 | % \begin{equation*} 250 | % \ell_t = s_t + w_t. 251 | % \end{equation*} 252 | % 253 | % 254 | % \vspace{4mm} 255 | % \begin{enumerate}[(a)] 256 | % \item Suppose that income follows the diffusion (Ornstein-Uhlenbeck / AR1) process 257 | % 258 | % \end{enumerate} 259 | 260 | 261 | 262 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 263 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 264 | \vspace{5mm} 265 | \section*{Problem 6: Consumption and Portfolio Choice} 266 | 267 | Consider a household that can trade two assets. The first asset is a stock. Stocks trade at price $Q_t$ and they pay the holder dividends at rate $D_t$. That is, when you hold the stock, your ``return'' comprises both the dividend payouts and the change in price until you sell the stock, which may be positive or negative. Formally, the rate of return on the stock is 268 | \begin{equation*} 269 | dR = \frac{D dt + dQ}{Q}, 270 | \end{equation*} 271 | where $\frac{D}{Q}$ is the dividend-price ratio and $\frac{dQ}{Q}$ is capital gains (change in price). We will now assume that the return process takes the form 272 | \begin{equation*} 273 | d R = \mu dt + \sigma dB. 274 | \end{equation*} 275 | for some $\mu$ and $\sigma$, where $B$ is standard Brownian motion. 276 | 277 | 278 | The second asset the household can trade is a bond, which trades at price $P$. And we assume that the bond price evolves simply according to 279 | \begin{equation*} 280 | \frac{dP}{P} = r dt, 281 | \end{equation*} 282 | which is the same as saying that holding the bond earns a riskfree rate of return $r dt$. Crucially, the household can both buy and sell these assets. Assume there are no borrowing (or short-sale) constraints. 283 | 284 | The household's lifetime value is given by 285 | \begin{equation*} 286 | V_0 = \max \mathbb E_0 \int_0^\infty e^{- \rho t} u(c_t) dt. 287 | \end{equation*} 288 | Let's denote by $b_t$ and $k_t$ the household's bond and stock holdings. Then the household budget constraint is 289 | \begin{equation*} 290 | Q dk + P db + c dt = D k dt. 291 | \end{equation*} 292 | Why does the budget constraint take this form? On the RHS, households earn dividends at rate $D$ for each unit of stock they hold. On the LHS, households can spend on consumption, or they can choose to purchase stocks or bonds. If $dk > 0$, the household purchases new stocks at price $Q$ on top of the current stock holdings. If $dk < 0$, the household sells stocks. 293 | 294 | 295 | We now define \textit{net worth} $n$ and the \textit{risky portfolio share} $\theta$ implicity via 296 | \begin{align*} 297 | \theta n &= Q k \\ 298 | (1-\theta) n &= Pb 299 | \end{align*} 300 | In other words, $n$ is a notion of total wealth of the household. And we say that the household invests fraction $\theta$ of total wealth in stocks, and fraction $1-\theta$ in bonds. 301 | 302 | 303 | \vspace{5mm} 304 | \begin{enumerate}[(a)] 305 | \item Derive the law of motion for household net worth and show that it satisfies: 306 | \begin{equation*} 307 | dn = \bigg[ r n + \theta n \Big( \mu - r \Big) - c \bigg] dt + \theta n \sigma dB 308 | \end{equation*} 309 | 310 | \item Derive the recursive representation of the households optimization problem. That is, write down the HJB equation that characterizes the household value function $V(n)$. Why was it useful to rewrite the household problem in terms of net worth (rather than stock and bond holdings)? Why is this value function stationary? 311 | 312 | \item Derive the first-order conditions for $c$ and the portfolio share $\theta$. 313 | 314 | \item \textit{Optional and more difficult}: Derive the Euler equation for marginal utility and show that it satisfies 315 | \begin{equation*} 316 | \frac{du_c}{u_c} = (\rho - r) dt - \frac{\mu - r}{\sigma} dB. 317 | \end{equation*} 318 | Show that with CRRA utility, this implies a consumption Euler equation 319 | \begin{equation*} 320 | \frac{dc}{c} = \frac{r - \rho}{\gamma} dt + \frac{1+\gamma}{2} \bigg( \frac{\mu - r}{\gamma \sigma} \bigg)^2 dt + \frac{\mu - r}{\gamma \sigma} dB. 321 | \end{equation*} 322 | 323 | 324 | \item Guess and verify that 325 | \begin{equation*} 326 | V(n) = \frac{1}{1-\gamma} \kappa^{-\gamma} n^{1-\gamma} 327 | \end{equation*} 328 | where $\kappa = \frac{1}{\gamma} \Big[ \rho - (1-\gamma)r - \frac{1-\gamma}{2\gamma} \Big( \frac{\mu-r}{\sigma} \Big)^2 \Big]$. 329 | 330 | 331 | \item Given the solution for $V(n)$, use the FOCs to also solve for $c(n)$ and $\theta(n)$. You have now solved the household portfolio choice problem in closed form!! 332 | 333 | 334 | \item What does this model tell us? Consider the interesting special case of log utility with $\gamma = 1$. Show that consumption collapses to $c = \rho n$. This implies that you want to consume a constant fraction $\rho$ of lifetime net worth. But how much should you invest in the stock market according to this model? Take the formula for $\theta$ and plug in log utility. Let's assume an equity premium of $6\%$, so $\mu-r = 0.0.06$. And let's assume volatility of stock returns of $16\%$, so $\sigma = 0.16$. Solve for the numeric value of $\theta$, i.e., the fraction of your total wealth that this model tells you to invest in the stock market. Is this high or low? What if ``total wealth'' also includes a notion of your human capital? 335 | 336 | \end{enumerate} 337 | 338 | \end{document} 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | -------------------------------------------------------------------------------- /homework/homework4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework/homework4.pdf -------------------------------------------------------------------------------- /homework/homework5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework/homework5.pdf -------------------------------------------------------------------------------- /homework/homework5.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{extarticle} 2 | \usepackage{fullpage,amsmath,amsfonts,microtype,nicefrac,amssymb, amsthm} 3 | \usepackage[left=1in, bottom=1in, top=1in, right = 1in]{geometry} 4 | \usepackage{textcomp} 5 | \usepackage{mathpazo} 6 | \usepackage{mathrsfs} 7 | \usepackage[T1]{fontenc} 8 | \usepackage[utf8]{inputenc} 9 | \usepackage[english]{babel} 10 | \usepackage{graphicx} 11 | 12 | \usepackage{microtype} 13 | 14 | \usepackage{bm} 15 | \usepackage{dsfont} 16 | \usepackage{enumerate} 17 | \usepackage{ragged2e} 18 | 19 | \setlength{\parindent}{24pt} 20 | \setlength{\jot}{8pt} 21 | 22 | 23 | \usepackage[shortlabels]{enumitem} 24 | 25 | 26 | %% FOOTNOTES 27 | \usepackage[bottom]{footmisc} 28 | \usepackage{footnotebackref} 29 | 30 | 31 | %% FIGURE ENVIRONMENT 32 | %\graphicspath{{}} 33 | \usepackage[margin=15pt, font=small, labelfont={bf}, labelsep=period]{caption} 34 | \usepackage{subcaption} 35 | \captionsetup[figure]{name={Figure}, position=above} 36 | \usepackage{float} 37 | \usepackage{epstopdf} 38 | 39 | 40 | %% NEW COMMANDS 41 | \renewcommand{\baselinestretch}{1.25} 42 | \renewcommand{\qedsymbol}{$\blacksquare$} 43 | \newcommand{\R}{\mathbb{R}} 44 | \newcommand{\indep}{\mathrel{\text{\scalebox{1.07}{$\perp\mkern-10mu\perp$}}}} 45 | \renewcommand{\b}{\begin} 46 | \newcommand{\e}{\end} 47 | 48 | %% NEWTHEOREM 49 | \theoremstyle{plain} 50 | \newtheorem{thm}{Theorem} 51 | \newtheorem{lem}[thm]{Lemma} 52 | \newtheorem{prop}[thm]{Proposition} 53 | 54 | \theoremstyle{definition} 55 | \newtheorem{defn}[thm]{Definition} 56 | \newtheorem{ex}[thm]{Example} 57 | \newtheorem{remark}[thm]{Remark} 58 | \newtheorem{cor}[thm]{Corollary} 59 | 60 | %% LINKS and COLORS 61 | \usepackage[dvipsnames]{xcolor} 62 | \usepackage{hyperref} 63 | \definecolor{myred}{RGB}{163, 32, 45} 64 | \hypersetup{ 65 | %backref=true, 66 | %pagebackref=true, 67 | colorlinks=true, 68 | urlcolor=myred, 69 | citecolor=myred, 70 | linktoc=all, 71 | linkcolor=myred, 72 | } 73 | 74 | %% TABLE OF CONTENTS 75 | \addto\captionsenglish{ 76 | \renewcommand{\contentsname} 77 | {}% This removes the heading over the table of contents. 78 | } 79 | 80 | 81 | 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 84 | %%%%%%%%%%%%%%%%%%%%%% END PREAMBLE %%%%%%%%%%%%%%%%%%%%%%%% 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 87 | 88 | \title{202A: Dynamic Programming and Applications\\[5pt] {\Large \textbf{Homework \#5}}} 89 | 90 | \author{Andreas Schaab\footnote{ 91 | UC Berkeley. Email: schaab@berkeley.edu.\\ 92 | This course builds on the excellent teaching material developed, in particular, by David Laibson and Benjamin Moll. I would also like to thank Gabriel Chodorow-Reich, Pablo Kurlat, J\'on Steinsson, and Gianluca Violante, as well as QuantEcon, on whose teaching material I have drawn. 93 | }} 94 | 95 | \date{} 96 | 97 | 98 | \begin{document} 99 | 100 | \maketitle 101 | 102 | The theoretical part of this Homework is very short. In Problem 1, you will set up the model of Aiyagari (1994) in continuous time and define its competitive equilibrium. In the numerical part of the Homework, you will then write code to solve this model numerically. 103 | 104 | 105 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 107 | \section*{Problem 1: Aiyagari (1994)} 108 | 109 | Time is continuous, $t \in [0, \infty)$, and there is no aggregate uncertainty. We allow for one-time, unanticipated (``MIT'') shocks to TFP (agents have perfect foresight with respect to this aggregate shock). Concretely, we denote by $A_t$ the TFP of the economy at date $t$, and $\{A_t\}$ is an exogenously given but deterministic sequence. 110 | 111 | 112 | \paragraph{Households.} 113 | The economy is populated by a continuum of measure one of households. Consider a household $i \in [0, 1]$. Household $i$'s preferences are encoded in her lifetime utility 114 | \begin{equation*} 115 | V_{i, 0} = \max_{ \{c_{i, t}\}_{t \geq 0} } \; \mathbb E_0 \int_0^\infty e^{- \rho t} u(c_{i, t}) dt. 116 | \end{equation*} 117 | 118 | 119 | The economy features a single asset---capital. Households are the owners of capital and rent it to firms. Denote the stock of capital owned by household $i$ at date $t$ by $k_{i, t}$. The household faces the budget constraints 120 | \begin{align*} 121 | c_{i, t} + i_{i, t} &= z_{i, t} w_t + r_t^k k_{i, t} \\ 122 | \dot k_{i, t} &= i_{i, t} - \delta k_{i, t} . 123 | \end{align*} 124 | The first line of the budget constraint says that household expenditures---consumption and investment---must equal her income. Income comprises labor income and income from renting capital to firms at rate $r_t^k$. We assume that households supply one unit of labor inelastically, earning wage rate $w_t$. But households also face idiosyncratic earnings risk: $z_{i, t}$ denotes the household's idiosyncratic labor productivity, and the wage $w_t$ is paid on efficiency units of work, rather than hours of work. We assume that $z_{i, t}$ follows a two-state Markov chain, with individual labor productivity taking on values in $\in \{z^L, z^H\}$. You may interpret $z^L$ as unemployment and $z^H$ as employment. Households transition between these dates at transition rates $\lambda$ (symmetric for simplicity). 125 | 126 | 127 | The second line summarizes the household's capital accumulation technology. Putting these two equations together, the household's stock of capital evolves simply as 128 | \begin{equation*} 129 | \dot k_{i, t} = r_t k_{i, t} + z_{i, t} w_t - c_{i, t}, 130 | \end{equation*} 131 | where we denote by $r_t$ the effective real rate of return on owning capital. Finally, we assume that households also face a short-sale constraint on capital 132 | \begin{equation*} 133 | k_{i, t} \geq 0. 134 | \end{equation*} 135 | 136 | 137 | \begin{enumerate} 138 | \item [(a)] Write down household $i$'s problem in sequence form. 139 | 140 | \item [(b)] Next, we characterize a recursive representation for the household problem. In terms of which state variables will we be able to derive a recursive representation? Write down the HJB for this problem. Is the HJB stationary---why or why not? Why are we now dropping the $i$ subscript when writing down this recursive representation of the household problem? 141 | 142 | \item [(c)] Where in the HJB does the short-sale constraint show up? Write down the explicit condition for this. 143 | 144 | \item [(d)] Take the first-order condition. 145 | \end{enumerate} 146 | 147 | 148 | \paragraph{Aggregation.} 149 | We denote by $g_t(k, z)$ the joint density of households over capital and individual labor productivities at date $t$. 150 | 151 | \begin{enumerate} 152 | \item [(e)] Write down the Kolmogorov forward equation for $g_t(k, z)$. 153 | \end{enumerate} 154 | 155 | 156 | \paragraph{Firms.} 157 | There is a representative firm that operates the production technology 158 | \begin{equation*} 159 | Y_t = A_t K_t^\alpha L_t^{1-\alpha}, 160 | \end{equation*} 161 | where the deterministic sequence $\{A_t\}$ denotes TFP. The firm maximizes profit $Y_t - w_t L_t - r_t^k K_t$. We use capital letters here to signify that $K_t$ and $L_t$ denote aggregate capital and labor. 162 | 163 | 164 | \begin{enumerate} 165 | \item [(f)] Write down the firm problem and take first-order conditions. This should yield two conditions for factor prices $w_t$ and $r_t^k$. Why are the implied profits zero? 166 | \end{enumerate} 167 | 168 | 169 | \paragraph{Markets and equilibrium.} 170 | 171 | 172 | \begin{enumerate} 173 | \item [(g)] Which markets have to clear in equilibrium? Write down each market clearing condition. 174 | 175 | \item [(h)] Define (recursive) competitive equilibrium. 176 | 177 | \item [(i)] Explicitly write down the system of equations that competitive equilibrium characterizes. Convince yourself that you have account for $\{c_t(k, z), V_t(k, z), g_t(k, z), Y_t, L_t, K_t\}_{t \geq 0}$ and $\{r_t, r_t^k, w_t\}_{t \geq 0}$. 178 | 179 | \item [(j)] Define stationary competitive equilibrium. Dropping time subscripts to denote allocation and prices in steady state, make sure you have accounted for $\{c(k, z), V(k, z), g(k, z), Y, L, K\}$ and $\{r, r^k, w\}$. 180 | \end{enumerate} 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | \end{document} 196 | -------------------------------------------------------------------------------- /homework_coding/homework_coding_week1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/homework_coding_week1.pdf -------------------------------------------------------------------------------- /homework_coding/homework_coding_week3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/homework_coding_week3.pdf -------------------------------------------------------------------------------- /homework_coding/homework_coding_week4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/homework_coding_week4.pdf -------------------------------------------------------------------------------- /homework_coding/homework_coding_week5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/homework_coding_week5.pdf -------------------------------------------------------------------------------- /homework_coding/honework_coding_week2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/honework_coding_week2.pdf -------------------------------------------------------------------------------- /homework_coding/solutions/homework_coding_week1_sol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/solutions/homework_coding_week1_sol.pdf -------------------------------------------------------------------------------- /homework_coding/solutions/homework_coding_week2_sol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/solutions/homework_coding_week2_sol.pdf -------------------------------------------------------------------------------- /homework_coding/solutions/homework_coding_week3_sol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/solutions/homework_coding_week3_sol.pdf -------------------------------------------------------------------------------- /homework_coding/solutions/homework_coding_week4_sol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_coding/solutions/homework_coding_week4_sol.pdf -------------------------------------------------------------------------------- /homework_solutions/Homework_Solutions_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_solutions/Homework_Solutions_1.pdf -------------------------------------------------------------------------------- /homework_solutions/Homework_Solutions_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_solutions/Homework_Solutions_2.pdf -------------------------------------------------------------------------------- /homework_solutions/Homework_Solutions_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_solutions/Homework_Solutions_3.pdf -------------------------------------------------------------------------------- /homework_solutions/Homework_Solutions_4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_solutions/Homework_Solutions_4.pdf -------------------------------------------------------------------------------- /homework_solutions/Homework_Solutions_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/homework_solutions/Homework_Solutions_5.pdf -------------------------------------------------------------------------------- /lectures/Brownian1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian1.png -------------------------------------------------------------------------------- /lectures/Brownian2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian2.png -------------------------------------------------------------------------------- /lectures/Brownian3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian3.png -------------------------------------------------------------------------------- /lectures/Brownian4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian4.png -------------------------------------------------------------------------------- /lectures/Brownian5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian5.png -------------------------------------------------------------------------------- /lectures/Brownian6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian6.png -------------------------------------------------------------------------------- /lectures/Brownian7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/Brownian7.png -------------------------------------------------------------------------------- /lectures/figures/HACT_MPC.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_MPC.png -------------------------------------------------------------------------------- /lectures/figures/HACT_accuracy_consumption.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_accuracy_consumption.png -------------------------------------------------------------------------------- /lectures/figures/HACT_accuracy_error1000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_accuracy_error1000.png -------------------------------------------------------------------------------- /lectures/figures/HACT_accuracy_error30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_accuracy_error30.png -------------------------------------------------------------------------------- /lectures/figures/HACT_consumption.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_consumption.png -------------------------------------------------------------------------------- /lectures/figures/HACT_diffusion_distribution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_diffusion_distribution.png -------------------------------------------------------------------------------- /lectures/figures/HACT_diffusion_savings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_diffusion_savings.png -------------------------------------------------------------------------------- /lectures/figures/HACT_distribution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_distribution.png -------------------------------------------------------------------------------- /lectures/figures/HACT_distribution2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_distribution2.png -------------------------------------------------------------------------------- /lectures/figures/HACT_example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_example1.png -------------------------------------------------------------------------------- /lectures/figures/HACT_example2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_example2.png -------------------------------------------------------------------------------- /lectures/figures/HACT_existence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_existence.png -------------------------------------------------------------------------------- /lectures/figures/HACT_savings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_savings.png -------------------------------------------------------------------------------- /lectures/figures/HACT_savings2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_savings2.png -------------------------------------------------------------------------------- /lectures/figures/HACT_spyA.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_spyA.png -------------------------------------------------------------------------------- /lectures/figures/HACT_transition.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/HACT_transition.png -------------------------------------------------------------------------------- /lectures/figures/ikc1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/ikc1.png -------------------------------------------------------------------------------- /lectures/figures/ikc2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/ikc2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_consumption_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_consumption_0.png -------------------------------------------------------------------------------- /lectures/figures/inequality_consumption_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_consumption_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_consumption_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_consumption_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_consumption_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_consumption_3.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_3.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_4.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_5.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_6.png -------------------------------------------------------------------------------- /lectures/figures/inequality_earnings_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_earnings_7.png -------------------------------------------------------------------------------- /lectures/figures/inequality_government_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_government_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_government_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_government_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_government_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_government_3.png -------------------------------------------------------------------------------- /lectures/figures/inequality_government_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_government_4.png -------------------------------------------------------------------------------- /lectures/figures/inequality_household_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_household_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_household_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_household_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_household_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_household_3.png -------------------------------------------------------------------------------- /lectures/figures/inequality_polarization_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_polarization_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_polarization_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_polarization_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_polarization_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_polarization_3.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_10.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_11.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_12.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_3.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_4.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wage_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wage_5.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wealth_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wealth_1.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wealth_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wealth_2.png -------------------------------------------------------------------------------- /lectures/figures/inequality_wealth_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/inequality_wealth_3.png -------------------------------------------------------------------------------- /lectures/figures/liquidity_constraint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/liquidity_constraint.png -------------------------------------------------------------------------------- /lectures/figures/precautionary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/precautionary.png -------------------------------------------------------------------------------- /lectures/figures/skiba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/skiba.png -------------------------------------------------------------------------------- /lectures/figures/skiba1978.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/skiba1978.png -------------------------------------------------------------------------------- /lectures/figures/skiba_savings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/skiba_savings.png -------------------------------------------------------------------------------- /lectures/figures/skiba_spyA.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/skiba_spyA.png -------------------------------------------------------------------------------- /lectures/figures/skiba_value.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/skiba_value.png -------------------------------------------------------------------------------- /lectures/figures/spyA.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/figures/spyA.png -------------------------------------------------------------------------------- /lectures/lecture1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture1.pdf -------------------------------------------------------------------------------- /lectures/lecture10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture10.pdf -------------------------------------------------------------------------------- /lectures/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture11.pdf -------------------------------------------------------------------------------- /lectures/lecture2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture2.pdf -------------------------------------------------------------------------------- /lectures/lecture3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture3.pdf -------------------------------------------------------------------------------- /lectures/lecture5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture5.pdf -------------------------------------------------------------------------------- /lectures/lecture7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture7.pdf -------------------------------------------------------------------------------- /lectures/lecture9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/lectures/lecture9.pdf -------------------------------------------------------------------------------- /sections/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sections/Section 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/sections/Section 1.pdf -------------------------------------------------------------------------------- /sections/Section 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/sections/Section 2.pdf -------------------------------------------------------------------------------- /sections/Section 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/sections/Section 3.pdf -------------------------------------------------------------------------------- /sections/Section_4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/sections/Section_4.pdf -------------------------------------------------------------------------------- /sections/Section_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/sections/Section_5.pdf -------------------------------------------------------------------------------- /sections/Syllabus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schaab-teaching/DynamicProgramming2024/481cfd8d139e31f496842efe7df60fa3664d10f4/sections/Syllabus.pdf -------------------------------------------------------------------------------- /sections/codes/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sections/codes/week1/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sections/codes/week1/capital_accumulation.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Script: capital_accumulation.m 3 | % 4 | % Author: Kiyea Jin 5 | % Date: October 14, 2024 6 | % 7 | % Description: 8 | % This script solves the continuous-time capital accumulation equation: 9 | % dK/dt = I - delta * K 10 | % where {I} represents a constant, exogenously given investment, and 11 | % an initial condition for capital K0 is provided. 12 | % 13 | % The solutions are obtained using: 14 | % 1. The integrating factor method for the analytical solution. 15 | % 2. The finite difference method for the numerical solution. 16 | % 17 | % The script also: 18 | % 3. Plots both the analytical and numerical solutions for comparison. 19 | % 4. Explores how the numerical solution varies with grid fineness. 20 | % 21 | % Parameters: 22 | % - Depreciation rate (delta): 0.05 23 | % - Constant investment (I): 0 or 5 24 | % - Initial boundary condition: K0 = 10 25 | % 26 | % Code Structure: 27 | % 1. DEFINE PARAMETERS 28 | % 2. INITIALIZE GRID POINTS 29 | % 3. ANALYTICAL SOLUTION: INTEGRATING FACTOR METHOD 30 | % 4. NUMERICAL SOLUTION: FINITE DIFFERENCE METHOD 31 | % 5. PLOT 32 | % (6. SOLVE USING ODE45) 33 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | 35 | clear all; 36 | close all; 37 | clc; 38 | 39 | %% 1. DEFINE PARAMETERS 40 | 41 | p = define_parameters(); 42 | 43 | %% 2. INITIALIZE GRID POINTS 44 | 45 | t = linspace(p.tmin, p.tmax, p.I)'; 46 | dt = (p.tmax-p.tmin)/(p.I-1); 47 | 48 | % Define investment series 49 | I = p.Inv*ones(p.I,1); 50 | 51 | %% 3. ANALYTICAL SOLUTION: INTEGRATING FACTOR METHOD 52 | 53 | % 3-1. Pre-allocate arrays for solutions 54 | K_analytical = zeros(p.I,1); 55 | 56 | % 3-2. Analytical solution: K(t) = I(t)/delta*(1-exp^{-delta*t})+exp^{-delta*t}*K(0) 57 | for i=1:p.I 58 | 59 | K_analytical(i) = (I(i)/p.delta) * (1 - exp(-p.delta*t(i))) + exp(-p.delta*t(i)) * p.K0; 60 | 61 | end 62 | 63 | %% 4. NUMERICAL SOLUTION: FINITE DIFFERENCE METHOD 64 | 65 | % 4-1. Pre-allocate arrays for solutions 66 | K_numerical = zeros(p.I,1); 67 | K_numerical(1) = p.K0; 68 | 69 | % 4-2. Numerical solution: (K(t+dt)-K(t))/dt = I(t) - delta*K(t) 70 | 71 | for i=1:p.I-1 72 | 73 | K_numerical(i+1) = K_numerical(i) + dt * (I(i) - p.delta*K_numerical(i)); 74 | 75 | end 76 | 77 | %% 5. PLOT 78 | 79 | % Compute the difference between two solutions 80 | error = K_analytical - K_numerical; 81 | 82 | % Plot both solutions for comparison 83 | figure; 84 | plot(t, K_analytical, 'r-', 'LineWidth', 2); hold on; 85 | plot(t, K_numerical, 'bo--', 'LineWidth', 1.5); 86 | xlabel('Time', 'FontSize', 11); 87 | ylabel('Capital (K)', 'FontSize', 11); 88 | legend('Analytical Solution', 'Numerical Solution (FDM)', 'FontSize', 12); 89 | title('Comparison of Analytical and Numerical Solutions', 'FontSize', 15); 90 | grid on; 91 | 92 | %% (6. SOLVE USING ODE45) 93 | % ode45 Solve non-stiff differential equations, medium order method. 94 | % [TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) integrates the system of 95 | % differential equations y' = f(t,y) from time TSPAN(1) to TSPAN(end) 96 | % with initial conditions Y0. Each row in the solution array YOUT 97 | % corresponds to a time in the column vector TOUT. 98 | 99 | % Define the ODE as a function handle 100 | ODE_capital = @(t, K) p.Inv - p.delta * K; 101 | 102 | K_numerical2 = zeros(p.I,1); 103 | 104 | % Call ode45 to solve the ODE 105 | [t, K_numerical2] = ode45(ODE_capital, t, p.K0); 106 | 107 | % Plot both solutions for comparison 108 | figure; 109 | plot(t, K_numerical, 'bo--', 'LineWidth', 2); hold on; 110 | plot(t, K_numerical2, 'go--', 'LineWidth', 1.5); 111 | xlabel('Time', 'FontSize', 11); 112 | ylabel('Capital (K)', 'FontSize', 11); 113 | legend('FD Scheme', 'ODE45', 'FontSize', 12); 114 | title('Comparison of FD Scheme and ODE45', 'FontSize', 15); 115 | grid on; 116 | -------------------------------------------------------------------------------- /sections/codes/week1/define_parameters.m: -------------------------------------------------------------------------------- 1 | function p = define_parameters() 2 | 3 | % This function defines the parameters needed for the capital_accumulation.m script 4 | 5 | %% Economic Parameters 6 | 7 | % Depreciation rate 8 | p.delta = 0.05; 9 | 10 | % Exogenous constant investment 11 | p.Inv = 5; 12 | 13 | %% Boundary Conditions 14 | 15 | % Initial capital 16 | p.K0 = 10; 17 | 18 | %% Grid Paramters 19 | 20 | % The minimum time value in the time domain (start of the simulation) 21 | p.tmin = 0; 22 | 23 | % The maximum time value in the time domain (end of the simulation) 24 | p.tmax = 300; 25 | 26 | % The number of time steps 27 | p.I = 100; 28 | 29 | end -------------------------------------------------------------------------------- /sections/codes/week2/ramsey/define_parameters.m: -------------------------------------------------------------------------------- 1 | function p = define_parameters() 2 | 3 | % This function defines the parameters needed for the ramsey.m script 4 | 5 | %% Economic Parameters 6 | 7 | % Discount rate 8 | p.rho = 0.03; 9 | 10 | % Inverse of IES 11 | p.theta = 1; 12 | 13 | % Technology growth rate 14 | p.g = 0.02; 15 | 16 | % Population growth rate 17 | p.n = 0.02; 18 | 19 | % Capital share 20 | p.alpha = 1/3; 21 | 22 | % TFP 23 | p.A = 1; 24 | 25 | %% Economic Functions 26 | 27 | % Production function 28 | p.f = @(k) p.A * k.^p.alpha; 29 | 30 | % MPK 31 | p.f_prime = @(k) p.alpha * p.A * k.^(p.alpha - 1); 32 | 33 | %% Boundary Conditions 34 | 35 | % Initial capital 36 | p.k0 = 10; 37 | 38 | %% Grid Paramters 39 | 40 | p.tmin = 0; 41 | p.tmax = 100; 42 | 43 | % The number of time steps 44 | p.I = 300; 45 | 46 | %% Newton Method Tuning Parameters 47 | 48 | % Tolerance for Newton's method 49 | p.tol = 1e-6; 50 | 51 | % Maximum iterations for Newton's method 52 | % p.maxit = 1000; 53 | 54 | end -------------------------------------------------------------------------------- /sections/codes/week2/ramsey/forward_simulate.m: -------------------------------------------------------------------------------- 1 | function [k, c] = forward_simulate(c0, k0, f, f_prime, rho, theta, g, n, dt, I) 2 | 3 | % This function solves the two differential equations using forward simulation. 4 | 5 | % Pre-allocate arrays for solution 6 | 7 | k = zeros(I, 1); 8 | c = zeros(I, 1); 9 | k(1) = k0; 10 | c(1) = c0; 11 | 12 | for i = 1:I-1 13 | 14 | % Euler equation for consumption growth: (c(i+1)-c(i))/dt = c(i)*(f'(k(i)-rho-theta*g)/theta 15 | c(i+1) = c(i) + dt * (f_prime(k(i)) - rho - theta * g) / theta * c(i); 16 | 17 | % Capital accumulation equation:(k(i+1)-k(i))/dt = f(k(i))-c(i)-(n+g)k(i) 18 | k(i+1) = k(i) + dt * (f(k(i)) - c(i) - (n + g) * k(i)); 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /sections/codes/week2/ramsey/ramsey.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Script: ramsey.m 3 | % 4 | % Author: Kiyea Jin 5 | % Date: October 15, 2024 6 | % 7 | % Description: 8 | % This script solves the Ramsey-Cass-Koopmans model 9 | % using FD scheme with shooting algorithm. 10 | % 11 | % The model consists of two equations: 12 | % (dc/dt)/c = (f'(k) - rho - theta*g)/theta 13 | % dk/dt = f(k) - c - (n+g)k 14 | % where an initial condition for capital K0 is provided, 15 | % and intertemporal budget constraint with equality is imposed as a terminal condition. 16 | % 17 | % Parameters: 18 | % - Discount rate (rho): 0.03 19 | % - Inverse of IES (theta): 1 20 | % - Technology growth rate (g): 0.02 21 | % - Population growth rate (n): 0.02 22 | % - Capital share (alpha): 1/3 23 | % - TFP (A): 1 24 | % - Initial boundary condition: K0 = 10 25 | % 26 | % Code Structure: 27 | % 1. DEFINE PARAMETERS 28 | % 2. INITIALIZE GRID POINTS 29 | % 3. STEADY STATES 30 | % 4. SHOOTING ALGORITHM 31 | % 5. PLOT 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 33 | 34 | clear all; 35 | close all; 36 | clc; 37 | 38 | %% 1. DEFINE PARAMETERS 39 | 40 | p = define_parameters(); 41 | 42 | %% 2. INITIALIZE GRID POINTS 43 | 44 | t = linspace(p.tmin, p.tmax, p.I)'; 45 | dt = (p.tmax-p.tmin)/(p.I-1); 46 | 47 | %% 3. STEADY STATES 48 | % kss = ((rho + theta*g)/(alpha*A))^(1/(alpha-1)) 49 | % css = Ak^alpha - (n+g)*kss 50 | 51 | kss = ((p.rho + p.theta * p.g) / (p.alpha * p.A))^(1 / (p.alpha - 1)); 52 | css = p.f(kss) - (p.n+p.g) * kss; 53 | 54 | %% 4. SHOOTING ALGORITHM 55 | 56 | % 4-1. Find the initial value of consumption that satisfies terminal boundary condition 57 | 58 | tic; 59 | % Objective function that calculates the difference between terminal capital k(T) and steady-state kss 60 | % Note: k(T)-kss is a function of c0 61 | diff = @(c0) terminal_condition(c0, p.k0, kss, p.f, p.f_prime, p.rho, p.theta, p.g, p.n, dt, p.I); 62 | 63 | % Guess an initial value of consumption 64 | c0_guess = 1; 65 | 66 | % Use fsolve to find the initial consumption c0 that makes k(T) = k_ss 67 | % Note: X = fsolve(FUN, X0, options) starts at the matrix X0 and tries to solve the equations in FUN. 68 | % Set OPTIONS = optimoptions('fsolve','Algorithm','trust-region'), and then pass OPTIONS to fsolve. 69 | options = optimoptions('fsolve', 'TolFun', p.tol, 'Display', 'iter'); 70 | c0 = fsolve(diff, c0_guess, options); 71 | 72 | % 4-2. Forward simulate with the updated initial consumption 73 | 74 | [k, c] = forward_simulate(c0, p.k0, p.f, p.f_prime, p.rho, p.theta, p.g, p.n, dt, p.I); 75 | toc; 76 | 77 | %% 5. PLOT 78 | 79 | % 5-1. Evolution of capital and consumption 80 | 81 | figure; 82 | subplot(2,1,1); 83 | plot(t, k, 'r-', 'LineWidth', 2); 84 | xlabel('Time'); ylabel('Capital k(t)'); 85 | title('Capital Accumulation over Time'); 86 | 87 | subplot(2,1,2); 88 | plot(t, c, 'b-', 'LineWidth', 2); 89 | xlabel('Time'); ylabel('Consumption c(t)'); 90 | title('Consumption Growth over Time'); 91 | 92 | %% 5-2. Phase diagram 93 | 94 | % Saddle path 95 | figure; 96 | p1 = plot(k, c, 'linewidth', 2); 97 | set(gca, 'FontSize', 18) 98 | xlabel('Capital, k','FontSize', 18) 99 | ylabel('Consumption, c','FontSize',18) 100 | 101 | % dk/dt = 0 102 | k_null_c = p.f(k) - (p.n+p.g)*k; 103 | hold on 104 | p2 = plot(k, k_null_c, 'linewidth', 2); 105 | yy = get(gca, 'yLim'); 106 | 107 | % dc/dt = 0 108 | p3 = plot([kss kss], yy, 'linewidth', 2); 109 | 110 | legend1 = legend([p1,p2,p3], 'Saddle path', '\Delta k=0', '\Delta c=0'); 111 | set(legend1, 'Location', 'best', 'FontSize', 18) 112 | hold off -------------------------------------------------------------------------------- /sections/codes/week2/ramsey/terminal_condition.m: -------------------------------------------------------------------------------- 1 | function difference = terminal_condition(c0, k0, kss, f, f_prime, rho, theta, g, n, dt, I) 2 | 3 | % This function calculates the difference between terminal capital k(T) and 4 | % steady-state kss. 5 | 6 | [k, ~] = forward_simulate(c0, k0, f, f_prime, rho, theta, g, n, dt, I); 7 | k_T = k(end); % Terminal capital k(T) 8 | 9 | % The difference between terminal k(T) and steady-state k_ss 10 | difference = k_T - kss; 11 | end -------------------------------------------------------------------------------- /sections/codes/week2/ramsey_HJB/HJB_ramsey_implicit_demo.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: HJB_ramsey_implicit 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Oct 27, 2024 6 | % 7 | % Description: 8 | % This MATLAB script implements implicit method to solve the HJB equation 9 | % of the deterministic Ramsey Growth Model using mixed method. Boundary 10 | % conditions are not yet considered in this script. 11 | % 12 | % Reference: 13 | % HJB_NGM_implicit.m by Benjamin Moll 14 | % ramsey_implicit.m by Pontus Rendahl 15 | % 16 | % Notes: 17 | % - CRRA utility function: U(c) = (c^(1-gamma))/(1-gamma) 18 | % - Production function: f(k) = A*k^alpha 19 | % - Relative risk aversion coefficient (gamma): 2 20 | % - Discount rate (rho): 0.03 21 | % - Depreciation rate (delta): 0.025 22 | % - Elasticity of output with respect to capital (alpha): 1/3 23 | % - Total fator productivity (A): 1 24 | % - Delta = 1000 (Can be arbitrarily large in implicit method) 25 | % 26 | % Code Structure: 27 | % 1. DEFINE PARAMETERS 28 | % 2. INITIALIZE GRID POINTS 29 | % 3. PRE-ITERATION INITIALIZATION 30 | % 4. VALUE FUNCTION ITERATION 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | 33 | clear all; 34 | close all; 35 | clc; 36 | 37 | %% 1. DEFINE PARAMETERS 38 | 39 | p = define_parameters_demo(); 40 | 41 | %% 2. INITIALIZE GRID POINTS 42 | 43 | % Steady-state level of capital: f'(kss)=rho+delta 44 | kss = ((p.rho+p.delta)/(p.A*p.alpha))^(1/(p.alpha-1)); 45 | 46 | % log(k_min) = log(kss)-p.klim 47 | k_min = kss*exp(-p.klim); 48 | k_max = kss*exp(p.klim); 49 | 50 | k = linspace(k_min, k_max, p.I)'; 51 | dk = (k_max-k_min)/(p.I-1); 52 | 53 | %% 3. PRE-ITERATION INITIALIZATION 54 | 55 | % 3-1. Construct the differential operator D such that DV=dV 56 | 57 | D = zeros(p.I, p.I); 58 | 59 | % Forward differencing for i=1 60 | D(1,1) = -1/dk; D(1,2) = 1/dk; 61 | 62 | % Backward differencing for i=I 63 | D(end,end-1) = -1/dk; D(end,end) = 1/dk; 64 | 65 | % Central differencing for i=2,...,I-1 66 | for i = 2:p.I-1 67 | D(i,i-1) = -0.5/dk; D(i,i+1) = 0.5/dk; 68 | end 69 | 70 | % 3-2. Guess an initial value of the value function 71 | 72 | v0 = p.u(p.f(k) - p.delta*k)/p.rho; 73 | V = v0; 74 | 75 | % 3-3. Guess an initial value of the consumption 76 | % Notes: This script does not consider the boundary conditions, so we'll 77 | % impose an initial value of consumption 78 | 79 | c0 = p.f(k); 80 | c = c0; 81 | 82 | %% 4. VALUE FUNCTION ITERATION 83 | 84 | tic; 85 | 86 | for n = 1:p.maxit 87 | 88 | % 4-3. Compute the optimal savings 89 | s = p.f(k) - p.delta*k - c; 90 | 91 | % 4-4. Update the value function: V^(n+1) = [(rho+1/Delta)*I - SD]^(-1)[u(c) + 1/Delta*V^n] 92 | 93 | % B = [(rho+1/Delta)*I - SD] 94 | S = diag(s); 95 | B = (p.rho + 1/p.Delta)*eye(p.I) - S*D; 96 | 97 | % b = [u(c) + 1/Delta*V^n] 98 | b = p.u(c) + 1/p.Delta*V; 99 | 100 | % V^(n+1) = B^(-1)*b 101 | V_update = B\b; 102 | 103 | % Update the value function 104 | V_change = V_update - V; 105 | V = V_update; 106 | 107 | % 4-1. Compute the derivative of the value function 108 | dV = D*V; 109 | 110 | % 4-2. Compute the optimal consumption 111 | c = p.inv_mu(dV); 112 | 113 | % 4-5. Check convergence 114 | 115 | dist(n) = max(abs(V_change)); 116 | 117 | if dist(n) c = inv_mu(dv) 37 | p.inv_mu = @(dv) dv.^(-1/p.gamma); 38 | 39 | % Production function 40 | p.f = @(k) p.A * k.^p.alpha; 41 | 42 | %% Grid Paramters 43 | 44 | % The number of time steps 45 | p.I = 2000; 46 | 47 | % Higher klim implies broader coverage 48 | p.klim = 1.5; 49 | 50 | %% Tuning Parameters 51 | 52 | % The maximum number of iteration 53 | p.maxit = 1000; 54 | 55 | % Convergence criterion, and 56 | p.tol = 1e-8; 57 | 58 | % Step size (Delta) 59 | p.Delta = 1000; 60 | 61 | end -------------------------------------------------------------------------------- /sections/codes/week3/HJB_ramsey_implicit_mixed.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: HJB_ramsey_implicit 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 5, 2024 6 | % 7 | % Description: 8 | % This MATLAB script implements implicit method to solve the HJB equation 9 | % of the deterministic Ramsey Growth Model using mixed method. Boundary 10 | % conditions are not yet considered in this script. 11 | % 12 | % Reference: 13 | % HJB_NGM_implicit.m by Benjamin Moll 14 | % ramsey_implicit.m by Pontus Rendahl 15 | % 16 | % Notes: 17 | % - CRRA utility function: U(c) = (c^(1-gamma))/(1-gamma) 18 | % - Production function: f(k) = A*k^alpha 19 | % - Relative risk aversion coefficient (gamma): 2 20 | % - Discount rate (rho): 0.03 21 | % - Depreciation rate (delta): 0.025 22 | % - Elasticity of output with respect to capital (alpha): 1/3 23 | % - Total fator productivity (A): 1 24 | % - Delta = 1000 (Can be arbitrarily large in implicit method) 25 | % 26 | % Code Structure: 27 | % 1. DEFINE PARAMETERS 28 | % 2. INITIALIZE GRID POINTS 29 | % 3. PRE-ITERATION INITIALIZATION 30 | % 4. VALUE FUNCTION ITERATION 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | 33 | clear all; 34 | close all; 35 | clc; 36 | 37 | %% 1. DEFINE PARAMETERS 38 | 39 | p = define_parameters(); 40 | 41 | %% 2. INITIALIZE GRID POINTS 42 | 43 | % Steady-state level of capital: f'(kss)=rho+delta 44 | kss = ((p.rho+p.delta)/(p.A*p.alpha))^(1/(p.alpha-1)); 45 | 46 | % log(k_min) = log(kss)-p.klim 47 | k_min = kss*exp(-p.klim); 48 | k_max = kss*exp(p.klim); 49 | 50 | k = linspace(k_min, k_max, p.I)'; 51 | dk = (k_max-k_min)/(p.I-1); 52 | 53 | %% 3. PRE-ITERATION INITIALIZATION 54 | 55 | % 3-1. Construct the differential operator D such that DV=dV 56 | 57 | D = zeros(p.I, p.I); 58 | 59 | % Forward differencing for i=1 60 | D(1,1) = -1/dk; D(1,2) = 1/dk; 61 | 62 | % Backward differencing for i=I 63 | D(end,end-1) = -1/dk; D(end,end) = 1/dk; 64 | 65 | % Central differencing for i=2,...,I-1 66 | for i = 2:p.I-1 67 | D(i,i-1) = -0.5/dk; D(i,i+1) = 0.5/dk; 68 | end 69 | 70 | % 3-2. Guess an initial value of the value function 71 | 72 | v0 = p.u(p.f(k))/p.rho; 73 | V = v0; 74 | 75 | %% 4. VALUE FUNCTION ITERATION 76 | 77 | tic; 78 | 79 | for n = 1:p.maxit 80 | 81 | % 4-1. Compute the derivative of the value function 82 | dV = D*V; 83 | 84 | % 4-2. Compute the optimal consumption 85 | c = p.inv_mu(dV); 86 | 87 | % 4-3. Compute the optimal savings 88 | s = p.f(k) - p.delta*k - c; 89 | 90 | % 4-4. Update the value function: V^(n+1) = [(rho+1/Delta)*I - SD]^(-1)[u(c) + 1/Delta*V^n] 91 | 92 | % B = [(rho+1/Delta)*I - SD] 93 | S = diag(s); 94 | B = (p.rho + 1/p.Delta)*eye(p.I) - S*D; 95 | 96 | % b = [u(c) + 1/Delta*V^n] 97 | b = p.u(c) + 1/p.Delta*V; 98 | 99 | % V^(n+1) = B^(-1)*b 100 | V_update = B\b; 101 | 102 | % Update the value function 103 | V_change = V_update - V; 104 | V = V_update; 105 | 106 | % 4-5. Check convergence 107 | 108 | dist(n) = max(abs(V_change)); 109 | 110 | if dist(n)=k_min is enforced which helps stability of the algorithm 94 | 95 | % 4-2. Compute the optimal consumption 96 | cf = p.inv_mu(dVf); 97 | cb = p.inv_mu(dVb); 98 | 99 | % 4-3. Compute the optimal savings 100 | sf = p.f(k) - p.delta*k - cf; 101 | sb = p.f(k) - p.delta*k - cb; 102 | 103 | % UPWIND SCHEME 104 | If = sf>0; 105 | Ib = sb<0; 106 | I0 = 1-If-Ib; 107 | dV0 = p.mu(p.f(k) - p.delta*k); 108 | 109 | dV_upwind = dVf.*If + dVb.*Ib + dV0.*I0; 110 | 111 | c = p.inv_mu(dV_upwind); 112 | 113 | % 4-4. Update the value function: V^(n+1) = [(rho+1/Delta)*I - SD]^(-1)[u(c) + 1/Delta*V^n] 114 | 115 | % B = [(rho+1/Delta)*I - SD] 116 | Sf = diag(sf.*If); 117 | Sb = diag(sb.*Ib); 118 | SD = Sf*Df + Sb*Db; 119 | 120 | B = (p.rho + 1/p.Delta)*eye(p.I) - SD; 121 | 122 | % b = [u(c) + 1/Delta*V^n] 123 | b = p.u(c) + 1/p.Delta*V; 124 | 125 | % V^(n+1) = B^(-1)*b 126 | V_update = B\b; 127 | 128 | % Update the value function 129 | V_change = V_update - V; 130 | V = V_update; 131 | 132 | % 4-5. Check convergence 133 | 134 | dist(n) = max(abs(V_change)); 135 | 136 | if dist(n) c = inv_mu(dv) 37 | p.inv_mu = @(dv) dv.^(-1/p.gamma); 38 | 39 | % Production function 40 | p.f = @(k) p.A * k.^p.alpha; 41 | 42 | %% Grid Paramters 43 | 44 | % The number of grid points 45 | p.I = 2000; 46 | 47 | % Higher klim implies broader coverage 48 | p.klim = 1.5; 49 | 50 | %% Tuning Parameters 51 | 52 | % The maximum number of iteration 53 | p.maxit = 1000; 54 | 55 | % Convergence criterion, and 56 | p.tol = 1e-8; 57 | 58 | % Step size (Delta) 59 | p.Delta = 1000; 60 | 61 | end -------------------------------------------------------------------------------- /sections/codes/week4/Asset Supply/Huggett_assetsupply.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: HJB_Huggett_assetsupply 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 7, 2024 6 | % 7 | % Description: 8 | % This MATLAB script solves the HJB equation and the KF equation 9 | % of the Huggett model. Also, it computes the asset supply S(r). 10 | % 11 | % Reference: Huggett_asset_supply.m by Benjamin Moll 12 | % 13 | % Notes: 14 | % - CRRA utility function: U(c) = (c^(1-sigma))/(1-sigma) 15 | % - Elasticity of intertemporal substitution (sigma): 2 16 | % - Discrete grid of interest rates levels (rgrid): -0.05 to 0.04 17 | % - Discount rate (rho): 0.05 18 | % - Income: z = [z_u, z_e] = [0.1, 0.2]; 19 | % - Lambda: la = [la_u, la_e] = [1.2, 1.2]; 20 | % - Discrete grid of asset levels (a): -0.15 to 5 21 | % - Borrowing constraint: a>=-0.15 22 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 23 | % 24 | % Code Structure: 25 | % 1. DEFINE PARAMETERS 26 | % 2. INITIALIZE GRID POINTS 27 | % 3. PRE-ITERATION INITIALIZATION 28 | % 4. VALUE FUNCTION ITERATION 29 | % 5. KF EQUATION 30 | % 6. GRAPHS 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | 33 | clear all; 34 | close all; 35 | clc; 36 | 37 | %% 1. DEFINE PARAMETERS 38 | 39 | p = define_parameters_assetsupply(); 40 | 41 | %% 2. INITIALIZE GRID POINTS 42 | 43 | a = linspace(p.amin, p.amax, p.I)'; 44 | da = (p.amax-p.amin)/(p.I-1); 45 | 46 | aa = [a, a]; % I*2 matrix 47 | 48 | %% 2-2. INITIALIZE GRID POINTS FOR INTEREST RATES 49 | 50 | rgrid = linspace(p.rmin, p.rmax, p.Ir)'; 51 | 52 | %% 3. PRE-ITERATION INITIALIZATION 53 | 54 | % 3-1. Construct the forward and backward differential operator 55 | % Df such that Df*V=dVf and Db such that Db*V=dVb 56 | 57 | Df = zeros(p.I, p.I); 58 | for i = 1:p.I-1 59 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 60 | end 61 | Df = sparse(Df); 62 | 63 | Db = zeros(p.I, p.I); 64 | for i = 2:p.I 65 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 66 | end 67 | Db = sparse(Db); 68 | 69 | % 3-2. Construct A_switch matrix 70 | 71 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 72 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 73 | 74 | %A_switch = zeros(2*I, 2*I); 75 | %for i=1:I 76 | % A_switch(i,i) = -lambda(1); 77 | % A_switch(i,i+I) = lambda(1); 78 | % A_switch(i+I,i) = lambda(2); 79 | % A_switch(i+I,i+I) = -lambda(2); 80 | %end 81 | 82 | %% 3-3. Guess an initial value of the value function 83 | 84 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 85 | 86 | % The value function of "staying put" 87 | r = rgrid(1); 88 | 89 | v0 = p.u(zz + max(r, 0.01).*aa)./p.rho; 90 | V = v0; 91 | 92 | %% 4. VALUE FUNCTION ITERATION 93 | 94 | for ir=1:p.Ir 95 | 96 | r = rgrid(ir); 97 | 98 | % Use the value function solution from the previous interest rate iteration 99 | % as the initial guess for the next iteration 100 | if ir>1 101 | v0 = V_r(:,:,ir-1); 102 | V = v0; 103 | end 104 | 105 | for n=1:p.maxit 106 | 107 | % 4-1. Compute the derivative of the value function 108 | dVf = Df*V; 109 | dVb = Db*V; 110 | 111 | % 4-2. Boundary conditions 112 | dVb(1,:) = p.mu(zz(1,:) + r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 113 | dVf(end,:) = p.mu(zz(end,:) + r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 114 | 115 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 116 | 117 | % 4-3. Compute the optimal consumption 118 | cf = p.inv_mu(dVf); 119 | cb = p.inv_mu(dVb); 120 | 121 | % 4-4. Compute the optimal savings 122 | sf = zz + r.*aa - cf; 123 | sb = zz + r.*aa - cb; 124 | 125 | % 4-5. Upwind scheme 126 | If = sf>0; 127 | Ib = sb<0; 128 | I0 = 1-If-Ib; 129 | dV0 = p.mu(zz + r.*aa); % If sf<=0<=sb, set s=0 130 | 131 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 132 | 133 | c = p.inv_mu(dV_upwind); 134 | 135 | % 4-6. Update value function: 136 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 137 | 138 | V_stacked = V(:); % 2I*1 matrix 139 | c_stacked = c(:); % 2I*1 matrix 140 | 141 | % A = SD 142 | SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 143 | SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 144 | SD = [SD_u, sparse(p.I, p.I); 145 | sparse(p.I, p.I), SD_e]; % 2I*2I matrix 146 | 147 | % P = A + A_switch 148 | P = SD + A_switch; 149 | 150 | % B = [(rho + 1/Delta)*I - P] 151 | B = (p.rho + 1/p.Delta)*speye(2*p.I) - P; 152 | 153 | % b = u(c) + 1/Delta*V 154 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 155 | 156 | % V = B\b; 157 | V_update = B\b; % 2I*1 matrix 158 | V_change = V_update - V_stacked; 159 | V = reshape(V_update, p.I, 2); % I*2 matrix 160 | 161 | % 3-6. Convergence criterion 162 | dist(n) = max(abs(V_change)); 163 | if dist(n) c=inv_mu(dV) 36 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 37 | 38 | %% Grid Parmaters 39 | 40 | p.amin = -0.15; 41 | p.amax = 5; 42 | 43 | % The number of grid points 44 | p.I = 1000; 45 | 46 | % Grid parameters for interest rate 47 | p.rmin = -0.05; 48 | p.rmax = 0.04; 49 | p.Ir = 20; 50 | 51 | %% Tuning parameters 52 | 53 | % Step size: can be arbitrarily large in implicit method 54 | p.Delta = 1000; 55 | 56 | % The maximum number of value function iterations 57 | p.maxit = 100; 58 | 59 | % Tolerance for value function iterations 60 | p.tol = 10^(-6); 61 | 62 | end -------------------------------------------------------------------------------- /sections/codes/week4/GE/Huggett_GE.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: HJB_Huggett_GE 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 7, 2024 6 | % 7 | % Description: 8 | % This MATLAB script solves the general equilibrium of the Huggett model, 9 | % finding the equilibrium interest rate that clears the bond market. 10 | % 11 | % Reference: Huggett_equilibrium_iterate.m by Benjamin Moll 12 | % 13 | % Notes: 14 | % - CRRA utility function: U(c) = (c^(1-sigma))/(1-sigma) 15 | % - Elasticity of intertemporal substitution (sigma): 2 16 | % - Discount rate (rho): 0.05 17 | % - Income: z = [z_u, z_e] = [0.1, 0.2]; 18 | % - Lambda: la = [la_u, la_e] = [1.2, 1.2]; 19 | % - Discrete grid of asset levels (a): -0.15 to 5 20 | % - Borrowing constraint: a>=-0.15 21 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 22 | % 23 | % Code Structure: 24 | % 1. DEFINE PARAMETERS 25 | % 2. INITIALIZE GRID POINTS 26 | % 3. PRE-ITERATION INITIALIZATION 27 | % 4. VALUE FUNCTION ITERATION 28 | % 5. KF EQUATION 29 | % 6. GRAPHS 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 31 | 32 | clear all; 33 | close all; 34 | clc; 35 | 36 | %% 1. DEFINE PARAMETERS 37 | 38 | p = define_parameters_GE(); 39 | 40 | %% 2. INITIALIZE GRID POINTS 41 | 42 | a = linspace(p.amin, p.amax, p.I)'; 43 | da = (p.amax-p.amin)/(p.I-1); 44 | 45 | aa = [a, a]; % I*2 matrix 46 | 47 | % %% 2-2. INITIALIZE GRID POINTS FOR INTEREST RATES 48 | % 49 | % rgrid = linspace(p.rmin, p.rmax, p.Ir)'; 50 | 51 | %% 3. PRE-ITERATION INITIALIZATION 52 | 53 | % 3-1. Construct the forward and backward differential operator 54 | % Df such that Df*V=dVf and Db such that Db*V=dVb 55 | 56 | Df = zeros(p.I, p.I); 57 | for i = 1:p.I-1 58 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 59 | end 60 | Df = sparse(Df); 61 | 62 | Db = zeros(p.I, p.I); 63 | for i = 2:p.I 64 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 65 | end 66 | Db = sparse(Db); 67 | 68 | % 3-2. Construct A_switch matrix 69 | 70 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 71 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 72 | 73 | %A_switch = zeros(2*I, 2*I); 74 | %for i=1:I 75 | % A_switch(i,i) = -lambda(1); 76 | % A_switch(i,i+I) = lambda(1); 77 | % A_switch(i+I,i) = lambda(2); 78 | % A_switch(i+I,i+I) = -lambda(2); 79 | %end 80 | 81 | %% 3-3. Guess an initial value of the interest rate 82 | 83 | r0 = 0.03; 84 | r_min = 0.01; 85 | r_max = 0.04; 86 | 87 | %% 3-4. Guess an initial value of the value function 88 | 89 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 90 | 91 | % The value function of "staying put" 92 | r = r0; 93 | 94 | v0 = p.u(zz + r.*aa)./p.rho; 95 | V = v0; 96 | 97 | %% 4. VALUE FUNCTION ITERATION 98 | 99 | for nr=1:p.Nr 100 | 101 | r_r(nr) = r; 102 | rmin_r(nr) = r_min; 103 | rmax_r(nr) = r_max; 104 | 105 | % Use the value function solution from the previous interest rate iteration 106 | % as the initial guess for the next iteration 107 | if nr>1 108 | v0 = V_r(:,:,nr-1); 109 | V = v0; 110 | end 111 | 112 | for n=1:p.maxit 113 | 114 | % 4-1. Compute the derivative of the value function 115 | dVf = Df*V; 116 | dVb = Db*V; 117 | 118 | % 4-2. Boundary conditions 119 | dVb(1,:) = p.mu(zz(1,:) + r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 120 | dVf(end,:) = p.mu(zz(end,:) + r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 121 | 122 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 123 | 124 | % 4-3. Compute the optimal consumption 125 | cf = p.inv_mu(dVf); 126 | cb = p.inv_mu(dVb); 127 | 128 | % 4-4. Compute the optimal savings 129 | sf = zz + r.*aa - cf; 130 | sb = zz + r.*aa - cb; 131 | 132 | % 4-5. Upwind scheme 133 | If = sf>0; 134 | Ib = sb<0; 135 | I0 = 1-If-Ib; 136 | dV0 = p.mu(zz + r.*aa); % If sf<=0<=sb, set s=0 137 | 138 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 139 | 140 | c = p.inv_mu(dV_upwind); 141 | 142 | % 4-6. Update value function: 143 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 144 | 145 | V_stacked = V(:); % 2I*1 matrix 146 | c_stacked = c(:); % 2I*1 matrix 147 | 148 | % A = SD 149 | SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 150 | SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 151 | SD = [SD_u, sparse(p.I, p.I); 152 | sparse(p.I, p.I), SD_e]; % 2I*2I matrix 153 | 154 | % P = A + A_switch 155 | P = SD + A_switch; 156 | 157 | % B = [(rho + 1/Delta)*I - P] 158 | B = (p.rho + 1/p.Delta)*speye(2*p.I) - P; 159 | 160 | % b = u(c) + 1/Delta*V 161 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 162 | 163 | % V = B\b; 164 | V_update = B\b; % 2I*1 matrix 165 | V_change = V_update - V_stacked; 166 | V = reshape(V_update, p.I, 2); % I*2 matrix 167 | 168 | % 3-6. Convergence criterion 169 | dist(n) = max(abs(V_change)); 170 | if dist(n)p.tol_S 216 | disp('Excess Supply') 217 | % Decrease r whenever S(r)>0 218 | r_max = r; 219 | r = (r_min+r_max)/2; 220 | elseif S(nr)<-p.tol_S 221 | disp('Excess Demand') 222 | % Increase r whenever S(r)<0 223 | r_min = r; 224 | r = (r_min+r_max)/2; 225 | elseif abs(S(nr)) c=inv_mu(dV) 36 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 37 | 38 | %% Grid Parmaters 39 | 40 | p.amin = -0.15; 41 | p.amax = 5; 42 | 43 | % The number of grid points 44 | p.I = 1000; 45 | 46 | % % WE NO LONGER CONSTRUCT GRID POINTS FOR INTEREST RATES 47 | % % Grid parameters for interest rate 48 | % p.rmin = -0.05; 49 | % p.rmax = 0.04; 50 | % p.Ir = 20; 51 | 52 | %% Tuning parameters 53 | 54 | % Step size: can be arbitrarily large in implicit method 55 | p.Delta = 1000; 56 | 57 | % The maximum number of value function iterations 58 | p.maxit = 100; 59 | 60 | % Tolerance for value function iterations 61 | p.tol = 10^(-6); 62 | 63 | %% TUNING PARAMETERS FOR INTEREST RATE ITERATION 64 | 65 | % The maximum number of interest rate iterations 66 | p.Nr = 1000; 67 | 68 | % Tolerance for interest rate iterations 69 | p.tol_S = 10^(-5); 70 | 71 | end -------------------------------------------------------------------------------- /sections/codes/week4/HJB/HJB_Huggett.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: HJB_Huggett 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 2, 2024 6 | % 7 | % Description: 8 | % This MATLAB script implements the implicit method to solve the HJB 9 | % equations of the Huggett Model using upwind scheme. 10 | % 11 | % Reference: HJB_stateconstraint_implicit.m by Benjamin Moll 12 | % 13 | % Notes: 14 | % - CRRA utility function: U(c) = (c^(1-sigma))/(1-sigma) 15 | % - Elasticity of intertemporal substitution (sigma): 2 16 | % - Interest rate (r) : 0.03 17 | % - Discount rate (rho): 0.05 18 | % - Income: z = [z_u, z_e] = [0.1, 0.2]; 19 | % - Lambda: la = [la_u, la_e] = [0.02, 0.03]; 20 | % - Discrete grid of asset levels (a): -0.02 to 2 21 | % - Borrowing constraint: a>=-0.02 22 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 23 | % 24 | % Code Structure: 25 | % 1. DEFINE PARAMETERS 26 | % 2. INITIALIZE GRID POINTS 27 | % 3. PRE-ITERATION INITIALIZATION 28 | % 4. VALUE FUNCTION ITERATION 29 | % 5. GRAPHS 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 31 | 32 | clear all; 33 | close all; 34 | clc; 35 | 36 | %% 1. DEFINE PARAMETERS 37 | 38 | p = define_parameters(); 39 | 40 | %% 2. INITIALIZE GRID POINTS 41 | 42 | a = linspace(p.amin, p.amax, p.I)'; 43 | da = (p.amax-p.amin)/(p.I-1); 44 | 45 | aa = [a, a]; % I*2 matrix 46 | 47 | %% 3. PRE-ITERATION INITIALIZATION 48 | 49 | % 3-1. Construct the forward and backward differential operator 50 | % Df such that Df*V=dVf and Db such that Db*V=dVb 51 | 52 | Df = zeros(p.I, p.I); 53 | for i = 1:p.I-1 54 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 55 | end 56 | Df = sparse(Df); 57 | 58 | Db = zeros(p.I, p.I); 59 | for i = 2:p.I 60 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 61 | end 62 | Db = sparse(Db); 63 | 64 | % 3-2. Construct A_switch matrix 65 | 66 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 67 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 68 | 69 | %A_switch = zeros(2*I, 2*I); 70 | %for i=1:I 71 | % A_switch(i,i) = -lambda(1); 72 | % A_switch(i,i+I) = lambda(1); 73 | % A_switch(i+I,i) = lambda(2); 74 | % A_switch(i+I,i+I) = -lambda(2); 75 | %end 76 | 77 | % 3-3. Guess an initial value of the value function 78 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 79 | 80 | % The value function of "staying put" 81 | v0 = p.u(zz + p.r.*aa)./p.rho; 82 | V = v0; 83 | 84 | %% 4. VALUE FUNCTION ITERATION 85 | 86 | for n=1:p.maxit 87 | 88 | % 4-1. Compute the derivative of the value function 89 | dVf = Df*V; 90 | dVb = Db*V; 91 | 92 | % 4-2. Boundary conditions 93 | dVb(1,:) = p.mu(zz(1,:) + p.r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 94 | dVf(end,:) = p.mu(zz(end,:) + p.r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 95 | 96 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 97 | 98 | % 4-3. Compute the optimal consumption 99 | cf = p.inv_mu(dVf); 100 | cb = p.inv_mu(dVb); 101 | 102 | % 4-4. Compute the optimal savings 103 | sf = zz + p.r.*aa - cf; 104 | sb = zz + p.r.*aa - cb; 105 | 106 | % 4-5. Upwind scheme 107 | If = sf>0; 108 | Ib = sb<0; 109 | I0 = 1-If-Ib; 110 | dV0 = p.mu(zz + p.r.*aa); % If sf<=0<=sb, set s=0 111 | 112 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 113 | 114 | c = p.inv_mu(dV_upwind); 115 | 116 | % 4-6. Update value function: 117 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 118 | 119 | V_stacked = V(:); % 2I*1 matrix 120 | c_stacked = c(:); % 2I*1 matrix 121 | 122 | % A = SD 123 | SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 124 | SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 125 | SD = [SD_u, sparse(p.I, p.I); 126 | sparse(p.I, p.I), SD_e]; % 2I*2I matrix 127 | 128 | % P = A + A_switch 129 | P = SD + A_switch; 130 | 131 | % B = [(rho + 1/Delta)*I - P] 132 | B = (p.rho + 1/p.Delta)*speye(2*p.I) - P; 133 | 134 | % b = u(c) + 1/Delta*V 135 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 136 | 137 | % V = B\b; 138 | V_update = B\b; % 2I*1 matrix 139 | V_change = V_update - V_stacked; 140 | V = reshape(V_update, p.I, 2); % I*2 matrix 141 | 142 | % 3-6. Convergence criterion 143 | dist(n) = max(abs(V_change)); 144 | if dist(n) c = inv_mu(dv) 41 | p.inv_mu = @(dv) dv.^(-1/p.sigma); 42 | 43 | %% Grid Paramters 44 | 45 | % The lower bound of the state space (borrowing constraint) 46 | p.amin = -0.02; 47 | 48 | % The upper bound of the state space 49 | p.amax = 2; 50 | 51 | % The number of grid points 52 | p.I = 500; 53 | 54 | %% Tuning Parameters 55 | 56 | % The maximum number of iteration 57 | p.maxit = 100; 58 | 59 | % Convergence criterion 60 | p.tol = 1e-8; 61 | 62 | % Step size (Delta) 63 | p.Delta = 1000; 64 | 65 | end -------------------------------------------------------------------------------- /sections/codes/week4/KF/Huggett_PE.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: HJB_Huggett_PE 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 7, 2024 6 | % 7 | % Description: 8 | % This MATLAB script solves the HJB equation and the KF equation 9 | % of the Huggett model. 10 | % 11 | % Reference: Huggett_partialeq.m by Benjamin Moll 12 | % 13 | % Notes: 14 | % - CRRA utility function: U(c) = (c^(1-sigma))/(1-sigma) 15 | % - Elasticity of intertemporal substitution (sigma): 1.2 16 | % - Interest rate (r) : 0.035 17 | % - Discount rate (rho): 0.05 18 | % - Income: z = [z_u, z_e] = [0.1, 0.2]; 19 | % - Lambda: la = [la_u, la_e] = [1.5, 1]; 20 | % - Discrete grid of asset levels (a): -0.02 to 2 21 | % - Borrowing constraint: a>=-0.02 22 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 23 | % 24 | % Code Structure: 25 | % 1. DEFINE PARAMETERS 26 | % 2. INITIALIZE GRID POINTS 27 | % 3. PRE-ITERATION INITIALIZATION 28 | % 4. VALUE FUNCTION ITERATION 29 | % 5. KF EQUATION 30 | % 6. GRAPHS 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | 33 | clear all; 34 | close all; 35 | clc; 36 | 37 | %% 1. DEFINE PARAMETERS 38 | 39 | p = define_parameters(); 40 | 41 | %% 2. INITIALIZE GRID POINTS 42 | 43 | a = linspace(p.amin, p.amax, p.I)'; 44 | da = (p.amax-p.amin)/(p.I-1); 45 | 46 | aa = [a, a]; % I*2 matrix 47 | 48 | %% 3. PRE-ITERATION INITIALIZATION 49 | 50 | % 3-1. Construct the forward and backward differential operator 51 | % Df such that Df*V=dVf and Db such that Db*V=dVb 52 | 53 | Df = zeros(p.I, p.I); 54 | for i = 1:p.I-1 55 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 56 | end 57 | Df = sparse(Df); 58 | 59 | Db = zeros(p.I, p.I); 60 | for i = 2:p.I 61 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 62 | end 63 | Db = sparse(Db); 64 | 65 | % 3-2. Construct A_switch matrix 66 | 67 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 68 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 69 | 70 | %A_switch = zeros(2*I, 2*I); 71 | %for i=1:I 72 | % A_switch(i,i) = -lambda(1); 73 | % A_switch(i,i+I) = lambda(1); 74 | % A_switch(i+I,i) = lambda(2); 75 | % A_switch(i+I,i+I) = -lambda(2); 76 | %end 77 | 78 | % 3-3. Guess an initial value of the value function 79 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 80 | 81 | % The value function of "staying put" 82 | v0 = p.u(zz + p.r.*aa)./p.rho; 83 | V = v0; 84 | 85 | %% 4. VALUE FUNCTION ITERATION 86 | 87 | for n=1:p.maxit 88 | 89 | % 4-1. Compute the derivative of the value function 90 | dVf = Df*V; 91 | dVb = Db*V; 92 | 93 | % 4-2. Boundary conditions 94 | dVb(1,:) = p.mu(zz(1,:) + p.r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 95 | dVf(end,:) = p.mu(zz(end,:) + p.r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 96 | 97 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 98 | 99 | % 4-3. Compute the optimal consumption 100 | cf = p.inv_mu(dVf); 101 | cb = p.inv_mu(dVb); 102 | 103 | % 4-4. Compute the optimal savings 104 | sf = zz + p.r.*aa - cf; 105 | sb = zz + p.r.*aa - cb; 106 | 107 | % 4-5. Upwind scheme 108 | If = sf>0; 109 | Ib = sb<0; 110 | I0 = 1-If-Ib; 111 | dV0 = p.mu(zz + p.r.*aa); % If sf<=0<=sb, set s=0 112 | 113 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 114 | 115 | c = p.inv_mu(dV_upwind); 116 | 117 | % 4-6. Update value function: 118 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 119 | 120 | V_stacked = V(:); % 2I*1 matrix 121 | c_stacked = c(:); % 2I*1 matrix 122 | 123 | % A = SD 124 | SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 125 | SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 126 | SD = [SD_u, sparse(p.I, p.I); 127 | sparse(p.I, p.I), SD_e]; % 2I*2I matrix 128 | 129 | % P = A + A_switch 130 | P = SD + A_switch; 131 | 132 | % B = [(rho + 1/Delta)*I - P] 133 | B = (p.rho + 1/p.Delta)*speye(2*p.I) - P; 134 | 135 | % b = u(c) + 1/Delta*V 136 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 137 | 138 | % V = B\b; 139 | V_update = B\b; % 2I*1 matrix 140 | V_change = V_update - V_stacked; 141 | V = reshape(V_update, p.I, 2); % I*2 matrix 142 | 143 | % 3-6. Convergence criterion 144 | dist(n) = max(abs(V_change)); 145 | if dist(n) c=inv_mu(dV) 35 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 36 | 37 | %% Grid Parmaters 38 | 39 | p.amin = -0.02; 40 | p.amax = 3; 41 | 42 | % The number of grid points 43 | p.I = 500; 44 | 45 | %% Tuning parameters 46 | 47 | % Step size: can be arbitrarily large in implicit method 48 | p.Delta = 1000; 49 | 50 | % The maximum number of value function iterations 51 | p.maxit = 100; 52 | 53 | % Tolerance for value function iterations 54 | p.tol = 10^(-6); 55 | 56 | end -------------------------------------------------------------------------------- /sections/codes/week5/Andreas's Repository/GE/HJB.m: -------------------------------------------------------------------------------- 1 | function [V, c, s, P] = HJB(r, G, p) 2 | 3 | %% 3. PRE-ITERATION INITIALIZATION 4 | 5 | % 3-1. Construct the forward and backward differential operator 6 | % Df such that Df*V=dVf and Db such that Db*V=dVb 7 | % 8 | % Df = zeros(p.I, p.I); 9 | % for i = 1:p.I-1 10 | % Df(i,i) = -1/da; Df(i,i+1) = 1/da; 11 | % end 12 | % Df = sparse(Df); 13 | % 14 | % Db = zeros(p.I, p.I); 15 | % for i = 2:p.I 16 | % Db(i,i-1) = -1/da; Db(i,i) = 1/da; 17 | % end 18 | % Db = sparse(Db); 19 | 20 | %% DIFFERENTIAL OPERATOR + BOUNDARY CONDITIONS 21 | 22 | %{ 23 | gen_FD.m: Constructs FD operators for given boundary conditions 24 | 25 | function G = gen_FD(G, BC, name) 26 | 27 | INPUTS: 28 | - G: Grid struct 29 | - BC: Boundary condition inputs. This is a (d x 1) cell, where each 30 | element is a struct with the following properties 31 | - left.type: type of left boundary condition 32 | - right.type: type of right boundary condition 33 | - left.f: values associated with left boundary condition 34 | - right.f: values associated with right boundary condition 35 | - name: (Optional) name associated with boundary condition, useful when 36 | using multiple boundary conditions for the same grid 37 | %} 38 | 39 | G.income = p.zz + r.*G.a; 40 | 41 | left_bound = p.mu(G.income(G.a == p.amin, :)); 42 | right_bound = p.mu(G.income(G.a == p.amax, :)); 43 | for j = 1:2 44 | BC{1}.left.type = 'VNB'; BC{1}.right.type = 'VNF'; % Von Nuemann Boundary Condition 45 | BC{1}.left.f = @(points) left_bound(j) * ones(size(points, 1), 1); 46 | BC{1}.right.f = @(points) right_bound(j) * ones(size(points, 1), 1); 47 | G = gen_FD(G, BC, num2str(j)); 48 | end 49 | 50 | % 3-2. Construct A_switch matrix 51 | 52 | A_switch = [speye(G.J).*(-p.lambda(1)), speye(G.J).*p.lambda(1); 53 | speye(G.J).*p.lambda(2), speye(G.J).*(-p.lambda(2))]; 54 | 55 | % 3-3. Guess an initial value of the value function 56 | 57 | % The value function of "staying put" 58 | 59 | v0 = p.u(G.income)./p.rho; 60 | V = v0; 61 | 62 | %% 4. VALUE FUNCTION ITERATION 63 | 64 | for n=1:p.maxit 65 | 66 | %% 4-1. Compute the derivative of the value function 67 | % dVf = Df*V; 68 | % dVb = Db*V; 69 | 70 | % function deriv = deriv_sparse(G, f, k, operator, name) 71 | 72 | dVf =zeros(G.J, 2); 73 | dVb =zeros(G.J, 2); 74 | for j = 1:2 75 | dVf(:, j) = deriv_sparse(G, V(:,j), 1, 'D1F', num2str(j)); 76 | dVb(:, j) = deriv_sparse(G, V(:,j), 1, 'D1B', num2str(j)); 77 | end 78 | 79 | %% 4-2. Boundary conditions 80 | % dVb(1,:) = p.mu(zz(1,:) + r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 81 | % dVf(end,:) = p.mu(zz(end,:) + r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 82 | 83 | % dVf(G.a == p.amax, :) = p.mu(G.income(G.a == p.amax, :)); 84 | % dVb(G.a == p.amin, :) = p.mu(G.income(G.a == p.amin, :)); 85 | 86 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 87 | 88 | % 4-3. Compute the optimal consumption 89 | cf = p.inv_mu(dVf); 90 | cb = p.inv_mu(dVb); 91 | 92 | % 4-4. Compute the optimal savings 93 | sf = G.income - cf; 94 | sb = G.income - cb; 95 | 96 | % 4-5. Upwind scheme 97 | If = sf>0; 98 | Ib = sb<0; 99 | I0 = 1-If-Ib; 100 | dV0 = p.mu(G.income); % If sf<=0<=sb, set s=0 101 | 102 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 103 | 104 | c = p.inv_mu(dV_upwind); 105 | s = G.income - c; 106 | 107 | % 4-6. Update value function: 108 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 109 | 110 | V_stacked = V(:); % 2I*1 matrix 111 | c_stacked = c(:); % 2I*1 matrix 112 | 113 | % A = SD 114 | % SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 115 | % SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 116 | 117 | %% Generator: A 118 | % Aa{1} = FD_operator(G, drift, volatility, dimension, type); 119 | s = If.*sf + Ib.*sb; 120 | 121 | Aa{1} = FD_operator(G, s(:,1), zeros(G.J, 1), 1, '1'); % earning type 1 122 | Aa{2} = FD_operator(G, s(:,2), zeros(G.J, 1), 1, '2'); % earning type 2 123 | 124 | SD = [Aa{1}, sparse(G.J, G.J); 125 | sparse(G.J, G.J), Aa{2}]; % 2I*2I matrix 126 | 127 | % SD = blkdiag(Aa{1}, Aa{2}); 128 | 129 | % P = A + A_switch 130 | P = SD + A_switch; 131 | 132 | % B = [(rho + 1/Delta)*I - P] 133 | B = (p.rho + 1/p.Delta)*speye(2*G.J) - P; 134 | 135 | % b = u(c) + 1/Delta*V 136 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 137 | 138 | % V = B\b; 139 | V_update = B\b; % 2I*1 matrix 140 | V_change = V_update - V_stacked; 141 | V = reshape(V_update, G.J, 2); % I*2 matrix 142 | 143 | % 3-6. Convergence criterion 144 | dist(n) = max(abs(V_change)); 145 | if dist(n) c=inv_mu(dV) 35 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 36 | 37 | %% Grid Parmaters 38 | 39 | p.amin = -0.15; 40 | p.amax = 5; 41 | 42 | % % The number of grid points 43 | % p.I = 1000; 44 | 45 | % The level of sparse grid 46 | p.l = 10; 47 | 48 | %% Tuning parameters 49 | 50 | % Step size: can be arbitrarily large in implicit method 51 | p.Delta = 1000; 52 | 53 | % The maximum number of value function iterations 54 | p.maxit = 100; 55 | 56 | % Tolerance for value function iterations 57 | p.tol = 10^(-6); 58 | 59 | %% TUNING PARAMETERS FOR INTEREST RATE ITERATION 60 | 61 | % The maximum number of interest rate iterations 62 | p.Nr = 40; 63 | 64 | % Tolerance for interest rate iterations 65 | p.tol_S = 10^(-6); 66 | 67 | end -------------------------------------------------------------------------------- /sections/codes/week5/Andreas's Repository/GE/main_GE.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: main_GE 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 16, 2024 6 | % 7 | % Description: 8 | % This MATLAB script solves the general equilibrium of the Huggett model. 9 | % The implementation leverages the SparseEcon repository developed by 10 | % Andreas Schaab and Allen T. Zhang (available at https://github.com/schaab-lab/SparseEcon). 11 | % 12 | % Reference: 13 | % - Huggett_equilibrium_iterate.m by Benjamin Moll 14 | % - Codes developed during the 2024 Coding Workshop by Andreas Schaab 15 | % 16 | % Notes: 17 | % - CRRA utility function: U(c) = (c^(1-sigma))/(1-sigma) 18 | % - Elasticity of intertemporal substitution (sigma): 2 19 | % - Discount rate (rho): 0.05 20 | % - Income: z = [z_u, z_e] = [0.1, 0.2]; 21 | % - Lambda: la = [la_u, la_e] = [1.5, 1]; 22 | % - Discrete grid of asset levels (a): -0.15 to 5 23 | % - Borrowing constraint: a>=-0.15 24 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | 27 | 28 | clear all; 29 | close all; 30 | clc; 31 | 32 | %% 0. Add the path of SparseEcon folder 33 | 34 | addpath(genpath('../SparseEcon/lib')); 35 | % addpath(genpath('SparseEcon/lib')); 36 | 37 | %% 1. DEFINE PARAMETERS 38 | 39 | p = define_parameters(); 40 | 41 | %% 2. INITIALIZE GRID POINTS 42 | 43 | % a = linspace(p.amin, p.amax, p.I)'; 44 | % da = (p.amax-p.amin)/(p.I-1); 45 | % 46 | % aa = [a, a]; % I*2 matrix 47 | 48 | %{ 49 | setup_grid.m: Initializes grid structure 50 | 51 | function G = setup_grid(n, surplus, min, max, varargin) 52 | 53 | INPUTS: 54 | - n: Level of sparse grid 55 | - surplus: Dimension-specific level surplus e.g., [0, 1, -1] 56 | - min: Dimension-specific minimums in economic units 57 | - max: Dimension-specific maximums in economic units 58 | 59 | VARIABLE INPUTS: 60 | - NamedDims: Cell of vectors of named dimensions 61 | - Names: Names for named dimensions 62 | - DxxDims: Vector of dimensions to compute dxx operators 63 | - DxyDims: (n x 2) matrix of dimensions to compute dxy operators 64 | 65 | %} 66 | 67 | G = setup_grid(p.l, 0, p.amin, p.amax, 'NamedDims', {1}, 'Names', {'a'}); 68 | 69 | % Notes: G.a is the grid points. G.J is the number of grid points. 70 | 71 | %% NEWTON'S METHOD 72 | 73 | % Guess an initial value of the interest rate 74 | r0 = p.r; 75 | 76 | % Define asset supply S(r) as a function of r 77 | S = @(r) stationary(r, G, p); 78 | 79 | % Solve for equilibrium interest rate using fsolve 80 | options = optimoptions("fsolve", "FunctionTolerance", p.tol_S); 81 | r_equilibrium = fsolve(S, r0, options); 82 | 83 | % Rerun stationary using the equilibrium interest rate 84 | [~, ss] = stationary(r_equilibrium, G, p); 85 | 86 | %% 6. GRAPHS 87 | 88 | % 6-1. Optimal consumption 89 | 90 | set(gca, 'FontSize', 18) 91 | plot(G.a, ss.c, 'LineWidth', 2) 92 | grid 93 | xlabel('Wealth, a','FontSize', 14) 94 | ylabel('Consumption, c_j(a)','FontSize', 14) 95 | xlim([p.amin p.amax]) 96 | legend('Unemployed', 'Employed', 'Location', 'best', 'FontSize', 14) 97 | 98 | % 6-2. Optimal savings 99 | 100 | % adot = G.income - c; 101 | 102 | set(gca, 'FontSize', 18) 103 | plot(G.a, ss.s, G.a, zeros(1,G.J), '--k', 'LineWidth', 2) 104 | grid 105 | xlabel('Wealth, a', 'FontSize', 14) 106 | ylabel('Saving, s_j(a)', 'FontSize', 14) 107 | xlim([p.amin p.amax]) 108 | legend('Unemployed', 'Employed', 'Location', 'best', 'FontSize', 14) 109 | 110 | % 6-3. Value function 111 | 112 | set(gca, 'FontSize', 18) 113 | plot(G.a, ss.V, 'LineWidth', 2) 114 | grid 115 | xlabel('Wealth, a', 'FontSize', 14) 116 | ylabel('Value function, V_j(a)', 'FontSize', 14) 117 | xlim([p.amin p.amax]) 118 | legend('Unemployed', 'Employed', 'Location', 'best', 'FontSize', 14) 119 | 120 | % 6-4. Wealth distribution 121 | 122 | set(gca, 'FontSize', 14) 123 | plot(G.a, ss.gg, 'LineWidth', 2) 124 | grid 125 | xlabel('Wealth, a', 'FontSize', 14) 126 | ylabel('Densities, g_j(a)', 'FontSize', 14) 127 | yy = get(gca, 'yLim'); 128 | hold on 129 | plot([-0.15,-0.15], yy, '--k', 'LineWidth', 2) 130 | text(-0.15, yy(1)-0.02*(yy(2) - yy(1)), '$\underline{a}$', 'HorizontalAlignment', 'center', 'FontSize', 15, 'Interpreter', 'latex') 131 | xlim([-0.2 1]) 132 | legend('Unemployed', 'Employed', 'Borrowing Constraint', 'Location', 'best', 'FontSize', 14) -------------------------------------------------------------------------------- /sections/codes/week5/Andreas's Repository/GE/stationary.m: -------------------------------------------------------------------------------- 1 | function [S, ss] = stationary(r, G, p) 2 | 3 | %% HJB 4 | 5 | [V, c, s, P] = HJB(r, G, p); 6 | 7 | %% KF 8 | 9 | [gg] = KF(P, G); 10 | 11 | %% OUTPUT 12 | 13 | S = gg(:,1)'*G.a*G.da + gg(:,2)'*G.a*G.da; 14 | 15 | ss.V = V; 16 | ss.gg = gg; 17 | ss.c = c; 18 | ss.s = s; 19 | 20 | end -------------------------------------------------------------------------------- /sections/codes/week5/Andreas's Repository/PE/define_parameters.m: -------------------------------------------------------------------------------- 1 | function p = define_parameters() 2 | 3 | % This function defines the parameters needed for the Huggett_GE.m script 4 | 5 | %% Economic Parameters 6 | 7 | % Relative risk aversion 8 | p.gamma = 2; 9 | 10 | % Discount rate 11 | p.rho = 0.05; 12 | 13 | % Exogenous interest rate 14 | p.r = 0.035; 15 | 16 | % Income process 17 | p.z_u = 0.1; 18 | p.z_e = 0.2; 19 | p.zz = [p.z_u, p.z_e]; 20 | 21 | % Probability density 22 | p.lambda_u = 1.5; 23 | p.lambda_e = 1; 24 | p.lambda = [p.lambda_u, p.lambda_e]; 25 | 26 | %% Economic Functions 27 | 28 | % Utility funtion 29 | p.u = @(c) c.^(1-p.gamma)/(1-p.gamma); 30 | 31 | % Marginal utility function 32 | p.mu = @(c) c.^(-p.gamma); 33 | 34 | % FOC: mu(c)=dV -> c=inv_mu(dV) 35 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 36 | 37 | %% Grid Parmaters 38 | 39 | p.amin = -0.15; 40 | p.amax = 5; 41 | 42 | % % The number of grid points 43 | % p.I = 1000; 44 | 45 | % The level of sparse grid 46 | p.l = 10; 47 | 48 | %% Tuning parameters 49 | 50 | % Step size: can be arbitrarily large in implicit method 51 | p.Delta = 1000; 52 | 53 | % The maximum number of value function iterations 54 | p.maxit = 100; 55 | 56 | % Tolerance for value function iterations 57 | p.tol = 10^(-6); 58 | 59 | %% TUNING PARAMETERS FOR INTEREST RATE ITERATION 60 | 61 | % The maximum number of interest rate iterations 62 | p.Nr = 40; 63 | 64 | % Tolerance for interest rate iterations 65 | p.tol_S = 10^(-6); 66 | 67 | end -------------------------------------------------------------------------------- /sections/codes/week5/Andreas's Repository/PE/main_PE.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MATLAB Code: main_PE 3 | % 4 | % Author: Kiyea Jin 5 | % Date: Nov 16, 2024 6 | % 7 | % Description: 8 | % This MATLAB script solves the partial equilibrium of the Huggett model. 9 | % The implementation leverages the SparseEcon repository developed by 10 | % Andreas Schaab and Allen T. Zhang (available at https://github.com/schaab-lab/SparseEcon). 11 | % 12 | % Reference: 13 | % - Huggett_equilibrium_iterate.m by Benjamin Moll 14 | % - Codes developed during the 2024 Coding Workshop by Andreas Schaab 15 | % 16 | % Notes: 17 | % - CRRA utility function: U(c) = (c^(1-sigma))/(1-sigma) 18 | % - Elasticity of intertemporal substitution (sigma): 2 19 | % - Discount rate (rho): 0.05 20 | % - Income: z = [z_u, z_e] = [0.1, 0.2]; 21 | % - Lambda: la = [la_u, la_e] = [1.5, 1]; 22 | % - Discrete grid of asset levels (a): -0.15 to 5 23 | % - Borrowing constraint: a>=-0.15 24 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | 27 | clear all; 28 | close all; 29 | clc; 30 | 31 | %% 0. Add the path of SparseEcon folder 32 | 33 | addpath(genpath('../SparseEcon/lib')); 34 | % addpath(genpath('SparseEcon/lib')); 35 | 36 | %% 1. DEFINE PARAMETERS 37 | 38 | p = define_parameters(); 39 | 40 | %% 2. INITIALIZE GRID POINTS 41 | 42 | % a = linspace(p.amin, p.amax, p.I)'; 43 | % da = (p.amax-p.amin)/(p.I-1); 44 | % 45 | % aa = [a, a]; % I*2 matrix 46 | 47 | %{ 48 | setup_grid.m: Initializes grid structure 49 | 50 | function G = setup_grid(n, surplus, min, max, varargin) 51 | 52 | INPUTS: 53 | - n: Level of sparse grid 54 | - surplus: Dimension-specific level surplus e.g., [0, 1, -1] 55 | - min: Dimension-specific minimums in economic units 56 | - max: Dimension-specific maximums in economic units 57 | 58 | VARIABLE INPUTS: 59 | - NamedDims: Cell of vectors of named dimensions 60 | - Names: Names for named dimensions 61 | - DxxDims: Vector of dimensions to compute dxx operators 62 | - DxyDims: (n x 2) matrix of dimensions to compute dxy operators 63 | 64 | %} 65 | 66 | G = setup_grid(p.l, 0, p.amin, p.amax, 'NamedDims', {1}, 'Names', {'a'}); 67 | 68 | % Notes: G.a is the grid points. G.J is the number of grid points. 69 | 70 | %% 3. PRE-ITERATION INITIALIZATION 71 | 72 | % 3-1. Construct the forward and backward differential operator 73 | % Df such that Df*V=dVf and Db such that Db*V=dVb 74 | % 75 | % Df = zeros(p.I, p.I); 76 | % for i = 1:p.I-1 77 | % Df(i,i) = -1/da; Df(i,i+1) = 1/da; 78 | % end 79 | % Df = sparse(Df); 80 | % 81 | % Db = zeros(p.I, p.I); 82 | % for i = 2:p.I 83 | % Db(i,i-1) = -1/da; Db(i,i) = 1/da; 84 | % end 85 | % Db = sparse(Db); 86 | 87 | %% DIFFERENTIAL OPERATOR + BOUNDARY CONDITIONS 88 | 89 | %{ 90 | gen_FD.m: Constructs FD operators for given boundary conditions 91 | 92 | function G = gen_FD(G, BC, name) 93 | 94 | INPUTS: 95 | - G: Grid struct 96 | - BC: Boundary condition inputs. This is a (d x 1) cell, where each 97 | element is a struct with the following properties 98 | - left.type: type of left boundary condition 99 | - right.type: type of right boundary condition 100 | - left.f: values associated with left boundary condition 101 | - right.f: values associated with right boundary condition 102 | - name: (Optional) name associated with boundary condition, useful when 103 | using multiple boundary conditions for the same grid 104 | %} 105 | 106 | G.income = p.zz + p.r.*G.a; 107 | 108 | left_bound = p.mu(G.income(G.a == p.amin, :)); 109 | right_bound = p.mu(G.income(G.a == p.amax, :)); 110 | for j = 1:2 111 | BC{1}.left.type = 'VNB'; BC{1}.right.type = 'VNF'; % Von Nuemann Boundary Condition 112 | BC{1}.left.f = @(points) left_bound(j) * ones(size(points, 1), 1); 113 | BC{1}.right.f = @(points) right_bound(j) * ones(size(points, 1), 1); 114 | G = gen_FD(G, BC, num2str(j)); 115 | end 116 | 117 | % 3-2. Construct A_switch matrix 118 | 119 | A_switch = [speye(G.J).*(-p.lambda(1)), speye(G.J).*p.lambda(1); 120 | speye(G.J).*p.lambda(2), speye(G.J).*(-p.lambda(2))]; 121 | 122 | % 3-3. Guess an initial value of the value function 123 | 124 | % The value function of "staying put" 125 | 126 | v0 = p.u(G.income)./p.rho; 127 | V = v0; 128 | 129 | %% 4. VALUE FUNCTION ITERATION 130 | 131 | for n=1:p.maxit 132 | 133 | %% 4-1. Compute the derivative of the value function 134 | % dVf = Df*V; 135 | % dVb = Db*V; 136 | 137 | % function deriv = deriv_sparse(G, f, k, operator, name) 138 | 139 | dVf =zeros(G.J, 2); 140 | dVb =zeros(G.J, 2); 141 | for j = 1:2 142 | dVf(:, j) = deriv_sparse(G, V(:,j), 1, 'D1F', num2str(j)); 143 | dVb(:, j) = deriv_sparse(G, V(:,j), 1, 'D1B', num2str(j)); 144 | end 145 | 146 | %% 4-2. Boundary conditions 147 | % dVb(1,:) = p.mu(zz(1,:) + r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 148 | % dVf(end,:) = p.mu(zz(end,:) + r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 149 | 150 | % dVf(G.a == p.amax, :) = p.mu(G.income(G.a == p.amax, :)); 151 | % dVb(G.a == p.amin, :) = p.mu(G.income(G.a == p.amin, :)); 152 | 153 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 154 | 155 | % 4-3. Compute the optimal consumption 156 | cf = p.inv_mu(dVf); 157 | cb = p.inv_mu(dVb); 158 | 159 | % 4-4. Compute the optimal savings 160 | sf = G.income - cf; 161 | sb = G.income - cb; 162 | 163 | % 4-5. Upwind scheme 164 | If = sf>0; 165 | Ib = sb<0; 166 | I0 = 1-If-Ib; 167 | dV0 = p.mu(G.income); % If sf<=0<=sb, set s=0 168 | 169 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 170 | 171 | c = p.inv_mu(dV_upwind); 172 | 173 | % 4-6. Update value function: 174 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 175 | 176 | V_stacked = V(:); % 2I*1 matrix 177 | c_stacked = c(:); % 2I*1 matrix 178 | 179 | % A = SD 180 | % SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 181 | % SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 182 | 183 | %% Generator: A 184 | % Aa{1} = FD_operator(G, drift, volatility, dimension, type); 185 | s = If.*sf + Ib.*sb; 186 | 187 | Aa{1} = FD_operator(G, s(:,1), zeros(G.J, 1), 1, '1'); % earning type 1 188 | Aa{2} = FD_operator(G, s(:,2), zeros(G.J, 1), 1, '2'); % earning type 2 189 | 190 | SD = [Aa{1}, sparse(G.J, G.J); 191 | sparse(G.J, G.J), Aa{2}]; % 2I*2I matrix 192 | 193 | % SD = blkdiag(Aa{1}, Aa{2}); 194 | 195 | % P = A + A_switch 196 | P = SD + A_switch; 197 | 198 | % B = [(rho + 1/Delta)*I - P] 199 | B = (p.rho + 1/p.Delta)*speye(2*G.J) - P; 200 | 201 | % b = u(c) + 1/Delta*V 202 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 203 | 204 | % V = B\b; 205 | V_update = B\b; % 2I*1 matrix 206 | V_change = V_update - V_stacked; 207 | V = reshape(V_update, G.J, 2); % I*2 matrix 208 | 209 | % 3-6. Convergence criterion 210 | dist(n) = max(abs(V_change)); 211 | if dist(n) [0.6, 0.8]; 19 | % - Discrete grid of asset levels (a): -0.15 to 4 20 | % - Discrete grid of time (time): 0.2 to 20 21 | % - Borrowing constraint: a>=-0.15 22 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 23 | % 24 | % Code Structure: 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | 27 | clear all; 28 | close all; 29 | clc; 30 | 31 | %% 1. INITIAL EQUILIBRIUM 32 | 33 | p = define_parameters_initial(); 34 | 35 | [ss_initial] = huggett_GE(p); 36 | 37 | %% 2. TERMINAL EQUILIBRIUM 38 | 39 | p = define_parameters_terminal(); 40 | 41 | [ss_terminal] = huggett_GE(p); 42 | 43 | % Notes: The shock is not mean-reverting; 44 | % it represents a one-time change that permanently shifts the parameter. 45 | % Hence, we use the updated parameters to solve for transition dynamics. 46 | 47 | %% 3. INITIALIZE GRID POINTS 48 | 49 | % 3-1. Construct grid points for wealth 50 | 51 | a = linspace(p.amin, p.amax, p.I)'; 52 | da = (p.amax-p.amin)/(p.I-1); 53 | aa = [a, a]; % I*2 matrix 54 | 55 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 56 | 57 | %% 3-2. Construct grid points for time 58 | 59 | time = linspace(p.tmin, p.tmax, p.It)'; 60 | dt = (p.tmax-p.tmin)/(p.It-1); 61 | 62 | %% 4. PRE-ITERATION INITIALIZATION 63 | 64 | % 4-1. Construct the forward and backward differential operator 65 | % Df such that Df*V=dVf and Db such that Db*V=dVb 66 | 67 | Df = zeros(p.I, p.I); 68 | for i = 1:p.I-1 69 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 70 | end 71 | Df = sparse(Df); 72 | 73 | Db = zeros(p.I, p.I); 74 | for i = 2:p.I 75 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 76 | end 77 | Db = sparse(Db); 78 | 79 | % 4-2. Construct A_switch matrix 80 | 81 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 82 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 83 | 84 | %A_switch = zeros(2*I, 2*I); 85 | %for i=1:I 86 | % A_switch(i,i) = -lambda(1); 87 | % A_switch(i,i+I) = lambda(1); 88 | % A_switch(i+I,i) = lambda(2); 89 | % A_switch(i+I,i+I) = -lambda(2); 90 | %end 91 | 92 | %% 4-3. Terminal condition for value function 93 | 94 | V_t = zeros(p.I, 2, p.It); 95 | 96 | V_T = ss_terminal.V; 97 | V_t(:,:,p.It) = V_T; 98 | 99 | %% 4-4. Initial condition for stationary distribution 100 | 101 | g_stacked = zeros(2*p.I, p.It); 102 | 103 | g_0 = ss_initial.gg(:); 104 | g_stacked(:,1) = g_0; 105 | 106 | gg_t = zeros(p.I, 2, p.It); 107 | gg_t(:,:,1) = reshape(g_0, p.I, 2); 108 | 109 | % 4-5. Pre-allocate arrays for S and dS 110 | 111 | S = zeros(p.It,1); 112 | dS = zeros(p.It,1); 113 | 114 | %% 5. Iteration to find a SERIES of equilibrium interest rates 115 | 116 | % 5-1. Guess an initial function of the interest rate 117 | 118 | r_T = ss_terminal.r; 119 | 120 | r0 = r_T*ones(p.It, 1); 121 | 122 | r_t = r0; 123 | 124 | % 5-2. Define the speed of updating the interest rate 125 | 126 | xi = 20*(exp(-0.05*(1:p.It)) - exp(-0.05*p.It)); 127 | 128 | for n=1:p.Nr 129 | 130 | r_t_n(:,n) = r_t; 131 | 132 | %% 6. VALUE FUNCTION ITERATION 133 | 134 | % For given r(t), solve the HJB equation backward in time with terminal 135 | % condition. 136 | 137 | for t=p.It:-1:1 138 | 139 | % V has I*2*It dimensions. For each iteration, we update V(:,:,t) 140 | % which has I*2 dimensions as the codes we have run so far. 141 | V = V_t(:,:,t); 142 | 143 | % 6-1. Compute the derivative of the value function 144 | dVf = Df*V; 145 | dVb = Db*V; 146 | 147 | % 6-2. Boundary conditions 148 | dVb(1,:) = p.mu(zz(1,:) + r_t(t).*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 149 | dVf(end,:) = p.mu(zz(end,:) + r_t(t).*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 150 | 151 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 152 | 153 | % 6-3. Compute the optimal consumption 154 | cf = p.inv_mu(dVf); 155 | cb = p.inv_mu(dVb); 156 | 157 | % 6-4. Compute the optimal savings 158 | sf = zz + r_t(t).*aa - cf; 159 | sb = zz + r_t(t).*aa - cb; 160 | 161 | % 6-5. Upwind scheme 162 | If = sf>0; 163 | Ib = sb<0; 164 | I0 = 1-If-Ib; 165 | dV0 = p.mu(zz + r_t(t).*aa); % If sf<=0<=sb, set s=0 166 | 167 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 168 | 169 | c = p.inv_mu(dV_upwind); 170 | 171 | % 6-6. Update value function: 172 | % V(t) = [(rho + 1/dt)*I - (S(t+1)*D(t+1)+A_switch)]^(-1)*[u(c(t+1)) + 1/dt*V(t+1)] 173 | 174 | V_stacked = V(:); % 2I*1 matrix 175 | c_stacked = c(:); % 2I*1 matrix 176 | 177 | % A = SD 178 | SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 179 | SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 180 | SD = [SD_u, sparse(p.I, p.I); 181 | sparse(p.I, p.I), SD_e]; % 2I*2I matrix 182 | 183 | % P = A + A_switch 184 | P = SD + A_switch; 185 | P_t{t} = P; 186 | 187 | % B = [(rho + 1/Delta)*I - P] 188 | B = (p.rho + 1/dt)*speye(2*p.I) - P; 189 | 190 | % b = u(c) + 1/Delta*V 191 | b = p.u(c_stacked) + (1/dt)*V_stacked; 192 | 193 | % V = B\b; 194 | V_update = B\b; % 2I*1 matrix 195 | 196 | if t>1 197 | V_t(:,:,t-1) = reshape(V_update, p.I, 2); % I*2 matrix 198 | end 199 | 200 | % 6-7. Compute savings 201 | s = zz + r_t(t)*aa - c; % I*2 matrix 202 | 203 | s_t(:,:,t) = s; 204 | c_t(:,:,t) = c; 205 | 206 | end 207 | 208 | %% 7. KF EQUATION 209 | % Notes: For given s(t), solve the KF equation forward in time with initial 210 | % condition. 211 | 212 | for t=1:p.It 213 | 214 | % 7-1. Define the transpose of P matrix for each t 215 | PT = P_t{t}'; 216 | 217 | % 7-2. Update g: g(t+1)=(I-dt*P')^(-1)*g(t) 218 | g_stacked(:,t+1) = (speye(2*p.I) - dt*PT)\g_stacked(:,t); 219 | 220 | gg_t(:,:,t+1) = reshape(g_stacked(:, t+1), p.I, 2); 221 | 222 | % 7-3. Compute asset supply 223 | S(t) = g_stacked(:,t)'*aa(:)*da; 224 | dS(t) = g_stacked(:,t+1)'*aa(:)*da - g_stacked(:,t)'*aa(:)*da; 225 | 226 | end 227 | 228 | %% 5-3. Update the interest rate 229 | 230 | S_n(:,n) = S; 231 | dS_n(:,n) = dS; 232 | 233 | % Update the interest rate to reduce aggregate saving 234 | rnew = r_t - xi'.*dS; 235 | r_t = rnew; 236 | 237 | Sdist(n) = max(abs(dS)); 238 | 239 | disp(['ITERATION = ', num2str(n)]) 240 | disp(['Convergence criterion = ', num2str(Sdist(n))]) 241 | if Sdist(n) [0.6, 0.8]; 19 | % - Discrete grid of asset levels (a): -0.15 to 4 20 | % - Discrete grid of time (time): 0.2 to 20 21 | % - Borrowing constraint: a>=-0.15 22 | % - Delta = 1000; (Can be arbitrarily large in implicit method) 23 | % 24 | % Code Structure: 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | 27 | clear all; 28 | close all; 29 | clc; 30 | 31 | %% 1. INITIAL EQUILIBRIUM 32 | 33 | ; 34 | 35 | ; 36 | 37 | %% 2. TERMINAL EQUILIBRIUM 38 | 39 | ; 40 | 41 | ; 42 | 43 | % Notes: The shock is not mean-reverting; 44 | % it represents a one-time change that permanently shifts the parameter. 45 | % Hence, we use the updated parameters to solve for transition dynamics. 46 | 47 | %% 3. INITIALIZE GRID POINTS 48 | 49 | % 3-1. Construct grid points for wealth 50 | 51 | a = linspace(p.amin, p.amax, p.I)'; 52 | da = (p.amax-p.amin)/(p.I-1); 53 | aa = [a, a]; % I*2 matrix 54 | 55 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 56 | 57 | %% 3-2. Construct grid points for time 58 | 59 | ; 60 | ; 61 | 62 | %% 4. PRE-ITERATION INITIALIZATION 63 | 64 | % 4-1. Construct the forward and backward differential operator 65 | % Df such that Df*V=dVf and Db such that Db*V=dVb 66 | 67 | Df = zeros(p.I, p.I); 68 | for i = 1:p.I-1 69 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 70 | end 71 | Df = sparse(Df); 72 | 73 | Db = zeros(p.I, p.I); 74 | for i = 2:p.I 75 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 76 | end 77 | Db = sparse(Db); 78 | 79 | % 4-2. Construct A_switch matrix 80 | 81 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 82 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 83 | 84 | %A_switch = zeros(2*I, 2*I); 85 | %for i=1:I 86 | % A_switch(i,i) = -lambda(1); 87 | % A_switch(i,i+I) = lambda(1); 88 | % A_switch(i+I,i) = lambda(2); 89 | % A_switch(i+I,i+I) = -lambda(2); 90 | %end 91 | 92 | %% 4-3. Terminal condition for value function 93 | 94 | ; 95 | 96 | ; 97 | ; 98 | 99 | %% 4-4. Initial condition for stationary distribution 100 | 101 | ; 102 | 103 | ; 104 | ; 105 | 106 | gg_t = zeros(p.I, 2, p.It); 107 | gg_t(:, :, 1) = reshape(g_0, p.I, 2); 108 | 109 | % 4-5. Pre-allocate arrays for S and dS 110 | 111 | S = zeros(p.It,1); 112 | dS = zeros(p.It,1); 113 | 114 | %% 5. Iteration to find a SERIES of equilibrium interest rates 115 | 116 | % 5-1. Guess an initial function of the interest rate 117 | 118 | ; 119 | ; 120 | 121 | ; 122 | 123 | % 5-2. Define the speed of updating the interest rate 124 | 125 | xi = 20*(exp(-0.05*(1:p.It)) - exp(-0.05*p.It)); % 1*It matrix 126 | 127 | 128 | %% 6. VALUE FUNCTION ITERATION 129 | 130 | % For given r(t), solve the HJB equation backward in time with terminal 131 | % condition. 132 | 133 | 134 | 135 | % V has I*2*It dimensions. For each iteration, we update V(:,:,t) 136 | % which has I*2 dimensions as the codes we have run so far. 137 | ; 138 | 139 | 140 | %% 7. KF EQUATION 141 | % Notes: For given s(t), solve the KF equation forward in time with initial 142 | % condition. 143 | 144 | 145 | 146 | % 7-1. Define the transpose of P matrix for each t 147 | ; 148 | 149 | % 7-2. Update g: g(t+1)=(I-dt*P')^(-1)*g(t) 150 | ; 151 | 152 | ; 153 | 154 | % 7-3. Compute asset supply 155 | ; 156 | ; 157 | 158 | 159 | 160 | %% 5-3. Update the interest rate 161 | 162 | S_n(:,n) = S; 163 | dS_n(:,n) = dS; 164 | 165 | % Update the interest rate to reduce aggregate saving 166 | ; 167 | ; 168 | 169 | ; 170 | 171 | disp(['ITERATION = ', num2str(n)]) 172 | disp(['Convergence criterion = ', num2str(Sdist(n))]) 173 | if 174 | break 175 | end 176 | 177 | 178 | 179 | 180 | %% 6. GRAPH 181 | 182 | %% 6-1. Initial and terminal stationary distribution 183 | 184 | set(gca, 'FontSize', 14) 185 | plot(a, ss_initial.gg(:,1), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'r') 186 | hold on 187 | plot(a, ss_initial.gg(:,2), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'b') 188 | hold on 189 | plot(a, ss_terminal.gg(:,1), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'r') 190 | hold on 191 | plot(a, ss_terminal.gg(:,2), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'b') 192 | hold off 193 | grid 194 | xlabel('Wealth, a', 'FontSize', 14) 195 | ylabel('Densities, g_j(a)', 'FontSize', 14) 196 | yy = get(gca, 'yLim'); 197 | hold on 198 | plot([p.amin, p.amin], yy, '--k', 'LineWidth', 2) 199 | hold off 200 | text(-0.1, 0.2, '$a=\underline{a}$', 'HorizontalAlignment', 'center', 'FontSize', 15, 'Interpreter', 'latex') 201 | xlim([-0.2 1]) 202 | ylim([0 10]) 203 | legend(sprintf('Unemployed, r=%.4f', ss_initial.r), ... 204 | sprintf('Employed, r=%.4f', ss_initial.r), ... 205 | sprintf('Unemployed, r=%.4f', ss_terminal.r), ... 206 | sprintf('Employed, r=%.4f', ss_terminal.r), 'Location', 'best', 'FontSize', 14) 207 | title('Initial Steady State (Solid) and Terminal Steady State (Dashed)', 'FontSize', 18) 208 | 209 | %% 6-2. Equilibrium interest rates path 210 | 211 | N1 = 4; 212 | T1 = -N1*dt; 213 | time1 = T1 + (1:N1)'*dt; 214 | time2 = [time1;time]; 215 | r_t2 = [ss_initial.r*ones(N1,1);r_t]; 216 | set(gca,'FontSize',14) 217 | plot(time2, r_t2, 'LineWidth', 2, 'LineStyle', '-', 'Color', 'b') 218 | hold on 219 | plot(time2, r_T*ones(1,N1+p.It), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'r') 220 | hold off 221 | xlim([T1, p.tmax]) 222 | xlabel('Time') 223 | title('Equilibrium Interest Rate, r(t)') 224 | 225 | %% 6-3. Dynamics of wealth distribution 226 | 227 | set(gca, 'FontSize', 14) 228 | plot(a, ss_initial.gg(:,1), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'r') 229 | hold on 230 | plot(a, ss_initial.gg(:,2), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'b') 231 | hold on 232 | plot(a, gg_t(:, 1, 25), 'LineWidth', 2, 'LineStyle', "--", 'Color', 'r') 233 | hold on 234 | plot(a, gg_t(:, 2, 25), 'LineWidth', 2, 'LineStyle', "--", 'Color', 'b') 235 | hold on 236 | plot(a, gg_t(:, 1, 50), 'LineWidth', 2, 'LineStyle', "-.", 'Color', 'r') 237 | hold on 238 | plot(a, gg_t(:, 2, 50), 'LineWidth', 2, 'LineStyle', "-.", 'Color', 'b') 239 | hold on 240 | plot(a, ss_terminal.gg(:,1), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'r') 241 | hold on 242 | plot(a, ss_terminal.gg(:,2), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'b') 243 | hold off 244 | grid 245 | xlabel('Wealth, a', 'FontSize', 14) 246 | ylabel('Densities, g_j(a)', 'FontSize', 14) 247 | yy = get(gca, 'yLim'); 248 | hold on 249 | plot([p.amin, p.amin], yy, '--k', 'LineWidth', 2) 250 | hold off 251 | text(-0.11, 0.1, '$a=\underline{a}$', 'HorizontalAlignment', 'center', 'FontSize', 15, 'Interpreter', 'latex') 252 | xlim([-0.2 0.5]) 253 | ylim([0 3]) 254 | legend({'T=0: Unemployed', 'T=0: Employed', ... 255 | 'T=5: Unemployed', 'T=5: Employed', ... 256 | 'T=10: Unemployed', 'T=10: Employed', ... 257 | 'T=\infty: Unemployed', 'T=\infty: Employed'}, ... 258 | 'Location', 'best', 'FontSize', 12, 'Interpreter', 'tex') 259 | title('Dynamics of Wealth Distribution', 'FontSize', 18) 260 | 261 | % %% 6-4. Dynamics of savings 262 | % 263 | % set(gca, 'FontSize', 14) 264 | % plot(a, ss_initial.s(:, 1), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'r') 265 | % hold on 266 | % plot(a, ss_initial.s(:, 2), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'b') 267 | % hold on 268 | % plot(a, s_t(:, 1, 1), 'LineWidth', 2, 'LineStyle', "--", 'Color', 'r') 269 | % hold on 270 | % plot(a, s_t(:, 2, 1), 'LineWidth', 2, 'LineStyle', "--", 'Color', 'b') 271 | % hold on 272 | % plot(a, s_t(:, 1, 5), 'LineWidth', 2, 'LineStyle', "-.", 'Color', 'r') 273 | % hold on 274 | % plot(a, s_t(:, 2, 5), 'LineWidth', 2, 'LineStyle', "-.", 'Color', 'b') 275 | % hold on 276 | % plot(a, ss_terminal.s(:, 1), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'r') 277 | % hold on 278 | % plot(a, ss_terminal.s(:, 2), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'b') 279 | % hold off 280 | % grid 281 | % xlabel('Wealth, a', 'FontSize', 14) 282 | % ylabel('Saving, s_j(a)', 'FontSize', 14) 283 | % yy = get(gca, 'yLim'); 284 | % hold on 285 | % plot([p.amin, p.amin], yy, '--k', 'LineWidth', 2) 286 | % hold off 287 | % text(-0.12, -0.05, '$a=\underline{a}$', 'HorizontalAlignment', 'center', 'FontSize', 15, 'Interpreter', 'latex') 288 | % xlim([-0.2 0.5]) 289 | % ylim([-0.2 0.1]) 290 | % legend({'T=0: Unemployed', 'T=0: Employed', ... 291 | % 'T=0.2: Unemployed', 'T=0.2: Employed', ... 292 | % 'T=1: Unemployed', 'T=1: Employed', ... 293 | % 'T=\infty: Unemployed', 'T=\infty: Employed'}, ... 294 | % 'Location', 'best', 'FontSize', 12, 'Interpreter', 'tex') 295 | % title('Dynamics of Saving', 'FontSize', 18) 296 | % 297 | % %% 6-5. Dynamics of consumption 298 | % 299 | % set(gca, 'FontSize', 14) 300 | % plot(a, ss_initial.c(:, 1), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'r') 301 | % hold on 302 | % plot(a, ss_initial.c(:, 2), 'LineWidth', 2, 'LineStyle', '-', 'Color', 'b') 303 | % hold on 304 | % plot(a, c_t(:, 1, 1), 'LineWidth', 2, 'LineStyle', "--", 'Color', 'r') 305 | % hold on 306 | % plot(a, c_t(:, 2, 1), 'LineWidth', 2, 'LineStyle', "--", 'Color', 'b') 307 | % hold on 308 | % plot(a, c_t(:, 1, 5), 'LineWidth', 2, 'LineStyle', "-.", 'Color', 'r') 309 | % hold on 310 | % plot(a, c_t(:, 2, 5), 'LineWidth', 2, 'LineStyle', "-.", 'Color', 'b') 311 | % hold on 312 | % plot(a, ss_terminal.c(:, 1), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'r') 313 | % hold on 314 | % plot(a, ss_terminal.c(:, 2), 'LineWidth', 2, 'LineStyle', ':', 'Color', 'b') 315 | % hold off 316 | % grid 317 | % xlabel('Wealth, a', 'FontSize', 14) 318 | % ylabel('Consumption, c_j(a)', 'FontSize', 14) 319 | % yy = get(gca, 'yLim'); 320 | % hold on 321 | % plot([p.amin, p.amin], yy, '--k', 'LineWidth', 2) 322 | % hold off 323 | % text(-0.11, 0.17, '$a=\underline{a}$', 'HorizontalAlignment', 'center', 'FontSize', 15, 'Interpreter', 'latex') 324 | % xlim([-0.2 0.5]) 325 | % ylim([0.1 0.25]) 326 | % legend({'T=0: Unemployed', 'T=0: Employed', ... 327 | % 'T=0.2: Unemployed', 'T=0.2: Employed', ... 328 | % 'T=1: Unemployed', 'T=1: Employed', ... 329 | % 'T=\infty: Unemployed', 'T=\infty: Employed'}, ... 330 | % 'Location', 'best', 'FontSize', 12, 'Interpreter', 'tex') 331 | % title('Dynamics of Consumption', 'FontSize', 18) -------------------------------------------------------------------------------- /sections/codes/week5/Transition Dynamics/define_parameters_initial.m: -------------------------------------------------------------------------------- 1 | function p = define_parameters_initial() 2 | 3 | % This function sets the parameters required to compute the initial equilibrium 4 | % for use in the Huggett_transition.m script. 5 | 6 | %% Economic Parameters 7 | 8 | % Relative risk aversion 9 | p.gamma = 2; 10 | 11 | % Discount rate 12 | p.rho = 0.05; 13 | 14 | % Income process 15 | p.z_u = 0.1; 16 | p.z_e = 0.2; 17 | p.zz = [p.z_u, p.z_e]; 18 | 19 | % Probability density 20 | p.lambda_u = 0.6; 21 | p.lambda_e = 0.5; 22 | p.lambda = [p.lambda_u, p.lambda_e]; 23 | 24 | %% Economic Functions 25 | 26 | % Utility funtion 27 | p.u = @(c) c.^(1-p.gamma)/(1-p.gamma); 28 | 29 | % Marginal utility function 30 | p.mu = @(c) c.^(-p.gamma); 31 | 32 | % FOC: mu(c)=dV -> c=inv_mu(dV) 33 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 34 | 35 | %% Grid Parmaters 36 | 37 | p.amin = -0.15; 38 | p.amax = 4; 39 | 40 | % The number of grid points 41 | p.I = 1000; 42 | 43 | %% Tuning parameters 44 | 45 | % Step size: can be arbitrarily large in implicit method 46 | p.Delta = 1000; 47 | 48 | % The maximum number of value function iterations 49 | p.maxit = 100; 50 | 51 | % Tolerance for value function iterations 52 | p.tol = 10^(-6); 53 | 54 | %% TUNING PARAMETERS FOR INTEREST RATE ITERATION 55 | 56 | % The maximum number of interest rate iterations 57 | p.Nr = 40; 58 | 59 | % Tolerance for interest rate iterations 60 | p.tol_S = 10^(-8); 61 | 62 | end -------------------------------------------------------------------------------- /sections/codes/week5/Transition Dynamics/define_parameters_terminal.m: -------------------------------------------------------------------------------- 1 | function p = define_parameters_terminal() 2 | 3 | % This function sets the parameters required to compute the terminal equilibrium 4 | % for use in the Huggett_transition.m script. 5 | 6 | %% Economic Parameters 7 | 8 | % Relative risk aversion 9 | p.gamma = 2; 10 | 11 | % Discount rate 12 | p.rho = 0.05; 13 | 14 | % Income process 15 | p.z_u = 0.1; 16 | p.z_e = 0.2; 17 | p.zz = [p.z_u, p.z_e]; 18 | 19 | % Probability density 20 | p.lambda_u = 0.6; 21 | p.lambda_e = 0.8; 22 | p.lambda = [p.lambda_u, p.lambda_e]; 23 | 24 | %% Economic Functions 25 | 26 | % Utility funtion 27 | p.u = @(c) c.^(1-p.gamma)/(1-p.gamma); 28 | 29 | % Marginal utility function 30 | p.mu = @(c) c.^(-p.gamma); 31 | 32 | % FOC: mu(c)=dV -> c=inv_mu(dV) 33 | p.inv_mu = @(dV) dV.^(-1/p.gamma); 34 | 35 | %% Grid Parmaters for wealth 36 | 37 | p.amin = -0.15; 38 | p.amax = 4; 39 | 40 | % The number of grid points 41 | p.I = 1000; 42 | 43 | %% Grid parameters for time 44 | 45 | p.tmin = 0.2; 46 | p.tmax = 20; 47 | p.It = 100; 48 | 49 | %% Tuning parameters 50 | 51 | % Step size: can be arbitrarily large in implicit method 52 | p.Delta = 1000; 53 | 54 | % The maximum number of value function iterations 55 | p.maxit = 100; 56 | 57 | % Tolerance for value function iterations 58 | p.tol = 10^(-6); 59 | 60 | %% TUNING PARAMETERS FOR INTEREST RATE ITERATION 61 | 62 | % The maximum number of interest rate iterations 63 | p.Nr = 1000; 64 | 65 | % Tolerance for interest rate iterations 66 | p.tol_S = 10^(-5); 67 | 68 | end -------------------------------------------------------------------------------- /sections/codes/week5/Transition Dynamics/huggett_GE.m: -------------------------------------------------------------------------------- 1 | function [ss] = huggett_GE(p) 2 | 3 | % This function solves the general equilibrium of the Huggett model, 4 | % finding the equilibrium interest rate that clears the bond market. 5 | 6 | %% 2. INITIALIZE GRID POINTS 7 | 8 | a = linspace(p.amin, p.amax, p.I)'; 9 | da = (p.amax-p.amin)/(p.I-1); 10 | 11 | aa = [a, a]; % I*2 matrix 12 | 13 | % %% 2-2. INITIALIZE GRID POINTS FOR INTEREST RATES 14 | % 15 | % rgrid = linspace(p.rmin, p.rmax, p.Ir)'; 16 | 17 | %% 3. PRE-ITERATION INITIALIZATION 18 | 19 | % 3-1. Construct the forward and backward differential operator 20 | % Df such that Df*V=dVf and Db such that Db*V=dVb 21 | 22 | Df = zeros(p.I, p.I); 23 | for i = 1:p.I-1 24 | Df(i,i) = -1/da; Df(i,i+1) = 1/da; 25 | end 26 | Df = sparse(Df); 27 | 28 | Db = zeros(p.I, p.I); 29 | for i = 2:p.I 30 | Db(i,i-1) = -1/da; Db(i,i) = 1/da; 31 | end 32 | Db = sparse(Db); 33 | 34 | % 3-2. Construct A_switch matrix 35 | 36 | A_switch = [speye(p.I).*(-p.lambda(1)), speye(p.I).*p.lambda(1); 37 | speye(p.I).*p.lambda(2), speye(p.I).*(-p.lambda(2))]; 38 | 39 | %A_switch = zeros(2*I, 2*I); 40 | %for i=1:I 41 | % A_switch(i,i) = -lambda(1); 42 | % A_switch(i,i+I) = lambda(1); 43 | % A_switch(i+I,i) = lambda(2); 44 | % A_switch(i+I,i+I) = -lambda(2); 45 | %end 46 | 47 | %% 3-3. Guess an initial value of the interest rate 48 | 49 | r0 = 0.03; 50 | r_min = 0.01; 51 | r_max = 0.04; 52 | 53 | %% 3-4. Guess an initial value of the value function 54 | 55 | zz = ones(p.I, 1).*p.zz; % I*2 matrix 56 | 57 | % The value function of "staying put" 58 | r = r0; 59 | 60 | v0 = p.u(zz + r.*aa)./p.rho; 61 | V = v0; 62 | 63 | %% 4. VALUE FUNCTION ITERATION 64 | 65 | for nr=1:p.Nr 66 | 67 | r_r(nr) = r; 68 | rmin_r(nr) = r_min; 69 | rmax_r(nr) = r_max; 70 | 71 | % Use the value function solution from the previous interest rate iteration 72 | % as the initial guess for the next iteration 73 | if nr>1 74 | v0 = V_r(:,:,nr-1); 75 | V = v0; 76 | end 77 | 78 | for n=1:p.maxit 79 | 80 | % 4-1. Compute the derivative of the value function 81 | dVf = Df*V; 82 | dVb = Db*V; 83 | 84 | % 4-2. Boundary conditions 85 | dVb(1,:) = p.mu(zz(1,:) + r.*aa(1,:)); % a>=a_min is enforced (borrowing constraint) 86 | dVf(end,:) = p.mu(zz(end,:) + r.*aa(end,:)); % a<=a_max is enforced which helps stability of the algorithm 87 | 88 | I_concave = dVb > dVf; % indicator whether value function is concave (problems arise if this is not the case) 89 | 90 | % 4-3. Compute the optimal consumption 91 | cf = p.inv_mu(dVf); 92 | cb = p.inv_mu(dVb); 93 | 94 | % 4-4. Compute the optimal savings 95 | sf = zz + r.*aa - cf; 96 | sb = zz + r.*aa - cb; 97 | 98 | % 4-5. Upwind scheme 99 | If = sf>0; 100 | Ib = sb<0; 101 | I0 = 1-If-Ib; 102 | dV0 = p.mu(zz + r.*aa); % If sf<=0<=sb, set s=0 103 | 104 | dV_upwind = If.*dVf + Ib.*dVb + I0.*dV0; 105 | 106 | c = p.inv_mu(dV_upwind); 107 | 108 | % 4-6. Update value function: 109 | % Vj^(n+1) = [(rho + 1/Delta)*I - (Sj^n*Dj^n+A_switch)]^(-1)*[u(cj^n) + 1/Delta*Vj^n] 110 | 111 | V_stacked = V(:); % 2I*1 matrix 112 | c_stacked = c(:); % 2I*1 matrix 113 | 114 | % A = SD 115 | SD_u = spdiags(If(:,1).*sf(:,1), 0, p.I, p.I)*Df + spdiags(Ib(:,1).*sb(:,1), 0, p.I, p.I)*Db; % I*I matrix 116 | SD_e = spdiags(If(:,2).*sf(:,2), 0, p.I, p.I)*Df + spdiags(Ib(:,2).*sb(:,2), 0, p.I, p.I)*Db; % I*I matrix 117 | SD = [SD_u, sparse(p.I, p.I); 118 | sparse(p.I, p.I), SD_e]; % 2I*2I matrix 119 | 120 | % P = A + A_switch 121 | P = SD + A_switch; 122 | 123 | % B = [(rho + 1/Delta)*I - P] 124 | B = (p.rho + 1/p.Delta)*speye(2*p.I) - P; 125 | 126 | % b = u(c) + 1/Delta*V 127 | b = p.u(c_stacked) + (1/p.Delta)*V_stacked; 128 | 129 | % V = B\b; 130 | V_update = B\b; % 2I*1 matrix 131 | V_change = V_update - V_stacked; 132 | V = reshape(V_update, p.I, 2); % I*2 matrix 133 | 134 | % 3-6. Convergence criterion 135 | dist(n) = max(abs(V_change)); 136 | if dist(n)p.tol_S 182 | disp('Excess Supply') 183 | % Decrease r whenever S(r)>0 184 | r_max = r; 185 | r = 0.5*(r_min+r_max); 186 | elseif S(nr)<-p.tol_S 187 | disp('Excess Demand') 188 | % Increase r whenever S(r)<0 189 | r_min = r; 190 | r = 0.5*(r_min+r_max); 191 | elseif abs(S(nr))