\subsection{gammapy.irf}
\label{ssec:gammapy-irf}
\todo{Fabio Pintore}
The {\it gammapy.irf} sub-package contains all classes and functionalities able to handle Instrument Response Functions (\irf) in a wide variety of formats. Usually, \irfs store instrument properties in the form of multi-dimension tables, with quantities expressed in terms of energy (true or reconstructed), off-axis angles, detector coordinates and so on. The main information stored in the common \gammaray \irfs are the effective area (Aeff), energy dispersion (Edisp), point spread function (PSF) and background (BKG). The {\it gammapy.irf} sub-package can open and access specific \irf extensions, interpolate and evaluate the quantities of interest on both energy and spatial axes, convert their format or units in different kinds, plot or write them into output files. In the following, we list the main sub-classes of the sub-package:

\begin{itemize}
\item Effective area: \gammapy provides the class {\it EffectiveAreaTable2D} to manage Aeff, which is usually defined in terms of true energy and offset angle. The class functionalities offer the possibility to read from files or to create it from scratch. {\it EffectiveAreaTable2D} can also convert, interpolate, write, and evaluate the effective area for a given energy and offset angles, or even plot the multi-dimensional Aeff table.

\item Point spread function: \gammapy allows user to treat different kinds of PSFs, in particular, multi-dimensional gaussians ({\it EnergyDependentMultiGaussPSF}), King functions ({\it PSFKing}), or a parametric one. The {\it EnergyDependentMultiGaussPSF} class is able to handle up to three gaussians, defined in terms of amplitudes and sigma given for each true energy and offset angle bin. Similarly, {\it PSFKing takes} into account the gamma and sigma parameters as defined here. The {\it ParametricPSF} allows to create a PSF with a representation different from gaussian(s) or King profile(s). Finally, the user can take advantage from the {\it PSFMap} class, which creates a multi-dimensional map of the PSF in WCS coordinates. At each position, a PSF kernel map ({\it PSFKernel}) provides a PSF as a function of the true energy. The creation of PSF kernel maps, where the PSF is defined for each sky-position, is also given to the user. The latter two can speed up analyses.

\item Energy dispersion: Edisp, in \iact, is generally given in terms of the so called migration parameter ($\mu$), which is defined as the ratio between the reconstructed energy and the true energy. This ratio should be as close as one and its dispersion can assume the shape of a gaussian (or even more complex distributions). Migration parameter is given at each offset angle and reconstructed energy. The main sub-classes are {\it EnergyDispersion2D}, designed to interpret Edisp, {\it EDispKernelMap}, which builds an Edisp kernel map, i.e. a 4-dimensional WCS map where at each sky-position is associated an Edisp kernel. The latter is a representation of the Edisp as a function of the true energy only thanks to the sub-class {\it EDispKernel}.

\item Background: the BKG can be represented in \gammapy as either 1) a 2D map ({\it Background2D}) of count rate normalised per steradians and energy at different energies and offset-angles or 2) as rate per steradians and energy, as a function of reconstructed energy and detector coordinates ({\it Background3D}). In the former, the background is expected to follow a radially symmetric shape, while in the latter, it can be more complex.

\end{itemize}

\begin{figure}
%	\import{code-examples/generated/}{gp_makers}

	\caption{In Fig.~\ref{ig*:minted:irf_examples}, we show some example of Aeff, PSF, Edisp and BKG read and plotted from a typical \irf.}
	\label{ig*:minted:irf_examples}
\end{figure}


\documentclass[letterpaper,10pt]{article}
\usepackage[margin=2cm]{geometry}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[colorlinks]{hyperref}
\usepackage{listings}

\newcommand{\panhline}{\begin{center}\rule{\textwidth}{1pt}\end{center}}

\title{\textbf{Linear Regression}}
\author{Aarti Singh (Instructor), HMW-Alexander (Noter)}

\begin{document}

\maketitle

\panhline
\href{../index.html}{Back to Index}

\panhline
\tableofcontents

\section*{Resources}

\begin{itemize}
	\item \href{../../Lectures/04_LinearRegression.pdf}{Lecture}
\end{itemize}

\panhline

\section{Discrete to Continuous Labels}

From classification to regression

\subsection{Task}

Given $X\in \mathcal{X}$, predict $Y \in \mathcal{Y}$, Construct prediction rule $f:\mathcal{X} \rightarrow \mathcal{Y}$

\subsection{Performance Measure}

\begin{itemize}
	\item Quantifies knowledge gained.
	\item Measure of closeness between true label Y and prediction f(X)
	\begin{itemize}
		\item 0/1 lose:$loss(Y,f(X))=1_{f(X)\neq Y}$. Risk: probability of error
		\item square loss: $loss(Y,f(X))=(f(X)-Y)^2$. Risk: mean square error
	\end{itemize}
	\item How well does the predictor perform on average?
	$$Risk~R(f)=\mathbb{E}[loss(Y,f(X))],~(X,Y)\sim P_{XY}$$
\end{itemize}

\subsection{Bayes Optimal Rule}

\begin{itemize}
	\item ideal goal: Construct prediction rule $f^*:\mathcal{X}\rightarrow\mathcal{Y}$
	$$f^*=\arg\min_f{E_{XY}[loss(Y,f(X))]}$$ (Bayes optimal rule)
	\item Best possible performance:
	$$\forall f,~R(f^*) \leq R(f)$$ (Bayes Risk)
\end{itemize}

Problem: $P_{XY}$ is unknown.

Solution: Training data provides a glimpse of $P_{XY}$
$$\text{(observed)~}\{(X_i,Y_i)\} \sim_{i.i.d} P_{XY}\text{~unknown}$$

\section{Macine Learning Algortihm}

\begin{itemize}
	\item Model based approach: use data to learn a model for $P_{XY}$
	\item Model-free approach: use data to learn mapping directly
\end{itemize}

\subsection{Empirical Risk Minimization (model-free)}

\begin{itemize}
	\item Optimal predictor: $$f^*=\arg\min_f{\mathbb{E}[(f(X)-Y)^2]}$$
	\item Empirical Minimizer: $$\hat{f}_n=\arg\min_{f\in\mathcal{F}}\frac{1}{n}\sum_{i=1}^{n}(f(X)-Y)^2$$
\end{itemize}

$\mathcal{F}$ is the class of predictors:
\begin{itemize}
	\item Linear
	\item Polynomial
	\item Nonlinear
\end{itemize}

\section{Linear Regression}

$$f(\vec{X})=\sum_{i=0}^{p}{\beta_0X^{i}}=\vec{X}^T\vec{\beta},~where~X^0=1,~\vec{\beta}=[\beta_0,\dots,\beta_p]^T$$

$$\hat{\vec{\beta}}=\arg\min_{\vec{\beta}}(A^T\vec{\beta}-\vec{Y})^T(A^T\vec{\beta}-\vec{Y}),~where~A=[\vec{X_1},\dots,\vec{X_n}]$$

$$J(\beta)=(A^T\vec{\beta}-\vec{Y})^T(A^T\vec{\beta}-\vec{Y})$$

\begin{equation*}
\begin{array}{rcl}
\frac{\partial J(\vec{\beta})}{\partial \vec{\beta}} & = & \frac{\partial (A^T\vec{\beta}-\vec{Y})^T(A^T\vec{\beta}-\vec{Y})}{\partial \vec{\beta}} \\
& = & \frac{\partial (\vec{\beta}^TAA^T\vec{\beta}-\vec{\beta}^TA\vec{Y}-\vec{Y}^TA^T\vec{\beta}+\vec{Y}^T\vec{Y})}{\vec{\beta}} \\
& = & (AA^T+(AA^T)^T)\vec{\beta}-A\vec{Y}-A\vec{Y} \\
& = & 2AA^T\vec{\beta}-2A\vec{Y} = 0 \\
& \Rightarrow & AA^T\vec{\beta}=A\vec{Y} \\
& \Rightarrow & \hat{\vec{\beta}}=(AA^T)^{-1}A\vec{Y},~\text{if $AA^T$ is invertible}
\end{array}
\end{equation*}

\subsection{Gradient Descent}

Even when $AA^T$ is invertible, might be computationally expensive if $A$ is huge; however, $J(\vec{\beta})$ is convex\footnote{A function is called convex if the line joining any two points on the function does not go below the function on the interval formed by these two points.} in $\beta$.

Minimum of a convex function can be reached by gradient descent algorithm:
\begin{itemize}
	\item Initialize: pick $\vec{w}$ at random
	\item Gradient: $$\nabla_{\vec{w}} l(\vec{w})=[\frac{\partial l(\vec{w})}{\partial w_0},\dots,\frac{\partial l(\vec{w})}{\partial w_d}]^T$$
	\item Update rule: $$\Delta \vec{w}=\eta \nabla_{\vec{w}}l(\vec{w})$$, $$w_i^{t+1} \leftarrow w_i^t - \eta \frac{\partial l(\vec{w})}{\partial w_i}|_t$$
	\item Stop: when some criterion met $\frac{\partial l(\vec{w})}{\partial w_i}|_t < \epsilon$
