% !TeX root = ../main.tex
\section{Conditional Narrowing}
Conditional narrowing is our next step on building tools for the main goal of our study, that is, the integration of functional and logic languages.

\begin{definition}
	If $c$ is a finite sequence of equations $s_1 = t_1, \dots, s_n = t_n$ then $\overline{c}$ denotes the multi-set $\{ eq(s_1, t_1), \dots, eq(s_n, t_n) \}$ which will be called \textit{goal clause}. If a \textit{goal clause} is the multi-set consisting of a finite number of ``true''s then we denote it as $\top$.
\end{definition}

Conditional narrowing is defined over goal clauses instead of being defined over terms directly, as in the unconditional case. To simplify notation, call a generic goal clause $eq(s_1, t_1)$ just as an expression $e$.

\begin{definition}
	Let $\trs$ be a CTRS. A goal clause $S$ \textit{conditionally narrows} to a goal clause $T$ iff there exists a goal $e \in S$, a non-variable position $p \in \basicPos{e}$, a variant $\rho : l \contr r \Leftarrow c$ of a conditional rewrite rule in $\trs$, and a substitution $\sigma$ such that:
	\begin{enumerate}
		\item $\sigma$ is a most general unifier of $\restr{e}{p}$ and $l$,
		\item $T = (S \setminus \{e\})\sigma \cup \{ e[r]_p \cup \overline{c} \}\sigma$
	\end{enumerate}
	We say that $S \narrow_{[e, p, \rho, \sigma]} T$ or simply $S \narrow_\sigma T$, when the other information is clear from the context.
\end{definition}

\begin{example}
	Consider the TRS
	\begin{displaymath}
		\trs =
		\begin{cases}
			even(0)    & \contr T                        \\
			even(s(x)) & \contr odd(x)                   \\
			odd(x)     & \contr T \Leftarrow even(x) = F \\
			odd(x)     & \contr F \Leftarrow even(x) = T
		\end{cases}
	\end{displaymath}
\end{example}

\begin{definition}
	Let $\trs$ be a CTRS and $S$ a goal clause. We write $\trs \vdash S$ iff $S \stc_R \top$. The set of all such goals is denoted by $\mathcal{G}_\top(\trs)$ or simply $\mathcal{G}_\top$.
\end{definition}

If $\trs$ is confluent then $\mathcal{G}_\top$ is closed under $\contr_\trs$. The level of a goal clause $T \in \mathcal{G}_\top$ is the least $n$ such that $\trs_n \vdash T$.

Now we prove soundness of conditional narrowing.

\begin{theorem}
	Let $\trs$ be a CTRS and $S$ a goal clause. If $S \narrow_\sigma^* T$ then $\trs \vdash S \sigma$.
	\begin{proof}
		The proof goes by induction on the length of the narrowing derivation from $S$ to $T$. The case of zero length is trivial. Suppose
		$$S \narrow_{[e, \rho, \sigma_1]} S_1 \narrow_{\sigma_2}^* T$$
		where $\rho : l \contr r \Leftarrow c$ is a variant of a rewrite rule from $\trs$. Let $\sigma = \sigma_1 \sigma_2$. By definition,
		$$S_1 = (S \setminus {e})\sigma_1 \cup \{ e[r]_p \cup \overline{c} \}\sigma_1.$$
		The induction hypothesis yields $\trs \vdash S_1 \sigma_2$. Hence, we have both $\trs \vdash (S \setminus {e}) \sigma \cup \{e[r]_p \cup \overline{c} \}\sigma$ and $\trs \vdash \overline{c} \sigma$. From the last observation we have that $\overline{c}\sigma \stc_\trs \top$, so all constraints of the rule $\rho : l \contr r \Leftarrow c$ is solved. So $l\sigma \contr_\trs r \sigma$ and therefore:
		$$S \sigma \contr_\trs (T \sigma \setminus \{ e\sigma \}) \cup \{ (e\sigma)[r\sigma]_p \}.$$
		Observe that $(S \sigma \setminus \{ e\sigma \}) \cup \{ (e\sigma)[r\sigma]_p \} = (S \setminus \{e\})\sigma \cup \{ e[r]_p \}\sigma$. Hence,
		$$S \sigma \contr_\trs S_1\sigma \stc_\trs \top$$
		Finally, $\trs \vdash S \sigma$, as requested.
	\end{proof}
\end{theorem}

Now the main goal is prove a completeness result for conditional narrowing. The idea is still the same: compare narrowing steps with a kind of rewrite relation and lift rewrite steps to narrowing steps. Remember that early we have defined narrowing on terms rather than equational goals. In order to compare conditional rewriting and conditional narrowing, we need to introduce a kind of rewriting relation on goal clauses, we call it \textit{reduction without evaluating conditions}.

\begin{definition}\label{definition:rewrite-on-goals}
	Let $\trs$ be a CTRS and suppose that $S$ and $T$ are goal clauses. We say $S \rewriteOnGoals_\trs T$ if there exists a goal $e \in S$, a position $p \in \pos{e}$, a variant $\rho : l \contr r \Leftarrow c$ of a rewrite rule in $\trs$ and a substitution $\sigma$ such that
	\begin{enumerate}
		\item $\restr{e}{p} = l \sigma$
		\item $T = (S \setminus \{e\}) \cup \{ e[r\sigma]_p \} \cup \overline{c} \sigma$.
		\item $\trs \vdash \overline{c} \sigma$.
	\end{enumerate}
\end{definition}

Note that this relation extend the action of the CTRS $\trs$ from terms to multi-sets of goal clauses. In this view, we also define the approximation $\rewriteOnGoals_\trs^n \; (n \geq 0)$ as in Definition \ref{definition:ctrs-approximation-rewrite-relation}, that is, $S \rewriteOnGoals_\trs^0$ if $T = (S \setminus \{e\}) \cup \{true\}$ with $e = eq(s,s) \in S$ and $S \rewriteOnGoals_\trs^n$ if $S \rewriteOnGoals_{[e, \rho, \sigma]} T$ with $\trs_n \vdash \overline{c} \sigma$. Again, $\rewriteOnGoals_\trs^n \subseteq \rewriteOnGoals_\trs^{n+1}$ for all $n \geq 0$ and
$$\rewriteOnGoals_\trs := \bigcup\limits_{n \geq 0} \rewriteOnGoals_\trs^n.$$
As usual, $\rewriteOnGoals^*$ denote the reflexive-transitive closure of $\rewriteOnGoals$.
\begin{remark}
	For 1-CTRS the relation $\rewriteOnGoals$ can be viewed as an special case of the conditional narrowing relation $\narrow$ but in general this is not true due to the possible extra variables in general conditional rewrite rules.
\end{remark}

\begin{lemma}\label{lemma:completeness-rewrite-on-goals}
	Let $\trs$ be a CTRS and $S$ a goal clause. We have $\trs \vdash S$ if and only if $S \rewriteOnGoals^* \top$.

	\begin{proof}
		Suppose first that $\trs \vdash T$. We reason by induction on $n$ to show that there exists a sequence $S \rewriteOnGoals^* \top$ whenever $\trs_n \vdash S$. If $n = 0$ then there exists a rewrite sequence from $S$ to $\top$ in which only the rule $eq(x,x) \contr true$ is used. By definition, this sequence is also a $\rewriteOnGoals$-sequence.

		Suppose $\trs_{n+1} \vdash S$. So there exists an $\trs_{n+1}$-sequence from $S$ to $\top$. Now we use induction on the length of this sequence. The case of zero length is trivial. So let
		$$S \contr_{\trs_{n+1}} S_1 \stc_{\trs_{n+1}} \top$$
		By the second induction hypothesis we obtain a reduction sequence $S_1 \rewriteOnGoals^* \top$. By the first reduction, there exists a goal $e \in S$, a position $p \in \pos{e}$, a variant $ \rho : l \contr r \Leftarrow c$ of a rewrite rule in $\trs$, and a substitution $\sigma$ such that $\restr{e}{p} = l \sigma$, $S_1 = (S \setminus{e}) \cup \{e[r\sigma]_p\}$, and $\trs_n \vdash \overline{c}\sigma$.  From the first induction hypothesis\footnote{That is, $\trs_n \vdash S$ implies that there exists a sequence $S \rewriteOnGoals^* \top$.}
		and by Definition \ref{definition:rewrite-on-goals} we have $\overline{c}\sigma \rewriteOnGoals^* \top$. Hence, $S \rewriteOnGoals S_1 \cup \overline{c}\sigma$. Combining this step with the previous one we get
		$$S \rewriteOnGoals S_1 \rewriteOnGoals^* \top.$$

		Conversely, suppose $S \rewriteOnGoals^* \top$. We use induction on the length of the sequence $S \rewriteOnGoals^* \top$. The case of zero length is trivial. Suppose $S \rewriteOnGoals S_1 \rewriteOnGoals^* \top$. The induction hypothesis yields $\trs \vdash S_1$. By the first step: there exists a goal $e \in S$, a position $p \in \pos{e}$, a variant $\rho: l \contr r \Leftarrow c$ of a rewrite rule in $\trs$, and a substitution $\sigma$ such that $\restr{e}{p} = l \sigma$, $S_1 = (S \setminus \{e\}) \cup \{e[r\sigma]_p\} \cup \overline{c}\sigma$, and $ \trs \vdash \overline{c}\sigma$. Note that $S \contr_\trs S_2 = (S \setminus \{e\}) \cup \{e[r\sigma]_p\}$. Since $S_2 \subseteq S_1$ we infer $\trs \vdash S_2$ from the fact that $\trs \vdash S_1$. Combining the results above we get
		$$S \contr_\trs S_1 \contr_\trs^* \top.$$
		Obtaining the result.
	\end{proof}
\end{lemma}

We write $S \cong T$ if the goal clauses $S$ and $T$ are identical or they differ only in the number of $true$'s, i.e., $S - \top = T - \top$ by  abuse of notation.

\begin{proposition}(\cite{Middeldorp1994}, Proposition 6.9)
	Let $\trs$ be a CTRS and $S$ a goal clause.
	\begin{enumerate}
		\item If $S \rewriteOnGoals^*_\trs T$ then $T$ can be partitioned into $T_1$ and $T_2$ such that $S \rewriteOnGoals^*_\trs T_1$ and $\trs \vdash T_2$.
		\item If $S \rewriteOnGoals^*_\trs T$ then there exists a goal clause $T_1$ such that $S \rewriteOnGoals^*_\trs T \cup T_1$ and $\trs \vdash T_1$.
	\end{enumerate}
\end{proposition}

\begin{lemma}
	Let $\trs$ be a confluent CTRS. If $S \rewriteOnGoals^*_\trs T_1$ and $S \rewriteOnGoals^*_\trs T_2$ then there exists goal clauses $T_3 \cong T_4$ such that $T_1 \rewriteOnGoals^*_\trs T_3$ and $T_2 \rewriteOnGoals^*_\trs T_4$.
\end{lemma}

Now, following our standard strategy, we give the statement of a \textit{lifting lemma} for conditional narrowing.

\begin{lemma}\label{lemma:lifting-lemma-conditional-narrowing}
	Let $\trs$ be a 1-CTRS. Suppose we have goals clauses $S$ and $T$, a normalised substitution $\theta$, and a set $V$ of variables such that $\vars{S} \cup \dom{\theta} \subseteq V$ and $T = S \theta$. If $T \rewriteOnGoals^*_\trs T'$ then there exists a goal clause $S'$ and substitution $\theta', \sigma$ such that
	\begin{enumerate}
		\item $S \narrow^*_\trs S'$
		\item $S'\theta' = T'$
		\item $\theta =^V = \sigma\theta'$
		\item $\theta'$ is normalised
	\end{enumerate}
	Furthermore, we may assume that the narrowing derivation $S \narrow^*_\sigma S'$ and the rewrite sequence $T \rewriteOnGoals^*_\trs T'$ employ the same rewrite rules at the same positions in the corresponding goals.
	\begin{proof}
		Almost identical to the proof of the lifting lemma for TRSs, Lemma \ref{lemma:lifting-lemma-ordinary-narrowing}. The only difference is that we are dealing with goals clauses instead of terms.
	\end{proof}
\end{lemma}

\begin{definition}\label{definition:CTRS-sub-solution}
	A substitution $\sigma$ is called an $\trs$-solution of a goal clause $T$ if $\trs \vdash T\sigma$.
\end{definition}

\begin{theorem}
	Conditional narrowing is complete for complete 1-CTRSs.
	\begin{proof}
		Let $\trs$ be a complete 1-CTRSs and suppose $\sigma$ is an $\trs$-solution of a goal clause $T$. Let $\trsNF{\sigma}$ be the normal form of $\sigma$. We obtain $\trs \vdash T \trsNF{\sigma}$ from the confluence of $\trs$. According to Lemma \ref{lemma:completeness-rewrite-on-goals} there exists a sequence $T \trsNF{\sigma} \rewriteOnGoals^*_\trs \top$. Lemma \ref{lemma:lifting-lemma-conditional-narrowing} yields a narrowing derivation $T \narrow^*_\tau \top$ and a substitution $\sigma'$ such that $\tau \sigma' =^{\vars{T}} \trsNF{\sigma}$. Therefore $\tau \iqoless[\trs][\vars{T}] \trsNF{\sigma}$.
	\end{proof}
