\section{Ghosts}
\label{sect:ghosts}

VCC methodology makes heavy use of \Def{ghost} data and code - data
and code that are not seen by the C compiler (and therefore are not
part of the executable), but are included to aid reasoning about the
program. Part of the VCC philosophy is that programmers would rather
writing extra code than have to drive interactive theorem provers, so
ghosts are the preferred way to explain to VCC why your program works.

We have already seen some ghost fields of ordinary data structures
(e.g. \vcc{\closed}, \vcc{\owner}, \vcc{\owns}m \vcc{\valid}, etc.) as
well as built-in pieces of ghost code (e.g. \vcc{_(wrap ...)} and
\vcc{_(unwrap ...)}). This section is about how to write your own
ghost code.

Our first tyical use of ghost data is to express data abstaction. If
you implement an abstract set as a list, it is good practice to expose
to the client only the set abstraction, while keeping the list
implementation private to the implementation of the data type. One way
to do this is to store the abstract value in a ghost field and update
it explicitly in ghost code when operating on the data
structure\footnote{ An alternative approach is to write a pure ghost
  function that takes a concrete data structure and returns its
  abstract value.  The disadvantage of this approach is that for
  recursive structures like lists, the abstraction function is
  likewise recursive, and so reasoning with it requires substantial
  guidance from the user.}. Functions operating on the data structure
are specified in terms of their effect on the abstract value only; the
connection between the abstract and concrete values is written as an
invariant of the data structure.

VCC's mathematical types are usually better suited to representing these
abstract values than C's built-in types. Here, we will use VCC maps.
Recal that a declaration \vcc{int m[T*]} defines a map \vcc{m} from
\vcc{T*} to \vcc{int}; for any pointer \vcc{p} of type \vcc{T*}
\vcc{m[p]} gives the \vcc{int} to which \vcc{m} maps \vcc{p}.
A map \vcc{bool s[int]} can be thought of as a set of \vcc{int}s: the operation
\vcc{s[k]} will return true if and only if the element \vcc{k} is in the set \vcc{s}.

For example, here is a simple example of a set of \vcc{int}s implemented with an array:
\vccInput[linerange={types-init}]{c/7.0.arraySet.c}

\noindent
The map \vcc{mem} gives the abstract value of the set. The map
\vcc{idx} says where to find each abstract member of the set. We could
have eliminated \vcc{idx} and instead used existential quantification
to say that every member of the abstract set occurs somewhere in the
list. The disadvantage of using an explicit witness like \vcc{idx} is
that we have to update it appropriately. The advantage is that the
prover doesn't have to guess how to instantiate such existential
quantifiers, which makes the verification more robust.

Here is the initializer for these sets:
\vccInput[linerange={init-mem}]{c/7.0.arraySet.c}
\noindent
The standard form of a constructor is to take some raw
memory\footnote{
In C, it is normal for constructors to take a pointer to raw memory,
to that they can be used to make objects that are embedded within data
structures.
}, and
wrap it, while establishing some condition on its abstract value.
Values of maps are constructed using \Def{lambda expressions}.  The
expression \vcc{\lambda T x; E} returns a map, which for any \vcc{x}
returns the value of expression \vcc{E} (which can mention \vcc{x}).
If \vcc{S} is the type of \vcc{E}, then this map is of type
\vcc{S[T]}.

Here is the membership test:
\vccInput[linerange={mem-add}]{c/7.0.arraySet.c}
\noindent
As usual, an accessor is marked as \vcc{_(pure)}, and reads only the
abstract value of the set. Finally, here is a function that adds a new
element to the set:
\vccInput[linerange={add-del}]{c/7.0.arraySet.c}
\noindent
Note that in addition to updating the concrete representation, we also
update the abstract value and the witness. This example shows another
way to update a map, using array syntax; if \vcc{m} is a variable of
type \vcc{S[T]}, \vcc{e} is of type \vcc{T}, and \vcc{e2} is of
type \vcc{S},  then the statement \vcc{m[e1] = e2} abbreviates the
statement
\vcc{m = \lambda T v; v == e1 ? e2 : m[v]}.

\subsection*{Exercises}
\begin{enumerate}
\item Extend \vcc{ArraySet} with a function that deletes a value from
  the set.
\item Modify \vcc{ArraySet} to keep the set without duplication.
\item Modify \vcc{ArraySet} to keep the elements ordered. Use binary
  search to check for membership and for insertion of a new element.
\item Extend \vcc{ArraySet} with a function that adds the contents of
  one set into another. (Try to calculate the size of the combined list
  before modifying the target, so that you can fail gracefully.)
\end{enumerate}

\subsection{Linked Data Structures}
As an example of a more typical dynamic data structure, consider
the following alternative implementation of \vcc{int} sets as lists:

\vccInput[linerange={types-init}]{c/7.1.list0.c}