\end{itemize}

\subsection{If $AA^T$ is not invertible}

$Rank(AA^T)$ = number of non-zero eigenvalues of $AA^T$ = number of non-zero singular values of A $\leq \min(n,p)$ since $A$ is $n\times p$

$$A=U \Sigma V^T \Rightarrow AA^T=U\Sigma^2U^T \Rightarrow AA^T U = U\Sigma^2$$

\subsubsection{Regularized Leasts Squares}

Ridge Regression (L2 penalty)

\begin{equation}
\begin{array}{rcl}
\hat{\vec{\beta}}_{MAP} & = & \arg\min_{\vec{\beta}}(A^T\vec{\beta}-\vec{Y})^T(A^T\vec{\beta}-\vec{Y}) +\lambda \vec{\beta}^T\vec{\beta}~~(\lambda \geq 0) \\
& = & (AA^T + \lambda I)^{-1} A\vec{Y}
\end{array}
\end{equation}

$(AA^T + \lambda I)$ is invertible if $\lambda > 0$. Proof:
\begin{itemize}
	\item the symmetric matrix $AA^T$ is positive-semidefinite matrix, because a matrix is positive-semidefinite iff it arises as the Gram matrix of some set of vectors\footnote{In contrast to the positive-definite case, these vectors need not be linearly independent.}.
	\item $\therefore \forall \lambda>0~and~\vec{x}\neq\vec{0}$,
	$$\vec{x}^T(AA^T)\vec{x} = (A^T\vec{x})^T(A^T\vec{x}) \geq 0$$
	$$\vec{x}^T(AA^T+\lambda I)\vec{x} = \vec{x}^T (AA^T) \vec{x} + \lambda \vec{x}^T\vec{x} >0$$
	\item $\therefore$ $(AA^T+\lambda I)$ is positive definite.
	\item $\therefore$ the eigenvalues of $B=(AA^T+\lambda I)$ are all positive. $$B\vec{v}=\lambda\vec{v} \Rightarrow \vec{v}^T B \vec{v} = \lambda >0$$
	\item $\therefore$ $(AA^T + \lambda I)$ is invertible if $\lambda > 0$
\end{itemize}

\subsubsection{Understanding Regularized Least Squared}

Why we need constraints: r equations, p unknowns - underdetermined system of linear equations.

$$\min_{\vec{\beta}} J(\beta)+\lambda pen(\vec{\lambda})$$
\begin{itemize}
	\item Ridge Regression: $pen(\beta)=||\beta||_2^2$
	\item Lasso Regression: $pen(\beta)=||\beta||_1$. No closed form solution, but can optimize using sub-gradient descent.
	\item $pen(\beta)=||\beta||_0=\sum 1_{\beta_i \neq 0}$
\end{itemize}

\begin{figure}[!h]
	\centering
	\includegraphics[width=10cm]{./img/ridgeregression.png}
	\caption{For Lasso regression, results are in sparse solution - vector with more zero coordinates. Good for high-dimenstional problems - don't have to store all coordinates, interpretable solution! }
\end{figure}

Matlab code:
\begin{lstlisting}[language=Matlab]
[B,FitInfo] = lasso(X,Y,Name,Value)
\end{lstlisting}
\begin{itemize}
	\item X: Numeric matrix with n rows and p columns. Each row represents one observation, and each column represents one predictor (variable).
	\item Y: Numeric vector of length n, where n is the number of rows of X. Y(i) is the response to row i of X.
	\item 'Alpha': Scalar value from 0 to 1 (excluding 0) representing the weight of lasso (L1) versus ridge (L2) optimization. Alpha = 1 represents lasso regression, Alpha close to 0 approaches ridge regression, and other values represent elastic net optimization. See Definitions.
	Default: 1
\end{itemize}

\subsection{Regularized Least Squares - Connection to MLE and MAP (Model-based Approaches)}

\subsubsection{Least Squares and M(C)LE (Maximum Conditional Likelihood Estimator)}

$$Y=f^*(X)+\epsilon=X\beta^*+\epsilon$$
$$\epsilon \sim \mathcal{N}(0,\sigma^2I)~~Y\sim\mathcal{N}(X\beta^*,\sigma^2I)$$
$$\hat{\beta}_{MLE} = \arg\max_\beta (\log p(\{Y_i\}|\beta,\sigma^2,\{X_i\}))=\arg\min_{\beta}\sum_i(X_i\beta-Y_i)^2$$
\begin{itemize}
	\item Model parameters: $\beta,\sigma^2$
	\item Conditional log likelihood: $\log p(\{Y_i\}|\beta,\sigma^2,\{X_i\})$
\end{itemize}

Least Square Estimator is same as Maximum Conditional Likelihood Estimator under a Gaussian model.

\subsubsection{Regularized Least Squares and M(C)AP (Maximum Conditional A Prior Estimator)}

If $AA^T$ is not invertible.

$$Y=f^*(X)+\epsilon=X\beta^*+\epsilon$$
$$\epsilon \sim \mathcal{N}(0,\sigma^2I)~~Y\sim\mathcal{N}(X\beta^*,\sigma^2I)$$
(1) Gaussian prior:
$$\beta \sim \mathcal{N}(0,\tau^2 I)~~p(\beta) \propto \exp(-\beta^T\beta/2\tau^2)$$
$$\hat{\beta}_{MAP} = \arg\max_\beta \log p(\{Y_i\}|\beta,\sigma^2,\{X_i\}) +\log p(\beta)=\arg\min_{\beta}\sum_i(X_i\beta-Y_i)^2+\lambda(\sigma^2,\tau^2)||\beta||_2^2$$
(2) Laplace prior:
$$\beta \sim Laplace(0,t)~~p(\beta_i) \propto \exp(-|\beta_i|/t)$$
$$\hat{\beta}_{MAP} = \arg\max_\beta \log p(\{Y_i\}|\beta,\sigma^2,\{X_i\}) +\log p(\beta)=\arg\min_{\beta}\sum_i(X_i\beta-Y_i)^2+\lambda(\sigma^2,\tau^2)||\beta||_1$$

\begin{itemize}
	\item Model parameters: $\beta,\sigma^2$
	\item Conditional log likelihood: $\log p(\{Y_i\}|\beta,\sigma^2,\{X_i\})$
	\item Log prior: $\log p(\beta)$
\end{itemize}

\section{Polynomial Regression}

\begin{itemize}
	\item Univariate: $f(X)=\sum{\beta_iX^i}=[1, X, X^2, \dots, X^m]^T\beta$
	$$\hat{\beta}=(AA^T)^{-1}AY~or~(AA^T+\lambda I)^{-1}AY$$
	\item Multivariate: $f(X) = \sum_i{\beta_i X^{(i)}} + \sum_{i,j}{\beta_{i,j} X^{(i)} X^{(j)}}+\sum_{i,j,k}{\beta_{i,j,k} X^{(i)} X^{(j)}X^{(k)}}+\dots$
\end{itemize}

\subsection{Bias - Vairance Tradeoff}
\begin{itemize}
	\item Large bias, small variance: poor approximation but robust/stable
	\item Small bias, large variance: good approximation but unstable
\end{itemize}

Bias-Variance Decomposition:
$$E[(f(X)-f^*(X))^2] = Bias^2 + Variance$$
\begin{itemize}
	\item $Bias = E[f(X)] - f^*(X)$: How far is the model from best model.
	\item $Variance = E[(f(X)-E[f(X)])^2]$: How variable is the model.
\end{itemize}

\begin{figure}[!h]
	\centering
	\includegraphics[width=8cm]{./img/testerror.png}
	\includegraphics[width=8cm]{./img/trainerror.png}
\end{figure}

\section{Regression with Basis Functions or Nonlinear Features}

$$f(X)=\sum_i \beta_i \phi_i(X)$$

\end{document}


\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{graphicx,color}
\usepackage{gensymb}

\title{\bf{Mid-Project Review: C++ implementation of the Metropolis-Hasting Algorithm}}
\author{Nicholas Malaya \\ University of Texas at Austin} \date{}

\begin{document}
\maketitle

\section{Introduction}

Bayesian Inference is a powerful statistical method that has found a
variety of applications in uncertainty quantification, decision theory,
model selection and many others. These methods have been applied to
diverse fields such as epidemiology, spam filters, computer vision, as
well as assessments of the reliability of the readyness of the United
States nuclear weapon stockpiles.

However, Bayes' Theorem, despite existing since the 18th century, has
only recently become more prevalent in scientific applications. The
recent dramatic growth in Bayesian methods coincides with the
exponential expansion of computational power. This is because for all
but a small set of models, the posterior distribution is unable to be
analytically determined, and must be statistically sampled, often
requiring far more samples than are humanly possible to generate.

For this project, we have developed a C++ implementation of the
Metropolis-Hastings algorithm. This is a Markov Chain Monte Carlo (MCMC)
method to sample from a probability distribution.  These samples will be
generated in order to estimate the posterior of a distribution for a
Bayesian Inference problem. We will generate results for a few different
models, in order to investigate the model against a known (analytic)
conjugate prior, as well as more complicated (non-tracible without
sampling) distributions.

The code is written in C++, and will be parallelized with openMP.
\section{Methodology}
%
% 3-4 page report on the progess of the project
%

Suppose we have a posterior that we want to sample from, but:
\begin{itemize}
 \item the posterior doesn't look like any distribution we know (no
       conjugate model exists)
 \item some (or all) of the full conditionals do not look like any
      distributions we know (no Gibbs sampling)
