\chapter{Numerical Solution}
\label{chap:model_soln}
\index{Numerical Solution%
@\emph{Numerical Solution}}%

%\pagestyle{empty}

When solving for the full transition path, we solve the model in two steps.  First, we solve for the steady state prices and allocations.  Next, we iterate backwards solving for prices and allocations along the transition path to the steady state.  Here we define the solution to the model, starting with the steady-state solution.

\section{Solving for stationary steady-state equilibrium}\label{AppSSsolve}

  This section describes the solution method for the stationary steady-state equilibrium described in Definition \ref{DefEquilSS}.  To obtain the steady-state equilibrium, we do the following:

   \begin{enumerate}
    \item Use the techniques in Chapter \ref{AppPopGrowth} to solve for the steady-state population distribution vector $\bm{\bar{\omega}}$ of the exogenous population process.
    \item Choose an initial guess for the stationary steady-state wage rate, $\bar{w}$ and real interest rate, $\bar{r}$.
%      \begin{itemize}
%        \item A good first guess is a large positive number for all the $\bar{n}_{j,s}$ that is slightly less than $\tilde{l}$ and to choose some small positive number for $\bar{b}_{j,s+1}$ that is small enough to be less than the minimum income that an individual might have $\bar{w}e_{j,s}\bar{n}_{j,s}$.
%      \end{itemize}
   \item With $\bar{w}$ and $\bar{r}$, use an unconstrained root finder to solve the $4\times 2 \times M$ equations defining the steady-state version of the firms' problem.  This will yield $\bar{I}_{m,c}$, $\bar{EL}_{m,c}$, $\bar{V}_{m,c}$, $\bar{p}_{m,c}$.
   \item Using $\bar{p}_{m,c}$ and the fixed coefficient matrix $\Pi$, we can determine $\bar{p}_{i}$, the price of consumption goods.
   \item $\bar{p}_{i}$ and maximization of the consumer's subutility function imply $\bar{p}_{s}$, the price of the composite consumption good.
    \item Perform an unconstrained root finder that chooses $\bar{c}_{j,s}$ and $\bar{b}_{j,s+1}$ that solves the $2JS$ stationary steady-state Euler equations.
    \item Make sure none of the implied steady-state consumptions $\bar{c}_{j,s}+\sum_{i=1}^{I}c_{i,s}$ exceeds income.
      \begin{itemize}
        \item If consumption exceeds income, the individual can not afford the minimum required consumption amounts.  We then...
      \end{itemize}
    \item Given consumption demand and the fixed coefficient matrix $\Xi$, find the implied demand for output, $X_{m}$.
    \item Use the consumer's subutility function over corporate and non-corporate goods to determine demand for corporate and non-corporate production goods to get $X_{m,n}$ and $X_{m,nc}$.
    \item Make sure that demand for these production goods matches the supply given firm's decisions in (iii).
    \item Make sure that none of the Euler errors is too large in absolute value for interior stationary steady-state values. A steady-state Euler error is the following, which is supposed to be close to zero for all $j$ and $s$:
      \begin{align}
        \begin{split}
          &\frac{\chi^n_{s}\left(\frac{b}{\tilde{l}}\right)\left(\frac{\bar{n}_{j,s}}{\tilde{l}}\right)^{v-1}\left[1 - \left(\frac{\bar{n}_{j,s}}{\tilde{l}}\right)\right]^{\frac{1-v}{v}}}{(\bar{c}_{j,s})^{-\sigma}\left(\bar{w} e_{j,s} - \frac{\partial\bar{T}_{j,s}}{\partial \bar{n}_{j,s}}\right)} - 1 \\
          &\qquad\qquad\qquad\qquad\qquad\qquad\qquad\forall j\quad\text{and}\quad E+1\leq s\leq E+S
        \end{split} \label{EqSSeulerrLab} \\
        \begin{split}
          &\frac{e^{-g_y\sigma}\left(\rho_s\chi^b \left(\bar{b}_{j,s+1}\right)^{-\sigma} + \beta(1-\rho_s)(\bar{c}_{j,s+1})^{-\sigma}\left[(1 + \bar{r}) - \frac{\partial \bar{T}_{j,s+1}}{\partial \bar{b}_{j,s+1}}\right]\right)}{(\bar{c}_{j,s})^{-\sigma}} - 1 \\
          &\qquad\qquad\qquad\qquad\qquad\qquad\qquad\forall j \quad\text{and}\quad E+1\leq s\leq E+S-1 \\
        \end{split} \label{EqSSeulerrSav} \\
        &\frac{\chi^b e^{-g_y\sigma}(\bar{b}_{j,E+S+1})^{-\sigma}}{\left(\bar{c}_{j,E+S}\right)^{-\sigma}} - 1 \quad\forall j \label{EqSSeulerrBeq}
      \end{align}
  \end{enumerate}