\noindent
The invariant states that:
\begin{itemize}
\item the list owns the head node (if it's non-null)
\item if the list owns a node, it also owns the next node (provided it's non-null)
\item if the list owns a node \vcc{n} then \vcc{n->data} is in \vcc{val};
\item if \vcc{v} is in \vcc{val}, then it is the \vcc{data} for some
  node (\vcc{find[v]}) owned by the list.
\end{itemize}

Note that we have chosen to put all of the list
invariants in the \vcc{List} data structure, rather than in the nodes
themselves (which would also work). A disadvantage of putting all of
the invariants in the \vcc{List} type is that when you modify one of
the nodes, you have to check these invariants for all of the nodes
(although the invariants are easy to discharge for nodes that are not
immediate neighbors). Some advantages of this choice is that it is
easier to modify all of the nodes as a group, and that the same
\vcc{Node} type can be used for data structures with different
invariants (e.g., cyclic lists).

Here is the implementation of the \vcc{add} function:

\vccInput[linerange={endspec-member}]{c/7.1.list0.c}

\noindent
We allocate the node, unwrap the list, initialize the new node and
wrap it, and prepend the node at the beginning of the list.  Then we
update the owns set to include the new node, update the abstract value
\vcc{val} and the witness \vcc{find}, and finally wrap the list up
again (when exiting the \vcc{_(unwrapping)} block).

The invariants of our list say that the abstract value contains
exactly the \vcc{data} fields of nodes owned by the list. It also said
that pointers from list nodes take you to list nodes. But it doesn't
say that every node of the list can be reached from the first node;
the invariants would hold if, in addition to those nodes, the list
also owned some unrelated cycle of nodes. As a result, the natural
algorithm for checking if a value is in the set (by walking down the
list) won't verify; if it finds a value, its data is guaranteed to be
in the set, but not vice-versa. Moreover, the invariants aren't strong
enough to guarantee that the list itself is acyclic.

Thus, we need to strengthen the invariant of the list to guarantee
that every node of the list is reachable from the head. This cannot be
expressed directly with first-order logic, but there are several ways
to express this using VCC using ghost data:
\begin{itemize}
\item you can keep track of the ``depth'' of each list node (i.e.,
  how far down the list it appears);
\item you can maintain the abstract sequence of list nodes (i.e., a
  map from \vcc{\natural} to nodes, along with a \vcc{\natural} giving
  the length of the sequence);
\item you can maintain the ``reachability'' relationship between
  ordered pairs of nodes.
\end{itemize}

For this example, we'll use the first approach. We add the following
to the definition of the \vcc{List} type:

\vccInput[linerange={moreList-noMoreList}]{c/7.2.list.c}

The new invariants say that the head (if it exists) is at depth 0 (and
is the only node at depth 0), that depth increases by 1 when following
list pointers, and that you can never ``miss'' a node by following the
list. (These are similar to the invariants you would use if each node
had a key and you were maintaining an ordered list.)

The only change to the code we've previously seen is that when adding
a node to the list, we have to also update the node indices:
\begin{VCC}
l->idx = (\lambda Node *m; m == n ? 0 : l->idx[m] + 1);
\end{VCC}

We can now write and verify the membership test:
\vccInput[linerange={member-out}]{c/7.2.list.c}

\noindent Note that the second invariant of the loop is analogous to the
invariant we would use for linear search in an array.

The \vcc{_(assert)} is an example of a situation where VCC needs a
little bit to see why what you think is true really is true. The loop
invariant says that there are no nodes in the list with key \vcc{k},
but VCC on its own will fail to make the appropriate connection to
\vcc{l->val[k]} via \vcc{l->find[k]} without this hint (which is just
giving an instantiation for the last list invariant).

\subsection*{Exercises}
\begin{enumerate}
\item
Modify the list implementation so that on a successful membership
test, the node that is found is moved to the front of the list. (Note
that the resulting function is no longer pure.)
\item
Implement sets using sorted lists.
\item
Implement sets using binary search trees.
\end{enumerate}

\subsection{Sorting revisited}
\label{sect:sorting-perm}

In \secref{sorting} we verified that bubblesort returns a sorted array.
But we didn't prove that it returned a permutation of the input
array\footnote{For arrays in which no value occurs more than once,
  this property can be expressed that every value in the output array
  is in the input array. But with multiple occurrances, this would
  require stating that the multiplicity of each value is the same, a
  property that isn't first-order.
}. To express this postcondition, we return a ghost map, which states the
exact permutation that the sorting algorithm produced:

\vccInput[linerange={begin-out}]{c/7.3.sort.c}

This sample introduces two new features.
The first is the output ghost parameter \vcc{_(out Perm p)}.
An \vcc{out} parameter is used to return data from the function to the
caller. (One could also do it with a pointer to ghost memory, but
using an \vcc{out} parameter is simpler and more efficient.)

To call \vcc{sort()} you need to supply a local variable to hold
the permutation when the function exits, as in:
\begin{VCC}
void f(int *buf, unsigned len)
  // ...
{
  _(ghost Perm myperm; )
  // ...
  sort(buf, len _(out myperm));
}
\end{VCC}
The effect is to copy the value of the local variable \vcc{p} of the
function to the variable \vcc{myperm} on exit from the function.

The second feature is the use of \Def{record} types. A record type is
introduced by putting \vcc{_(record)} before the definition of a
like a \vcc{struct} type. They are mathematically cleaner than
\vcc{structs}, in several ways:
\begin{itemize}
\item A record is a single, indivisible value (like a map), so you
  don't have to worry about aliasing individual fields of a record.
\item Two records are equal iff their corresponding fields are
  equal. Conversely, C doesn't allow \vcc{==} on struct values
  (primarily because padding makes the meaning problematic).
\item Because \vcc{==} makes sense on records, a record type can be
  used as the domain of a map, whereas a \vcc{struct} type cannot.
\end{itemize}
However, records also have some limitations relative to \vcc{struct}s:
\begin{itemize}
\item Records are values, not objects, so records cannot have
  invariants.
\item Record fields can be of record type, but cannot be of compound
  (\vcc{struct} or \vcc{union}) type.
\item You can't take the address of a field of a record (and so cannot
  pass it to a function that updates it). However, you can use record
  fields as \vcc{_(out)} parameters.
\end{itemize}

\subsection{Inductive Proofs}
Sometimes, you will want to verify programs that depend on
mathematics that is too hard for VCC to do on its own, typically
because they require guidance. In VCC, you do this by writing ghost
code. In particular, you can do inductive proofs by writing loops or
by writing recursive functions, with the loop invariant or function
spec serving as the inductive hypothesis. Because C doesn't allow the
definition of anonymous recursive functions, loops are usually more
convenient.

Here is a small example of using ghost code to prove the formula for
triangular numbers:

\vccInput[linerange={begin-end}]{c/7.4.math.c}

Sometimes, you might want to use inductively defined types other than
\vcc{\natural} or \vcc{\integer}. VCC lets you define your own
(ghost) inductive types, much like modern functional languages, but
using the following C-like syntax:

\vccInput[linerange={begin-app}]{c/7.5.lists.c}

This defines an inductive datatype of lists, where a list is either of
the empty list \vcc{nil()} or an \vcc{int} followed by a
list. Values of abstract types are deconstructed using the following
\vcc{switch} construct (corresponding to matching expressions in
functional languages):

\vccInput[linerange={app-appAssoc}]{c/7.5.lists.c}

Note that unlike the usual C \vcc{switch} statement, fallthrough from
one case to the next is not allowed (since the scope of variables like
\vcc{v} and \vcc{l} introduced by the \vcc{case} construct only go to
the end of the case). Note that VCC automatically chooses the
termination measure \vcc{_(decreases size(x), size(y))}, which
suffices for typical functions where termination follows by structural
induction.

We can now prove something about the function we defined:

\vccInput[linerange={appAssoc-rev}]{c/7.5.lists.c}

Note that the recursive call provides the needed inductive case of the
theorem we're trying to prove. We can similarly use the resulting
function as a lemma in proving other theorems.

\subsection*{Exercises}
\begin{enumerate}

\item
Verify your favorite sorting functions (quicksort, heapsort,
mergesort, etc.).
\item
Using \vcc{app}, define a recursive function that reverses a
\vcc{List}. Prove that reversing a list twice gives you the same list
back. (Hint: you will need some additional lemmas along the way; one
solution can be found in \vcc{7.5.lists.c}.)
\end{enumerate}


% !TEX program = pdflatex
% !BIB program = bibtex
\documentclass[11pt]{article}
\usepackage{amsmath,amsfonts,amsthm,amssymb,geometry,dsfont,natbib}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[
bookmarks=false,
pdfstartview={FitV},
pdftitle={Equilibrium Technology Diffusion, Trade, and Growth: Computational Appendix},
pdfauthor={Jesse Perla, Christopher Tonetti, Michael E. Waugh},
pdfcreator={Jesse Perla, Christopher Tonetti, Michael E. Waugh},
pdfkeywords={economics,international,growth,technology diffusion, trade, Perla, Tonetti, Waugh},
colorlinks=true,
linkcolor=darkgray,
citecolor=darkgray,
urlcolor=darkgray,
filecolor=darkgray,
anchorcolor=darkgray,
breaklinks]
{hyperref}
\usepackage[capitalise,noabbrev]{cleveref} %
\crefname{equation}{}{} %
\crefname{assumption}{Assumption}{Assumptions}
\crefname{property}{Property}{Properties}
\geometry{left=1in,right=1in,top=0.6in,bottom=1in}
\bibliographystyle{ecta}
\newcommand{\D}[1][]{\ensuremath{\boldsymbol{\partial}_{#1}}}
\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\diff}{\ensuremath{\mathrm{d}}}
\newcommand{\ex}{\ensuremath{\mathrm{ex}}}
\newcommand{\set}[1]{\ensuremath{\left\{{#1}\right\}}}
\newcommand{\indicator}[1]{\ensuremath{\mathds{1}\left\{{#1}\right\}}}
\newcommand{\condexpec}[3][]{\ensuremath{\mathbb{E}_{#1}\left[{#2} \; \middle| \; {#3} \right]}}
\newcommand{\expec}[2][]{\ensuremath{\mathbb{E}_{{#1}}\left[ {#2} \right]}}

\newenvironment{psmallmatrix}
{\left(\begin{smallmatrix}}
	{\end{smallmatrix}\right)}

\begin{document}
\title{Equilibrium Technology Diffusion, Trade, and Growth\\Online Computational Appendix\thanks{Thanks to Ben Moll for advice on numerical methods and his superb lecture notes.  Excellent research assistance was provided by Chiyoung Ahn, Sev Hou, Arnav Sood, and Dongxiao Zhang.  Also,we appreciate all of the help from Chris Rackauckas, Yingbo Ma, and Xingjian Guo for the Julia DiffEq package and expertise.}}
\author{Jesse Perla \and Christopher Tonetti \and Michael E. Waugh}
\maketitle

\noindent See \url{https://github.com/jlperla/PerlaTonettiWaugh.jl} for the code and instructions.
\bigskip

This document is a self-contained summary of all of the equations and algorithms used to solve the model and transition dynamics.  Equations in the code have a comment to reference the corresponding equation numbers in this document.

\cite{AchdouHanLasryEtAl2017} and its technical appendix provide many of the details on the upwind finite difference methods used here.  While using these techniques to discretize the ``spatial'' dimension, we make several changes to the general approach taken by many economists to solve PDEs: (1) after discretizing the spatial dimension of the PDE to make them ODEs, we stack in further equilibrium equations into the ODEs to make them a system of DAEs; and (2) instead of discretizing the spatial dimension with finite differences and the time-dimension with a homegrown time-stepping (e.g. backwards euler), we instead rely on the underlying ODE/DAE solvers to do numerically stable, efficient, and often adaptive--time-stepping.  Hence, we concentrate on specifying the full set of ODE/DAE equations, which are then handed over to industrial strength solvers.

\section{Simplified Growth Model with Transition Dynamics}
We start with a simplified version of the model with transition dynamics.  In the more complicated case of this paper, the $\pi(z,t), r(t), x(t)$ will come from equilibrium conditions rather than being given exogenously, but the structure of solving the resulting system of integro-differential-algebraic equations remains the same.

There is a useful simplifying result in both this minimal and the full model: in the transition dynamics experiments we are looking at, the normalized distribution $F(z)$ will remain constant over time.  Hence, we do not need to jointly solve the KFE as we will be at the stationary level (post-normalization).  This is a consequence of the PDF remaining stationary (after normalization) under the transition dynamics we examine, and is a result from the theory, \textbf{not} an assumption.

\subsection{Summary of Equations}\label{sec:summary-simple}
We follow the notation of the main paper wherever possible.  This model is the geometric Brownian motion (GBM) model in the technical appendix of \cite{BenhabibPerlaTonetti2017}.

Let $Z$ be productivity, where $Z(t) \geq M(t)$.  Let $V(Z,t)$ is the value function prior to normalization, $M(t)$ the endogenous threshold, $\Pi(Z,t)$ the profits, and $X(t)$ the adoption cost.  The $\Pi(Z,t)$, $r$, and $X(t)$ are given exogenously in this simplified version, and endogenized with monopolistic competition and trade in the full model.

Define the normalization that $z \equiv \log(Z/M(t))$, $v(z,t) \equiv \frac{e^{-z}}{M(t)}V(M(t)e^z, t))$, $\pi(z,t) \equiv \frac{e^{-z}}{M(t)}\Pi(M(t)e^z, t))$, and $x(t) \equiv X(t)/M(t)$.  \cref{sec:simple-derivation} shows that the rescaled system of equations is
\begin{align}
\D[t]v(z,t) &= A(t)v(z,t) - \pi(z,t)\label{eq:simple-summary1}\\
v(0,t) &= \int_{0}^{\infty}  v(z,t) \left(e^z F'(z)\right) \diff z - x(t)\label{eq:simple-summary2}\\
\D[z]v(0,t) + v(0,t) &= 0\label{eq:simple-summary3}\\
\intertext{Given the differential operator,}
A(t) &\equiv \left(r - \mu - \frac{\upsilon^2}{2}\right) - (\mu+ \upsilon^2 - g(t)) \D[z] - \frac{\upsilon^2}{2} \D[zz]\label{eq:simple-summary4}\\
\intertext{Where the normalized distribution for all $t$ is}
F(z) &= 1 - e^{-\theta z}
\end{align}

\subsection{Analytic Steady State}\label{sec:simple-steady-state}
For the exogenous $\pi(z,t)$ and $x(t)$ we require that at some $T < \infty$ it becomes stationary.  Hence we will assume with little loss of generality that,\footnote{Note that the $\pi(z,T) = 1$ requires that pre-normalized profits are $\Pi(Z,T) \propto e^Z$.  With different functional forms of the profits, we would rescale by a different power in the exponential--such as the $e^{-\xi z}$ in the full model.}
\begin{align}
x(t) &= \zeta,\quad \text{ for all }t \geq T\label{eq:terminal-x}\\
\pi(z,t) &= 1,\quad \text{ for all }t \geq T\label{eq:terminal-pi}\\
f(z,t) &= \theta e^{-\theta z},\quad \text{ for all }t \label{eq:f-stationary-summary}
\end{align}

\noindent \cite{BenhabibPerlaTonetti2017} gives us a closed-form solution for this balanced growth path (i.e., steady state when normalized)

\begin{align}
g \equiv g(T) &= 	\mu+ \frac{1-(\theta -1) \zeta  \left(r-\mu\right)}{(\theta -1)^2 \zeta }+ \frac{\upsilon^2}{2}\frac{\theta  \left(\theta(\theta -1)    \left(r-\mu-\frac{\upsilon ^2}{2}\right) \zeta-2\right)+1}{(\theta -1) \left((\theta -1)   \left(r-\mu-\frac{\upsilon ^2}{2}\right)\zeta-1\right)}. \label{eq:g-gbm}\\
v(z,T) &= \frac1{r-\mu- \upsilon^2/2}\left(1 + \frac1{\nu} e^{-(\nu + 1) z}\right)\label{eq:v-gbm-sol},
\intertext{where,}
\nu &\equiv  \frac{\mu- g}{\upsilon^2} + \sqrt{\left(\frac{g-\mu}{\upsilon^2} \right)^{2} + \frac{r-g}{\upsilon^2/2}}. \label{eq:nu-gbm}
\end{align}

Note: since $v(z,T)$ has been rescaled by $e^{-z}$ above, it is now a \textbf{decreasing} function in $z$.  This will lead to appealing numerical properties when solving the PDE for $v(t,z)$ since it becomes asymptotically constant as $z \to \infty$ for any $t$.

\subsection{Summary of Equations For Numerical Solution}\label{sec:summary-simple-numerical}
To solve the model numerically, we need to bound the state space by adding an artificial upper bound, $\bar{z}$.  With this and using the derivation in \cref{sec:simple-derivation}, the equations of \cref{sec:summary-simple} become,

\begin{align}
\D[t]v(z,t) &= A(t)v(z,t) - \pi(z,t)\label{eq:simple-summary1-numerical}\\
v(0,t) &= \int_{0}^{\bar{z}}  v(z,t) \left(e^z \frac{F'(z)}{F(\bar{z})}\right) \diff z - x(t)\label{eq:simple-summary2-numerical}\\
\D[z]v(0,t) + v(0,t) &= 0\label{eq:simple-summary3-numerical}\\
\D[z]v(\bar{z},t) + v(\bar{z},t) &= 0\label{eq:simple-summary4-numerical}\\
\intertext{With the same operator $A(t)$ in \cref{eq:simple-summary4} now defined on $z \in (0,\bar{z})$}
A(t) &\equiv \left(r - \mu - \frac{\upsilon^2}{2}\right) - (\mu+ \upsilon^2 - g(t)) \D[z] - \frac{\upsilon^2}{2} \D[zz]\label{eq:simple-summary5-numerical}
\end{align}

\noindent A solution is a $g(t)$ and $v(z,t)$ such that this system of equations holds.\footnote{\textbf{Note:} in this class of models, it is known that if the distribution was forced to have a finite-support with a fixed frontier, it would lead to no long-run growth.  This is not what the artificial boundary is doing.  In particular, the pre-normalized boundary is growing at the same rate as the economy, i.e., $\bar{Z}(t) = M(t) e^{\bar{z}}$.  We have numerically verified that for a large $\bar{z}$, this converges to the known analytic solution with an infinite support.}

\subsection{Spatial Discretization with Upwind Finite-Differences}

In order to solve for the dynamics of $v(z,t)$ and $g(t)$, we will discretize the PDE, integral, and algebraic equations from \cref{eq:simple-summary1-numerical,eq:simple-summary2-numerical,eq:simple-summary3-numerical,eq:simple-summary4-numerical} into a system of ODEs and algebraic equations.\footnote{As a test, we solve the steady state version of the discretized system of ODEs numerically and compare it to the closed-form solution in \cref{sec:simple-steady-state} to determine the degree of numerical error and stability of the methods.}

\paragraph{Grid}
The core PDE in \cref{eq:simple-summary1-numerical} is defined on the interior of the domain, i.e., $z \in (0, \bar{z})$ while the boundary conditions are defined on the closure $z \in [0,\bar{z}]$.  To aid the implementation, define the potentially irregular grid on $[0,\bar{z}]$
\begin{align}
	z_{\ex} &= \set{z_i}_{i=0}^{P+1} \in \R^{P+2},
\end{align}
where $z_0 = 0$ and $z_{P+1} = \bar{z}$ is a ``large'' number (keeping in mind that the effective number is $e^{\bar{z}}$).

We abuse notation and use $z$ to denote the grid on the interior, representing where the PDE holds outside of boundary conditions.  That is,
\begin{align}
	z &= \set{z_i}_{i=1}^P \in \R^{P},
\end{align}

where we note that $z \equiv z_{\ex}[2:P+1] = \set{z_1, z_2, \ldots z_P}$.\footnote{Throughout this document, when we use the $[ ]$ to access within an array, it has $1$ based indices.}  Consequently, after discretizing, the PDE will be solved on the interior $\set{z_i}_{i=1}^P$ and the boundary conditions for $z_0 = 0$ and $z_{P+1} = \bar{z}$ will be fulfilled by the solution through the discretization approach.

\paragraph{Discretized Functions}

After discretizing, we abuse notation and denote the grid by the variable name, i.e., $z \equiv \set{z_i}_{i=1}^{P}$.  We also denote discretized function with the same symbol, but dropping the dependence on $z$.  In the case of the payoffs and resulting functions, they are only defined on the interior, i.e., $\pi(t) \equiv \set{\pi(z_i,t)}_{i=1}^P\in \R^P$ and $v(t) \equiv \set{v(z_i,t)}_{i=1}^P\in \R^P$.

\paragraph{Discretized Operator}

To discretize the system, we use upwind finite differences.  In particular, we need to discretize the differential operator in \cref{eq:simple-summary5-numerical} subject to the \cref{eq:simple-summary3-numerical,eq:simple-summary4-numerical} boundary conditions.  Crucially, since these boundary conditions are homogeneous, we can decompose the linear $A(t)$ operator into additive parts where: the $\D[z]$ is discretized as $L^{-}_1$ using backwards first differences, and the $\D[zz]$ is discretized as $L_2$ using central second difference.   Furthermore, the $L^{-}_1$ and $L_2$ linear operators \textit{impose the boundary conditions \cref{eq:simple-summary3-numerical,eq:simple-summary4-numerical} for any vector} they operator on.

See \cref{sec:discretized-operators} for the formulas for $L_1^-$ and $L_2$, and \url{https://github.com/QuantEcon/SimpleDifferentialOperators.jl/releases/download/dev/discretized-differential-operator-derivation.pdf} for the derivations.  While the full equations are complicated by the irregular grids and boundary conditions, for intuition note that the discretization of the $L_1^-$ backwards first difference operator is such that in the interior
$$
\D[z] v(z_i) \approx L_1^{-}\, v(z_i) = \frac{v_i - v_{i-1}}{z_i - z_{i-1}},\text{ for } i = 2, \ldots P-1
$$

Using that, we can calculate the approximate $\D[z] v(z_i)$ for all $v \equiv \set{v(z_i)}_{i=1}^P$ with $L_1^{-} v$ and $\D[zz] v(z_i)$ for all $i$ as $L_2 v$.  Alternatively, combining the discretization of $A(t)$ in \cref{eq:simple-summary5-numerical} subject to the boundary conditions is then the $P \times P$ matrix
\begin{align}
A(t) &\equiv \left(r - \mu - \frac{\upsilon^2}{2}\right) I - (\mu + \upsilon^2 - g(t)) L^{-}_1 - \frac{\upsilon^2}{2} L_2, \label{eq:A-def-simple}
\end{align}
where $I$ is the identity matrix.

While non-binding in practice here, to check that the  $L^{-}_1$ backwards differences is the correct upwind direction, verify that\footnote{In the steady-state, this equation will be fulfilled for any non-degenerate solutions, but it is best to check this result at a few points in the transition path for off-BGP steps in the numerical algorithm.}
\begin{equation}
\mu + \sigma^2 - g(t) < 0.
\end{equation}

\paragraph{Quadrature}
In order to discretize \cref{eq:simple-summary2-numerical}, we need to calculate the integral using quadrature.  Since the grid points are already given (i.e., we can't choose Gaussian quadrature nodes), we use the weights for the non-uniform trapezoidal rule.

To calculate these, use $\Delta_{-}$ and $\Delta_{+}$ backwards and forward diffs (respectively) for the $z$ grid.  That is, $\Delta_{i,+} = z_{i+1} - z_i$ for $i = 0,\ldots P$, and $\Delta_{i,-} = z_i - z_{i-1}$ for $i = 1,\ldots P+1$.  Pad these with (with $\Delta_{0,-} = 0$ and $\Delta_{P+1,+} = 0$), so that from \cref{eq:Delta-minus,eq:Delta-plus}
\begin{align}
 	\Delta_{-} &\equiv \begin{bmatrix} 0 \\
	\text{diff}(z)
	\end{bmatrix}\in\R^{P+2}\\
	\Delta_{+} &\equiv \begin{bmatrix} \text{diff}(z)\\
	0
\end{bmatrix}\in\R^{P+2}.
\intertext{Define}
\bar{\omega}&\equiv \frac1{2}(\Delta_{-} + \Delta_{+})\in \R^{P+2}\\
\Xi_1 &\equiv \frac{1}{1 - \xi \Delta_{0,+}}\\
\Xi_P &\equiv \frac{1}{1 + \xi \Delta_{P+1,-}}.
\end{align}

Use \cref{eq:omega-i}, so that the rule only uses the interior values $v_1(t)$ to $v_P(t)$ and all other values are collapsed into constants (i.e., $e^z$ and the $F'(z)$) with $\bar{\omega}$ so that\footnote{It is important to remember that we are imposing the boundary conditions here in the calculation of the quadrature rules, so this approach would require adjustment with different boundaries (i.e., the $\Xi_1$ and $\Xi_P$ might be different).}
\begin{align}
\omega_i &\equiv\begin{cases}
\bar{\omega}_1 \frac{\theta e^{(\xi - \theta)z_1}}{1 - e^{-\theta \bar{z}}} + \bar{\omega}_0\Xi_1 \frac{\theta e^{(\xi - \theta)z_0}}{1 - e^{-\theta \bar{z}}}& \text{ for } i = 1\\
\bar{\omega}_i \frac{\theta e^{(\xi - \theta)z_i}}{1 - e^{-\theta \bar{z}}} & \text{ for } i = 2, \ldots P-1\\
\bar{\omega}_P \frac{\theta e^{(\xi - \theta)z_P}}{1 - e^{-\theta \bar{z}}} +  \bar{\omega}_{P+1}\Xi_P\frac{\theta e^{(\xi - \theta)z_{P+1}}}{1 - e^{-\theta \bar{z}}}& \text{ for } i = P
\end{cases}
\end{align}
% \frac{\theta e^{(\xi - \theta)z_0}}{1 - e^{-\theta \bar{z}}}

\noindent Using this definition, the integral is approximated by a linear operator $\omega\in\R^P$ applied to the solution of the PDE on the interior, i.e., $v(t)$.
\begin{align}
 \int_{0}^{\bar{z}}  v(z,t) \left(e^z \frac{F'(z)}{F(\bar{z})}\right) \diff z &\approx \omega \cdot v(t) .
\end{align}

\subsection{Discretized Model as an DAE}
Let $v_1(t)$ be the $1$st element in the $v(t)$ vector (i.e., the first value in the interior).  From \cref{sec:boundary-extrapolation}, we know that $v(0, t) = \Xi_1 v_1(t)$  Then value matching condition in \cref{eq:simple-summary2-numerical,eq:simple-summary1-numerical} along with the system of ODEs becomes the system of differential-algebraic equations (DAE)
\begin{align}
	v'(t) &= A(t) v(t) - \pi(t)\label{eq:discretized-simple}\\
	0 &= \Xi_1 v_1(t) - \omega \cdot v(t) + x(t) . \label{eq:discretized-simple-vm}
\end{align}

\noindent To find the stationary solution at $T$,  $v'(T) = 0$, we can solve the nonlinear system of equations
\begin{align}
	0 &= A(T; g) v(T) - \pi(T)\label{eq:discretized-simple-stationary}\\
	0 &= \Xi_1 v_1(T) - \omega \cdot v(T) + x(T) , \label{eq:discretized-simple-vm-stationary}
\end{align}
where the $ A(T; g)$ notation is to reinforce that it is the only component which is a function of $g$. This can be solved by using a nonlinear solver for $v(T)$ and $g$. This can also be solved by iterating on $g$: fix $g$, solve the linear \cref{eq:discretized-simple-stationary} for $v(T)$, check \cref{eq:discretized-simple-vm-stationary} for a non-zero residual, and update the guess of $g$ until convergence to a sufficiently small residual.

\subsection{Solving the Transition Dynamics}

Transition dynamics can be solved for any exogenous $x(t)$ and $\pi(z,t)$ functions defined on $t \in [0,\infty)$ where they remain constant after $T$.  The general approach  for this is to directly solve the system \cref{eq:discretized-simple,eq:discretized-simple-vm} as a DAE.\footnote{ We experimented extensively with the more typical method in the economics literature of guessing a path for $g(t)$, solving the discretized system as an ODE in \cref{eq:discretized-simple}, and then minimizing residuals of \cref{eq:discretized-simple-vm}.  For various reasons, this approach ended up being much slower and less stable for this particular problem, which led to the DAE solution.  While we were able to get solutions using the typical approach for the simple model, it failed to effectively solve the full model dynamics robustly.}

\begin{itemize}
	\item Choose an appropriate $z$ grid, with a large number of points close to $0$ as well as near the $\bar{z}$.\footnote{There is a degree of sensitivity in this model for the particular choice of $\bar{z}$ and the grid points for several reasons: (a) the imposition of the \cref{eq:simple-summary3-numerical} condition on $\D[z]v(0,t)$  is analytically the correct boundary condition of the optimal stopping problem for the imitation threshold--it comes from the smooth-pasting condition--but is sensitive to the precise curvature.  For this reason, the discretization needs to give it enough precision or the $g(t)$ can get distorted; (b) the majority of the curvature is close to the $z=0$ barrier, after which the function rapidly becomes constant; (c) the artificial boundary at $\bar{z}$ introduces a lot of curvature right near the end.  If there are not enough grid points close to the $\bar{z}$, then the local curvature near $v(\bar{z},t)$ effects a larger part of the whole distribution; and (d) since the expectation in \cref{eq:discretized-simple-vm} is taken over a heavy-tailed distribution, and is essential for solving the endogenous $g(t)$, truncations can have a significant bias if $\bar{z}$ is too small.  In particular, if you change the $\theta$ radically or conditions which lead to large changes in $g(t)$, you should verify that the analytical and numerical steady states do not become too different.}
	\item Identify an industrial-strength DAE solver (e.g., Sundials.jl in \url{http://docs.juliadiffeq.org/latest/tutorials/dae_example.html}).\footnote{See \cite{RackauckasNie2017} for more on the Julia ODE and DAE packages.  We use the SUNDIALS DAE solver from \cite{HindmarshBrownGrantEtAl2005}.}  To use these solvers, you provide an operator in the form of a stacked system of ODEs and algebraic equations, and then flag the algebraic ones for the solver.
	\item Find the time $T$ steady state as $v(T) \in \R^P$ and $g(T) \in R$ from \cref{eq:discretized-simple-stationary,eq:discretized-simple-vm-stationary} as the \textit{initial condition}, for working backwards in time.\footnote{While we have an analytical solution in this specific case, it is better to use the numerical solution for the steady-state since it is the steady-state of the DAE for a given grid.}
	\item Setup a function for equations \cref{eq:discretized-simple,eq:discretized-simple-vm}, tagging the last equation as an algebraic one.
	\item Solve the system, which is at most a medium-sized problem for a serious DAE solver.
\end{itemize}

This approach to solving transition dynamics seems to be generally applicable:\footnote{In fact, we suspect that this approach to be extremely fast and robust for most models.  Part of the difficulty in solving our setup is that models with endogenously determined growth rates have peculiarities which lead to extra sensitivity in numerical solutions.} (1) use a spatial discretization to turn the PDEs into a system of ODEs; (2) add any equilibrium constraints on the value functions or distributions as algebraic equations, using quadrature to convert any integrals into linear operators; (3) stack both to create a DAE system, and then use a high-performance solver.  A more general piece of advice for these is for economists to exploit high-performance ODE solvers with flexibility for various time-stepping methods rather than discretizing the time dimension on their own.

\section{Full Growth Model with Transition Dynamics}
The fundamental differences between solving the full model with transition dynamics and the simple model presented earlier are the following:
\begin{itemize}
	\item The $\pi(z,t)$ and $x(t)$ come from static equilibrium conditions given the trade-structure of the paper and the number of active local varieties, $\Omega(t)$, rather than being given exogenously
	\item The interest rate $r(t)$ is no longer given exogenously, but needs to be found in equilibrium.  Luckily, this is forward looking and can be solved in the current structure with only minor changes since the transition dynamics are being solved backwards
	\item The number of varieties $\Omega(t)$ is endogenously determined based on the endogenous entry decision, and may change slowly according to profit maximizing and income smoothing incentives
	\item There is an additional algebraic equation for the export threshold, $\hat{z}(t)$
\end{itemize}

\paragraph{General Algorithm}
Of these, the only significant change is that the $\Omega(t)$ must be solved separately since the entry decision is dynamic, forward-looking, and begins with a stock $\Omega(0)$.  Therefore, we cannot simply add it as a further algebraic equation.  In order to find the solution, we will need to guess a sequence of $\Omega(t)$ fulfilling the boundary conditions, solve the model, and iterate to convergence.

The iteration begins with an (irregular) grid of time-stops for a large terminal time $T$.  Instead of guessing the $\Omega(t)$ directly, we guess a sequence of $\hat{E}$ on all of the time-stops such that $\Omega(0) = \Omega_0$ and $\Omega(T) = \Omega_T$ when integrated, where the $\hat{E}$ are mapped to the $E$ and $\Omega$ through \cref{sec:entry-ODE}.

\begin{enumerate}
	\item Solve for the steady states for the time $0$ and time $T$ to find $\Omega_0, \Omega_T$, and the terminal solution for $v(T),\hat{z}(T),g(T)$
	\item Use the current $\hat{E}$ guess to solve for the $\Omega(t)$ function from \cref{sec:entry-ODE}.  This maps to a function such that the known boundary conditions $\Omega(0) = \Omega_0$ and $\Omega(T) = \Omega_T$ are fulfilled
	\item Solve the underlying DAE treating the $\Omega(t)$ as exogenous with the same basic algorithm as the simple model.
	\begin{itemize}
		\item The additional algebraic variables are $g, E, \hat{z}$ where the $E$ is used in all calculations instead of the guessed $\hat{E}$.  In particular, the $\hat{L}(t)$ calculation.
		\item The system becomes a DAE with: the ODE, the value-matching condition, the export threshold, and the free-entry condition  (i.e., of length $P + 3$).
	\end{itemize}
	\item Extract the $E(t)$ exactly at the time-stops used in the $\hat{E}$ guess from the DAE solution
	\item Convert the $E(t)$ to an implied $\hat{E}(t)$ by subtracting $\delta$ and then rescaling so that $\hat{E}(0) = -1$ and $\hat{E}(T) = 0$.
	\item This final step makes the process a fixed point in the $\hat{E}$ guess on the interior.
	\item Iterate the fixed point mapping with Anderson acceleration, and stop the iteration when $\hat{E}$ converges.
\end{enumerate}

\paragraph{Transition Dynamics Experiment}

Assume $\delta > 0$. The experiment is to examine a one-time unanticipated \textit{decrease} in $d$ as the trade liberalization.  We calculate the two steady states and examine the transition dynamics.

To evaluate the kernel of the DAE at a given $t$, we need to calculate $\tilde{\rho}(t), \pi_{\min}(t),\hat{z}(t),$ etc  as a function of $g(t), \hat{z}(t), \Omega(t),$ and parameters.  As a simplification, we will assume that the cost of adoption is in labor, i.e., $\eta = 0$, so that $x(t) = \zeta$ for all $t$ from (PTW H.11).\footnote{Otherwise, there is an addition system of implicit equations to solve.}  The other simplification will be to assume $\log$ utility, so that $\gamma = 1$, resulting in a slightly more manageable calculation of interest rates.

\subsection{Static Calculations and Definitions}
Let $E(t)\Omega(t)$ be the associated gross entry flow at time $t$ and $\Omega(t)$ the number of varieties.  Then use a given $g(t), \hat{z}(t),$ and  $\tilde{L}(t+\Delta_t)$ for some small $\Delta_t$ to calculate the static equilibrium conditions,\footnote{See \cref{sec:derive-interest-rates} for a derivation of the interest rates, and \cref{sec:full-rescaling} for a derivation of the normalized profits.}${}^{,}$.\footnote{Note: the $\hat{z}(t)$ is not taken with a $\log$, unlike the $z$.  Hence, to see if a firm exports, we need to check if $z \geq \log(\hat{z}(t))$.  Furthermore, it means that the $\hat{z}$ has a minimum value of $1$.  This decision was taken simply to make comparison to the main paper algebra clearer.}
\begin{align}
	S(t) &\equiv \theta \left( g(t) - \mu - \theta\frac{\upsilon^2}{2}\right)\\
	1 - \tilde{L}(t) &\equiv 1 - \Omega(t)\left((N -1)\hat{z}^{-\theta}\kappa + \zeta \left(S(t) + E(t)/\chi \right)\right)\\
	\tilde{L}_{\text{x}}(t) &= \Omega(t) (N -1)\hat{z}^{-\theta}\kappa\\
	\tilde{L}_{\text{E}}(t) &= \frac{\zeta}{\chi}\Omega(t) E(t)\\
	\tilde{L}_{\text{a}}(t) &= \zeta \Omega(t) S(t)\\
	\bar{z}(t)&\equiv \left(\Omega(t)
\frac{\theta}{1+\theta - \sigma}\left(1 + (N-1)d^{1-\sigma}\hat{z}^{\sigma - 1 -\theta} \right)\right)^{\frac{1}{\sigma - 1}}\\
	\bar{\pi}_{\min}(t) &\equiv \frac{1-\tilde{L}(t)}{(\sigma-1)\bar{z}(t)^{\sigma-1}}\\
	\pi(z,t) &\equiv \bar{\pi}_{\min}(t)\left(1 + (N-1)d^{1-\sigma}\indicator{z \geq \log(\hat{z}(t))}\right) - (N-1)\kappa e^{-(\sigma - 1)z}\indicator{z \geq \log(\hat{z}(t))}\\
	\tilde{\rho}(t) &\equiv \underbrace{\rho+ \delta + \D[t]\log\left(1 - \tilde{L}(t)\right)}_{\equiv \tilde{r}(t)} - (\sigma - 1)\left(\mu - g(t) + (\sigma - 1)\frac{\upsilon^2}{2} \right).
\end{align}
To calculate the $\D[t]\log\left(1 - \tilde{L}(t)\right)$ we need to reference the ``future'' $\tilde{L}(t+\Delta_t)$. That is,
\begin{align}
	\D[t]\log\left(1 - \tilde{L}(t)\right) &\approx \frac{\log\left(1 - \tilde{L}(t+\Delta_t)\right) - \log\left(1 - \tilde{L}(t)\right)}{\Delta_t},
\end{align}
where the $\Delta_t$ is that periods (potentially adaptive) step-size in the time-stepping algorithm of the DAE solver.  Since the algorithm solves the transition dynamics backwards, this can be done using the previous (i.e., future) timestep, and then forward first-differences for a given $\tilde{L}(t)$.   Alternatively, given a sequence of $\tilde{L}(t)$, it can be calculated afterwards as part of an iterative step.

\subsection{Number of Varieties and Entry in Transition}\label{sec:entry-ODE}

When making a guess for the varieties over the transition path, in principle we could have chosen either the $\Omega(t)$ or the $E(t)$ (for $t \in [0,T]$) and converted between them using \cref{eq:E-defined-by-Omega}.\footnote{In practice, though, the model does not solve effectively with guesses on $\Omega(t)$ since small changes in the guess can lead to large deviations in $E(t)$, which enter the growth calculations directly.}  Analytically, we know that any guess for $E(t)$ on $t \in [0,T]$ is one where $E(T) = \delta$ and where $\Omega(T) = \Omega_T$ from the new steady state.  Since choosing a guess on an $E(t)$ which fulfills these conditions is difficult, we instead do the change of variables to guess a $\widehat{E}(t)$ such that $\widehat{E}(T) = 0$, where $\widehat{E}(0)$ is \textit{indeterminate} and can be rescaled.  See \cref{sec:Omega-E} for the detailed derivation.

Given the guess on $\widehat{E}(t)$, and assuming that $\widehat{E}(t)$ is not $0$ throughout, first calculate the scaling constant\footnote{In the case of checking if the model solves transition dynamics without solutions (i.e., when $E(t) = \delta$ is the solution), this assumption is not an issue due to the indeterminancy.  As long as there is any guess other than flat, $Q = 0$ comes out as the scaling factor, which solves the solution perfectly.}

\begin{align}
	Q &= \log \left(\dfrac{\Omega_T}{\Omega_0}\right)\left(\int_0^T \widehat E(t) dt \right)^{-1} . \\
	\intertext{Then map to the $E(t)$ and $\Omega(t)$ with the following equation and differential equation}
	E(t) &= Q \widehat{E}(t) + \delta\\
	\D[t] \Omega(t) &= Q \widehat{E}(t) \Omega(t).
\end{align}

In practice, since the guess $\widehat{E}(0)$ is arbitrary and simply changes the scale of $Q$ used to map to $E(t)$, it will be helpful to set it to a single number between iterations.  Here we maintain $\widehat{E}(0) = - 1$ at each iteration.

\subsection{DAE for the Full Solution}\label{sec:full-ODE}
The dynamic set of differential equations (rescaled) is derived in \cref{sec:full-model-derivations} and is very similar to the simple model.
\begin{align}
	\D[t]v(z,t) &= A(t)v(z,t) - \pi(z,t) \\
	A(t) &\equiv \tilde{\rho}(t)  - (\mu - g(t) + (\sigma - 1)\upsilon^2)\D[z] - \frac{\upsilon^2}{2}\D[zz]\\
	v(0,t) &= \int_{0}^{\bar{z}}v(z,t) \frac{e^{\sigma - 1}F'(z)}{F(\bar{z})} \diff z - \zeta\\
	0 &= (\sigma - 1)v(0,t) + \D[z]v(0,t)\\
	0 &= (\sigma - 1)v(\bar{z},t) + \D[z]v(\bar{z},t)
	\intertext{With the export threshold}
	0&=\hat{z}(t)^{\sigma-1}-  \kappa d^{\sigma - 1} \bar{\pi}_{\min}(t)^{-1}
	\intertext{And free-entry condition}
	0 &= v(0,t)- \zeta \frac{1-\chi}{\chi},\quad\text{ if } E(t) > 0
\end{align}

As described in the solution to the simple model, when the normalization is undone, the artificial $\bar{z}$ boundary used for the discretization is growing at the same speed as the economy and does create a fixed finite-support which would prevent long-run growth.

\subsection{Dynamic Equations and Discretization}
To discretize the DAE system presented in \cref{sec:full-ODE}, define the operator, in a way similar to \cref{eq:A-def-simple}
\begin{align}
	A(t) &\equiv \tilde{\rho}(t) I - (\mu - g(t) + (\sigma - 1)\upsilon^2) L^{-}_1 - \frac{\upsilon^2}{2} L_2 . \label{eq:A-def-full}
	\end{align}
As before, this operator implement the boundary conditions in the discretized operator to get the system of ODEs,
\begin{align}
	v'(t) &= A(t) v(t) - \pi(t).
\end{align}

The three algebraic variables required in the DAE are $g(t), \hat{z}(t),$ and $\Omega(t)$. The \cref{eq:normalized-vm-summary-rescaled} value matching equation uses the same discretization approach as in the simple example (combining everything into the weights $\omega$, with the truncated exponential distribution, etc.)
\begin{align}
	0 &= \Xi_1 v_1(t) - \omega \cdot v(t) + \zeta .
	\intertext{The export threshold equation is}
	0&=\hat{z}(t)^{\sigma-1}-  \kappa d^{\sigma - 1} \bar{\pi}_{\min}(t)^{-1} .
	\intertext{From \cref{sec:free-entry}, we see that the free-entry condition is}
	\Xi_1 v_1(t) -  \zeta \frac{1-\chi}{\chi} &\leq 0,\, = 0 \text{ if } E(t) > 0 .
\end{align}

\paragraph{Final Calculations}
Some additional calculations useful for analyzing the model, but not required for the intermediate calculations are
\begin{align}
	\lambda_{ii}(t) &= \frac{1}{1 + (N-1)\hat{z}(t)^{\sigma-1-\theta}d^{1-\sigma}}\label{eq:lambda-ii-t-summary}\\
c(t) &= (1 - \tilde{L}(t))\bar{z}(t) \label{eq:c-summary}\\
\log M(t) &= \int_0^t g(s)\diff s\label{eq:log-M-summary}\\
U(t) &= \int_0^{T-t}e^{-\rho \tau}\left(\log M(t+\tau)+\log c(t+\tau)\right)\diff \tau + \frac{e^{-\rho( T-t)}}{\rho^2}\left((1+\rho( T-t))g(T) + \rho\left(\log c(T) + \log M(T) \right) \right)\label{eq:U-dynamics-summary}\\
\bar{\pi}_{\text{rat}}(t) &= \theta\frac{1 - \hat{z}(t)^{-\theta + \sigma - 1}}{\theta - \sigma + 1} + (1 + (N-1)d^{1-\sigma})\theta\frac{\hat{z}(t)^{-\theta + \sigma - 1}}{\theta - \sigma + 1} + (N-1)\frac{\kappa}{\bar{\pi}_{\min}(t)} \hat{z}(t)^{-\theta}\label{eq:pi_rat-dynamics-summary}\\
&= \notag \frac{\theta}{1 + \theta - \sigma} + (N-1)d^{1-\sigma}\frac{(\sigma-1)\hat{z}^{\sigma-1-\theta}}{1 + \theta - \sigma}
\intertext{The calculation of the consumption equivalent ratio, useful to compare the $U(0)$ from above to an alternative $U^{\text{old}}(0))$ is}
M_{\text{CE}} &= \exp \left(\rho(U(0) - U^{\text{old}}(0))\right) .
\end{align}
In the above cases, given the interpolated solutions of the problem, we can use any quadrature method, including pre-built libraries, to calculate the integrals since they are only evaluated after the rest of the solution is complete.

\newpage
\appendix
\makeatletter
\def\@seccntformat#1{Appendix\ \csname the#1\endcsname\quad}
\makeatother
\makeatletter
\def\@seccntformat#1{\csname Pref@#1\endcsname \csname the#1\endcsname\quad}
\def\Pref@section{Appendix~}
\makeatother
\numberwithin{equation}{section}
%\let\normalsize\small
%\small

\section{Discretization}
\subsection{Spatial Discretization}\label{sec:discretization}
This will discretize space with backward differences in the first derivative, and central in space for the 2nd derivative.\footnote{Under a $g(t) > \gamma$ assumption (which may be a general requirement on parameter restrictions), the drift is negative, and the correct ``upwind'' finite difference scheme is always backwards.}  After discretizing the spatial dimension, we have a system of ODEs in time - which are solved using various time-stepping algorithms.

\begin{itemize}
	\item Define an irregular  grid $\set{z_i}_{i=0}^{P+1}$ with $z_0 = 0$ and $z_{P+1} = \bar{z}$ is a ``large'' number (keeping in mind that the effective number is $e^{\bar{z}}$).  After discretizing, we will denote the grid with the variable name, i.e., $z \equiv \set{z_i}_{i=0}^{P+1}$.
	\item Denote the distance between the grid points as the \textit{backwards} and \textit{forwards} difference respectively
	\begin{align}
	\Delta_{i,-} &\equiv z_i - z_{i-1},\, \text{for } i = 1,\ldots, P+1\\
	\Delta_{i,+} &\equiv z_{i+1} - z_i,\, \text{for } i = 0,\ldots, P
	\end{align}
	\item Define the vector of backwards and forwards first differences (padding with $0$s) as
	\begin{align}
	\Delta_{-} &\equiv \begin{bmatrix} 0 \\
	\text{diff}(z)
	\end{bmatrix}\in\R^{P+2}\label{eq:Delta-minus}\\
	\Delta_{+} &\equiv \begin{bmatrix} \text{diff}(z)\\
	0
	\end{bmatrix}\in\R^{P+2}
	\end{align}\label{eq:Delta-plus}
	\item The grid on time $t \in [0,T]$ may be adaptive, and we will let the ODE solver handle the grid in order to use optimal time-stepping algorithms.
	\item Denote time-varying functions on the grid as a vector without the spatial dimension (on the interior of the domain), i.e.,
	\begin{align}
	v(t) &\equiv \set{v(z_i, t)}_{i=1}^P\in\R^P\\
	\hat{\pi}(t) &\equiv	\hat{\pi}(z,t) \in \R^P
	\end{align}
\end{itemize}

\subsection{Boundary Extrapolation}\label{sec:boundary-extrapolation}
Note in the above definition, $v(t) \in \R^P$ and hence is not evaluated at the boundaries.  In the case of boundary points in the discretized model, define $v_0(t) \equiv v(z_0, t)$ and $v_{P+1}(t) \equiv v(z_{P+1},t)$.

Given a solution for $v(t)$, we can extrapolate to find the function at the boundaries $v_0(t)$ and $v_{P+1}(t)$ by using the boundary conditions from the PDE.  In the case of the boundary conditions in \cref{eq:new-BC1,eq:new-BC2}, substitute in the first differences (forward and backwards, respectively) and use the $z$ grid

\begin{align}
	\xi v(z_0, t) + \frac{v(z_1,t) - v(z_0,t)}{z_1 - z_0} &= 0\\
	\xi v(z_{P+1},t) + \frac{v(z_{P+1},t) - v(z_P,t)}{z_{P+1} - z_P} &= 0
\end{align}
Rearrange and use definitions of the vectors to find expressions for the extrapolation as linear functions of the interior nodes
\begin{align}
	v_0(t) &= \Xi_1 v_1(t) \label{eq:v0-1-bc}\\
	v_{P+1}(t) &=  \Xi_P v_P(t)\label{eq:vP-P1-bc}
	\intertext{Where,}
	\Xi_1 &\equiv \frac{1}{1 - \xi \Delta_{0,+}}\\
	\Xi_P &\equiv \frac{1}{1 + \xi \Delta_{P+1,-}}
\end{align}

\subsection{Discretized Operators}\label{sec:discretized-operators}
For completeness: From \url{https://github.com/QuantEcon/SimpleDifferentialOperators.jl/releases/download/dev/discretized-differential-operator-derivation.pdf} the discretizations for first and second derivatives with the irregular grid are (where $\underline{\xi}  = \bar{\xi}  = \sigma - 1$)
\begin{align}
	L_{1}^- &\equiv \begin{psmallmatrix}
	\Delta_{1,-}^{-1} [1 + (-1 + \underline{\xi} \Delta_{1,-})]^{-1} &0&0&\dots&0&0&0\\
	-\Delta_{2,-}^{-1}&\Delta_{2,-}^{-1}&0&\dots&0&0&0\\
	\vdots&\vdots&\vdots&\ddots&\vdots&\vdots&\vdots\\
	0&0&0&\dots&-\Delta_{P-1,-}^{-1}&\Delta_{P-1,-}^{-1}&0\\
	0&0&0&\cdots&0&-\Delta_{P,-}^{-1}&\Delta_{P,-}^{-1}
	\end{psmallmatrix}\\
	L_2 &\equiv 2 \begin{psmallmatrix}
	\Xi_1 &
	(\Delta_{1,+}+\Delta_{1,-})^{-1} \Delta_{1,+}^{-1}
	&0&\cdots&0&0&0 \\
	\vdots&\ddots&\ddots&\ddots&\ddots&\vdots&\vdots\\
	0&\cdots&
	(\Delta_{i,+}+\Delta_{i,-})^{-1} \Delta_{i,-}^{-1} &
	-\Delta_{i,-}^{-1} \Delta_{i,+}^{-1}  &
	 (\Delta_{i,+}+\Delta_{i,-})^{-1} \Delta_{i,+}^{-1}&\cdots&0 \\
	\vdots&\vdots&\vdots&\ddots&\ddots&\ddots&\vdots\\
	0&0&0&\cdots&0&(\Delta_{P,+}+\Delta_{P,-})^{-1} \Delta_{P,-}^{-1}&\Xi_P
	\end{psmallmatrix}
	\intertext{where,}
	\Xi_{1} &\equiv - (-1 + \underline{\xi} \Delta_{1,-})^{-1} (\Delta_{1,+} + \Delta_{1,-})^{-1}  \Delta_{1,-}^{-1} \\
	\Xi_{P} &\equiv  (1 + \bar{\xi} \Delta_{P,+})^{-1} (\Delta_{P,+} + \Delta_{P,-})^{-1}  \Delta_{P,+}^{-1}
\end{align}

\section{Derivations for Simple Model}\label{sec:simple-derivation}
\subsection{Equations Prior to Rescaling}

From the technical appendix of \cite{BenhabibPerlaTonetti2017} (but renaming the $v$ function to be $\hat{v}$ here to avoid confusion with our normalization), assume an exogenously given $\hat{\pi}(z,t)$ and $x(t)$ function.  We will only look at examples where $F(z) = 1 - e^{-\theta z}$ for all $t$.
\begin{align}
(r - g(t)) \hat{v}(z,t) &= \hat{\pi}(z,t) + (\mu- g(t)) \D[z] \hat{v}(z,t) + \frac{\upsilon^2}{2} \D[zz] \hat{v}(z,t) + \D[t]\hat{v}(z,t)\label{eq:bellman-GBM-dynamic}	\\
\hat{v}(0,t) &= \int_{0}^{\infty} \hat{v}(z,t) F'(z)\diff z - x(t)\label{eq:vm-GBM-dynamic}\\
\D[z]\hat{v}(0,t) &= 0\label{eq:sp-GBM-dynamic}
\end{align}

A solution to this problem is a $g(t)$ and $\hat{v}(z,t)$ that fulfills the above equations for all $t\in[0,T]$ and $z\in[0,\infty)$.  While not listed above, in practice another boundary condition (e.g. transversality) is used to ensure that \cref{eq:bellman-GBM-dynamic} can be solved. For this problem, an artificial reflecting barrier at a ``large'' $\bar{z}$ converges to the correct solution.  Again, keep in mind that for the pre-normalized solution, this artificial threshold is growing with the economy, and does not actually impose a bounded distribution (i.e., $\bar{Z}(t) = M(t) e^{\bar{z}}$).

Use the boundary value\footnote{Keep in mind that this is just a step in the numerical solution, rather than introducing a true reflecting barrier.  We will need to verify that it does not introduce issues by verifying the numeric solution matches the closed form solution for large $\bar{z}$ and compare to the analytic equation for external validity.  Also note that if $\upsilon = 0$, due to the upwind procedure this boundary value would be unnecessary and/or drop out of the solution.}
	\begin{align}
	\D[z]\hat{v}(\bar{z},t) &= 0\label{eq:reflecting-GBM-dynamic}
	\end{align}

\subsection{Change of Variables to Normalize and Rescale}\label{eq:simple-rescale}
Solving \cref{eq:bellman-GBM-dynamic} for $\hat{v}(z,t)$ on a grid is problematic since the scale goes from approximately $1$ to $e^{\bar{z}}$, which is very large for a high $\bar{z}$.  To make the solution more stable, we rescale the equation.  Choose some $\xi \geq 0$ for convenience and to ensure stability and let
\begin{align}
	v(z,t) &\equiv e^{-\xi z}\hat{v}(z,t) = e^{-\xi z}\frac{V(e^z M(t),t)}{M(t)}\\
	\intertext{Differentiate and reorganize this expression to yield}
	\D[z]\hat{v}(z,t) &= e^{\xi z}\left(\xi v(z,t) + \D[z]v(z,t) \right)\\
	\D[zz]\hat{v}(z,t) &= e^{\xi z}\left(\xi^{2} v(z,t) + 2 \xi\D[z]v(z,t) + \D[zz]v(z,t)  \right)\\
	\D[t]\hat{v}(z,t) &= e^{\xi z} \D[t]v(z,t)
\end{align}
Define $\pi(z,t) = e^{-\xi z}\hat{\pi}(z,t)$ so that if $\hat{\pi}(z,t) = e^{\xi z}$ then $\pi(z,t) = 1$.  Substitute into \cref{eq:bellman-GBM-dynamic}, divide by $e^{\xi z}$ and simplify,

\begin{align}
	\left(r - g(t)- \xi(\mu-g(t)) - \frac{\upsilon^2}{2}\xi^2\right)  v(z,t) &= \pi(z,t) + (\mu+ \upsilon^2\xi - g(t)) \D[z]v(z,t) \nonumber \\ &+ \frac{\upsilon^2}{2} \D[zz]v(z,t) + \D[t]v(z,t)  \label{eq:bellman-GBM-dynamic-normalized}
\end{align}

\noindent Now, substitute into the boundary conditions \cref{eq:sp-GBM-dynamic,eq:reflecting-GBM-dynamic} to find,
\begin{align}
	\xi v(0,t) + \D[z]v(0,t ) &= 0\label{eq:new-BC1}\\
	\xi v(\bar{z},t) + \D[z]v(\bar{z},t) &= 0\label{eq:new-BC2}
\end{align}
Finally, in the value matching condition, $\hat{v}(0,t) = e^{0} v(0,t)$ where if the $F'(z) = \theta e^{-\theta z}$ then,
\begin{align}
	 v(0,t) &= \int_{0}^{\infty}  v(z,t) \left(e^{\xi z} F'(z)\right) \diff z - x(t)\label{eq:vm-GBM-dynamic-normalized-not-truncated}
\end{align}
However, for the numerical approximation with the artificial boundary at $\bar{z}$, we need to use a truncated distribution for $F(z)$.  Since the support of $F(z)$ is $[0,\infty)$, and we truncate at $\bar{z} = \bar{z}$ then the equation in \cref{eq:vm-GBM-dynamic-normalized-not-truncated} becomes
\begin{align}
	 v(0,t) &= \int_{0}^{\bar{z}}  v(z,t) \left(e^{\xi z} \frac{F'(z)}{F(\bar{z})}\right) \diff z - x(t)\label{eq:vm-GBM-dynamic-normalized}
\end{align}

\subsection{Quadrature}\label{sec:quadrature}
With the discretized grid, the integral in \cref{eq:vm-GBM-dynamic-normalized} will be calculated with some quadrature rules.\footnote{An issue here is that the support of the integral is infinite, but the finite differences go to $\bar{z}$.  While not ideal, since $F'(\bar{z})\to 0$ rapidly, the Trapezoidal rule should be a  reasonable approximation.}  Recall that with a grid on $z$ we have that $z_0 \equiv 0$ and $z_{P+1} \equiv \bar{z}$.  Define Let $q \equiv \set{q(z_i)}_{i=0}^{P+1}$ for some function $q(\cdot)$, then a quadrature rule are weights $\bar{\omega} \in \R^{P+2}$ such that
\begin{align}
\int_0^{\bar{z}} q(z) \diff z &\approx \bar{\omega} \cdot q
\intertext{To derive the non-uniform trapezoidal rules for an arbitrary $q$ function,}
\int_{0}^{\bar{z}} q(z)\diff z &\approx \frac{1}{2}\sum_{i=0}^{P}(z_{i+1}-z_i)(q(z_i) + q(z_{i+1}))\\
\intertext{Use the forward difference $\Delta_{i,+} \equiv z_{i+1} - z_i$, and rearrange}
&= \frac1{2}\left((0 + \Delta_{0,+})q(z_0) + (\Delta_{0,+} + \Delta_{1,+})q(z_1) + \ldots (\Delta_{P-1,+} + \Delta_{P,+})q(z_{P})+(\Delta_{P,+} + 0)q(z_{P+1})\right)
\intertext{Since $\Delta_{i,+} = \Delta_{i+1,-}$ for all $i = 1, \ldots P$}
&= \frac1{2}\left((0 + \Delta_{0,+})q(z_0) + (\Delta_{1,-} + \Delta_{1,+})q(z_1) + \ldots (\Delta_{P,-} + \Delta_{P,+})q(z_{P})+(\Delta_{P+1,-} + 0)q(z_{P+1})\right)
\end{align}
Finally, use the $\Delta_{-}$ and $\Delta_{+}$ as defined from \cref{eq:Delta-minus,eq:Delta-plus} (note the use of a padding $0$ in those definitions) and group to find
\begin{align}
\int_{0}^{\bar{z}} q(z)\diff z &\approx \bar{\omega}\cdot q\\
\intertext{Where,}
\bar{\omega}&\equiv \frac1{2}(\Delta_{-} + \Delta_{+})\in\R^{P+2}\\
\intertext{Moving to the specific integral in \cref{eq:vm-GBM-dynamic-normalized},}
\int_{0}^{\bar{z}}  v(z,t) \left(e^{\xi z} \frac{F'(z)}{F(\bar{z})}\right) \diff z &\approx \sum_{i=0}^{P+1} \bar{\omega}_i e^{\xi z_i}\frac{F'(z_i)}{F(\bar{z})}v(z_i,t)
\intertext{With this, define the $\omega \in R^P$ weights to combine the $\bar{\omega}$ and the $\left(e^{\xi z} \frac{F'(z)}{F(\bar{z})}\right)$ terms, and use \cref{eq:vP-P1-bc,eq:v0-1-bc} so that}
\omega_i &\equiv\begin{cases}
\Xi_1\bar{\omega}_0 e^{\xi z_0}\frac{F'(z_0)}{F(\bar{z})} + \bar{\omega}_1 e^{\xi z_1}\frac{F'(z_1)}{F(\bar{z})} & \text{ for } i = 1\\
\bar{\omega}_i e^{\xi z_i}\frac{F'(z_i)}{F(\bar{z})} & \text{ for } i = 2, \ldots P-1\\
\bar{\omega}_P e^{\xi z_P}\frac{F'(z_P)}{F(\bar{z})} +  \Xi_P\bar{\omega}_{P+1} e^{\xi z_{P+1}}\frac{F'(z_{P+1})}{F(\bar{z})}& \text{ for } i = P
\end{cases}\label{eq:omega-i}
\intertext{Then with the discretized vector $v(t) \in \R^P$, the integral in \cref{eq:vm-GBM-dynamic-normalized} becomes,}
\Xi_1 v_1(t) &= \omega \cdot v(t) - x(t)
\end{align}

\section{Derivations of Full Model}\label{sec:full-model-derivations}

\subsection{Static Objects and Algebraic Solution}\label{sec:full-algebraic-solution}

Note that we have the following static calculations from PTW (H.1) to (H.11)

\begin{align}
	F(z) &= 1 - z^{-\theta} \\
	S &= \theta \left(g - \mu - \theta \frac{\upsilon^2}{2}\right) \\
	\nu &\equiv  \frac{\mu- g}{\upsilon^2} + \sqrt{\left(\frac{g-\mu}{\upsilon^2} \right)^{2} + \frac{r-g}{\upsilon^2/2}}\\
	a &= \frac{1}{r - g - (\sigma - 1)(\mu - g  + (\sigma - 1)\upsilon^2/2)} \\
	b &= (1 - a(r-g))d^{1-\sigma}\hat{z}^{\nu + \sigma -1} \\
	r &= \rho + \gamma g + \delta \\
	\tilde{L} &= \Omega [(N-1)\hat{z}^{-\theta}\kappa + (1-\eta)\zeta(S + \delta/\chi)] \\
	\tilde{L}_{\text{x}} &= \Omega (N -1)\hat{z}^{-\theta}\kappa\\
	\tilde{L}_{\text{E}} &= \frac{\zeta}{\chi}\Omega\delta\\
	\tilde{L}_{\text{a}} &= \zeta \Omega S\\
%	\bar{z} &= [\Omega(\mathbb{E}[z^{\sigma - 1}] + (N-1)(1 - F(\hat{z}))d^{1 - \sigma}\mathbb{E}[z^{\sigma - 1}|z > \hat{z}])]^{1/(\sigma - 1)} \\
	\bar{z}&\equiv \left[\Omega
	\frac{\theta}{1+\theta - \sigma}\left(1 + (N-1)d^{1-\sigma}\hat{z}^{\sigma - 1 -\theta} \right)\right]^{1/(\sigma - 1)} \\
	\hat{z} &= d \left(\frac{\kappa}{\bar{\pi}_{\textup{min}}}\right)^{\frac{1}{\sigma - 1}} \\
	w &= \frac{\sigma - 1}{\sigma}\bar{z} \\
	x &= \zeta(1 - \eta + \eta \Theta / w) \\
		c &= (1 - \tilde{L})\bar{z} \\
		\bar{U} &=
	\begin{cases}
	\frac{\rho \log(c) + g}{\rho^2}, & \gamma = 1 \\
	\frac{1}{1-\gamma} \frac{c^{1 - \gamma}}{\rho + (\gamma - 1)g} & \text{otherwise}
	\end{cases}
\end{align}

Note also that the following formula for the ratio of aggregate to minimum profits, PTW (31), holds in steady state:

\begin{equation}
	\bar{\pi}_{\text{rat}} \equiv \frac{\left(\theta + (N-1)(\sigma-1)d^{-\theta}\big(\frac{\kappa}{\zeta} \frac{\chi}{\rho (1-\chi)}\big)^{1 - \frac{\theta}{\sigma - 1}}\right)}{1 + \theta - \sigma}
\end{equation}

Or:

\begin{equation*}
\bar{\pi}_{\text{rat}} = \frac{\theta}{1 + \theta - \sigma} + (N-1)d^{1-\sigma}\frac{(\sigma-1)\hat{z}^{\sigma-1-\theta}}{1 + \theta - \sigma}
\end{equation*}

The above gives us a system of three nonlinear equations to solve:

		\begin{align}
		\frac{x}{\bar{\pi}_{\min}} &=  a \frac{\chi}{1-\chi}\frac{\sigma + \nu - 1}{\nu} , \\
		1 + \frac{\sigma - 1}{\nu} &= {\scriptstyle \frac{\frac{\nu  (n-1) (\theta -\sigma +1) \left(d^{1-\sigma } (\theta +\nu ) \hat{z}^{-\theta +\sigma -1}-b \theta  \hat{z}^{-\theta -\nu }\right)}{a (g-r)}+\theta  \left(\nu  (n-1) d^{1-\sigma } (\theta +\nu ) \hat{z}^{-\theta +\sigma -1}+(\nu +\sigma -1) (\theta +\nu -\sigma +1)\right)}{\nu  (\theta +\nu ) (\theta -\sigma +1)} -  \frac{\chi}{1-\chi}\frac{\sigma + \nu - 1}{\nu}} , \\
		\bar{\pi}_{\min} &= \tfrac{1 - \tilde{L}}{(\sigma - 1) \bar{z}^{\sigma - 1}}.
		\end{align}

\subsection{Normalization and Rescaling}\label{sec:full-rescaling}
Note: unlike the main paper we have $z \equiv \log(Z/M(t))$ throughout these notes.  The value is normalized as $\hat{v}(z,t) = \frac{V(e^z M(t),t)}{M(t)w(t)}$.  With this,
\begin{align}
V(Z,t) &:= w(t) M(t) \hat{v}(t, \log(Z/M(t)))\label{eq:V-norm}\\
\intertext{Differentiate \cref{eq:V-norm} with respect to $t$, divide by $w(t)M(t)$, and use the definitions $z := \log(Z/M(t)), g(t):= M'(t)/M(t)$ and $g_w(t) := W'(t)/W(t)$}
\frac{1}{w(t) M(t)}\D[t]V(Z,t) &= \left(g(t) + g_w(t)\right)\hat{v}(z,t) - g(t)\D[z]\hat{v}(z,t) + \D[t]\hat{v}(z,t) \label{eq:dV-dt}\\
\intertext{Similarly, differentiate \cref{eq:V-norm} with respect to $Z$,}
\frac{1}{w(t) M(t)}\D[Z]V(Z,t) &= \frac{1}{Z}\D[z]\hat{v}(z,t)\label{eq:dV-dZ}\\
\frac{1}{w(t) M(t)}\D[ZZ]V(Z,t) &= \frac{1}{Z^2}\left(\D[zz]\hat{v}(z,t)-\D[z]\hat{v}(z,t)\right)\label{eq:dV-dZZ}
\end{align}
Take the unnormalized Bellman equation from the paper, repeated below,
\begin{align}
r(t) V(Z,t) &=  \Pi(Z,t)+ \left(\mu + \frac{\upsilon^2}{2}\right) Z\, \D[Z]V(Z,t)+ \frac{\upsilon^2}{2} Z^2 \D[ZZ]V(Z,t) +  \D[t]V(Z,t), \label{ap-eq:bellman-deterministic-prenorm}
\intertext{Use $\pi(z,t) \equiv \frac{\Pi(Z,t)}{w(t)M(t)}$ with the new $z$, divide \cref{ap-eq:bellman-deterministic-prenorm} by $w(t)M(t)$ and then use \cref{eq:dV-dt,eq:dV-dZ,eq:dV-dZZ} to find,}
(r(t) - g(t) - g_w(t))\hat{v}(z,t) &= \pi(z,t) + (\mu - g(t))\D[z]\hat{v}(z,t) + \frac{\upsilon^2}{2}\D[zz]\hat{v}(z,t) + \D[t]\hat{v}(z,t)\label{eq:normalized-bellman}
\intertext{The smooth pasting condition, $\D[Z]V(M(t),t) = 0$ becomes}
\D[z]\hat{v}(0,t) &= 0\label{eq:normalized-sp}
\intertext{Take the value matching condition from the paper and divide by $M(t)w(t)$,}
\frac{{V}(M(t),t)}{M(t)w(t)} &= \int_{M(t)}^{\infty}\frac{{V}(Z,t)}{M(t)w(t)} \phi(Z,t) \diff Z - \frac{X(t)}{M(t)w(t)}
\intertext{Substitute for $\hat{v}(z,t)$ and $x(t) \equiv \frac{X(t)}{M(t)w(t)}$,}
\hat{v}(0,t) &= \int_{0}^{\infty}\hat{v}(\log(Z/M(t)),t) \phi(Z,t) \diff Z - x(t)
\intertext{With a change of variables in the integral to  $z = \log(Z/M(t))$,}
\hat{v}(0,t) &= \int_{0}^{\infty}\hat{v}(z,t) f(z,t) \diff z - x(t)\label{eq:normalized-vm}
\intertext{We will only solve versions of the model starting from the steady-state normalized distribution of the GBM process, which is a Pareto distribution with tail index $\theta$ and minimum $M(t)$.  If $\phi(Z,t) = \theta M(t)^{\theta}Z^{-(1+\theta)}$ then}
f(z) &= \theta e^{-\theta z}\label{eq:f-stationary}
\end{align}
for all $t$.  It can be proven that this will be maintained by the KFE in the setup for any $g(t)$ sequence.

While this transformation could be done all at once, we will base it off of the previous section for easier comparison.  Define the following
\begin{align}
v(z,t) &\equiv e^{-(\sigma - 1)z}\hat{v}(z,t)\label{eq:v-tilde}\\
\pi(z,t) &\equiv e^{-(\sigma - 1)z}\hat{\pi}(z,t)
\end{align}
Rearrange and differentiate \cref{eq:v-tilde},
\begin{align}
\D[t]\hat{v}(z,t) &= e^{(\sigma - 1)z} \D[t]v(z,t)\label{eq:v-tilde-dt}\\
\D[z]\hat{v}(z,t) &= e^{(\sigma - 1)z}\left((\sigma - 1) v(z,t) + \D[z]v(z,t) \right)\label{eq:v-tilde-dz}\\
\D[zz]\hat{v}(z,t) &= e^{(\sigma - 1)z}\left((\sigma - 1)^2 v(z,t) + 2(\sigma - 1)\D[z]v(z,t) + \D[zz]v(z,t)\right)\label{eq:v-tilde-dzz}\\
\intertext{And at the adoption threshold, from \cref{eq:v-tilde-dz}}
\D[z]\hat{v}(0,t) &= (\sigma - 1) v(0,t) + \D[z]v(0,t)\label{eq:vt-0-dz}
\end{align}

\noindent To use these substitutions, start with \cref{eq:normalized-vm} and use the definition of $f(z)$,
\begin{align}
v(0,t) &= \theta \int_{0}^{\infty} v(z,t) e^{(-\theta + \sigma - 1)z} \diff z - x(t)\label{eq:normalized-vm-summary-rescaled}\\
\intertext{Combine \cref{eq:normalized-sp,eq:vt-0-dz} to get}
0 &= (\sigma - 1) v(0,t) + \D[z]v(0,t)\label{eq:normalized-sp-summary-rescaled}
\intertext{Finally, substitute all of the derivatives into \cref{eq:normalized-bellman} and divide by $e^{(\sigma - 1)z}$}
\tilde{\rho}(t)  v(z,t) &= \pi(z,t) + (\mu - g(t) + (\sigma - 1)\upsilon^2)\D[z]v(z,t) + \frac{\upsilon^2}{2}\D[zz]v(z,t) + \D[t]v(z,t)
\intertext{Where,}
\tilde{\rho}(t) &\equiv  r(t) - g(t) - g_w(t) - (\sigma - 1)\left(\mu - g(t) + (\sigma - 1)\frac{\upsilon^2}{2} \right)\label{eq:rhot}
\end{align}

\noindent From (PTW.C.26 to C.28) using the $z\equiv\log(Z/M(t))$ definition,
\begin{align}
	\hat{\pi}(z,t) &= \pi_{\min}(t) e^{(\sigma - 1)z}\left(1 + (N-1)d^{1-\sigma}\indicator{z \geq \log(\hat{z}(t))}\right) - (N-1)\kappa\indicator{z \geq \log(\hat{z}(t))}\label{eq:pi-z-t-summary}
\intertext{Multiply by $e^{-(\sigma - 1)z}$ to get,}
\pi(z,t) &\equiv \bar{\pi}_{\min}(t)\left(1 + (N-1)d^{1-\sigma}\indicator{z \geq \log(\hat{z}(t))}\right) - (N-1)\kappa e^{-(\sigma - 1)z}\indicator{z \geq \log(\hat{z}(t))}
\end{align}

To calculate the profitability ratio of the mean-to-min firm, take from PTW (24) -- keeping in mind the change in notation to $z = \log(Z/M(t))$

\begin{align}
	\bar{\pi}_{\text{rat}}(t) &= \frac{1}{\bar{\pi}_{\min}}\int_0^{\infty} \hat{\pi}(z)f(z)\diff z\\
	\intertext{Use \cref{eq:pi-z-t-summary} and split the integral}
	&= \int_0^{\log(\hat{z}(t))} e^{(\sigma - 1)z} f(z) \diff z
	+  (1 + (N-1)d^{1-\sigma})\int_{\log(\hat{z}(t))}^{\infty}e^{(\sigma - 1)z} f(z) \diff z + (N-1)\frac{\kappa}{\bar{\pi}_{\min}(t)}\int_{\log(\hat{z}(t))}^{\infty}  f(z)\diff z
	\intertext{Use \cref{eq:f-stationary} and simplify}
	&= \theta\frac{1 - \hat{z}(t)^{-\theta + \sigma - 1}}{\theta - \sigma + 1} + (1 + (N-1)d^{1-\sigma})\theta\frac{\hat{z}(t)^{-\theta + \sigma - 1}}{\theta - \sigma + 1} + (N-1)\frac{\kappa}{\bar{\pi}_{\min}(t)} \hat{z}(t)^{-\theta}\\
	&= \notag \frac{\theta}{1 + \theta - \sigma} + (N-1)d^{1-\sigma}\frac{(\sigma-1)\hat{z}^{\sigma-1-\theta}}{1 + \theta - \sigma}
\end{align}

\subsection{Computation of equilibrium $\Omega(t)$ and $E(t)$}\label{sec:Omega-E}

Recall that $E(t)$ is the \textbf{gross} entry flow is $E(t)\Omega(t)$.  Since the \textbf{gross} exit flow is $\delta \Omega(t)$, then the differential equation for $\Omega(t)$ comes from the net flows,
\begin{align}
	\D[t] \Omega(t) &= \left(E(t) - \delta \right)\Omega(t)\label{eq:E-defined-by-Omega}
%	\intertext{Rearrange,}
%	E(t) &\equiv \delta + \D[t]\log \Omega(t)
\end{align}
Which shows that in the steady-state, the entry must simply replace exogenous exit, $E(t) = \delta$ for $t \geq T$.

First, note that $\Omega(t)$ is defined by the solution of the differential equation $	\D[t] \Omega(t) = \left(E(t) - \delta \right)\Omega(t)$ from \cref{eq:E-defined-by-Omega} with boundary conditions of $\Omega(0) = \Omega_0$ and $\Omega(T) = \Omega_T$ where $\Omega_0$ and $\Omega_T$ are the stationary solutions for $\Omega(t)$ at $t = 0$ and $t = T$ respectively. Hence, it suffices to find the equilibrium $E(t)$. On the other hand, note that in steady-state we must have $E(t) = \delta$.  In the computation, we use cubic splines with nodes that are uniformly distributed across $[0,T]$.

To find the equilibrium $E(t)$, we first find $\widehat E(t)$ with an arbitrary endpoint on $[0,T]$ and rescale $\widehat E(t)$ by $Q$
\begin{align}\label{eq:E-normalized-defn}
	Q \widehat E(t) = E(t) - \delta
\end{align}
such that $Q \widehat E(T) = \delta$. Substituting \cref{eq:E-normalized-defn} in \cref{eq:E-defined-by-Omega}, we have
\begin{align}
\D[t] \Omega(t) = Q \widehat{E}(t) \Omega(t)
\end{align}
Solving for $\Omega(t)$, we have
\begin{align}
\Omega_T = \Omega_0 \exp \left(Q \int_0^T \widehat{E}(t) dt\right)
\end{align}
Which yields
\begin{align}
Q = \log \left(\dfrac{\Omega_T}{\Omega_0}\right)\left(\int_0^T \widehat E(t) dt \right)^{-1}
\end{align}
providing the analytic solution for $E(t)$ and $\Omega(t)$ given $\widehat{E} (t)$ with arbitrary endpoints.

\subsection{Free-entry Condition}\label{sec:free-entry}
To determine the $E(t)$ function, we need the free entry complementarity condition to hold.  Note that (PTW D.19) actually holds for any $t$ as well, and does not require the stationary solution.  From (PTW D.19) and using $v(t,0) = \hat{v}(t,0)$, we see that if $E(t) > 0$ then,
\begin{align}
v(t,0) &= \zeta \frac{1-\chi}{\chi}
\intertext{In the case that there is no entry, and $E(t) = 0$ for some $t$, from (PTW D.18)}
v(t,0) &< \zeta \frac{1-\chi}{\chi}
\end{align}

\subsection{Derivation of Rates of Change and Interest Rates}\label{sec:derive-interest-rates}
Since consumption is $C(t) := c(t) M(t)$, take logs and differentiate to get,
\begin{align}
\D[t]\log C(t) &= g(t) + g_c(t)
\intertext{With a CRRA parameter of $\gamma \geq 0$, the interest rate rate faced by a firm is,}
r(t) &= \rho + \delta + \gamma \D[t]\log C(t)\\
&= \rho+ \delta + \gamma(g_c(t)+ g(t))\label{eq:r-def}
\end{align}

\noindent From the PTW (C.34), for $\eta = 0$,
 \begin{align}
 \frac{c(t)}{w(t)}&\propto 1 - \tilde{L}(t)
 \intertext{Take the log and differentiate,}
g_c(t) - g_w(t) &= \D[t]\log\left(1 - \tilde{L}(t)\right)\label{eq:g-c-w-diff}
\intertext{Define,}
\tilde{r}(t) &\equiv r(t) - g(t) - g_w(t)\\
\intertext{From \cref{eq:r-def}}
&= \rho + \delta + \gamma(g_c(t)+ g(t)) - g(t) - g_w(t)\\
&= \rho + \delta + (g_c(t) - g_w(t)) + (\gamma - 1)(g_c(t) + g(t))\\
\intertext{From \cref{eq:g-c-w-diff}}
&= \rho + \delta + \D[t]\log\left(1 - \tilde{L}(t)\right) + (\gamma - 1)(g_c(t) + g(t))\\
\intertext{And in the $\log$ utility case with $\gamma = 1$, the last term drops, so that we have,}
 \tilde{r}(t) &=  \rho+ \delta + \D[t]\log\left(1 - \tilde{L}(t)\right)\label{eq:tilde-r-L}
 \end{align}

 \subsection{Derivations of Cutoffs and Profits}
 From (PTW H.11), the adoption cost relative to wages remains constant throughout the transition (i.e., assumed $\eta = 0$):
 \begin{align}
 x(t) &= \zeta\label{eq:x-zeta}
 \intertext{For the components of aggregate profits, we need to find $\pi_{\min}(t)$. In order to solve for the static $\hat{z}(t)$ condition, we will leave it as a variable in the equations.  Start with (PTW C.26)}
 \pi_{\min}(t) &= \frac{1-\tilde{L}(t)}{(\sigma-1)\bar{z}(t)^{\sigma-1}}\label{eq:pi-min-def}\\
 \intertext{From (PTW C.19), along the transition dynamics $E(t)$ is left general}
 \tilde{L}(t) &=\Omega(t)\left[(N-1)(1-F(\hat{z}(t)))\kappa + \zeta \left( S(t) + E(t)/\chi\right)\right]\label{eq:L-tilde-sub}\\
 &=\Omega(t)\left((N -1)\hat{z}^{-\theta}\kappa + \zeta \left(S(t) + E(t)/\chi \right)\right)\\
 \intertext{From (PTW C.10)}
 \bar{z}(t) &= \left[\Omega(t)\left(\expec{z^{\sigma - 1}} + (N-1)(1-F(\hat{z}(t)))d^{1-\sigma}\condexpec{z^{\sigma - 1}}{z > \hat{z}(t)}\right)\right]^{\frac{1}{\sigma - 1}}\\
 &= \left[\Omega(t)
 \frac{\theta}{1+\theta - \sigma}\left(1 + (N-1)d^{-\theta}\left(\frac{\hat{z}}{d} \right)^{\sigma - 1 -\theta} \right)\right]^{\frac{1}{\sigma - 1}}\label{eq:z-bar-sub}\\
 \end{align}

 \noindent Hence,
 \begin{align}
 \bar{z}(t)^{\sigma - 1}&= \Omega(t)
 \frac{\theta}{1+\theta - \sigma}\left(1 + (N-1)d^{1-\sigma}\hat{z}^{\sigma - 1 -\theta} \right)\label{eq:z-bar-sub-power}\\
 \intertext{Use (PTW C.29)}
 \hat{z}(t)&= d \left(\tfrac{\kappa}{\bar{\pi}_{\min}(t)} \right)^{\frac{1}{\sigma - 1}}\label{eq:z-hat-def}
\intertext{Reorganize \cref{eq:z-hat-def} to find an implicit equation in $\hat{z}$ at every $t$,}
 0&=\hat{z}^{\sigma-1}-  \kappa d^{\sigma - 1} \bar{\pi}_{\min}(t)^{-1}\label{eq:z-hat-power}
 \end{align}
 Note that \cref{eq:z-hat-power} provides an implicit equation in $\hat{z}(t)$ given an exogenous $\Omega(t)$ and $g(t)$ and \cref{eq:L-tilde-sub}, and can be solved separately for each $t$.  From the implicit $\hat{z}(t)$, calculate the home trade share through (PTW C.47),
 \begin{align}
 \lambda_{ii}(t) &= \frac{1}{1 + (N-1)\hat{z}(t)^{\sigma-1-\theta}d^{1-\sigma}}\label{eq:lambda-ii-t}\\
\intertext{Use (PTW C.49) to calculate,}
 \bar{\pi}_{\min}(t) &= \frac{(N-1) \hat z(t) ^{-\theta}\kappa}{1-\lambda_{ii}(t)} \label{eq:pi-bar-t}
 \end{align}

\subsection{Welfare Calculations}
Assume that there is a $T > 0$ such that the equilibrium reaches a steady state.   Given that $g(t) \equiv \D[t]\log M(t)$.  With this,
\begin{align}
M(t) &=M(0)\times\begin{cases}
\exp\left(\int_0^t g(s)\diff s \right) & \text{ if } 0 \leq t \leq T\\
\exp\left(\int_0^{T} g(s)\diff s  + (t - T)\bar{g} \right) & t \geq T
\end{cases}\label{eq:M-t-sol}
\intertext{Assume wlog that $M(0) = 1$, then,}
\log M(t) &= \int_0^t g(s)\diff s\label{eq:log-M}
\end{align}

\subsection*{Intermediate Objects}

Before proceeding, define the following intermediate objects (see Appendix H of PTW):

\begin{align}
	y &= c = (1 - \tilde{L})\bar{z} \\
	\lambda_{ii} &= \frac{1}{1 + (N-1)\hat{z}^{\sigma - 1 - \theta}d^{1-\sigma}}
\end{align}
Note that $y = c$ here because we are only solving the special case of all adoption being done with labor (i.e., $\eta = 0$).

At the steady state, define $\bar{g}$ and $\bar{c}$ as the growth rate and consumption relative to real wages for $t \geq T$.  The consumer welfare can be calculated as from (PTW 1),
\begin{align}
U(t) &= \int_0^{\infty}e^{-\rho \tau}\log C(t+\tau)\diff \tau\\
&= \int_0^{\infty}e^{-\rho \tau}\log M(t+\tau) \diff \tau + \int_0^{\infty}e^{-\rho \tau}\log c(t+\tau)\diff \tau\label{eq:U-def}
\intertext{Given the convergence at time $T$, use \cref{eq:U-def} to split the welfare integral,}
U(t) &= \int_0^{T-t}e^{-\rho \tau}\left(\log M(t+\tau)+\log c(t+\tau)\right)\diff \tau + \int_{T-t}^{\infty}e^{-\rho \tau}\left(\log M(t+\tau)+\log c(t+\tau)\right)\diff \tau\\
\intertext{Note that $c(t)= \bar{c}$ for $t \geq T$,}
&= \int_0^{T-t}e^{-\rho \tau}\left(\log M(t+\tau)+\log c(t+\tau)\right)\diff \tau + \frac{e^{-\rho( T-t)}}{\rho^2}\left(\bar{g} + \rho\left(\log \bar{c} + T \bar{g} \right) \right)\label{eq:U-dynamics}
\intertext{With (PTW G.31) and (PTW G.33) and \cref{eq:f-stationary-summary}}
c(t) &= \left(\frac{\theta}{1-\sigma+\theta}\right)^{\frac{1}{\sigma-1}}\left(1 - \tilde{L}(t)\right)\Omega(t)^{\frac{1}{\sigma - 1}} \lambda_{ii}(t)^{\frac{1}{1-\sigma}}\label{eq:c-def}
\end{align}

\subsection{Consumption Equivalent}
Note that we need to consider if $M(0) \neq 1$.  Modify \cref{eq:log-M}
\begin{align}
\log M(t) &= \log M(0) + \int_0^t g(s)\diff s\label{eq:log-M-notone}\\
\intertext{Hence, modifying \cref{eq:U-def} to get the welfare at time $0$ given a $M(0)$}
U(0; M(0)) &= \log M(0)\int_0^{\infty}e^{-\rho \tau} \diff \tau + \int_0^{\infty}e^{-\rho \tau}\left(\int_0^\tau g(s)\diff s\right)\diff \tau + \int_0^{\infty}e^{-\rho \tau}\log c(\tau)\diff \tau\label{eq:U-def-notone}
\end{align}
If we were in the steady state, then the second two terms are the welfare if $M(0) = 1$, hence,
\begin{align}
U(0, M(0)) &= \frac{\log M(0)}{\rho} + U(0, 1)
\end{align}

Calculate the welfare at the steady state before the shock (which assumes a $M(0) = 1$), defined as $U^{\text{old}}(0)$, then calculate from \cref{eq:U-dynamics} the welfare at time $0$ after the shock as just realized, and calculated as $U(0)$, then to find the indifference level of $M(0)$, equate
\begin{align}
	U(0) &= U^{\text{old}}(0)  + \frac{\log M(0)}{\rho}\\
	\intertext{Solve for $M(0)$}
	M(0) &= \exp \left(\rho(U(0) - U^{\text{old}}(0))\right)
\end{align}

Then the $M(0)$ should be greater than $1$, and it is the inverse of the proportion of consumption the would be willing to give up to change regimes.

For general $\gamma$ (i.e., $\gamma \neq 1$), the result is:

\begin{align}
M(0) &= \left(\frac{U(0)}{U^{\text{old}}(0)}\right)^\frac{1}{1 - \gamma}
\end{align}

\newpage
\bibliography{numerical_algorithm}

\end{document}


% C-c C-o to insert the block

% Individual equation: equation* block
% Inline equation \begin{math}\frac{sin(x)}{x}\end{math}
\documentclass{article}

\usepackage{amsmath,amssymb}

\ifdefined\ispreview
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{math}
\PreviewEnvironment{equation*}
\fi

\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator*{\argmin}{arg\,min}

\begin{document}

\subsection{Page 3}

Each search starts from the root state following the most promising actions,
selected using the utility value U(s, a), proportional to
\begin{math}Q(s, a) + \frac{P(s, a)}{1+N(s,a)}\end{math}.

\subsection{Page 4}
Once the self-play game has been finished and the final outcome has become
known, every step of the game is added to the training dataset, which is a list
of tuples \begin{math}(s_t, \pi_t, r_t)\end{math},
where \begin{math}s_t\end{math} is the game state, \begin{math}\pi_t\end{math} is the action
probabilities calculated from MCTS search sampling and \begin{math}r_t\end{math} is the game’s outcome
from the perspective of the player at step t.

With this at hand, our training is simple: we sample minibatches from replay
buffer of training examples and minimizing the MSE between the value head
prediction and actual position value and cross-entropy loss between predicted
probabilities and sampled probabilities \begin{math}\pi\end{math}.

\subsection{Page 11}
The final function in the class returns the probability of actions and the
action values for game state using the gathered statistics during the MCTS
searches. There are two modes of probability calculation, specified by the
parameter \begin{math}\tau\end{math}. If it equals to zero, the selection becomes deterministic, as we
select the most frequently visited action. In other cases, the distribution
given by
\begin{math}\frac{N(s,a)^\frac{1}{\tau}}{\sum_kN(s,k)^\frac{1}{\tau}}\end{math}
is used, which, again, improves exploration.

\subsection{Page 12}
The function accepts lots of parameters, including MCTS class instance (one or
two, as we potentially want to use separate MCTS statistics for different neural
networks), optional replay buffer, networks to be used during the game, amount
of game steps need to be taken before the \begin{math}\tau\end{math} parameter used for the action
probability calculatation will be changed from 1 to 0. Other options are amount
of MCTS searchers to perform and other flags.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\setlength{\parindent}{0pt}

\newtheorem*{theorem}{Theorem}
\newtheorem*{definition}{Definition}
\newtheorem*{lemma}{Lemma}
\newtheorem*{corollary}{Corollary}
\newtheorem{example}{Example}
\newtheorem*{trick}{Trick}
\newtheorem*{question}{Question}

\title{Lecture 14: Non-independent Variables}
\author{}
\date{}

\begin{document}

\maketitle

\section{Motivation}

We often need to deal with multivariable functions with non-independent
variables. A real world example is that in physics some physical quantities can
be described as a function of pressure $P$, volume $V$, and temperature $T$,
where these three quantities satisfy a relation as $PV = nRT$.

In general, a multivariable function with non-independent variables can be
described as $f(x, y, z)$, where $g(x, y, z) = c$.

In the previous lecture, we use Lagrange multipliers to find the local maximum
or minimum points for such functions. We want to study more details about those
functions, such as calculating the rate of change of the function value with
respect to a certain variable.

\section{Partial Derivatives of Non-independent Variables}

Since $g(x, y, z) = c$, any of the three variables can be described as a
function of other two variables, though in most cases it is hard to find the
explicit formula for the function. For example, we know that the variable $z$
can be described as $z = z(x, y)$. In this case, $x$ and $y$ are considered as
independent variables. Though we might not be able to explicitly express the
non-independent variables as functions of independent variables, we still want
to study the relationship, such as how those non-independent variables change
with regards to independent variables.

\begin{example}
  For $x^2 + yz + z^3 = 8$ at $(2, 3, 1)$, calculate the partial derivatives of
  $z$ with regards to $x$ and $y$.

  According to the total differential formula,
  \begin{equation*}
    2xdx + zdy + (y + 3z^2)dz = 0
  \end{equation*}
  At the point $(2, 3, 1)$, the total differential becomes
  \begin{equation*}
    4dx + dy + 6dz = 0
  \end{equation*}
  If we consider $z$ as the non-independent variable, in other words
  $z = z(x, y)$, then
  \begin{equation*}
    dz = -\frac{1}{6}(4dx + dy)
  \end{equation*}
  By total differential formula, we can derive that
  \begin{gather*}
    \frac{\partial z}{\partial x} = -\frac{2}{3} \\
    \frac{\partial z}{\partial y} = -\frac{1}{6} \\
  \end{gather*}
  The above partial derivatives can also be derived by setting $x$ and $y$ as
  constant respectively, hence the $dx$ and $dy$ would become 0 respectively in
  the total differential formula of $z$.
\end{example}

In general, when $g(x, y, z) = c$, according to the total differential formula,
\begin{gather*}
  dg = g_xdx + g_ydy + g_zdz = 0 \\
  dz = -\frac{g_x}{g_z}dx - \frac{g_y}{g_z}dy \\
  \frac{\partial z}{\partial x} = -\frac{g_x}{g_z} \\
  \frac{\partial z}{\partial y} = -\frac{g_y}{g_z} \\
\end{gather*}
Using the same method, we can also find $\frac{\partial x}{\partial y}$,
$\frac{\partial x}{\partial z}$, $\frac{\partial y}{\partial x}$,
$\frac{\partial y}{\partial z}$ in the cases where they are considered as the
non-independent variables.

\section{Pitfall in Notation}

\begin{example}
  Suppose that there is a multivariable function $f(x, y) = x + y$, then
  \begin{equation*}
    \frac{\partial f}{\partial x} = 1
  \end{equation*}
  With changes of variables $x = u$, and $y = u + v$, the function becomes
  \begin{equation*}
    f(u, v) = 2u + v
  \end{equation*}
  Then
  \begin{equation*}
    \frac{\partial f}{\partial u} = 2
  \end{equation*}
  It turns out that although $x = u$,
  \begin{equation*}
    \frac{\partial f}{\partial x} \neq \frac{\partial f}{\partial u}
  \end{equation*}
  How to explain this?

  The reason is that the original notation of partial derivatives has implicit
  meanings. In this case, $\frac{\partial f}{\partial x}$ means the rate of
  change of $f$ with regards to $x$ while keeping $y$ constant, and
  $\frac{\partial f}{\partial u}$ means the rate of change of $f$ with regards
  to $u$ while keeping $v$ constant. While $x$ and $u$ are the same variables,
  keeping $y$ constant and keeping $v$ constant are different. According to
  $y = u + v$, when we vary $u$ or $x$, keeping $y$ constant means $v$ are going
  to change as well, and keeping $v$ constant means $y$ are going to change.
\end{example}

Therefore, we need a new notation to make the implicit meaning of partial
derivatives explicit:
\begin{gather*}
  (\frac{\partial f}{\partial x})_y: \textnormal{the rate of change of f with regards to x while keeping y constant} \\
  (\frac{\partial f}{\partial u})_v: \textnormal{the rate of change of f with regards to u while keeping v constant} \\
  (\frac{\partial f}{\partial x})_v: \textnormal{the rate of change of f with regards to x while keeping v constant} \\
\end{gather*}
Therefore, in terms of the above cases, the inequality relation actually
describes:
\begin{equation*}
  (\frac{\partial f}{\partial x})_y \neq (\frac{\partial f}{\partial u})_v
\end{equation*}

The use case of the new notation in this lecture is that it helps us to specify
among the variables of a multivariable function which ones are considered as
independent. The variables appearing in the new notation are independent
variables.

\section{Partial Derivatives of Function Value with regards to Independent Variables}

\begin{example}
  Find the rate of change of the area of the following triangle with regards to
  the angle $\theta$ in the diagram.

  \begin{tikzpicture}
    [help line/.style={dashed}]
    \draw (0, 0) -- (2, 0) -- (2, 2) -- (0, 0);
    \draw (1, 0) node[below] {a};
    \draw (1, 1) node[above left] {b};
    \draw (0.2, 0) arc [start angle=0, end angle=45, radius=0.2] node[right]
      {$\theta$};
    \draw (2, 0.2) -- (1.8, 0.2) -- (1.8, 0);
  \end{tikzpicture}

  Since the triangle is a right triangle, the three variables $a$, $b$, and
  $\theta$ cannot be independent at the same time. They satisfy the following
  constraint:
  \[ a = b\cos\theta \]
  According to the geometric knowledge, the area of the triangle is
  \[ A = \frac{1}{2}ab\sin\theta \]
  We need to find the rate of change of $A$ with regards to $\theta$.

  Since there is a non-independent variable, we need to discuss different
  possible cases by considering which variables are considered as independent:
  \begin{itemize}
    \item If we consider the variables $a$ and $\theta$ as independent
      variables, then the rate of change of $A$ with regards to $\theta$ can be
      expressed as $(\frac{\partial A}{\partial \theta})_a$.
    \item If we consider the variables $b$ and $\theta$ as independent
      variables, then the rate of change of $A$ with regards to $\theta$ can be
      expressed as $(\frac{\partial A}{\partial \theta})_b$.
  \end{itemize}

  Suppose we consider $a$ and $\theta$ as independent variables and want to find
  $(\frac{\partial A}{\partial \theta})_a$, there are two methods:
  \begin{itemize}
    \item Method 1: Use the total differential formula.

      Let's look at the constraint first.
      \begin{gather*}
        a = b\cos\theta \\
        a - b\cos\theta = 0 \\
        da - \cos\theta db + b\sin\theta d\theta = 0 \\
        (\frac{\partial b}{\partial \theta})_a = b\tan\theta \\
      \end{gather*}
      Then we look at the multivariable function.
      \begin{gather*}
        A = \frac{1}{2}ab\sin\theta \\
        dA = \frac{1}{2}b\sin\theta da + \frac{1}{2}a\sin\theta db + \frac{1}{2}ab\cos\theta d\theta \\
      \end{gather*}
      When we try to calculate $(\frac{\partial A}{\partial \theta})_a$, we
      consider the variable $a$ as constant. Then
      \begin{gather*}
        \frac{1}{2}b\sin\theta da = \frac{1}{2}b\sin\theta \cdot 0 = 0 \\
        \begin{split}
          \frac{1}{2}a\sin\theta db &= \frac{1}{2}a\sin\theta ((\frac{\partial b}{\partial \theta})_a d\theta + (\frac{\partial b}{\partial a})_\theta da) \\
                                    &= \frac{1}{2}a\sin\theta ((\frac{\partial b}{\partial \theta})_a d\theta + (\frac{\partial b}{\partial a})_\theta \cdot 0) \\
                                    &= \frac{1}{2}a\sin\theta \cdot (\frac{\partial b}{\partial \theta})_a \cdot d\theta \\
                                    &= \frac{1}{2}ab\sin\theta \tan\theta \cdot d\theta
        \end{split} \\
        \begin{split}
          dA &= \frac{1}{2}b\sin\theta da + \frac{1}{2}a\sin\theta db + \frac{1}{2}ab\cos\theta d\theta \\
             &= \frac{1}{2}ab\sin\theta \tan\theta \cdot d\theta + \frac{1}{2}ab\cos\theta d\theta \\
             &= \frac{1}{2}ab(\sin\theta \tan\theta + \cos\theta) d\theta \\
             &= \frac{1}{2}ab\sec\theta d\theta \\
        \end{split} \\
        (\frac{\partial A}{\partial \theta})_a = \frac{1}{2}ab\sec\theta \\
      \end{gather*}
    \item Method 2: Use the chain rule.

      According to the chain rule,
      \begin{gather*}
        (\frac{\partial A}{\partial \theta})_a = A_\theta (\frac{\partial \theta}{\partial \theta})_a + A_a (\frac{\partial a}{\partial \theta})_a + A_b (\frac{\partial b}{\partial \theta})_a \\
        (\frac{\partial A}{\partial \theta})_a = A_\theta \cdot 1 + A_a \cdot 0 + A_b (\frac{\partial b}{\partial \theta})_a \\
        (\frac{\partial A}{\partial \theta})_a = A_\theta + A_b (\frac{\partial b}{\partial \theta})_a \\
        (\frac{\partial A}{\partial \theta})_a = \frac{1}{2}ab\cos\theta + \frac{1}{2}a\sin\theta \cdot b\tan\theta \\
        (\frac{\partial A}{\partial \theta})_a = \frac{1}{2}ab(\cos\theta + \sin\theta \tan\theta) \\
        (\frac{\partial A}{\partial \theta})_a = \frac{1}{2}ab\sec\theta \\
      \end{gather*}
  \end{itemize}
  We can see the two methods reach to the same results.
\end{example}

\end{document}


\documentclass[12pt,letterpaper]{article}
\usepackage[backend=biber]{biblatex}
\usepackage[margin=1.00in]{geometry}
\usepackage{minted}
\usepackage{csquotes}

\addbibresource{references.bib}

\title{Lambda Calculus as a Programming Language}
\author{Jeffrey Lin}

\begin{document}
\maketitle

\section{Introduction}
The Lambda Calculus was invented by the mathematician Alonzo Church in the 1930s around the same time Alan Turing came up with the idea of the Turing Machine.
It was later proved that the Lambda Calculus and the Turing Machine are equivalent: whatever computation one can represent, the other can as well.
While the Turing Machine is the basis for imperative languages and their constructs today, the Lambda Calculus is the basis of functional programming.

Note that for the purposes of our model, we define the Lambda Calculus using \citeauthor{Harrison_1997}'s \citetitle{Harrison_1997}.

\section{Defining and Manipulating Terms}

There are three categories of terms in the Lambda Calculus: variables, applications, and abstractions.
In OCaml, we can therefore represent the type term as:
\begin{minted}{ocaml}
type term = Var of string | Abs of string * term | App of term * term
\end{minted}
Using this type, we will model computation as a tree, which can be easily traversed and manipulated with recursive pattern matching functions.

\subsection{Printing}

To pretty-print terms, we will want to use as few parentheses as possible.
An initial design for the term printer \mintinline{ocaml}{string_of_term} always used parentheses around both terms in applications \mintinline{ocaml}{App(s, t)}.
However, this resulted in nested applications producing too many parentheses, which made many complex terms look ugly.
In the latest version, we use a mutually recursive set of functions \mintinline{ocaml}{string_of_term} and \mintinline{ocaml}{string_of_exp} which will only output a pair of parentheses when the term inside is not a simple variable.

\subsection{Free and Bound Variables}

As the definition of free and bound variables is given in terms of mathematical sets, we construct a module set \mintinline{ocaml}{VarSet} of \mintinline{ocaml}{String}s that will allow us to perform these set operations.
Free variables are those that are excluded from the scope of an abstraction, whereas bound variables are those that are included in the scope of an abstraction.
While this isn't immediately useful, we will need to know what the free variables of a given term are when we model substitution.

\subsection{Substitution}

It is interesting to note that abstractions and applications are actually the inverses of each another.
We write the \mintinline{ocaml}{substitute} function based on a few basic rules:
\begin{itemize}
  \item To perform substitution on variables, replace the \enquote{old} term with the \enquote{new} term.
  \item We can perform substitution on applications by \enquote{propagating} the substitution inwards to both subterms.
  \item Substitution on an abstraction is a bit trickier.
    We must be careful to not accidentally bind or free any variables.
    Given a term \mintinline[escapeinside=||]{ocaml}{|$\lambda$| x.s}, we can safely perform substitution if the term being replaced is not a free variable in $s$, or $x$ is not a free variable in the replacing term.
    Otherwise, we have to rename $x$ to a variable that is not free in either $s$ or the replacing term, after which we can proceed as normal.
\end{itemize}
To help us to choose a \enquote{safe} variable, we perform the proper operations and pass the \mintinline{ocaml}{VarSet} of free variables to \mintinline{ocaml}{fresh_var}, which returns a variable that we can rename $x$ to.

\subsection{Conversions}

There are three types of conversions: alpha ($\alpha$), beta ($\beta$), and ($\eta$).
We actually do an implicit alpha conversion in the last case for substitutions, when we rename $x$ before proceeding with the substitution.
The most important of these is the beta conversion, in which we can take an application of the form \mintinline[escapeinside=||]{ocaml}{(|$\lambda$| x.s) t} and substitute $t$ for $x$ in $s$.
Eta conversion is interesting for logical analysis as it can assist us with potentially reducing even further, but isn't as useful as the beta reduction for simulating programs.

\subsection{Normalization of Terms}

It can be proved that if we continuously process the expression by reducing the \enquote{leftmost outermost} term of the form \mintinline[escapeinside=||]{ocaml}{(|$\lambda$| x.s) t}, we will eventually end up with an expression that can no longer be reduced, i.e.\ the expression will be in normal form.
Given a term as input, \mintinline{ocaml}{normalize_step} returns the term that is the result of one step of reduction.
To fully reduce, \mintinline{ocaml}{normalize} runs \mintinline{ocaml}{normalize_step} until the output is the same as the input, with the assumption being that no more reductions are possible after that point.

\section{Executing Programs}

We can now write and reduce arbitrary terms, but that is not very entertaining or useful to the typical end user.
While the Lambda Calculus can model any computation, it does not do so in a manner that is easy to read.

\subsection{Defining Procedures}
To make terms slightly easier to work with, we define a simple \mintinline{ocaml}{procedure} type that allows us to alias terms to human-readable identifiers:
\begin{minted}{ocaml}
type procedure = Def of string * Terms.term | Use of Terms.term | Noop | Eof
\end{minted}
A \mintinline{ocaml}{procedure} corresponds to a single statement in our language.
We can use a \mintinline{ocaml}{Def} to associate an identifier with the term it represents, and a \mintinline{ocaml}{Use} to perform operations (the most computationally useful one being reduction/normalization).
\mintinline{ocaml}{Noop} and \mintinline{ocaml}{Eof} are more for the implementation, as some statements, such as comments, may not be of value to our interpreter.

\subsection{Defining Programs}
Now that the concept of procedures is well-defined, we can combine sequences of them to create larger programs.
We split the definition of a program into two parts: its environment and routines:

\begin{minted}{ocaml}
module EnvMap = Map.Make (struct
  type t = Terms.term
  let compare = compare
end)

type program = { env: string EnvMap.t ; routine: Terms.term list }
\end{minted}
The environment \mintinline{ocaml}{env} will store \mintinline{ocaml}{Def}s and allow us to look up terms by their identifiers.
We use a list of \mintinline{ocaml}{term}s to represent the terms that we actually want to see the normalization of.
In reality, this list could be rather short in comparison to the size of the environment.

\end{document}


\problemname{The Polar Express}

\noindent It's almost December, and the Polar Express is planning for its annual trip towards the North Pole. The Polar Express is a train with $N$ carts numbered $1$ to $N$, and you are the conductor! However, due to an exceptionally heavy snowstorm, your train recently ran into an accident and veered off-track. Now, the ordering of all of the carts have been scrambled! You cannot leave to pick up the children for Christmas unless you can reorganize the train in the original sorted order.\\

At the start of every hour, you can move the first $K$ carts from the front of the train to a holding station, which is a stack. At the end of every hour, the magical elves of the holding station will move the carts from the holding station to the back of the train, in reverse order.\\

For example, suppose the train is currently ordered $1, 2, 5, 4, 3$ from front to back. You can move the first three trains to the holding station. At the end of the hour, your carts will be returned to you, and the train will be ordered as $4, 3, 5, 2, 1$.\\

Your job is to come up with a sequence of instructions to sort the train, such that cart $1$ is at the front and cart $N$ is at the back. However, Christmas is in $1\,000$ hours and you cannot take longer than that to complete your task. It is guaranteed that such a solution will always exist.

\section*{Input}
The first line of input consists of a single integer $N$ ($1 \leq N \leq 200$), specifying the number of carts that the train contains.\\
$N$ lines follow, each of which consists of a single integer, together forming a permutation of the numbers from $1$ to $N$. This permutation specifies the initial order of the train carts.

\section*{Output}
On the first line of output, print a single integer $M$, the number days it will take to sort the train.\\
Print $M$ lines next, the $i$th of which should consist of a single integer denoting the number of carts to move to the holding station on the $i$th hour. Each integer printed must be between $1$ and $N$, inclusive.\\
Your answer will be considered correct if $0 \leq M \leq 1\,000$, and your given sequence correctly sorts the train carts.\\