\end{itemize}
In these cases, it is necessary to sample the underlying (and often
quite complicted) distribution. The Metropolis-Hastings algorithm
accomplishes this.

\subsection{Model Document: The Metropolis Hastings Algorithm}

For each Metropolis step, we begin with a guess for the starting point,
$\phi_1$. Afterward, we begin sampling. Each step of this so-called,
``rejection sampling'' involves ``proposing'' a new value, which if it
meets a specified criterion, is accepted. The next step then begins.

Thus, we begin with a proposal step (in this case, for step two),
\begin{equation}
 q(\tilde \phi_2 | \phi_1) = N(\tilde \phi_2 | \phi_1, \nu^2).
\end{equation}
In other words, our proposal step is correlated with the previous step,
in that it is drawn from a Gaussian (Normal) distribution with mean of
the previous step, and standard deviation, $\nu$. We will discuss our
choice of $\nu$ more, later. In order to draw this proposal, we generate a
proposal from a standard normal distribution, Z, and then transform it according to,
\begin{equation}
 X = \phi_1 + \nu*Z.
\end{equation}

Thus, with our new proposal $\tilde \phi_2$ drawn, we must evaluate
whether to accept this or reject it. In order to accomplish this, we
next draw from a uniform distribution, $u \sim U(0,1)$.

Now, we accept the new $\tilde \phi_2$ if it meets the following
criterion,
\begin{equation}
u < \frac{\pi_n(\tilde \phi_2)}{\pi_n(\phi_1)}.
\end{equation}
If we accept, then $\tilde \phi_2$ becomes $\phi_2$, and we begin the
next step. If we reject, then we keep our present value, e.g. $\phi_2 =
\phi_1$.

Now as we proceed, you might ask, what dictates whether we accept or
reject? Well, after the ``burn-in'' period, where we move around looking
for the region where the distribution has non-zero probability, we
eventually start sampling the posterior. Here, we want to ``jump''
around in each guess just enough that we will capture the tails of the
distribution, but simultaneously, we don't want to jump so far that we
move away from the areas of the distribution that has most of the
probability associated with it. This is where our $\nu$ comes in. This
is the variance of the proposal. If it is very large, then we will tend
to make large jumps away from the mean for our proposal. These are great
for finding the distribution in the first place, and they will tend to
capture more of the tails, but simultaneously, too many large jumps will
result in more rejections by our sampling algorithm. Therefore, we need
to balance $\nu$ so that is jumps around, but not too far.

It turns out that mathematicians have shown that for various situations,
an, ``acceptance ratio'' of right around 40\% is just about right. This
acceptance ratio is simply the ratio of accepted proposals to total MCMC
steps we have taken. Therefore, we have tried to tune our $\nu$
parameter for each situation to have an acceptance ratio around 40\%.

\section{Report on Progress}

The metropolis algorithm as detailed above has now been implemented in a few hundred lines of C++.
Presently, we are sampling from a Gaussian distribution, in order to analytically verify that
our distribution matches a known posterior. A histogram from one thousand samples is provided in
figure one.
\begin{center}
 \includegraphics[width=3.5in]{figs/hist}
\end{center}

This matches quite well, and implies the basic kernel is functioning. Our next objective will be to
move toward a few other distributions, with an eye on generating samples for a prior and posterior set of
distributions that do not have an analytical solution.

The codebase for this project has been developed on bitbucket, with logs
 for each commit. A fully functioning build system (a makefile) alongside a 'make
 check' regression suite has been developed. At present, three
 regression tests are run, that perform simple checks on the
 distributions and sampling algorithm. The code has been tested on a local i7 quad core laptop, as well as TACC's
lonestar supercomputer.

Finally, a directory, ``postprocessing'' contains scripts used to
generate all plots. These are routines using python with numpy+scipy and
matplotlib.

\section{Future Work}

As mentioned in the previous section, the we will expand to a few other distributions, in addition to
sampling gaussians. This should not be a significant part of the work. These distributions will also be
verified against conjugate priors (analytically known solution distributions) when available.

The largest remaining deliverable is to add OpenMP directives to the c++
implementation. Afterward, a detailed scaling study will be
performed. Due to the nature of MCMC, each markov chain should be
completely independent, and the underlying algorithm is therefore
``embarassingly parallel''. As a result, and we expect the routines to
scale essentially perfectly. At least, we hope so!

\end{document}


%% LyX 2.0.3 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[twoside,english]{paper}
\usepackage{lmodern}
\renewcommand{\ttdefault}{lmodern}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=3cm,bmargin=2.5cm,lmargin=2cm,rmargin=2cm}
\usepackage{color}
\usepackage{babel}
\usepackage{float}
\usepackage{bm}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{esint}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 0},backref=false,colorlinks=false]
 {hyperref}
\usepackage{breakurl}
\usepackage{mathrsfs}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\numberwithin{equation}{section}
\numberwithin{figure}{section}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{babel}

\@ifundefined{showcaptionsetup}{}{%
 \PassOptionsToPackage{caption=false}{subfig}}
\usepackage{subfig}
\makeatother

\begin{document}

\section{Structure of the observable}

In this document we report the relevant formulas for the computation
of semi-inclusive deep-inelastic scattering (SIDIS) multiplicities
under the assumption that the (negative) virtuality of the $Q^2$ of
the exchanged vector boson is much smaller than the $Z$ mass. This
allows us to neglect weak contributions and write the multiplicity
as:
\begin{equation}
\frac{d\sigma}{dxdQ^2 dz d p_T} = \frac{2p_T\pi \alpha^2
  }{z^2 x Q^4}\left[1+(1-y)^2\right] H(Q,\mu) \sum_q e_q^2
  \int_0^\infty db \,b J_0\left(\frac{bp_T}{z}\right)\overline{F}_q(x,b;\mu,\zeta) \overline{D}_{q}(z,b;\mu,\zeta)\,,
\end{equation}
where:
\begin{equation}
\overline{F}_i(x,b;\mu,\zeta) =xF_i(x,b;\mu,\zeta) = R_q(\mu_0,\zeta_0\rightarrow \mu,\zeta;b) \sum_{j}\int_x^1dy\,\mathcal{C}_{ij}(y;\mu_0,\zeta_0)\left[\frac{x}{y}f_j\left(\frac{x}{y},\mu_0\right)\right]\,,
\end{equation}
and:
\begin{equation}
\overline{D}_{i}(z,b;\mu,\zeta) =z^3D_{i}(z,b;\mu,\zeta) = R_q(\mu_0,\zeta_0\rightarrow \mu,\zeta;b) \sum_{j}\int_z^1dy\,\left[y^2\mathbb{C}_{ij}(y;\mu_0,\zeta_0)\right]\left[\frac{z}{y}d_j\left(\frac{z}{y},\mu_0\right)\right]\,.
\end{equation}

\begin{thebibliography}{alp}

\end{thebibliography}

\end{document}


%!TEX root = ../../dissertation.tex

\section{New Keynesian Model} \label{app:NKModel}

In A1-A7, we provide a description of Bellman and Euler equation algorithms, which we use to solve
the neoclassical stochastic growth model with inelastic labor supply described in Section 2
including i) envelope condition method iterating on value function (ECM-VF); ii) conventional value
function interation (VFI); iii) endogenous grid method (EGM); iv) policy function iteration via
envelope condition (ECM-PI); v) conventional policy function iteration via FOC (PI); vi) envelope
condition method iterating on derivative of value function (ECM-DVF); vii) conventional Euler
equation method (EE).

\paragraph{A1: Envelope condition method (ECM).}