\end{theorem}


\section{Prob 3. )}

Solve the ODE for these 3 initial conditions using an appropriate numerical method. Results shown in \ref{fig:fig1}.

\lstinputlisting{handin2_p3.py}

\begin{figure}[ht!]
  \centering
  \includegraphics[width=0.9\linewidth]{./plots/handin2_p3.png}
  \caption{$t$ vs $D(t)$ for $D(1), D'(1) = [3,2], [10,-10], [5,0]$.}
  \label{fig:fig1}
\end{figure}


\documentclass[11pt]{article}

\usepackage[headings]{fullpage}
\usepackage[utopia]{mathdesign}

\pagestyle{myheadings}
\markboth{MATH426/CISC410}{MATH426/CISC410}

\input{../../fncextra}

\begin{document}

\begin{center}
  \bf Not entirely stable
\end{center}

Consider the function
\[
f(x) = \frac{e^x-1}{x}.
\]
A computation of its relative condition number $\kappa$ shows that $|\kappa(x)|<1$ for all $|x|<1$. In that sense, $f$ is ``easy'' to compute accurately. In practice, though, it's not so simple.

An obvious sequence of steps to compute $f$ is as follows:
\begin{equation}
  \label{direct}
  y_1 = e^x, \qquad y_2 = y_1 - 1, \qquad y_3 = y_2/x.
\end{equation}
The operations for $y_1$ and $y_3$ are well conditioned for $|x|<1$, but the subtraction to get $y_2$ will suffer from cancellation error if $y_1\approx 1$, or $x\approx 0$. That error makes this sequence of operations unstable for $x\approx 0$.

Now consider a power series expansion of $f$,
\begin{equation}
  \label{series}
  f(x) = 1 + \frac{1}{2!} x + \frac{1}{3!}x^2 + \cdots.
\end{equation}
For $x>0$, every term in the series is positive, leaving no possibility of cancellation error. (The analysis for negative $x$ is more subtle.) If $x\approx 0$, then we should be able to find $n$ such that $x^n/(n+1)!$ is smaller than machine precision, so that adding it to the terms before it will not change the result numerically. Thus a truncated form of the series can serve as a stable method for $x\approx 0$.

\subsection*{Goals}

You will experiment with the two methods of computing $f$ and observe their relative accuracy.

\subsection*{Preparation}

Read section 1.3.

\subsection*{Procedure}

Download the template script and complete it to perform the following steps.

\begin{enumerate}
\item MATLAB has a stable way of computing $y_2$ in~\eqref{direct} without using subtraction. You will use it to get reference ``exact'' values of $f$. Let
\begin{verbatim}
x = logspace(-16,0,500);
\end{verbatim}
  Create a vector \texttt{y} the same size as \texttt{x} such that $y_j$ is the result of \texttt{expm1(x(j))/x(j)}.

\item Create a vector \texttt{z} such that $z_j$ is computed using the three steps in~\eqref{direct} for $x_j$.

\item Compute a vector of relative differences between $z_j$ and $y_j$. Make a log-log plot of the result as a function of $x$. You will see a loss of accuracy as $x\to 0$.

\item By trial and error, find a value of $n$ such that $0.01^n/(n+1)!$ is less than \texttt{eps} (machine epsilon). This defines the last term to keep in truncating the series~\eqref{series}.

\item Create a vector \texttt{w} such that $w_j$ is computed using the truncated form of~\eqref{series}. Repeat step~3 for \texttt{w} in place of \texttt{z}, using \texttt{semilogx} instead of \texttt{loglog}. This time you should see accuracy maintained as $x\to 0$. (If more terms of the series were kept, the accuracy could be maintained as $x\to 1$ as well, but the direct method is more efficient there.)

\end{enumerate}

\end{document}


\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%        ELECTRODYNAMICS SECTION        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Electricity \& Magnetism}
\label{sec:electro}
The electricity and magnetism subject matter in the qualifying exam is primarily at the level of \cite{griffithsIntroductionElectrodynamics2018}, a common, upper-level undergraduate electricity and magnetism text.