%FIRMS STUFF:
%\section{Solving the model}
%
%
%\subsection{Solving for the steady state}
%
%On the supply side (with one sector), we have to solve for the factor prices, $\bar{i}$ and $bar{w}$ (the price of output $\bar{p}^{C}$ is normalized to one), the shadow price of capital, $\bar{q}^{C}$, and the allocations $\bar{EL}^{C}$, $\bar{K}^{C}$, $\bar{I}^{C}$.  From these all the other variables follow trivially.
%
%Start by solving for the steady-states of Equations \ref{eqn:opt_i} and \ref{eqn:foc_k}.  Equation \ref{eqn:opt_i} becomes:
%
%\begin{equation}
%\label{eqn:opt_i_ss}
%\bar{q}^{C}=\underbrace{1-b^{C}-\bar{\Omega}^{C}\bar{\tau}^{b}(f_{e}-f_{b}b^{C})-f_{d}(1-f_{e})\bar{Z}^{C}}_{\text{function of only parameters}}
%\end{equation}
%
%This yields the solution to $\bar{q}^{C}$.
%
%Next, consider the steady-state of Equation \ref{eqn:foc_k}:
%
%\begin{equation}
%\label{eqn:foc_k_ss}
%\bar{q}^{C}=\frac{1}{1+\bar{\theta}}\left[(1-\delta^{C})\bar{q}^{C} + \frac{\partial \bar{X}^{C}}{\partial \bar{K}^{C}} - \{(1-\bar{\tau}^{b})\bar{\Omega}^{C}\bar{\tau}^{pC} + (1-f_{i}\bar{\tau}^{i})\bar{i}\bar{\Omega}^{C}b^{C} - \delta^{C}(1-b^{C}-\bar{\Omega}^{C}(1-f_{p}\bar{\tau}^{b}b^{C}))\}\right]
%\end{equation}
%
%We can rearrange this and solve for the steady-state marginal product of capital in sector $C$:
%
%\begin{equation}
%\label{eqn:mpk_ss}
%\frac{\partial \bar{X}^{C}}{\partial \bar{K}^{C}} = (\bar{\theta}+\delta^{C})\bar{q}^{C} + (1-\bar{\tau}^{b})\bar{\Omega}^{C}\bar{\tau}^{pC} + (1-f_{i}\bar{\tau}^{i})\bar{i}\bar{\Omega}^{C}b^{C} - \delta^{C}(1-b^{C}-\bar{\Omega}^{C}(1-f_{p}\bar{\tau}^{b}b^{C}))
%\end{equation}
%
%Notice that given Equation \ref{eqn:opt_i_ss}, the RHS to the above equation is function of parameters and the steady state nominal interest rate, $\bar{i}$.  The LHS of the equation is a function of $\bar{K}^{C}$ and $\bar{EL}^{C}$.
%
%I think we can use the following to identify the SS values of the variables of interest:
%\begin{enumerate}
%\item $\bar{i}$ will be determined by the SS of the household's Euler equations (I think this can be done as described in the HH sol'n method)
%\item $\bar{w}$ will be determined by the SS of the household's FOCs for labor supply ((I think this can be done as described in the HH sol'n method)
%\item $\bar{q}^{C}$ is determined by Equation \ref{eqn:opt_i_ss}
%\item $\bar{EL}^{C}$ is determined by the SS version of Equation \ref{eqn:foc_l}, plus $\bar{w}$
%\item $\bar{K}^{C}$ is determined by Equation \ref{eqn:mpk_ss} and $\bar{i}$
%\item $\bar{I}^{C}$ is then solved for using the steady state law of motion for capital $\implies \bar{I}^{C}=\delta^{C}\bar{K}^{C}$
%\end{enumerate}
%
%In solving for $\bar{EL}^{C}$ and $\bar{K}^{C}$, note that we'll have use the MPK and the MPL simultaneously.  Given our production function, we have:
%
%\begin{equation}
%\label{eqn:mpk}
%\frac{\partial X^{C}_{u}}{\partial K^{C}_{u}} = \left[(\gamma_{C})^{1/\epsilon_{C}}(K^{C}_{u})^{(\epsilon_{C}-1)/\epsilon_{C}}+(1-\gamma_{C})^{1/\epsilon_{C}}(EL^{C}_{u})^{(\epsilon_{C}-1)/\epsilon_{C}}\right]^{1/(\epsilon_{C}-1)}(\gamma_{C})^{1/\epsilon_{C}}(K^{C}_{u})^{-1/\epsilon_{C}}
%\end{equation}
%
%and
%
%\begin{equation}
%\label{eqn:mpl}
%\frac{\partial X^{C}_{u}}{\partial EL^{C}_{u}} = \left[(\gamma_{C})^{1/\epsilon_{C}}(K^{C}_{u})^{(\epsilon_{C}-1)/\epsilon_{C}}+(1-\gamma_{C})^{1/\epsilon_{C}}(EL^{C}_{u})^{(\epsilon_{C}-1)/\epsilon_{C}}\right]^{1/(\epsilon_{C}-1)}(1-\gamma_{C})^{1/\epsilon_{C}}(EL^{C}_{u})^{-1/\epsilon_{C}}
%\end{equation}
%
%We know that, at an optimum, the marginal revenue product of labor equals the wage rate, and the marginal revenue product of capital equals a function of the interest rate, marginal $q$, and the model parameters.  Call this function $g(i_{u},q^{C}_{u},q^{C}_{u-1},\Theta)$.  We thus have $p^{C}_{u}\frac{\partial X^{C}_{u}}{\partial EL^{C}_{u}} =w_{u}$ and $p^{C}_{u}\frac{\partial X^{C}_{u}}{\partial K^{C}_{u}}=g(i_{u},q^{C}_{u},q^{C}_{u-1},\Theta)$.  Dividing these two equations, we have:
%
%\begin{equation}
%\label{eqn:cap_lab_ratio}
%\begin{split}
%& \frac{\frac{\partial X^{C}_{u}}{\partial K^{C}_{u}}}{\frac{\partial X^{C}_{u}}{\partial EL^{C}_{u}}} =\frac{(\gamma_{C})^{1/\epsilon_{C}}(K^{C}_{u})^{-1/\epsilon_{C}}}{(1-\gamma_{C})^{1/\epsilon_{C}}(EL^{C}_{u})^{-1/\epsilon_{C}}}-=\frac{g(i_{u},q^{C}_{u},q^{C}_{u-1},\Theta)}{w_{u}} \\
%& \implies \frac{K^{C}_{u}}{EL^{C}_{u}} =\frac{(1-\gamma_{C})}{\gamma_{C}}\left(\frac{w_{u}} {g(i_{u},q^{C}_{u},q^{C}_{u-1},\Theta)}\right)^{\epsilon_{C}} \\
%\end{split}
%\end{equation}
%
%We can use the SS version of Equation \ref{eqn:cap_lab_ratio} to solve for capital as function of labor (and $\bar{q}, \bar{i}, \bar{w}$), and then use that in the SS version of Equation \ref{eqn:mpl} to solve for labor as s function of $\bar{q}, \bar{i}, \bar{w}$.  We then go back to the SS version of Equation \ref{eqn:cap_lab_ratio} to get the SS choice of capital as a function of $\bar{q}, \bar{i}, \bar{w}$.
%
%All of the above will work for each sector in a model with any number of sectors (though care has to be taken to include the prices of output and capital in those other sectors, since only one sector's output can be the numeraire).
%
%\subsection{Solving for the transition path}
%
%I believe we can just use the Euler equations to go backwards in time, from the SS back along the transition path to $t=0$.  Assume period $T$ is the SS,  The solution would look like the following:
%
%\begin{enumerate}
%\item Use Equation \ref{eqn:foc_k} to solve for the for $q^{C}_{T-1}$ since we have the solution to the RHS of the equation after we've solved for the SS.
%\item Use the law of motion for capital to find: $K^{C}_{T-1}=\frac{K^{C}_{T}-I^{C}_{T-1}}{(1-\delta^{C})}=\frac{\bar{K}^{C}-I^{C}_{T-1}}{(1-\delta^{C})}$
%\item Use Equation \ref{eqn:opt_i} and the value of $q^{C}_{T-1}$ to find $I^{C}_{T-1}$ (and $K^{C}_{T-1}$ given the law of motion relationship.
%\item Given $w_{T-1}$ we can use the FOC for labor demand to find $EL^{C}_{T-1}$
%\item Given $i_{T-1}$ we can use Equation \ref{eqn:foc_k} to solve for $q^{C}_{T-2}$
%\item We then repeat the above steps until we work back to $t=0$.
%\end{enumerate}
%
%
%
%To solve for any stationary non-steady-state equilibrium time path of the economy from an arbitrary current state to the steady state, we follow the time path iteration (TPI) method of \citet{AuerbachKotlikoff:1987}. Appendix \ref{AppNonSSsolve} details how to solve for the non-steady-state equilibrium time path using the TPI method. The approach is to choose an arbitrary time path for the stationary aggregate capital stock $\hat{K}_t$, stationary aggregate labor $\hat{L}_t$, and total bequests received $\hat{BQ}_{j,t}$ for each type $j$. This initial guess of a path implies arbitrary beliefs that violate the rational expectations requirement. We then solve for households' optimal decisions given the time paths of those variables, which decisions imply new time paths of those variables. We then update the time path as a convex combination of the initial guess and the new implied path. Figure \ref{FigKpathTPI} shows the equilibrium time path of the aggregate capital stock for the calibration described in Table \ref{TabExogVars} for $T=160$ periods starting from an initial distribution of savings in which $b_{j,s,1}=\bm{\bar{\Gamma}}$ for all $j$ and $s$ in the case that no policy experiment takes place. The initial capital stock $\hat{K}_1$ is not at the steady state $\bar{K}$ because the initial population distribution is not at the steady-state.
%
%
%
%

\section{Solving for stationary non-steady-state equilibrium by time path iteration}\label{AppNonSSsolve}

  \setcounter{equation}{0}

  This section outlines the benchmark time path iteration (TPI) method of \citet{AuerbachKotlikoff:1987} for solving the stationary non-steady-state equilibrium transition path of the distribution of savings. TPI finds a fixed point for the transition path of the distribution of capital for a given initial state of the distribution of capital. The idea is that the economy is infinitely lived, even though the agents that make up the economy are not. Rather than recursively solving for equilibrium policy functions by iterating on individual value functions, one must recursively solve for the policy functions by iterating on the entire transition path of the endogenous objects in the economy (see \citet[ch. 17]{StokeyLucas:1989}).

  The key assumption is that the economy will reach the steady-state equilibrium described in Definition \ref{DefEquilSS} in a finite number of periods $T<\infty$ regardless of the initial state. Let $\bm{\hat{\Gamma}}_t$ represent the distribution of stationary savings at time $t$.
  \begin{equation}\tag{\ref{EqSavDist}}
    \bm{\hat{\Gamma}}_t \equiv \Bigl\{\bigl\{\hat{b}_{j,s,t}\bigr\}_{j=1}^J\Bigr\}_{s=E+2}^{E+S+1}, \quad\forall t
  \end{equation}
  In Section \ref{SecMCEqlbm}, we describe how the stationary non-steady-state equilibrium time path of allocations and price is described by functions of the state $\bm{\hat{\Gamma}}_t$ and its law of motion. TPI starts the economy at any initial distribution of savings $\bm{\hat{\Gamma}}_1$ and solves for its equilibrium time path over $T$ periods to the steady-state distribution $\bm{\bar{\Gamma}}_T$.

  The first step is to assume an initial transition path for aggregate stationary capital $\bm{\hat{K}}^i = \left\{\hat{K}_1^i,\hat{K}_2^i,...\hat{K}_T^i\right\}$, aggregate stationary labor $\bm{\hat{L}}^i = \left\{\hat{L}_1^i,\hat{L}_2^i,...\hat{L}_T^i\right\}$, and total bequests received $\bm{\hat{BQ}}_j^i=\{\hat{BQ}_{j,1}^i,\hat{BQ}_{j,2}^i,...\hat{BQ}_{j,T}^i\}$ for each ability type $j$ such that $T$ is sufficiently large to ensure that $\bm{\hat{\Gamma}}_T = \bar{\bm{\Gamma}}$, $\hat{K}_T^i\left(\bm{\Gamma}_T\right)$, $\hat{L}_T^i\left(\bm{\Gamma}_T\right) = \bar{L}\left(\bar{\bm{\Gamma}}\right)$, and $\hat{BQ}_{j,T}^i\left(\bm{\Gamma}_T\right) = \bar{BQ}_j\left(\bar{\bm{\Gamma}}\right)$ for all $t\geq T$. The superscript $i$ is an index for the iteration number. The transition paths for aggregate capital and aggregate labor determine the transition paths for both the real wage $\bm{\hat{w}}^i = \left\{\hat{w}_1^i,\hat{w}_2^i,...\hat{w}_T^i\right\}$ and the real return on investment $\bm{r}^i = \left\{r_1^i,r_2^i,...r_T^i\right\}$. The time paths for the total bequests received also figure in each period's budget constraint and are determined by the distribution of savings and intended bequests.

  The exact initial distribution of capital in the first period $\bm{\hat{\Gamma}}_1$ can be arbitrarily chosen as long as it satisfies the stationary capital market clearing condition \eqref{EqMktClrCapStat}.
  \begin{equation}\label{EqMktClrCapStat1}
    \hat{K}_1 = \frac{1}{1 + \tilde{g}_{n,1}}\sum_{s=E+2}^{E+S+1}\sum_{j=1}^{J}\hat{\omega}_{s-1,0}\lambda_j \hat{b}_{j,s,1}
  \end{equation}
  Simiilarly, each initial value of total bequests received $\hat{BQ}_{j,1}^i$ must be consistent with the initial distribution of capital through the stationary version of \eqref{EqTotBeq}.
  \begin{equation}\label{EqTotBeqStat1}
    \hat{BQ}_{j,1} = \frac{(1+r_1)\lambda_j}{1+\tilde{g}_{n,1}}\sum_{s=E+1}^{E+S}\rho_s\hat{\omega}_{s,0}\hat{b}_{j,s+1,1} \quad\forall j
  \end{equation}
  However, this is not the case with $\hat{L}_1^i$. Its value will be endogenously determined in the same way the $K_2^i$ is. For this reason, a logical initial guess for the time path of aggregate labor is the steady state in every period $L_t^1 = \bar{L}$ for all $1\leq t\leq T$.

  It is easiest to first choose the initial distribution of savings $\bm{\hat{\Gamma}}_1$ and then choose an initial aggregate capital stock $\hat{K}_1^i$ and initial total bequests received $\hat{BQ}_{j,1}^i$ that correspond to that distribution. As mentioned earlier, the only other restrictions on the initial transition paths for aggregate capital, aggregate labor, and total bequests received is that they equal their steady-state levels $\hat{K}_T^i = \bar{K}\left(\bm{\bar{\Gamma}}\right)$, $\hat{L}_T^i = \bar{L}\left(\bm{\bar{\Gamma}}\right)$, and $\hat{BQ}_{j,T}^i = \bar{BQ}_j\left(\bm{\bar{\Gamma}}\right)$ by period $T$. \citet{EvansPhillips:2014} have shown that the initial guess for the aggregate capital stocks $\hat{K}_t^i$ for periods $1<t<T$ can take on almost any positive values satisfying the constraints above and still have the time path iteration converge.

  Given the initial savings distribution $\bm{\hat{\Gamma}}_1$ and the transition paths of aggregate capital $\bm{\hat{K}}^i = \left\{\hat{K}_1^i,\hat{K}_2^i,...\hat{K}_T^i\right\}$, aggregate labor $\bm{\hat{L}}^i = \left\{\hat{L}_1^i,\hat{L}_2^i,...\hat{L}_T^i\right\}$, and total bequests received $\bm{\hat{BQ}}_j^i = \left\{\hat{BQ}_{j,1}^i,\hat{BQ}_{j,2}^i,...\hat{BQ}_{j,T}^i\right\}$, as well as the resulting real wage $\bm{\hat{w}}^i = \left\{\hat{w}_1^i,\hat{w}_2^i,...\hat{w}_T^i\right\}$, and real return to savings $\bm{r}^i = \left\{r_1^i,r_2^i,...r_T^i\right\}$, one can solve for the period-1 optimal labor supply and intended bequests for each type $j$ of $s=E+S$-aged agents in the last period of their lives $n_{j,E+S,1}=\phi_{j,E+S}(\hat{b}_{j,E+S,1},\hat{BQ}_{j,E+S,1},\hat{w}_1,r_1)$ and $\hat{b}_{j,E+S+1,2}=\psi_{j,E+S}(\hat{b}_{j,E+S,1},\hat{BQ}_{j,E+S,1},\hat{w}_1,r_1)$ using his two $s=E+S$ static Euler equations \eqref{EqEulerLabStat} and \eqref{EqEulerSavEpSstat}.
  \begin{equation}\label{EqEulerSlabt1}
    \begin{split}
      &(\hat{c}_{j,E+S,1})^{-\sigma}\Biggl(\hat{w}_1^i e_{j,E+S} - \frac{\partial\hat{T}_{j,E+S,1}}{\partial n_{j,E+S,1}}\Biggr) = ... \\
      &\qquad\qquad\qquad\qquad \chi^n_{E+S}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,E+S,1}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,E+S,1}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} \quad\forall j \\
      &\quad\text{where}\quad \hat{c}_{j,E+S,1} = ... \\
      &\qquad\qquad\qquad \left(1 + r_1^i\right)\hat{b}_{j,E+S,1} + \hat{w}_1^i e_{j,E+S}n_{j,E+S,1} + \frac{\hat{BQ}_{j,1}}{\lambda_j} - e^{g_y}\hat{b}_{j,E+S+1,2} - \hat{T}_{j,E+S,1} \\
      &\quad\text{and}\quad \frac{\partial \hat{T}_{j,E+S,1}}{\partial n_{j,E+S,1}} = ... \\
      &\qquad\qquad\qquad \hat{w}_1^i e_{j,E+S}\biggl[\tau^I\bigl(F\hat{a}_{j,E+S,1}\bigr) + \frac{\hat{a}_{j,E+S,1}CDF\bigl[2A(F\hat{a}_{j,E+S,1})+B\bigr]}{\bigl[A(F\hat{a}_{j,E+S,1})^2+B(F \hat{a}_{j,E+S,1})+C\bigr]^2} + \tau^P\Biggr]
    \end{split}
  \end{equation}
  \begin{equation}\label{EqEulerSbeqt1}
    (\hat{c}_{j,E+S,1})^{-\sigma} = \chi^b e^{-g_y\sigma}(\hat{b}_{j,E+S+1,2})^{-\sigma} \quad\forall j
  \end{equation}
  Note that this is simply two equations \eqref{EqEulerSlabt1} and \eqref{EqEulerSbeqt1} and two unknowns $n_{j,E+S,1}$ and $\hat{b}_{j,E+S+1,2}$.

  We then solve the problem for all $j$ types of $E+S-1$-aged individuals in period $t=1$, each of which entails labor supply decisions in the current period $n_{j,E+S-1,1}$ and in the next period $n_{j,E+S,2}$, a savings decision in the current period for the next period $\hat{b}_{j,E+S,2}$ and an intended bequest decision in the last period $\hat{b}_{j,E+S+1,3}$. The labor supply decision in the initial period and the savings period in the initial period for the next period for each type $j$ of $E+S-1$-aged individuals are policy functions of the current savings and the total bequests received and prices in this period and the next $\hat{b}_{j,E+S,2} = \psi_{j,E+S-1}(\hat{b}_{j,E+S-1,1},\{\hat{BQ}_{j,t},\hat{w}_t,r_t\}_{t=1}^2)$ and $\hat{n}_{j,E+S-1,1} = \phi_{j,E+S-1}(\hat{b}_{j,E+S-1,1},\{\hat{BQ}_{j,t},\hat{w}_t,r_t\}_{t=1}^2)$. The labor supply and intended bequests decisions in the next period are simply functions of the savings, total bequests received, and prices in that period $\hat{n}_{j,E+S,2} = \phi_{j,E+S}(\hat{b}_{j,E+S,2},\hat{BQ}_{j,2},\hat{w}_2,r_2)$ and $\hat{b}_{j,E+S+1,3} = \psi_{j,E+S}(\hat{b}_{j,E+S,2},\hat{BQ}_{j,2},\hat{w}_2,r_2)$. These four functions are characterized by the following versions of equations \eqref{EqEulerLabStat}, \eqref{EqEulerSavStat}, and \eqref{EqEulerSavEpSstat}.
  \begin{equation}\label{EqEulerSm1labt1}
    \begin{split}
      &(\hat{c}_{j,E+S-1,1})^{-\sigma}\Biggl(\hat{w}_1^i e_{j,E+S-1} - \frac{\partial\hat{T}_{j,E+S-1,1}}{\partial n_{j,E+S-1,1}}\Biggr) = ... \\
      &\qquad\qquad\qquad \chi^n_{E+S-1}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,E+S-1,1}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,E+S-1,1}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} \quad\forall j
    \end{split}
  \end{equation}
  \begin{equation}\label{EqEulerSm1savt1}
    \begin{split}
      &(\hat{c}_{j,E+S-1,1})^{-\sigma} = ... \\
      &e^{-g_y\sigma}\Biggl(\rho_{E+S-1}\chi^b \bigl(\hat{b}_{j,E+S,2}\bigr)^{-\sigma} + \beta(1-\rho_{E+S-1})(\hat{c}_{j,E+S,2})^{-\sigma}\Biggl[(1 + r_2^i) - \frac{\partial T_{j,E+S,2}}{\partial b_{j,E+S,2}}\Biggr]\Biggr) \\
      &\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\forall j \\
      &\qquad\text{where}\quad \frac{\partial T_{j,E+S,2}}{\partial b_{j,E+S,2}} = ...\\
      &\qquad\qquad r_2^i\Biggl(\tau^I(F\hat{a}_{j,E+S,2}) + \frac{F\hat{a}_{j,E+S,2}CD\left[2A(F\hat{a}_{j,E+S,2}) + B\right]}{\left[A(F\hat{a}_{j,E+S,2})^2 + B(F\hat{a}_{j,E+S,2}) + C\right]^2}\Biggr) ... \\
      &\qquad\qquad \tau^W(\hat{b}_{j,E+S,2}) + \frac{\hat{b}_{j,E+S,2}PHM}{\left(H\hat{b}_{j,E+S,2} + M\right)^2}
    \end{split}
  \end{equation}
  \begin{equation}\label{EqEulerSlabt2}
    \begin{split}
      &(\hat{c}_{j,E+S,2})^{-\sigma}\Biggl(\hat{w}_2^i e_{j,E+S} - \frac{\partial\hat{T}_{j,E+S,2}}{\partial n_{j,E+S,2}}\Biggr) = ... \\
      &\qquad\qquad\qquad \chi^n_{E+S}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,E+S,2}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,E+S,2}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} \quad\forall j
    \end{split}
  \end{equation}
  \begin{equation}\label{EqEulerSsavt2}
    (\hat{c}_{j,E+S,2})^{-\sigma} = \chi^b e^{-g_y\sigma}(\hat{b}_{j,E+S+1,3})^{-\sigma} \quad\forall j
  \end{equation}
  Note that this is four equations \eqref{EqEulerSm1labt1}, \eqref{EqEulerSm1savt1}, \eqref{EqEulerSlabt2}, and \eqref{EqEulerSsavt2} and four unknowns $n_{j,E+S-1,1}$, $\hat{b}_{j,E+S,2}$, $n_{j,E+S,2}$, and $\hat{b}_{j,E+S+1,3}$.

  This process is repeated for every age of household alive in $t=1$ down to the age $s=E+1$ household at time $t=1$. Each of these households $j$ solves the full set of remaining $S-s+1$ labor supply decisions, $S-s$ savings decisions, and one intended bequest decision at the end of life. After the full set of lifetime decisions has been solved for all the households alive at time $t=1$, each ability $j$ household born in period $t\geq 2$ can be solved for, the solution to which is characterized by the following full set of Euler equations analogous to \eqref{EqEulerLabStat}, \eqref{EqEulerSavStat}, and \eqref{EqEulerSavEpSstat}.
  \begin{equation}\label{EqEulerslabt}
    \begin{split}
      &(\hat{c}_{j,s,t})^{-\sigma}\Biggl(\hat{w}_t^i e_{j,s} - \frac{\partial\hat{T}_{j,s,t}}{\partial n_{j,s,t}}\Biggr) =  \chi^n_{s}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,s,t}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,s,t}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} \\
      &\qquad\qquad\qquad\qquad\qquad\forall j \quad\text{and}\quad E+1\leq s\leq E+S\quad\text{and}\quad t\geq 2
    \end{split}
  \end{equation}

  \begin{equation}\label{EqEulersSavt}
    \begin{split}
      &(\hat{c}_{j,s,t})^{-\sigma} = ... \\
      &e^{-g_y\sigma}\Biggl(\rho_{s}\chi^b \bigl(\hat{b}_{j,s+1,t+1}\bigr)^{-\sigma} + \beta(1-\rho_{s})(\hat{c}_{j,s+1,t+1})^{-\sigma}\Biggl[(1 + r_{t+1}^i) - \frac{\partial T_{j,s+1,t+1}}{\partial b_{j,s+1,t+1}}\Biggr]\Biggr) \\
      &\qquad\qquad\qquad\forall j \quad\text{and}\quad E+1\leq s\leq E+S-1 \quad\text{and}\quad t\geq 2
    \end{split}
  \end{equation}

  \begin{equation}\label{EqEulerSsavt}
    (\hat{c}_{j,E+S,t})^{-\sigma} = \chi^b e^{-g_y\sigma}(\hat{b}_{j,E+S+1,t+1})^{-\sigma} \quad\forall j \quad\text{and}\quad t\geq 2
  \end{equation}
  For each household of ability type $j$ entering the economy in period $t\geq 1$, the entire set of $2S$ lifetime decisions is characterized by the $2S$ equations represented in \eqref{EqEulerslabt}, \eqref{EqEulersSavt}, and \eqref{EqEulerSsavt}.

  We can then solve for the entire lifetime of savings and labor supply decisions for each age $s=1$ individual in periods $t=2,3,...T$. The central part of the schematic diagram in Figure \ref{FigTPIdiag} shows how this process is done in order to solve for the equilibrium time path of the economy from period $t=1$ to $T$. Note that for each full lifetime savings and labor supply path solved for an individual born in period $t\geq 2$, we can solve for the aggregate capital stock and total bequests received implied by those savings decisions $\bm{\hat{K}}^{i'}$ and $\bm{\hat{BQ}}_{j}^{i'}$ and aggregate labor implied by those labor supply decisions $\bm{\hat{L}}^{i'}$.

  \begin{figure}[p]\centering \captionsetup{width=4.0in}
    \caption{\label{FigTPIdiag}\textbf{Diagram of TPI solution method within each iteration for $S=4$ and $J=1$}}
    \fbox{\resizebox{4.2in}{6.0in}{\includegraphics{images/TPIdiag.pdf}}}
  \end{figure}

  % THIS IS THE CODE FOR THE TABLE UNDERLYING THE FIGURE ABOVE
  % \begin{tabular}{>{\footnotesize}l| >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c >{\footnotesize}c}
  %          & Initial & & & & & & & & Implied \\
  %          & Aggr. & & \multicolumn{5}{c}{Implied distribution of savings $\bm{\hat{\Gamma}}_t$} & & Aggr. \\
  %   Period & Paths    & & \multicolumn{5}{c}{and labor supply}   & & Paths    \\
  %   \hline
  %   & & & & & & & & & \\
  %   $t=1$ & $\begin{matrix}\hat{K}_1^i, \: \hat{BQ}_{j,1}^i \\ \hat{L}_1^i\end{matrix}$ & $\begin{matrix}= \\ \rightarrow\end{matrix}$ & $\begin{matrix}\: \\ n_{1,1}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,1} \\ n_{2,1}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,1} \\ n_{3,1}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,1} \\ n_{4,1}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,1} \\ \,\end{matrix}$ & $\begin{matrix}= \\ \rightarrow\end{matrix}$ & $\begin{matrix}\hat{K}_1^i, \: \hat{BQ}_{j,1}^{i'} \\ \hat{L}^{i'}_1\end{matrix}$ \\[10mm]
  %   $t=2$ & $\begin{matrix}\hat{K}_2^i, \: \hat{BQ}_{j,2}^i \\ \hat{L}^i_2\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\: \\ n_{1,2}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,2} \\ n_{2,2}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,2} \\ n_{3,2}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,2} \\ n_{4,2}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,2} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_2^{i'}, \: \hat{BQ}_{j,2}^{i'} \\ \hat{L}^{i'}_2\end{matrix}$ \\[10mm]
  %   $t=3$ & $\begin{matrix}\hat{K}_3^i, \: \hat{BQ}_{j,3}^i \\ \hat{L}^i_3\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\: \\ n_{1,3}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,3} \\ n_{2,3}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,3} \\ n_{3,3}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,3} \\ n_{4,3}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,3} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_3^{i'}, \: \hat{BQ}_{j,3}^{i'} \\ \hat{L}^{i'}_3\end{matrix}$ \\[10mm]
  %   $t=4$ & $\begin{matrix}\hat{K}_4^i, \: \hat{BQ}_{j,4}^i \\ \hat{L}^i_4\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\: \\ n_{1,4}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,4} \\ n_{2,4}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,4} \\ n_{3,4}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,4} \\ n_{4,4}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,4} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_4^{i'}, \: \hat{BQ}_{j,4}^{i'} \\ \hat{L}^{i'}_4\end{matrix}$ \\[10mm]
  %   $\quad\vdots$ & $\vdots$ & & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & & $\vdots$ \\[10mm]
  %   $t=T-2$ & $\begin{matrix}\hat{K}_{T-2}^i, \: \hat{BQ}_{j,T-2}^i \\ \hat{L}^i_{T-2}\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\: \\ n_{1,T-2}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,T-2} \\ n_{2,T-2}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,T-2} \\ n_{3,T-2}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,T-2} \\ n_{4,T-2}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,T-2} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_{T-2}^{i'}, \: \hat{BQ}_{j,T-2}^{i'} \\ \hat{L}^{i'}_{T-2}\end{matrix}$ \\[10mm]
  %   $t=T-1$ & $\begin{matrix}\hat{K}_{T-1}^i, \: \hat{BQ}_{j,T-1}^i \\ \hat{L}^i_{T-1}\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\: \\ n_{1,T-1}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,T-1} \\ n_{2,T-1}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,T-1} \\ n_{3,T-1}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,T-1} \\ n_{4,T-1}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,T-1} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_{T-1}^{i'}, \: \hat{BQ}_{j,T-1}^{i'} \\ \hat{L}^{i'}_{T-1}\end{matrix}$ \\[10mm]
  %   $t=T$ & $\begin{matrix}\hat{K}_{T}^i, \: \hat{BQ}_{j,T}^i \\ \hat{L}^i_{T}\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\: \\ n_{1,T}\end{matrix}$ & $\begin{matrix}\hat{b}_{2,T} \\ n_{2,T}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,T} \\ n_{3,T}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,T} \\ n_{4,T}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,T} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_{T}^{i'}, \: \hat{BQ}_{j,T}^{i'} \\ \hat{L}^{i'}_{T}\end{matrix}$ \\[10mm]
  %   $t=T+1$ & $\begin{matrix}\hat{K}_{T+1}^i, \: \hat{BQ}_{j,T+1}^i \\ \hat{L}^i_{T+1}\end{matrix}$ & $\rightarrow$ & $\bullet$ & $\begin{matrix}\hat{b}_{2,T+1} \\ n_{2,T+1}\end{matrix}$ & $\begin{matrix}\hat{b}_{3,T+1} \\ n_{3,T+1}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,T+1} \\ n_{4,T+1}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,T+1} \\ \,\end{matrix}$ & $\rightarrow$ & $\begin{matrix}\hat{K}_{T+1}^{i'}, \: \hat{BQ}_{j,T+1}^{i'} \\ \:\end{matrix}$ \\[10mm]
  %   $t=T+2$ & $\begin{matrix}\hat{K}_{T+2}^i, \: \hat{BQ}_{j,T+2}^i \\ \hat{L}^i_{T+2}\end{matrix}$ & $\rightarrow$ & $\bullet$ & $\bullet$ & $\begin{matrix}\hat{b}_{3,T+2} \\ n_{3,T+2}\end{matrix}$ & $\begin{matrix}\hat{b}_{4,T+2} \\ n_{4,T+2}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,T+2} \\ \,\end{matrix}$ & & \\[10mm]
  %   $t=T+3$ & $\begin{matrix}\hat{K}_{T+3}^i, \: \hat{BQ}_{j,T+3}^i \\ \hat{L}^i_{T+3}\end{matrix}$ & $\rightarrow$ & $\bullet$ & $\bullet$ & $\bullet$ & $\begin{matrix}\hat{b}_{4,T+3} \\ n_{4,T+3}\end{matrix}$ & $\begin{matrix}\hat{bq}_{5,T+3} \\ \,\end{matrix}$ & & \\[10mm]
  %   $t=T+4$ & $\begin{matrix}\hat{K}_{T+4}^i, \: \hat{BQ}_{j,T+4}^i \\ \hat{L}^i_{T+4}\end{matrix}$ & $\rightarrow$ & $\bullet$ & $\bullet$ & $\bullet$ & $\bullet$ & $\begin{matrix}\hat{bq}_{5,T+4} \\ \,\end{matrix}$ & & \\
  % \end{tabular}
  % \clearpage

  Once the set of lifetime saving and labor supply decisions has been computed for all individuals alive in $1\leq t\leq T$, we use the household decisions to compute a new implied time path of the aggregate capital stock and aggregate labor. The implied paths of the aggregate capital stock $\bm{\hat{K}}^{i'}=\{\hat{K}_1^i,\hat{K}_2^{i'},...\hat{K}_T^{i'}\}$, aggregate labor $\bm{\hat{L}}^{i'}=\{\hat{L}_1^i,\hat{L}_2^{i'},...\hat{L}_T^{i'}\}$, and total bequests received $\bm{\hat{BQ}}_j^{i'}=\{\hat{BQ}_{j,1}^i,\hat{BQ}_{j,2}^{i'},...\hat{BQ}_{j,T}^{i'}\}$ in general do not equal the initial guessed paths $\bm{\hat{K}}^{i}=\{\hat{K}_1^i,\hat{K}_2^{i},...\hat{K}_T^{i}\}$, $\bm{\hat{L}}^{i}=\{\hat{L}_1^i,\hat{L}_2^{i},...\hat{L}_T^{i}\}$, and $\bm{\hat{BQ}}_j^{i}=\{\hat{BQ}_{j,1}^i,\hat{BQ}_{j,2}^{i},...\hat{BQ}_{j,T}^{i}\}$ used to compute the household savings and labor supply decisions $\bm{\hat{K}}^{i'}\neq\bm{\hat{K}}^i$, $\bm{\hat{L}}^{i'}\neq\bm{\hat{L}}^i$, and $\bm{\hat{BQ}}_j^{i'}\neq\bm{\hat{BQ}}_j^i$.

  Let $\norm{\:\cdot\:}$ be a norm on the space of time paths of the aggregate capital stock $\bm{\hat{K}}\in\mathcal{K}\subset\mathbb{R}_{++}^T$, aggregate labor supply $\bm{\hat{L}}\in\mathcal{L}\subset\mathbb{R}_{++}^T$, and $J$ paths of total bequests received $\bm{\hat{BQ}}_j\in\mathcal{B}\subset\mathbb{R}_{++}^T$. Then the fixed point necessary for the equilibrium transition path from Definition \ref{DefEquilNonSS} has been found when the distance between these $J+2$ paths is arbitrarily close to zero.
  \begin{equation}\label{EqTPIconverge}
    \norm{\Bigl[\bm{\hat{K}}^{i'}, \bm{\hat{L}}^{i'},\bigl\{\bm{\hat{BQ}}_j^{i'}\bigr\}_{j=1}^J\Bigr] - \Bigl[\bm{\hat{K}}^{i},\bm{\hat{L}}^{i},\bigl\{\bm{\hat{BQ}}_j^{i}\bigr\}_{j=1}^J\Bigr]} \leq \ve \quad\text{for}\quad \ve>0
  \end{equation}
  If the fixed point has not been found $\norm{\Bigl[\bm{\hat{K}}^{i'}, \bm{\hat{L}}^{i'},\bigl\{\bm{\hat{BQ}}_j^{i'}\bigr\}_{j=1}^J\Bigr] - \Bigl[\bm{\hat{K}}^{i},\bm{\hat{L}}^{i},\bigl\{\bm{\hat{BQ}}_j^{i}\bigr\}_{j=1}^J\Bigr]} > \ve$, then new transition paths for the aggregate capital stock and aggregate labor are generated as a convex combination of $\Bigl[\bm{\hat{K}}^{i'},\bm{\hat{L}}^{i'},\bigl\{\bm{\hat{BQ}}_j^{i'}\bigr\}_{j=1}^J\Bigr]$ and $\Bigl[\bm{\hat{K}}^{i},\bm{\hat{L}}^{i},\bigl\{\bm{\hat{BQ}}_j^{i}\bigr\}_{j=1}^J\Bigr]$.
  \begin{equation}\label{EqTPInewpath}
    \begin{split}
      \bm{\hat{K}}^{i+1} &= \nu\bm{\hat{K}}^{i'} + (1-\nu)\bm{\hat{K}}^{i} \\
      \bm{\hat{L}}^{i+1} &= \nu\bm{\hat{L}}^{i'} + (1-\nu)\bm{\hat{L}}^{i} \\
      \bm{\hat{BQ}}_1^{i+1} &= \nu\bm{\hat{BQ}}_1^{i'} + (1-\nu)\bm{\hat{BQ}}_1^{i} \\
      &\vdots \\
      \bm{\hat{BQ}}_J^{i+1} &= \nu\bm{\hat{BQ}}_J^{i'} + (1-\nu)\bm{\hat{BQ}}_J^{i}
    \end{split} \quad\quad\text{for}\quad \nu\in(0,1]
  \end{equation}
  This process is repeated until the initial transition paths for the aggregate capital stock, aggregate labor, and total bequests received are consistent with the transition paths implied by those beliefs and household and firm optimization.

  In essence, the TPI method iterates on individual beliefs about the time path of prices represented by a time paths for the aggregate capital stock $\bm{\hat{K}}^i$, aggregate labor $\bm{\hat{L}}^i$, and total bequests received $\bm{\hat{BQ}}_j^i$ until a fixed point in beliefs is found that are consistent with the transition paths implied by optimization based on those beliefs.

  The following are the steps for computing a stationary non-steady-state equilibrium time path for the economy.
  \begin{enumerate}
    \item Input all initial parameters. See Table \ref{TabExogVars}.
      \begin{enumerate}
        \item The value for $T$ at which the non-steady-state transition path should have converged to the steady state should be at least as large as the number of periods it takes the population to reach its steady state $\bm{\bar{\omega}}$ as described in Appendix \ref{AppPopGrowth}.
      \end{enumerate}

    \item Choose an initial distribution of savings and intended bequests $\bm{\hat{\Gamma}}_1$ and then calculat the initial state of the stationarized aggregate capital stock $\hat{K}_1$ and total bequests received $\hat{BQ}_{j,1}$ consistent with $\bm{\hat{\Gamma}}_1$ according to \eqref{EqMktClrCapStat} and \eqref{EqTotBeqStat1}.
      \begin{enumerate}
        \item Note that you must have the population weights from the previous period $\hat{\omega}_{s,0}$ and the growth rate between period 0 and period 1 $\tilde{g}_{n,1}$to calculate $\hat{BQ}_{j,1}$.
      \end{enumerate}
    \item Conjecture transition paths for the stationarized aggregate capital stock $\bm{\hat{K}}^1=\{\hat{K}^1_t\}_{t=1}^\infty$, stationarized aggregate labor $\bm{\hat{L}}^1=\{\hat{L}^1_t\}_{t=1}^\infty$, and total bequests received $\bm{\hat{BQ}}_j^1=\{\hat{BQ}^{1}_{j,t}\}_{t=1}^\infty$ where the only requirements are that $\hat{K}^i_1$ and $\hat{BQ}^i_{j,1}$ are functions of the initial distribution of savings $\bm{\hat{\Gamma}}_1$ for all $i$ is your initial state and that $\hat{K}^i_t=\bar{K}$, $\hat{L}^i_t=\bar{L}$, and $\hat{BQ}^i_{j,t}= \bar{BQ}_j$ for all $t\geq T$. The conjectured transition paths of the aggregate capital stock $\bm{\hat{K}}^i$ and aggregate labor $\bm{\hat{L}}^i$ imply specific transition paths for the real wage $\bm{\hat{w}}^i=\{\hat{w}^i_t\}_{t=1}^\infty$ and the real interest rate $\bm{r}^i=\{r^i_t\}_{t=1}^\infty$ through expressions \eqref{EqFOCwageStat} and \eqref{EqFOCrate}.
      \begin{enumerate}
        \item An intuitive choice for the time path of aggregate labor is the steady-state in every period $\hat{L}^1_t = \bar{L}$ for all $t$.
      \end{enumerate}
    \item With the conjectured transition paths $\bm{\hat{w}}^i$, $\bm{r}^i$, and $\bm{\hat{BQ}}_j^i$ one can solve for the lifetime policy functions of each household alive at time $1\leq t\leq T$ using the systems of Euler equations of the form \eqref{EqEulerLabStat}, \eqref{EqEulerSavStat}, and \eqref{EqEulerSavEpSstat} and following the diagram in Figure \ref{FigTPIdiag}.
    \item Use the implied distribution of savings and labor supply in each period (each row of $\hat{b}_{j,s,t}$ and $n_{j,s,t}$ in Figure \ref{FigTPIdiag}) to compute the new implied time paths for the aggregate capital stock $\bm{\hat{K}}^{i'} = \{\hat{K}_1^i,\hat{K}_2^{i'},...\hat{K}_T^{i'}\}$, aggregate labor supply $\bm{\hat{L}}^{i'} = \{\hat{L}_1^i,\hat{L}_2^{i'},...\hat{L}_T^{i'}\}$, and total bequests received $\bm{\hat{BQ}}_j^{i'} = \{\hat{BQ}_{j,1}^i,\hat{BQ}_{j,2}^{i'},...\hat{BQ}_{j,T}^{i'}\}$.
    \item Check the distance between the two sets time paths.
      \begin{equation*}
        \norm{\Bigl[\bm{\hat{K}}^{i'}, \bm{\hat{L}}^{i'},\bigl\{\bm{\hat{BQ}}_j^{i'}\bigr\}_{j=1}^J\Bigr] - \Bigl[\bm{\hat{K}}^{i},\bm{\hat{L}}^{i},\bigl\{\bm{\hat{BQ}}_j^{i}\bigr\}_{j=1}^J\Bigr]}
      \end{equation*}
      \begin{enumerate}
        \item If the distance between the initial time paths and the implied time paths is less-than-or-equal-to some convergence criterion $\ve>0$, then the fixed point has been achieved and the equilibrium time path has been found \eqref{EqTPIconverge}.
        \item If the distance between the initial time paths and the implied time paths is greater than some convergence criterion $\norm{\cdot}>\ve$, then update the guess for the time paths according to \eqref{EqTPInewpath} and repeat steps (4) through (6) until a fixed point is reached.
      \end{enumerate}
  \end{enumerate}


\subsubsection{Scalable Nonparametric Directed Graphical Model Inference
and Learning}

Author: Kai Londenberg (Kai.Londenberg@gmail.com), June 2014

\paragraph{Abstract}

This short article tries to give an overview over complementary
techniques over MCMC for general inference in arbitrary directed
probabilistic graphical models. The focus lies on techniques and
algorithms for creating hybrid models which can be scaled to high

dimensional problems, problems with huge data sets and distributed among
multiple machines.

\paragraph{Motivation}

Two papers got me thinking:
\href{http://www.dauwels.com/Papers/Particle.pdf}{Dauwels et al:
Particle Methods as Message Passing}, which gives a nice overview of how
to generalize Message Passing methods, by mixing sampling based methods
freely with exact or fast approximate inference algorithms.

The second is \href{http://arxiv.org/pdf/1311.4780v1.pdf}{Neiswanger et
al: Embarassingly Parallel MCMC}, where an algorithm is described which
can be used to scale MCMC to problems with huge data sets.

Both algorithms actually suffer from the same problem, namely the
\textbf{Message Fusion} problem (described way down). A problem which
has luckily been successfully solved before. I add to that list by
proposing a new approach to efficient density estimation from MCMC
models, called \textbf{Density Mapping}

What I hope is, that these ideas can lead to a practical implementation
of a general, flexible inference system with semantics similar to those
found in common MCMC packages for Bayesian Inference, but with the
capability to outperform these for problems with huge data sets or a
large number of dimensions if the distribution can be factorized into
smaller problems somehow.

I hope to be able to extend the existing Bayesian Modeling Toolkit for
Python \href{https://github.com/pymc-devs/pymc}{PyMC3} via my
side-project \href{https://github.com/kadeng/pypgmc}{PyPGMc} to support
the algorithms mentioned in this article.

So this article is both an overview, and sort of a collection of ideas
and roadmap items.

\paragraph{Introduction to Directed Graphical Models}

Directed Probabilistic Graphical Models (DGMs) provide a flexible and
powerful framework for probabilistic reasoning. In all generality, they
are a way to efficiently represent complex probability distributions in
high dimensional spaces by factorizing the joint distribution into
conditional distributions.

Given a set of random variables \$ x\_i \sim X\_i \$ and their joint
random vector \$ x \sim X \$ with \$ x = \{ x\_1, \ldots, x\_N \} \$ we
represent their joint distribution as the product of a set of
conditional distributions

\[
P(X) = \prod_i^N P_i(X_i|{pa}(X_i)
\]

Where \$ pa(X\_i) \$ is the set of parents of variable \$ X\_i \$ in a
directed graph \$ G \$, where each vertex in the graph represents one of
the random variables.

Each vertex in this graph could assign an arbitrary probability
distribution to it's random variables. But this probability distribution
may only depend on the parents of the variable in the graph.

Such a representation has many advantages, even to list them completely
would be out of the scope of this article. An excellent overview is
present in the book
\href{http://mitpress.mit.edu/books/probabilistic-\%20graphical-models}{Probabilistic
Graphical Models} by Daphne Koller, who also offers a
\href{https://www.coursera.org/course/pgm}{Coursera course} by the same
name, which is highly recommended.

\paragraph{D-Separation / Conditional Independence}

Most importantly, the graph encodes information about (conditional)
independencies among the variables. By a property called D-Separation
which can be determined using a few simple rules, we can safely
determine whether the probability distribution of a given set of
variables in the graph can be affected by changes in the probability of
another set of variables \textbf{given} another set of variables which
are held fixed.

If the conditional dependencies that hold over a joint probability
distribution are a subset of the conditional independence assumptions
made by the graph, this distribution is compatible to the graph in the
sense that the distribution can be faithfully represented by a
factorization of the distribution along that graph.

\paragraph{Causal Models}

A very common form of these models is to restrict parent / child
relationships to cause/effect pairs. Furthermore, the network must be
complete in the sense that no common causes of any two variables are
missing from the model.

While it is not neccessary for the machinery of DGMs to work that they
are causal models, this can, under certain circumstances, be used to
perform so called causal inference or causal reasoning using DGMs. More
on that can be found in Judea Pearl's excellent book on
\href{http://bayes.cs.ucla.edu\%20/BOOK-2K/}{Causality}.

One important rule to note is, that in order to perform causal inference
in such a causal network, i.e.~to estimate the impact of an explicit
\textbf{action} where the value of a variable is forced to have a
certain value (in contrast to observing it having that value) it is
neccessary to sever all ties from the parents of said variable to it
(since they are no longer causally connected). Furthermore, the network

In Pearl's see/do calculus, he discerns between \$ P(x\textbar{}y) \$ (
probability of x given that I \emph{see} $y$ ) and \$
P(x\textbar{}do(y)) \$ ( probability of x given that I \emph{do} x).

While not of further concern here, Pearl provides a great deal of
informal insights and formal rules into when and how observations can be
converted into causal claims, how to transfer the results of studies
from one setting to another.

\paragraph{Common types of DGMs}

Some common types of DGMs that you might have heard about include:

\begin{itemize}
\item
  Bayesian Networks (BNs)
\item
  Hierarchical Bayesian Models
\item
  (Gaussian) Mixture Models (GMMs)
\end{itemize}
Also a lot of common models for time-series can be thought of as DGMs,
among them:

\begin{itemize}
\item
  Vector Auto-Regressive Models (VAR)
\item
  Hidden Markov Models (HMMs)
\item
  State Space Models (SSM)
\item
  Dynamic Bayesian Networks (DBNs)
\end{itemize}
Many of these models have their own set of specialized inference and
learning algorithms, their own set of advantages and disadvantages.

\paragraph{Inference / Reasoning in Graphical Models}

Generally speaking, we can use these graphical models to reason about
the marginal probability distributions, most likely configurations (MLE
/ MAP configurations) etc. of variables of interest \textbf{given
evidence}. This in turn can be used in many applications, from decision
support (making decisions under uncertainty) and as a key component for
supervised, unsupervised and semi- supervised learning.

\paragraph{Parametric VS Nonparametric representations}

If we can restrict our probability distributions to come from specifiy
families of distributions, inference can be made very efficient in some
cases. But if you want to have a general model which can capture any
kind of weird multi-modal and non-continous distributions, you are
limited to very slow inference using MCMC methods. Also, the scalability
in these cases is very limited, because most MCMC algorithms are not
made to be distributed.

\textbar{} Family \textbar{} Advantages \textbar{} Disadvantages
\textbar{}
\textbar{}------------------------------------------\textbar{}----------------\textbar{}-------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------------\textbar{}
\textbar{} Conditional Linear Gaussian (CLG) \textbar{} Very Fast
\textbar{} Are your distributions linear combinations of gaussians ?
\textbar{} \textbar{} Discrete (Categorical) \textbar{} Fast \textbar{}
High number of parameters if number of parents of any variable, or
number of discrete ``bins'' of variables becomes too large. Quickly
becomes intractable in these cases. \textbar{} \textbar{} Generic
(arbitrary functions of parents) \textbar{} Most Flexible \textbar{}
Very slow inference (MCMC) or strongly biased approximate inference,
hard to determine convergence / mixing. Usually intractable in high
dimensions. \textbar{}

\paragraph{Hierarchical Bayesian Models and MCMC}

In Bayesian Hierarchical Modeling, we are usually either interested in
estimating marginals of certain model parameters in order to gain
insights into specific problems, or we are interested in evaluating the
expected value of some (utility- or loss-) function over the posterior
of a set of random variables.

Given that in the Bayesian view, the unknown parameters of a model are
random variables like any other, so we can use the machinery of DGM
inference. Since these models can have almost arbitrary functional
relations between variables, it is commong to perform Markov Chain Monte
Carlo simulation to sample from the posterior distribution.

What is problematic about these methods is that inference is usually
slow, and it is hard to determine whether the model has converged
(mixed) to a stable posterior. Generally, MCMC does not scale well to
high-dimensional problems using established methods (yet), despite the
fact that there have been some special areas where MCMC methods could be
applied to solve high dimensional problems such as large scale matrix
factorization for recommender systems.

MCMC, while an approximate approach, is asymptotically exact if applied
correctly.

\paragraph{Message Passing Algorithms}

Among the most efficient algorithms for exact and approximate inference
in discrete and conditional linear gaussian DGMs are so called Message
Passing (MP) or Belief Propagation Algorithms. These algorithms operate
on a so-called factor graph, which is very similar to a DGM, except that
vertices (factors) may represent joint distributions of multiple
variables. If factors share variables, they have to be connected (at
least indirectly) using a chain of factors where each factor contains
that variable.

Correspondingly, edges (along which messages are passed) need to be able
to convey information about joint distributions of several variables at
once.

By collapsing a DGM into a factor graph tree (so called Clique Tree or
Junction Tree), it is possible to perform efficient exact inference on
discrete and conditional linear gaussian networks using message passing
inference. That is, unless the resulting tree has at some point a too
large tree-width (loosely, the result of a large but too dense graph),
which can make exact inference intractable.

Even in those cases where exact inference is intractable, the Loopy
Belief Propagation algorithm can provide very fast approximate
(asymptotically biased) inference, providing good solutions (empirical
results) in cases where other algorithms fail or are too slow.

It is important to note that the core algorithm (message passing) of
approximate Loopy Belief Propagation and exact Clique Tree Inference are
the same.

Again, I refer to the book
\href{http://mitpress.mit.edu/books/probabilistic-graphical-models}{Probabilistic
Graphical Models} by Daphne Koller and her
\href{https://www.coursera.org/course/pgm}{Coursera course} for details.

\paragraph{Nonparametric and Particle Belief Propagation / Message
Passing}

As Dauwels et al. have pointed out in the Paper
\href{http://www.dauwels.com/Papers/Particle.pdf}{Particle Methods as
Message Passing}, it is actually possible to combine parametric exact
inference and nonparametric approximate inference by using
\textbf{Particle Lists} as messages in the message passing algorithm.

They used this approach to show that it is possible to view common MCMC
procedures such as Gibbs Sampling, Metropolis Hastings and Importance
Sampling as special cases of \textbf{Particle Message Passing}.

What is also important here: Each factor \textbf{could be an independend
MCMC sampler working on a subset of the variables and / or evidence}. Or
a faster parametric inference algorithm, if the problem allows.

Other fast inference algorithms such as \textbf{Expectation Propagation}
and other Forms of Variational Inference such as \textbf{Mean-Field}
based method can be also be cast as variants of Message Passing
procedures.

There are several key papers which describe important aspects and
approaches that might be taken:

\begin{itemize}
\item
  \href{http://www.dauwels.com/Papers/Particle.pdf}{Dauwels et Al:
  Particle Methods as Message Passing}
\item
  \href{http://ssg.mit.edu/nbp/papers/nips03.pdf}{Ihler, Sudderth et al:
  Nonparametric Belief Propagation}
\item
  \href{http://ssg.mit.edu/nbp/papers/nips03.pdf}{Ihler, Sudderth et al:
  Efficient Multiscale Sampling from Products of Gaussian Mixtures}
\item
  \href{http://machinelearning.wus\%20tl.edu/mlpapers/paper\_files/AISTATS09\_IhlerM.pdf}{Ihler,
  Mc. Allister: Particle Belief Propagation}
\item
  \href{http://robotics.stanford.edu/~koller/Papers/Koller+al:UAI99.pdf}{Koller
  et al: A General Algorithm for Approximate Inference and Its
  Application to Hybrid Bayes Nets}
\end{itemize}
\subparagraph{The Message Fusion Problem}

All of the above papers identify a single performance bottleneck in
these algorithms.

If we have two factors which share at least one continuous variable \$ x
\$ with a smoot pdf, the probability that two factors will independently
choose the same value for that variable is essentially zero.

So if we have two factors represented using discrete particle lists \$
\phi(x) \$ and \$ \theta(x) \$, their product will be zero with near
certainty everywhere.

What we need to do is to perform so called \textbf{Message Fusion}, for
which several approaches have been proposed.

The standard approach is to use some form of \textbf{Kernel Density
Estimation} (KDE), which effectively represents each particle/sample not
as a discrete probability spike, but smoothes the density using an
appropriate kernel. Usually, Gaussian Kernels are used. Given an
efficient sampling procedure from products of Gaussian Mixtures, such as
\href{http://ssg.mit.edu/nbp/papers/nips03.pdf}{Ihler, Sudderth et al:
Efficient Multiscale Sampling from Products of Gaussian Mixtures}, we
can efficiently sample from these. More on that approach is found in
\href{http://ssg.mit.edu/nbp/papers/nips03.pdf}{Ihler, Sudderth et al:
Nonparametric Belief Propagation}

But a problem remains: It's computationally expensive to evaluate the
probability density and curvature (Jacobian and Hessian) of these
messages. And that might be important if we would like to sample from a
product of such a mixture density message with the probability density
function of a factor.

\paragraph{Particle Belief Propagation Approach}

In
\href{http://machinelearning.wus\%20tl.edu/mlpapers/paper\_files/AISTATS09\_IhlerM.pdf}{Ihler,
Mc. Allister: Particle Belief Propagation} it has been proposed to
sample from the particles in the messages themselves. This is similar to
what happens in \textbf{Particle Filters}. While potentially a good
idea, it (like Particle Filters) suffers from the \emph{thinning}
problem: If the message and the factor do not agree, the number of
useable particles gets very low and the procedure produces unreliable
and/or inaccurate results.

Like with Particle Filters, one approach to fix this problem is to use
\textbf{resampling}. That is, loosely speaking, we tell the original
factor where we got the message from, that we would like to have samples
of a finer resolution in certain regions. Then the original factor
replies with a new (importance sampled) message list, where it provides
new samples, with more (but downweighted) samples in the corresponding
regions of interest.

This procedure can already provide accurate distributed inference. It
just has one problem: It is probably pretty slow (all this re-sampling)
and requires lots of communication.

If we let the numbers of particles in each list become low, it can be
seen as a form of Gibbs Sampling where we exchange not just one particle
(the sample), but multiple of them.

This procedure, as inefficient as it might seem, might have a distinct
advantage over most MCMC Algorithms: \textbf{It allows for much easier
convergence diagnostics}. By measuring the convergence on a per-message
level, we can probably automatically determine when the algorithm has
converged to a final solution, given that we can determine this for each
factor individually.

While this sounds not so much of a great deal, actually it is: For MCMC
you usually need a human expert who decides if the numbers of samples
have been sufficient, if all relevant states have been visited etc. But
even then, that person can never be sure. Much the less, if the problem
gets high-dimensional. Having a clear convergence diagnostic opens the
door for novel applications in large scale risk analysis.

\paragraph{Compact Message Density Estimation for Message Fusion}

Another approach, which has also been taken or at least proposed by
several researchers ( see
\href{http://mach\%20inelearning.wustl.edu/mlpapers/paper\_files/AISTATS09\_IhlerM.pdf}{Ihler,
Mc. Allister: Particle Belief Propagation} for an overview) is to try to
estimate message densities using some form of nonparametric density
estimation technique which both smoothes the distribution, and
compresses the amount of data required to transfer the message. See
\href{http://robotics.stanford.edu/~koller/Papers/Koller+al:UAI99.pdf}{Koller
et al: A General Algorithm for Approximate Inference and Its Application
to Hybrid Bayes Nets} for a more thorough discussion of this.

In that paper, Density Estimation Trees (DETs) with GMMs at the leaves
have been used with success by Koller et. al. as density estimators, so
that might be a good choice to make as well. They iteratively refined
these density estimates using an iterative approach, similar to the
resampling mentioned above.

Generally, Multivariate Gaussian Mixture Models (GMMs) trained with
Regularized Expectation Maximization (EM) might be a another good
choice. See \href{http://www.cs.ubc.ca/~murphyk/MLbook/}{Kevin Murphy:
Machine Learning: A Probabilistic Perspective, Chapter 11}. These would
lend themselves to the fast methods in
\href{http://ssg.mit.edu/nbp/papers/nips03.pdf}{Ihler, Sudderth et al:
Efficient Multiscale Sampling from Products of Gaussian Mixtures}

But how do we determine the optimal number of mixture components ? Maybe
we can make the algorithm automatically choose the number of components
based on the data ?

One obvious but very slow approach would be to use cross-validation to
select an optimal number of components. But this gets prohibitevely
slow. A different approach would be to use a
\href{http://www.gatsby.ucl.ac.uk/~edward/pub/inf.mix.nips.99.pdf}{Dirichlet
Process Clustering}, also called \textbf{Infinite Gaussian Mixture
Model} to choose a data-dependent number of components. Alternatively, a
possibly better alternative is not to use the Dirichlet Process Prior
for the number of components, but rather a
\href{http://en.wikipedia.org/wiki/Pitman\%E2\%80\%93Yor\_process}{Pitman-Yor
Process}, a more flexible two-parameter generalization of the Dirichlet
Process which allows for Power-Law (fat) tails.

There are a lot of ready-made implementations of these (except for the
Pitman- Yor Process Clustering), see
\href{http\%20://scikit-learn.org/stable/modules/mixture.html}{Scikit-Learn
Documentation: Mixtures}

Another (novel) approach is the following, which might be more efficient
if the Particle Lists are created using MCMC Sampling.

\paragraph{Embarassingly Parallel MCMC}

In a recent paper (
\href{http://arxiv.org/pdf/1311.4780v1.pdf}{Neiswanger et al:
Embarassingly Parallel MCMC} ) an asymptotically exact algorithm for
performing embarassingly parallel distributed MCMC was presented.
Interestingly, the main problem solved in that paper is almost exactly
the Message Fusion problem stated above. So by solving one problem, we
get to solve inference for both high-dimensional and big-data problems.

\paragraph{Density Mapping (DRAFT)}

The core idea for Density Mapping is to combine MCMC, Gradient Ascent or
EM and Kernel Density Estimation (KDE) into a single, more efficient
algorithm for Kernel Density Estimation which can be used in the context
of large scale distributed Nonparametric Message Passing inference
engines.

We sample from a density function, and then modify the sampling density
function by subtracting from it density estimates around local modes.
This way, the probability density gets simultaneously \emph{mapped out},
ensuring that the MCMC chain spends it's computational time efficiently
by mapping so far uncovered regions of the probability space.

Let \$ f\^{}\emph{: \mathbb{R}\^{}D \mapsto \mathbb{R} \$ be our
unnormalized posterior density function which we can evaluate at any
point. The corresponding normalized density is \$ P\^{}} \$ with \$
P\^{}\emph{(x) = \frac{1}{Z} f\^{}}(x) \$ and \$ x \in
\mathbb{R}\^{}D \$, with \$ Z \$ being the normalization constant, i.e.
\$ Z = \int{f^*(x) dx} \$

We assume that we can consistently estimate the density \$ P\^{}* \$
(usually a posterior) using a suitable Markov Chain Monte Carlo
algorithm such as a Metropolis Hastings sampler given the unnormalized
density function \$ f\^{}*(x) \$

Now let us assume we have a kernel probability density estimate which
has been estimated step by step from N point probability masses:

\[
P^{E}(x) = \frac{1}{H} \sum_{i=1}^{N} \gamma_i \cdot K_i(x)
\]

Where \$ H = \sum{\gamma_i} \$ and each \$ K\_i \$ is itself a properly
normalized density function (Kernel) with a single mode \$k\_i =
\{argmax\}\_\{x\} K\_i(x) \$ with \$ k\_i \in \mathbb{R}\^{}D \$. We
define the unnormalized kernel density at time step N as

\[
f_N^E(x) =  \sum_{i=1}^{N} \frac{f_i^E(k_i)}{K_i(k_i)} \cdot K_i(x)
\]

with \$ f\_0\^{}E(x) = f\^{}\emph{(x) \$. Correspondingly, we define \$
\gamma\_i = \frac{f^*(k_i)}{f_i^E(k_i)} \$ which ensures that \$
f\_N\^{}E(x) = P\^{}\{E\}(x) = f\^{}}(x) \$ for all points \$ x \in \{
k\_1, \ldots, k\_N \} \$.

We define the \textbf{Unnormalized Sampling Function} as:

\[
f^F(x) = min(max(f^*(x)-f^E(x), f^*(x)^\frac{1}{s}), f^*(x))
\]

With \$ s \textgreater{} 1 \$ being a cooling factor which flattens the
original distribution. Plausible initial values for s might be in the
range from 2 to 100 depending on how flat we would like the distribution
to become.

If we chose the density estimation Kernels \$ K\_i \$ such that they (at
least approximately) have limited support around their mode or mean \$
x\_i \$, this Sampling Function can be calculated (or approximated)
quickly, even if N (the number of kernels in the density estimate) is
large, by just taking the nearest kernels to a given point into account.
Such a lookup can be made efficient using \textbf{KD-Trees} or
\textbf{Cover-Trees} to be performed in \$ O(D \cdot \log(N)) \$ time,
with \$ N \$ being the number of components, and \$ D \$ being the
dimensionality of the points.

During or after MCMC sampling, we should check for each sample whether
\$ f\textsuperscript{*(x)-f}E(x) \$ becomes negative at that point. This
would indicate regions where we over-estimate the density. In such a
case, it should be possible to shrink the variance of the responsible
component of f\^{}E(x) in that direction. Since this is computationally
intensive (we have to re-compute all mixture components), this should be
prevented by scaling down the variance of the kernel components in
directions of high variance.

\paragraph{Density Mapping Algorithm (DRAFT)}

Now, after initializing s to a sensible value, and starting with \$
f\^{}E=0\$ (constant), the density estimation procedure works like this:

\begin{enumerate}[1.]
\item
  Run MCMC to collect a number of samples from \$ f\^{}F \$ . Discard
  burn-in samples.
\item
  Check all sampled points for values with negative
  f\textsuperscript{*(x)-f}E(x). Shrink the variance of responsible
  components of \$ f\^{}E \$.
\item
  Pick a random sample, and perform gradient ascent or EM to find a
  local optimum/mode of \$ f\^{}F \$: called \$ k\_i \$
\item
  Check if this is a new local optimum. If not: Increase \$ s \$ and
  continue at 1.)
\item
  Create a Kernel density estimate \$ K\_i \$ around local optimum \$
  k\_i \$ (use the Hamiltonian of \$ f\^{}* \$ at \$ k\_i \$ as a
  scale/precision matrix, apply sensible regularization)
\item
  (Optional): Add \$ k\_i \$ to a KD-Tree index to speed up
  nearest-neighbour lookups
\item
  Update \$ f\^{}E \$ and \$ f\^{}F \$ using the new kernel estimate \$
  K\_i \$
\item
  Stopping criterion: Has the density been flattened enough ? Then stop.
\item
  otherwise go to 3.) or 1.)
\end{enumerate}
The result (so I hope) is a rather good density estimate. This algorithm
has yet to be tried in practice.

\subparagraph{Conclusions}

It seems like everything is ready to build a generic framework for large
scale inference in directed probabilistic graphical models. Someone just
has to do it.


\documentclass[14pt]{article}
\usepackage[left=0.5in, right=.5in, top=0.5in, bottom=0.75in]{geometry}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{bbold}

\usepackage[left=0.5in, right=.5in, top=0.5in, bottom=0.75in]{geometry}
\usepackage{hyperref}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{caption}
\hypersetup{colorlinks,linkcolor={blue},citecolor={blue}, urlcolor={red}}
\begin{document}
	\title{Lab works : Subject 1}
	\date{}
\author{\textbf{Mahmoud El Omar}}

\maketitle
\pagenumbering{arabic}
\section*{Theoretical Study in the case of sine wave}
We have the following signal :
\begin{align*}
	x(t) = a\cos(2\pi f_0t + \phi)
\end{align*}
	\begin{enumerate}[label=\alph*)]
	 \item We already know that $\mathcal{F}_{cc}\{\cos(2\pi f_0t)\} = \frac{1}{2}(\delta (f-f_0) + \delta(f+f_0))$ and $\mathcal{F}_{cc}\{\sin(2\pi f_0t)\} = \frac{1}{2j}(\delta (f-f_0) - \delta(f+f_0))$ \break
	\newline
	 $x(t) = a\cos(2\pi f_0t + \phi) = a(\cos(2\pi f_0t)\cos(\phi) - \sin(2\pi f_0t)\sin(\phi)) \Rightarrow \newline
	 \newline $For the sake of notation simplicity, let's denote $ \boxed{X(f) =  \mathcal{F}_{cc}x(f)}$ and $\boxed{X_s(\lambda) = \mathcal{F}_{dc}x_s(\lambda)}$ \newline \newline
	 \begin{equation*}
	 	X(f) = \frac{a}{2}\cos(\phi)(\delta (f-f_0) + \delta(f+f_0)) + \frac{a}{2j}\sin(\phi)(\delta (f-f_0) - \delta(f+f_0)) \Rightarrow \newline \newline
	  \boxed{X(f) = \frac{a}{2}(\delta(f-f_0)e^{j\phi} + \delta(f+f_0)e^{-j\phi})}
	 \end{equation*}

	  \item The sampled signal  $x_s[n] = x(nT_s) = x(\frac{n}{f_s}) = a\cos(2\pi \frac{f_0}{f_s}n + \phi) = a\cos(2\pi \lambda_0 n + \phi)$ with $\lambda_0 = \frac{f_0}{f_s}$
	  \begin{equation*}
	  	X_s(\lambda) = \sum_{n=-\infty}^{\infty} x_s[n]e^{-j2\pi \lambda n} = \sum_{n=-\infty}^{\infty} a\cos(2\pi \lambda_0 n+ \phi)e^{-j2\pi \lambda n}
	 \end{equation*}

	 \begin{equation*}
	 X_s(\lambda) = \frac{a}{2}\sum_{n=-\infty}^{\infty}e^{j2\pi \lambda_0 n}e^{-j2\pi \lambda n}e^{j\phi} + e^{-j2\pi \lambda_0 n}e^{-j2\pi \lambda n}e^{-j\phi}
	 \end{equation*}

	\begin{align*}
		X_s(\lambda) = e^{j\phi}\frac{a}{2}\sum_{n=-\infty}^{\infty}e^{-j2\pi (\lambda - \lambda_0) n} + \sum_{n=-\infty}^{\infty}e^{-j2\pi (\lambda + \lambda_0) n}
 	\end{align*}
 	\begin{equation*}
 	\boxed{
 		X_s(\lambda) = \frac{a}{2}e^{j\phi}\sum_{k = -\infty}^{\infty}\delta(\lambda - \lambda_0 - k) +\frac{a}{2}e^{-j\phi}\sum_{k = -\infty}^{\infty}\delta(\lambda + \lambda_0 - k)}
 	\end{equation*}
Using the scaling property of the Dirac delta function : $|a|\delta(at) = \delta(t)$, and since $\lambda = \frac{f}{f_s}$ and $\lambda_0 = \frac{f_0}{f_s}$, we can reach the following :
	\begin{equation*}
		\mathcal{F}_{dc}x_s(\lambda) = \mathcal{F}_{dc}x_s(\frac{f}{f_s}) = \frac{a}{2}e^{j\phi}\sum_{k = -\infty}^{\infty}\delta(\frac{f-f_0 - kf_s}{f_s}) +  \frac{a}{2}e^{-j\phi}\sum_{k = -\infty}^{\infty}\delta{\frac{f+f_0 - kf_s}{f_s})}
	\end{equation*}
	\begin{equation*}
		\mathcal{F}_{dc}x_s(\frac{f}{f_s}) =  \frac{af_s}{2}e^{j\phi}\sum_{k = -\infty}^{\infty}\delta(f-f_0 - kf_s) +  \frac{af_s}{2}e^{-j\phi}\sum_{k = -\infty}^{\infty}\delta(f+f_0 - kf_s)
	\end{equation*}

	\begin{equation*}
		\boxed{\frac{1}{f_s}\mathcal{F}_{dc}x_s(\frac{f}{f_s}) =  \frac{a}{2}e^{j\phi}\sum_{k = -\infty}^{\infty}\delta(f-f_0 - kf_s) +  \frac{af_s}{2}e^{-j\phi}\sum_{k = -\infty}^{\infty}\delta(f+f_0 - kf_s)}
	\end{equation*}

	\item The Fourier Transform of rect${_N{_t}}$ is the Dirichlet kernel $D_{N_{t}}(\lambda)$ as defined in the course notes.
	\begin{equation*}
	y[n] = x_s[n]\text{rect}_{N_t} \iff Y(\lambda) = X_s(\lambda)*D_{N_{t}}(\lambda)
	\end{equation*}
	\begin{equation*}
	\boxed{Y(\lambda) = \frac{a}{2}e^{j\phi}\sum_{k = -\infty}^{\infty}D_{N_{t}}(\lambda - \lambda_0 - k) +\frac{a}{2}e^{-j\phi}\sum_{k = -\infty}^{\infty}D_{N_{t}}(\lambda + \lambda_0 - k)}
	\end{equation*}
	\newpage
	\item Since $f_s >>2f_0$, therefore the Shannon condition is satisfied and the periodic spectrums do not overlap, we can perform all our calculations on just one period of the spectrum $Y(\lambda)$ without loss of generalisation. We can chose any portion of the spectrum that is equal to 1 \textit{(since $Y(\lambda)$ is 1-periodic)}. So for reasons of symmetry, I will chose the period centred around 0, that corresponds to $k=0$ in the previous equation.
	\newline \newline
	From here on out, I will consider that :
	\begin{equation*}
	Y(\lambda) = \frac{a}{2}e^{j\phi}D_{N_t}(\lambda - \lambda_0) + \frac{a}{2}e^{-j\phi}D_{N_t}(\lambda + \lambda_0)
	\end{equation*}
	To sample $Y(\lambda)$, we need to take its value at frequencies multiple of $\frac{1}{N_f}$, so if we multiply $Y(\lambda)$ with a discrete comb of repeated deltas with period equal to $\frac{1}{N_f}$. In other words, replace $\lambda$ with $\frac{n}{N_f} \text{where} n \in \mathbb{Z}$ in $Y(\lambda)$ and we will manage to get the desired sampled spectrum :
	\begin{equation*}
	Y_{N_f}[n] = Y(\lambda)\textbf{1}_{\uparrow N_f}[n] = 	Y(\lambda)\sum_{k = -\infty}^{\infty}\delta[n - \frac{k}{N_f}]	 = Y(\frac{k}{N_f})
	\end{equation*}
	\begin{equation*}
		Y_{N_f}[n] = \frac{a}{2}e^{j\phi}D_{N_t}(\frac{n - n_0}{N_f}) + \frac{a}{2}e^{-j\phi}D_{N_t}(\frac{n + n_0}{N_f})
	\end{equation*}
	\begin{figure}[hp]
		\centering
		\includegraphics[width = 10cm]{1}
		\caption{Plot of the Spectrum of $x(t)$.}
		\label{fig1}
	\end{figure}
	\begin{figure}[hp]
		\centering
		\includegraphics[width = 16cm]{3.png}
		\caption{Plot of the Spectrum of $x_s[n]$.}
		\label{fig2}
	\end{figure}

	\begin{figure}[t]
		\centering
		\includegraphics[width = 20cm]{2.jpg}
		\caption{Plot of the Spectrum of $y[n]$ over 2 periods.}
		\label{fig1}
	\end{figure}

	\end{enumerate}
	\section*{Numerical Implementation}
	\begin{enumerate}[label=\alph*)]
	\item If $N_f$ = $N_t$, we can recognise the formula for the Discrete Fourier Transform, where the result of this computation is the discrete spectrum of the discrete time signal $y[n]$, and it allows us to obtain the spectral samples directly from the time samples.
	\item If $N_f \geq N_t$, then we can zero samples after the $N_t -1 $ sample of the original signal until we can have $N_f = N_t$ and then, we'll be able to calculate the Discrete Fourier Transform and obtain the spectral samples.
	\end{enumerate}

\end{document}
w

spectral analysis in matlab/report/Untitled.tex


\subsection{The Investment Saving - Liquidity preference - Money supply (IS-LM) model}

The IS curve plots output against the (real) interest rate. As (real) interest rates rise, investment and therefore output falls.

The LM curve plots output against the (nominal) interest rate. As output rises, (nominal) interest rates fall to ensure clearing.

As prices are fixed in the IS-LM model, we can use the real and nominal rates interchangably.

The IS-LM model identifies the intercepts of the two curves and the equilibrium output and interest rate.

This model takes prices, money supply, taxes and government spending to be exogenous.

\subsubsection{Effect of monetary expansion}

In the LM model a monetary expansion lowers interest rates.

In the IS-LM model this effect is lessened. The lower interest rates cause higher output, increasing money demand, and raising interest rates.

\subsubsection{Effect of fiscal expansion}

Is the IS model a fiscal expansion caused a corresponding increase in output.

In the IS-LM model this is lessened because the increase also causes more real money demand, raising interest rates, and lowering output.


\documentclass[../Thesis]{subfiles}

\begin{document}

\chapter{Introduction}
\chaptermark{Chapter 1 title} % optional for veryy long chapter, you can rename what appear in the header
\section{Start}
\subsection{Basic info}
I studied \autocite{C01} and that was fun. I also looked at \autocite{C02} \footnote{you should do footnotes like this. More details here \url{https://www.overleaf.com/learn/latex/Footnotes}}.

I also wrote this \autocite{C05}

URI should be included like this \url{https://github.com/jackred/Heriot_Watt_Thesis_Template}.

\subsection{Equation}
Equations placed on separate lines from the text should be numbered whether or not they are referred to in the text. Numbering should appear in round brackets at the right hand side of the page and be ordered consecutively either throughout the thesis as (1) etc, or in each chapter (1.1) etc. Equations should be referred to in the text as equation(1) etc.\par
\bigskip
Believe it or not, this is the equation for the canonical version of PSO, using the inertia factor, first proposed in 1998.
\begin{equation}
    V_{i} = wV_{i} + c_{1} * U(0,1) * (P_{i} - X_{i}) + c_{2} * U(0,1) * (L_{i} - X_{i})
    \label{eqn:velocityInertia}
\end{equation}

% you refer an equation like this
And here you have the constriction factor equation, which have the same role as the inertia factor defined in \eref{eqn:velocityInertia}, but is used differently. Proposed in 1999.
\begin{equation}
    \chi = \dfrac{2}{|2 - \phi - \sqrt{\phi^{2} - 4\phi}|}
    \label{eqn:velocityConstriction}
\end{equation}

Was I just lazy and copied the equation of my Master Thesis? Not at all. Look, here is the gravity equation for PSO2011, proposed, as the name suggest, in 2011.
\begin{equation}
    G_{i} = \dfrac{X_{i} + (X_{i}+U(0,1)c(P_{i}-X_{i})) + (X_{i}+U(0,1)c(L_{i}-X_{i}))}{3}
    \label{eqn:gravityVelocity2011}
\end{equation}

\clearpage  % aesthetic purpose

\subsection{Tables}
Tables, figures etc. shall be numbered either consecutively throughout the thesis–Table 1, Figure 1 etc., or within individual chapters Chapter –Table1.1, but not within sections or subsections. With in the text tables should be referred to as table 1etc.

\begin{table}[H]
    \centering
        \begin{tabular}{llllll}
        D10 & min      & max      & median   & std      & average  \\
        f1  & 0.00E+00 & 0.00E+00 & 0.00E+00 & 0.00E+00 & 0.00E+00 \\
        f3  & 4.40E-02 & 1.37E+08 & 3.49E+05 & 2.18E+07 & 6.46E+06 \\
        f8  & 2.01E+01 & 2.05E+01 & 2.03E+01 & 8.44E-02 & 2.03E+01 \\
        f9  & 1.51E+00 & 6.96E+00 & 4.54E+00 & 1.21E+00 & 4.53E+00 \\
        f15 & 1.41E+02 & 1.04E+03 & 7.16E+02 & 2.26E+02 & 6.64E+02 \\
        f20 & 1.26E+00 & 3.82E+00 & 3.02E+00 & 5.67E-01 & 2.93E+00 \\
        f21 & 1.00E+02 & 4.00E+02 & 4.00E+02 & 1.05E+02 & 3.33E+02 \\
        f22 & 8.79E+01 & 8.30E+02 & 4.83E+02 & 2.00E+02 & 4.91E+02 \\
        f25 & 2.04E+02 & 2.23E+02 & 2.15E+02 & 3.73E+00 & 2.15E+02
        \end{tabular}
    \rule{35em}{0.5pt}
    \caption[Example table]{Summary Statistics for the 10 dimensional case of PSO 2007 with a ring neighborhood of 4. I know you don't know what it means. But at least you have an example of a table. More info here \url{https://www.overleaf.com/learn/latex/Tables}}
    \label{tab:PSO_2007_D10_R4}
\end{table}

\subsection{Figures}
Because I think you are very interested in PSO (or I am just very lazy) here is a nice figure explaining how PSO 2006 works.
\begin{figure}[H]
    \centering
    \includegraphics[]{Figures/pso2006.png}
    \rule{35em}{0.5pt}
    \caption[SPSO 06/07 movement]{SPSO 2006 and 2007 particle's position update. $X'_{i}$ and $X"_{i}$ are temporary point to explain the second and third terms of equation \ref{eqn:velocityInertia}.}
    \label{fig:schemaPSO2006Update}
\end{figure}

\clearpage % aesthetic purpose

And while we are at it, look at the 2011 version, that you can't probably understand without the context, but eh, it's a figure to illustrate how to put some. We can see it is different than \fref{fig:schemaPSO2006Update}
\begin{figure}[H]
    \centering
    \includegraphics[]{Figures/pso2011.png}
    \rule{35em}{0.5pt}
    \caption[SPSO 2011 movement]{SPSO 2011 particle's position update. $X'_{i}$ is generated inside the hyper-sphere of center $G_{i}$.}
    \label{fig:schemaPSO2011Update}
\end{figure}

I placed my figure right after the text, but you will usually use other option to place them, such as \textit{htpb}. More info \url{https://www.overleaf.com/learn/latex/Positioning_of_Figures}.

\begin{figure}[H]
    \centering
    \rotatebox{90}{\includegraphics[width=22cm]{Figures/rk4_0-100_dt-0-0001}}
    \rule{35em}{0.5pt}
    \caption[Sideways picture]{How to put a very big picture sideways. It is in French but who cares?}
    \label{fig:veryBigFigure}
\end{figure}

\end{document}


\documentclass[]{article}
\usepackage{graphicx}
\usepackage{caption}
\graphicspath{ {./images/} }

% Minted
\usepackage[cache=false]{minted}
	\usemintedstyle{vs}
	\usepackage{xcolor}
		\definecolor{light-gray}{gray}{0.97}

\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{comment}

\title{Data Mining: Exercise 3-2}
\author{Brandon Hosley}
\date{\today}

\begin{document}
\maketitle

\section*{Do the Five Assignments in the Exercise Guide}

\begin{minted}[breaklines,bgcolor=light-gray]{shell-session}
from pyspark.ml.fpm import FPGrowth
from pyspark.sql.functions import col, split, count, collect_set
\end{minted}

\clearpage

\subsection*{Q1: Reading data}
\begin{enumerate}[before=\itshape,label=\arabic*.]
	\item Write spark codes to read above data.
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Read four tables only for this exercise
		\begin{enumerate}[before=\itshape,label=\roman*.]
			\item orders
			\item products
			\item departments
			\item order\_products\_train
		\end{enumerate}
		\item Read “headers” as well
		\begin{enumerate}[before=\itshape,label=\roman*.]
			\item Each csv file of the dataset has a header line.
		\end{enumerate}
	\end{enumerate}
	\item Take screenshots of running your code in your pyspark shell in your terminal.
\end{enumerate}

\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
departments = spark.read.format('csv').options(header='true').load( '/user/data/CSC533DM/instacart/departments.csv')
order_products_train = spark.read.format('csv').options(header='true').load( '/user/data/CSC533DM/instacart/order_products__train.csv')
orders = spark.read.format('csv').options(header='true').load( '/user/data/CSC533DM/instacart/orders.csv')
products = spark.read.format('csv').options(header='true').load( '/user/data/CSC533DM/instacart/products.csv')

departments.createOrReplaceTempView("departments")
order_products_train.createOrReplaceTempView("order_products_train")
orders.createOrReplaceTempView("orders")
products.createOrReplaceTempView("products")
\end{minted}
\includegraphics[width=\linewidth]{image1}

\clearpage

\subsection*{Q2: Training a model using FPGrowth}
\begin{enumerate}[before=\itshape,label=\arabic*.]
	\item Write spark codes to train the data to calculate frequent itemsets
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Review Ex3-1
		\item Pyspark use different library to use FPGrowth
		\item Read data from baskets view
		\begin{enumerate}[before=\itshape,label=\roman*.]
			\item Using this query: select items from baskets
			\item Should understand how the data will be organized or stored in DataFrame
		\end{enumerate}
		\item Use MinSupport = 0.001, MinConfidence = 0
		\item Create temporary view
	\end{enumerate}
	\item Take screenshot(s) of running your codes in your pyspark shell in your terminal.
	\item Take a screen shot of output of frequentItemsets
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Using this command: model.freqItemsets.show(15, truncate=False)
		\item Show 15 rows, not 10 rows. (below output shows 10 rows)
	\end{enumerate}
\end{enumerate}

\begin{comment}
df_orders_dow = spark.sql("""
	select count(order_id) as total_orders,
		(case
			when order_dow = '0' then 'Sunday'
			when order_dow = '1' then 'Monday'
			when order_dow = '2' then 'Tuesday'
			when order_dow = '3' then 'Wednesday'
			when order_dow = '4' then 'Thursday'
			when order_dow = '5' then 'Friday'
			when order_dow = '6' then 'Saturday'
		end) as day_of_week
	from orders
	group by order_dow
	order by total_orders desc
""")

df_orders_dow.show()

df_orders_hour = spark.sql("""
	select count(order_id) as total_orders, order_hour_of_day as hour
		from orders
	group by order_hour_of_day
	order by order_hour_of_day
""")

df_orders_hour.show(24)

df_orders_shelf = spark.sql("""
	select d.department, count(distinct p.product_id) as products
		from products p
	inner join departments d
		on d.department_id = p.department_id
	group by d.department
	order by products desc
	limit 10
""")

df_orders_shelf.show()

rawData = spark.sql("select p.product_name, o.order_id from products p inner join order_products_train o where o.product_id = p.product_id")
baskets = rawData.groupBy('order_id').agg(collect_set('product_name').alias('items'))
baskets.createOrReplaceTempView('baskets')
baskets.show(2, truncate=False)

\end{comment}

\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
# Extract out the items
baskets_ds = spark.sql("select items from baskets")

# Use FPGrowth
fpgrowth = FPGrowth().setItemsCol("items").setMinSupport(0.001).setMinConfidence(0)
model = fpgrowth.fit(baskets_ds)

# Calculate frequent itemsets
mostPopularItemInABasket = model.freqItemsets

# Create temporary view
mostPopularItemInABasket.createOrReplaceTempView("mostPopularItemInABasket")

# Show Output of:
model.freqItemsets.show(15, truncate=False)
\end{minted}

\includegraphics[width=\linewidth]{image2}

\subsection*{Q3: Finding frequent itemsets}
\begin{enumerate}[before=\itshape,label=\arabic*.]
	\item Take a screenshot of running above codes in your pyspark shell in your terminal.
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item SQL query: select items, freq from mostPopularItemInABasket where size(items) \\ $>$ 2 order by freq desc limit 20
	\end{enumerate}
	\item Take a screen shot of output of frequentItemsets
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Using this command: mostPopularItemInABasket\_2.show(15, truncate=False)
		\item Show 15 rows, not 10 rows. (above output shows 10 rows)
	\end{enumerate}
\end{enumerate}

\begin{minted}[breaklines,bgcolor=light-gray]{shell-session}
model.freqItemsets.show(10, truncate=False)
mostPopularItemInABasket_2 = spark.sql("""
select items, freq from mostPopularItemInABasket where size(items) > 2 order by freq desc limit 20
""")
mostPopularItemInABasket_2.show(10, truncate=False)
\end{minted}
\includegraphics[width=\linewidth]{image3}

\subsection*{Q4: Generating association rules}
\begin{enumerate}[before=\itshape,label=\arabic*.]
	\item Write spark codes
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Review Ex3-1
		\item Store generated association rule in a DataFrame (name: ifThen)
		\item Create a temporary view (name: ifThen)
	\end{enumerate}
	\item Take screenshot(s) of running your codes in your pyspark shell in your terminal.
	\item Take a screenshot of output of frequentItemsets
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Show three columns only (search ‘.select’ command)
		\begin{enumerate}[before=\itshape,label=\roman*.]
			\item Antecedent
			\item Consequent
			\item Confidence
		\end{enumerate}
		\item Show 10 rows, not 5 rows. (below output shows 5 rows with all columns)
	\end{enumerate}
\end{enumerate}

\begin{minted}[breaklines,bgcolor=light-gray]{shell-session}
# Store generated association rules in a DataFrame
ifThen = model.associationRules
# Create a view of ifThen
ifThen.createOrReplaceTempView("ifThen")
ifThen.show(10, truncate=False)
\end{minted}
\includegraphics[width=\linewidth]{image4}

\clearpage

\subsection*{Q5: Identifying items frequently purchased together (association rules)}

\begin{enumerate}[before=\itshape,label=\arabic*.]
	\item Write spark codes to find items frequently purchased together using above SQL query
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Store the results of the query as a DataFrame, named ‘assocRules’
	\end{enumerate}
	\item Take screenshot(s) of running your codes in your pyspark shell in your terminal.
	\item Take a screenshot of output of frequentItemsets
	\begin{enumerate}[before=\itshape,label=\alph*.]
		\item Show 10 rows, not 5 rows. (below output shows 5 rows)
	\end{enumerate}
\end{enumerate}

\begin{minted}[breaklines,bgcolor=light-gray]{shell-session}
assocRules = spark.sql("""
	select antecedent as `antecedent (if)`,
		consequent as `consequent (then)`,
		confidence
	from ifThen
	order by confidence desc
	limit 20
""")
assocRules.show(10, truncate=False)
\end{minted}
\includegraphics[width=\linewidth]{image5}

\end{document}

\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
\end{minted}
\includegraphics[width=\linewidth]{image1}


\subsection{Energies of Isolated Atoms}\index{EISOL}\index{EHEAT}\index{ATHEAT}
The $\Delta H_f$ calculated by semiempirical methods is defined as the  energy
in kcal.mol$^{-1}$ required to form one mole of the system in the gas phase at
298K from its elements in their standard state:
$$
\Delta H_f = E_{elect} + E_{nuc} + \sum_AE_{isol}(A) +\sum_AE_{atom}(A)
$$
In order to calculate $\Delta H_f$, the quantity $E_{isol}$ must be
determined; this is the energy required to form the isolated atom from its
valence electrons:
$$
E_{isol}(A)=E_{\rm neutral\ atom}(A) -E_{\rm nucleus}(A)-
E_{\rm valence\ electrons}(A)
$$
In the calculation of $E_{elect}$, the energy of valence electrons is  defined
as zero, likewise in calculating $E_{nuc}$, the energy of the  isolated nucleus
is defined as zero, therefore the calculation of $E_{isol}$ simplifies to the
calculation of $E_{\rm neutral\ atom}$.

The energy of $E_{eisol}$ is the energy released when the valence  electrons
are added to the nucleus.  For example, for the hydrogen atom, this would be
$U_{ss}$.  For poly-electronic atoms, the electron-electron  interactions must
be included, in addition to the one-electron contributions. Most elements have
open shell ground states, and for these systems, the  nature of the state is
important.

For all main group elements, that is, elements with valence shell
configurations of the form $ns^anp^b$, other than the alkali metals, the value
of $E_{isol}$ is  given by:
$$
E_{isol} =aU_{ss}+bU_{pp}+(a-1)G_{ss}+a.bG_{sp}+(b(b-1))/2G_{p2}-bH_{sp}-
cH_{pp}
$$
in which $c=min(b(b-1)/2,(6-b).(5-b)/2)$.  Except for the $H_{pp}$ term, all
the contributions to $E_{isol}$ are obvious.  Non-zero $H_{pp}$ terms occur
when there are two or more unpaired electrons in the ground state, in  which
case there is an exchange stabilization that is otherwise absent.

Because $H_{pp}$ is usually written as $1/2(G_{pp}-G_{p2})$, the expression
for systems with 2 to 4 $p$ electrons is recast as:
$$
E_{isol} =aU_{ss}+bU_{pp}+(a-1)G_{ss}+a.bG_{sp}+((b(b-1))/2+c/2)G_{p2}-
(a-1)bH_{sp}-c/2G_{pp},
$$
or
$$
E_{isol} =2U_{ss}+bU_{pp}+G_{ss}+2.bG_{sp}+((b(b-1))/2+c/2)G_{p2}-
(a-1)bH_{sp}-c/2G_{pp}.
$$
For the alkali metals, the equation for $E_{isol}$ is the same as that for
hydrogen.

For the transition metals, the coefficients for the $d-d$ interactions are more
complicated.

The general form for $E_{isol}$ for a transition metal of configuration
$s^md^n$s, in which there are $m_a$ $\alpha$ $s$-electrons and $m_b$ $\beta$
$s$-electrons, and $n_a$ $\alpha$ $d$-electrons and $n_b$ $\beta$
$d$-electrons, and the total angular quantum number is $L$, is:
\begin{eqnarray} \nonumber
E_{isol}&=&mU_{ss}+nU_{dd}+
(m(m-1))/2G_{ss}+m.nG_{sd}-(m_an_a+m_bn_b)H_{sd}\\ \nonumber
&& +(n(n-1))/2\frac{G_{dd}^0}{5} +(-4(n_a^2+n_b^2)+13n-3/2(L(L+1)))\frac{G_{dd}^2}{49}\\ \nonumber
&& +(-(n_a^2+n_b^2)/2-9/2n+5/6(L(L+1)))\frac{G_{dd}^4}{49}. \nonumber
\end{eqnarray}
As might be imagined, derivation of this expression is by no means  obvious,
particularly the terms for $G_{dd}^2$ and $G_{dd}^4$.  Interested readers are
referred to Racah's paper in {\it Phys Rev}, {\bf 61}, 186 (1942). In this,
Racah derived an expression for the $d$ orbital energy of the ground  state in
terms of three quantities, $A$, $B$, and $L$, the total angular momentum:
$$
<\! ^{n+1}L|H|^{n+1}L>=\frac{1}{2}n(n-1)(A-8B)+\frac{3}{2}[6n-L(L+1)]B.
$$

The quantities $A$ and $B$, and a third quantity, $C$, not used here, are
related  to the $G_k$ as follows:
\begin{eqnarray} \nonumber
A&=&G_{dd}^0-49G_{dd}^2\\ \nonumber
B&=&G_{dd}^2-5G_{dd}^4\\ \nonumber
C&=&35G_{dd}^4 \\ \nonumber
\end{eqnarray}
Using Racah's equation, derivation of  $E_{isol}$ is straightforward.  In texts
on transition metal ion theory, the quantities $G_{dd}^0$, $G_{dd}^0$, and
$G_{dd}^0$ are usually represented by the symbols $F_0$, $F_2$, and $F_4$,
respectively.  However, care should be exercised when reading these texts:
sometimes other quantities, $F^0$, $F^2$, and $F^4$ are used.  The relationship
between these three sets of symbols is as follows:
\begin{eqnarray}\nonumber
G_{dd}^0  =  F_0 &=& F^0 \\ \nonumber
G_{dd}^2  =  F_2 &=& \frac{1}{49}F^2 \\ \nonumber
G_{dd}^4  =  F_4 &=& \frac{1}{441}F^4 \\ \nonumber
\end{eqnarray}

Because the coefficients for the two electron terms are so complicated,  values
for all elements likely to be parameterized for semiempirical  methods are
presented in Table~\ref{confs}.  From this table, the values of some
coefficients are readily derived.  Thus for the $s$-$d$ coulomb integral,
$G_{sd}$, the coefficient is simply the number of $s$ electrons times the
number of $d$ electrons.  One $s$-$d$ exchange integral, $H_{sd}$, exists for
each electron in the $s$ shell for which there is an electron of the  same spin
in the $d$ shell.  For elements with two $s$ electrons, this is simply the
number of $d$ electrons, for elements with one $s$ electron, the Aufbau
principle indicates that the $d$ shell with higher occupancy has the same spin
as that of the $s$ electron.  Finally, the coefficients for the  simple $d$-$d$
repulsion integral, $G_{dd}^0$, are given by the number of possible $d$-$d$
interactions.

Note also that there are no elements with both $p$ and $d$ valence electrons,
therefore terms of the type $G_{pd}$ are not necessary.

\begin{table}
\caption{\label{confs}Two Electron Energy Contributions to EISOL for Atoms in
their Ground States}
\begin{center}
\compresstable
\begin{tabular}{cclcccccccccccc}\hline
\multicolumn{2}{c}{Element}   &  Orbital & State&  &
G$_{ss}$ & G$_{sp}$&H$_{sp}$& G$_{pp}$ &G$_{p2}$ &G$_{sd}$&
H$_{sd}$&G$_{dd}^0$&G$_{dd}^2$& G$_{dd}^4$\\
%                       Gss  Gsp Hsp  Gpp   Gp2  Gsd Hsd Gdd0 Gdd2 Gdd4
&   & Config.& & Mult.: & 1  & 1  &-1 & -1/2&1/2 &  1& -1/5 &1 &-1/49&-1/49\\
\hline
1&H  & $1s^1    $ &$^2S$&&    &    &   &     &    &   &   &    &    &     \\
2&He & $1s^2    $ &$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
3&Li & $2s^1    $ &$^2S$&&    &    &   &     &    &   &   &    &    &     \\
4&Be & $2s^2    $ &$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
5&B  & $2s^22p^1$ &$^2P$&& 1  & 2  & 1 &     &    &   &   &    &    &     \\
6&C  & $2s^22p^2$ &$^3P$&& 1  & 4  & 2 &  1  &  3 &   &   &    &    &     \\
7&N  & $2s^22p^3$ &$^4S$&& 1  & 6  & 3 &  3  &9   &   &   &    &    &     \\
8&O  & $2s^22p^4$ &$^3P$&& 1  & 8  & 4 &  1  &13  &   &   &    &    &     \\
9&F  & $2s^22p^5$ &$^2P$&& 1  & 10 & 5 &     &20  &   &   &    &    &     \\
10&Ne & $2s^22p^6$ &$^1S$&& 1  & 12 & 6 &     &30  &   &   &    &    &     \\
11&Na & $3s^1    $ &$^2S$&&    &    &   &     &    &   &   &    &    &     \\
12&Mg & $3s^2    $ &$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
13&Al & $3s^23p^1$ &$^2P$&& 1  & 2  & 1 &     &    &   &   &    &    &     \\
14&Si & $3s^23p^2$ &$^3P$&& 1  & 4  & 2 &  1  &3   &   &   &    &    &     \\
15&P  & $3s^23p^3$ &$^4S$&& 1  & 6  & 3 &  3  &9   &   &   &    &    &     \\
16&S  & $3s^23p^4$ &$^3P$&& 1  & 8  & 4 &  1  &13  &   &   &    &    &     \\
17&Cl & $3s^23p^5$ &$^2P$&& 1  & 10 & 5 &     &20  &   &   &    &    &     \\
18&Ar & $3s^23p^6$ &$^1S$&& 1  & 12 & 6 &     &30  &   &   &    &    &     \\
19&K  & $4s^1    $ &$^2S$&&    &    &   &     &    &   &   &    &    &     \\
20&Ca & $4s^2    $ &$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
21&Sc & $4s^23d^1$ &$^2D$&& 1  &    &   &     &    & 2 & 1 &    &    &     \\
22&Ti & $4s^23d^2$ &$^3F$&& 1  &    &   &     &    & 4 & 2 & 1  & 8  & 1   \\
23&V  & $4s^23d^3$ &$^4F$&& 1  &    &   &     &    & 6 & 3 & 3  & 15 & 8   \\
24&Cr & $4s^13d^5$ &$^7S$&&    &    &   &     &    & 5 & 5 & 10 & 35 & 35  \\
25&Mn & $4s^23d^5$ &$^6S$&& 1  &    &   &     &    &10 & 5 & 10 & 35 & 35  \\
26&Fe & $4s^23d^6$ &$^5D$&& 1  &    &   &     &    &12 & 6 & 15 & 35 & 35  \\
27&Co & $4s^23d^7$ &$^4F$&& 1  &    &   &     &    &14 & 7 & 21 & 43 & 36  \\
28&Ni & $4s^23d^8$ &$^3F$&& 1  &    &   &     &    &16 & 8 & 28 & 50 & 43  \\
29&Cu & $4s^13d^{10}$&$^2S$&&  &    &   &     &    &10 & 5 & 45 & 70 & 70  \\
30&Zn & $4s^2     $&$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
31&Ga & $4s^24p^1$ &$^2P$&& 1  & 2  & 1 &     &    &   &   &    &    &     \\
32&Ge & $4s^24p^2$ &$^3P$&& 1  & 4  & 2 &  1  &3   &   &   &    &    &     \\
33&As & $4s^24p^3$ &$^4S$&& 1  & 6  & 3 &  3  &9   &   &   &    &    &     \\
34&Se & $4s^24p^4$ &$^3P$&& 1  & 8  & 4 &  1  &13  &   &   &    &    &     \\
35&Br & $4s^24p^5$ &$^2P$&& 1  & 10 & 5 &     &20  &   &   &    &    &     \\
36&Kr & $4s^24p^6$ &$^1S$&& 1  & 12 & 6 &     &30  &   &   &    &    &     \\
37&Rb & $5s^1    $ &$^2S$&&    &    &   &     &    &   &   &    &    &     \\
38&Sr & $5s^2    $ &$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
39&Y  & $5s^24d^1$ &$^2D$&& 1  &    &   &     &    & 2 & 1 &    &    &     \\
40&Zr & $5s^24d^2$ &$^3F$&& 1  &    &   &     &    & 4 & 2 & 1  & 8  & 1   \\
41&Nb & $5s^14d^4$ &$^6D$&&    &    &   &     &    & 4 & 4 & 6  & 21 & 21  \\
42&Mo & $5s^14d^5$ &$^7S$&&    &    &   &     &    & 5 & 5 & 10 & 35 & 35  \\
43&Tc & $5s^24d^5$ &$^6S$&& 1  &    &   &     &    &10 & 5 & 10 & 35 & 35  \\
44&Ru & $5s^14d^7$ &$^5F$&&    &    &   &     &    & 7 & 5 & 21 & 43 & 36  \\
45&Rh & $5s^14d^8$ &$^4F$&&    &    &   &     &    & 8 & 5 & 28 & 50 & 43  \\
46&Pd & $5s^04d^{10}$&$^1S$&&  &    &   &     &    &   &   & 45 & 70 & 70  \\
47&Ag & $5s^14d^{10}$&$^2S$&&  &    &   &     &    &10 & 5 & 45 & 70 & 70  \\
\hline
\end{tabular}
\end{center}
\end{table}

\begin{table}
\caption{Two Electron Energy Contributions to EISOL for Atoms in their Ground
States}
\begin{center}
\compresstable
\begin{tabular}{cclcccccccccccc} \hline
\multicolumn{2}{c}{Element}  &  Orbital & State& &
G$_{ss}$ & G$_{sp}$&H$_{sp}$& G$_{pp}$ &G$_{p2}$ &G$_{sd}$&
H$_{sd}$&G$_{dd}^0$&G$_{dd}^2$& G$_{dd}^4$\\
%                     Gss  Gsp Hsp  Gpp   Gp2  Gsd Hsd Gdd0 Gdd2 Gdd4
&   & Config.& & Mult.: & 1  & 1  &-1 & -1/2&1/2 &  1& -1/5 &1 &-1/49&-1/49\\
\hline
48&Cd & $5s^2     $&$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
49&In & $5s^25p^1$ &$^2P$&& 1  & 2  & 1 &     &    &   &   &    &    &     \\
50&Sn & $5s^25p^2$ &$^3P$&& 1  & 4  & 2 &  1  &3   &   &   &    &    &     \\
51&Sb & $5s^25p^3$ &$^4S$&& 1  & 6  & 3 &  3  &9   &   &   &    &    &     \\
52&Te & $5s^25p^4$ &$^3P$&& 1  & 8  & 4 &  1  &13  &   &   &    &    &     \\
53&I  & $5s^25p^5$ &$^2P$&& 1  & 10 & 5 &     &20  &   &   &    &    &     \\
54&Xe & $5s^25p^6$ &$^1S$&& 1  & 12 & 6 &     &30  &   &   &    &    &     \\
55&Cs & $6s^1    $ &$^2S$&&    &    &   &     &    &   &   &    &    &     \\
56&Ba & $6s^2    $ &$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
72&Hf & $6s^25d^2$ &$^3F$&& 1  &    &   &     &    & 4 & 2 & 1  & 8  & 1   \\
73&Ta & $6s^25d^3$ &$^4F$&& 1  &    &   &     &    & 6 & 3 & 3  & 15 & 8   \\
74&W  & $6s^25d^4$ &$^5D$&& 1  &    &   &     &    & 8 & 4 & 6  & 21 & 21  \\
75&Re & $6s^25d^5$ &$^6S$&& 1  &    &   &     &    &10 & 5 & 10 & 35 & 35  \\
76&Os & $6s^25d^6$ &$^5D$&& 1  &    &   &     &    &12 & 6 & 15 & 35 & 35  \\
77&Ir & $6s^25d^7$ &$^4F$&& 1  &    &   &     &    &14 & 7 & 21 & 43 & 36  \\
78&Pt & $6s^15d^9$ &$^3D$&&    &    &   &     &    & 9 & 5 & 36 & 56 & 56  \\
79&Au & $6s^15d^{10}$&$^2S$&&  &    &   &     &    &10 & 5 & 45 & 70 & 70  \\
80&Hg & $6s^2     $&$^1S$&& 1  &    &   &     &    &   &   &    &    &     \\
81&Tl & $6s^26p^1$ &$^2P$&& 1  & 2  & 1 &     &    &   &   &    &    &     \\
82&Pb & $6s^26p^2$ &$^3P$&& 1  & 4  & 2 &  1  &3   &   &   &    &    &     \\
83&Bi & $6s^26p^3$ &$^4S$&& 1  & 6  & 3 &  3  &9   &   &   &    &    &     \\
84&Po & $6s^26p^4$ &$^3P$&& 1  & 8  & 4 &  1  &13  &   &   &    &    &     \\
85&At & $6s^25p^5$ &$^2P$&& 1  & 10 & 5 &     &20  &   &   &    &    &     \\
\hline
\end{tabular}
\end{center}
\end{table}