\qquad

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 1. ECM.} \\ \hline
\emph{Initialization.} \\
\quad a. Choose an approximating function $\widehat{V}(\cdot ;b)\approx V$.
\\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct grid $\Gamma =\{k_{m},z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $b^{(1)}$. \\ \hline
\emph{Iterative cycle. Computation of a solution.} \\ \hline
\quad At iteration $i$, perform the following steps: \\
\emph{Step 1. Computation of values of }$V$ \emph{on the grid.} \\
\quad For $m=1,...,M,$ \\
\quad a. Use $b^{\left( i\right) }$ to compute $\widehat{V}_{1}\left(
k_{m},z_{m};b^{\left( i\right) }\right) $. \\
\quad b. Compute the corresponding values of $c_{m}$ using \\
\quad $c_{m}=u^{\prime -1}\left[ \frac{\widehat{V}_{1}\left(
k_{m},z_{m};b^{\left( i\right) }\right) }{1-\delta +z_{m}f^{\prime }\left(
k_{m}\right) }\right] .$ \\
\quad c. Find $k_{m}^{\prime }$ using \\
\quad $k_{m}^{\prime }=(1-\delta )k_{m}+z_{m}f\left( k_{m}\right) -c_{m}$.
\\
\quad d. Find value function on the grid \\
\quad $\widehat{v}_{m}\equiv u\left( c_{m}\right) +\beta
\sum_{j=1}^{J}\omega _{j}\widehat{V}\left( k_{m}^{\prime },z_{m}^{\rho }\exp
\left( \varepsilon _{j}\right) ;b^{(i)}\right) .$ \\ \hline
\emph{Step 2. Computation of }$b$\emph{\ that fits the value function on the
grid. } \\
\quad Run a regression to find $\widehat{b}:$ \\
\quad $\widehat{b}=\arg \underset{b}{\min }\sum_{m=1}^{M}\Vert \widehat{v}
_{m}-\widehat{V}(k_{m},z_{m};b)\Vert $. \\ \hline
\emph{Step 3. Convergence check and fixed-point iteration.} \\
\quad a. Check for convergence for $i\geq 2$: end Step 2 if \\
\quad $\dfrac{1}{M}\sum_{m=1}^{M}\left \vert \dfrac{(k_{m}^{\prime
})^{\left( i\right) }-(k_{m}^{\prime })^{\left( i-1\right) }}{(k_{m}^{\prime
})^{\left( i-1\right) }}\right \vert <10^{-9}.$ \\
\quad b. Use damping with $\xi =1$ to compute $b^{\left( i+1\right) }=(1-\xi
)b^{\left( i\right) }+\xi \widehat{b}$. \\ \hline \hline
\end{tabular}
}

\newpage

\paragraph{A2: Value function iteration (VFI).}

\qquad

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 2. VFI.} \\ \hline
\emph{Initialization.} \\
\quad a. Choose an approximating function $\widehat{V}(\cdot ;b)\approx V$.
\\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct a grid $\Gamma =\{k_{m},z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $b^{(1)}$. \\ \hline
\emph{Iterative cycle. Computation of a solution.} \\ \hline
\quad At iteration $i$, perform the following steps: \\
\emph{Step 1. Computation of values of }$V$\emph{\ on the grid.} \\
\quad For $m=1,...,M$, \\
\quad a. Solve for $k_{m}^{\prime }$ satisfying \\
\quad $u^{\prime }\left( (1-\delta )k_{m}+z_{m}f\left( k_{m}\right)
-k_{m}^{\prime }\right) =\beta \sum_{j=1}^{J}\omega _{j}\widehat{V}
_{1}\left( k_{m}^{\prime },z_{m}^{\rho }\exp \left( \varepsilon _{j}\right)
;b^{(i)}\right) .$ \\
\quad b. Find $c_{m}$ satisfying \\
\quad $c_{m}=(1-\delta )k_{m}+z_{m}f\left( k_{m}\right) -k_{m}^{\prime }$.
\\
\quad c. Find value function on the grid \\
\quad $\widehat{v}_{m}\equiv u\left( c_{m}\right) +\beta
\sum_{j=1}^{J}\omega _{j}\widehat{V}\left( k_{m}^{\prime },z_{m}^{\rho }\exp
\left( \varepsilon _{j}\right) ;b^{(i)}\right) .$ \\ \hline
\emph{Step 2. Computation of }$b$\emph{\ that fits value function on the
grid. } \\
\quad Run a regression to find $\widehat{b}:$ \\
\quad $\widehat{b}=\arg \underset{b}{\min }\sum_{m=1}^{M}\Vert \widehat{v}
_{m}-\widehat{V}(k_{m},z_{m};b)\Vert $. \\ \hline
\emph{Step 3. Convergence check and fixed-point iteration.} \\
\quad a. Check for convergence for $i\geq 2$: end Step 2 if \\
\quad $\dfrac{1}{M}\sum_{m=1}^{M}\left \vert \dfrac{(k_{m}^{\prime
})^{\left( i\right) }-(k_{m}^{\prime })^{\left( i-1\right) }}{(k_{m}^{\prime
})^{\left( i-1\right) }}\right \vert <10^{-9}.$ \\
\quad b. Use damping with $\xi =1$ to compute $b^{\left( i+1\right) }=(1-\xi
)b^{\left( i\right) }+\xi \widehat{b}$. \\ \hline \hline
\end{tabular}
}

\newpage

\paragraph{A3: Endogenous grid method (EGM).}

\qquad

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 3. EGM.} \\ \hline
\emph{Initialization.} \\
\quad a. Choose an approximating function $\widehat{V}(\cdot ;b)\approx V$.
\\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct grid $\Gamma =\{k_{m}^{\prime },z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $b^{(1)}$. \\ \hline
\emph{Iterative cycle. Computation of a solution.} \\ \hline
\quad At iteration $i$, perform the following steps: \\
\emph{Step 1. Computation of values of }$V$\emph{\ on the grid.} \\
\quad For $m=1,...,M$, \\
\quad a. Compute $\widehat{W}\left( k_{m}^{\prime },z_{m};b^{(i)}\right)
\equiv \sum_{j=1}^{J}\omega _{j}\widehat{V}\left( k_{m}^{\prime
},z_{m}^{\rho }\exp \left( \varepsilon _{j}\right) ;b^{(i)}\right) $ \\
\quad and $\widehat{W}_{1}\left( k_{m}^{\prime },z_{m};b^{(i)}\right) \equiv
\sum_{j=1}^{J}\omega _{j}\widehat{V}_{1}\left( k_{m}^{\prime },z_{m}^{\rho
}\exp \left( \varepsilon _{j}\right) ;b^{(i)}\right) $. \\
\quad b. Find $c_{m}=u^{\prime -1}\left[ \beta \widehat{W}_{1}\left(
k_{m}^{\prime },z_{m};b^{(i)}\right) \right] $. \\
\quad c. Use a solver to find $k_{m}$ satisfying \\
\quad $(1-\delta )k_{m}+z_{m}f\left( k_{m}\right) =c_{m}+k_{m}^{\prime }$.
\\
\quad d. Find value function on the grid \\
\quad $\widehat{v}_{m}\equiv u(c_{m})+\beta \widehat{W}\left( k_{m}^{\prime
},z_{m};b^{(i)}\right) $. \\ \hline
\emph{Step 2. Computation of }$b$\emph{\ that fits value function on the
grid. } \\
\quad Run a regression to find $\widehat{b}:$ \\
\quad $\widehat{b}=\arg \underset{b}{\min }\sum_{m=1}^{M}\Vert \widehat{v}
_{m}-\widehat{V}(k_{m},z_{m};b)\Vert $. \\ \hline
\emph{Step 3. Convergence check and fixed-point iteration.} \\
\quad a. Check for convergence for $i\geq 2$: end Step 2 if \\
\quad $\dfrac{1}{M}\sum_{m=1}^{M}\left \vert \dfrac{(k_{m})^{\left( i\right)
}-(k_{m})^{\left( i-1\right) }}{(k_{m})^{\left( i-1\right) }}\right \vert
<10^{-9}.$ \\
\quad b. Use damping with $\xi =1$ to compute $b^{\left( i+1\right) }=(1-\xi
)b^{\left( i\right) }+\xi \widehat{b}$. \\ \hline \hline
\end{tabular}
}

\newpage

\paragraph{A4: Policy function iteration using envelope condition (PI-ECM)}

