\section{Locality (almost done!)}
\begin{theorem}
We discovered that $S^\sca_\ast(X)\hookrightarrow S_\ast(X)$ is a subcomplex, and this induced an isomorphism in homology.
\end{theorem}
We talked about subdivision and the cone construction, the latter of which dealt with a star-shaped region, relative to some point (which we can safely assume is the origin) $b$. If $\sigma:\Delta^n\to X$ is a map, then $b\ast \sigma:S_n(X)\to S_{n+1}(X)$ where $\ast$ is the join. We did all of this before. The property that this had is that it's a homotopy between $1$ and $\eta_b\epsilon$, i.e., $db\ast + b\ast d = 1-\eta_b\epsilon$. This is called equation $(\ast)$. Look above for the definition of $\eta_b$ and $\epsilon$. Hopefully you remember this story.

The subdivision operator $\$:S_\ast(X)\to S_\ast(X)$ for any space $X$ is natural, so it's enough to say what $\$\iota_n$ and $\$\iota_0$ is. Define $\$\iota_0=\iota_0$, and define $\$\iota_n=b_n\ast\$(d\iota_{n-1})$ where $b_n$ is the barycenter of the $n$-simplex. The standard simplex is star-shaped relative to its barycenter, so by naturality, it suffices to do this for $\iota_n$. The two key properties are the following.
\begin{theorem}
\begin{enumerate}
\item $\$$ is a chain map.
\item There is a chain homotopy $T:\$\sim 1$.
\end{enumerate}
\end{theorem}
\begin{proof}
Let's try to prove that it's a chain map. We'll use induction on $n$. It's enough to show that $d\$\iota_n=\$ d\iota_n$, because:$$d\$\sigma=d\$\sigma_\ast\iota_n=\sigma_\ast d\$\iota_n=\sigma_\ast \$d\iota_n=\$ d\sigma_\ast\iota_n=\$ d\sigma$$
We declared $d\$\iota_0=d\iota_0=0$. But also $\$d\iota_0=\$0=0$, so this works.

For $n\geq 1$, we want to compute $d\$\iota_n$. This is:
\begin{align*}
d\$\iota_n & =d(b_n\ast \$ d\iota_n) & \\
 & = (1-\eta_b\epsilon-b_n\ast d)(\$ d\iota_n) & \text{by $(\ast)$}
\end{align*}
What happens when $n=1$? Well:
$$\eta_b\epsilon\$d\iota_1 = \eta_b\epsilon \$(c^0_1 - c^0_0)=\eta_b\epsilon(c^0_1 - c^0-0)=0$$
Because $\epsilon$ takes sums of coefficients, which is $1+(-1)=0$. Let's continue.
\begin{align*}
d\$\iota_n & = ... & \\
 & = \$d\iota_n - b_n\ast d\$ d\iota_n & \\
 & = \$d\iota_n - b_n\$d^2\iota_n &\\
 & = \$d\iota_n & \text{because $d^2=0$}
\end{align*}
So we're done.

To define the chain homotopy $T$, we'll just write down a formula and not justify it. We just need to define $T\iota_n$ by naturality. So define:
\begin{equation*}
T\iota_n = \begin{cases}
0 & n=0\\
b_{n}\ast(\$\iota_n - \iota_n- Td\iota_n)\in S_{n+1}(\Delta^n) & n>0
\end{cases}
\end{equation*}
This is because $T:S_n(X)\to S_{n+1}(X)$ such that $dT+Td=\$-1$. I'm confused about this, so help me out. Hmm. The term $\$\iota_n - \iota_n-Td\iota_n$ is an $n$-chain. We're going to do this by induction. Again, we need to check only on the universal case.

When $n=0$, $dT\iota_0 + Td\iota_0 = 0+0 = 0 = \$\iota_0 - \iota_0$ because $\$\iota_0=\iota_0$. Now let's induct. For $n\geq 1$, let's start by computing $dT\iota_n$. This is:
\begin{align*}
dT\iota_n & = d_n(b_n\ast(\$\iota_n - \iota_n - Td\iota_n)) & \\
& = (1-b_n\ast d)(\$\iota_n - \iota_n - Td\iota_n) & \text{by $(\ast)$}\\
 & = \$\iota_n-\iota_n-Td\iota_n-b_n\ast (d\$\iota_n - d\iota_n - dTd\iota_n)
\end{align*}
We can ignore the $\eta_b\epsilon$ part because we're in dimension $\geq 1$. All we want now is that $b_n\ast(d\$\iota_n - d\iota_n - dTd\iota_n)=0$. We can do this via induction, because $T(d\iota_n)$ is in dimension $n$ (or is it $n-1$?):
\begin{align*}
dTd\iota_n & = -Td(d\iota_b)+\$ d\iota_n - d\iota_n\\
& = \$d\iota_n - d\iota_n\\
& = d\$\iota_n - d\iota_n
\end{align*}
This means that $d\$\iota_n-d\iota_n - dTd\iota_n=0$, so we're done.
\end{proof}
\begin{corollary}
$\$^k\sim 1:S_\ast(X)\to S_\ast(X)$. I.e., we're iterating subdivision. We want $T_k$ such that $dT_k+T_kd=\$^k-1$
\end{corollary}
\begin{proof}
$dT+Td=\$-1$. Let's apply $\$$ to this. We get $\$dT+\$Td=\$^2-\$$. Sum up these two things, so we get $dT+Td+\$dT+\$Td = \$^2-1$. But now, $\$d=d\$$, so the left hand side is $dT+d\$T + Td+\$Td = d(\$+1)T + (\$+1)Td$, i.e., $d(\$+1)T+(\$+1)Td=\$^2-1$. So define $T_2=(\$+1)T$, and continuing, you see that $T_k=(\$^{k-1}+\$^{k-2}+\cdots+1)T=\left(\sum^{k-1}_{i=0}\$^i\right)T$.
\end{proof}
\begin{prop}[Almost completes the proof of locality]
Let $\sca$ be a cover of $X$. For every chain $c\in S_n(X)$, there is a $k\geq 0$ such that $\$^kc\in S^\sca_n(X)$. This is the geometric thing we have to prove.
\end{prop}
\begin{proof}
We may assume that $c:\sigma:\Delta^n\to X$, and this makes sense because you just take the max of the $k$ of the terms of the sum. A great trick is the following: define an open cover $\mathscr{U}$ of $\Delta^n$ defined by $\mathscr{U}:=\{\sigma^{-1}(\mathrm{Int}(A))|A\in\sca\}$. This is a cover, a basic result from topology. Then we use the Lesbegue covering lemma:
\begin{lemma}[Lesbegue covering lemma]
We'll pretend this is part of 18.901. Let $M$ be a compact metric space (eg. $\Delta^n$), and let $\mathscr{U}$ be an open cover. Then there is $\epsilon> 0$ such that for all $x\in M$, there is $B_\epsilon(x)\subseteq U$ for some $U\in \mathscr{U}$.
\end{lemma}
\begin{proof}
Omitted, may be in 18.901. Or even 18.100B.
\end{proof}
Let's apply this to the cover we constructed. What we want is that for all $\epsilon>0$, there is a $k$ such that the diameter of the simplices in $\$^k\iota_n$ is less than $\epsilon$. Let's do that.
\begin{question}
How small are these subdivided simplices in $\$^k\iota_n$?
\end{question}
For example, suppose $\sigma:\Delta^n\to\Delta^n$ is something in the subdivision. These are all affine simplices, i.e., it's determined by where the simplices of $\Delta^n$ go to in $\sigma$. We can write $\sigma=\langle v_,\cdots,v_n\rangle$. It could be in $\mathbf{R}^N$ if you wanted; maybe it's easier to think of it this way. The barycenter is $\frac{\sum_{i=0}^nv_i}{n+1}$. Let's compute:
\begin{align*}
|b-v_i| & =\left|\frac{v_0+\cdots+v_n-(n+1)v_i}{n+1}\right|\\
& =\left|\frac{(v_0-v_i)+(v_1-v_i)+\cdots+(v_n-v_i)}{n+1}\right|\\
 & \leq \frac{n}{n+1}\max_{i,j}|v_i-v_j|\\
 & = \frac{n}{n+1}\mathrm{diam}(\img\sigma)
\end{align*}
The following lemma completes the proof because there's always a $k$ such that $\left(\frac{n}{n+1}\right)^k<\epsilon$.
\begin{lemma}
Let $\tau$ be a simplex in $\$^k\sigma$ where $\sigma$ is an affine simplex. Then $\mathrm{diam}(\tau)\leq \frac{n}{n+1}\mathrm{diam}(\sigma)$.
\end{lemma}
\begin{proof}
Let's write $\tau=\langle w_0=b,\cdots,w_n\rangle$ and $\sigma=\langle v_0,\cdots,v_n\rangle$. We saw:
\begin{align*}
|b-w_i| & \leq\max_i|b-v_i|\\
 & \leq \frac{n}{n+1}\mathrm{diam}(\sigma)
\end{align*}
For the other cases, well, we use induction:
\begin{align*}
|w_i-w_j|& \leq \mathrm{diam}(\text{simplex in }\$d\sigma)\\
& \leq \frac{n-1}{n}\mathrm{diam}(d\sigma)\\
& \leq \frac{n}{n+1}\mathrm{diam}(\sigma)
\end{align*}
We're almost there. We'll finish the proof of locality on Friday.
\end{proof}
\end{proof}


\section{Evaluation}
\label{sec:Evaluation}
This section contains the evaluation and the graphical representation of the measurements. The following subsections contain the measurement results of the individual tasks listed in chapter \ref{subsec:Measurements}. The aim is to determine the sound velocity and the attenuation coefficient of different materials. The exact descriptions of the tasks can be found in the assignment \cite{ultrasound}.

%-----------------------------------------------------------------------------------
\subsection{Wave Length Calculation}
\label{subsec:wave_length_calculation}
The following table \ref{tab:wave_length_calculation} shows the calculated wave length $\lambda$ of the ultrasound wave in air, distilled water and aluminium at 1 kHz, 1 MHz and 5 MHz. All literature longitudinal sound velocities are at 20 \textdegree C room temperature.

\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c|c|c}
		& $\boldsymbol{c_L}$ ($\frac{\si{m}}{\si{s}}$) \cite{kohlrausch} & $\boldsymbol{\lambda}$ \textbf{@ 1 kHz} (m) & $\boldsymbol{\lambda}$ \textbf{@ 1 MHz} (m) & $\boldsymbol{\lambda}$ \textbf{@ 5 MHz} (m) \\
		\hline\hline
		\textbf{Air} & 344 & 0.34 & $344\cdot 10^{-6}$ & $68.8\cdot 10^{-6}$ \\
		\textbf{Distilled Water} & 1483 & 1.48 & $1.48\cdot 10^{-3}$ & $297\cdot 10^{-6}$ \\
		\textbf{Aluminium} & 6400 & 6.40 & $6.40\cdot 10^{-3}$ & $1.28\cdot 10^{-3}$ \\
	\end{tabular}
	\caption{Calculated wave lengths $\lambda$ for different solids and liquids at the audible frequency 1 kHz and the two ultrasound frequencies 1 MHz and 5 MHz. The calculations were carried out in the software Microsoft Office Excel.}
	\label{tab:wave_length_calculation}
\end{table}

%-----------------------------------------------------------------------------------
\subsection{Longitudinal Ultrasound Velocities in Solids}
\label{subsec:Longitudinal_Ultrasound_Velocities_in_Solids}
Table \ref{tab:Longitudinal_Ultrasound_Velocities_in_Solids} shows the calculated longitudinal ultrasound velocities from the measurements. These results were obtained using linear regression. This was done in the software QtiPlot. To use linear regression, equation \ref{eq:sound_velocity} has to be rearranged, so that it relates the time of flight with the distance and an offset $b$ has to be added:

\begin{equation}
\Delta t = \frac{2x}{c_L} + b
\label{eq:sound_velocity_linear_regression}
\end{equation}

This equation \ref{eq:sound_velocity_linear_regression} can now be used in QtiPlot.

\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c|c}
		& $\boldsymbol{c_L}$ \textbf{@ 1 MHz} ($\frac{\si{m}}{\si{s}}$) & $\boldsymbol{c_L}$ \textbf{@ 5 MHz} ($\frac{\si{m}}{\si{s}}$) & \textbf{Literature Value} ($\frac{\si{m}}{\si{s}}$) \cite{kohlrausch} \\
		\hline\hline
		\textbf{PMMA} & $(2751\pm 5)$ & $(2721\pm 10)$ & $\approx 2700$ \\
		\textbf{Aluminium} & - & $(6387\pm 30)$ & $\approx 6400$ \\
		\textbf{Copper} & - & $(4680\pm 17)$ & $\approx 4700$ \\
		\textbf{Brass} & - & $(4627\pm 46)$ & $\approx 4700$ \\
	\end{tabular}
	\caption{Measured longitudinal ultrasound velocities $c_L$ in different solids. Furthermore, the literature value of $c_L$ is stated. The literature values are from a physics book from 1968.}
	\label{tab:Longitudinal_Ultrasound_Velocities_in_Solids}
\end{table}

The longitudinal ultrasound velocities of the metals could not be measured with a frequency of 1 MHz. Figure \ref{fig:ghost} shows the measured \flqq ghost signals\frqq\ (signals which are overlapping). Due to this, the longitudinal ultrasound velocities of the metals was only measured with a frequency of 5 MHz, which works perfectly fine.

