\documentclass{article}
    \usepackage{subcaption}
    \usepackage{amsmath, amssymb}
    \usepackage{graphicx, float}
    \usepackage[hidelinks]{hyperref}
    \usepackage[bottom]{footmisc}
    \usepackage[margin=.8in, tmargin=.8in]{geometry}
    \usepackage{esint} % for double line integrals

    \renewcommand{\baselinestretch}{1.2}
    \newcommand{\eps}{\epsilon}
    \newcommand{\der}{\partial}
    \newcommand{\del}{\nabla}
    \newcommand{\bm}[1]{\mathbf{#1}}
    \newcommand{\vf}[1]{\vec{\mathbf{#1}}}
    \newcommand{\norm}{\hat{\bm{n}}}

    \setlength\parindent{0pt}
    \captionsetup{justification=centering}

    \title{Vector Calculus}
    \date{\today}
    \author{Traiko Dinev \textless traiko.dinev@gmail.com\textgreater}

\begin{document}
\maketitle

\textit{NOTE: This partially follows Engineering Mathematics 2, a second year engineering course at the University of Edinburgh and partially follows MIT's 18.02, Multivariable Calculus.}

\textit{NOTE: Note this "summary" is NOT a reproduction of the course materials nor is it copied from the corresponding courses. It was entirely written and typeset from scratch.}

\textit{License: Creative Commons public license; See README.md of repository}

\textit{NOTE: Images are to be added soon!}

\section{Introduction}

Define the following:

\begin{align*}
    \del &=
        \bigg\langle
            \frac{\partial}{\partial x},
            \frac{\partial}{\partial y},
            \frac{\partial}{\partial z}
        \bigg\rangle
            && \text{gradient vector} \\
    \del f &=
        \bigg\langle
            \frac{\partial f}{\partial x},
            \frac{\partial f}{\partial y},
            \frac{\partial f}{\partial z}
        \bigg\rangle
            && \text{vector derivative}\\
    %
    \vf{F} &=
        \langle
            f(x, y, z), g(x, y, z), h(x, y, z)
        \rangle =
        f(...) \mathbf{i} + g(...)\mathbf{j} + h(...) \mathbf{k}
            && \text{Vector field}\\
    %
    \del \cdot \vf{F} &=
        \frac{\der f(x, y, z)}{\der x} +
        \frac{\der g(x, y, z)}{\der y} +
        \frac{\der h(x, y, z)}{\der z}
            && \text{Divergence (dot product)} \\
    %
    \del \times \vf{F} &=
        \begin{pmatrix}
            i & j & k \\
            \frac{\der}{\der x} &
                \frac{\der}{\der x} &
                    \frac{\der}{\der x} \\
            f(...) & g(...) & h(...)
        \end{pmatrix}
            && \text{Curl (cross product)} \\
\end{align*}

Gradients are just the derivatives in each dimension. Vector fields define a vector at each point in space, as defined by (x, y, z).
\vskip 0.1in
Here divergence gives the change in the vector field outwards (source of flux). This can also be defined as the limit of the net flow. Curl defines a rotational field (3D). It is the limit of the path integral. These two are neat relations, but I've never actually had to use them:

\begin{align*}
    \del \cdot \vf{F} &=
        \lim_{V \to p} \iint
            \frac{\vf{F} \cdot \norm}{|V|} dS \\
    (\del \times \vf{F}) \cdot \norm &=
        \lim_{A \to 0}\frac{1}{|A|} \int_c \vf{F} \cdot d\vf{r}
\end{align*}

\section{Double Integrals}
On to double (and triple) integrals. A double integral is just two single integrals stacked together. These are good to know:

\begin{align*}
    &\iint_R dA && \text{Area of R} \\
    &\iint_R \rho\ dA && \text{Mass, where $\rho$ is the density} \\
    &\bar{x} = \frac{1}{M}\iint_R x\ \rho\ dA && \text{Center of mass} \\
    &\iint_R r^2\ \rho\ dA && \text{Moment of intertia, $r$ is the distance from the axis}
\end{align*}

\subsection{Change of Variables}
Let:

\begin{align*}
    u &= u(x, y) \\
    v &= v(x, y)
\end{align*}

then:
\begin{equation*}
dx dy = \bigg|\frac{\der(u, v)}{\der(x, y)}\bigg|^{-1} du dv = \begin{pmatrix} \frac{\der u}{\der x} & \frac{\der u}{\der y} \\ \frac{\der v}{\der x} & \frac{\der v}{\der y} \end{pmatrix} du dv
\end{equation*}

This is derived from the following, which is just from the definition of derivatives:
\begin{equation*}
    \begin{pmatrix}\Delta u \\ \Delta v\end{pmatrix} \approx
        \begin{pmatrix}
            \frac{\der u}{\der x} & \frac{\der u}{\der y} \\
             \frac{\der v}{\der x} & \frac{\der v}{\der y}
        \end{pmatrix}
        \begin{pmatrix} \Delta x \\ \Delta y \end{pmatrix}
\end{equation*}

Or you could consider a parallelogram.. check out MIT's derivation for this one.

\section{Line Integration}
Consider a vector field $\vec{\mathbf{F}}$ and a path $c$. This could be a line or some parametric curve as defined by:

\begin{align}
    x &= x(t) \\
    y &= y(t) \\
    \vf{r} &= (x(t), y(t)) \\
    \frac{d\vec{r}}{dt} &= \langle \frac{dx}{dt}, \frac{dy}{dt} \rangle
\end{align}

for $t$ between 0 and 1. Or any other limit, but you can always scale it. You could also have more parameters.
\vskip 0.1in
Then the line (or path) integral is defined as follows:
\begin{equation*}
    \int_c \vf{F} \cdot d\vf{r} =
        \int_c Mdx + Ndy + Pdz = \int_c \vf{F} \cdot \vf{T} ds
\end{equation*}

Here $\vf{F} = (M, N, P)$ but the summation is not three separate integrals. Change of variables is needed. $\vf{T}$ is a tangent vector to the curve. $ds$ is the change along the curve.
\vskip 0.1in
Perhaps a better way to view this is by considering that work is force times distance: $W = \vf{F} \cdot \Delta \vf{r}$. Note this is a dot product between the force vector and the r defined above. When we sum, we obtain the integral:
\begin{equation}
    W = \lim_{i \to \infty} \sum_i \vf{F} \cdot \Delta \vf{r}_i = \int_C \vf{F} \cdot d\vf{r}
\end{equation}

Now we can break this up using the parametrization of $\vf{r}(t)$ above to arrive at:

\begin{equation}
    \int_c \vf{F} \cdot \Delta \vf{r} = \int_{0}^{1} (\vf{F} \cdot \frac{d\vf{r}}{dt})\ dt
\end{equation}

If you want a more rigorous derivation, you can start with the definition of derivatives and take the limit. The above is how we usually calculate line integrals. Note the derivative of r is just a vector, as defined above in (4). So path integrals are in reality just single variable integrals. Neat-o.
\vskip 0.1in
We can also do the integral in normal form. As opposed to a dot product with the tangent vector $\vf{T}$ we consider the (perpendicular) normal vector $\norm$. This is defined as the \textbf{flux} of the vector field $\vf{F}$. The flux measures how much "stuff" would be displaced by the vector field for a unit of time. It actually technically measures the force through a curve. Useful for magnetic fields:

\begin{equation}
    \int_c \vf{F} \cdot \norm\ ds \approx \sum_i (\vf{F} \cdot \norm)\ \Delta \vec{s}_i
\end{equation}

\textit{TODO: Insert parallelogram picture of normal and tangential path integrals and derivation.}

\subsection{Gradient Field Theorem}
\textit{A.k.a. the fundamental theorem of (vector) calculus}
If $\vf{F}$ is a gradient field, i.e. $\vf{F} = \nabla f$.

\begin{equation}
    \int_c \vf{F} \cdot d\vf{r} = \int \nabla f \cdot d\vf{r} = f(p_1) - f(p_0)
\end{equation}

where $p_0$ and $p_1$ are the endpoints of the curve $c$. A.k.a Stoke's theorem in 2D. \textit{TODO: add intuition}

\subsection{Green's Theorem}
This, where $c$ is counter-clockwise:

\begin{equation}
    \oint_c \vf{F} \cdot d\vf{r} = \iint_R (\nabla \times \vf{F})\ dA
\end{equation}

where $R$ is the region bounded by $c$.
\vskip 0.1in
Green's theorem also works for normal form (flux) integrals. Also known as the divergence theorem when we do in 3D.

\begin{equation}
    \oint_c \vf{F} \cdot \norm\ ds = \iint_R (\nabla \cdot \vf{F})\ dA
\end{equation}

\section{Triple Integration}
Area integrals become volume integrals:

\begin{align*}
    &\iiint_R dV && \text{volume} \\
    &dV = dx\ dy\ dz \\
    &\iiint_R \rho\ dV && \text{Mass} \\
    &\hat{f} = \frac{1}{\text{Volume}(R)}
        \iiint_R f\ dV && \text{Center of mass} \\
    &\iiint_R (\text{distance to axis})^2\delta\ dV
        && \text{Moment of inertia} \\
    &\mathbb{I}_z = \iiint_R (x^2 + y^2)^2\delta\ dV
        && \text{Moment of inertia around z-axis} \\
\end{align*}

\subsection{Cylindrical Coordinates}
\textit{TODO: add image}

\begin{align*}
    x &= \rho\ \cos \phi \\
    y &= \rho\ \sin \phi \\
    z &= z \\
    dV &= r\ dr\ d\theta\ dz
\end{align*}

\subsection{Spherical Coordinates}
\textit{TODO: add image}

\begin{align*}
    x = r\ \cos\theta = \rho \sin\phi\ \cos\theta\\
    y = r\ \sin\theta = \rho \sin\phi\ \sin\theta\\
    z = \rho \cos\theta \\
    dV = \rho^2 \sin\phi\ d\rho\ d\phi\ d\theta
\end{align*}

\section{Surface Integrals}
\textit{TODO: add image}
Natural extension to normal integration (see path integral) to three dimensions. This gives us the amount of "stuff" that goes through a patch in 3D. A patch (surface) would be the equivalent of a parametric curve in 2D.

\begin{equation}
    \iint_S \vf{F} \cdot d\vf{S} = \iint_S \vf{F} \cdot \norm\ dS
\end{equation}

Here $d\vf{S} = \norm\ dS$. Now these are a pain to calculate, but there are shortcuts:

\subsection{On a sphere}
\begin{equation*}
    dS = r^2\ \sin\phi\ d\phi\ d\theta
\end{equation*}

Here the normal vector is $\norm = \pm \frac{1}{a} \langle x, y, z \rangle$.

\subsection{Horizontal plane}
Three separate cases, we will consider when $z = a$. Hence:
\begin{align*}
    z &= a \\
    \norm &= \pm \mathbf{\hat{k}} \\
    dS &= dx\ dy
\end{align*}

\subsection{Cylinder of $r = a$ centered on the z axis}
\begin{align*}
    \norm &= \pm \langle x, y, 0, \rangle \frac{1}{a} \\
    dS &= a\ dz\ d\theta
\end{align*}

\subsection{Graph of function}
\begin{align*}
    z &= f(x, y) \\
    \norm\ dS &= \pm \langle -f_x, -f_y, 1\rangle\ dx\ dy
\end{align*}

\subsection{Parametric Surface}
\begin{align*}
    x &= x(u, v) \\
    y &= y(u, v) \\
    z &= z(u, v) \\
    \norm\ dS &= \pm
        \bigg(\frac{\der \vf{r}}{\der u}
            \times \frac{\der \vf{r}}{\der v} \bigg) du\ dv
\end{align*}

here $\times$ is a cross product. Why? Stay tuned.. I might add a derivation. Check MIT's course.

\subsection{Slanted plane with normal $\mathbf{\hat{N}}$}
\textit{TODO}

\section{Divergence Theorem}
This is the 3D analogue of Green's theorem for flux in 2D. If:

\begin{itemize}
    \item $S$ encloses $D$ in space, where $S$ is a closed surface
    \item $\norm$ is the normal vector pointing
        \textit{outwards} and $\vf{F}$ is defined and differentiable within $D$
\end{itemize}

Then:
\begin{equation}
    \oiint_S \vf{F} \cdot d\vf{S} = \iiint_D \nabla \cdot \vf{F}\ dV
\end{equation}

This means the total stuff escaping $S$ is the net sum of all the sources within it.

\section{Stoke's Theorem}
If $C$ is a closed curve and $S$ is any surface bounded by C.

\begin{equation}
    \oint_C \vf{F} s\vf{r} = \iint_S (\nabla \times \vf{F}) \cdot \norm \ dS
\end{equation}

If I walk along $C$ with $S$ to my left, then $\norm$ is pointing up.

\end{document}


\documentclass[11pt]{article}
\usepackage{amsmath, amsfonts, amsthm, amssymb}  % Some math symbols
\usepackage{enumerate}
\usepackage{fullpage}

\usepackage[x11names, rgb]{xcolor}
\usepackage{tikz}
\usepackage[colorlinks=true, urlcolor=blue]{hyperref}
\usepackage{graphicx}
\usepackage{gensymb}

\usetikzlibrary{snakes,arrows,shapes}

\usepackage{listings}
\usepackage{array}
\usepackage{mathtools}
\setlength{\parindent}{0pt}
\setlength{\parskip}{5pt plus 1pt}
\pagestyle{empty}

\def\indented#1{\list{}{}\item[]}
\let\indented=\endlist

\newcounter{questionCounter}
\newenvironment{question}[2][\arabic{questionCounter}]{%
    \addtocounter{questionCounter}{1}%
    \setcounter{partCounter}{0}%
    \vspace{.25in} \hrule \vspace{0.5em}%
        \noindent{\bf #2}%
    \vspace{0.8em} \hrule \vspace{.10in}%
}{}

\newcounter{partCounter}[questionCounter]
\renewenvironment{part}[1][\alph{partCounter}]{%
    \addtocounter{partCounter}{1}%
    \vspace{.10in}%
    \begin{indented}%
       {\bf (#1)} %
}{\end{indented}}

%%%%%%%%%%%%%%%%% Identifying Information %%%%%%%%%%%%%%%%%
%% This is here, so that you can make your homework look %%
%% pretty when you compile it.                           %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\myhwname}{Homework 5}
\newcommand{\mysection}{CS Fundamentals: Pong Review: Math! [due Tuesday]}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{center}
    {\Large \myhwname} \\
    \mysection \\
    \today
\end{center}

%%%%%%%%%%%%%%%%% PROBLEM 1: Probability Review %%%%%%%%%%%%%%%%%%%%%%%%%
\section{Review: Pong Game [Expected Duration: 15 - 20 min]}
\textbf{If you have any questions about the directions or any blocks you have not used before, let me know via email or text!}\\\\
\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}\\
In the last lesson, we built together a very cool pong game together.
We built this cool calculator by using many things we learned previously and MATH.
For homework, I want you to do some math with angles. Math is quite important in programming! Have fun!\\\\
I want you to use the \textbf{word} bank below to complete this assignment.
Note that \textbf{the words in the word bank is used exactly once... all words are used}.\\\\
If you have any questions, \textbf{please} let me know!\\\\
\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}\\
\textbf{Instructions/Notes:}\\
$0\degree = 360\degree = -360\degree$ is Up.\\
$90\degree = -270 \degree$ is East.\\
$-90\degree = 270 \degree$ is West.\\
$180\degree = -180\degree$ is South.\\\\
This homework is hard! You will need to know how to do reflection over x and y-axis. Do as best as you can! It is okay to not finish. Looking at the code for the pong game can help!
\\
\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
\begin{enumerate}
\item \textbf{Bouncing off of the top wall}\\
The only walls the ball in pong can bounce off of are the ones on the bottom and at the top. Drawing pictures may help solve the problems below.
 % $\rule{2.5cm}{0.15mm}$
\begin{enumerate}[a.]
\item Let's say a ball was approaching the top wall at a $-45\degree$ (NorthWest) angle. To simulate bouncing off of the wall, we send the ball going at a $-135\degree$ (SouthWest) angle. Was this a reflection over the x-axis or the y-axis (think 2D coordinate! if you don't know what this means, email me!)?\\\\\\
\item If the ball was approaching the top wall at a $50 \degree$ angle, what direction (degree) should the ball be traveling once the ball bounces? (Think reflection.)\\\\\\
\item Generally, if the ball was approaching the top wall at a $x \degree$ angle, what direction $y \degree$ shoudld the ball be traveling once the ball bounces? Write an equation for $y$ in terms of $x$. (note: this is hard! try your best! the code can help!)\\\\\\\\\\\\
\end{enumerate}
\end{enumerate}
\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
\begin{enumerate}
\item \textbf{Bouncing off of the bottom wall}\\
The only walls the ball in pong can bounce off of are the ones on the bottom and at the top. Drawing pictures may help solve the problems below.
 % $\rule{2.5cm}{0.15mm}$
\begin{enumerate}[a.]
\item Let's say a ball was approaching the bottom wall at a $135\degree$ (Southeast) angle. To simulate bouncing off of the wall, we send the ball going at a $45\degree$ (Northeast) angle. Was this a reflection over the x-axis or the y-axis (think 2D coordinate! if you don't know what this means, email me!)?\\\\\\
\item If the ball was approaching the bottom wall at a $-100 \degree$ angle, what direction (degree) should the ball be traveling once the ball bounces? (Think reflection.)\\\\\\
\item Generally, if the ball was approaching the bottom wall at a $x \degree$ angle, what direction $y \degree$ shoudld the ball be traveling once the ball bounces? Write an equation for $y$ in terms of $x$. (note: this is hard! try your best! the code can help!)\\\\\\\\\\\\\\\\\\\\\\
\end{enumerate}
\end{enumerate}
\begin{enumerate}
\item \textbf{Bouncing off of the right player's paddle}\\
There are 2 players in pongs. Each has a paddle. The balls can bounce off these paddles. Drawing pictures may help solve the problems below.
 % $\rule{2.5cm}{0.15mm}$
\begin{enumerate}[a.]
\item Let's say a ball was approaching the right player's paddle  at a $135\degree$ (Southeast) angle. To simulate bouncing off of the paddle, we send the ball going at a $-135 \degree$ (Southwest) angle. Was this a reflection over the x-axis or the y-axis (think 2D coordinate! if you don't know what this means, email me!)?\\\\\\
\item If the ball was approaching the right player's paddle at a $100 \degree$ angle, what direction (degree) should the ball be traveling once the ball bounces? (Think reflection again.)\\\\\\
\item Generally, if the ball was approaching the right player's paddle at a $x \degree$ angle, what direction $y \degree$ shoudld the ball be traveling once the ball bounces? Write an equation for $y$ in terms of $x$. (note: this is hard! try your best! the code can help!)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\end{enumerate}
\end{enumerate}
\begin{enumerate}
\item \textbf{Bouncing off of the left player's paddle}\\
There are 2 players in pongs. Each has a paddle. The balls can bounce off these paddles. Drawing pictures may help solve the problems below.
 % $\rule{2.5cm}{0.15mm}$
\begin{enumerate}[a.]
\item Let's say a ball was approaching the left player's paddle  at a $-45\degree$ (NorthWest) angle. To simulate bouncing off of the paddle, we send the ball going at a $45 \degree$ (NorthEast) angle. Was this a reflection over the x-axis or the y-axis (think 2D coordinate! if you don't know what this means, email me!)?\\\\\\
\item If the ball was approaching the right player's paddle at a $120 \degree$ angle, what direction (degree) should the ball be traveling once the ball bounces? (Think reflection again.)\\\\\\
\item Generally, if the ball was approaching the right player's paddle at a $x \degree$ angle, what direction $y \degree$ shoudld the ball be traveling once the ball bounces? Write an equation for $y$ in terms of $x$. (note: this is hard! try your best! the code can help!)
\end{enumerate}
\end{enumerate}
\end{document}


\subsection{The social graph}
\label{social_graph}

One of the problems we investigated at LinkedIn was using the social graph to
guide and power the design of data-driven products.
Assume that a social graph is presented as a
a table, {\tt E}, with 2 columns, {\tt from} and {\tt to}. Q supports the
creation of auxiliary data structures to enable fast access but, in keeping with
our minimalist philosophy, does not prvoide them out of the box.
\be
\item {\tt f, t = Q.sort2(E.from, E.to, "ascending")}, sorts {\tt E}
with {\tt from} as primary key and {\tt to} as
secondary key (Section~\ref{par_sort})
\item {\tt V.m = Q.unique(f)} creates a Vector of
member IDs, sorted ascending.
\item For each member in {\tt V.m}, we determine the contiguous edges out of it
  by
  \be
\item {\tt V.lb = Q.join(f, V.m, min\_index)}
\item {\tt V.ub = Q.join(f, V.m, max\_index)}
  \ee
Now, we can assert that member
{\tt m = V.m[i]} is connected to members {\tt t[V.lb[i] .. V.ub[i]]}
\ee

\subsubsection{Speeding up sort}
\label{par_sort}
Consider a simple parallelization of the sort routine. Assume that we
were given a set of bins such
that each bin would get ``roughly'' the same number of elements of an input
Vector \(x\). Then a simple parallel sort consists of
(1) partitioning \(n\) elements of \(x\) into \(n_B\) bins so that each bin has
\(n(b)\) elements
(2) sorting each bin in parallel
(3) copying each bin into the right place. Probabilistic guarantees
of the form \(P[n(b) \geq (1+\beta) \times(n/n_B)] \leq \epsilon\)
allow us to allocate slightly more space (specified by \(\beta\)) than absoutely necessary and to
fall back to the sequential sort in the small probability (specified by
\(\epsilon\))  that our estimates are off.

But where do these bins come from? As in the social graph example above, while
new connections are being created and deleted all the time, the change in these
overall distribution is much, much slower and can be updated monthly while
retaining enough accuracy for the purpose at hand --- load balancing.
{\tt Q.sort} operator accepts ``hints'' in the form of
optional arguments. In this case, the hint is a Vector whose length is the
number of bins and each element is the upper bound of that bin. For Vectors of
length greater than \(2^{20}\), we obtained linear speedup on an Intel Core i7.

To quote George Santayana, ``those who don't remember the past are condemned to
repeat it''.  \Q\ minimizes re-work in several ways
\be
\item
Remembering basic statistics such as min, max, sum, average, whenever the
corresponding operators are invoked.
Such meta-data is often the by product of other operators.
\item the sorted-ness of a vector is
stored as one of (a) unknown (b) ascending (c) descending (d) not sorted.
\Q\
uses sort heavily to simplify other operators by converting them into linear
scans.
%% explain difference between unknown and not sorted
For example, consider {\tt x, y  = Q.count(z)} where \(x\) contains the
unique values of \(z\) and \(y\) the number of occurrences. If \(z\) is not
sorted, then the count operator internally (a) creates a copy, \(z'\), of \(z\)
(b) sorts \(z'\) (c) passes \(z'\) to the core {\tt count} code and (d) \(z'\) gets
garbage collected when the count operator returns. Of course, if \(z\) is
sorted, then this cost of sorting \(z'\) is eliminated.

\ee


\chapter{Government}
\index{Government%
@\emph{Government}}%

%\pagestyle{empty}

\section{Overview of government in the model}

The government has four functions in the model.  First, it runs the tax and social security systems as described in the households' and firms' problems.  That is, it collects revenues and makes payments in accordance with the parameterized tax and social security policy functions.  The tax functions are user determined, but will default to a current law baseline if the user does not specify a policy proposal in affecting the relevant tax function.  Second, the government makes direct transfers to households outside of the social security system.  Third, the government produces a non-excludable public good.  Finally, the government contributes to the production of a private consumption good.  We discuss each of these function in more detail below after first defining the government's budget constraint.
Government will have four functions in our model:

\section{Government budgeting}

The government's per-period budget constraint is given by:

      \begin{equation}
      \label{eqn:gbc}
      D_{t+1} + T^{\tau}_{t} = (1+r_{t})D_{t} + T^{H}_{t} + G^{subs}_{t} + w_{t}EL^{G}_{t} + p^{K}_{g}I^{G}_{t},
      \end{equation}

where $D_t$ denotes the government's outstanding debt, $T_t$ is total tax revenue across all tax sources and net of social security transfers, $T^H_t$ is total direct transfers to households, $G^{subs}_t$ are government subsides to the production private goods, $EL_t$ is are effective labor units employed by government in the production of the public good, and $I_t$ is government investment in capital used to produce the government provided public good.  The price of capital for the government sector is given by $p^{K}_{g}$ and the price of an effective labor unit is $w_{t}$.  Note that we do not impose a balanced budget.  In any particular period, the government may run a surplus or deficit.  The government finances any gaps using debt, $D_{t}$.

    \subsection{Rule for long-term fiscal stability}

      While the government can use debt to finance temporary budget shortfalls, the government cannot finance infinite amounts of debt.  For example, it cannot be the case that the debt level grows to such an extent that interest payments on the debt exceed GDP.  To ensure that the government budget is sustainable in the long run, we impose a rule that returns the debt-to-GDP ratio to some predetermined value after a set amount of time.  In particular, the user will select (or the model will default to) a particular steady-state debt-to-GDP ratio, $\bar{d}=\frac{\bar{D}}{\bar{Y}}$.  The model will then specify some period, $T$, such that after this time, the government budget adjusts to return to $\bar{d}$.  In particular, if the debt-to-GDP ratio exceeds $\bar{d}$, then government provision of the public good, $G_{t}$ is reduced.  This government debt rule takes the form:

%      \begin{equation}
%        D_{t+1} = D_t(1+r_t) - T_t + T^H_t + G_t + L_t + S_t
%      \end{equation}
%
%      Letting a carat denote the ratio of a variable to GDP, we can rewrite this as follows:
%
%      \begin{equation} \label{EqDhatlom}
%        (1+g_{Yt}) \hat D_{t+1} = \hat D_t(1+r_t) - \hat T_t + \hat T^H_t + \hat G_t + \hat L_t + \hat S_t
%      \end{equation}
%
%      We need to adopt a government fiscal rule that determines how our residual expenditure $\hat G_t$ evolves over time.
%
%      One way is to adopt a balanced budget rule which keeps the debt-to-GDP ratio constant at it's initial value of $\hat D_0$.
%
%      \begin{align}
%        (1+g_{Yt}) \hat D_0 & = \hat D_0(1+r_t) - \hat T_t + \hat T^H_t + \hat G_t + \hat L_t + \hat S_t \nonumber \\
%        \hat G_t & = \hat D_0(g_{Yt}-r_t) + \hat T_t - \hat T^H_t -\hat L_t - \hat S_t \label{EqBalBudRule}
%      \end{align}
%
%      Another rule is to hold govenrment spending constant and let debt evolve as it will for several period.  Then in period $T$ impose fiscal austerity which forces $\hat G_t$ to adjust over time so that $\hat D_t$ goes to a steady value.

      \begin{equation}
         G_{t} - \bar{G} = \rho_t (\hat{d}_t - \bar{d});\quad \rho_t<0,
        \label{EqAdjRule}
      \end{equation}

\noindent\noindent where $\hat{G}$ is the steady state level of public good (given steady-state debt-to-GDP ratio $\bar{d}$) and $\rho$ is a parameter that determines how quickly government debt returns to its steady-state value.  The parameter $\rho$ will be estimated from historical data on the response of government spending to debt.  This calibration is discussed in the calibration chapters to be added to this document.
%
%      Substituting this into \eqref{EqDhatlom} gives:
%      \begin{align}
%        (1+g_{Yt}) \hat D_{t+1} & = \hat D_t(1+r_t) - \hat T_t + \hat T^H_t + \rho_t (\hat D_t - \bar D) + \bar G + \hat L_t + \hat S_t \nonumber \\
%        \hat D_{t+1} & = \frac{\hat D_t(1+r_t) - \hat T_t + \hat T^H_t + \rho_t (\hat D_t - \bar D) + \bar G + \hat L_t + \hat S_t }{1+g_{Yt}} \label{EqDhatlom2}
%      \end{align}
%
%      Consider the steady state version of this.
%      \begin{align}
%        (1+\bar g_{Y}) \bar D & = \bar D(1+\bar r) + \bar T - \bar T^H + \rho_t (\bar D - \bar D) + \bar G + \bar L + \bar S_t  \nonumber \\
%        \bar G & = \bar D(\bar g_{Y} -\bar r) + \bar T - \bar T^H - \bar L - \bar S  \label{EqGbardef}
%      \end{align}
%
%      This tells us the long-run value of government spending to GDP that will maintain the debt to GDP target.
%
%      In order for \eqref{EqDhatlom2} to be a contraction mapping over $\hat D$ and thus converge to a steady state, we must put bounds on $\rho_t$.  Rearranging \eqref{EqDhatlom2} and using \eqref{EqGbardef}:
%
%      \begin{align}
%        \begin{split}
%        (1+g_{Yt}) \hat D_{t+1} & = \hat D_t (1+r_t) - \hat T_t + \hat T^H_t + \rho_t (\hat D_t - \bar D) + \hat L_t + \hat S_t \\
%        & + \bar D(\bar g_{Y} - \bar r) + \bar T - \bar T^H - \bar L - \bar S
%        \end{split} \nonumber \\
%        \begin{split}
%        (1+g_{Yt}) \hat D_{t+1} & = \hat D_t (1+r_t) - \hat T_t + \hat T^H_t + \rho_t \hat D_t - \rho_t \bar D + \hat L_t + \hat S_t \\
%        & + \bar g_Y \bar D - \bar r \bar D + \bar T - \bar T^H - \bar L - \bar S
%        \end{split} \nonumber \\
%        \begin{split}
%        (1+g_{Yt}) \hat D_{t+1} & =  \hat D_t (1+r_t) + \rho_t (\hat D_t -\bar D) + (\bar g_Y - \bar r ) \bar D \\
%        & - (\hat T_t - \bar T) + (\hat T^H_t -\bar T^H) + (\hat L_t -\bar L) + (\hat S_t -\bar S)
%        \end{split} \nonumber \\
%        \begin{split}
%        \hat D_{t+1} - \bar D & = \hat D_t \frac{1+r_t}{1+g_{Yt}} + \frac{\rho_t}{1+g_{Yt}} (\hat D_t -\bar D) + \left( \frac{\bar g_Y - \bar r}{1+g_{Yt}} - 1 \right) \bar D \\
%        & + \frac{-(\hat T_t - \bar T) + (\hat T^H_t -\bar T^H) + (\hat L_t -\bar L) + (\hat S_t -\bar S)}{1+g_{Yt}}
%        \end{split}  \nonumber \\
%        \begin{split}
%        \hat D_{t+1} - \bar D & = \frac{1+r_t+\rho_t}{1+g_{Yt}} (\hat D_t -\bar D) \\
%        & + \frac{-(\hat T_t - \bar T) + (\hat T^H_t -\bar T^H) + (\hat L_t -\bar L) + (\hat S_t -\bar S)}{1+g_{Yt}}
%        \end{split}
%        \label{EqStab}
%      \end{align}
%
%      We need $\frac{\hat D_{t+1} - \bar D}{\hat D_{t} - \bar D} < 1$ for stability.  Equation \eqref{EqStab} gives:
%      \begin{align}
%        \frac{\hat D_{t+1} - \bar D}{\hat D_t -\bar D} & = \frac{1+r_t+\rho_t}{1+g_{Yt}}  + \frac{-(\hat T_t - \bar T) + (\hat T^H_t -\bar T^H) + (\hat L_t -\bar L) + (\hat S_t -\bar S)}{(1+g_{Yt})(\hat D_t -\bar D)} < 1 \nonumber \\
%        & \frac{1+r_t+\rho_t}{1+g_{Yt}}  < \frac{(\hat T_t - \bar T) - (\hat T^H_t -\bar T^H) - (\hat L_t -\bar L) - (\hat S_t -\bar S)}{(1+g_{Yt})(\hat D_t -\bar D)} \nonumber \\
%        & \rho_t  < (1+r_t)\frac{(\hat T_t - \bar T) - (\hat T^H_t -\bar T^H) - (\hat L_t -\bar L) - (\hat S_t -\bar S)}{\hat D_t -\bar D}
%      \end{align}

 \section{Direct transfers}
      Direct transfers to individuals, $T^{H}_{t}$, will be modeled as a polynomial function of age and income.  In that sense, this function will be similar to those used to determine individual income taxes.  This function will be estimated from data on government transfers by age and income....

\section{Government production of public goods}

The government engages in the production of a non-excludable public good.  Utility from the public good enters the individuals' utility functions in an additively separable way, and thus is excluded from the description above since it does not impact consumer decisions.  To produce the public good, the government uses a capital and labor in a constant returns to scale Cobb-Douglas production function.  In particular, the total quantity of the public good is given by:

\begin{equation}
\label{eqn:pub_good}
G_{t} = (K^{G}_{t})^{\alpha}(EL^{G}_{t})^{1-\alpha}
\end{equation}

\noindent\noindent The parameter $\alpha$ thus represents capital's share of output of the public good.  The government's capital stock follows the standard law of motion: $K^{G}_{t+1} = (1-\delta^{G})K^{G}_{t} + I^{G}_{t}$.

We determine $G_{0}$ as the the total amount of spending on government goods and services less the purchase of inputs for government production of private goods in the model's base year.  We then set $G_{t}=G_{0}$ for all $t<T$.  At period $T$, the government budget may need to adjust $G_{t}$ to make debt sustainable.  Thus the government supply of the public good is exogenous in periods 0 to $T$ and determined by the steady state value of debt there after.  We use this to solve for the expenditures on labor and capital to produce this public good.  The government solves:

%\begin{equation}
%\label{eqn:pub_good_prob}
\begin{align}
\min_{\{EL^{G}_{u},I^{G}_{u}\}^{\infty}_{u=t}} \sum_{u=t}^{\infty} \prod_{\nu=t}^{u}\left(\frac{1}{1+r_{\nu}}\right) w_{u}EL^{G}_{u} + p^{K}_{g,u}I^{G}_{u} & \\
\text{subject to:} \ & G_{t} = (K^{G}_{t})^{\alpha}(EL^{G}_{t})^{1-\alpha} \ \text{and} \\
	& K^{G}_{t+1} = (1-\delta^{G})K^{G}_{t} + I^{G}_{t}, \forall  \ t
\end{align}
%\end{equation}

The equation above can be solved for government's demand for capital and labor as a function of the model parameters, factor prices, and government supply of public goods.  In particular, we can find the demand for capital as:

\begin{equation}
K^{G}_{t+1} = \frac{\alpha}{1-\alpha}\frac{w_{t}}{p^{K}_{g,t}}(1+r_{t})L_{t}\frac{G_{t+1}}{G_{t}}
\end{equation}

\section{Government production of private goods}

The government is one of the $M$ production sectors.  Its problem is thus described in Chapter \ref{chap:firms}.  However, the government firm differs from the private firms in that the gross-of-tax output price, $p_{g,t}$, includes a subsidy.  This means that the government sells output at a price that is below the cost of production. Instead of the zero-profit condition, the condition for government firms is:

\begin{equation}
G^{subs}_{t} = p_{g,t}X_{g,t} - w_{t}EL_{g,t} - r_{t}K_{g,t}
\end{equation}

\noindent\noindent  Thus the government subsidy towards the production of private goods is the difference between government revenues at the subsidized price and the costs of inputs to production.