\section{Electrostatics}
We begin with the study of static electric fields. The electric field is one of two principle elements in electrodynamics,
% Electric Field
\subsection{Electric Field}
% 	Superposition
The \textit{Principle of Superposition} in electrostatics states that the forces between two charges do not depend on any other charges. Consequently, multiple such forces and fields may be \textit{superimposed}, or added together, to construct the net force and field.
% 	Coulomb Law
Now that we know we can add forces between pairs of charges together, we must describe the force between two electric charges, \textit{Coulomb's law}:
\eqn{\vec{F} = k\frac{qQ}{r^2}\vec{\hat{r}}
\quad\quad \text{where} \quad\quad k = \frac{1}{4\pi \epsilon_0}
\quad\quad \text{and} \quad\quad \vec{r}=\vec{r}_Q - \vec{r}_q}
In the above, $\epsilon$ is the \textit{permittivity of free space}. Note that for like sign charges, $\vec{F}$ is repulsive, and for dislike sign charges, $\vec{F}$ is attractive.
% 	Electric Field
The above equations also make use of the \textit{test charge} $Q$, but it is also possible to discuss the vector field $\vec{E}$ created by the source charges $q$ by the following:
\eqn{\vec{F} = Q\vec{E}
\quad\quad \text{where} \quad\quad
\vec{E}(\vec{r}) = k \sum_i \frac{q_i}{r_i^2}\vec{\hat{r}}_i = k \int \frac{1}{r^2}\vec{\hat{r}}\ dq,
\quad \vec{r}_i = \vec{r} - \vec{r}_{q_i}}
% 	Continuous Charge Distribution
For continuous charge distributions, the above can be rewritten in terms of various charge \textit{densities}. In the linear case, a line charge density $\lambda(\vec{r})$ is used. Similarly, for surface charges, $\sigma(\vec{r})$ is used, and for volume charges $\rho(\vec{r})$ is used.
\eqn{\begin{aligned}
	\vec{E}(\vec{r}) &= k \int \frac{\lambda\wrap{\vec{r}'}}{r^2}\vec{\hat{r}}\ dl' \\
	\vec{E}(\vec{r}) &= k \int \frac{\sigma\wrap{\vec{r}'}}{r^2}\vec{\hat{r}}\ dS' \\
	\vec{E}(\vec{r}) &= k \int \frac{\rho\wrap{\vec{r}'}}{r^2}\vec{\hat{r}}\ d\tau' \\
\end{aligned}}
% Divergence and Curl of Electrostatic Fields
\subsection{Div and Curl of E Fields}
The equations given in the previous section are sufficient to compute an electrostatic field given an arbitrary charge distribution; however, these integrals can be formidable. We now introduce techniques to simplify the computation of such fields, primarily via applications of Gauss' law:
% 	Gauss' Law / Field Lines / Flux
\eqn{\oint \vec{E} \cdot d\vec{S} = \frac{Q_{\text{enc}}}{\epsilon_0}
\quad \iff \quad \vec{\del}\cdot\vec{E} = \frac{\rho}{\epsilon_0}
}
% 	Divergence of E / Apply Gauss' Law
Gauss' law can be applied in cases in which $\rho$ exhibits symmetry, such that a \textit{Gaussian surface} can be constructed containing the charge. On such a surface, $\vec{E}\cdot d\vec{S}$ must be constant, so that it can be factored out of the integrand. For instance, consider the field generated by a sphere of radius $R$ and charge $q$, at a point $r>R$.
\eqn{\int\limits_{\mathcal{S}} \norm{\vec{E}} \cdot da = \norm{\vec{E}} \int\limits_{\mathcal{S}}da = \norm{\vec{E}} 4\pi r^2 \implies \vec{E} = k\frac{q}{r^2}\vec{\hat{r}}}
% 	Curl of E
Similar methods can be used for an infinite sheet of charge, to produce $E=\frac{\sigma}{2\epsilon_0}$. We've exploited the divergence of the $\vec{E}$ field thus far; now we turn the the curl of an $\vec{E}$ field. Starting with a simple example of a point charge, one obtains $\int_a^b \vec{E} \cdot d\vec{l} = kq \wrap{1/r_a - 1/r_b}$, which only depends upon the end points of the integration. Consequently, $\vec{E}$ is a conservative field, and we have:
\eqn{\oint \vec{E} \cdot d\vec{l} = 0 \iff \vec{\del}\times \vec{E} = 0}
% Electric Potential
\subsection{Electric Potential}
Since the curl of $\vec{E}$ is zero, is it a closed form, and is therefore also an exact form. We know that exact forms are the exterior derivatives of simpler forms, and therefore that $\vec{E} = -\del V$, where $V$ is called the \textit{scalar potential}. For more on closed and exact forms, see Appendix \ref{app:vector-analysis:potentials}.
% 	Definitions of Potential
We can then define the potential directly:
\eqn{V(\vec{r}) \equiv - \int_{\mathcal{O}}^{\vec{r}} \vec{E} \cdot d\vec{l}
\quad\quad\quad V(\vec{r}) = - \int_{\infty}^{\vec{r}} k \frac{\rho(\vec{r}')}{r'}d\tau'}
% 	Poisson's Equation / Laplace Equation
The choice of reference point $\mathcal{O}$ amounts to a the addition of a constant to $V$. The conventional choice is \textit{infinitely far away}, or $\mathcal{O}=\infty$. Combining both $\vec{\del}\cdot \vec{E}$ and $\vec{E}=-\del V$, we obtain the general \textit{Poisson} equation, which is the \textit{Laplace} equation.
\eqn{\del^2 V = -\frac{\rho}{\epsilon_0}\quad \text{(Poisson)}
\quad\quad\quad\quad\quad \rho\to 0 \implies \del^2 V = 0 \quad \text{(Laplace)}}
% 	Boundary Conditions
From the infinite-sheet example we can infer important details about continuity of electric fields when a boundary is involved. A charged boundary will induce a discontinuity in the perpendicular component $E^{\perp}$, whereas the parallel component $\vec{E}^{\parallel}$ is always continuous. Specifically:
\eqn{E^{\perp}_{\text{ext}} - E^{\perp}_{\text{int}} = \frac{\sigma}{\epsilon_0} \quad\quad\quad \vec{E}^{\parallel}_{\text{ext}} = \vec{E}^{\parallel}_{\text{int}}}
It should be noted that the potential $V$ is \textit{always} continuous, whereas the gradient of the potential is not necessarily continuous, since $-\vec{E} = \del V$.
% Work and Energy in electrostatics
\subsection{Work and Energy in Electrostatics}
We have discussed field configurations and how those fields give rise to forces on test charges. Now, we discuss the work and energy induced by the electric force, using the conventional, Newtonian definition of work:
\eqn{W = \int_a^b \vec{F} \cdot d\vec{l} = -Q \int_a^b \vec{E} \cdot d\vec{l} = Q\wraph{V\wrap{\vec{b}} - V\wrap{\vec{a}}}}
% 	Charge configuration energy
Note that the energy of a charge configuration may be computed by evaluating the work required to move each point charge from infinity to its final location: $W_i = q_i V(\vec{r}_i)$, since we choose $\vec{V}_{\infty}=0$.
When configuring multiple charges, the potential will change after each charge has been put in place, and will affect subsequent charges. For example, in a system of three charges, the first requires no work $W_1=0$, the second is only affected by the first $W_2 = k q_2 q_1 / r_{12}$, but the third is affected by both the first two charges $W_3 = k q_3 \wrap{q_1 / r_{13} + q_2 / r_{23}}$.
% 	Electrostatic Energy
For a general system of $n$ charges $q$:
\eqn{W = k \sum_{i=1}^n \sum_{j>i}^n \frac{q_i q_j}{r_{ij}} = \half \sum_{i=1}^n q_i V\wrap{\vec{r}_i} \quad\quad \text{where} \quad r_{ij} = \norm{\vec{r}_j - \vec{r}_i}}
For a continuous charge distribution $\rho$, the configuration energy $W$ can be rewritten in terms of $E$ instead of $\rho$, giving the nicer form. Note that in field theories, the energy is considered \textit{stored in the field}.
\eqn{W = \half \int \rho V d\tau = \frac{\epsilon_0}{2} \int E^2 d\tau}
% Conductors
\subsection{Conductors}
% 	Basic Properties
In a \textit{conductor}, electrons are more loosely bound than in insulators, and are consequently free to move around. The mobility of internal electrons produces several distinct properties:
\begin{enumerate}[label=(\roman*)]
	\item $\vec{E}=0$ \textbf{inside a conductor}: Charges move to counterbalance external fields.
	\item $\rho = 0$ \textbf{inside a conductor}: follows (i) from Gauss' Law.
	\item \textbf{All net charge resides on the surface}: Since no charge is in the interior.
	\item \textbf{A conductor is an equipotential}: a
	\item $\vec{E}$ \textbf{$\perp$ surface, just outside a conductor}: any tangential component is cancelled via (i).
\end{enumerate}
% 	Induced Charges
When a charge $q$ is placed near or within a conductor, this \textit{induces} a charge $q_{\text{induced}}=-q$ on the surface of the conductor nearest $q$, by either attracting or repelling the mobile electrons within. Note that $\vec{E}$ is still 0 within the conductor.
% 	Surface Charges
Therefore, the field immediately outside must be:
\eqn{\vec{E} = \frac{\sigma}{\epsilon_0}\vec{\hat{n}} \quad\quad\quad\quad
\sigma = - \epsilon_0 \pd{V}{n} \quad\quad \implies \quad\quad
\vec{F} = \sigma \vec{E}_{\text{avg}} = \frac{\sigma^2}{2 \epsilon_0}\vec{\hat{n}}}
% 	Capacitors
Combining two conductors can produce a \textit{capacitor}, which is capable of storing a certain amount of charge $Q$ at a particular potential $V$, related by the \textit{capacitance} $C$ linearly. Note that $C$ is a purely geometrical quantity, and is measured in \textit{farads (F)}. Some examples below in Eq. \ref{eq:em:caps}
\eqn{C\equiv \frac{Q}{V} \quad\quad\quad W = \half CV^2}
\eqn[eq:em:caps]{C_{\text{Par-Plate}} = \frac{A\epsilon_0}{d} \quad\quad\quad
C_{\text{Cylinder}} = \frac{2 \pi \epsilon_0}{\ln\wrap{R_{\text{outer}} / R_{\text{inner}}}}}

\section{Potentials}
% Laplace Equation
% 	Introduction
%	Laplace Equation in One Dimension
%	Laplace Equation in Two Dimensions
%	Laplace Equation in Three Dimensions
% 	Boundary Conditions / First Uniqueness
% 	Conductors / Second Uniqueness
% Method of Images
% 	Classic Problem
% 	Induced Surface Charge
% 	Force and Energy
% 	Other Image Problems
% Separation of Variables
% 	Cartesian Coordinates
% 	Spherical Coordinates
% Multipole Expansion
% 	Approximate Potentials
% 	Monopole and Dipole Terms
% 	Origin of Coordinates in Expansion
% 	E Field of Dipole

\section{Electric Fields in Matter}
\section{Magnetostatics}
\section{Magnetic Fields in Matter}
\section{Electrodynamics}
\section{Conservation Laws}
\section{Electromagnetic Waves}
\section{Potentials and Fields}
\section{Radiation}
\section{Circuits}


\subsection{Torsion tensor}


%====================================================================================
\section{Error component model}
%====================================================================================
\begin{frame}{Error component model}
	\begin{itemize}
		\item Fixed effects or within-grup estimator
		\item Random effects estimator
			\begin{itemize}
				\item Feasible GLS estimation
				\item Maximum Likelihood Estimation.
			\end{itemize}
	\end{itemize}
\end{frame}
%------------------------------------------------
\begin{frame}{Error component model}
	In the model
		\begin{align}
			y_{it} & = \alpha +\beta ^{\prime }X_{it}+u_{it} \\
			u_{it} & = \mu _{i}+\nu _{it}  \notag
		\end{align}
	\begin{itemize}
		\item In Fixed effects or within-grup estimator the moments to match are $E\left(X_{it}^{\prime}\nu_{it}\right)=0$, there is no restriction on $E\left(X_{it}^{\prime}\mu_{i}\right).$
		\item In Random effects estimator, the moments to match are $E\left(X_{it}^{\prime}\nu_{it}\right) = 0$ and $E\left( X_{it}^{\prime}\mu_{i}\right)=0.$
	\end{itemize}
\end{frame}


\section{Supplementary Material}

Randomized Self Organizing Map \\
Nicolas P. Rougier$^{1,2,3}$ and Georgios Is. Detorakis$^4$ \\
$^1$ Inria Bordeaux Sud-Ouest \\
$^2$ Institut des Maladies Neurodégénératives, Université  de Bordeaux, CNRS UMR 5293 \\
$^3$ LaBRI, Université de Bordeaux, Institut Polytechnique de Bordeaux, CNRS UMR 5800 \\
$^4$ adNomus Inc., San Jose, CA, USA \\

\noindent\textbf{Abstract.} Self-organizing maps usually rely on a predetermined topology
of the neural space (the map), which is either a rectangular or a hexagonal Cartesian grid.
When the intrinsic dimension of  the input space is much higher than the allowed dimension
of the neural space, then the self-organizing map can be ill-formed. To overcome this problem
in  high dimensional input spaces we propose a variation of the self organizing map algorithm,
where we consider random placement of neurons on a high-dimensional manifold. The positions of
the neural units are drawn from a blue noise  distribution thus various topologies can be
derived. These topologies possess  random (but controllable) discontinuities that allow for a
more flexible self-organization, especially with high-dimensional data. The proposed algorithm
has been tested on one-, two- and \gid{three-dimensions tasks as well as on MNIST handwritten
digits dataset. Furthermore, we investigate the reorganization of the self-organizing maps when
we either remove or add neurons to the map. To analyze the results we use spectral analysis and
Topological Data Analysis tools}. \par

\renewcommand\thefigure{S\arabic{figure}}
\setcounter{figure}{0}

\subsection{One dimensional data -- Experiment ${\bf 1}$}

\subsection{Two-dimensional data -- Experiment ${\bf 2}$a}

\begin{figure}[!htpb]
     \centering
     \includegraphics[width=\textwidth]{figures/experiment-2-pd.pdf}
     \caption{{\bfseries \sffamily Persistent Barcode and Diagram for
     Experiment ${\bf 2}$a.} $2$D uniformly distributed points of an annulus are being used
     as input to the Kohonen and VSOM learning algorithms. We compute the persistence
     barcode and diagram of the input samples (first column) top and bottom panels, respectively.
     The middle column indicates the barcodes and the persistence diagram of the Kohonen
     map the right column the barcodes and the persistence diagram of the VSOM. We observe
     that the topology of the neural space generated by VSOM approaches closer the topology
     of the input space than Kohonen does. The upper row shows the persistence barcodes and
     the bottom row the persistence diagrams. }%
     \label{Fig:persistence_exp2}
\end{figure}

\begin{table}[!ht]
  \begin{center}
    \begin{tabular}{ccccc}
        \textbf{Algorithm} & $d_b$ $H0$ & $W_p$ $(H0)$ & $d_b$ $H1$ & $W_p$ $(H1)$ \\
        \hline
        Kohonen & 0.0522 & 0.0233 & 5.2965 & 2.3564 \\
        VSOM    & 0.0501 & 0.0246 & 4.5197 & 2.3669
      \end{tabular}
      \caption{\textbf{Experiment $2$a Persistence Diagram Distances.} Bottleneck
      and Wasserstein distances between the persistence diagrams of the input and
      Kohonen and VSOM. In this experiment both the Kohonen and the VSOM do not express
      any significant difference. $d_b$ is the bottleneck distance~\eqref{eq:bottle},
      $W_p$ is the Wasserstein distance~\eqref{eq:wasser}, $H0$ indicates the Homology Group $0$
      (path-connected components, or one-dimensional holes), $H1$ is the Hologoy group $1$
      (two-dimensional holes).}
      \label{table:parameters}
  \end{center}
\end{table}

\subsection{Distributions of Eigenvalues}

\begin{figure}[!htpb]
     \centering
     \includegraphics[width=\textwidth]{figures/eig-distributions.pdf}
     \caption{{\bfseries \sffamily Eigenvalues Distributions}
     {\bfseries \sffamily A} Experiment $2$a, a two-dimensional SOM
     maps a two-dimensional uniform annulus distribution\@.
     {\bfseries \sffamily B} Experiment $2$b a two-dimensional uniform
     distribution with holes is the input space for a two-dimensional SOM\@.
     {\bfseries \sffamily C} Experiment $3$ A two-dimensional SOM learns the
     representations of a three-dimensional uniform distribution (cube).
     {\bfseries \sffamily D} Experiment $4$ A two-dimensional map learns
     the representations of MNIST handwritten data set.
     In all panels blue color indicates the standard Kohonen SOM and the
     black color shows the VSOM.}%
     \label{Fig:distributions}
\end{figure}

\subsection{Two-dimensional data -- Experiment ${\bf 2}$b}

\begin{figure}[!htpb]
     \centering
     \includegraphics[width=\textwidth]{figures/experiment-2-bis-random-crop.pdf}
     \caption{{\bfseries \sffamily Experiment $2$b -- Uniform distribution on a plane with holes.}
     Voronoidal SOM made of $1024$ neurons with a $2$-nearest neighbors
     induced topology. Model has been trained for $25,000$ epochs on two-dimensional
     points drawn from a uniform distribution on the Cartesian plane $[0, 1] \times [0, 1]$.
     \textbf{A} Map topology in neural space. \textbf{B} Map prototypes displayed
     in neural space using Voronoi cells (whose color indicates prototype according
     to colormap). \textbf{C to H} Map activity for six neurons.}
     \label{Fig:experiment2b}
\end{figure}

\begin{figure}[!htpb]
     \centering
     \includegraphics[width=\textwidth]{figures/experiment-2-bis-pd.pdf}
     \caption{{\bfseries \sffamily Persistent Barcode and Diagram for
     Experiment ${\bf 2}$b.} $2$D uniformly distributed points on a plane with holes
     are being fed as input to the Kohonen and VSOM learning algorithms. We compute
     the persistence barcodes and diagrams of the input samples (first column) top and
     bottom panels, respectively, as well as of the Kohonen map (middle column) and of
     the VSOM (last column).
     We observe that the topology of the neural space generated by VSOM approaches closer
     the topology of the input space than Kohonen does. The upper row shows the persistence
     barcodes and the bottom row the persistence diagrams. }
     \label{Fig:persistence_exp2b}
\end{figure}

\begin{table}[!ht]
  \begin{center}
    \begin{tabular}{ccccc}
        \textbf{Algorithm} & $d_b$ $H0$ & $W_p$ $(H0)$ & $d_b$ $H1$ & $W_p$ $(H1)$ \\
        \hline
         SOM & 0.0343 & 0.1130 & 6.7631 & 1.5311 \\
        RSOM & 0.0390 & 0.1130 & 5.7588 & 1.4158
      \end{tabular}
      \caption{\textbf{Experiment $2$b Persistence Diagram Distances.} Bottleneck
      and Wasserstein distances between the persistence diagrams of the input and
      Kohonen and VSOM. In this experiment both the Kohonen and the VSOM do not express
      any significant difference. $d_b$ is the bottleneck distance~\eqref{eq:bottle},
      $W_p$ is the Wasserstein distance~\eqref{eq:wasser}, $H0$ indicates the Homology Group $0$
      (path-connected components, or one-dimensional holes), $H1$ is the Hologoy group $1$
      (two-dimensional holes).}
      \label{table:parameters}
  \end{center}
\end{table}


\chapter{Numbers}
\label{numbertypeschapter}
\mainindex{number}

This chapter describes Scheme's model for numbers.  It is important to
distinguish between the mathematical numbers, the Scheme objects that
attempt to model them, the machine representations used to implement
the numbers, and notations used to write numbers.  In this report, the
term \textit{number} refers to a mathematical number, and the term
\textit{number object} refers to a Scheme object representing a
number.  This report uses the types \type{complex}, \type{real},
\type{rational}, and \type{integer} to refer to both mathematical
numbers and number objects.  The \type{fixnum} and \type{flonum} types
refer to special subsets of the number objects, as determined by
common machine representations, as explained below.

\section{Numerical tower}
\label{numericaltypes}
\index{numerical types}

Numbers may be arranged into a tower of subsets in which each level
is a subset of the level above it:
\begin{tabbing}
\ \ \ \ \ \ \ \ \ \=\tupe{number} \\
\> \tupe{complex} \\
\> \tupe{real} \\
\> \tupe{rational} \\
\> \tupe{integer}
\end{tabbing}

For example, 5 is an integer.  Therefore 5 is also a rational,
a real, and a complex.  The same is true of the number objects
that model 5.

Number objects are organized as a corresponding tower of subtypes
defined by the predicates {\cf number?}, {\cf complex?}, {\cf real?},
{\cf rational?}, and {\cf integer?}; see section~\ref{number?}.
Integer number objects are also called \textit{integer
  objects}\mainindex{integer object}.

There is no simple relationship between the subset that contains a
number and its representation inside a computer.  For example, the
integer 5 may have several representations.  Scheme's numerical
operations treat number objects as abstract data, as independent of
their representation as possible.  Although an implementation of
Scheme may use many different representations for numbers, this should
not be apparent to a casual programmer writing simple programs.

\section{Exactness}
\label{exactly}

\mainindex{exactness}It is useful to distinguish between number objects
that are known to correspond to a number exactly, and those number
objects whose computation involved rounding or other errors.  For
example, index operations into data structures may need to know the index
exactly, as may some operations on polynomial coefficients in a symbolic algebra
system.  On the other hand, the results of measurements are inherently
inexact, and irrational numbers may be approximated by rational and
therefore inexact approximations.  In order to catch uses of numbers
known only inexactly where exact numbers are required, Scheme
explicitly distinguishes \defining{exact} from \defining{inexact} number objects.  This
distinction is orthogonal to the dimension of type.

A
number object is exact if it is the value of an exact numerical
literal or was derived from exact number objects using only exact
operations.  Exact number objects correspond to mathematical numbers
in the obvious way.

Conversely, a number object is inexact if it is the value of an
inexact numerical literal, or was derived from inexact number objects,
or was derived using inexact operations.  Thus inexactness is
contagious.

Exact arithmetic is reliable in the following sense: If exact number
objects are passed to any of the arithmetic procedures described in
section~\ref{propagationsection}, and an exact number object is
returned, then the result is mathematically correct.  This is
generally not true of computations involving inexact number objects
because approximate methods such as floating-point arithmetic may be
used, but it is the duty of each implementation to make the result as
close as practical to the mathematically ideal result.

\section{Fixnums and flonums}

A \defining{fixnum} is an exact integer object that lies
within a certain implementation-dependent subrange of the
exact integer objects. (Library section \extref{lib:fixnumssection}{Fixnums} describes a
library for computing with fixnums.)
Likewise, every implementation must
designate a subset of its inexact real number objects as \defining{flonum}s, and
to convert certain external representations into flonums.
(Library section \extref{lib:flonumssection}{Flonums} describes a library for
computing with flonums.)  Note that
this does not imply that an implementation must use
floating-point representations.

\section{Implementation requirements}

\index{implementation restriction}\label{restrictions}

Implementations of Scheme must support number objects for
the entire tower of subtypes given in section~\ref{numericaltypes}.
Moreover, implementations must support exact integer
objects and exact rational number objects of practically unlimited
size and precision, and to implement certain procedures (listed in
\ref{propagationsection}) so they always return exact results when
given exact arguments.  (``Practically unlimited'' means that the size
and precision of these numbers should only be limited by the size of
the available memory.)

Implementations may support only a limited range of inexact number
objects of any type, subject to the requirements of this section.  For
example, an implementation may limit the range of the inexact real
number objects (and therefore the range of inexact integer and
rational number objects) to the dynamic range of the flonum format.
Furthermore the gaps between the inexact integer objects and
rationals are likely to be very large in such an implementation as the
limits of this range are approached.

An implementation may use floating point and other approximate
representation strategies for \tupe{inexact} numbers.
This report recommends, but does not require, that the IEEE
floating-point standards be followed by implementations that use
floating-point representations, and that implementations using
other representations should match or exceed the precision achievable
using these floating-point standards~\cite{IEEE}.

In particular, implementations that use floating-point representations
must follow these rules: A floating-point result must be represented
with at least as much precision as is used to express any of the
inexact arguments to that operation.
Potentially inexact operations such as {\cf sqrt}, when
applied to exact arguments, should produce exact answers whenever possible
(for example the square root of an exact 4 ought to be an exact 2).
However, this is not required.
If, on the other hand, an exact number object is operated upon so as to produce an
inexact result (as by {\cf sqrt}), and if the result is represented in
floating point, then the most precise floating-point format available
must be used; but if the result is represented in some other way then
the representation must have at least as much precision as the most
precise floating-point format available.

It is the programmer's responsibility to avoid using inexact number
objects with magnitude or significand too large to be represented in
the implementation.

\section{Infinities and NaNs}

Some Scheme implementations, specifically those that follow the IEEE
floating-point standards, distinguish special number objects called
\mainindex{infinity}\defining{positive infinity}, \defining{negative
  infinity}, and \defining{NaN}.

Positive infinity is regarded as an inexact real (but not rational) number
object that represents an indeterminate number greater than the
numbers represented by all rational number objects.  Negative infinity
is regarded as an inexact real (but not rational) number object that represents
an indeterminate number less than the numbers represented by all
rational numbers.

A NaN is regarded as an inexact real (but not rational) number object so
indeterminate that it might represent any real number, including
positive or negative infinity, and might even be greater than positive
infinity or less than negative infinity.

\section{Distinguished -0.0}

\index{-0.0}
Some Scheme implementations, specifically those that follow the IEEE
floating-point standards, distinguish between number objects for $0.0$
and $-0.0$, i.e., positive and negative inexact zero.  This report
will sometimes specify the behavior of certain arithmetic operations
on these number objects.  These specifications are marked with ``if
$-0.0$ is distinguished'' or ``implementations that distinguish
$-0.0$''.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "r6rs"
%%% End:


\documentclass[12pt]{article}

\title{Pattern Recognition Homework 2}
\author{Kosate Limpongsa \\ kosatelim@gmail.com}

\usepackage{amsmath,amsthm,mathtools}
\usepackage{graphicx}
\usepackage{float}
\usepackage{listings}
\usepackage{hyperref}
\graphicspath{ {images/} }
\begin{document}

\maketitle

\section{MLE}

\begin{align*}
  y_2 = \alpha y_1 + w_1 &\qquad w_0, w_1 \sim N(0, \sigma^2) \\
  y_1 = \alpha y_0 + w_0 &\qquad y_0 \sim N(0, \lambda)
\end{align*}
\begin{center}
Find $\operatorname*{argmax}_\alpha p(y_2,y_1,y_0|\alpha)$
\end{center}
\begin{align*}
p(y_2,y_1,y_0|\alpha) ={} & p(y_2|y_1,y_0,\alpha) \, p(y_1|y_0, \alpha) \, p(y_0|\alpha) \\
p(y_n|y_{n-1}, y_{n-2}, ...) ={} & p(y_n|y_{n-1}) \\
p(y_2,y_1,y_0|\alpha) ={} & p(y_2|y_1, \alpha) \, p(y_1|y_0, \alpha) \, p(y_0|\alpha)
\end{align*}
\begin{align*}
\begin{split}
p(y_2,y_1,y_0|\alpha) ={}
  & \frac{1}{\sqrt{2\pi\sigma^2}}exp(-\frac{(y_2-\alpha y_1)^2}{2 \sigma^2}) \\
  & \frac{1}{\sqrt{2\pi\sigma^2}}exp(-\frac{(y_1-\alpha y_0)^2}{2 \sigma^2}) \\
  & \frac{1}{\sqrt{2\pi\lambda}}exp(-\frac{y_0^2}{2 \lambda})
\end{split}
\\
log p(y_2,y_1,y_0|\alpha) ={}
  & -\frac{1}{2}log\sqrt{2\pi\sigma^2} - \frac{(y_2-\alpha y_1)^2}{2 \sigma^2} \\
  & -\frac{1}{2}log\sqrt{2\pi\sigma^2} - \frac{(y_1-\alpha y_0)^2}{2 \sigma^2} \\
  & -\frac{1}{2}log\sqrt{2\pi\lambda} - \frac{y_0^2}{2 \lambda}
\\
\frac{\partial log p(y_2,y_1,y_0|\alpha)}{\partial \alpha} ={}
  & \frac{y_2 y_1}{\sigma^2} - \frac{\alpha y_1^2}{\sigma^2} +
  \frac{y_1 y_0}{\sigma^2} - \frac{\alpha y_0^2}{\sigma^2}
\end{align*}
Find MLE by taking $\frac{\partial log P}{\partial \alpha} = 0$
\begin{align*}
  \frac{\partial log p(y_2,y_1,y_0|\alpha)}{\partial \alpha} ={} & 0\\
  0 ={} & \frac{y_2 y_1 - \alpha y_1^2 + y_1 y_0 - \alpha y_0^2}{\sigma^2} \\
  0 ={} & y_2 y_1 - \alpha y_1^2 + y_1 y_0 - \alpha y_0^2 \\
  \alpha ={} & \frac{y_2 y_1 - y_1 y_0}{y_1^2 + y_2^2}
\end{align*}

\section{Simple Bayes Classifier}
\label{sec:bayes}

\subsection{Plot posteriors of $N(5,2)$ and $N(0,2)$}
\begin{figure}[ht]
  \centering
  \includegraphics[width=1\textwidth]{bayes-1.png}
  \end{figure}
As you see, the decision boundary of $N(5,2)$ and $N(0,2)$ $= 2.5$

\begin{align*}
P(x|w_1) \, P(w_1) &= P(x|w_2) \, P(w_2) \\
P(x | w_1) &= P(w | x_2) \\
(\frac{1}{\sqrt{2 \pi \sigma_1^2}}) \, exp (-\frac{(x-\mu_1)^2}{2 \sigma_1^2}) &= (\frac{1}{\sqrt{2 \pi \sigma_2^2}}) \, exp (-\frac{(x-\mu_2)^2}{2 \sigma_2^2}) \\
  \text{Since } \sigma_1 &= \sigma_2 \\
  (x-\mu_1)^2 &= (x-\mu_2)^2 \\
  x^2 - 2 x \mu_1 + \mu_1^2 &= x^2 - 2 x \mu_2 + \mu_2^2 \\
  -10x + 25 &= 0 \\
  x &= 2.5
\end{align*}

\subsection{What happens if prior of happy cat is $0.8$}

\begin{figure}[ht]
  \centering
  \includegraphics[width=1\textwidth]{bayes-2.png}
  \end{figure}
As you see, the decision boundary of $N(5,2)$ and $N(0,2)$ $\approx 1.95$

\begin{align*}
  P(x|w_1) \, P(w_1) &= P(x|w_2) \, P(w_2) \\
  (\frac{1}{\sqrt{2 \pi \sigma_1^2}}) \, exp (-\frac{(x-\mu_1)^2}{2 \sigma_1^2}) (0.8) &= (\frac{1}{\sqrt{2 \pi \sigma_2^2}}) \, exp (-\frac{(x-\mu_2)^2}{2 \sigma_2^2}) (0.5) \\
  exp (-\frac{(x-5)^2}{4}) 4 &= exp (-\frac{x^2}{4}) \\
  -\frac{(x - 5)^2}{8} + ln 4 &= -\frac{x^2}{8} \\
  x &= \frac{25 - 4 log 4}{10} \\
  x &\approx 1.9454
  \end{align*}

\subsection{Plot $P(x|w_1) = N(5,2), P(x|w_2) = N(0,4)$}

\begin{figure}[ht]
  \centering
  \includegraphics[width=1\textwidth]{bayes-3.png}
  \end{figure}

As you see, the decision boundary when change prior of happy cat $\approx 2.74$

\begin{align*}
  P(x|w_1) \, P(w_1) &= P(x|w_2) \, P(w_2) \\
  \text{Since } P(w_1) &= P(w_2) \\
  P(x|w_1) &= P(x|w_2) \\
  (\frac{1}{\sqrt{2 \pi \sigma_1^2}}) \, exp (-\frac{(x-\mu_1)^2}{2 \sigma_1^2}) &= (\frac{1}{\sqrt{2 \pi \sigma_2^2}}) \, exp (-\frac{(x-\mu_2)^2}{2 \sigma_2^2}) \\
  (\frac{1}{\sqrt{4 \pi}}) \, exp (-\frac{(x-5)^2}{4}) &= (\frac{1}{\sqrt{8 \pi}}) \, exp (-\frac{x^2}{8}) \\
  log\sqrt{2} - \frac{x^2 - 10x + 25}{4} &= -\frac{x^2}{8} \\
  8log\sqrt{2} - 50 &= x(x - 20) \\
  x &= 10 - \sqrt{50 + log16} \\
  x &\approx 2.7355
\end{align*}

% \subsection{(Optional) Prove decision boundary is $x = \frac{\mu_1 + \mu_2}{2}$}

\begin{align*}
  P(x|w_1) \, P(w_1) &= P(x|w_2) \, P(w_2) \\
  \text{Since } P(w_1) &= P(w_2) = 0.5 \\
  P(x|w_1) &= P(x|w_2) \\
  (\frac{1}{\sqrt{2 \pi \sigma^2}}) \, exp (-\frac{(x-\mu_1)^2}{2 \sigma^2}) &= (\frac{1}{\sqrt{2 \pi \sigma^2}}) \, exp (-\frac{(x-\mu_2)^2}{2 \sigma^2}) \\
  x^2 - 2x\mu_1 + \mu_1^2 &= x^2 - 2x\mu_2 + \mu_2^2 \\
  x &= \frac{\mu_2^2 - \mu_1^2}{2\mu_2 - 2\mu_1} \\
  x &= \frac{(\mu_2^2 - \mu_1^2)(\mu_2 + \mu_1)}{2(\mu_2 - \mu_1)(\mu_2 + \mu_1)} \\
  x &= \frac{\mu_2 + \mu_1}{2}
\end{align*}

\section{Housekeeping Genes Prediction}

\subsection{Data Items}
How many items are NaN in the $is\_hk$ column? How many items are known
housekeeping genes? How many items are known tissue specific genes?
  \begin{figure}[H]
    \noindent\makebox[\textwidth]{
      \setlength{\fboxsep}{0pt}\fbox{
        \includegraphics[width=0.2\paperwidth]{hk_count.png}
      }
    }
    \end{figure}
\subsection{Observe Histogram}
Observe the histograms. Can we use a Gaussian to estimate this histogram?
Why? What about a Gaussian Mixture Model (GMM)?

\begin{figure}[H]
  \centering
  \includegraphics[width=1\textwidth]{cdn_before.png}
  \end{figure}

We should not use Gaussian to estimate this histogram because there are some little details that
Gaussian could not capture. I think it can archive very high accuracy with only single Gaussian but if you want to boost up some accuracy.
So it is better to use GMM that can capture some complex curve inside these information.

\subsection{Zero Bins}
How many bins have zero counts? Do you think this is a good discretization?
Why?

\begin{figure}[H]
  \noindent\makebox[\textwidth]{
    \setlength{\fboxsep}{0pt}\fbox{
      \includegraphics[width=0.4\paperwidth]{hk_zero_bins.png}
    }
  }
  \end{figure}

The number of zero bins is about 82.3\% of total amount of bins. It is not a good discretization
because amount of bin that include zero are too much.

\subsection{New Discretization}

Plot the histogram according to our new discretization scheme just like the
process above (with ∼1000 bins, and show 3 plots). Does it come out like how
it should be?

\begin{figure}[H]
  \noindent\par
  \raisebox{-.5\height}{\includegraphics[width=0.48\textwidth]{cdna.png}}%
  \hfill
  \raisebox{-.5\height}{\includegraphics[width=0.47\textwidth]{cds.png}}%
  \par
  \caption{(Left) cDNA density plot with 70 spacing (Right) CDS density plot with 100 spacing}
  \end{figure}

It does come out like how it should be. The first half of data is swing while the other half are normal uniform,
which is similar to the first plot that first half of data contain a lot of data (It is easiler to swing) and other half has no data.

\subsection{Likehood Distribution}

What is the MLE for the likelihood distributions of each of the 9 features? Plot
the likelihood distributions.

\begin{figure}[H]
  \centering
  \includegraphics[width=1\textwidth]{mle_1.png}
  \end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[width=1\textwidth]{mle_2.png}
  \end{figure}

\subsection{Prior}
What is the prior distribution of the two classes?
\begin{align*}
P(is\_hk) &= 0.133766 \\
P(is\_not\_hk) &= 0.866234
\end{align*}

\subsection{Prediction}
Use the learned distributions to classify the test set. Don’t forget to allow
your classifier to handle missing values in the test set. Report the overall
Accuracy. Then, report the Precision, Recall, and F score for detecting
housekeeping gene.
\begin{align*}
Accuracy &= 0.987179 \\
Precision &= 0.916667 \\
Recall &= 1.000000 \\
F1 &= 0.956522 \\
\end{align*}

\subsection{Baseline Random Choice}
The random choice baseline is the accuracy if you make a random guess for
each test sample. Give random guess (50\% being housekeeping, and 50\%
being not housekeeping) to the test samples. Report the overall Accuracy.
Then, report the Precision, Recall, and F score for detecting housekeeping
gene using the random choice baseline
\begin{align*}
Accuracy &= 0.525641 \\
Precision &= 0.175000 \\
Recall &= 0.636364 \\
F1 &= 0.274510
\end{align*}

\subsection{Baseline Majority}
The majority rule is the accuracy if you use the most frequent class from the
training set as the classification decision. Report the overall Accuracy. Then,
report the Precision, Recall, and F score for detecting housekeeping gene using
the majority rule baseline.
\begin{align*}
Accuracy &= 0.858974 \\
Precision &= 0.000000 \\
Recall &= 0.000000 \\
F1 &= 0.000000
\end{align*}

\begin{tabular}{l*{4}{c}r}
  Algorithm              & Accuracy & Precision & Recall & F1 \\
  \hline
  Random Choice   & 52.56 & 17.50 & 63.64  & 27.45  \\
  Majority        & 85.90 & 0.00  & 0.00   & 0.00  \\
  Naive Bayes     & 98.72 & 91.67 & 100.00 & 95.65  \\
  \end{tabular}

\subsection{Threshold Value}
Use the following threshold values
$t = np.arange(-5,5,0.05)$
find the best accuracy, and F score (and the corresponding thresholds)

\begin{figure}[H]
  \noindent\makebox[\textwidth]{
    \setlength{\fboxsep}{0pt}\fbox{
      \includegraphics[width=0.45\paperwidth]{best_acc.png}
    }
  }
  \end{figure}

\subsection{RoC}
Plot the RoC of your classifier.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.8\textwidth]{roc_less.png}
  \end{figure}

\subsection{RoC with different bin size}
Change the number of discretization bins from ${\sim}1000$ to ${\sim}500$. What happens to the RoC curve? Which discretization is better? The number of discretization bins can be considered as a hyperparameter, and must be chosen by comparing the final performance.

\begin{figure}[H]
  \noindent\par
  \raisebox{-.5\height}{\includegraphics[width=0.48\textwidth]{roc_less.png}}%
  \hfill
  \raisebox{-.5\height}{\includegraphics[width=0.49\textwidth]{roc_much.png}}%
  \par
  \caption{(Left) ROC of ${\sim}1000$ bins (Right) ROC of ${\sim}50$ bins}
  \end{figure}

The RoC curve with large amount of bins is trend to be more beautiful than the curve with less bins data.
We will see if we decrease the size of decreatization bins, the true positive rate will be decrease,
and errors will be increase, which cause the accuracy of train dataset to be drop-off.

\section*{Code Submission}

I have submitted 4 files of code consist of:

\begin{enumerate}
  \item \textbf{simple-bayes-classifier.ipynb} -- Bayes graph plot in task \ref{sec:bayes}
  \item \textbf{gene-prediction-gaussian.ipynb} -- Gene prediction source code, finding MLE by using Gaussian method.
  \item \textbf{gene-prediction-bin-bucket.ipynb} -- Gene prediction source code, finding MLE from decreatization from train data.
  \item \textbf{predict.csv} -- The prediction of $unsup\_train\_set$
\end{enumerate}
Feel free to use Docker by enter
\begin{lstlisting}[language=bash]
  docker-compose up
\end{lstlisting}
to start.

\end{document}


\documentclass[10pt]{article}
\title{The Dana Scott Recurrence}
\author{{\sc Hal Canary} \\
{\small University of Wisconsin--Madison}}
%\date{March 15, 2004}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{graphicx}
\hypersetup{pdfstartview=FitH,pdfauthor={},
pdftitle={Newton's Recurrence}}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}

\setlength{\textwidth}{6.3in}
\setlength{\textheight}{8.7in}
\setlength{\topmargin}{0pt}
\setlength{\headsep}{0pt}
\setlength{\headheight}{0pt}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{0pt}

\newcommand{\R}{\mathbb{R}}
\newcommand{\onto}{\rightarrow}

\begin{document}

\maketitle

%\begin{abstract}
%\end{abstract}

\section{Introduction}

In an article on the Somos Sequence \cite{gale91}, David Gale mentions a
recurrence discovered by Dana Scott,
\begin{equation}\label{scott}
a_n a_{n-4} = a_{n-1} a_{n-3} + a_{n-2} \qquad a_1=a_2=a_3=a_4=1,
\end{equation}
and mentions that there exists a number theoretical proof that it
always gives integers. We will present an alternative proof.

Emilie Hogan found a family of similar recurrences indexed by the
parameter $k$ ($k$ is odd):
\begin{equation}\label{hogan}
a_n a_{n-k} = a_{n-1} a_{n-(k-1)} + a_{n-(k-1)/2} + a_{n-(k+1)/2}.
\end{equation}
Hopefully, (\ref{hogan}) can be approached just like we will approach
(\ref{scott}).

\section{Linearizing}

Equation (\ref{scott}) generates the sequence $\{1,$ $1,$ $1,$ $1,$
$2,$ $3,$ $5, 13,$ $22,$ $41,$ $111,$ $191,$ $361,$ $982,\ldots\}$.
Using a computer, we found that this sequence grows like
$\textrm{O}(C^n)$.  This suggested that the sequence satisfied a
linear recurrence, which we proceeded to find:
\begin{equation}\label{linscott}
0 = a_n - 10~ a_{n-3} + 10~ a_{n-6} - a_{n-9}.
\end{equation}
The characteristic equation of (\ref{linscott}) is
\[
0 = x^9 - 10~ x^6 + 10~ x^3 - 1.
\]
Finding the explicit formula for a linear recurrance is fairly simple,
but time-consuming.  It is better to ask a computer to do it.  The
following code will produce the explicit formula.
\begin{quote}
\begin{verbatim}
#begin Maple code
# Manually enter the characteristic polynomial.
chari := (x) -> x^9 - 10*x^6 + 10*x^3 - 1:
# order of characteristic polynomial.
N:=9:
# quadratic recurrance.
quadrat := proc(n) option remember;
  if n<3 then 1
  else return((quadrat(n-1)*quadrat(n-3)+
    quadrat(n-2))/quadrat(n-4));
  fi;
end:

# Solve for roots of characteristic polynomial.
routes := solve(chari(x),x) :
# Get initial conditions from quadratic.
initial := seq(quadrat(n),n=0..N-1):
# Solve for coefficients.
assign(solve({seq( add( coffs[i]*routes[i]^(j-1) ,i=1..N)
  = initial[j],j=1..N)}, {seq(coffs[i],i=1..N)})):
# Explicit function.
explicit := (n) ->
  simplify(add( coffs[i]*routes[i]^n ,i=1..N)):
# Print out explicit formula.
interface(prettyprint=false):
explicit(n);
#verify that explicit satisfies the quadratic recurrence.
evalb(simplify(explicit(n)*explicit(n-4)) =
 simplify(explicit(n-1)*explicit(n-3)+explicit(n-2)));
#end Maple code
\end{verbatim}
\end{quote}
If the last line of that code returns \emph{true} (which it doesn't),
then we have just proved that the Dana Scott Recurrence is equivilant
to the recurrence given in (\ref{linscott}), and since a linear
recurrance that starts with integers always gives integer, the Dana
Scott Recurrence wil also always give integers.

\section{A Better Proof}

Define the sequence $\{a\}$ recursivly:
\begin{equation}\label{linscott2}
	a_n = 10~ a_{n-3} - 10~ a_{n-6} + a_{n-9}.
\end{equation}
With the intial conditions
$(a_1{}\ldots{}a_9 ) = (1, 1, 1, 1, 2, 3, 5, 13, 22)$.

We wish to prove by induction that $\{a\}$ is the same as the Dana
Scott recurrance, that it satisifes
\[     	a_n a_{n-4} - a_{n-1} a_{n-3} - a_{n-2} = 0.	\]

For convienence, let
\[	\phi(n) := a_n a_{n-4} - a_{n-1} a_{n-3} - a_{n-2}	\]
Assume that $\phi(k)=0$ for $k < n$.  Show that $\phi(n) = 0$.
This gives:
\begin{eqnarray*}
	\phi(n-1) & =& a_{n-1} a_{n-5} - a_{n-2} a_{n-4} - a_{n-3} = 0 \\
	\phi(n-2) & =& a_{n-2} a_{n-6} - a_{n-3} a_{n-5} - a_{n-4} = 0\\
	\phi(n-3) & =& a_{n-3} a_{n-7} - a_{n-4} a_{n-6} - a_{n-5} = 0\\
	\phi(n-4) & =& a_{n-4} a_{n-8} - a_{n-5} a_{n-7} - a_{n-6} = 0\\
	\phi(n-5) & =& a_{n-5} a_{n-9} - a_{n-6} a_{n-8} - a_{n-7} = 0\\
	\phi(n-6) & =& a_{n-6} a_{n-10} - a_{n-7} a_{n-9} - a_{n-8} = 0\\
	\phi(n-7) & =& a_{n-7} a_{n-11} - a_{n-8} a_{n-10} - a_{n-9} = 0\\
	\phi(n-8) & =& a_{n-8} a_{n-12} - a_{n-9} a_{n-11} - a_{n-10} = 0\\
	\phi(n-9) & =& a_{n-9} a_{n-13} - a_{n-10} a_{n-12} - a_{n-11} = 0\\
	\phi(n-10)& =& a_{n-10} a_{n-14} - a_{n-11} a_{n-13} - a_{n-12} = 0\\
	& \ldots &
\end{eqnarray*}

Now, compute $\phi(n)$.
\[	\phi(n) = a_{n}  a_{n-4} -  a_{n-1}  a_{n-3} - a_{n-2} \]
Substitute for $a_{n}$ and $a_{n-1}$ from the definition of $\{a\}$.
\begin{eqnarray*}
	a_{n}   &=& 10  a_{n-3} - 10  a_{n-6} + a_{n-9} \\
	a_{n-1}	&=& 10  a_{n-4} - 10  a_{n-7} + a_{n-10}
\end{eqnarray*}
\begin{eqnarray*}
\phi(n) &=&   ( 10  a_{n-3} - 10  a_{n-6} + a_{n-9}  )  a_{n-4}\\
        && - ( 10  a_{n-4} - 10  a_{n-7} + a_{n-10} )   a_{n-3} \\
	&& - ( 10  a_{n-5} - 10  a_{n-8} + a_{n-11} )
\end{eqnarray*}
Simplify:
\begin{eqnarray*}
	\phi(n) &=&  10 a_{n - 3} a_{n - 7} - 10 a_{n - 4} a_{n - 6} - 10 a_{n - 5} \\
		&&+ a_{n - 4} a_{n - 9} - a_{n - 3} a_{n - 10} + 10 a_{n - 8} - a_{n - 11}
\end{eqnarray*}
Since $\phi(n-3)=0$,
\begin{eqnarray*}
	\phi(n) &=&  a_{n - 4} a_{n - 9}  - a_{n - 3} a_{n - 10}\\
		&& + 10 a_{n - 8}  - a_{n - 11}
\end{eqnarray*}

Substitute for $a_{n-3}$ and $a_{n-4}$ from the definition of $\{a\}$.
\begin{eqnarray*}
	a_{n-3} &=& 10  a_{n-6} - 10  a_{n-9}  + a_{n-12} \\
	a_{n-4} &=& 10  a_{n-7} - 10  a_{n-10} + a_{n-13}\end{eqnarray*}
\begin{eqnarray*}
	\phi(n) &=& (10  a_{n-7} - 10  a_{n-10} + a_{n-13} )  a_{n - 9} \\
	        && - (10  a_{n-6} - 10  a_{n-9}  + a_{n-12} ) a_{n - 10} \\
	        && + 10  a_{n - 8} \\
	        && - a_{n - 11} ;
\end{eqnarray*}
Simplify:
\begin{eqnarray*}
	\phi(n) &=& - 10 a_{n - 10}  a_{n - 6}  + 10 a_{n - 9}  a_{n -
		7}  + 10 a_{n - 8} \\
		&&  + a_{n - 9}  a_{n - 13} - a_{n - 10}  a_{n - 12} -
		a_{n - 11}
\end{eqnarray*}
\begin{eqnarray*}
	\phi(n) &=& - 10  ( + a_{n - 10}  a_{n - 6} - a_{n - 9}  a_{n - 7}
		  - a_{n - 8} )\\
		&& + a_{n - 9}  a_{n - 13} - a_{n - 10}  a_{n - 12} - a_{n - 11}
\end{eqnarray*}
Since $\phi(n-6) = \phi(n-9) = 0$
\[	a_{n-6} a_{n-10} - a_{n-7} a_{n-9} - a_{n-8} = 0 \]
\[	a_{n-9} a_{n-13} - a_{n-10} a_{n-12} - a_{n-11} = 0 \]
\[	\phi(n) = 0 \]
QED.

\section{Aside: Laurent Polynomials}

If we choose the first four terms of (\ref{scott}) to be $(w,x,y,z)$
instead of $(1,1,1,1)$, then the next five terms are
\[a_5=\frac{z x + y}{w}\]
\[a_6=\frac{yzx+y^2+zw}{wx}\]
\[a_7=\frac{yz^2x+zy^2+z^2w+zx^2+xy}{wxy}\]
\[a_8=\frac{yz^3x^2+2y^2z^2x+zy^3+z^3wx+z^2wy+z^2x^3+2zx^2y+xy^2+wy^2zx+wy^3+w^2yz}
{w^2xyz}\]
\[a_9=\frac{
\begin{array}{c}
yz^2x^3+x^2y^2z^3+x^2z^2w+2x^2zy^2+x^2zw^2+2xwyz^3+2xz^2y^3\\
+xz^2w^2y+xzwy+xzwy^3+xy^3+w^2z^3+2z^2y^2w+z^2w^3+2zw^2y^2+zy^4+wy^4
\end{array}
}{x^2yzw^2}\]
These are all Laurent polynomials, so if we choose the first nine
terms of $\{a\}$ to be $(w,x,y,z,a_5,a_6,a_7,a_8,a_9)$
instead of $(1, 1, 1, 1, 2, 3, 5, 13, 22)$, then by the
linearity of (\ref{linscott2}), all subsequent terms are Laurent
polynomials.

\begin{quote}
\begin{verbatim}
#begin Maple code
a := proc(n)
  if n = 1 then w
  elif n = 2 then x
  elif n = 3 then y
  elif n = 4 then z
  else simplify((a(n - 1)*a(n - 3) + a(n - 2))/a(n - 4))
  end if
end proc:
interface(prettyprint=false);
seq(a(n),n=1..9);
#end Maple code
\end{verbatim}
\end{quote}

\begin{thebibliography}{9}

\bibitem{gale91}
{\sc David Gale.}  ``The Strange and Surprising Saga of the Somos
Sequences.''  \emph{Mathematical Intelligencer} \textbf{13} 1 (1991)
40-42.

\end{thebibliography}

\end{document}


\documentclass{article}

\title{Convolutional Neural Networks}
\author{Vladimir Feinberg}

\input{../old/defs}
\usepackage{animate}

\begin{document}

\maketitle

A convolution is a mathematical operation that operates as layer in an ANN. Content is mostly from Dr. Goodfellow's \nurl{http://www.deeplearningbook.org/contents/convnets.html}{Deep Learning Book}, but also taken from \nurl{https://www.coursera.org/learn/neural-networks}{Dr. Hinton's Coursera Class}, lecture week 5.

Convolutional neural nets (CNNs) are principally used for computer vision, but they can be viewed as a broader form of parameter sharing that's applicable to many domains. In particular, CNNs find ``local patterns'' in grid topologies, possibly of 1 dimension (like time series: joint angle data for a robot or audio data) or more than two (such as 3D fMRI imaging). Multiple channels (such as colors, or later feature maps) can just be viewed as separate image maps or new dimensions in the data.

\section{Visual Difficulties}

Visual difficulties we'd like to avoid include segmentation (teasing apart an object from its background and other object), deformation, lighting, and viewpoint.

\section{Convolution}

Convolution is a linear operation that replaces a fully-connected layer in a neural network. Thus, whereas a fully-connected layer with a vector input $\vx$ and activation $f$ may look like $f(W\vx+\vb)$ for weight $W$ and bias $\vb$, a convolutional layer would behave like $f(\vw *\vx)$. In this context, the weight $\vw$ is referred to as a kernel, and $f$ is typically a rectifier followed by a pooling operation. It is common for multiple kernels to be applied to the same $\vx$, the pooled results of which form the output of the layer as feature maps. The terminology follows similarly when replacing the vectors $\vx,\vw$ with arbitrary tensors $\mathsf{X},\mathsf{W}$; convolutions are well-defined on them.

Convolution can be viewed as a linear operation. A 1D convolution is multiplication by a circulant matrix, a 2D convolution is multiplication by a doubly-circulant block matrix, and so on (\nurl{https://dsp.stackexchange.com/questions/35373/convolution-as-a-doubly-block-circulant-matrix-operating-on-a-vector}{example derivation}). Thus, indeed convolution is a form of parameter sharing and sparse interactions. Convolutions are typically implemented as cross-correlations. Even though this loses the commutativity property, these networks are just as expressive since we can just transpose the kernel. For small kernel sizes, convolution can be implemented much more efficiently than with their associated matrix multiplies.

Some nuance is required to deal with channels. A 2D image with width $w$ and height $h$ might have three RGB values at each pixel, so its input is $w\times h\times 3$. A convolutional layer with $k$ kernels applied to a greyscale image might also result in $k$ output feature maps of size $w\times h$. Given a 3D volume of 2D images, with size $w\times h\times d$, what does it mean to apply a 2D convolution with $k$ kernels?
\begin{itemize}
\item Usually, this ``full convolution'' refers to using $kd$ filters total, such that the $j$-th output layer, is the sum of $d$ independent kernels applied to each of the $d$ 2D inputs. For all $j\in[k]$, this gives our full volume output. Then, if our kernel size is $s$, the total number of parameters in this layer is $s^2kd$ (assuming no bias is added after convolution - per-kernel bias is very redundant in full convolutions).
\item A $1\times 1$ convolution, then, reduces to a matrix multiply from the space of the input channels to that of the output channels (i.e., it creates a linear relationship between the input and output).
  \item  Separable convolutions (\nurl{https://arxiv.org/abs/1610.02357}{Chollet et al 2016} have only $d$ regular kernels for each of the input feature maps, which run a convolution. Then a full convolution with $1\times 1$ kernels (which has $kd$ scalars total) is run. Thus, separable convolutions are much slimmer. More complicated filter connections do exist, but are sparingly used. See \nurl{https://medium.com/technologymadeeasy/the-best-explanation-of-convolutional-neural-networks-on-the-internet-fbb8b1ad5df8}{this overview} and \nurl{https://stats.stackexchange.com/questions/180850/how-are-filters-and-activation-maps-connected-in-convolutional-neural-networks}{this LeNet analysis}.
\end{itemize}

Modifications or common enhancements to convolution (see \nurl{https://arxiv.org/abs/1603.07285}{Dumoulin and Visin 2016}, Fig.~\ref{fig:conv}):
\begin{itemize}
\item Padding. Various paddings add zeros around the inputs and result in different output sizes. These dictate the role that partially applied filters at the boundary will play.
\item Strides. A stride of greater than one skips applications of the kernel filter, which can be viewed as a form of regularization through downsampling. Along a similar vein, diluted convolutions apply the kernel itself at strides along the input. A transposed form of convolution, deconvolution, reverses this process, upsampling compressed input (\nurl{http://www.matthewzeiler.com/pubs/cvpr2010/cvpr2010.pdf}{Zeiler 2010}).
\item Detectors. These are the nonlinearities applied after our linear convolutional transform.
\item Degrees of parameter sharing. Unshared convolutions learn a new kernel for each application of the kernel to an input feature map, these have no sharing and are called locally connected layers. Tiled convolutions rotate through a small set of kernels as a middle ground proposed by \nurl{https://papers.nips.cc/paper/4136-tiled-convolutional-neural-networks}{Le et al 2010}, but they're usually replaced by having many parallel regular convolutional layers now.
\end{itemize}

\begin{figure}[!h]
\centering
\animategraphics[autoplay,loop,width=0.5\textwidth]{1}{padding-movie/theano-pad-}{0}{35}
\caption{Demonstration of a convolutional layer with padding, no strides, and no dilation retrieved from \nurl{http://deeplearning.net/software/theano/tutorial/conv_arithmetic.html}{Theano docs}.}
\label{fig:conv}
\end{figure}

\begin{center}

\end{center}

\section{Pooling}

Pooling is typically applied after a convolutional layer/stage. It may be a nonlinear operation which is applied to a local neighborhood of the input, like a convolution. For various types of pooling and when to use them, see \nurl{http://www.di.ens.fr/willow/pdfs/icml2010b.pdf}{Boureau et al 2010}. Max-pools, for instance, encourage invariance between adjacent kernel outputs, since only one of them needs to be activated to activate the associated max pool.

Pools have a specified pool width (determining the size of the neighborhood that is pooled) and stride, which has the same effect as kernel stride.

Pooling typically reduces image width, so in cases where we would like to classify input pixels themselves, one solution is to simply produce an output at a lower resolution than the input (Fig.~\ref{fig:pinheiro}).

\begin{figure}[!h]
\centering
{\includegraphics[width=\textwidth]{pinheiro.pdf}}
\caption{Architecture of segmentation architecture from Fig.~1 of \nurl{https://arxiv.org/abs/1506.06204}{Pinheiro et al 2015})}
\label{fig:pinheiro}
\end{figure}

Pooling isn't always necessary (\nurl{https://arxiv.org/abs/1412.6806}{Springenberg et al 2014}), and maxout-like cross-channel pooling is an interesting response (\nurl{https://arxiv.org/abs/1510.00921}{Liu et al 2015}).

\section{Initialization}

\subsection{Random Kernel Intialization}

Random kernel initialization is surprisingly good, so much so that \nurl{https://cs.stanford.edu/~acoates/papers/coatesleeng_aistats_2011.pdf}{Coates et al 2011} even states that a network trained only on the last layer can be used as a selection method for CNN architectures.

\nurl{http://www.robotics.stanford.edu/~ang/papers/nipsdlufl10-RandomWeights.pdf}{Saxe et al 2010} finds that architectures with random weight initialization in CNNs can be further improved with unsupervised pretraining. See the initialization document for details.

\section{Preprocessing}

\begin{itemize}
\item Normalize data input channels from integer pixel values $[0,255]$ to floats on $[0,1]$.
\item Train on many cropped versions of the data and have classifiers vote (can this be re-interpreted as parallel CNNs, and if so how does sharing affect this technique?)
\item Denoise with respect to some cannoncial form of the data (rotations, skew). This can enable smaller models but some caution should be used. It is sometimes better to let the models figure out the cannonical form for themselves.
\item Global contrast normalization (GCN) standardizes pixel inputs; local contrast normalization (LCN) scopes deviation to a window (helps make edges stand out). GCN reduces pressure on the model to handle different scales of input intensity.
\item Random perturbations to input such as color changes or translations or rotations can help augment the dataset for better generalization (\nurl{http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf}{LeCun et al 1998b}, \nurl{https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks}{Krizhevsky et al 2012})
\end{itemize}

\section{Common Architectural Approaches}

\nurl{https://arxiv.org/abs/1512.00567}{Szegedy et al 2015} give several general strategies for CNN design:
\begin{enumerate}
\item Avoid representation bottlenecks. MLPs should gently decrease in hidden layer size (though representation complexity is more nuanced than just width). Correspondingly, for CNNs, use high-dimensional embeddings as outputs of CNN layers. In other words, the number of convolution kernels should increase to maintain information density (with the advantage being that the kernels will tease out individual features). This doesn't conflict with the first item of advice, since a convolutional layer doesn't have as much representational density as a fully connected one.
\item Convolutions can be preceded by low-dimensional embeddings, since strong local correlation in inputs can be compressed.
\item Width and depth should be balanced: optimal performance to compute is achieved by increasing both in parallel rather than one a lot.
\end{enumerate}

An excellent overview of individual techniques proposed in CNN architecture can be found in a \nurl{https://adeshpande3.github.io/adeshpande3.github.io/The-9-Deep-Learning-Papers-You-Need-To-Know-About.html}{blog post by Adit Deshpande}.

\end{document}
% LocalWords: Dumoulin Visin Boureau Ngiam autoplay Theano


\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx}
\usepackage{multicol}
\setlength{\oddsidemargin}{0in} \setlength{\evensidemargin}{0in}
\setlength{\topmargin}{0in} \setlength{\textheight}{8.5in}
\setlength{\textwidth}{6.5in}

\begin{document}
\begin{flushleft}
	\bfseries{MATH 260, Linear Systems and Matrices, Fall `14}\\
	\bfseries{Activity 2:  Matrix Operations}\\
%	\bfseries{Honor Code:} \hspace{3.5in}\bfseries{Names:}\\
\end{flushleft}
\begin{flushleft}
\vspace{.25in}

Matrices for today's problems:\\

$\textbf{A}=\left[
\begin{array}{c}
	2\\
	4\\
	-1\\
\end{array}
\right]
$
\hspace{.1in}
$\textbf{B}=\left[
\begin{array}{ccc}
3 & 2 & -5
\end{array}\right]$
\hspace{.1in}

$\textbf{C}=\left[
\begin{array}{cc}
1 & 4\\
-3 & 5
\end{array}
\right]
$
\hspace{.1in}
$\textbf{D}=\left[
\begin{array}{cc}
2 & 1\\
4 & 3
\end{array}
\right]$
\hspace{.1in}
$\textbf{E}=\left[
\begin{array}{ccc}
3 & 8 & 2\\
-1 & x & x^2
\end{array}
\right]
$

\section*{Warmup: Constants and Matrices}

Find $k\textbf{C}$ where $k$ is a constant. What is $k\textbf{C}$ if $k=3$?

\vspace{2in}

\section*{Problem 1:  Vectors, and Vectors with Matrices}

a) Find the expected dimension of \textbf{AB}, \textbf{BI$_3$}, and \textbf{BE}.

\newpage

b) Now calculate each product.

\vspace{5in}

\section*{Problem 2: Matrix on Matrix}

a) Find \textbf{CD}.

\vspace{2in}

b) Make a prediction (don't calculate yet): Is the statement:  $\textbf{CD} = \textbf{DC}$ true or false?

\newpage

c) Find \textbf{DC}. Were you correct?

\vspace{1.5in}

d) Can you find any two matrices (\textbf{A} and \textbf{B}) for which $\textbf{AB}=\textbf{BA}$?  Note that here, \textbf{A} and \textbf{B} are NOT the matrices given on page 1. \textit{Hint: This is a SPECIAL circumstance.}

\vspace{1in}

\section*{Problem 3: More Matrix on Matrix}

a) Which of these are true for any generic matrices where the operations are well-defined (give some reasoning, you don't \textit{have} to prove them):\\
$(\textbf{AB})\textbf{C}=\textbf{A}+\textbf{BC}$ \hspace{0.2in} $\textbf{A}(\textbf{B}+\textbf{C})=\textbf{AB}+\textbf{AC}$ \hspace{0.2in} $(\textbf{B}+\textbf{C})\textbf{A}=\textbf{AB}+\textbf{CA}$

\vspace{2in}

b) Under certain circumstances, we can raise a matrix to a power.  Consider the $m \times n$ matrix $\textbf{A}$.  Discuss at your table what some requirements on $\textbf{A}$ might be, and what the resultant dimensions of $\textbf{A}^{k}$ would be.

\newpage

c) Find $\textbf{CC}$.  This is equivalent to $\textbf{C}^2$.

\vspace{1.5in}

d) Find $\textbf{C}^3$. \textit{Hint: Use your result from (c) to reduce your work}

\vspace{1.5in}

e) Could you find $\textbf{E}^3$?  If you can, give its dimensions.  If you can't find it, explain why not?

\vspace{0.75in}

\section*{Problem 4: Transposing Matrices}

We can find what's called the ``transpose" of a matrix by swapping the matrix's rows and columns.  More technically, to find the transpose of \textbf{A}, denoted by $\textbf{A}^\texttt{T}$ for all $i,j$ $[a_{ij}]^\texttt{T}=[a_{ji}]$.

\vspace{0.2in}

Find $\textbf{E}^\texttt{T}$.

\end{flushleft}
\end{document}

Fall 2014 - Capaldi A/Activities/Activity02_MatrixOps.tex


\documentclass[../thesis.tex]{subfiles}

\begin{document}

    In this chapter we wish to study the derived categories of $A_\infty$-algebras. At the heart of homological algebra is the derived category of algebras, so it is only natural to ask how this category looks like in the $A_\infty$ case. In the last chapter we studied the relationship between the category of algebras and coalgebras to understand how quasi-isomorphisms between $A_\infty$-algebras worked. In this chapter we will instead study the relationship between module and comodule categories in order to understand how quasi-isomorphisms between $A_\infty$-modules will work. At the heart of this discussion are twisting morphisms $\alpha : C \rightarrow A$, which allows us to study the relationship between $Mod^A$ and $CoMod^C$.

    From twisting morphisms we will obtain functors $L_\alpha : CoMod^C \rightarrow Mod^A$ and $R_\alpha : Mod^A \rightarrow CoMod^C$ which create an adjoint pair of functors. Whenever the twisting morphism $\alpha$ is acyclic, this will in fact become a Quillen Equivalence.

    We wish to reuse all of the methods we have gained and acquired thorughout this thesis. This chapter will mostly be reformulation and recontextualization of previous definitions, concepts and techniques.

    \section{Twisting Morphisms}

        Twisting morphisms were already introduced in chapter 1. There, they were used mostly to be represented by the bar and cobar construction. Now we want twisting morphisms and twisting tensors to play a bigger role. In order to define the functors $L_\alpha$ and $R_\alpha$, these constructions will be crucial.

        \subsection{Twisted Tensor Products}

            Let $A$ be an augmented dg-algebra, $C$ a conilpotent dg-coalgebra and $\alpha : C \rightarrow A$ a twisting morphism. The right (left) twisted tensor product is the complex $C \otimes_\alpha A$ ($A\otimes_\alpha C$) together with the differential $d_\alpha^\bullet = d_{C\otimes A}^\bullet + d_\alpha^r$. The perturbation is defined as
            \begin{align*}
                d_\alpha^r = (\nabla_A\otimes id_C) \circ (id_A \otimes \alpha \otimes id_C) \circ (id_A \otimes \Delta_C).
            \end{align*}

            If $M$ is a right $A$-module and $N$ is a left $C$-comodule then the tensor product $M\otimes_\mathbb{K} N$ exists and is a $\mathbb{K}$-module with differential $d_{M\otimes N}$. We may define a perturbation to this differential as
            \begin{align*}
                d_\alpha^r = (\mu_M\otimes id_N) \circ (id_M \otimes \alpha \otimes id_N) \circ (id_M \otimes \nu_N).
            \end{align*}
            By using the same line of thought as proposition \ref{prop: twisted-differential}, there is a twisted tensor product $M\otimes_\alpha N$ with differential $d_\alpha^\bullet = d_{M\otimes N} + d_\alpha^r$.

            \begin{remark}
                Koszuls sign rule forces us to define the differential of the left twisted tensor product as $d_\alpha^\bullet = d_{N\otimes M} - d_\alpha^l$.
            \end{remark}

            \begin{definition}
                Suppose that $M\in Mod^A$ ($M\in Mod_A$) and $N\in CoMod_C$ ($N\in CoMod^C$), then the left (right) twisted tensor product is the $\mathbb{K}$-module $M\otimes_\alpha N$ ($N\otimes_\alpha M$).
            \end{definition}

            In this setting right handedness and left handedness for the twisted tensor product is more clear in this setting, as we only have an action or coaction from one of the chosen sides. Trying to force the other handedness on the twisted tensors would just be ill-defined.

            \begin{definition}
                Let $A$ be an augmented dg-algebra and $C$ a conilpotent dg-coalgebgra, such that there is a twisting morphism $\alpha: C\rightarrow A$. Given a linear map $f: N \rightarrow M$ between a right $C$-comodule $N$ and a right $A$-module $M$ we say that it is an $\alpha$ right twisted linear homomorphism, or just an  $\alpha$ twisted morphism, if it satisfies the following equation:
                \begin{align*}
                    \partial f - f\star \alpha = 0
                \end{align*}
            \end{definition}

            This definitions gives us a functor $Tw_\alpha : CoMod^C \times Mod^A \rightarrow Ab$ which is the collection of right twisting linear homomorphisms between a comodule and module.

            Suppose that $\alpha : C \rightarrow A$ is a twisting morphism. We define the functor $L_\alpha = \otimes_\alpha A : CoMod^C \rightarrow Mod^A$ as an arbitrary right twisted tensor product with $A$. This functor does indeed hit $Mod^A$ by using the free right $A$-module structure on $A$. Likewise, we define a functor $R_\alpha = \otimes_\alpha C : Mod^A \rightarrow CoMod^C$ as an arbitrary left twisted tensor product with $C$. This does also hit right $C$-comodules by using the free right $C$-comodule structure on $C$.

            \begin{proposition}
                Suppose that $\alpha : C \rightarrow A$ is a twisting morphism. The functor $L_\alpha$ and $R_\alpha$ form an adjoint pair of categories.
                \begin{center}
                    \begin{tikzcd}
                        CoMod^C \ar[bend left]{r}[]{L_\alpha} \ar[phantom]{r}[]{\bot} & Mod^A \ar[bend left]{l}[]{R_\alpha}
                    \end{tikzcd}
                \end{center}
            \end{proposition}

            \begin{proof}
                This proof breaks down to showing $CoMod^C(N, L_\alpha(M))\simeq Tw_\alpha(N,M) \simeq Mod^A(R_\alpha(N),M)$. This is a rutine calculation, much like the proof for \ref{thm: cobar-bar-adj}.
            \end{proof}

            Let $A$ be a dg-algebra, and $M$ a right $A$-module. Recall that by the Cobar-Bar adjunction \ref{thm: cobar-bar-adj} there exists a universal twisting morphism $\pi_A : BA \rightarrow A$. We define the bar-construction of $M$ as $B_AM = R_{\pi_A}M = M\otimes_{\pi_A}BA$. Likewise, given a conilpotent dg-coalgebra $C$ and $N$ a right $C$-comodule we define the cobar-construction as $\Omega_CN = L_{\iota_C}N = N\otimes_{\iota_C}\Omega C$. In these cases we obtain adjunctions $\Omega_{BA} \dashv B_A$ and $\Omega_C \dashv B_{\Omega C}$.

            Let $A$ and $B$ be two algebras and $f : A \rightarrow B$ is an algebra morphism. Then $f$ induces a functor between the module categories by restriction: $f^* : Mod^B \rightarrow Mod^A$. Since $A$ and $B$ considered as algebroids are small, and the category of abelian groups is cocomplete, so the left Kan extension (induction) along this functor exists.
            \begin{center}
                \begin{tikzcd}
                    Mod^B \ar[]{r}[]{f^*} & Mod^A \ar[bend right]{l}[above]{f_!}
                \end{tikzcd}
            \end{center}

            Dually, if $C$ and $D$ are two coalgebras and $g : C \rightarrow D$ is an coalgebra morphism. Then $g$ induces a functor between the module categories by composing: $g* : CoMod^C \rightarrow CoMod^D$. Since $C$ and $D$ considered as coalgebroids are small, and the category of abelian groups is complete, so the right Kan extension (co-induction) along this functor exists.
            \begin{center}
                \begin{tikzcd}
                    CoMod^C \ar[]{r}[]{g_*} & CoMod^D \ar[bend left]{l}[above]{g^!}
                \end{tikzcd}
            \end{center}

            \begin{lemma}
                Let $\tau : C \rightarrow A$ be a twisting morphism. The adjunction $(L_\tau, R_\tau)$ factors as $(f_{\tau !}, f_\tau^*)\circ (L_{\iota_C},R_{\iota_C})$ or $(L_{\pi_A},R_{\pi_A})\circ (g_{\tau *}, g_\tau^!)$.
            \end{lemma}

            \begin{proof}
                This follows from corollary \ref{cor: universal-twisting}, that is $\tau = f_\tau \circ \iota_C = \pi_A\circ g_\tau$.
            \end{proof}

            \begin{definition}
                A twisting morphism $f: C \rightarrow A$ is called acyclic if the counit of the adjunction $L_\alpha \dashv R_\alpha$ is a pointwise quasi-isomorphism.
            \end{definition}

            \begin{lemma}
                Let $A$ be an augmented dg-algebra and $C$ a conilpotent dg-coalgebra. The universal twisting morphisms $\pi_A$ and $\iota_C$ are acyclic.
            \end{lemma}

            \begin{proof}
                We start with $\pi_A$. Recall that $\pi_A$ is constructed as the twisting morphism corresponding to $id_{BA}$. This morphism is thus given as the projection onto the first dimension of $BA$, that is:
                \begin{align*}
                    & \pi_A{sa} = a \\
                    & \pi_A(sa\otimes ...) = 0
                \end{align*}

                We say that $\pi_A$ is acyclic if the counit $\varepsilon : L_{\pi_A}R_{\pi_A} \Rightarrow Id_{Mod^A}$ at each object $M$ is a quasi-isomorphism.

                For each $M$ in $Mod^A$, $L_{\pi_A}R_{\pi_A}M = M\otimes_{\pi_A}BA\otimes_{\pi_A}A$. We may split up the differential into two summands, $d_v$ and $d_h$. $d_v$ is the ordinary differential on the tensor product, while $d_h = (-d^l_{\pi_A}\otimes A) + M\otimes d_2 \otimes A + d^r_{\pi_A}$. Since $(d_v + d_h)^2 = 0$ and $d_v^2 = 0$ we can observe that $d_vd_h = -d_hd_v$ and $d_h^2 = 0$. This is evident as $d_v$ changes the homological degree while $d_h$ does not, so in order for both of the first equations to hold, the last two must hold as well. We almost obtain a double complex.
                \begin{center}
                    \begin{tikzcd}
                        ... \ar[]{r}[]{d_h} & M\otimes BA_{(i)}\otimes A \ar[]{r}[]{d_h} \ar[loop, distance = 2em]{}[above]{d_v} & ... \ar[]{r}[]{d_h} & M\otimes BA_{(1)}\otimes A \ar[]{r}[]{d_h} \ar[loop, distance=2em]{}[above]{d_v} & M \otimes A \ar[]{r}[]{0} \ar[loop, distance=2em]{}[above]{d_v} & 0
                    \end{tikzcd}
                \end{center}
                It is clear that the total complex of this "double complex" is in fact $L_{\pi_A}R_{\pi_A}M$. Moreover, the counit induces an augmentation to this complex resolution of $M$, denoted as $cone(\varepsilon_M)$.
                \begin{center}
                    \begin{tikzcd}
                        ... \ar[]{r}[]{d_h} & M\otimes BA_{(i)}\otimes A \ar[]{r}[]{d_h} \ar[loop, distance = 2em]{}[above]{d_{M\otimes BA_{(i)}\otimes A}} & ... \ar[]{r}[]{d_h} & M\otimes BA_{(1)}\otimes A \ar[]{r}[]{d_h} \ar[loop, distance=2em]{}[above]{d_{M\otimes BA_{(1)}\otimes A}} & M \otimes A \ar[]{r}[]{\varepsilon_M} \ar[loop, distance=2em]{}[above]{d_{M\otimes A}} & M \ar[loop, distance = 2em]{}[above]{d_M} \ar[]{r}[]{0} & 0
                    \end{tikzcd}
                \end{center}

                To see that this is in fact a resolution we define a morphism $h : cone(\varepsilon_M) \rightarrow cone(\varepsilon_M)$ of degree $-1$. It works by the following formula:
                \begin{align*}
                    h(m \otimes (sa_1 \otimes ... \otimes sa_n) \otimes a) = m \otimes (sa_1 \otimes ... \otimes sa_n \otimes sa) \otimes 1
                \end{align*}
                It is clear that $id_{cone(\varepsilon_M)} = d_hh - hd_h$ and $d_vh = hd_v$. Thus to see that the cone is acyclic we let $c\in cone(\varepsilon_M)$ be a cycle, that is $(d_v + d_h)(c) = 0$. Our goal is to show that $h(c)$ is a preimage of $c$ along $d_v + d_h$.
                \begin{multline*}
                    (d_v + d_h)\circ h(c) = d_v\circ h(c) + d_h\circ h(c) = h\circ d_v(c) + c + h\circ d_h(c) = h\circ (d_v + d_h)(c) + c = c
                \end{multline*}

                We will now treat $\iota_C$. By construction $\iota_C (c) = s^{-1}c$ if $c\notin \mathbb{K}$, as it is the restriction to $\bar{C}$ of the identity on $\Omega C$.

            \end{proof}

        \subsection{Model Structure on Module Categories}

            Let $A$ be an augmented dg-algebra, then we know that $Mod^A$ is a model category. By corollary \ref{cor: Model-Mod} we have a model structure where the fibrations, cofibrations and weak equivalences are given as follows:
            \begin{itemize}
                \item $f\in Ac$ is a weak equivalence if $f$ is a quasi-isomorphism.
                \item $f\in Fib$ is a fibration if $f^\#$ is an epimorphism.
                \item $f\in Cof$ is a cofibration if it has LLP to acyclic fibrations.
            \end{itemize}

            Every object in this category is fibrant as the morphism $0 : M \rightarrow 0$ is always an epimorphism.

        \subsection{Model Structure on Comodule Categories}

            Unless stated otherwise, for this section we fix $A$ to be an augmented dg-algebra, $C$ as a conilpotent dg-coalgebra and $\tau : C \rightarrow A$ as an acyclic twisting morphism. We endow $CoMod^C_{conil}$ with three classes of morphisms:
            \begin{itemize}
                \item $f\in Ac$ is a weak equivalence if $L_\tau f$ is a quasi-isomorphism.
                \item $f\in Fib$ is a cofibration if $f^\#$ is a monomorphism.
                \item $f\in Cof$ is a fibration if it har RLP to acyclic cofibrations.
            \end{itemize}

            \begin{thm}\label{thm: model-comod}
              The category $CoMod^C_{conil}$ with the three classes as above forms a model category. Every object is cofibrant, and those objects which is a direct summand of $R_\tau M$ for some $M\in Mod^A$ are fibrant. The adjoint pair $(L_\tau, R_\tau)$ is a Quilllen equivalence.
            \end{thm}

            We will call this model structure for the canonical model structure on $CoMod^C_{conil}$.

            To be able to prove this we will need some lemmata. This proof is essentially the same as the case for dg-coalgebras. The main difference is to show independence of the choice of a twisting morphism $\tau$. To this end we must establish the relationship between graded quasi-isomorphisms and weak equivalences, as well as a technical lemma.

            Recall that given a coaugmented coalgebra $C$ we have a filtration called the coradical filtration, defined as $Fr_iC = Ker(\bar{\Delta_C})^i$. If $N$ is a right $C$-comodule we may define the coradical filtration of $N$ as $Fr_iN = Ker(\bar{\omega_N}^i)$. This filtration is admissable, meaning it is exhaustive and $Fr_0N=0$.

            \begin{lemma}
                Let $C$ be a conilpotent dg-coalgebra, $M$ and $N$ be right $C$-comodules. Then any graded quasi-isomorphism $f: M \rightarrow N$ is a weak equivalence.
            \end{lemma}

            \begin{proof}
                This proof is identical to \ref{lem: graded-qif-are-w}.
            \end{proof}

            \begin{lemma}
                Let $M$ and $N$ be two objects of $Mod^A$. The functor $R_\tau$ sends a quasi-isomorphism $f: M \rightarrow N$ to a weak equivalence $R_\tau f: R_\tau M \rightarrow r_\tau N$.

                The unit of the adjunction $\eta : Id \rightarrow R_\tau L_\tau$ is a pointwise weak equivalence.
            \end{lemma}

            \begin{proof}
                $R_\tau f$ is a weak equivalence if $L_\tau R_\tau f$ is a quasi-isomorphism. By naturality of the counit we have the following commutative diagram.
                \begin{center}
                    \begin{tikzcd}
                        M \ar[]{d}[]{f} & L_\tau R_\tau M \ar[]{d}[]{L_\tau R_\tau f} \ar[]{l}[]{\varepsilon_M} \\
                        N & L_\tau R_\tau N \ar[]{l}[]{\varepsilon_N}
                    \end{tikzcd}
                \end{center}

                By assumption we know that all three of $f$, $\varepsilon_M$ and $\varepsilon_N$ are quasi-isomorphisms. It follows by the $2$-out-of-$3$ property that $L_\tau R_\tau f$ is a quasi-isomorphism as well.

                To show that $\eta : Id \rightarrow L_\tau R_\tau$ is a pointwise weak-equivalence, we must show that $L\eta$ is a pointwise quasi-isomorphism. Since $L_\tau$ is left adjoint to $R_\tau$ we know that $\eta$ is split on the image of $L_\tau$, i.e.
                \begin{align*}
                    \varepsilon_{L_\tau}\circ L_\tau\eta = id_{L_\tau}
                \end{align*}
                Since we know that the natural isomorphisms $\varepsilon$ and $id$ are pointwise quasi-isomorphisms, we get by the $2$-out-of-$3$ property that $L\eta$ is a pointwise quasi-isomorphism as well.
            \end{proof}

            \begin{lemma}
                The functor $L_\tau$ preserves cofibrations and sends weak-equivalences to quasi-isomorphisms.
            \end{lemma}

            \begin{proof}
                This proof is essentially the same as \ref{lem: bar-cobar-Quill-adj}.
            \end{proof}

            With the above lemmata we have now established that the adjunction $(L_\tau, R_\tau)$ forms a Quillen equivalence if $CoMod^C$ is a model category.

            The next lemma is a technical lemma which we need. There will not be given a proof for it, but this is lemma 2.2.2.9 in \cite{LefevreHasegawa03}.

            \begin{lemma}
                Let $M$ be a right $A$-module and $N$ a right $C$-comodule. Let $p : M \rightarrow L_\tau N$ be a fibration of modules. The projection $j : R_\tau M \prod_{R_\tau L_\tau N} N \rightarrow R_\tau M$ is an acyclic cofibration of comodules.
            \end{lemma}

            \begin{proof}
                This proof is omitted.
            \end{proof}

            \begin{proof}[Proof of \ref{thm: model-comod}]
                With the above lemmata established, this proof is identical to the proof of \ref{thm: model-coalg}.
            \end{proof}

        \subsection{Triangulation of Homotopy Categories}
            In this section we will show that the homotopy categories are triangulated. If we look at the category $Mod^A$ we will observe that the category $HoMod^A$ is our beloved derived category $\mathcal{D}(A)$. It is not quite the same for the cateogry $CoMod^C$. Here we want $HoCoMod^C$ to be equivalent to the derived category of a ring, so we will see that the derived category is a further localization of $HoCoMod^C$.

            We will use the theory of triangulated categories to show that the model structure on $CoMod^C$ is independent on the choice of acyclic twisting morphism. This breaks down to show that every acyclic twisting morphism induce an equivalence between derived categories.

        \subsection{The Fundamental Theorem of Twisting Morphisms}

    \section{Polydules}
\end{document}