\qquad

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 4. PI-ECM.} \\ \hline
\emph{Initialization.} \\
\quad a. Choose an approximating function $\widehat{V}(\cdot ;b)\approx V$
and $\widehat{K}(\cdot ;v)\approx K$. \\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct grid $\Gamma =\{k_{m},z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $v^{(1)}$ and $b^{(1)}$. \\ \hline
\emph{Iterative cycle. Computation of a solution.} \\ \hline
\quad At iteration $i$, perform the following steps: \\
\emph{Step 1. Computation of values of }$V$ \emph{on the grid.} \\
\quad For $m=1,...,M,$ \\
\quad a. Use $v^{\left( i\right) }$ to compute $k_{m}^{\prime }=\widehat{K}
\left( k_{m},z_{m};v^{\left( i\right) }\right) $. \\
\quad b. Compute the corresponding values of $k_{m}^{\prime }$ using \\
\quad $c_{m}=(1-\delta )k_{m}+z_{m}f\left( k_{m}\right) -k_{m}^{\prime }.$
\\
\quad c. Iterate on $b^{\left( i\right) }$ to find an approximate solution to
\\
\quad $\widehat{V}\left( k_{m},z_{m};b^{(i)}\right) =u\left( c_{m}\right)
+\beta \sum_{j=1}^{J}\omega _{j}\widehat{V}\left( k_{m}^{\prime
},z_{m}^{\rho }\exp \left( \varepsilon _{j}\right) ;b^{(i)}\right) $. \\
\quad d. Find policy function on the grid \\
\quad $\widehat{k}_{m}^{\prime }=(1-\delta )k_{m}+z_{m}f\left( k_{m}\right)
-u^{\prime -1}\left[ \frac{\widehat{V}_{1}\left( k_{m},z_{m};b^{\left(
i\right) }\right) }{1-\delta +z_{m}f^{\prime }\left( k_{m}\right) }\right] .$
\\ \hline
\emph{Step 2. Computation of }$v$\emph{\ that fits the value function on the
grid. } \\
\quad Run a regression to find $\widehat{v}:$ \\
\quad $\widehat{v}=\arg \underset{v}{\min }\sum_{m=1}^{M}\Vert \widehat{k}
_{m}^{\prime }-\widehat{K}(k_{m},z_{m};v)\Vert $. \\ \hline
\emph{Step 3. Convergence check and fixed-point iteration.} \\
\quad a. Check for convergence for $i\geq 2$: end Step 2 if \\
\quad $\dfrac{1}{M}\sum_{m=1}^{M}\left \vert \dfrac{(k_{m}^{\prime
})^{\left( i\right) }-(k_{m}^{\prime })^{\left( i-1\right) }}{(k_{m}^{\prime
})^{\left( i-1\right) }}\right \vert <10^{-9}.$ \\
\quad b. Use damping with $\xi =1$ to compute $v^{\left( i+1\right) }=(1-\xi
)v^{\left( i\right) }+\xi \widehat{v}$. \\ \hline \hline
\end{tabular}
}

\newpage

\paragraph{A5: Policy function iteration using first-order condition (PI-FOC)
}

\qquad

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 5. PI-FOC.} \\ \hline
\emph{Initialization.} \\
\quad a. Choose an approximating function $\widehat{V}(\cdot ;b)\approx V$
and $\widehat{K}(\cdot ;v)\approx K$. \\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct grid $\Gamma =\{k_{m},z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $v^{(1)}$ and $b^{(1)}$. \\ \hline
\emph{Iterative cycle. Computation of a solution.} \\ \hline
\quad At iteration $i$, perform the following steps: \\
\emph{Step 1. Computation of values of }$V$ \emph{on the grid.} \\
\quad For $m=1,...,M,$ \\
\quad a. Use $v^{\left( i\right) }$ to compute $k_{m}^{\prime }=\widehat{K}
\left( k_{m},z_{m};v^{\left( i\right) }\right) $. \\
\quad b. Compute the corresponding values of $k_{m}^{\prime }$ using \\
\quad $c_{m}=(1-\delta )k_{m}+z_{m}f\left( k_{m}\right) -k_{m}^{\prime }.$
\\
\quad c. Iterate on $b^{\left( i\right) }$ to find an approximate solution to
\\
\quad $\widehat{V}\left( k_{m},z_{m};b^{(i)}\right) =u\left( c_{m}\right)
+\beta \sum_{j=1}^{J}\omega _{j}\widehat{V}\left( k_{m}^{\prime
},z_{m}^{\rho }\exp \left( \varepsilon _{j}\right) ;b^{(i)}\right) $. \\
\quad d. Find policy function on the grid \\
$u^{\prime }\left( (1-\delta )k_{m}+z_{m}f\left( k_{m}\right) -\widehat{k}
_{m}^{\prime }\right) =\beta \sum_{j=1}^{J}\omega _{j}\widehat{V_{1}}\left(
\widehat{k}_{m}^{\prime },z_{m}^{\rho }\exp \left( \varepsilon _{j}\right)
;b^{(i)}\right) .$ \\ \hline
\emph{Step 2. Computation of }$v$\emph{\ that fits the value function on the
grid. } \\
\quad Run a regression to find $\widehat{v}:$ \\
\quad $\widehat{v}=\arg \underset{v}{\min }\sum_{m=1}^{M}\Vert \widehat{k}
_{m}^{\prime }-\widehat{K}(k_{m},z_{m};v)\Vert $. \\ \hline
\emph{Step 3. Convergence check and fixed-point iteration.} \\
\quad a. Check for convergence for $i\geq 2$: end Step 2 if \\
\quad $\dfrac{1}{M}\sum_{m=1}^{M}\left \vert \dfrac{(k_{m}^{\prime
})^{\left( i\right) }-(k_{m}^{\prime })^{\left( i-1\right) }}{(k_{m}^{\prime
})^{\left( i-1\right) }}\right \vert <10^{-9}.$ \\
\quad b. Use damping with $\xi =1$ to compute $v^{\left( i+1\right) }=(1-\xi
)v^{\left( i\right) }+\xi \widehat{v}$. \\ \hline \hline
\end{tabular}
}

\newpage

\paragraph{A6: Euler equation algorithm parameterizing $Q$.}

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 6. Euler equation algorithm parameterizing }$V_{1}.$ \\
\hline
\emph{Initialization.} \\
\quad a. Choose an approximating function $\widehat{V}_{1}(\cdot ;b)\approx
V_{1}$. \\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct grid $\Gamma =\{k_{m},z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $b^{(1)}$. \\ \hline
\emph{Iterative cycle. Computation of a solution.} \\ \hline
\quad At iteration $i$, perform the following steps: \\
\emph{Step 1. Computation of values of }$V_{1}$ \emph{on the grid.} \\
\quad For $m=1,...,M,$ \\
\quad a. Use $b^{\left( i\right) }$ to compute $\widehat{V}_{1}\left(
k_{m},z_{m};b^{\left( i\right) }\right) $. \\
\quad b. Find $k_{m}^{\prime }$ using \\
\quad $k_{m}^{\prime }=\left( 1-\delta \right) k_{m}+z_{m}f\left(
k_{m}\right) -u^{\prime -1}\left( \frac{\widehat{V}_{1}\left(
k_{m},z_{m};b^{\left( i\right) }\right) }{1-\delta +z_{m}f\left(
k_{m}\right) }\right) $ \\
\quad c. Find the values of $v_{m}$ on the grid \\
\quad $\widehat{v}_{1,m}\equiv \beta \left[ 1-\delta +zf^{\prime }\left(
k_{m}\right) \right] \sum_{j=1}^{J}\omega _{j}\widehat{V}_{1}\left(
k_{m}^{\prime },z_{m}^{\rho }\exp \left( \varepsilon _{j}\right)
;b^{(i)}\right) $. \\ \hline
\emph{Step 2. Computation of }$b$\emph{\ that fits the V}$_{1}$\emph{\
function on the grid. } \\
\quad Run a regression to find $\widehat{b}:$ \\
\quad $\widehat{b}=\arg \underset{b}{\min }\sum_{m=1}^{M}\Vert \widehat{v}
_{1,m}-\widehat{V}_{1}(k_{m},z_{m};b)\Vert $. \\ \hline
\emph{Step 3. Convergence check and fixed-point iteration.} \\
\quad a. Check for convergence for $i\geq 2$: end Step 2 if \\
\quad $\dfrac{1}{M}\sum_{m=1}^{M}\left \vert \dfrac{(k_{m}^{\prime
})^{\left( i\right) }-(k_{m}^{\prime })^{\left( i-1\right) }}{(k_{m}^{\prime
})^{\left( i-1\right) }}\right \vert <10^{-9}.$ \\
\quad b. Use damping with $\xi =1$ to compute $b^{\left( i+1\right) }=(1-\xi
)b^{\left( i\right) }+\xi \widehat{b}$. \\ \hline \hline
\end{tabular}
}

\qquad \newpage

\paragraph{A7. Euler equation algorithm parameterizing $K$}