Figures \ref{fig:Longitudinal_Ultrasound_Velocity_of_PMMA} and \ref{fig:Longitudinal_Ultrasound_Velocity_of_Aluminium} show the plots obtained from QtiPlot for PMMA and aluminium respectively.

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.94]{Longitudinal_Ultrasound_Velocity_of_PMMA}
	\caption{This plot shows the time of flight $\Delta t$ in relation to the distance $x$ for PMMA.Linear regression can then be used on these discrete values to obtain the longitudinal ultrasound velocity of PMMA. The fitted curve is shown in red and the discrete measurements are shown as black dots.}
	\label{fig:Longitudinal_Ultrasound_Velocity_of_PMMA}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.94]{Longitudinal_Ultrasound_Velocity_of_Aluminium}
	\caption{Linear regression is used here as well. This time to obtain the longitudinal ultrasound velocity of aluminium. The measurement had to be done at a frequency of 5 MHz because of ghost signals (see figure \ref{fig:ghost}). The fit is shown in red and the measurements as black dots.}
	\label{fig:Longitudinal_Ultrasound_Velocity_of_Aluminium}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.5]{ghost}
	\caption{When metals are measured with a transducer that has an oscillation frequency of 1~MHz, \flqq ghost signals\frqq\ occur. This is due to the much higher longitudinal ultrasound velocity of the metals, which leads to signals overlapping also known as \flqq ghost signals\frqq. To get rid of this effect the oscillation frequency can be increased.}
	\label{fig:ghost}
\end{figure}

\newpage
%-----------------------------------------------------------------------------------
\subsection{Attenuation Coefficient of PMMA}
\label{subsec:Attenuation_Coefficient_of_PMMA}
The results for the attenuation coefficient $\mu$ of PMMA are shown in table \ref{tab:Attenuation_Coefficient_of_PMMA} below. The attenuation coefficient $\mu$ was measured with an oscillation frequency of 1 MHz and 5 MHz.

\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c}
		& $\boldsymbol{\mu}$ \textbf{@ 1 MHz} ($m^{-1}$) & $\boldsymbol{\mu}$ \textbf{@ 5 MHz} ($m^{-1}$) \\
		\hline\hline
		\textbf{PMMA} & $(16\pm 1)$ & $(43\pm 3)$ \\
	\end{tabular}
	\caption{Results of the non-linear fit carried out in the software QtiPlot. The unit of the attenuation coefficient $\mu$ has to be m$^{-1}$ due to the fact, that the exponent of $e$ has to be uniform and the distance has the unit m. This leads to the following: $\si{m}^{-1}\cdot \si{m} = 1$.}
	\label{tab:Attenuation_Coefficient_of_PMMA}
\end{table}

To obtain these results, non-linear regression was used. The necessary exponential relationship of the absorption is shown in equation \ref{eq:absorption}. This relationship was used in QtiPlot to create the fit shown in figure \ref{fig:Attenuation_Coefficient_of_PMMA}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{Attenuation_Coefficient_of_PMMA}
	\caption{Semi-log plot of the amplitude $A$ in function of the distance $x$ which shows the attenuation of the signal in PMMA. The black and red line represent the exponential fit of the attenuation. The fits look like a linear function because of the logarithmic y-axis. Furthermore, the attenuation at a lower frequency is less than at a higher frequency, just as expected (see chapter \ref{subsec:Absorption}). Noticeable is the last red measurement value which seems to be off by quite a bit. This is probably due to the small amplitude which leads to measurement inaccuracies.}
	\label{fig:Attenuation_Coefficient_of_PMMA}
\end{figure}

\newpage
%-----------------------------------------------------------------------------------
\subsection{Longitudinal Ultrasound Velocities in Liquids}
\label{subsec:Longitudinal_Ultrasound_Velocities_in_Liquids}
Table \ref{tab:Longitudinal_Ultrasound_Velocities_in_Liquids} shows the results of the fits obtained from QtiPlot. The longtidunial ultrasound velocities $c_L$ differ quite bit from each other. In other words, the temperature of a liquid such as water is not negligible when doing calculations with sound waves.

\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c}
		& $\boldsymbol{c_L}$ \textbf{@ 5 MHz} ($\frac{\si{m}}{\si{s}}$) & \textbf{Literature Value} ($\frac{\si{m}}{\si{s}}$) \cite{kohlrausch} \\
		\hline\hline
		\textbf{Water 21 \textdegree C} & $(1488\pm 3)$ & $\approx 1483$ \\
		\textbf{Water 49 \textdegree C} & $(1538\pm 5)$ & $\approx 1540$ \\
		\textbf{Saltwater 40 \textdegree C} & $(1612\pm 6)$ & $\approx 1620$ \\
	\end{tabular}
	\caption{Measured longitudinal ultrasound velocities $c_L$ in water and saltwater at different temperatures. The saltwater has a salt concentration of approx. 10 \% (90 g salt / 0.901 L). Furthermore, the literature value of $c_L$ is stated.}
	\label{tab:Longitudinal_Ultrasound_Velocities_in_Liquids}
\end{table}

Figure \ref{fig:Longitudinal_Ultrasound_Velocity_of_Water} shows an example plot from QtiPlot for destilled water with a temperature of 21 \textdegree C. Solely an oscillation frequency of 5 MHz was used to obtain all measurements.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{Longitudinal_Ultrasound_Velocity_of_Water}
	\caption{This plot shows the linear fit and the discrete measurement values for destilled water with a temperature of 21 \textdegree C. The time of flight was measured in function of the travelled distance. The measurements were taken with no linear relationship to each other (not always the same $\Delta x$ between to meassured values). The statistical error is quite low, which means the measured values must match each other really well.}
	\label{fig:Longitudinal_Ultrasound_Velocity_of_Water}
\end{figure}

\newpage
%-----------------------------------------------------------------------------------
\subsection{Transversal Ultrasound Velocity in PMMA}
\label{subsec:Transversal_Ultrasound_Velocity_in_PMMA}
A special transversal transducer was used to measure the transverse time of flight of a sound wave in PMMA. An oscillation frequency $f$ of 1 MHz was used to obtain the measurement results. Table \ref{tab:Transversal_Ultrasound_Velocity_in_PMMA} shows the calculated transversal ultrasound velocity $c_T$ for PMMA.

\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c}
		& $\boldsymbol{c_T}$ \textbf{@ 1 MHz} ($\frac{\si{m}}{\si{s}}$) & \textbf{Literature Value} ($\frac{\si{m}}{\si{s}}$) \cite{kohlrausch} \\
		\hline\hline
		\textbf{PMMA (Transversal)} & $(1405\pm 14)$ & $\approx 1350$ \\
	\end{tabular}
	\caption{To measure the transversal time of flight, a special kind of ultrasound shear gel had to be used in conjunction with a special transducer. The transducer was manufactured to be used at an oscillation frequency of 1 MHz.}
	\label{tab:Transversal_Ultrasound_Velocity_in_PMMA}
\end{table}

Only two measurments were obtained, but it is possible to use $0$ as another measurement value. This is due to the fact that at a distance of $0$ the time of flight will always be $0$ as well. Figure \ref{fig:Transversal_Ultrasound_Velocity_of_PMMA} shows the plot obtained from QtiPlot.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{Transversal_Ultrasound_Velocity_of_PMMA}
	\caption{Measurement values of the transversal time of flight $\Delta t$ in PMMA in function of the distance $x$. With only two measurement values QtiPlot obviously can not calculate the statistical error. Due to this, an additional measurement value at a distance of $0$ m was set to $0$ s. Furthermore, linear regression was used to fit the red linear function.}
	\label{fig:Transversal_Ultrasound_Velocity_of_PMMA}
\end{figure}


\listfiles
\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{listings}
\usepackage{xcolor}

\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclareMathOperator{\cl}{cl}
\DeclareMathOperator{\spn}{span}
\DeclareMathOperator{\E}{E}
\DeclareMathOperator{\St}{St}
\def\Z{\mathbb{Z}}
\def\N{\mathbb{N}}
\def\R{\mathbb{R}}
\def\Q{\mathbb{Q}}
\def\K{\mathbb{K}}
\def\T{\mathbb{T}}
\def\B{\mathcal{B}}
\def\XX{\mathfrak{X}}
\def\YY{\mathfrak{Y}}
\def\AA{\mathfrak{A}}
\def\ZZ{\mathfrak{Z}}
\def\BB{\mathcal{B}}
\def\UU{\mathcal{U}}
\def\MM{\mathcal{M}}
\def\M{\mathfrak{M}}
\def\l{\lambda}
\def\L{\Lambda}
\def\<{\langle}
\def\>{\rangle}

\newcommand{\todo}[1]{{\leavevmode\color{orange}#1}}

\usepackage[a4paper,margin=1in]{geometry}

\setlength{\parindent}{0cm}
\setlength{\parskip}{1em}

\title{Zero-Knowledge Proofs}
\date{}

\begin{document}
\maketitle

\section*{Honesty in the Digital World}

In the analogue world, there exists devices called lie detectors. Their efficiency is based on a premise that when a person is lying, they exhibit signs such as sweating and an increased heart rate, which can be detected.

The field of zero-knowledge proofs is an attempt to build a protocol that behaves like a lie detector, but in the digital realm. Suppose Alice wishes to prove to Bob that she is rich. She could send a bank statement to Bob to prove this; however, by doing so, Bob learns not only the fact that Alice is rich, but other potentially sensitive data such as her income sources and spending habits. A zero-knowledge proof would allow Bob to learn only of the facts that he needs to learn, and nothing else.

\subsection*{Definitions}

The founding paper of this field was TBD. They gave three criteria a proof system should satisfy to be a zero-knowledge proof system. We will define a ``proof system" more carefully later, but for now we can think of it as a system for a \textit{prover} to convince a \textit{verifier} of the truth of some \textit{statement}.

\begin{enumerate}
\item{Completeness.} If the statement is true, then the prover can convince the verifier.
\item{Soundness.} If the statement is false (i.e. the prover is ``cheating"), the prover cannot convince the verifier.
\item{Zero-Knowledge.} The verifier, even if cheating, does not learn any information except for the validity of the statement.
\end{enumerate}

\subsection*{Applications}

TBD. digital signature, election fraud, mix-nets, verifiable outsourced computation, ring/group signatures, zerocoin, malicious to honest-but-curious, lie prevention, proof-carrying data

\section*{Zero-knowledge Proof Systems
}

First, we revisit and try to make more precise our definition of a zero-knowledge proof system from section TBD. To do this, we first review one definition of the complexity class $NP$ (nondeterministic polynomial-time); it turns out that the definition of an interactive proof system will be a modification to the proof systems used in $NP$.

TBD.

\subsection*{A Zero-Knowledge Proof System for Graph Isomorphism}

Let us define a proof system for graph isomorphism. For concreteness, we encode our graphs by labelling the vertices with consecutive natural numbers, and giving the graph as an edgelist. Hence, the following two graphs (which happen to be isomorphic) are encoded as TBD.

The decisional graph isomorphism problem is: given two graphs $G_0$ and $G_1$, determine if $G_0$ and $G_1$ are isomorphic. It is easy to see that this problem is in $NP$: the witness is an isomorphism between the graphs, or more concretely a permutation of the vertices, encoded as a list of $|V|$ integers.

Now let us describe an interactive proof system for this problem. The prover is trying to prove that $G_0$ and $G_1$ are isomorphic, and furthermore, that he possesses the isomorphism $G_0 \leftrightarrow G_1$ denoted by $w$. The interactive proof consists of $r$ \textit{rounds}, and each round consists of three \textit{steps}. In the first step, the prover randomly permutes the vertices of $G_0$ to obtain a graph $H$, and sends $H$ over. In the second step, the verifier sends a single bit $b \in \{0, 1\}$. In the third step, the prover sends an isomorphism between $G_b$ and $H$.

The verifier rejects if, at any round, he fails to verify the isomorphism, and accepts if after $r$ rounds he has not rejected.

\textbf{Exercise}: show that this proof system is complete, sound and zero-knowledge.

Completeness. We will show that in the case that the statement is true (the prover really possesses a witness $w$), he will convince an honest verifier. We can show this for every round. In the first step, let the isomorphism between $G_0$ and $H$ be $h$. Suppose the verifier sends $b = 0$; then the prover can simply send $h$. Otherwise, if the verifier sends $b = 1$, then the prover can simply send $h \circ w$, which is an isomorphism between $H$ and $G_1$.

\textbf{Soundness}: Suppose the graphs are not isomorphic, and the cheating verifier is trying to convince an honest prover. The verifier sends a graph $H$ which can be isomorphic to $G_0$ or $G_1$, but not both. Let $H$ be not isomorphic to $G_c$ for $c \in \{0, 1\}$. With probability at least $\frac{1}{2}$ the verifier will choose $b = c$ and the prover cannot possibly produce an isomorphism between $G_b$ and $H$ in the last step.

With $r$ rounds, the chance that a cheating prover is not caught in $2^{-r}$.

\textbf{Zero-Knowledge}: We assume an arbitrary verifier, who might be crafting his queries to try to learn something about $w$, interacts with an honest prover. It is clear that in the case that the verifier is honest, he learns nothing about $w$, since at each round the verifier learns either

\begin{enumerate}
\item{A random isomorphism $G_0 \sim H$, or}
\item{$w$ composed with a random isomorphism $G_0 \sim H$.}
\end{enumerate}

This is equivalent to sampling from the space of random isomorphisms of $G_0$ $r$ times, where each sample is independent, and this distribution does not depend on $w$, the protocol is zero-knowledge.

However, this proof is not sufficient; \todo{there are protocols where an honest verifier learns nothing, but a dishonest one does! TBD.} Reference: Theorem 2 of GMW.

\subsection*{Removing Interactivity}

The proof system presented above is interactive, that is, the messages that a verifier and prover send at various steps depend on previous messages. \todo{Example of a non-public-coin interactive proof system?} It is also \textbf{public-coin}, that is, an honest verifier sends uniformly random challenges. \todo{Example of an interactive, non-public-coin proof system?}

In a public-coin protocol, if there is a randomness source (e.g. the NIST random beacon) trusted by both parties, then the verifier can use that instead of generating his own randomness. If the challenges are big enough, the Fiat-Shamir heuristic allows one to derive a non-interactive proof system from a public-coin interactive proof system under some conditions. The heuristic is to use a collision-resistant hash function to deterministically generate the random challenges. The prover computes a transcript where the verifier's challenges are replaced by the output of the hash function.

\subsection*{Statements}

The protocol above allows a prover to convince a verifier about statements of the form ``this graph is isomorphic to that one" in zero-knowledge. Now, we try to formalize the notion of a ``statement" so we can generalize it.

TBD.

\subsection*{Arguments vs Proofs}

\subsection*{AC-SAT}

\section*{Efficient ZKPs based on the Discrete Log Problem}

\section*{Linear-Time ZKPs for AC-SAT}

\section*{Pairing-Based SNARKs}

\end{document}


\documentclass[../main.tex]{subfiles}
\begin{document}
The research consists of 3 objectives:
\begin{itemize}
	\item \textit{Optimization:} Determine the parcel's optimal beacon range.
	\item \textit{Discovery:} Determine whether exchanging parcels improves the solution.
	\item \textit{Discovery:} Determine how the delivery strategy improves the solution.
\end{itemize}
To determine whether the solution is improved, we make use of the cost function used by Gendreau et al. This function rates the solution based on a trade-off between operational costs and customer satisfaction. \cite{gendreau2006neighborhood}
$$
C = \sum_{k \in M}{T_k} + \alpha \sum_{v \in V} \text{max} \left\{ 0, t_v - l_v \right\} + \beta \sum_{k \in M} \text{max} \left\{0, \bar{t_k} - l_0 \right\}
$$
With $T_k$ the total travel time on route $R_k$ and $\alpha$I and $\beta$ weighting parameters. In order of appearance the terms symbolize the following values:
\begin{enumerate}
	\item The total travel time on route
	\item The sum of lateness over all pick-up and delivery locations
	\item The sum of overtime over all vehicles
\end{enumerate}

\subsubsection{Independant Variables}
\begin{enumerate}
	\item $c$ radius of circle in which contact with the parcel can be established.
	\item Making use of exchanging parcels between agents or not.
	\item The used delivery strategy.
\end{enumerate}

\end{document}


\section{Bias of OLS estimators}


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

  \section{Demographics}
    A measure $\omega_{1,t}$ of individuals with heterogeneous working ability $e \in\mathcal{E}\subset\mathbb{R}_{++}$ is born in each period $t$ and live for $E+S$ periods, with $S\geq 4$.\footnote{Theoretically, the model exposition of the model works without loss of generality for $S\geq 3$. However, because we are calibrating the ages outside of the economy to be one-fourth of $S$ (e.g., ages 21 to 100 in the economy, and ages 1 to 20 outside of the economy), we need $S$ to be at least 4.} The population of age-$s$ individuals in any period $t$ is $\omega_{s,t}$. Households are termed ``youth'', and do not participate in market activity, during ages $1\leq s\leq E$. The households enter the workforce and economy in period $E+1$ and remain in the workforce until they unexpectedly die or live until age $s=E+S$.\footnote{We model the population with households age $s\leq E$ outside of the workforce and economy in order most closely match the empirical population dynamics.} The population of agents of each age in each period, $\omega_{s,t}$, evolves according to the following function,
    \begin{equation}\label{EqPopLawofmotion}
      \begin{split}
        \omega_{1,t+1} &= \sum_{s=1}^{E+S} f_s\omega_{s,t}\quad\forall t \\
        \omega_{s+1,t+1} &= (1 + i_s - \rho_s)\omega_{s,t}\quad\forall t\quad\text{and}\quad 1\leq s \leq E+S-1
      \end{split}
    \end{equation}
    where $f_s\geq 0$ is an age-specific fertility rate, $i_s$ is an age-specific immigration rate, $\rho_s$ is an age specific mortality hazard rate,\footnote{The parameter $\rho_s$ is the probability that a household of age $s$ dies before age $s+1$.} and $1+i_s-\rho_s$ is constrained to be nonnegative. The total population in the economy $N_t$ at any period is simply the sum of individuals in the economy, the population growth rate in any period $t$ from the previous period $t-1$ is $g_{n,t}$, $\tilde{N}_t$ is the working age population, and $\tilde{g}_{n,t}$ is the working age population growth rate in any period $t$ from the previous period $t-1$.
    \begin{equation}\label{EqPopDef}
      N_t\equiv\sum_{s=1}^{E+S} \omega_{s,t} \quad\forall t
    \end{equation}
    \begin{equation}\label{EqPopGrowth}
      g_{n,t+1} \equiv \frac{N_{t+1}}{N_t} - 1 \quad\forall t
    \end{equation}
    \begin{equation}\label{EqPopWkDef}
      \tilde{N}_t\equiv\sum_{s=E+1}^{E+S} \omega_{s,t} \quad\forall t
    \end{equation}
    \begin{equation}\label{EqPopWkGrowth}
      \tilde{g}_{n,t+1} \equiv \frac{\tilde{N}_{t+1}}{\tilde{N}_t} - 1 \quad\forall t
    \end{equation}

  \section{Households}
  Consumer's are forward-looking, intertemporal optimizers.  Their objective is the maximize the expected, discounted value of lifetime utility.  Expectations are taken over mortality risk, the only source of uncertainty in the model.  Individuals are heterogenous with repeat to age and lifetime income group.  We define the expected, discounted lifetime utility at time $t$ for an individual in lifetime income group $j$ and age $s$ to be $U_{j,s,t}$.  We assume that utility is additively separable across periods and thus write expected, discounted lifetime utility as:
    \begin{equation}\label{EqUtilMax}
      \begin{split}
        &U_{j,s,t} = \sum_{u=0}^{E+S-s}\beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right] u\left(c_{j,s+u,t+u},n_{j,s+u,t+u},b_{j,s+u+1,t+u+1}\right) \\
        &\text{where}\quad \rho_{s-1}=0 \\
        &\text{and} \quad u\left(c_{j,s,t},n_{j,s,t},b_{j,s+1,t+1}\right) = \frac{\left(c_{j,s,t}\right)^{1-\sigma} - 1}{1-\sigma} ... \\
        &\qquad\qquad + e^{g_y t(1-\sigma)}\chi^n_s\left(b\left[1 - \left(\frac{n_{j,s,t}}{\tilde{l}}\right)^\upsilon\right]^\frac{1}{\upsilon} + k\right) + \rho_s\chi^b\frac{\left(b_{j,s+1,t+1}\right)^{1-\sigma} - 1}{1-\sigma} \\
        &\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\forall j,t\quad\text{and}\:E+1\leq s\leq E+S
      \end{split}
    \end{equation}

\noindent\noindent The parameter $\beta\in(0,1)$ represents the individual's rate of time preference.  The quantities $c_{j,s,t}$, $n_{j,s,t},$ and $b_{j,s,t}$ are total consumption of a composite consumption good, labor supply, and asset holdings, respectively.  The parameter $\sigma \geq 1$ is the coefficient of relative risk aversion, $\upsilon$ is a measure of the elasticity of labor supply, and $\tilde{l}$ is the total time endowment of the individual.  The utility weight for the disutility of labor is given by the age-dependent parameters $\chi^{n}_{s}$.  The parameter $g_y$ is a constant growth rate of labor augmenting technological progress, which we explain in more detail in the firm's problem .\footnote{The term with the growth rate $e^{g_y t(1-\sigma)}$ must be included in the period utility function because consumption and bequests will be growing at rate $g_y$ and this term stationarizes the individual Euler equation by making the marginal disutility of labor grow at the same rate as the marginal benefits of consumption and bequests.  This is the same balanced growth technique as that used in \citet{MertensRavn:2011}.}  The disutility of labor term in the utility function looks nonstandard, but is simply the upper quadrant of an ellipse that closely approximates the standard constant relative risk aversion utility of leisure functional form.\footnote{Appendix \ref{AppEllipseUtil} describes how the elliptical function closely matches the more standard utility of leisure of the form $\frac{(\tilde{l}-n_{j,s,t})^{1-\eta} - 1}{1-\eta}$. The parameters $b$ and $k$ are the scale and shift parameters of describing the elliptical form.  This elliptical utility function forces an interior solution that automatically respects both the upper and lower bound of labor supply, which greatly simplifies the computation of equilibrium.  For a more in-depth discussion see \citet{EvanPhillips:2015}} The utility weight on bequests (both intentional and accidental) is given by $\chi^{b}$.

 \begin{figure}[htb]\centering \captionsetup{width=4.0in}
\caption{\label{fig:hh_tree}\textbf{Summary of the Individual Problem}}
\begin{tikzpicture}
\tikzstyle{every node}=[auto,every node/.style={rectangle,draw, text centered, text width=1.3cm,minimum height=0.8cm },node distance=3cm]
\tikzset{%
level 1/.style={sibling distance = 2cm, level distance=3cm,edge from parent path={(\tikzparentnode.south) -- (\tikzchildnode.north)}},
level 2/.style={sibling distance = 1cm,level distance=3cm}
%level 4/.style={sibling distance = 1.1cm,level distance=3cm}
}
  \node (0){$U_{j,s,t}$}
    child {node (1) {$\tilde{c}_{j,s,t}$}
    child {node {}}
    child {node {}}
    child {node {}}
    child {node (2){$c_{i,j,s,t}, i=1,...,I$}
child {node (3) {}}
            child {node {}}
            child {node {}}
            child {node {$X_{m,t}, m=1,..,M$}
             child {node {$X_{m,c,t} \ \ $}}
             child {node {$\ \ X_{m,nc,t}$}}}
             child {node {}}
                        }
                  }
child {node {$n_{j,s,t}$}}
child {node {$bq_{j,s,t}$}} ;

\node at (0) [xshift=+2.6cm,right,draw=none, text width=6cm]{Utility, $U_{j,s,t}$, is a CRRA function of consumption, leisure, and bequests};
\node at (1) [xshift=+4.5cm, right,draw=none, text width=6cm]{Composite consumption, $\tilde{c}_{j,s,t}$, is a Stone-Geary function of distinct consumption goods};
\node at (2) [xshift=+3.0cm, right,draw=none, text width=6cm]{Consumption goods, $c_{i,j,s,t}$, are determined by a fixed coefficient mix of production goods };
\node at (3) [xshift=+5.0cm, right,draw=none, text width=6cm]{Production goods for each industry, $X_{m,t}$, are determined by a CES function of production goods from each sector};
\end{tikzpicture}
\end{figure}

    Households choose consumption of a composite consumption good, $c_{j,s+u,t+u}$, labor supply, $n_{j,s+u,t+u},$ and asset holdings, $b_{j,s+u+1,t+u+1}$, to maximize the expected, discounted, lifetime utility subject to their per-period budget constraint.  Total consumption of the composite good is made up of discretionary consumption, $\tilde{c}_{j,s,t}$, and minimum required purchases of each consumption good, $\bar{c}_{i,s}$.  Thus the consumer's choice is over $\tilde{c}_{j,s,t}$, which together with the minimum required purchases equal determine total composite consumption: $c_{j,s,t}=\tilde{c}_{j,s,t}+\sum_{i=1}^{I}c_{i,s}$.  It is therefore the case that there minimum required purchases affect the household's ability to smooth consumption over time.  We discuss the composite consumption good in more detail in Section \ref{sec:subutil}.  This composite good is age dependent, thus the price of the composite consumption good varies with age $s$.  We denote the gross-of-tax price of the composite consumption good for households of age $s$ in period $t$ as $\tilde{p}_{s,t}$ and the gross-of-tax price for good $i$ at time $t$ as $p_{i,t}$. The households' per period budget constraint is:

    \begin{equation}\label{EqBC}
      \begin{split}
        \sum_{i=1}^{I} p_{i,t}\bar{c}_{i,s} + \tilde{p}_{s,t}\tilde{c}_{s,t} + b_{j,s+1,t+1} \leq \left(1 + r_t\right) b_{j,s,t} + w_t e_{j,s}&n_{j,s,t} + \frac{BQ_{j,t}}{\lambda_j\tilde{N}_t} - T_{j,s,t} \\
        \quad\text{where}\quad b_{j,s,1} = 0 \\
        &\text{for} \quad E+1\leq s \leq E+S \quad \forall j,t
      \end{split}
    \end{equation}

\noindent\noindent Here, $r_{t}$ and $w_{t}$ are the real interest rate and the wages rate on a unit of effective labor.  The variable $e_{j,s}$ denotes the effective labor units of an individual from lifetime income group $s$ and age $j$.  An individual's labor income is thus determined by her choice of $n_{j,s,t}$ units of labor times her measure of effective labor units, $e_{j,s}$, times the wage per unit of effect labor.  An individuals effective labor units vary over the life-cycle, as the age subscript implies.  $BQ_{j,t}$ denote aggregate bequests left from those in lifetime income group {j} at time {t}.  We divide this number by the number of individuals in lifetime income group $j$ at time $t$, given by $\lambda_{j}\tilde{N}_{t}$, to determine the amount of bequests received by each household in lifetime income group $j$.\footnote{This distribution of bequests is just place holder. The goal is to find suitable data to calibrate the process describing the transmission of bequests between individuals of different ages and lifetime income groups.}  The last term in the budget constraint, $T_{j,s,t}$ are total taxes paid by the individual.  These include all non-consumption taxes and are based on tax functions for separate tax sources that we estimate based on a microsimulation model.  We discuss the parameterization and calibration of these functions below.

    The Lagrangian for the individual's problem can be written as:
     \begin{equation}\label{eqn:hh_prob_lagrangian}
      \begin{split}
     \mathcal{L} =  \max_{\left\{ \substack{\tilde{c}_{j,s+u,t+u},\\\ {n}_{j,s+u,t+u},\\\ {b}_{j,s+u,t+u}}\right\}_{u=0}^{E+S-s}}  &  \sum_{u=0}^{E+S-s}\beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right]  \frac{\left(c_{j,s+u,t+u}\right)^{1-\sigma} - 1}{1-\sigma} + ...\\
  &   e^{g_y t(1-\sigma)}\chi^n_s+u\left(b\left[1 - \left(\frac{n_{j,s+u,t+u}}{\tilde{l}}\right)^\upsilon\right]^\frac{1}{\upsilon} + k\right) + \rho_s\chi^b\frac{\left(b_{j,s+u+1,t+u+1}\right)^{1-\sigma} - 1}{1-\sigma}  +... \\
       & \lambda_{j,s+u,t+u}\left\{ (1+r_{t+u})b_{j,s+u,t+u} + w_{t+u}e_{j,s+u}n_{j,s+u,t+u}+\frac{BQ_{j,t+u}}{\lambda_{j}\tilde{N}_{t+u}}-T_{j,s+u,t+u} -.... \right.\\
       & \left. \sum_{i=1}^{I}p_{i,t+u}\bar{c}_{i,s+u}-\tilde{p}_{s+u,t+u}\tilde{c}_{j,s+u,t+u} -b_{j,s+u+1,t+u+1} \right\}
        \end{split}
    \end{equation}

\noindent\noindent taking derivatives with respect to $\{\tilde{c}_{j,s,t},n_{j,s,t+u},b_{j,s,t+1}\}$ gives us the necessary conditions for each $j,s$ and $t$.  The necessary condition with respect to the discretionary consumption of the composite consumption good, $\tilde{c}_{j,s+u,t+u}$, labor supply, $n_{j,s+u,t+u}$, and asset holdings, $b_{j,s+u+1,t+u+1}$, are:

    \begin{equation}\label{Eqcfoc}
      \begin{split}
     \frac{\partial U}{\partial \tilde{c}_{j,s+u,t+u}}  = \beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right] c_{j,s+u,t+u}^{-\sigma} - \beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right]  \lambda_{j,s+u,t+u} \tilde{p}_{s+u,t+u} = 0, \forall u
        \end{split}
    \end{equation}

    \begin{equation}\label{Eqnfoc}
      \begin{split}
      \frac{\partial U}{\partial n_{j,s+u,t+u}} & = \beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right] e^{g_y (t+u)(1-\sigma)}\chi^n_{s}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,s+u,t+u}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,s+u,t+u}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} \\
      & -  \beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right]\lambda_{j,s+u,t+u} \left( w_{t+u} e_{j,s+u} - \frac{\partial T_{j,s+u,t+u}}{\partial n_{j,s+u,t+u}} \right)= 0, \forall u
        \end{split}
    \end{equation}

    \begin{equation}\label{Eqbfoc}
      \begin{split}
      \frac{\partial U}{\partial b_{j,s+u+1,t+u+1}} & = \beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right] \rho_s\chi^b\bigl(b_{j,s+u+1,t+u+1}\bigr)^{-\sigma} - \beta^u\left[\prod_{v=s-1}^{s+u-1}(1-\rho_v)\right] \lambda_{j,s+ut+u}  \\\
      & + \beta^{u+1}\left[\prod_{v=s-1}^{s+u}(1-\rho_v)\right] \lambda_{j,s+u+1,t+u+1} \left( 1 + r_{t+u+1} - \frac{\partial T_{j,s+u+1,t+u+1}}{\partial b_{j,s+u+1,t+u+1}} \right)= 0, \forall u
      \end{split}
    \end{equation}

 Note that the term $\frac{\partial T_{j,s+u+1,t+u+1}}{\partial n_{j,s+U+1,t+u+1}}$ give the change in total taxes for additional labor supply $\frac{\partial T_{j,s+u+1,t+u+1}}{\partial b_{j,s+U+1,t+u+1}}$ gives the change in total taxes for additional savings.  The tax functions that define the total taxes paid will take into account the interactions, for example how increasing capital income by saving more impacts the marginal tax rate on labor income in a system that progressively taxes labor income.  Rearranging the equations above to solve each for $\lambda_{t+u}$, we get the following:
    \begin{equation}
      \begin{split}
      \lambda_{j,s+u,t+u} = \frac{c_{j,s+u,t+u}^{-\sigma}}{\tilde{p}_{s,t+u}} \nonumber
      \end{split}
    \end{equation}

    \begin{equation}
      \begin{split}
      \lambda_{j,s+u,t+u} = \frac{e^{g_y (t+u)(1-\sigma)}\chi^n_{s}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,s+u,t+u}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,s+u,t+u}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}}}{ w_{+u} e_{j,s+u} - \frac{\partial T_{j,s+u,t+u}}{\partial n_{j,s+u,t+u}} }  \nonumber
      \end{split}
    \end{equation}

    \begin{equation}
      \begin{split}
      \lambda_{j,s+u,t+u} = \rho_s\chi^b\bigl(b_{j,s+u+1,t+u+1}\bigr)^{-\sigma} - \beta (1-\rho_{s+u}) \lambda_{t+u+1} \left( 1 + r_{t+u+1} - \frac{\partial T_{j,s+u+1,t+u+1}}{\partial b_{j,s+u+1,t+u+1}} \right)
        \end{split}  \nonumber
    \end{equation}

    These three equations can then be reduced to just two equations that must hold for all $j,s,$ and $t$.  The first relates the marginal utility of consumption of the composite good to the marginal utility of labor:
    \begin{equation}\label{EqcEuler}
      \begin{split}
      & \frac{ c_{j,s+u,t+u}^{-\sigma}}{\tilde{p}_{s+u,t+u}} \\
      & = \frac{ e^{g_y (t+u)(1-\sigma)}\chi^n_{s}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,s+u,t+u}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,s+u,t+u}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} } { w_{t+u} e_{j,s+u} - \frac{\partial T_{j,s+u,t+u}}{\partial n_{j,s+u,t+u}} }
       \end{split}
    \end{equation}

    \noindent\noindent The second equation is the intertemporal Euler equation for savings, including the utility effects of bequests:
    \begin{equation}\label{EqbEuler}
      \begin{split}
      & \frac{ c_{j,s+u,t+u}^{-\sigma}}{\tilde{p}_{s+u,t+u}} = \rho_s\chi^b\bigl(b_{j,s+u+1,t+u+1}\bigr)^{-\sigma}  + \frac{ \beta(1-\rho_{s+u}) c_{j,s+u+1,t+u+1}^{-\sigma}} {\tilde{p}_{s+u+1,t+u+1}} \times \left( 1 + r_{t+u+1} - \frac{\partial T_{j,s+u+1,t+u+1}}{\partial b_{j,s+u+1,t+u+1}} \right)
      \end{split}
    \end{equation}

    \subsection{Household's Portfolio Problem}\label{sec:portfolio}

Household's are assumed to have constant elasticity of substitution (CES) preferences over debt and equity in their portfolio.  These preferences allows for investor portfolios that are a mix of debt and equity despsite the two assets having differential returns.  Thus, while our model does not include idiosyncratic or aggregate uncertainty, these CES preferences account for the premium paid to the more risky assets through the preference parameters. To match lifecycle portfolio changes, we may consider allowing to the CES preference parameters to vary by age.  Given the CES preferences, the household's total assets, $a_{j,s,t}$ are given by (\textcolor{red}{NOTE that if we include these notations we should change our notation for assets from $b$ to $a$. We need to also think about the notion for equity - I use $e$ below, but we are already using that for effective labor units.}):

\begin{equation}
\label{eqn:ces_port}
a_{j,s,t}= \left[\gamma_{a,s}^{\frac{-1}{\ve_{a,s}}}b_{j,s,t}^{\frac{1+\ve_{a.s}}{\ve_{a,s}}}+(1-\gamma_{a,s})^{\frac{-1}{\ve_{a,s}}}e_{j,s,t}^{\frac{1+\ve_{a,s}}{\ve_{a,s}}}\right]^{\frac{\ve_{a,s}}{1+\ve_{a,s}}}
\end{equation}

The parameter $\ve_{a,s}$ is the elasticity of substitution between bonds and stocks in the asset portfolio of an age $s$ household.  $\gamma_{a,s}$ is the taste parameter in these preferences (\textcolor{red}{I've written this in the most flexible way, where both parameters depend upon age, but perhaps we just want the taste parameter to vary by age the rate of substitution is constant.}).

Since neither firms of governments default in our model, the rate of return on bonds issued by firms and government will all have the same pretax rate of return, $r_{b,t}$.

It's not clear whether firm equity will all have the same return.  It seems that multinationals, with the ability to shift profits, may have higher returns than domestic corporations.  In general, firms will earn economic profits and thus have a rate of return in excess of the risk free return, $r_{b,t}$.  If it is the case that some firms earn greater returns than others, I think we may just assume that households own an diversified equity portfolio of all firms and thus earn the average return on equity.  Thus we'll have either a market return for equity that is the average of the heterogeneous returns across firms or the return that is the same for all firms.  We will denote the pretax market turn on equity as $r_{e,t}$.

We introduce further notation that is the after-tax gross return on the each asset composite.  The after-tax gross return on bonds for a household of age $s$, lifetime income group $j$, and in year $t$ is given by:

\begin{equation}
\rho_{b,j,s,t}=(1+r_{b,t}(1-\tau^{int}(y_{j,s,t})))
\end{equation}

\textcolor{red}{Note that I've again shifted some notation.  We had used $a$ to denote total income for tax purposes, while I use $y$ above (since I've used $a$ for total assets).  Also, I'm writing the marginal tax rate on interest income as a function of total income, $\tau^{int}(y_{j,s,t})$, rather than using the total tax function.  I think this is helpful for expository purposes.  Is it ok to use the marginal rather than the average tax function?  Seems like we could use either interchangeably.  But if we want to use the total tax function, the notation for the above would be: }

\begin{equation}
\rho_{b,j,s,t}=(1+r_{b,t}) - \frac{\partial T_{j,s,t}}{\partial b_{j,s,t}}
\end{equation}

\textcolor{red}{Either way, we'll want to be sure that when we calibrate this functions we account for the mix of tax exempt and taxable interests realized by household and how that changes across age and income group.  The micro simulation model should be able to help us here.}

The after-tax gross return on equity for a household of age $s$, lifetime income group $j$, and in year $t$ is given by:

\begin{equation}
\rho_{e,j,s,t}=(1+r_{e,t}(1-\tau^{cap}(y_{j,s,t})))
\end{equation}

Here, $\tau^{cap}$ will be the tax rate on capital income - some mix of the tax on dividends and capital gains from corporate and non-corporate entities.  Recall that we do not explicitly model capital gains realizations nor do we track dividend issues from each representative firm.  Instead, the return to the firms (both from dividends and capital gains) is put into the per period return on equity, $r_{e,t}$.  Thus the tax rate on these returns must be a weighted average of the taxes on dividends and capital gains, where the weighting is given by data on the share of income from capital gains and dividends by age and income group in the data.

We can now write the gross after-tax return on the households total portfolio:

\begin{equation}
\label{eqn:port_return}
\rho_{a,j,s,t}a_{j,s,t}=\rho_{b,j,s,t}b_{j,s,t}+\rho_{e,j,s,t}e_{j,s,t}
\end{equation}

The optimal portfolio is given by the household choosing bonds and stocks to maximize Equation \ref{eqn:port_return} subject to Equation \ref{eqn:ces_port}.  The Lagrangian for this problem is:

\begin{equation}
\mathcal{L} =  \max_{b_{j,s,t},e_{j,s,t}} \rho_{b,j,s,t}b_{j,s,t} + \rho_{e,j,s,t}e_{j,s,t} + \lambda_{j,s,t}\left(a_{j,s,t} -  \left[\gamma_{a,s}^{\frac{-1}{\ve_{a,s}}}b_{j,s,t}^{\frac{1+\ve_{a.s}}{\ve_{a,s}}}+(1-\gamma_{a,s})^{\frac{-1}{\ve_{a,s}}}e_{j,s,t}^{\frac{1+\ve_{a,s}}{\ve_{a,s}}}\right]^{\frac{\ve_{a,s}}{1+\ve_{a,s}}}\right)
\end{equation}

Since every variable is subscripted by $j,s,t$ we drop these and write the necessary conditions as:

\begin{equation}
\label{eqn:port_foc_b}
\frac{\partial \mathcal{L}}{\partial b}: \rho_{b} = \lambda \gamma_{a,s}^{\frac{-1}{\ve_{a,s}}}b^{\frac{1}{\ve_{a,s}}}\left[\gamma_{a,s}^{\frac{-1}{\ve_{a,s}}}b^{\frac{1+\ve_{a.s}}{\ve_{a,s}}}+(1-\gamma_{a,s})^{\frac{-1}{\ve_{a,s}}}e^{\frac{1+\ve_{a,s}}{\ve_{a,s}}}\right]^{\frac{-1}{1+\ve_{a,s}}}
\end{equation}

\begin{equation}
\label{eqn:port_foc_e}
\frac{\partial \mathcal{L}}{\partial e}: \rho_{e} = \lambda (1- \gamma_{a,s})^{\frac{-1}{\ve_{a,s}}}e^{\frac{1}{\ve_{a,s}}}\left[\gamma_{a,s}^{\frac{-1}{\ve_{a,s}}}b^{\frac{1+\ve_{a.s}}{\ve_{a,s}}}+(1-\gamma_{a,s})^{\frac{-1}{\ve_{a,s}}}e^{\frac{1+\ve_{a,s}}{\ve_{a,s}}}\right]^{\frac{-1}{1+\ve_{a,s}}}
\end{equation}

\begin{equation}
\label{eqn:port_foc_lambda}
\frac{\partial \mathcal{L}}{\partial \lambda}: a = \left[\gamma_{a,s}^{\frac{-1}{\ve_{a,s}}}b^{\frac{1+\ve_{a.s}}{\ve_{a,s}}}+(1-\gamma_{a,s})^{\frac{-1}{\ve_{a,s}}}e^{\frac{1+\ve_{a,s}}{\ve_{a,s}}}\right]^{\frac{\ve_{a,s}}{1+\ve_{a,s}}}
\end{equation}

We can use Equations \ref{eqn:port_foc_b} and \ref{eqn:port_foc_e} to find the ratio of bonds to stocks as (again, suppressing the subscripts):

\begin{equation}
\label{eqn:port_ratio}
\frac{b}{e}= \left(\frac{\rho_{b}}{\rho_{e}}\right)^{\ve_{a,s}}\frac{\gamma_{a,s}}{(1-\gamma_{a,s})}
\end{equation}

Using Equation \ref{eqn:port_ratio} can then be used with \ref{eqn:port_foc_lambda} to find demand for bonds and stocks separately.  The solution should yield (\textcolor{red}{We should work this out here, I had some trouble, but we know what the solution should be...}):

\begin{equation}
\label{eqn:bond_demand}
b_{j,s,t} = \left(\frac{\rho_{b,j,s,t}}{\rho_{e,j,s,t}}\right)^{\ve_{a,s}}\gamma_{a,s}a_{j,s,t}
\end{equation}

\begin{equation}
\label{eqn:equity_demand}
e_{j,s,t} = \left(\frac{\rho_{e,j,s,t}}{\rho_{b,j,s,t}}\right)^{\ve_{a,s}}(1-\gamma_{a,s})a_{j,s,t}
\end{equation}

We can thus find the return to the portfolio as:

\begin{equation}
\label{eqn:port_return}
\rho_{a,j,s,t} = \left(\gamma_{a,s}\rho_{b,j,s,t}^{\ve_{a,s}}+(1-\gamma_{a,s})\rho_{e,j,s,t}^{\ve_{a,s}}\right)^{\frac{1}{1+\ve_{a,s}}}
\end{equation}

\subsubsection{After-tax return differentials}

I don't think we have any problem in that household have different after tax returns.  Each will hold a different portfolio because of this, but none will have corner solutions because of the CES preferences.  So it's not problem if, for example, the after-tax return to stocks exceed the return to bonds.  This just means households hold relatively more stocks.

When considering the firms' problems we do need to think about different household having different after-tax returns to equity.  The solution there will be that the firms just chooses a representative household when thinking about maximizing firm value.  The household will be termed the ``marginal investor".  However, we will need to think a bit about which household in the microsimulation model represents this investor.

    \subsection{Household's Subutility Function}\label{sec:subutil}

    Household preferences over the composite consumption good are modeled as a Stone-Geary function. The aggregate discretionary consumption of the composite good is defined as follows.
    \begin{equation} \label{eqn:comp_cons}
        \tilde{c}_{j,s,t}  = \prod_{i=1}^I \left( c_{i,j,s,t} - \bar c_{i,s} \right) ^{\alpha_{i,s}}
    \end{equation}

Where, $c_{i,j,s,t}$ is consumption of good $i$ by household of type $j$, age $s$, at time $t$.  There are $I$ total goods and $\bar{c}_{i,s}$ represents the minimum consumption amount for each good at each age.  The parameters $\alpha_{i,s}$ are the share parameters (and $\sum_{i=1}^{I} \alpha_{i,s}=1$).  They correspond to the share of income, after minimum expenditure amounts, that are spent on each good at each age.  Allowing the minimum consumption amounts and the share parameters to vary by age helps to incorporate life-cycle profiles of consumption into the model.  For example, we do not explicitly model household formation decisions, but they will be some of the effects of changes in household composition over the life-cycle are obtained through the parameters of the Stone-Geary function.  For example, the minimum required expenditure on shelter may be higher in the middle of the life-cycle when household size is larger.  The minimum consumption amounts also mean that the composition of consumption will vary with income, even though all households have the same utility function.

The consumer chooses $c_{i,j,s,t}$ to maximize Equation \ref{Eqcagg} subject to the budget constraint:

    \begin{equation} \label{eqn:cons_budgetcons}
        \sum_{i=1}^{I} p_{i,t}(c_{i,j,s,t}-\bar{c}_{i,s})  = \tilde{p}_{s,t}\tilde{c}_{j,s,t}
    \end{equation}

\noindent where $p_{i,t}$ is the gross of tax price of good $i$ at time $t$ and $\tilde{p}_{s,t}$ is the gross of tax price of the the discretionary component of the composite consumption good consumed by those of age $s$ at time $t$.  Maximization of \ref{Eqcagg} subject to \ref{eqn:cons_budgetcons} yields:

    \begin{equation} \label{eqn:cons_lagrangian}
       \mathcal{L} =  \max_{\{c_{i,j,s,t}\}_{i=1}^{I}}  \prod_{i=1}^I \left( c_{i,j,s,t} - \bar c_{i,s} \right) ^{\alpha_{i,s}}  + \lambda \left(\tilde{p}_{s,t}\tilde{c}_{j,s,t} - \sum_{i=1}^{I} p_{i,t}(c_{i,j,s,t}-\bar{c}_{i,j,s,t})\right)
    \end{equation}

    Which as $I$ FOCs (for each $j$, $s$, $t$):

      \begin{equation} \label{eqn:cons_FOC}
      \begin{split}
       & \frac{\partial \mathcal{L}}{\partial c_{i,j,s,t}} = \frac{\alpha_{i,s} \prod_{i=1}^I \left( c_{i,j,s,t} - \bar c_{i,s} \right) ^{\alpha_{i,s}}}{(c_{i,j,s,t}-\bar{c}_{i,s})}-\lambda p_{i,t} = 0, \forall \ i  \\
       & \implies  \frac{\alpha_{i,s} \prod_{i=1}^I \left( c_{i,j,s,t} - \bar c_{i,s} \right) ^{\alpha_{i,s}}}{(c_{i,j,s,t}-\bar{c}_{i,s})} = \lambda p_{i,t}, \forall \ i \\
       & \implies  \frac{\alpha_{i,s} \prod_{i=1}^I \left( c_{i,j,s,t} - \bar c_{i,s} \right) ^{\alpha_{i,s}}}{ p_{i,t}(c_{i,j,s,t}-\bar{c}_{i,s})} = \lambda, \forall \ i \\
       & \implies \frac{\alpha_{i,s}}{p_{i,t}(c_{i,j,s,t}-\bar{c}_{i,s})}=\frac{\alpha_{j,s}}{p_{k,t}(c_{k,j,s,t}-\bar{c}_{k,s})}, \forall \ i,k \\
       & \implies c_{i,j,s,t}= \frac{\alpha_{i,s} p_{k,t}(c_{k,j,s,t}-\bar{c}_{k,s})}{\alpha_{k,s} p_{i,t}} + \bar{c}_{i,s} \forall i,k
       \end{split}
    \end{equation}

    Now substitute the last line of \ref{eqn:cons_FOC} into the budget constraint (Equation \ref{eqn:cons_budgetcons}):

          \begin{equation} \label{eqn:cons_solve}
      \begin{split}
       & \tilde{p}_{s,t}\tilde{c}_{j,s,t} = \sum_{i=1}^{I}p_{i,t}(c_{i,j,s,t}-\bar{c}_{i,s}) \\
       & \implies  \tilde{p}_{s,t}\tilde{c}_{j,s,t} = \sum_{i=1}^{I}p_{i,t}\left[ \frac{\alpha_{i,s} p_{k,t}(c_{k,j,s,t}-\bar{c}_{k,s})}{\alpha_{k,s} p_{i,t}} + \bar{c}_{i,s}- \bar{c}_{i,s}\right] \\
       & \implies  \tilde{p}_{s,t}\tilde{c}_{j,s,t} = \sum_{i=1}^{I}\left[ \frac{\alpha_{i,s} p_{k,t}(c_{k,s}-\bar{c}_{k,s})}{\alpha_{k,s}}\right] \\
       & \implies  \tilde{p}_{s,t}\tilde{c}_{j,s,t} = \frac{ p_{k,t}(c_{k,j,s,t}-\bar{c}_{k,s})}{\alpha_{k,s}} \underbrace{\sum_{i=1}^{I}\alpha_{i,s}}_{=1} \\
        & \implies  \tilde{p}_{s,t}\tilde{c}_{j,s,t} = \frac{ p_{k,t}(c_{k,j,s,t}-\bar{c}_{k,s})}{\alpha_{k,s}} \\
        & \implies  \frac{ p_{k,t}(c_{k,j,s,t}-\bar{c}_{k,s})}{\alpha_{k,s}}  = \tilde{p}_{s,t}\tilde{c}_{j,s,t}   \\
        & \implies  c_{k,j,s,t}  = \frac{\alpha_{k,s} \tilde{p}_{s,t}\tilde{c}_{j,s,t}}{p_{k,t}} + \bar{c}_{k,s},  \forall \ k  \\
       \end{split}
    \end{equation}

    Thus, total consumption of each good $i$, $c_{i,j,s,t}$, is given by the the amount of minimum consumption plus the share of total expenditures remaining after making the minimum expenditures on all goods (this is called the ``supernumerary" expenditure).  We derive the prices of the age $s$ composite consumption good in period $t$, $\tilde{p}_{s,t}$ by using the demand for good $i$ provided in Equation \ref{cons_solve} in the function defining aggregate discretionary consumption, Equation \ref{eqn:comp_cons}:

              \begin{equation} \label{eqn:composite_price}
      \begin{split}
      & \tilde{c}_{j,s,t} = \prod_{i=1}^{I}(c_{i,j,s,t}-\bar{c}_{i,s})^{\alpha_{i,s}} \\
      &\implies \tilde{c}_{j,s,t} = \prod_{i=1}^{I}\left( \frac{\alpha_{i,s} \tilde{p}_{s,t}\tilde{c}_{j,s,t}}{p_{i,t}} + \bar{c}_{i,s}-\bar{c}_{i,s}\right)^{\alpha_{i,s}} \\
      &\implies \tilde{c}_{j,s,t} = \prod_{i=1}^{I} \left( \frac{\alpha_{i,s} \tilde{p}_{s,t}\tilde{c}_{j,s,t}}{p_{i,t}} \right)^{\alpha_{i,s}} \\
      &\implies \tilde{c}_{j,s,t} =  \tilde{p}_{s,t}\tilde{c}_{j,s,t} \prod_{i=1}^{I}\left( \frac{\alpha_{i,s}}{p_{i,t}} \right)^{\alpha_{i,s}} \\
      &\implies \frac{\tilde{p}_{s,t}\tilde{c}_{j,s,t}}{\tilde{c}_{j,s,t}} =  \prod_{i=1}^{I}\left( \frac{p_{i,t}}{\alpha_{i,s}} \right)^{\alpha_{i,s}} \\
       &\implies \tilde{p}_{s,t} =  \prod_{i=1}^{I}\left( \frac{p_{i,t}}{\alpha_{i,s}} \right)^{\alpha_{i,s}} \\
       \end{split}
    \end{equation}

    This composite good price is then used in the household's intertemporal optimization problem described in Equation \ref{eqn:hh_prob_lagrangian}.  With the parameters and endogenous variables, we then use \ref{eqn:cons_solve} to find the $c_{i,j,s,t}$.

    \subsection{Relating Consumption and Production Goods}\label{sec:prod_cons_map}

    Our model contains $I$ consumption goods and $M$ production goods.  We denote the quantity of production good $m$ in period $t$ as $X_{m,t}$.  We relate the output of the production sectors and the consumption goods using a fixed coefficient model. That is, we assume each consumption good is made up of a mix of the outputs of different production sectors.  This means that the composition of these consumption goods do not respond to prices. The weights that determine the mix for each consumption goods are given in the matrix $\Pi$.  Element $\pi_{i,m}$ of the matrix $\Pi$ corresponds to the percentage contribute of the output of sector $m$ in the production of good $i$.  The total supply of good $i$ in the economy at time $t$ is thus given by:

             \begin{equation} \label{eqn:mix_cons}
             c_{i,t} = \sum_{m=1}^{M}\pi_{i,m}X_{m,t}
    	\end{equation}

 \noindent\noindent And the price of a unit of consumption good $i$ at time $t$ is:

             \begin{equation} \label{eqn:mix_cons_price}
             p_{i,t} = \sum_{m=1}^{M}\pi_{i,m}p_{m,t},
    	\end{equation}

    \noindent\noindent Where $p_{m}$ is the price of output of production sector $m$ at time $t$.

    \subsection{Preferences for Corporate vs. Noncorporate Goods}\label{sec:pref_corp_noncorp}

    Production sectors may contain corporate and non-corporate producers, each facing different tax treatment.  If the output from corporate and non-corporate entities are perfect substitutes, then if the producers have the same production technology, consumers will end up consuming only the output from the sector with lowest after tax cost of producing.  \citet{GK1989} propose a model where different production sectors use different technologies, which can give rise to an equilibrium where both the corporate and non-corporate sector produce the same good.  We take a different track, following \citet{FR1993} we allow production technologies to vary across industry, but not across sectors within industry.  Both sectors produce output in equilibrium, because output across sectors are not perfect substitutes.  For example, food outside the home from a corporate, chain restaurant chain is not the same as food outside the home from a small, family-owned restaurant.  Specifically, we define consumer preferences such that demand for the composite production good (combing output from the corporate and non-corporate sector) for production sector $m$ at time $t$, $X_{m,t}$, is a constant elasticity of substitution (CES) function of the output from the corporate and non-corporate sectors, $X_{m,t,C}$ and $X_{m,t,NC}$, respectively:

                  \begin{equation} \label{eqn:comp_output}
             X_{m,t} = \left[\gamma_{m}^{\frac{1}{\ve_{3}}}X_{m,t,C}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}X_{m,t,NC}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(\ve_{3}-1)}},
    	\end{equation}

	\noindent where $\ve_{3}$ is the elasticity of substitution between corporate and non-corporate output and is assumed to be constant across industries.  The share parameter in the CES function, $\gamma_{m}$ is allowed to vary across industry and will be identified by the fraction of corporate produced output across industries.  The CES function thus explains the existence of corporate and non-corporate production within each industry as well as the different shares of corporate output across industries.  Because of these preferences, changes in corporate and non-corproate tax treatment will have differential impacts across consumers of different ages and income levels.
	Consumers choose $X_{m,t,C}$ and $X_{m,t,NC}$ to maximize \ref{eqn:comp_output} subject to:

	 \begin{equation} \label{eqn:comp_output_cons}
             p_{m,t}X_{m,t} = p_{m,t,C}X_{m,t,C}+p_{m,t,NC}X_{m,t,NC},
    	\end{equation}

\noindent where $p_{m,t,C}$ and $p_{m,t,NC}$ are the prices of output from the corporate and non-corporate firms in production industry $m$, respectively.  Note that these prices are determined through the firm's profit maximization problem and the zero economic profit condition for firms. The constrained optimization problem consumers face is:

 \begin{equation} \label{eqn:comp_output_lagrangian}
	\begin{split}
	 \mathcal{L} = \left[\gamma_{m}^{\frac{1}{\ve_{3}}}X_{m,t,C}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}X_{m,t,NC}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(\ve_{3}-1)}} + \lambda_{m,t,C}\left(p_{m,t}X_{m,t} - p_{m,t,C}X_{m,t,C}+p_{m,t,NC}X_{m,t,NC}\right)
  	\end{split}
\end{equation}

    FOCs are:

\begin{equation} \label{eqn:comp_output_foc_C}
	\begin{split}
       	&  \frac{\partial \mathcal{L}}{\partial X_{m,t,C}} = \gamma_{m}^{\frac{1}{\ve_{3}}} X_{m,t,C}^{\frac{-1}{\ve_{3}}} \left[\gamma_{m}^{\frac{1}{\ve_{3}}}X_{m,t,C}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}X_{m,t,NC}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{1}{(\ve_{3}-1)}} - \lambda_{m,t,C} p_{m,t,C} = 0
      	 \end{split}
\end{equation}

   and

\begin{equation} \label{eqn:comp_output_foc_NC}
	\begin{split}
       	&  \frac{\partial \mathcal{L}}{\partial X_{m,t,NC}} = (1-\gamma_{m})^{\frac{1}{\ve_{3}}} X_{m,t,NC}^{\frac{-1}{\ve_{3}}} \left[\gamma_{m}^{\frac{1}{\ve_{3}}}X_{m,t,C}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}X_{m,t,NC}^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{1}{(\ve_{3}-1)}} - \lambda_{m,t,C} p_{m,t,NC} = 0
	\end{split}