\qquad

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 7. Euler equation algorithm parameterizing } $K$\textbf{.}
\\ \hline
\emph{Initialization.} \\
\quad a. Choose approximating functions $\widehat{K}(\cdot ;v)\approx K$. \\
\quad b. Choose integration nodes, $\varepsilon _{j}$, and weights, $\omega
_{j}$, $j=1,...,J$. \\
\quad c. Construct grid $\Gamma =\{k_{m},z_{m}\}_{m=1}^{M}$. \\
\quad d. Make an initial guess on $v^{(1)}$. \\ \hline
\emph{Step 1. Computation of values of }$\widehat{k}^{\prime }$ \emph{on the
grid}. \\
\quad For $m=1,...,M$, \\
\quad a. Use $v^{\left( i\right) }$ to compute $\widehat{K}\left(
k_{m},z_{m};v^{\left( i\right) }\right) $ and \\
\quad $k_{m,j}^{\prime \prime }=\widehat{K}\left( \widehat{K}\left(
k_{m},z_{m};v^{\left( i\right) }\right) ,z_{m}^{\rho }\exp \left( \epsilon
_{j}\right) ;v^{\left( i\right) }\right) $, $j=1,...,J$. \\
\quad b. Find $c_{m,j}^{\prime }$ using \\
\quad $c_{m,j}^{\prime }=(1-\delta )\widehat{K}\left( k_{m},z_{m};v^{\left(
i\right) }\right) +z_{m}f\left( \widehat{K}\left( k_{m},z_{m};v^{\left(
i\right) }\right) \right) -k_{m,j}^{\prime \prime }$. \\
\quad c. Find the values of $c_{m}$ on the grid \\
\quad $u^{\prime }\left( c_{m}\right) =\beta \sum_{j=1}^{J}\omega
_{j}u^{\prime }\left( c_{m,j}^{\prime }\right) \left[ 1-\delta +z_{m}^{\rho
}\exp \left( \epsilon _{j}\right) f^{\prime }\left( \widehat{K}\left(
k_{m},z_{m};v^{\left( i\right) }\right) \right) \right] .$ \\
\quad d. Find the values of $k_{m}^{\prime }$ on the grid \\
\quad $\widehat{k}_{m}^{\prime }=(1-\delta )k_{m}+z_{m}f\left( k_{m}\right)
-c_{m}$. \\ \hline
\emph{Step 2. Computation of }$v$\emph{\ that fits the capital function on
the grid. } \\
\quad a. Run a regression to find $\widehat{v}:$ \\
\quad $\widehat{v}=\arg \underset{v}{\min }\sum_{m=1}^{M}\Vert \widehat{k}
_{m}^{\prime }-\widehat{K}\left( k_{m},z_{m};v^{\left( i\right) }\right)
\Vert $. \\
\quad b. Use damping with $\xi =1$ to compute $v^{\left( i+1\right) }=(1-\xi
)v^{\left( i\right) }+\xi \widehat{v}$. \\ \hline \hline
\end{tabular}
}

\qquad \newpage

\begin{center}
\label{APNK}{\Large Appendix B: An Euler equation algorithm for solving a
new Keynesian model}
\end{center}

We consider a new Keynesian model studied in Maliar and Maliar (2015). This is
a stylized new Keynesian model with Calvo-type price frictions and a Taylor
(1993) rule.

\paragraph{The model.}

The economy is populated by households, final-good firms, intermediate-good
firms, monetary authority and government.

\textit{Households. }The representative household solves

\begin{gather}
\underset{\left \{ C_{t},L_{t},B_{t}\right \} _{t=0,...,\infty }}{\max }E_{0}
\overset{\infty }{\underset{t=0}{\sum }}\beta ^{t}\exp \left( \eta
_{u,t}\right) \left[ \frac{C_{t}^{1-\gamma }-1}{1-\gamma }-\exp \left( \eta
_{L,t}\right) \frac{L_{t}^{1+\vartheta }-1}{1+\vartheta }\right]  \label{uNK}
\\
\text{s.t. }P_{t}C_{t}+\frac{B_{t}}{\exp \left( \eta _{B,t}\right) R_{t}}
+T_{t}=B_{t-1}+W_{t}L_{t}+\Pi _{t},  \label{bcNK}
\end{gather}

where $\left( B_{0},\eta _{u,0},\eta _{L,0},\eta _{B,0}\right) $ is given; $
C_{t}$, $L_{t}$, and $B_{t}$ are consumption, labor and nominal bond holdings,
respectively; $P_{t}$, $W_{t}$ and $R_{t}$ are the commodity price, nominal
wage and (gross) nominal interest rate, respectively; $\eta _{u,t}$ and $\eta
_{L,t}$ are exogenous preference shocks to the overall momentary utility and
disutility of labor, respectively; $\eta _{B,t}$ is an exogenous premium in the
return to bonds; $T_{t}$ is lump-sum taxes; $\Pi _{t}$ is the profit of
intermediate-good firms; $\beta \in \left( 0,1\right) $ is the discount factor;
$\gamma >0$ and $\vartheta >0$ are the utility-function parameters. The
processes for shocks are

\begin{gather}
\eta _{u,t+1}=\rho _{u}\eta _{u,t}+\epsilon _{u,t+1},\qquad \epsilon
_{u,t+1}\sim \mathcal{N}\left( 0,\sigma _{u}^{2}\right) ,  \label{nuu} \\
\eta _{L,t+1}=\rho _{L}\eta _{L,t}+\epsilon _{L,t+1},\qquad \epsilon
_{L,t+1}\sim \mathcal{N}\left( 0,\sigma _{L}^{2}\right) ,  \label{nuL} \\
\eta _{B,t+1}=\rho _{B}\eta _{B,t}+\epsilon _{B,t+1},\qquad \epsilon
_{B,t+1}\sim \mathcal{N}\left( 0,\sigma _{B}^{2}\right) ,  \label{nuB}
\end{gather}

where $\rho _{u}$, $\rho _{L}$, $\rho _{B}$ are the autocorrelation
coefficients, and $\sigma _{u}$, $\sigma _{L}$, $\sigma _{B}$ are the standard
deviations of disturbances.

\textit{Final-good firms. }Perfectly competitive final-good firms produce final
goods using intermediate goods. A final-good firm buys $Y_{t}\left( i\right) $
of an intermediate good $i\in \left[ 0,1\right] $ at price $P_{t}\left(
i\right) $ and sells $Y_{t}$ of the final good at price $P_{t}$ in a perfectly
competitive market. The profit-maximization problem is

\begin{gather}
\underset{Y_{t}\left( i\right) }{\max }\quad
P_{t}Y_{t}-\int_{0}^{1}P_{t}\left( i\right) Y_{t}\left( i\right) di
\label{maxfin} \\
\text{s.t. }Y_{t}=\left( \int_{0}^{1}Y_{t}\left( i\right) ^{\frac{
\varepsilon -1}{\varepsilon }}di\right) ^{\frac{\varepsilon }{\varepsilon -1}
},  \label{bcfin}
\end{gather}

where (\ref{bcfin}) is a Dixit-Stiglitz aggregator function with $\varepsilon
\geq 1$.

\textit{Intermediate-good firms. }Monopolistic intermediate-good firms produce
intermediate goods using labor and are subject to sticky prices. The firm $i$
produces the intermediate good $i$. To choose labor in each period $ t$, the
firm $i$ minimizes the nominal total cost, TC (net of government subsidy $v$),

\begin{gather}
\underset{L_{t}\left( i\right) }{\min }\quad \text{TC}\left( Y_{t}\left(
i\right) \right) =\left( 1-v\right) W_{t}L_{t}\left( i\right)  \label{tc} \\
\text{s.t. }Y_{t}\left( i\right) =\exp \left( \eta _{a,t}\right) L_{t}\left(
i\right) ,  \label{tech} \\
\eta _{a,t+1}=\rho _{a}\eta _{a,t}+\epsilon _{a,t+1},\qquad \epsilon
_{a,t+1}\sim \mathcal{N}\left( 0,\sigma _{a}^{2}\right) ,  \label{nua}
\end{gather}

where $L_{t}\left( i\right) $ is the labor input; $\exp \left( \eta
_{a,t}\right) $ is the productivity level; $\rho _{a}$ is the autocorrelation
coefficient; and $\sigma _{a}$ is the standard deviation of the disturbance.
The firms are subject to Calvo-type price setting: a fraction $1-\theta $ of
the firms sets prices optimally, $P_{t}\left( i\right) =\widetilde{P}_{t}$, for
$i\in \left[ 0,1\right] $, and the fraction $\theta $ is not allowed to change
the price and maintains the same price as in the previous period, $P_{t}\left(
i\right) =P_{t-1}\left( i\right) $, for $i\in \left[ 0,1\right] $. A
reoptimizing firm $i\in \left[ 0,1\right] $ maximizes the current value of the
profit over the time when $ \widetilde{P}_{t}$ remains effective,

\begin{gather}
\underset{\widetilde{P}_{t}}{\max }\quad \underset{j=0}{\overset{\infty }{
\mathop{\displaystyle \sum } }}\beta ^{j}\theta ^{j}E_{t}\left \{ \Lambda
_{t+j}\left[ \widetilde{P}_{t}Y_{t+j}\left( i\right) -P_{t+j}\text{mc}
_{t+j}Y_{t+j}\left( i\right) \right] \right \}  \label{maxmon} \\
\text{s.t. }Y_{t}\left( i\right) =Y_{t}\left( \frac{P_{t}\left( i\right) }{
P_{t}}\right) ^{-\varepsilon },  \label{bcmon}
\end{gather}

where (\ref{bcmon}) is the demand for an intermediate good $i$ following from
(\ref{maxfin}), (\ref{bcfin}); $\Lambda _{t+j}$ is the Lagrange multiplier on
the household's budget constraint (\ref{bcNK}); mc$_{t+j}$ is the real marginal
cost of output at time $t+j$ (which is identical across the firms).

\textit{Government. }Government finances a stochastic stream of public
consumption by levying lump-sum taxes and by issuing nominal debt. The
government budget constraint is