\end{equation}

    Solving the two necessary conditions, we can find the equations for the demand for the corporate and non-corporate output in industry $m$ as a function of the prices out output from each sector of industry $m$, price of the composite production good, the demand for the composite production good, and the parameters:

\begin{equation} \label{eqn:demand_XmtC}
	X_{m,t,C} = \frac{\gamma_{m}p_{m,t}X_{m,t}}{p_{m,t,C}^{\ve_{3}}\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}
\end{equation}

    and

    \begin{equation} \label{eqn:demand_XmtNC}
	X_{m,t,NC} = \frac{(1-\gamma_{m})p_{m,t}X_{m,t}}{p_{m,t,NC}^{\ve_{3}}\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}
\end{equation}

To determine $p_{m,t}$, note that the CES subutility function defining preferences over corporate and non-corporate output within a production industry is linearly homogenous.  Because the subutility function is linearly homogenous, we know that the associated indirect utility function is homogenous of degree one in $X_{m,t}$.  Letting $V(\cdot)$ represent the indirect utility function, this means that $V(p_{m,t,C},p_{m,t,NC},\lambda X_{m,t}) = \lambda V(p_{m,t,C},p_{m,t,NC}, X_{m,t})$.  The linear homogeneity of the utility function also means that the indirect utility function is homogenous of degree -1 in prices.  That is, $V(\lambda p_{m,t,C},\lambda p_{m,t,NC}, X_{m,t}) = \frac{V(p_{m,t,C},p_{m,t,NC}, X_{m,t})}{\lambda}$. Linear homogeneity of the utility function means that:

\begin{equation}
V(p_{m,t,C},p_{m,t,NC}, X_{m,t}) = \frac{p_{m,t}X_{m,t}}{e(p_{m,t,C},p_{m,t,NC})},
\end{equation}

\noindent\noindent where $e(p_{m,t,C},p_{m,t,NC})$ is the minimum expenditure for a unit of the composite good given prices.  Rearranging, we have:

 \begin{equation}
 \label{eqn:price_comp}
 \begin{split}
& e(p_{m,t,C},p_{m,t,NC}) = \frac{p_{m,t}X_{m,t}}{V(p_{m,t,C},p_{m,t,NC}, X_{m,t})}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) = p_{m,t}X_{m,t}/ \\
& {\left[\gamma_{m}^{\frac{1}{\ve_{3}}}\left( \frac{\gamma_{m}p_{m,t}X_{m,t}}{p_{m,t,C}^{\ve_{3}}\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}\left(\frac{(1-\gamma_{m})p_{m,t}X_{m,t}}{p_{m,t,NC}^{\ve_{3}}\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(\ve_{3}-1)}}}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) = p_{m,t}X_{m,t}/ \\
& {p_{m,t}X_{m,t}\left[\gamma_{m}^{\frac{1}{\ve_{3}}}\left( \frac{\gamma_{m}}{p_{m,t,C}^{\ve_{3}}\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}\left(\frac{(1-\gamma_{m})}{p_{m,t,NC}^{\ve_{3}}\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(\ve_{3}-1)}}}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) = 1/ \\
& \left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]{\left[\gamma_{m}^{\frac{1}{\ve_{3}}}\left( \frac{\gamma_{m}}{p_{m,t,C}^{\ve_{3}}}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}\left(\frac{(1-\gamma_{m})}{p_{m,t,NC}^{\ve_{3}}}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(\ve_{3}-1)}}}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) =
\frac{\left[\gamma_{m}^{\frac{1}{\ve_{3}}}\left( \frac{\gamma_{m}}{p_{m,t,C}^{\ve_{3}}}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})^{\frac{1}{\ve_{3}}}\left(\frac{(1-\gamma_{m})}{p_{m,t,NC}^{\ve_{3}}}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(1-\ve_{3})}}}{\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) =
\frac{\left[\gamma_{m}\left( \frac{1}{p_{m,t,C}^{\ve_{3}}}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+(1-\gamma_{m})\left(\frac{1}{p_{m,t,NC}^{\ve_{3}}}\right)^{\frac{(\ve_{3}-1)}{\ve_{3}}}+\right]^{\frac{\ve_{3}}{(1-\ve_{3})}}}{\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) =
\frac{\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]^{\frac{\ve_{3}}{(1-\ve_{3})}}}{\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]}\\
&\implies e(p_{m,t,C},p_{m,t,NC}) =
\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]^{\frac{1}{(1-\ve_{3})}}\\
\end{split}
\end{equation}