\begin{equation}
T_{t}+\frac{B_{t}}{\exp \left( \eta _{B,t}\right) R_{t}}=P_{t}\frac{
\overline{G}Y_{t}}{\exp \left( \eta _{G,t}\right) }+B_{t-1}+vW_{t}L_{t},
\label{Gbc}
\end{equation}

where $\frac{\overline{G}Y_{t}}{\exp \left( \eta _{G,t}\right) }=G_{t}$ is
government spending, $vW_{t}L_{t}$ is the subsidy to the intermediate-good
firms, and $\eta _{G,t}$ is a government-spending shock,

\begin{equation}
\eta _{G,t+1}=\rho _{G}\eta _{G,t}+\epsilon _{G,t+1},\qquad \epsilon
_{G,t+1}\sim \mathcal{N}\left( 0,\sigma _{G}^{2}\right) ,  \label{nuG}
\end{equation}

where $\rho _{R}$ is the autocorrelation coefficient, and $\sigma _{R}$ is the
standard deviation of disturbance.

\textit{Monetary authority. }The monetary authority follows a Taylor rule with
a zero lower bound (ZLB) on the nominal interest rate:

\begin{equation}
R_{t}=\max \left \{ R_{\ast }\left( \frac{R_{t-1}}{R_{\ast }}\right) ^{\mu }
\left[ \left( \frac{\pi _{t}}{\pi _{\ast }}\right) ^{\phi _{\pi }}\left(
\frac{Y_{t}}{Y_{N,t}}\right) ^{\phi _{y}}\right] ^{1-\mu }\exp \left( \eta
_{R,t}\right) ,1\right \} ,  \label{Tr2}
\end{equation}

where $R_{\ast }$ is the long-run value of the gross nominal interest rate; $
\pi _{\ast }$ is the target inflation; $Y_{N,t}$ is the natural level of
output; and $\eta _{R,t}$ is a monetary shock,

\begin{equation}
\eta _{R,t+1}=\rho _{R}\eta _{R,t}+\epsilon _{R,t+1},\qquad \epsilon
_{R,t+1}\sim \mathcal{N}\left( 0,\sigma _{R}^{2}\right) ,  \label{nuR}
\end{equation}

where $\rho _{R}$ is the autocorrelation coefficient, and $\sigma _{R}$ is the
standard deviation of disturbance.

\textit{Natural level of output. }The natural level of output $Y_{N,t}$ is the
level of output in an otherwise identical economy but without distortions. It
is a solution to the following planner's problem

\begin{gather}
\underset{\left \{ C_{t},L_{t}\right \} _{t=0,...,\infty }}{\max }E_{0}
\overset{\infty }{\underset{t=0}{\sum }}\beta ^{t}\exp \left( \eta
_{u,t}\right) \left[ \frac{C_{t}^{1-\gamma }-1}{1-\gamma }-\exp \left( \eta
_{L,t}\right) \frac{L_{t}^{1+\vartheta }-1}{1+\vartheta }\right]  \label{Pob}
\\
\text{s.t. }C_{t}=\exp \left( \eta _{a,t}\right) L_{t}-G_{t},  \label{Pbc}
\end{gather}

where $G_{t}=\frac{\overline{G}Y_{t}}{\exp \left( \eta _{G,t}\right) }$ is
given, and $\eta _{u,t+1}$, $\eta _{L,t+1}$, $\eta _{a,t+1}$, and $\eta _{G,t}$
follow the processes (\ref{nuu}), (\ref{nuL}), (\ref{nua}), and (\ref {nuG}),
respectively.

\paragraph{Parameterization and implementation details}

Most of the parameters are calibrated using the estimates of Del Negro et al.
(2007, Table 1, column "DSGE posterior"); namely, we assume $\gamma =1$ and
$\vartheta =2.09$ in the utility function (\ref{uNK}); $\phi _{y}=0.07$, $\phi
_{\pi }=2.21$, and $\mu =0.82$ in the Taylor rule (\ref{Tr2}); $ \varepsilon
=4.45$ in the production function of the final-good firm (\ref {bcfin});
$\theta =0.83$ (the fraction of the intermediate-good firms affected by price
stickiness); $\overline{G}=0.23$ in the government budget constraint
(\ref{Gbc}); and $\rho _{u}=0.92$, $\rho _{G}=0.95$, $\rho _{L}=0.25$, $\sigma
_{u}=0.54\%$, $\sigma _{G}=0.38\%$, $\sigma _{L}=18.21\%$ (the latter is a
lower estimate of Del Negro et al., 2007, Table 1, column "DSGE posterior"),
and $\sigma _{L}=40.54\%$ (an average estimate of Del Negro et al., 2007) in
the processes for shocks (\ref{nuu}), (\ref{nuG}) and (\ref{nuL}). From Smets
and Wouters (2007), we take the values of $\rho _{a}=0.95$, $\rho _{B}=0.22$,
$\rho _{R}=0.15$, $\sigma _{a}=0.45\%$, $ \sigma _{B}=0.23\%$ and $\sigma
_{R}=0.28\%$ in the processes for shocks ( \ref{nua}), (\ref{nuB}) and
(\ref{nuR}). We set the discount factor at $ \beta =0.99$. To parameterize the
Taylor rule (\ref{Tr2}), we use the steady-state interest rate $R_{\ast
}=\frac{\pi _{\ast }}{\beta }$, and we consider two alternative values of the
target inflation, $\pi _{\ast }=1$ (a zero net inflation target) and $\pi
_{\ast }=1.0598$ (this estimate comes from Del Negro et al., 2007).

To approximate the equilibrium policy rules, we use a family of ordinary
polynomials. To compute conditional expectations in Euler equations (\ref
{NK1}), (\ref{NK2}) and (\ref{NK5}), we use a monomial integration rule (either
a formula with $2N$ nodes or the one with $2N^{2}+1$ nodes); see Judd, Maliar
and Maliar (Quantitative Economics, 2011) for a detailed description of the
monomial integration formulas. To compute a solution, we use two alternative
grids: one is a random grid composed of uniformely distributed points (i.e., we
make independent random draws for all variables in the grid within the given
range), and the other is a quasi-Monte Carlo grid (namely, a Sobol grid that
fills in a given multidimensional hypercube). In Step 2b, the damping parameter
is set at $\xi =0.1$, and the convergence parameter is set at $\varpi
=10^{-7}$. The solution algorithm is described in Appendix.

\paragraph{Computational method}

In Appendix B, we describe an Euler equation algorithm, which we use to solve
the new Keynesian model described in Section 3.