\noindent\noindent Thus we have the price of the corporate-non-corporate composite good from production industry $m$ at time $t$ as:
\begin{equation}
 e(p_{m,t,C},p_{m,t,NC})=p_{m,t}=\left[\gamma_{m}p_{m,t,C}^{1-\ve_{3}}+(1-\gamma_{m})p_{m,t,NC}^{1-\ve_{3}}\right]^{\frac{1}{(1-\ve_{3})}}
 \end{equation}

%\textcolor{red}{I'm not sure, but I think we then have  $e(p_{m,t,C},p_{m,t,NC})=p_{m,t}$ since the unit of ``utility" is really a unit of output of the composite production good, $X_{m,t}$}.


\documentclass[./Thesis.tex]{subfiles}
\begin{document}

\chapter{Exponentiation}
\label{chap:exponentiation}

\epigraph{
  Accurate reckoning. \\
  The entrance into the knowledge of all existing
  things and all obscure secrets.
}{
  Translated from the Rhind Egyptian Mathematical Papyrus.
  \cite{rhind-papyrus}
}

In this chapter we will investigate formalizing exponentiation and the
algorithms to compute it. As \Agda{} is a proof relevant language, these things
are one and the same \cite{hott-book}.
% TODO: Rewrite
\section{Inductive Definitions}
\label{sec:inductive-definitions}

\begin{code}[hide]
  open import Algebra using (CommutativeMonoid)
  module Exponentiation where
  open import Relation.Nullary.Decidable using (False)
  open import Relation.Nullary.Negation using (contradiction)
  open import AKS.Nat using (ℕ; zero; suc; _+_; _*_; _<_; lte; _≟_)
  open import AKS.Nat using (n%m<m)
  open import Data.Nat.Properties using (*-identityˡ; *-assoc; *-comm)
  open import Relation.Binary.PropositionalEquality using (_≡_; _≢_; module ≡-Reasoning) renaming (refl to ≡-refl; sym to ≡-sym; cong to ≡-cong)
\end{code}

Before formalizing a piece of mathematics its often useful to step
back and think about the desired semantics of your formalism. In this case we
ask the question what are the semantics of exponentiation. Middle and Elementary
students often learn that exponentiation is ``repeated'' multiplication. This
thought is made more precise below:
\begin{align}
  \label{eqn:exp-first-idea}
  x^n = \underbrace{x \times x \times \dots \times x}_{n - 1 \text{ multiplications}}
\end{align}
The equation above lacks parentheses, arbitrarily assigning right precedence
the equation becomes:
\begin{align}
  \label{eqn:exp-second-idea}
  x^n = x \times (x \times (x \times \dots (x \times x) \dots ))
\end{align}
This leads to an obvious inductive definition to compute $x^n$ just multiply $x$
by $x^{n -1}$ with a base case of $x^0 = 1$. This definition cleanly bakes a law
of exponents $x^{1+n} = x^1 \times x^n = x \times x^n$ into itself. This
idea is formalized below. \\

\begin{code}[hide]
  module Basic (C : Set) where
    open ≡-Reasoning
    infixr 8 _^_
\end{code}
\begin{code}
    _^_ : ℕ → ℕ → ℕ
    x ^ zero = 1
    x ^ suc n = x * x ^ n
\end{code} \\

We demonstrate the correctness of this definition by proving one of the
fundamental laws of exponentiation,
the exponentiational homomorphism $x^{n + m} = x^n x^m$.
\begin{code}
    ^-homo : ∀ x n m → x ^ (n + m) ≡ x ^ n * x ^ m
\end{code}
The type above just makes the quantifiers explicit. The base case below
is more interesting. We first simplify and unwrap the definitions then
tack a $1$ onto the exponential. This is allowable as $1$ is the
multiplicative unit. Finally we re-wrap definitions, notice that
$x \string^{} \, 0$ is defined to be $1$ so the simplifier can work in reverse.
\begin{code}
    ^-homo x zero m = begin
      x ^ (0 + m)   ≡⟨⟩
      x ^ m         ≡⟨ ≡-sym (*-identityˡ (x ^ m)) ⟩
      1 * x ^ m     ≡⟨⟩
      x ^ 0 * x ^ m ∎
\end{code}
The inductive step starts similarlly to the base case in so far as we
unfold the defintions. Then we invoke the inductive hypothesis which in \Agda{}
is just a recursive call to our proof. Notice that the induction is well founded
as the natural $n$ is decreasing by one each inductive step.
Lastly we associate the multiplications correctly.
\begin{code}
    ^-homo x (suc n) m = begin
      x ^ (suc n + m)     ≡⟨⟩
      x ^ suc (n + m)     ≡⟨⟩
      x * x ^ (n + m)     ≡⟨ ≡-cong (λ e → x * e) (^-homo x n m) ⟩
      x * (x ^ n * x ^ m) ≡⟨ ≡-sym (*-assoc x (x ^ n) (x ^ m)) ⟩
      (x * x ^ n) * x ^ m ≡⟨⟩
      (x ^ suc n) * x ^ m ∎
\end{code}
\section{Don't Repeat Yourself}
\label{sec:dont-repeat-yourself}
In the previous section we assumed that the base of our exponential was a
natural number. This definition restricts even basic mathematics as expressions
like $(-1)^3$ and $\pi^2$ are not well typed. A mathematician might suggest
using a base of complex numbers as the naturals, integers, and reals are all
subclasses of $\mathbb{C}$. This solution has two problems, firstly subtyping
(the type theoretic analog of subclassing) is an open problem in a language like
\Agda{} \cite{algebraic-subtyping}. Secondly there are types which are not
subclasses of $\mathbb{C}$ that we would like to exponentiate. A notable example
from linear algebra, the $n$th entry of the Fibonacci sequence can be found by
raising a certain matrix to the $n$th power \cite{linear-algebra}.
\begin{align}
  \label{eqn:fib-example}
  \rowarrowsep=-2pt
  \begin{gmatrix}[p]
    F_{n - 1} & F_{n} \\
    F_{n} & F_{n + 1}
  \end{gmatrix}
  =
  {
  \begin{gmatrix}[p]
    0 & 1 \\
    1 & 1
  \end{gmatrix}
  }^n
\end{align}
In the coming chapters we will need to exponentiate non-number types so we need
to step back and generalize our base. In the previous section the proofs
required almost no properties of multiplication. The proofs required an
associative binary operation equipped with an identity element. Those
constraints basically define a monoid. A quick sanity check shows that all the
types listed above form a monoid under their respective multiplication
operators. \\

We can generalize our definition by creating a module that takes any monoid.
Our monoid laws are proven over an arbitrary \textit{setoid}. A setoid is a
packaging of some type $C$ and an equivalence class $\AgdaFunction{\_≈\_}$ over
that type. For instance the
naturals form a setoid with $\AgdaFunction{\_≡\_}$ as the equivalence class.
\begin{code}[hide]
  open import Algebra using (Monoid; CommutativeMonoid)
  open import Relation.Binary using (Setoid)
\end{code}
\begin{code}
  module Exponentiation₁ {c ℓ} (M : Monoid c ℓ) where
    open Monoid M public
      using (ε; _∙_; ∙-cong; ∙-congˡ; ∙-congʳ; identityˡ; identityʳ; assoc)
      using (_≈_; setoid)
      renaming (Carrier to C)
    open Setoid setoid public using (refl; sym)
    open import Relation.Binary.Reasoning.Setoid setoid public
\end{code}
From this point on bases are values of type C, the
binary operation is $∙$, and the identity element is
$\varepsilon$. This leads to a more general inductive definition:
\begin{code}
    _^_ : C → ℕ → C
    x ^ zero = ε
    x ^ suc n = x ∙ x ^ n
\end{code}
We can update the homomorphism proof as well keeping in mind now we are working
over an arbitrary setoid instead of the built in equality type. This is
reflected in the following new type of $\AgdaFunction{\^{}-homo}$.
% TODO: Define setoid
\begin{code}[hide]
    infixr 8 _^_
\end{code}
\begin{code}
    ^-homo : ∀ x n m → x ^ (n + m) ≈ x ^ n ∙ x ^ m
\end{code}
The base case closely resembles the original proof with one change. Adding on
the identity element is only true up to setoid equality. This is
reflected in the combinator used to string the proof steps together.
\begin{code}
    ^-homo x zero m = begin
      x ^ (0 + m)   ≡⟨⟩
      x ^ m         ≈⟨ sym (identityˡ (x ^ m)) ⟩
      ε ∙ x ^ m     ≡⟨⟩
      x ^ 0 ∙ x ^ m ∎
\end{code}
Not all functions are congruent over an arbitrary setoid, so you need to prove
congruence for specific functions. Luckily by definition, all monodial binary
operations are congruent. This fact is witnessed by the
proof generating function $\AgdaFunction{∙-congˡ}$.
\begin{code}
    ^-homo x (suc n) m = begin
      x ^ (suc n + m)     ≡⟨⟩
      x ^ suc (n + m)     ≡⟨⟩
      x ∙ x ^ (n + m)     ≈⟨ ∙-congˡ (^-homo x n m) ⟩
      x ∙ (x ^ n ∙ x ^ m) ≈⟨ sym (assoc x (x ^ n) (x ^ m)) ⟩
      (x ∙ x ^ n) ∙ x ^ m ≡⟨⟩
      (x ^ suc n) ∙ x ^ m ∎
\end{code}
Another core law of exponentiation is the fact that exponentiation distributes
over multiplication or in our case the binary operator
$(x \cdot y)^n = x^n \cdot y^n$. Unfortunately this law is false over
non-commutative monoids. For instance the free monoid, commonly called the list
data structure \cite{awodey}, is a counterexample to this law:
\begin{align}
  \label{eqn:dist-counterexample}
       ([1] \, \diamond \, [2])^2
     = [1, 2]^2
     = [1, 2, 1, 2]
  \neq [1, 1, 2, 2]
     = [1, 1] \, \diamond \, [2, 2]
     = [1]^2 \, \diamond \, [2]^2
\end{align}
This means we need to strengthen our assumptions and work with commutative
monoids. As every commutative monoid is a monoid all our previous work holds
true, and this can be witnessed by passing the correct modules around.
Note that we now have access to the $\AgdaFunction{comm}$ property, a proof that the
binary operator is commutative.
\begin{code}
  module Exponentiation₂ {c ℓ} (M : CommutativeMonoid c ℓ) where
    open CommutativeMonoid M using (monoid; comm) public
    open Exponentiation₁ monoid public
\end{code}
The type and base case for this proof are quite similar to the proof above. Note
that for any monoid $\varepsilon = \varepsilon^2$.
\begin{code}
    ∙-^-dist : ∀ x y n → (x ∙ y) ^ n ≈ x ^ n ∙ y ^ n
    ∙-^-dist x y zero = begin
      (x ∙ y) ^ 0   ≡⟨⟩
      ε             ≈⟨ sym (identityˡ ε) ⟩
      ε ∙ ε         ≡⟨⟩
      x ^ 0 ∙ y ^ 0 ∎
\end{code}
The inductive step contains the meat of this proof. We first apply the inductive
hypothesis then we reassociate the terms until $y$ and $x \,\,\, \hat{} \,\,\, n$ are siblings.
Only then can we apply the commutativity axiom. Note the repeated uses of
$\AgdaFunction{∙-cong}$ to ignore irrelevant parts of expression tree. The proof concludes by
reassociating the terms in reverse.
\begin{code}
    ∙-^-dist x y (suc n) = begin
      (x ∙ y) ^ suc n           ≡⟨⟩
      (x ∙ y) ∙ (x ∙ y) ^ n     ≈⟨ ∙-congˡ (∙-^-dist x y n) ⟩
      (x ∙ y) ∙ (x ^ n ∙ y ^ n) ≈⟨ assoc x y (x ^ n ∙ y ^ n) ⟩
      x ∙ (y ∙ (x ^ n ∙ y ^ n)) ≈⟨ ∙-congˡ (sym (assoc y (x ^ n) (y ^ n))) ⟩
      x ∙ ((y ∙ x ^ n) ∙ y ^ n) ≈⟨ ∙-congˡ (∙-congʳ (comm y (x ^ n))) ⟩
      x ∙ ((x ^ n ∙ y) ∙ y ^ n) ≈⟨ ∙-congˡ (assoc (x ^ n) y (y ^ n)) ⟩
      x ∙ (x ^ n ∙ (y ∙ y ^ n)) ≈⟨ sym (assoc x (x ^ n) (y ∙ y ^ n)) ⟩
      (x ∙ x ^ n) ∙ (y ∙ y ^ n) ≡⟨⟩
      x ^ suc n ∙ y ^ suc n     ∎
\end{code}
We end this section by analyzing the run time of this algorithm. The run time
can be represented by this simple recurrence. Assume that the binary operation
runs in $\BigO{1}$ time\footnote
{In general this assumption is false but we
  will use this algorithm to exponentiate modular rings.
}.
\begin{align}
  \label{eqn:slow-recurrence}
  \begin{split}
    T(0) &= \BigO{1} \\
    T(n) &= T(n - 1) + \BigO{1}
  \end{split}
\end{align}
This recurrence clearly solves to $T(n) = \BigO{n}$. This linear result obscures
the exponential run-time of this algorithm. The key to realize this is to ask
``What does $n$ represent in that recurrence?'' It represents the full integer, not
the length of the integer's binary representation. This insight shows that an inductively
defined exponential algorithm will run exponentially slower then the run-time of the
binary operation.
\section{Exponential Speedup}
\label{sec:exponential-speedup}
The solution to this exponential problem can be found by raising a base to
powers of two. In the following equation assume $n$ is a power of two $n = 2^\ell$:
\begin{align}
  \label{eqn:squaring-even}
  x^n = x^{2^{\, \ell}}
  {}  = x^{2 \cdot 2^{\ell - 1}}
  {}  = \Parens*{x^2}^{2^{\ell - 1}}
  {}  = \Parens*{x^2}^{n / 2}
\end{align}
This equation provides a simple algorithm to exponentiate a base to a power of
two. Just square the base and recurse on half the exponent. The run time of this
algorithm is modeled by the recurrence given below.
\begin{align}
  \label{eqn:power-of-two-recurrance}
  \begin{split}
    T(0) &= \BigO{1} \\
    T(n) &= T(n / 2) + \BigO{1}
  \end{split}
\end{align}
The well known recurrence yields a complexity of $T(n) = \BigO{\Lg{n}}$. This is an
exponential improvement. Unfortunately this algorithm only works correctly for
powers of two. This is because equation \ref{eqn:squaring-even} requires n to be
even. The defining feature of powers of two is that they are all even,
disregarding $1$. So if we can find a parallel equation to equation
\ref{eqn:squaring-even} for odd numbers, we will be able to exponentiate any power
quickly.
\begin{align}
  \label{eqn:squaring-odd}
  x^n = x^{n - 1 + 1} = x x^{n - 1} = x x^{2\Parens*{n - 1} / 2} = x \Parens*{x^2}^{\Parens*{n - 1} / 2}
\end{align}
We can combine these two ideas \ref{eqn:squaring-even}, \ref{eqn:squaring-odd}
into a single cased based definition.
\begin{align}
  \label{eqn:squaring-def-division}
  \begin{split}
    x^0 &= 1 \\
    x^n &=
    \begin{cases}
      \Parens*{x^2}^{n / 2} & n \text{ is even} \\
      x \Parens*{x^2}^{\Parens*{n - 1} / 2} & n \text{ is odd}
    \end{cases}
  \end{split}
\end{align}
This leads straight into a similar cased based recurrence.
\begin{align}
  \label{eqn:squaring-recurrance}
  \begin{split}
    T(0) &= \BigO{1} \\
    T(n) &=
    \begin{cases}
      T(n / 2) + \BigO{1} & n \text{ is even} \\
      T(n / 2) + 2 \BigO{1} & n \text{ is odd} \\
  \end{cases}
  \end{split}
\end{align}
The cases in this recurrence collapse together as the constant $2$ folds into
the big-o term $\BigO{1}$. This fact makes this recurrence equal to
\ref{eqn:power-of-two-recurrance} and so they have equal run time.
\section{A Binary Digression}
\label{sec:a-binary-digression}
We could attempt to translate definition \ref{eqn:squaring-def-division} directly to
\Agda{}. While this is possible, the definition obstructs proof writing as
checking for parity and integer division are non-trivial constructions in
\Agda{}. Instead we can apply a couple of well known tricks in the world
of bitwise programming. First division by two is the same as bit shift right ($\gg$)
by 1. In the following code $n_i$ represents the $i$th bit of $n$ and
$n_{\ell - 1} n_{\ell - 2} \dots n_{1} n_{0}$ represents the bitstring of $n$.
\begin{align}
  \Floor{n / 2}
  {} &= \Floor[\big]{\Parens[\big]{n_{\ell - 1} n_{\ell - 2} \dots n_{1} n_{0}} / 2} \\
  {} &= \Floor[\big]{\Parens[\big]{2^{\ell - 1}n_{\ell - 1} + 2^{\ell - 2} n_{\ell - 2} + \dots + 2^1 n_1 + 2^0 n_0} / 2} \\
  {} &= \Floor[\big]{\Parens[\big]{2^{\ell - 1}n_{\ell - 1}} / 2}
      + \Floor[\big]{\Parens[\big]{2^{\ell - 2} n_{\ell - 2}} / 2}
      + \dots
      + \Floor[\big]{\Parens[\big]{2^1 n_1} / 2}
      + \Floor[\big]{\Parens[\big]{2^0 n_0} / 2} \\
  {} &= 2^{\ell - 2}n_{\ell - 1}
      + 2^{\ell - 3} n_{\ell - 2}
      + \dots
      + 2^0 n_1 \\
  {} &= n_{\ell - 2} n_{\ell - 3} \dots n_{1} = n \gg 1
\end{align}
The term $\Floor[\big]{\Parens[\big]{2^0 n_0} / 2}$ disappears because the numerator is less
than $2$, a quirk of integer division. By a similar analysis shown above the
parity of an integer can be determined by the first bit of the integer's binary
representation. If the first bit is set then the integer is odd. These two
tricks allow us to rewrite our definition in terms \Agda{} will like.
\begin{align}
  \label{eqn:squaring-def-shift}
  \begin{split}
    x^0 &= 1 \\
    x^n &=
    \begin{cases}
      \Parens*{x^2}^{n \gg 1} & n_0 = 0𝕓 \\
      x \Parens*{x^2}^{n \gg 1} & n_0 = 1𝕓
    \end{cases}
  \end{split}
\end{align}
\Agda{} does not come with bitwise operator primitives so we will need to build
our own binary number type. Note that at every level of recursion we only need
to inspect the last element of the bitstring, so can we drop that element.
Thus if we store the binary number as a snoc list we can perform the
required operations in $\BigO{1}$. A snoc list is a purely functional linked
list that supports push/pop operations from the back of the list in
$\BigO{1}$ time \cite{okasaki}. The figure below is a snoc list encoding of the
number $10$.
\begin{align}
  \label{eqn:snoc-binary-10}
  [] :: 1𝕓 :: 0𝕓 :: 1𝕓 :: 0𝕓
\end{align}
This encoding has one major flaw. Numbers do not have a unique encoding as a user
can add arbitrarily many leading zeros. Take for instance the two encodings of
$3$ given below.
\begin{align}
  [] :: 0𝕓 :: 1𝕓 :: 1𝕓 \stackrel{?}{=} [] :: 1𝕓 :: 1𝕓
\end{align}
We can fix this issue by fusing the empty list and the most significant digit
together \cite{donnacha}. This idea generates a simple \Agda{} data type, note
that $\AgdaInductiveConstructor{𝕓1ᵇ}$ is the fused based case.
\begin{code}
  infixl 5 _0ᵇ _1ᵇ
  data 𝔹⁺ : Set where
    𝕓1ᵇ : 𝔹⁺
    _0ᵇ _1ᵇ : 𝔹⁺ → 𝔹⁺
\end{code}
This encoding unfortunately forbids any leading zeros, and the number $0$
requires exactly one leading zero. We can special case $0$ with its own
constructor.
\begin{code}
  infixr 4 +_
  data 𝔹 : Set where
    𝕓0ᵇ : 𝔹
    +_ : 𝔹⁺ → 𝔹

  three : 𝔹
  three = + 𝕓1ᵇ 1ᵇ
\end{code}
The following code defines a function to reify a binary encoded natural number
into \Agda{} primitive naturals. We define a helper function to multiply a number
by $2$. We could have used the term $2 * n$ but the simplifier transforms that
expression into $n + (n + 0)$. This expression unnecessarily complicates proofs
in the following sections.
\begin{code}
  2* : ℕ → ℕ
  2* n = n + n

  ⟦_⇓⟧⁺ : 𝔹⁺ → ℕ
  ⟦ 𝕓1ᵇ  ⇓⟧⁺ = 1
  ⟦ x 0ᵇ ⇓⟧⁺ = 2* ⟦ x ⇓⟧⁺
  ⟦ x 1ᵇ ⇓⟧⁺ = 1 + 2* ⟦ x ⇓⟧⁺

  ⟦_⇓⟧ : 𝔹 → ℕ
  ⟦ 𝕓0ᵇ ⇓⟧ = 0
  ⟦ + x ⇓⟧ = ⟦ x ⇓⟧⁺

  evaluation-test : ⟦ + 𝕓1ᵇ 0ᵇ 1ᵇ 0ᵇ ⇓⟧ ≡ 10
  evaluation-test = ≡-refl
\end{code}
In order to develop a way to upcast naturals to our binary representation we
need to introduce an formal specification of integer division. In order
to make use of integer division we usually require both the quotient
and remainder so we create a type that bundles these results together. This type
also contains a proof of correctness of the division algorithm along with a
proof that the remainder is less than the divisor.
\begin{code}
  record Euclidean (n : ℕ) (m : ℕ) : Set where
    constructor Euclidean✓
    field
      q : ℕ
      r : ℕ
      division : n ≡ r + m * q
      r<m : r < m
\end{code}
The code above is just a type, we also require a function that produces the type
for a given dividend and divisor. We must ensure that the divisor is not equal
to $0$, to do this we introduce two new concepts. A boolean function
$\AgdaFunction{\_≟\_}$ which returns true if the two inputs are equal. As well
as a type which is only inhabited if a boolean function returns
$\AgdaDatatype{false}$. These two concepts combine to require that any user of
the $\AgdaFunction{\_div\_}$ proves that the divisor is not equal to $0$. The
actual implementation of this function is omitted as it makes use of complicated
primitive functions provided by \Agda{}.
\begin{code}[hide]
  open import Data.Nat.DivMod using (_/_; _%_; m≡m%n+[m/n]*n)
  open import Relation.Binary.PropositionalEquality.TrustMe using (trustMe)
\end{code}
\begin{code}
  _div_ : ∀ (n m : ℕ) {≢0 : False (m ≟ 0)} → Euclidean n m
\end{code}
\begin{code}[hide]
  n div (suc m) = Euclidean✓ (n / suc m) (n % suc m) div-proof (n%m<m n (suc m))
    where
    div-proof : n ≡ n % suc m + suc m * (n / suc m)
    div-proof rewrite *-comm (suc m) (n / suc m) = m≡m%n+[m/n]*n n m
\end{code}
Now we can write a function that reifies non zero naturals into our binary
representation. We begin by dividing the input by $2$. Note that we can
determine the parity of the input by observing the remainder after dividing by
$2$. In the case where the remainder is $0$ we know the input is even so we
recurse adding a $\AgdaInductiveConstructor{0ᵇ}$ to the result. \Agda{} infers
that the quotient must be greater than $0$ as the input is greater than $0$.
When the remainder is equal to $1$ the quotient could be $0$ so we perform a
case analysis. If the quotient is $0$, we introduce our base case of
$\AgdaInductiveConstructor{𝕓1ᵇ}$, otherwise we recurse and add
$\AgdaInductiveConstructor{1ᵇ}$ to the result as the input was odd.
\begin{code}[hide]
  {-# TERMINATING #-}
\end{code}
\begin{code}
  ⟦_⇑⟧⁺ : ∀ (n : ℕ) {≢0 : False (n ≟ 0)} → 𝔹⁺
  ⟦ suc n ⇑⟧⁺ with suc n div 2
  ... | Euclidean✓ (suc q) 0 _ _ = ⟦ suc q ⇑⟧⁺ 0ᵇ
  ... | Euclidean✓ zero    1 _ _ = 𝕓1ᵇ
  ... | Euclidean✓ (suc q) 1 _ _ = ⟦ suc q ⇑⟧⁺ 1ᵇ

  ⟦_⇑⟧ : ℕ → 𝔹
  ⟦ zero ⇑⟧ = 𝕓0ᵇ
  ⟦ suc n ⇑⟧ = + ⟦ suc n ⇑⟧⁺
\end{code}
Lastly, we need a proof that $\AgdaFunction{⟦\_⇓⟧}$ and $\AgdaFunction{⟦\_⇑⟧}$
form an isomorphism. After applying the inductive hypothesis the proof
can be offloaded to an automated ring solver. For this reason we exclude
the body of the proof.
\begin{code}
  ℕ→𝔹→ℕ : ∀ n → ⟦ ⟦ n ⇑⟧ ⇓⟧ ≡ n
\end{code}
\begin{code}[hide]
  ℕ→𝔹→ℕ n = trustMe
\end{code}
\section{Mutally Recursive Proofs}
\label{sec:mutally-recursive-proofs}
In this section we redefine our exponential inline with
\ref{eqn:squaring-def-shift}. Then we prove that the fast exponential is
equivalent to the inductively defined exponential up to setoid equality. In
order to do this we need to rename the old exponential and all its associated
proofs. To accomplish this we tack an (i)nductive onto all the old functions and proofs.
\begin{code}
  module Exponentiation₃ {c ℓ} (M : CommutativeMonoid c ℓ) where
    open Exponentiation₂ M
      renaming (_^_ to _^ⁱ_; ^-homo to ^ⁱ-homo; ∙-^-dist to ∙-^ⁱ-dist)
\end{code}
With that renaming out of the way the definitions follow simply from
\ref{eqn:squaring-def-shift}.
\begin{code}
    _^ᵇ⁺_ : C → 𝔹⁺ → C
    x ^ᵇ⁺ 𝕓1ᵇ = x
    x ^ᵇ⁺ (b 0ᵇ) = (x ∙ x) ^ᵇ⁺ b
    x ^ᵇ⁺ (b 1ᵇ) = x ∙ (x ∙ x) ^ᵇ⁺ b

    _^ᵇ_ : C → 𝔹 → C
    x ^ᵇ 𝕓0ᵇ = ε
    x ^ᵇ (+ b) = x ^ᵇ⁺ b

    _^_ : C → ℕ → C
    x ^ n = x ^ᵇ ⟦ n ⇑⟧
\end{code}
As $\AgdaFunction{\_\^{}ᵇ\_}$ is defined by recursion on a value of type
$\AgdaDatatype{𝔹}$ any proof about $\AgdaFunction{\_\^{}ᵇ\_}$ will have to case
on an input of type $\AgdaDatatype{𝔹}$ in order for the proof term to simplify.
So we create a lemma $\AgdaFunction{loop}$ that is true for any value of type
$\AgdaDatatype{𝔹}$ then we instantiate that lemma at $⟦ \, n ⇑⟧ :
\AgdaDatatype{𝔹}$. This will leave a term of the form $⟦ \, ⟦ \, n ⇑⟧ ⇓⟧$ which is
isomorphic to $n$.
\begin{code}
    x^n≈x^ⁱn : ∀ x n → x ^ n ≈ x ^ⁱ n
    x^n≈x^ⁱn x n = begin
      x ^ n ≈⟨ loop x ⟦ n ⇑⟧ ⟩
      x ^ⁱ ⟦ ⟦ n ⇑⟧ ⇓⟧ ≡⟨ ≡-cong (λ t → x ^ⁱ t) (ℕ→𝔹→ℕ n) ⟩
      x ^ⁱ n ∎
\end{code}
\begin{code}[hide]
      where
\end{code}
This lemma only proves the desired result for values of type $\AgdaDatatype{𝔹}$
we require another similar lemma (produced below) for values of type $\AgdaDatatype{𝔹⁺}$.
\begin{code}
        loop : ∀ x b → x ^ᵇ b ≈ x ^ⁱ ⟦ b ⇓⟧
        loop x 𝕓0ᵇ = refl
        loop x (+ b) = begin
          x ^ᵇ (+ b)    ≡⟨⟩
          x ^ᵇ⁺ b       ≈⟨ loop⁺ x b ⟩
          x ^ⁱ ⟦ b ⇓⟧⁺  ≡⟨⟩
          x ^ⁱ ⟦ + b ⇓⟧ ∎
\end{code}
\begin{code}[hide]
          where
\end{code}
Cases two and three simplify to extremely similar expressions so we factored the
common pattern out into the function $\AgdaFunction{even}$. This has a problem
though, $\AgdaFunction{even}$ calls $\AgdaFunction{loop⁺}$ and
$\AgdaFunction{loop⁺}$ calls $\AgdaFunction{even}$. This pattern is called
mutual recursion, and \Agda{} supports it nicely. To signal to \Agda{} that two
functions are mutually recursive you just declare both their type signatures
before providing a body to either function.
\begin{code}
          even : ∀ x b → (x ∙ x) ^ᵇ⁺ b ≈ x ^ⁱ 2* ⟦ b ⇓⟧⁺
          loop⁺ : ∀ x b → x ^ᵇ⁺ b ≈ x ^ⁱ ⟦ b ⇓⟧⁺
\end{code}
The idea behind $\AgdaFunction{even}$ is quite simple, we just apply the
inductive hypothesis and then we can work with proofs about the inductively
defined exponential.
\begin{code}
          even x b = begin
            (x ∙ x) ^ᵇ⁺ b               ≈⟨ loop⁺ (x ∙ x) b ⟩
            (x ∙ x) ^ⁱ ⟦ b ⇓⟧⁺          ≈⟨ ∙-^ⁱ-dist x x ⟦ b ⇓⟧⁺  ⟩
            x ^ⁱ ⟦ b ⇓⟧⁺ ∙ x ^ⁱ ⟦ b ⇓⟧⁺ ≈⟨ sym (^ⁱ-homo x ⟦ b ⇓⟧⁺ ⟦ b ⇓⟧⁺) ⟩
            x ^ⁱ (⟦ b ⇓⟧⁺ + ⟦ b ⇓⟧⁺)    ≡⟨⟩
            x ^ⁱ 2* ⟦ b ⇓⟧⁺            ∎
\end{code}
The simplifier can deduce most $\AgdaFunction{loop⁺}$ and then we apply
$\AgdaFunction{even}$. The only difference in the even and odd cases is a
single $x$ on the outside of the proof.
\begin{code}
          loop⁺ x 𝕓1ᵇ = begin
            x ^ᵇ⁺ 𝕓1ᵇ      ≡⟨⟩
            x              ≈⟨ sym (identityʳ x) ⟩
            x ∙ ε          ≡⟨⟩
            x ∙ x ^ⁱ 0     ≡⟨⟩
            x ^ⁱ 1         ≡⟨⟩
            x ^ⁱ ⟦ 𝕓1ᵇ ⇓⟧⁺ ∎
          loop⁺ x (b 0ᵇ) = begin
            x ^ᵇ⁺ (b 0ᵇ)    ≡⟨⟩
            (x ∙ x) ^ᵇ⁺ b   ≈⟨ even x b ⟩
            x ^ⁱ 2* ⟦ b ⇓⟧⁺ ≡⟨⟩
            x ^ⁱ ⟦ b 0ᵇ ⇓⟧⁺ ∎
          loop⁺ x (b 1ᵇ) = begin
            x ^ᵇ⁺ (b 1ᵇ)          ≡⟨⟩
            x ∙ (x ∙ x) ^ᵇ⁺ b     ≈⟨ ∙-congˡ (even x b) ⟩
            x ∙ x ^ⁱ 2* ⟦ b ⇓⟧⁺   ≡⟨⟩
            x ^ⁱ (1 + 2* ⟦ b ⇓⟧⁺) ≡⟨⟩
            x ^ⁱ ⟦ b 1ᵇ ⇓⟧⁺       ∎
\end{code}
Beyond the correctness of the algorithm, this proof has many uses. If we ever need to prove a
property of exponentials we can prove that fact with the inductive definition.
Then we can apply this proof to get a version of that property that works on
fast exponentiation for free.

\end{document}


\subsection{Bundle projection}

This is a projection from any point on any of the fibres, to the underlying base manifold.