{\small
\begin{tabular}{l}
\hline \hline
\textbf{An algorithm iterating on the Euler equation} \\ \hline
\textit{Step 1. Initialization.} \\ \hline
{\quad a. Choose }$\left( {\Delta }_{{-1}}{,R}_{{-1}}{,\eta }_{{u,0}}{,\eta }
_{{L,0}}{,\eta }_{{B,0}}{,\eta }_{{a,0}}{,\eta }_{{R,0}}{,\eta }_{{G,0}
}\right) ${\ and }${T}${.} \\
{\quad b. Draw }$\left \{ {\epsilon }_{{u,t+1}}{,\epsilon }_{{L,t+1}}{
,\epsilon }_{{B,t+1}}{,\epsilon }_{{a,t+1}}{,\epsilon }_{{R,t+1}}{,\epsilon }
_{{G,t+1}}\right \} _{{t=0,...,T-1}}${. } \\
{\quad \quad Compute and fix }$\left \{ {\eta }_{{u,t+1}}{,\eta }_{{L,t+1}}{
,\eta }_{{B,t+1}}{,\eta }_{{a,t+1}}{,\eta }_{{R,t+1}}{,\eta }_{{G,t+1}
}\right \} _{{t=0,...,T-1}}${.} \\
{\quad c. Choose approximating functions }${S\approx }\widehat{{S}}\left( {
\cdot ;b}^{{S}}\right) ${, }${F\approx }\widehat{{F}}\left( {\cdot ;b}^{{F}
}\right) ${, MU}${\approx }\widehat{\text{{MU}}}\left( {\cdot ;b}^{\text{{MU}
}}\right) ${.} \\
{\quad d. Make initial guesses on }${b}^{{S}}${, }${b}^{{F}}${, }${b}^{\text{
{MU}}}${. } \\
{\quad e. Choose integration nodes, }$\left \{ {\epsilon }_{{u,j}}{,\epsilon
}_{{L,j}}{,\epsilon }_{{B,j}}{,\epsilon }_{a,j}{,\epsilon }_{{R,j}}{
,\epsilon }_{{G,j}}\right \} _{{j=1,...,J}}${\ and weights, }$\left \{ {
\omega }_{{j}}\right \} _{{j=1,...,J}}${.} \\ \hline
{\quad f. Constuct a grid }${\Gamma }=\left \{ {\Delta }_{{m}}{,R}_{{m}}{
,\eta }_{{u,m}}{,\eta }_{{L,m}}{,\eta }_{{B,m}}{,\eta }_{{a,m}}{,\eta }_{{R,m
}}{,\eta }_{{G,m}}\right \} _{{m=1,...,M}}\equiv \left \{ {x}_{{m}}\right \}
_{{m=1,...,M}}${\ } \\ \hline
\textit{Step 2. Computation of a solution for }$S$\textit{, }$F$\textit{, MU.
} \\ \hline
{\quad a. At iteration }${i}${, for }${m=1,...,M}${, compute} \\
\quad -- ${S}_{{m}}=\widehat{{S}}\left( {x}_{{m}}{;b}^{{S}}\right) ${, }${F}
_{{m}}=\widehat{{F}}\left( {x}_{{m}}{;b}^{{F}}\right) ${, }${C}_{{m}}=\left[
\widehat{\text{{MU}}}\left( {x}_{{m}}{;b}^{\text{{MU}}}\right) \right] ^{{
-1/\gamma }}${;} \\
\quad -- ${\pi }_{{m}}$ {from} $\frac{{S}_{m}}{{F}_{m}}{=}\left[ \frac{{
1-\theta \pi }_{m}^{\varepsilon -1}}{{1-\theta }}\right] ^{\frac{1}{
1-\varepsilon }}${\ and }${\Delta }_{{m}}^{\prime }{=}\left[ \left( {
1-\theta }\right) \left[ \frac{{1-\theta \pi }_{m}^{\varepsilon -1}}{{
1-\theta }}\right] ^{\frac{\varepsilon }{\varepsilon -1}}{+\theta }\frac{{
\pi }_{m}^{\varepsilon }}{{\Delta }_{m}}\right] ^{{-1}}${;} \\
\quad -- ${Y}_{{m}}=\left( {1}-\frac{\overline{G}}{\exp \left( \eta
_{G,m}\right) }\right) ^{{-1}}{C}_{{m}}${, and }${L}_{{m}}={Y}_{{m}}\left[ {
\exp }\left( {\eta }_{{a,m}}\right) {\Delta }_{{m}}^{{\prime }}\right] ^{-1}$
{;} \\
\quad --{\ }${Y}_{{N},{m}}=\left[ \frac{{\exp }\left( {\eta }_{a,m}\right)
^{1+\vartheta }}{\left[ {\exp }\left( {\eta }_{{G,m}}\right) \right]
^{-\gamma }{\exp }\left( {\eta }_{{L,m}}\right) }\right] ^{\frac{1}{
\vartheta +\gamma }}${;} \\
{\quad }--{\ }${R}_{{m}}^{{\prime }}={\max }\left \{ {1,}\text{ }{\Phi }_{{m}
}\right \} ${, }${\Phi }_{{m}}{=R}_{\ast }\left( \frac{R_{m}}{R_{\ast }}
\right) ^{{\mu }}\left[ \left( \frac{\pi _{m}}{\pi _{\ast }}\right) ^{{\phi }
_{\pi }}\left( \frac{Y_{m}}{Y_{N,m}}\right) ^{{\phi }_{y}}\right] ^{{1-\mu }}
{\exp }\left( {\eta }_{{R,m}}\right) ${;} \\
{\quad }--{\ }${x}_{{m,j}}^{{\prime }}=\left( \Delta _{{m}}^{{\prime }},{R}_{
{m}}^{{\prime }},{\eta }_{{u,m,j}}^{{\prime }},\eta _{{L,m,j}}^{{\prime }
},\eta _{{B,m,j}}^{{\prime }},\eta _{{a,m,j}}^{{\prime }},\eta _{{R,m,j}}^{{
\prime }},\eta _{{G,m,j}}^{{\prime }}\right) ${\ for all }${j}${;} \\
{\quad }--{\ }${S}_{{m,j}}^{{\prime }}{=}\widehat{{S}}\left( {x}_{{m,j}}^{{
\prime }}{;b}^{{S}}\right) ${, }${F}_{{m,j}}^{{\prime }}=\widehat{{F}}\left(
{x}_{{m,j}}^{{\prime }}{;b}^{{F}}\right) ${, }${C}_{{m,j}}^{{\prime }}{=}
\left[ \widehat{\text{{MU}}}\left( {x}_{{m,j}}^{{\prime }}{;b}^{\text{{MU}}
}\right) \right] ^{{-1/\gamma }}${;} \\
{\quad }-- ${\pi }_{{m,j}}^{{\prime }}${\ from }$\frac{{S}_{m,j}^{\prime }}{{
F}_{m,j}^{\prime }}{=}\left[ \frac{{1-\theta }\left( \pi _{m,j}^{^{\prime
}}\right) ^{\varepsilon -1}}{{1-\theta }}\right] ^{\frac{{1}}{{1-\varepsilon
}}}${;} \\
\quad -- $\widehat{{S}}_{{m}}{=}\frac{{\exp }\left( {\eta }_{{u,m}}{+\eta }_{
{L,m}}\right) }{{\exp }\left( {\eta }_{{a,m}}\right) }{L}_{{m}}^{{\vartheta }
}{Y}_{{m}}{+\beta \theta }\underset{{j=1}}{\overset{{J}}{\sum }}{\omega }_{{j
}}{\cdot }\left \{ \left( {\pi }_{{m,j}}^{{\prime }}\right) ^{{\varepsilon }}
{S}_{{m,j}}^{{\prime }}\right \} ,$ \\
\quad -- $\widehat{{F}}_{{m}}={\exp }\left( {\eta }_{{u,m}}\right) {C}_{{m}
}^{{-\gamma }}{Y}_{{m}}{+\beta \theta }\underset{{j=1}}{\overset{{J}}{\sum }}
{\omega }_{{j}}{\cdot }\left \{ \left( {\pi }_{{m,j}}^{^{\prime }}\right) ^{{
\varepsilon -1}}{F}_{{m,j}}^{{\prime }}\right \} {,}$ \\
\quad -- $\widehat{{C}}_{{m}}^{{-\gamma }}{=}\frac{{\beta \exp }\left( \eta
_{{B,m}}\right) {R}_{{m}}^{{\prime }}}{{\exp }\left( {\eta }_{{u,m}}\right) }
\underset{{j=1}}{\overset{{J}}{\sum }}{\omega }_{{j}}\cdot \left[ \frac{
\left( {C}_{{m,j}}^{\prime }\right) ^{{-\gamma }}{\exp }\left( {\eta }_{{
u,m,j}}^{{\prime }}\right) }{{\pi }_{{m,j}}^{{\prime }}}\right] .$ \\
\quad {b. Find }${b}^{{S}}$, ${b}^{{F}}$, ${b}^{\text{{MU}}}$ {that solve
the system in Step 2a}. \\
\quad -- {Get: }$\widehat{{b}}^{{S}}\equiv {\arg }\underset{{b}^{{S}}}{{\min
}}\sum_{{m=1}}^{{M}}\left \Vert \widehat{{S}}_{{m}}-\widehat{{S}}\left( {x}_{
{m}};{b}^{{S}}\right) \right \Vert ${. Similarly, get }$\widehat{{b}}^{{F}}${
\ and }$\widehat{{b}}^{\text{{MU}}}${.} \\
\quad -- {Use damping to compute }${b}^{\left( {i+1}\right) }=\left( {1-\xi }
\right) {b}^{\left( {i}\right) }{+\xi }\widehat{{b}} ${, where }${b}\equiv
\left( \widehat{{b}}^{{S}},\widehat{{b}}^{{F}},\widehat{{b}}^{\text{{MU}}
}\right) ${.} \\
\quad -- {Check for convergence: end Step 2 if} \\ \hline
\quad $\frac{{1}}{{M}}{\max }\left \{ \mathop{\displaystyle \sum } \limits_{{
m=1}}^{{M}}\left \vert \frac{\left( {S}_{{m}}\right) ^{\left( {i+1}\right)
}-\left( {S}_{{m}}\right) ^{\left( i\right) }}{\left( {S}_{{m}}\right)
^{\left( i\right) }}\right \vert {,}\mathop{\displaystyle \sum } \limits_{{
m=1}}^{{M}}\left \vert \frac{\left( {F}_{{m}}\right) ^{\left( i+1\right)
}-\left( {F}_{{m}}\right) ^{\left( i\right) }}{\left( {F}_{{m}}\right)
^{\left( i\right) }}\right \vert {,}\mathop{\displaystyle \sum } \limits_{{
m=1}}^{{M}}\left \vert \frac{\left( \text{{MU}}_{{m}}\right) ^{\left( {i+1}
\right) }-\left( \text{{MU}}_{{m}}\right) ^{\left( {i}\right) }}{\left(
\text{{MU}}_{{m}}\right) ^{\left( {i}\right) }}\right \vert \right \} {
<\varpi }$. \\ \hline
\textit{Iterate on Step 2 until convergence.} \\ \hline \hline
\end{tabular}
}

We compute residuals on a stochastic simulation of $10,200$ observations (we
eliminate the first $200$ observations). In the test, we use a monomial rule
$M2$ with $2\cdot 6^{2}+1$ nodes which is more accurate than the other monomial
rule $M1$.