\section{The Erone's model}
As discussed in the previous chapters, once some points of interest are determined in the wheel profile, it is simple to estimate its measures: in fact, they are simple distances between the keypoints, along a specific axis. However, the estimation of the wheel diameter is not so trivial. As shown in Section \ref{sec:sys-cmp}, there are many models used to estimate the diameter, and all of them require at least three point to reach this goal. Furthermore, many of them can be improved by using more points along the profile. In systems like the one we are considering, we need more laser-camera pair to increase the number of the profiles acquired at the same time, thus the number of detectable rolling points is increased. Another solution could be to project a single laser beam perpendicularly to the wheel rolling section, so with a single stripe we are able to collect more than three points. \\
The second problem is the choice of the fitting algorithm, in order to correctly identify the shape of the rolling section. As we already said, rail wheels are conical-shaped, thus it is extremely important to identify the correct plane in which the rolling section lies. Also in this case, there are a lot of algorithm that solve this problem, and our choice has been the Erone's formula. \\

The Erone's formula is a general mathematical model that allows to estimate the area of a triangle, when the lengths of its sides are known. Let $a$, $b$ and $c$ are these length, the equation for the area is given by:
  \begin{equation}
    A = \frac{\sqrt{( a + b + c )( - a + b + c )( a - b + c )( a + b - c )}}{4}
    \label{eq:area}
  \end{equation}
It is simple to see that the detected rolling points along the profile could be seen as the vertexes of a triangle inscribed in a circumference, thus $a$, $b$ and $c$ can be estimated as the reciprocal distance between these points. % An example of this approximation is shown in Figure \ref{fig:triang-inscritto}.
%  \begin{figure}[b!]
%    \centering
%    \includegraphics[width=0.4\textwidth]{./images/wpms/triang-insc.png}
%    \caption{Representation of the triangle obtained from the rolling points detected from the wheel profiles}
%    \label{fig:triang-inscritto}
%  \end{figure}
Hence, we can define the diameter $D$ of the circle circumscribed to the triangle as follows:
  \begin{equation*}
    D = \frac{a\cdot b\cdot c}{2A}
  \end{equation*}
and replacing $A$ with Equation \ref{eq:area}, we can conclude that:
  \begin{equation}
    D = \frac{2\cdot a\cdot b\cdot c}{\sqrt{( a + b + c )( - a + b + c )( a - b + c )( a + b - c )}}
    \label{eq:diam:erone}
  \end{equation}
~\\

As we have done for the model proposed in Chapter \ref{ch::model}, we are interested in determining the error made while we are evaluating this property of the wheel, and if possible, to develop a model that allows to determine the better configuration during the design of the system. Thus, we used the same approach as before, and the equation for the propagation error is:
  \begin{equation}
    \sigma_D = \sqrt{
      \left( \frac{\partial D}{\partial a} \sigma_a \right)^2 +
      \left( \frac{\partial D}{\partial b} \sigma_b \right)^2 +
      \left( \frac{\partial D}{\partial c} \sigma_c \right)^2
    }
    \label{eq:diam-prop-1}
  \end{equation}
However, we had some problems to evaluate the errors $\sigma_a$, $\sigma_b$ and $\sigma_c$. Each point used as vertex for the triangle, was obtained from a different laser-camera pair. Accordingly with what we have done in Chapter \ref{ch::model}, each of these pairs could be set in a different way, depending by the requirements, hence the weight of the noise in the final spot detection, can be different pair by pair.

To solve this problem we thought to project all the points in a common reference system, like the one shown in Figure \ref{fig:common-rs}.
  \begin{figure}[t!]
    \centering
    \includegraphics[width=\textwidth]{./images/wpms/diam_rs.png}
    \caption{Example of the common reference system used for all the detected rolling points}
    \label{fig:common-rs}
  \end{figure}
In this way, each vertex can be defined by a vector, starting from the laser projector to the point itself. We called this vector $y_i$. Using simple trigonometric relationships, we can decompose each vector in the its components $w_i$ and $z_i$ as follows:
  \begin{equation}
    \begin{matrix}
      w_i = y_i \cdot \sin \theta_i \\ \\
      z_i = y_i \cdot \cos \theta_i + H_i \\
    \end{matrix}
    \label{eq:components}
  \end{equation}
where $\theta_i$ is the triangulation angle and $H_i$ is the offset of the laser projector with respect to the origin of the coordinate system. At this point, it is simple to compute the edges length as:
  \begin{equation}
    \left\{
    \begin{matrix}
      & a = \sqrt{(w_1 - w_2)^2 + (z_1 - z_2)^2} \\ \\
      & b = \sqrt{(w_2 - w_3)^2 + (z_2 - z_3)^2} \\ \\
      & c = \sqrt{(w_3 - w_1)^2 + (z_3 - z_1)^2}
    \end{matrix}
    \right.
    \label{eq:edges-len}
  \end{equation} \\
Hence, replacing in Equation \ref{eq:diam-prop-1} the results obtained in Equations \ref{eq:edges-len} and \ref{eq:components}, we can conclude that the error is propagated accordingly with:
  \begin{equation}
    \sigma_D = \sqrt{
      \sum_{i = 1}^3 \left( \frac{\partial D}{\partial y_i} \sigma_{y_i} \right)^2 +
      \sum_{i = 1}^3 \left( \frac{\partial D}{\partial H_i} \sigma_{H_i} \right)^2 +
      \sum_{i = 1}^3 \left( \frac{\partial D}{\partial \theta_i} \sigma_{\theta_i} \right)^2
    }
    \label{eq:diam-prop-2}
  \end{equation} \\

The last step, is linking the Equation \ref{eq:diam-prop-2} with the output of the Equations \ref{eq:radial-compensations}. In real conditions, the transformations between the laser-camera pairs and the common coordinate reference system are allowed by a second calibration process. As discussed in Section \ref{sec:teo-calibration}, the camera calibration can determine the intrinsic and extrinsic parameters of a single camera. However, this process can be modified in order to align two or more cameras at the same reference system, as in this case. \\
Ideally, the two reference systems are related to each other through a rototranslation, i.e. a linear transformation. This means that the vector $y_i$ remains the same, except for the values of its coordinates. Thus, we can determine the norm of $y_i$ as:
  \begin{equation*}
    |y_i| = \sqrt{x_w^2 + y_w^2 + z_w^2}
  \end{equation*}
where $\left( x_w, y_w, z_w \right)$ are the coordinates evaluated with Equations \ref{eq:radial-compensations}, and $z_w = 0$ accordingly with the fact that the laser plane is arbitrarily put at $0$. Now, it is simple to determine $\sigma_{y_i}$ as:
  \begin{equation*}
    \sigma_{y_i} = \sqrt{
      \left( \frac{\partial y_i}{\partial x_w} \sigma_{x_w} \right)^2 +
      \left( \frac{\partial y_i}{\partial y_w} \sigma_{y_w} \right)^2
    }
    % = \frac{x}{\sqrt{x_w^2 + y_w^2}}
  \end{equation*}
where $\sigma_{x_w}$ and $\sigma_{y_w}$ are the value computed with Equations \ref{eq:err-radial-comp-xw} and \ref{eq:err-radial-comp-yw}, respectively. \\

Concerning the estimation of $\sigma_{H_i}$ and $\sigma_{\theta_i}$, the things are simpler. The errors of $H_i$ are a constructive parameters, i.e. we can set them reasonably, with respect to the requirements of product construction. Vice-versa, $\theta_i$ are the same triangulation angles used in Chapter \ref{ch::model}, thus $\sigma_{\theta_i}$ can be computed accordingly with Equation \ref{eq:det_a}.


\section{Free precession\label{correctBrettAppendix}}

This argument is modelled after~\cite{Ruf:02}. The moment of inertia $\ve{L}$ of a rigid
body is defined as
\begin{equation}
\label{correctBrett1}
\ve{L} = \m{I}\ve{\omega}
\end{equation}
where $\m{I}$ is the inertia tensor and $\ve{\omega}$ is the angular velocity vector.
Torque is the rate of change of angular momentum over time:
\begin{equation}
\label{correctBrett2}
\ve{\tau} = \dot{\ve{L}} = \dot{\m{I}}\ve{\omega} + \m{I}\dot{\ve{\omega}}
\end{equation}

We can further evaluate $\dot{\m{I}}$ by writing \m{I} as a product with some rotation matrix
$\m{R}$ and its transpose:
\begin{equation}
\label{correctBrett3}
\m{I} = \m{R}\m{I}_\mathrm{body}\m{R}^T
\end{equation}

It can be shown that such a decomposition of the inertia tensor always exists, and that
$\m{I}_\mathrm{body}$ is a diagonal, time-invariant matrix containing the moments
of inertia about the body's principal axes~\cite{Feynman:63}. Hence we obtain
\begin{equation}
\label{correctBrett4}
\dot{\m{I}} = \dot{\m{R}}\m{I}_\mathrm{body}\m{R}^T +
    \m{R}\m{I}_\mathrm{body}\frac{\diff}{\diff t}\m{R}^T
\end{equation}

Witkin~\cite{BaraffWitkin:97} derives that $\dot{\m{R}} = \dual{\ve{\omega}}\,\m{R}$
for a rotation matrix $\m{R}$ and an angular velocity vector $\ve{\omega}$.
Using this identity and taking the differential operator onto the inside of the
transpose at the end of equation~\ref{correctBrett4},
\begin{eqnarray}
\dot{\m{I}} &=& \dual{\ve{\omega}}\,\m{R}\m{I}_\mathrm{body}\m{R}^T +
    \m{R}\m{I}_\mathrm{body}(\dual{\ve{\omega}}\,\m{R})^T \nonumber\\
&=& \dual{\ve{\omega}}\,\m{R}\m{I}_\mathrm{body}\m{R}^T -
    \m{R}\m{I}_\mathrm{body}\m{R}^T\dual{\ve{\omega}} \nonumber\\
&=& \dual{\ve{\omega}}\,\m{I} - \m{I}\dual{\ve{\omega}} \label{correctBrett5}
\end{eqnarray}

Substituting equation~\ref{correctBrett5} back into~\ref{correctBrett2}:
\begin{eqnarray}
\ve{\tau} & = & \m{I}\dot{\ve{\omega}} + \dual{\ve{\omega}}\,\m{I}\ve{\omega} -
    \m{I}\dual{\ve{\omega}}\,\ve{\omega} \nonumber \\
& = & \m{I}\dot{\ve{\omega}} + \dual{\ve{\omega}}\,\m{I}\ve{\omega} \label{correctBrett6}
\end{eqnarray}

Equation~\ref{correctBrett6} corrects the similar expression in~\cite{Saunders:PhD},
page~34. This means that the angular acceleration of a rigid body is given by
\begin{equation}
\label{correctBrett7}
\dot{\ve{\omega}} = \m{I}^{-1} (\ve{\tau} - \dual{\ve{\omega}}\,\m{I}\ve{\omega}).
\end{equation}
where \ve{\tau} is the sum of all torque vectors acting on the body. This means that even if
there are no torques, its angular velocity may change if \m{I} is not diagonal (i.e.\ if
the body is somehow asymmetric). This effect is called \emph{free precession}.

In a simulation, we usually integrate over torques to find angular momentum, and then calculate
the angular velocity from the momentum in each time step using the current moment of inertia. In
this case, the angular acceleration in equation~\ref{correctBrett7} is not needed. It is required
only for purposes of computing constraint forces and torques.


\subsection{Eigenvalues \& Eigenvectors}
\begin{definition}
	Let $A$ be an $n \times n$ matrix. A scalar $\lambda$ and a vector $\vec{v}$ are an eigenvalue and eigenvector of $A$ if
	\begin{equation*}
		A\vec{v} = \lambda\vec{v}
	\end{equation*}
\end{definition}
\noindent
We call $p(\lambda) = \det{(A - \lambda I)}$ the characteristic polynomial of $A$. The eigenvalues for $A$ are the solutions to the equation
\begin{equation*}
	p(\lambda) = \det{(A - \lambda I)} = 0
\end{equation*}
Once we have an eigenvalue, we can find the basis vectors for the corresponding eigenspace by solving the equation
\begin{equation*}
	\left( A - \lambda I \right)\vec{v} = \vec{0}
\end{equation*}
The basis vectors of the eigenspace for $A$ are the union of the basis vectors of each eigenspace corresponding to each eigenvalue.

\ifodd\includeBackgroundReviewExamples\input{./backgroundReview/matrices/eigenvaluesEigenvectors_example.tex}\fi


\section{The intuition behind SVD}

In this section we will provide several informal ways of looking at
the SVD factorization, aiming to ignite the formal discussion of next
section (where we prove the SVD theorem). \\

\input{svd-intuit-fcomp.tex}
\input{svd-intuit-cbasis.tex}
\input{svd-intuit-geoin.tex}

Although the geometrical interpretation works great for a simple
example in \R{2}, there are a couple of missing details in the action of
matrix $\Sigma$ which are worth mentioning. The first, is that the
dimensions of $\Sigma$ are those of the original matrix ($n \times
m$); therefore, its action is not only expanding or contracting, but
also a migration of space (from \R{n} to \R{m}). If there are more
rows than columns ($m > n$), the transformation $\Sigma$ will produce
a bigger vector than its input (the diagonal entries beyond position
$n$ will be zeroes, which in turn will fill the new vector entries
with zeroes as well; up to $m$ entries). If on the contrary we have
more columns than rows ($m < n$), then the effect will be a truncation
of the input vector (resulting vector has as many entries as rows in
$\Sigma$). In our example, this change of space was not perceived, as $n
= m$. \\

The second omitted detail about the action of $\Sigma$ in
\cref{fig:svd-geo-diag}, is even more subtle: along with the migration
of space \R{n} to space \R{m}, we are also changing the basis; from
$\{\vec{v_1},\vec{v_2},\dots,\vec{v_n}\}$ to
$\{\vec{u_1},\vec{u_2},\dots,\vec{u_m}\}$. This additional effect may
not be evident at all, but is thanks to an additional property that
is required on the two basis for the SVD factorization to hold:

\[
A\vec{v_i} = \sigma_i\vec{u_i}, \ds{}\forall i=1 \dots r, \ds{}\text{where
} r = rank(A).
\]
\hfill

The above property tells us that the vectors from the two basis were
picked in a very special way: each vector \vec{u_i} is parallel to the
image under $A$ of its associated \vec{v_i}; in other words, the
transformation $A$ maps the \R{n} basis
$\{\vec{v_1},\dots\,\vec{v_n}\}$, into vectors which are parallel to
the \R{m} basis $\{\vec{u_1},\dots\,\vec{u_m}\}$. Given that both basis
are orthonormal, a consequence from this observation is that the
orthogonality of the basis $\{\vec{v_1},\dots\,\vec{v_n}\}$ is preserved
under $A$ (\cite{kalman96}). This is not a trivial property, and not
every basis has it (given $A$ is assumed to be given). This is
actually the key problem of the SVD factorization, and the proofs
provided in the next section, focus around the problem of finding such
special basis.


\subsection{Ring ideals}\label{subsec:ring_ideals}

\begin{definition}\label{def:semiring_ideal}
  Let \( R \) be a semiring and \( I \) be a subset of \( R \). We say that \( I \) is an \term{ideal} (left, right or two-sided) if \( (I, +) \) is a subgroup of \( (R, +) \) and \( (I, \cdot) \) is a \hyperref[def:magma_ideal]{magma ideal} of \( (R, \cdot) \).
\end{definition}

\begin{proposition}\label{thm:semiring_ideal_is_nonunital_subsemiring}
  Two-sided semiring ideals are subsemirings. In the special case where the semiring is unital, an ideal is a unital subsemiring if and only if it is not a proper ideal.
\end{proposition}
\begin{proof}
  Follows from \fullref{thm:magma_ideal_is_submagma} and \fullref{thm:unital_magma_ideal_is_submagma_iff_contains_identity}.
\end{proof}

\begin{definition}\label{thm:semiring_ideal_iff_kernel}
  A subset of a ring is a two-sided \hyperref[def:semiring_ideal]{ideal} if and only if it is the \hyperref[def:semiring_kernel]{kernel} of some ring homomorphism.
\end{definition}
\begin{proof}
  \SufficiencySubProof Let \( I \) be a two-sided ideal. Since it is an abelian group, \( I \) is a normal subgroup and thus we can form the quotient \hyperref[def:normal_subgroup]{group} \( R / I \) with the canonical projection
  \begin{balign*}
     & \pi: R \to R / I       \\
     & \pi(x) \coloneqq x + I
  \end{balign*}

  Multiplication in \( R \) induces multiplication in \( R / I \) by
  \begin{equation*}
    (x + I) \cdot (y + I) \coloneqq (xy + I).
  \end{equation*}

  It is well-defined since if \( x + I = x' + I \) and \( y + I = y' + I \), then
  \begin{balign*}
    (x + I) (y + I)
     & =
    xy + (Iy + xI + II)
    =    \\ &=
    xy + I
    =    \\ &=
    x'y' + I
    =    \\ &=
    x'y' + (Iy' + x'I + II)
    =    \\ &=
    (x' + I) (y' + I).
  \end{balign*}

  Thus, the ring structure on \( R \) induces a ring structure on \( R / I \).

  The canonical projection \( \pi \) is an additive group homomorphism. Since we just showed that \( \pi(xy) = \pi(x) \pi(y) \), it follows that it is also a ring homomorphism.

  It only remains to show that \( \ker(\pi) = I \). Since \( I \) is closed under addition, naturally \( I \subseteq \ker(\pi) \). Conversely, if \( x \in \ker(\pi) \), then \( \pi(x) = \pi(0) = I \), i.e. \( x \in I \). Hence, \( \ker(\pi) = I \).

  \NecessitySubProof Let \( f: R \to T \) is a ring homomorphism. We must show that \( \ker(f) \) is an ideal. If \( x \in R \) and \( y \in \ker(f) \), then
  \begin{equation*}
    f(xy) = f(x) f(y) = f(x) 0 = 0.
  \end{equation*}

  Thus, \( xy \in \ker(f) \). Similarly, we can show that \( yx \in \ker(f) \). Thus, \( R \ker(f) = \ker(f) R = \ker(f) \) and \( \ker(f) \) is a two-sided ideal.
\end{proof}

\begin{definition}\label{def:generated_ring_ideal}
  Let \( R \) be a commutative ring, so that left and right ideals coincide. Let \( S \subseteq R \) be any nonempty subset of \( R \). We define the ideal generated by \( S \) equivalently as either
  \begin{thmenum}
    \thmitem{def:generated_ring_ideal/minimal} the smallest ideal of \( R \) that contains \( S \).
    \thmitem{def:generated_ring_ideal/direct} the ideal
    \begin{equation*}
      \braket S \coloneqq \left\{ \sum_{k=1}^n r_k s_k \mid r_1, \ldots, r_n \in R, s_1, \ldots, s_n \in S, n \in \BbbZ_{>0} \right\}
    \end{equation*}
    of finite linear combinations.

    \thmitem{def:generated_ring_ideal/polynomials} the ideal
    \begin{equation*}
      \braket S \coloneqq \left\{ p(s_1, s_2, \ldots, s_n) \mid s_1, \ldots, s_n \in S, p \in R[X_1, \ldots, X_n], n \in \BbbZ_{>0} \right\}
    \end{equation*}
  \end{thmenum}

  If \( S \) is finite, then \( \braket S \) is called \term{finitely generated}. If \( S = \{ s_1, \ldots, s_n \} \), then
  \begin{equation*}
    \braket S = s_1 R + s_2 R + \cdots s_n R.
  \end{equation*}
\end{definition}

\begin{definition}\label{def:principal_ideal}
  If an ideal \( I \) is \hyperref[def:generated_ring_ideal]{generated} by a single element, it is called a \term{principal ideal}.
\end{definition}

\begin{proposition}\label{thm:product_of_principal_ideals}
  In a commutative unital ring \( R \) the product of the principal ideals \( \braket{x} \) and \( \braket{y} \) is \( \braket{xy} \).
\end{proposition}


\section*{Exercises}

\begin{ex} The following is a Markov (migration) matrix for three locations
\begin{equation*}
\begin{mymatrix}{rrr}
\vspace{0.05in}\frac{7}{10} & \vspace{0.05in}\frac{1}{9} & \vspace{0.05in}\frac{1}{5} \\
\vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{7}{9} & \vspace{0.05in}\frac{2}{5} \\
\vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{9} & \vspace{0.05in}\frac{2}{5}
\end{mymatrix}
\end{equation*}
\begin{enumerate}
\item
Initially, there are $90$ people in location $1$, $81$ in location $2$, and $85$ in location $3$. How many are in each location after one time period?
\item
The total number of individuals in the migration process is $256$. After a long time, how many are in each location?
\end{enumerate}
\begin{sol}
\begin{enumerate}
\item  Multiply the given matrix by the initial state vector given by
$\begin{mymatrix}{r}
90 \\
81 \\
85
\end{mymatrix}$. After one time period there are $89$ people in location $1$, $106$ in location $2$, and $61$ in location $3$.
\item  Solve the system given by $(I - A) X_s = 0$ where $A$ is the migration matrix and $X_s = \begin{mymatrix}{c}
x_{1s} \\
x_{2s} \\
x_{3s}
\end{mymatrix}$ is the steady state vector. The solution to this system is given by
\begin{eqnarray*}
x_{1s} &=& \frac{8}{5} x_{3s} \\
x_{2s} &=& \frac{63}{25} x_{3s}
\end{eqnarray*}
Letting $x_{3s} = t$ and using the fact that there are a total of $256$ individuals, we must solve
\[
\frac{8}{5} t + \frac{63}{25} t + t = 256
\]
We find that $t=50$. Therefore after a long time, there are $80$ people in location $1$, $126$ in location $2$, and $50$ in location $3$.
\end{enumerate}
\end{sol}
\end{ex}

\begin{ex} The following is a Markov (migration) matrix for three locations
\begin{equation*}
\begin{mymatrix}{rrr}
\vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{2}{5} \\
\vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{1}{5} \\
\vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{2}{5}
\end{mymatrix}
\end{equation*}
\begin{enumerate}
\item Initially, there are $130$ individuals in location $1$, $300$ in location $2$, and $70$ in location $3$. How many are in each location after two time periods?

\item
The total number of individuals in the migration process is $500$. After a
long time, how many are in each location?
\end{enumerate}
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} The following is a Markov (migration) matrix for three locations
\begin{equation*}
\begin{mymatrix}{rrr}
\vspace{0.05in}\frac{3}{10} & \vspace{0.05in}\frac{3}{8} & \vspace{0.05in}\frac{1}{3} \\
\vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{3}{8} & \vspace{0.05in}\frac{1}{3} \\
\vspace{0.05in}\frac{3}{5} & \vspace{0.05in}\frac{1}{4} & \vspace{0.05in}\frac{1}{3}
\end{mymatrix}
\end{equation*}
The total number of individuals in the migration process is $480$. After a
long time, how many are in each location?
\begin{sol}
We solve $(I-A)X_s = 0$ to find the steady state vector $X_s = \begin{mymatrix}{c}
x_{1s} \\
x_{2s} \\
x_{3s}
\end{mymatrix}$.
The solution to the system is given by
\begin{eqnarray*}
x_{1s} &=& \frac{5}{6} x_{3s} \\
x_{2s} &=& \frac{2}{3} x_{3s}
\end{eqnarray*}
Letting $x_{3s} = t$ and using the fact that there are a total of $480$ individuals, we must solve
\[
\frac{5}{6} t + \frac{2}{3} t + t = 480
\]
We find that $t=192$. Therefore after a long time, there are $160$ people in location $1$, $128$ in location $2$, and $192$ in location $3$.
\end{sol}
\end{ex}

\begin{ex} The following is a Markov (migration) matrix for three locations
\begin{equation*}
\begin{mymatrix}{rrr}
\vspace{0.05in}\frac{3}{10} & \vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{1}{5} \\
\vspace{0.05in}\frac{3}{10} & \vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{7}{10} \\
\vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{1}{10}
\end{mymatrix}
\end{equation*}
The total number of individuals in the migration process is $1155$. After a
long time, how many are in each location?
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} The following is a Markov (migration) matrix for three locations
\begin{equation*}
\begin{mymatrix}{rrr}
\vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{1}{8} \\
\vspace{0.05in}\frac{3}{10} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{5}{8} \\
\vspace{0.05in}\frac{3}{10} & \vspace{0.05in}\frac{1}{2} & \vspace{0.05in}\frac{1}{4}
\end{mymatrix}
\end{equation*}
The total number of individuals in the migration process is $704$. After a
long time, how many are in each location?
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} A person sets off on a random walk with three possible locations. The Markov matrix of probabilities $A = [a_{ij}]$ is given by
\[
\begin{mymatrix}{rrr}
0.1 & 0.3 & 0.7 \\
0.1 & 0.3 & 0.2 \\
0.8 & 0.4 & 0.1
\end{mymatrix}
\]
If the walker starts in location $2$, what is the probability of ending back in location $2$ at time $n = 3$?
\begin{sol}
\[
X_{3} = \begin{mymatrix}{r}
0.38 \\
0.18 \\
0.44
\end{mymatrix}
\]
Therefore the probability of ending up back in location $2$ is $0.18$.
\end{sol}
\end{ex}

\begin{ex} A person sets off on a random walk with three possible locations. The Markov matrix of probabilities $A = [a_{ij}]$ is given by
\[
\begin{mymatrix}{rrr}
0.5 & 0.1 & 0.6 \\
0.2 & 0.9 & 0.2 \\
0.3 & 0 & 0.2
\end{mymatrix}
\]

It is unknown where the walker starts, but the probability of starting in each location is given by
\[
X_{0}
= \begin{mymatrix}{r}
0.2 \\
0.25 \\
0.55
\end{mymatrix}
\]
What is the probability of the walker being in location $1$ at time $n = 2$?
\begin{sol}
\[
X_{2} = \begin{mymatrix}{r}
0.367 \\
0.4625 \\
0.1705
\end{mymatrix}
\]
Therefore the probability of ending up in location $1$ is $0.367$.
\end{sol}
\end{ex}

\begin{ex} You own a trailer rental company in a large city and you have four
locations, one in the South East, one in the North East, one in the North
West, and one in the South West. Denote these locations by SE,NE,NW, and SW
respectively. Suppose that the following table is observed to take place.
\begin{equation*}
\begin{tabular}{lllll}
& SE & NE & NW & SW \\[0.5em]
SE & $\frac{1}{3}$ & $\frac{1}{10}$ & $\frac{1}{10}$ & $\frac{1}{5}$ \\[0.5em]
NE & $\frac{1}{3}$ & $\frac{7}{10}$ & $\frac{1}{5}$ & $\frac{1}{10}$ \\[0.5em]
NW & $\frac{2}{9}$ & $\frac{1}{10}$ & $\frac{3}{5}$ & $\frac{1}{5}$ \\[0.5em]
SW & $\frac{1}{9}$ & $\frac{1}{10}$ & $\frac{1}{10}$ & $\frac{1}{2}$ \\
\end{tabular}
\end{equation*}
In this table, the probability that a trailer starting at $NE$ ends in $NW$
is $1/10$, the probability that a trailer starting at $SW$ ends in $NW$ is
$1/5$, and so forth. Approximately how many will you have in each location
after a long time if the total number of trailers is $413$?
\begin{sol}
The migration matrix is
\[
A =
\begin{mymatrix}{rrrr}
\vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{1}{10} &\vspace{0.05in}\frac{1}{5} \\
\vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{7}{10} & \vspace{0.05in}\frac{1}{5} &\vspace{0.05in}\frac{1}{10} \\
\vspace{0.05in}\frac{2}{9} & \vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{3}{5} &\vspace{0.05in}\frac{1}{5} \\
\vspace{0.05in}\frac{1}{9} & \vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{1}{10} &\vspace{0.05in}\frac{1}{2}
\end{mymatrix}
\]
To find the number of trailers in each location after a long time we solve system $(I - A)X_s = 0$ for the steady state vector $X_s = \begin{mymatrix}{c}
x_{1s} \\
x_{2s} \\
x_{3s} \\
x_{4s}
\end{mymatrix}$.
The solution to the system is
\begin{eqnarray*}
x_{1s} &=& \frac{9}{10} x_{4s} \\
x_{2s} &=& \frac{12}{5} x_{4s} \\
x_{3s} &=& \frac{8}{5} x_{4s}
\end{eqnarray*}
Letting $x_{4s} = t$ and using the fact that there are a total of $413$ trailers we must solve
\[
\frac{9}{10} t + \frac{12}{5} t + \frac{8}{5}t  + t = 413
\]
We find that $t=70$. Therefore after a long time, there are $63$ trailers in the SE, $168$ in the NE, $112$ in the NW and $70$ in the SW.
\end{sol}
\end{ex}

\begin{ex} You own a trailer rental company in a large city and you have four
locations, one in the South East, one in the North East, one in the North
West, and one in the South West. Denote these locations by SE,NE,NW, and SW
respectively. Suppose that the following table is observed to take place.
\begin{equation*}
\begin{tabular}{lllll}
& SE & NE & NW & SW \\[0.5em]
SE & $\frac{1}{7}$ & $\frac{1}{4}$ & $\frac{1}{10}$ & $\frac{1}{5}$ \\[0.5em]
NE & $\frac{2}{7}$ & $\frac{1}{4}$ & $\frac{1}{5}$ & $\frac{1}{10}$ \\[0.5em]
NW & $\frac{1}{7}$ & $\frac{1}{4}$ & $\frac{3}{5}$ & $\frac{1}{5}$ \\[0.5em]
SW & $\frac{3}{7}$ & $\frac{1}{4}$ & $\frac{1}{10}$ & $\frac{1}{2}$ \\
\end{tabular}
\end{equation*}
In this table, the probability that a trailer starting at $NE$ ends in $NW$
is $1/10$, the probability that a trailer starting at $SW$ ends in $NW$ is
$1/5$, and so forth. Approximately how many will you have in each location
after a long time if the total number of trailers is $1469$.
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} The following table describes the transition probabilities between the
states rainy, partly cloudy and sunny. The symbol p.c. indicates partly
cloudy. Thus if it starts off p.c. it ends up sunny the next day with
probability $\frac{1}{5}$. If it starts off sunny, it ends up sunny the next
day with probability $\frac{2}{5}$ and so forth.
\begin{equation*}
\begin{array}{cccc}
& \text{rains} & \text{sunny} & \text{p.c.} \\
\text{rains} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{3} \\
\text{sunny} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{1}{3} \\
\text{p.c.} & \vspace{0.05in}\frac{3}{5} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{1}{3}
\end{array}
\end{equation*}
Given this information, what are the probabilities that a given day is
rainy, sunny, or partly cloudy? \vspace{1mm}
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} The following table describes the transition probabilities between the
states rainy, partly cloudy and sunny. The symbol p.c. indicates partly
cloudy. Thus if it starts off p.c. it ends up sunny the next day with
probability $\frac{1}{10}$. If it starts off sunny, it ends up sunny the
next day with probability $\frac{2}{5}$ and so forth.
\begin{equation*}
\begin{array}{cccc}
& \text{rains} & \text{sunny} & \text{p.c.} \\
\text{rains} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{3} \\
\text{sunny} & \vspace{0.05in}\frac{1}{10} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{4}{9} \\
\text{p.c.} & \vspace{0.05in}\frac{7}{10} & \vspace{0.05in}\frac{2}{5} & \vspace{0.05in}\frac{2}{9}
\end{array}
\end{equation*}
Given this information, what are the probabilities that a given day is
rainy, sunny, or partly cloudy?
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} You own a trailer rental company in a large city and you have four
locations, one in the South East, one in the North East, one in the North
West, and one in the South West. Denote these locations by SE,NE,NW, and SW
respectively. Suppose that the following table is observed to take place.
\begin{equation*}
\begin{tabular}{lllll}
& SE & NE & NW & SW \\[0.5em]
SE & $\frac{5}{11}$ & $\frac{1}{10}$ & $\frac{1}{10}$ & $\frac{1}{5}$ \\[0.5em]
NE & $\frac{1}{11}$ & $\frac{7}{10}$ & $\frac{1}{5}$ & $\frac{1}{10}$ \\[0.5em]
NW & $\frac{2}{11}$ & $\frac{1}{10}$ & $\frac{3}{5}$ & $\frac{1}{5}$ \\[0.5em]
SW & $\frac{3}{11}$ & $\frac{1}{10}$ & $\frac{1}{10}$ & $\frac{1}{2}$ \\
\end{tabular}
\end{equation*}
In this table, the probability that a trailer starting at $NE$ ends in $NW$
is $1/10$, the probability that a trailer starting at $SW$ ends in $NW$ is
$1/5$, and so forth. Approximately how many will you have in each location
after a long time if the total number of trailers is $407$?
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} The University of Poohbah offers three degree programs, scouting
education (SE), dance appreciation (DA), and engineering (E). It has been
determined that the probabilities of transferring from one program to
another are as in the following table.
\begin{equation*}
\begin{tabular}{l|l|l|l|}
\cline{2-4}
& SE & DA & E \\ \hline
\multicolumn{1}{|l|}{SE} & 0.8 & 0.1 & 0.3 \\ \hline
\multicolumn{1}{|l|}{DA} & 0.1 & 0.7 & 0.5 \\ \hline
\multicolumn{1}{|l|}{E} & 0.1 & 0.2 & 0.2 \\ \hline
\end{tabular}
\end{equation*}
where the number indicates the probability of transferring from the top
program to the program on the left. Thus the probability of going from DA to
E is $0.2$. Find the probability that a student is enrolled in the various
programs.
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} In the city of Nabal, there are three political persuasions,
republicans (R), democrats (D), and neither one (N). The following table
shows the transition probabilities between the political parties, the top
row being the initial political party and the side row being the political
affiliation the following year.%
\begin{equation*}
\begin{array}{cccc}
& \text{R} & \text{D} & \text{N} \\
\text{R} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{6} & \vspace{0.05in}\frac{2}{7} \\
\text{D} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{1}{3} & \vspace{0.05in}\frac{4}{7} \\
\text{N} & \vspace{0.05in}\frac{3}{5} & \vspace{0.05in}\frac{1}{2} & \vspace{0.05in}\frac{1}{7}
\end{array}
\end{equation*}
Find the probabilities that a person will be identified with the various
political persuasions. Which party will end up being most important?
%\begin{sol}
%\end{sol}
\end{ex}

\begin{ex} The following table describes the transition probabilities between the
states rainy, partly cloudy and sunny. The symbol p.c. indicates partly
cloudy. Thus if it starts off p.c. it ends up sunny the next day with
probability $\frac{1}{5}$. If it starts off sunny, it ends up sunny the next
day with probability $\frac{2}{7}$ and so forth.
\begin{equation*}
\begin{array}{cccc}
& \text{rains} & \text{sunny} & \text{p.c.} \\
\text{rains} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{2}{7} & \vspace{0.05in}\frac{5}{9} \\
\text{sunny} & \vspace{0.05in}\frac{1}{5} & \vspace{0.05in}\frac{2}{7} & \vspace{0.05in}\frac{1}{3} \\
\text{p.c.} & \vspace{0.05in}\frac{3}{5} & \vspace{0.05in}\frac{3}{7} & \vspace{0.05in}\frac{1}{9}
\end{array}
\end{equation*}
Given this information, what are the probabilities that a given day is
rainy, sunny, or partly cloudy?
%\begin{sol}
%\end{sol}
\end{ex}


\section{Fold}

In the general case, functions of two parameters are \textbf{not associative}, so the order in which one carries out the \textbf{combination of the elements matters}. On lists, there are two obvious ways to carry this out: either by recursively combining the first element with the results of combining the rest (called a right fold) or by recursively combining the results of combining all but the last element with the last one, (called a left fold).

\begin{lstlisting}[language=Haskell]
-- f :: a -> a -> a -> a
-- [a1, a2, a3, a4]
-- Right fold: f a1 (f a2 (f a3 a4))
-- Left fold: f (f (f a1 a2) a3) a4
\end{lstlisting}

Also, in practice, it is convenient and natural to have an initial value which in the case of a right fold, is used when one reaches the end of the list, and in the case of a left fold, is what is initially combined with the first element of the list.

\subsection{What fold does to lists}
One can view a fold on lists as replacing the nil at the end of the list with a specific value, and replacing each cons with a specific function. These replacements can be viewed as a diagram:

\begin{center}
  \textbf{Foldr} \\
  \includegraphics[width=0.6\textwidth]{figures/foldr-overview.png}
\end{center}

\begin{center}
  \textbf{Foldl} \\
  \includegraphics[width=0.7\textwidth]{figures/foldl.png}
\end{center}

They also highlight the fact that foldr (:) [] is the identity function on lists (a shallow copy in Lisp parlance), as replacing cons with cons and nil with nil will not change the result. The left fold diagram suggests an easy way to reverse a list, foldl (flip (:)) []

\clearpage
\subsection{Linear Foldr}
\begin{lstlisting}[language=Haskell]
-- if the list is empty, the result is the initial value z; else
-- apply f to the first element and the result of folding the rest

foldr' :: (a -> b -> b) -> b -> [a] -> b
foldr' f z []     = z
foldr' f z (x:xs) = f x (foldr' f z xs)

-- foldr' f id [a1, a2, a3, a4]
-- f a1 (foldr' f id [a2, a3, a4])
-- f a1 (f a2 (foldr' f id [a3, a4]))
-- f a1 (f a2 (f a3 (foldr' f id [a4])))
-- f a1 (f a2 (f a3 (f a4 (foldr' f id [])))
-- f a1 (f a2 (f a3 (f a4 id))
\end{lstlisting}

\subsection{Linear Foldl}

Again, the first argument to foldl should be a function that is all about taking a default value, the first element of the list, and then returning a new default for the process to continue with a shortened list.

\begin{lstlisting}[language=Haskell]
-- if the list is empty, the result is the initial value; else
-- we recurse immediately, making the new initial value the result
-- of combining the old initial value with the first element.

foldl' :: (b -> a -> b) -> b -> [a] -> b
foldl' f z []     = z
foldl' f z (x:xs) = foldl' f (f z x) xs

-- foldl' f id [a1, a2, a3, a4]
-- foldl' f (f id a1) [a2, a3, a4]
-- foldl' f (f (f id a1) a2) [a3, a4]
-- foldl' f (f (f (f id a1) a2) a3) [a4]
-- foldl' f (f (f (f (f id a1) a2) a3) a4) []
-- foldl' f (f (f (f (f id a1) a2) a3) a4)
-- f (f (f (f (f id a1) a2) a3) a4)
\end{lstlisting}

\subsection{Difference between reduction and fold}
Fold takes an explicit initial value for the accumulator while reduce uses the first element of the input list as the initial accumulator value.

\subsection{Tree-like folds}
\begin{lstlisting}[language=Haskell]
foldt            :: (a -> a -> a) -> a -> [a] -> a
foldt f z []     = z
foldt f z [x]    = x                             -- aka foldt' of data-ordlist
foldt f z xs     = foldt f z (pairs f xs)

foldi            :: (a -> a -> a) -> a -> [a] -> a
foldi f z []     = z
foldi f z (x:xs) = f x (foldi f z (pairs f xs))  -- aka foldt of data-ordlist

pairs            :: (a -> a -> a) -> [a] -> [a]
pairs f (x:y:t)  = f x y : pairs f t
pairs f t        = t
\end{lstlisting}

\clearpage


\section{Fixed Point [25 pts]}

In this exercise we will use  \href{https://en.wikipedia.org/wiki/Cauchy_sequence}{Cauchy sequences}  to prove that value iteration will converge to a unique fixed point (in this case, a value function $V$) regardless of the starting point. An element $V$ is a fixed point for an operator $B$ (in this case the Bellman operator) if performance of $B$ on $V$ returns $V$, i.e., $BV = V$. Recall that the Bellman backup operator $B$ is defined as (in lecture 2):
\begin{equation}
    V_{k+1} \overset{def}{=} BV_{k} = \max_a[ R(s, a) + \gamma\sum_{s' \in S}p(s'|s,a)V^\pi_{k}(s')].
\end{equation}

Additionally, in lecture 2, we proved that this Bellman backup is a contraction for $\gamma < 1$ on the infinity norm
\begin{equation}
    \|BV' - BV''\|_\infty\leq \gamma \|V' - V''\|_\infty
\end{equation}
for any two value functions $V'$ and $V''$, meaning if we apply it to two different value functions, the distance between value functions (in the $\infty$ norm) shrinks after application of the operator to each element.

\begin{enumerate}[label=(\alph*)]
\item (5pts) Prove by induction that $\|V_{n+1} - V_{n}\|_\infty \leq \gamma^n \|V_1 - V_0\|_\infty$

\textbf{Answer:}
\begin{equation}
\begin{aligned}
& n = 1: \\
& \|V_2 - V_1\|_\infty = \|BV_1 - BV_0\|_\infty\leq \gamma^1
\|V_1 - V_0\|_\infty \\
& n = 2: \\
& \|V_3 - V_2\|_\infty = \|BV_2 - BV_1\|_\infty\leq \gamma^1 \|V_2 - V_1\|_\infty \leq \gamma^1 \|V_1 - V_0\|_\infty \\
& n = k, \text{assume:} \\
& \|V_{k+1} - V_k\|_\infty \leq \gamma^1 \|V_1 - V_0\|_\infty \\
& \|V_{k+2} - V_{k+1}\|_\infty = \|BV_{k+1} - BV_k\|_\infty \\
& \leq \gamma^1\|V_{k+1} - V_k\|_\infty \leq \gamma^1 \gamma^k \|V_1 - V_0\|_\infty = \gamma^{k+1} \|V_1 - V_0\|_\infty \\
& 0 < \gamma < 1, \text{by induction: } \|V_{n+1} - V_{n}\|_\infty \leq \gamma \|V' - V''\|_\infty
\end{aligned}
\end{equation}

\item (10pts) Prove that for any $c>0$, $\|V_{n+c} - V_n\|_\infty \leq \frac{\gamma^n}{1-\gamma}\|V_1 - V_0\|_\infty$
\end{enumerate}

\textbf{Answer:}
\begin{equation}
\begin{aligned}
\text{consider } c > 1: &\\
\|V_{n+c} - V_n\|_\infty & = \|V_{n+c} - V_{n+1} + V_{n+1} - V_n\|_\infty \leq  \|V_{n+c} - V_{n+1}\|_\infty + \|V_{n+1} - V_n\|_\infty \\
\|V_{n+c} - V_n\|_\infty  & \leq \sum_{k=n}^{n+c}  \|V_{k+1} - V_k\|_\infty \\
                          & = \sum_{k=n}^{n+c} \gamma^k \|V_{1} - V_0\|_\infty \\
                          & = \frac{\gamma^n - \gamma^{n+c}}{1-\gamma}\|V_{1} - V_0\|_\infty \\
                          & \leq \frac{\gamma^n}{1-\gamma}\|V_{1} - V_0\|_\infty
\end{aligned}
\end{equation}

A \emph{Cauchy sequence} is a sequence whose elements become arbitrarily close to each other as the sequence progresses. Formally a sequence $\{a_n\}$ in metric space $X$ with distance metric $d$ is a Cauchy sequence if given an $\epsilon > 0$ there exists k such that if m, n > k then $d(a_m, a_n) < \epsilon$. Real Cauchy sequences are convergent.
\begin{enumerate}[label=(\alph*)]
\setcounter{enumi}{2}
\item (2pts) Using this information about Cauchy sequences, argue that the sequence $V_0, V_1, ...$ is a Cauchy sequence and is therefore convergent and must converge to some element $V$ and this V is a fixed point

\textbf{Answer:}
\begin{equation}
\text{let } k < n, m = n + c, \epsilon = \frac{\gamma^k}{1-\gamma}\|V_{1} - V_0\|_\infty:
\end{equation}
\begin{equation}
\begin{aligned}
\|V_{m} - V_n\|_\infty  & \leq \frac{\gamma^n}{1-\gamma}\|V_{1} - V_0\|_\infty \\
                          & < \frac{\gamma^k}{1-\gamma}\|V_{1} - V_0\|_\infty \\
                          & = \epsilon
\end{aligned}
\end{equation}

\item (8pts) Show that this fixed point is unique.

\end{enumerate}

Assume there are two fixed points $V$ and $V'$

At those points, because of convergence: $BV = V$

$\|V - V'\|_\infty = \|BV - BV'\|_\infty \leq \gamma \|V - V'\|_\infty$

Because $ 0 < \gamma < 1$, so $\|V - V'\| = 0$

Contraditon, there is only one fix point.


\documentclass[en,12pt]{elegantpaper}

\begin{document}
    \section*{4}
    \noindent For Gamma distribution,
    \[
        f(\mathbf{x}|\alpha_0, \lambda)=\frac{\prod{x_i}^{\alpha_0-1}}{\Gamma^n(\alpha_0)}\exp\left(-\lambda\sum^n x_i+n\alpha_0\log(\lambda)\right).
    \]
    So, $T(X)=\bar{X}$ is a sufficient and complete statistics for $\lambda$. Next, we need to prove that $X_1/\bar{X}$ is an ancillary statistics.

    Let $Z\sim\Gamma(\alpha_0, 1)$, then
    \[
        X\sim\lambda^{\alpha_0}e^{-\lambda}Z, \quad \bar{X}\sim \lambda^{\alpha_0}e^{-\lambda}\bar{Z}.
    \]
    So,
    \[
        \frac{X_1}{\bar{X}}\sim\frac{\lambda^{\alpha_0}e^{-\lambda}Z_1}{\lambda^{\alpha_0}e^{-\lambda}\bar{Z}}=\frac{Z_1}{\bar{Z}}.
    \]
    This ratio is independent on $\lambda$, i.e., it is an ancillary statistics. So, from \emph{Basu theorem}, $X_1/\bar{X}$ is independent with $\bar{X}$.
\end{document}


%intro

\section{Prerequisits}

Following the standard notation from Bourbaki, we will denote for a given set $X$ the power set $\calp(X) = 2^X = \{A : A \subset X\}$.
\begin{defi}
A universe $\calu$ is a set satisfying the following properties:
\begin{enumerate}
\item $\emptyset \in \calu$,
\item $U \in \calu$ implies $U \subset \calu$,
\item $U \in \calu$ implies $\{U\} \in \calu$,
\item $U \in \calu$ implies $\calp(U) \in \calu$,
\item if $I \in \calu$ and $U_i \in \calu$ for $i \in I$ then
$$\bigcup_{i \in I} U_i \in \calu,$$
\item $\nz \in \calu$.
\end{enumerate}
We call a set $U$ a $\calu$-set if $U \in \calu$. We call a set $U$ $\calu$-small if it is isomorphic to a set in $\calu$.
\end{defi}
Following Grothendieck we shall add to the axiom system of Zermelo-Fraenkel the axiom demanding for any set $X$ there is a universe $\calu$ containing $X$.
\begin{defi}
An order on a set $I$ is a relation $\leq$ which is reflexive, antisymmetric and transitive. We call an order directed or filtrant if $I$ is non empty and if for any pair $i, j \in I$ there is $k \in I$ such that $i \leq k$ and $j \leq k$. We call an order total if for all $i, j \in I$ we have either $i \leq j$ or $j \leq i$ (or both). An ordered set $I$ is inductively ordered if any totally ordered subset $I' \subset I$ has an upper bound.
\end{defi}
We remark that following the axiom of choice any inductively ordered set as an maximal element (Zorn's lemma).

\subsection{Categories and functors}
\begin{defi}
A category $\calc$ consists of the following data:
\begin{enumerate}
\item the class of objects $\objc$,
\item the class of morphisms $\morc$ which consists for all $X, Y \in \mrm{Ob}(\calc)$ of the classes of morphisms $\homc{X,Y}$ and
\item the composition law of morphisms: for any triple $X, Y, Z$ of objects in $\calc$ and morphisms $f \in \homc{X,Y}$ and $g \in \homc{Y,Z}$ we get a morphism $h = g \circ f \in \homc{X,Z}$.
\end{enumerate}
For each object $X \in \objc$ there is a morphism $e \in \homc{X,X}$ such that $f \circ e = f$ and $e \circ g = g$ for all $f \in \homc{X,Y}$ and $g \in \homc{Y,X}$.
\end{defi}
We remark that the composition is associative and the latter morphism is simply the identity map. We call a category $\calc$ $\calu$-category if for all $X, Y \in \objc$ the class $\homc{X,Y}$ is $\calu$-small. A $\calu$-small category is a $\calu$-category $\calc$ such that $\objc$ is $\calu$-small.
\begin{defi}
Given a category $\calc$ we define the opposite category $\calc^{\mrm{op}}$ to be:
$\obj{\calc^{\mrm{op}}} = \objc$ and $\homo{\calc^{\mrm{op}}}{X,Y} = \homc{Y,X}$ for all $X, Y \in \objc$.
The composition is simply inverted: for $f \in \homo{\calc^{\mrm{op}}}{X,Y}$ and $g \in \homo{\calc^{\mrm{op}}}{Y,Z}$ we get:
$$g \circ^{\mrm{op}} f = f \circ g.$$
\end{defi}
A morphism $f : X \longrightarrow Y$ in $\calc$ is called an isomorphism if there exists a morphism $g : Y \longrightarrow X$ in $\calc$ such that
$$ f \circ g = id_Y  \ \mrm{and}\ g \circ f = id_X.$$
An endomorphism is a morphism with target object $Y = X$ - an automorphism is and endomorphism and isomorphism. Two morphism $f, g$ are parallel if they have the same source and targets:
$$f, g : X\  \substack{\longrightarrow\\\longrightarrow}\ Y.$$
A morphism $f : X \longrightarrow Y$ is a monomorphism if for any parallel morphisms $g_1 , g_2 : Z\  \substack{\longrightarrow\\\longrightarrow}\ X$ we have
$$g_1 \circ f = g_2 \circ f\ \Rightarrow\ g_1 = g_2.$$
A morphism $f : X \longrightarrow Y$ is an epimorphism if $f^{\mrm{op}}$ is a monomorphism.
We call a category $\calc'$ a subcategory of $\calc$ if $\obj{\calc'} \subset \objc$ and $\homo{\calc'}{X,Y} \subset \homc{X,Y}$ for all $X, Y \in \obj{\calc'}$. We call a subcategory $\calc'$ of $\calc$ full if $\homo{\calc'}{X,Y} = \homc{X,Y}$. A full subcategory $\calc'$ of $\calc$ is saturated if $X \in \calc$ belongs to $\calc'$ whenever $X$ is isomorphic to an object in $\calc'$. A category is discrete if all morphism are identity morphisms. A category $\calc$ is non empty if $\objc$ is non empty. A category is a groupoid if all morphisms are isomorphisms. A category $\calc$ is finite if $\morc$ is finite as a set. A category is connected if its non empty and for any pair objects $X, Y \in \calc$ there is a finite sequence of objects $X = X_0, \ldots, X_i = Y$ such that at least one of the sets $\homc{X_j,X_{j+1}}$ or $\homc{X_{j+1},X_j}$ is non empty for all $0 \leq j \leq i - 1$.\\
\indent A diagram in a category $\calc$ is a family of symbols representing objects in $\calc$ and arrows betweens these representing morphisms of these objects. The definition of a commutative diagrams follows in an obvious fashion.
\bsp
\begin{enumerate}
\item $\mrm{Set}$ is the category of $\calu$-sets and maps, $\mrm{Set}^f$ the full subcategory of finite $\calu$-sets.
\item The category $\mrm{Rel}$ of binary relations is defined to be:
$\obj{\mrm{Rel}} = \obj{\mrm{Set}}$ and $\homo{\mrm{Rel}}{X,Y} = \power{X \times Y}$, the set of subsets of $X \times Y$. The composition law is defined as follows: if $f : X \longrightarrow Y$ and $g : Y \longrightarrow Z$ then $g \circ f$ is
$$\{(x,z) \in X \times Z : \exists y \in Y,\ (x,y) \in f \wedge (y,z) \in g\}.$$
The identity morphism is the diagonal map $\Delta : X \longrightarrow X \times X$.
\item  Let $R$ be a unital ring (not necessarily commutative, $R \in \calu$). The category of $R$ left modules belonging to $\calu$  is denoted $\mrm{Mod}(R)$. The category of $R$ right modules is simply the $R^{\mrm{op}}$ left modules where $R^{\mrm{op}}$ is the opposite ring (with multiplication flipped). Its class of morphisms is
$$\homo{\mrm{Mod}(R)}{\;\cdot,\;\cdot} = \homo{R}{\;\cdot,\;\cdot}.$$
We denote with $\mrm{End}_R(M)$ the ring of $R$ endomorphisms on $M$ and $\mrm{Aut}_R(M)$ the group of automorphisms on $M$. We denote by $\mrm{Mod}^{\mrm{f}}$ the category of finitely generated modules over $R$ (recall: finitely generated iff there is a surjective $R$ linear map $u : R^\oplus \longrightarrow M$ for some $n \geq 1$). They are also called modules of finite type.\\
\indent We denote by $\mrm{Mod}^{\mrm{fp}}$ the category of finitely presented $R$ modules. Recall a module is finitely presented if it is of finite type and $\ker u$, as defined above, is also of finite type.
\item Let $(I,\leq)$ be an ordered set. We associate to it a category $\call{I}$ as follows:
$$\begin{array}{rcl}
\obj{\call{I}} &=& I\\
&&\\
\homo{\call{I}}{i,j} &=& \begin{cases}
\ast,& i \leq j\\
\emptyset, & \mrm{else}\\
\end{cases}\\
\end{array}$$
where $\ast$ stands for some pointed space. Thus, the set of morphisms is either single-pointed or empty.
\item We call a category of boolean type or a boolean category if there three maps
$$\begin{array}{rrcl}
a : & B \times B & \longrightarrow& B\\
&&&\\
o : & B \times B & \longrightarrow& B\\
&&&\\
n : & B &\longrightarrow& B\\
\end{array}$$
such that the following diagrams commute:
$$\begin{array}{cc}
\xymatrix{
B \ar[rd]_{id_B}\ar[r]^n&B\ar[d]^n\\
&B\\
} &
\xymatrix{
B \ar[d]_n & B \times B \ar[l]_a \ar[r]^o \ar[d]_{n \times n} & B\ar[d]^n\\
B & B \times B \ar[l]^o \ar[r]_a &B\\
}\\
\end{array}$$
Its morphisms are simply the maps preserving the each of the three maps.
\end{enumerate}
\subsubsection{Topological spaces as category}

\subsection{Functors}

\subsection{Yoneda functors and Yoneda lemma}
Given a universe $\calu$ and a $\calu$-category $\calc_\calu$ we define two functors:
$$\begin{array}{rrcl}
\hat{h} : &\calc_\calu &\longrightarrow& \hat{\calc}_\calu\\
&&&\\
&C& \longmapsto& \homo{\calc_{\calu}}{\;\cdot\;,C}\\
&&&\\
\hat{k} :&\calc &\longrightarrow& \check{\calc}_\calu\\
&&&\\
&C&\longmapsto&\homo{\calc_{\calu}}{C,\;\cdot\;}\\
\end{array}$$

\subsection{Group objects}
Given a category $\calc$ with initial object $\ast$ and finite products we call an object $G \in \calc$ a group object (in $\calc$) %there are morphisms $e \in \homo{\calc}{\ast,G}$, $m \in \homo{\calc}{G \times G, G}$ and $S : G \longrightarrow G$ if and only if $\mrm{im} m \supset G$ and the following diagrams commute:
if there is a functor $\overline{G} : \calc^{\mrm{op}} \longrightarrow \mrm{Grp}$ such that $G$ represents the composition functor $\mrm{For} \circ \overline{G}$ given in the following diagram:
$$\xymatrix{ \calc^{\mrm{op}} \ar[r]^{\overline{G}} \ar[rd]_{\mrm{For}\;\circ\;\overline{G}}& \mrm{Grp}\ar[d]^{\mrm{For}}\\
&\mrm{Set}.\\
}$$
Here, $\mrm{For}$ is simply the forgetful functor:
$$\mrm{Grp} \longrightarrow \mrm{Set}.$$
Furthermore, representation means that $\overline{G} \simeq h_{\calc}(G) = \homo{\calc}{\;\cdot\;,G}$. Therefore, we may identify $G$ and $\overline{G}$ ($G$ thought of as a functor). Furthermore, there is a functorial isomorphism:
$$G(X) \times G(X) \simeq (G \times G)(X)$$
making $m : G \times G \longrightarrow G$ a morphism in $\hat{\calc}$.
we get functors% $m : \hat{\calc} \times \hat{\calc} \longrightarrow \hat{\calc}$, $e : \hat{h}
\begin{description}
\item[Unity:]
$$\xymatrix{
\ast \times G \ar[rd]_\simeq \ar[r]^{e \times id_G}& G\times G \ar[d]^m&G\times \ast\ar[l]_{id_G \times e}\ar[ld]^\simeq\\
&G&\\
}$$
\item[Associativity]
$$\xymatrix{
G \times G \times G \ar[rr]^{m \times id_G}\ar[d]_{id_G\times m} && G\times G \ar[d]^m\\
G \times G \ar[rr]_m &&G\\
}$$
\end{description}
\subsubsection{Functors and schemes}
Two prominent examples of functors are the following:
\paragraph{Additive group scheme}

\paragraph{Multiplicative group scheme}
Given a commutative ring $R$ with unit, we define the following functor:
$$\mathbb{G}_m : \mrm{CURng} \longrightarrow \mrm{Grp},\ R \longmapsto \mrm{Gl}_1(R).$$
In this case the base scheme is simply the spectrum of $R$ and its affine scheme is:
$$\mrm{Spec} \left(R\left[x,x^{-1}\right]\right)$$
as follows: the ring of Laurent polynomials $R[x,x^{-1}]$ over $R$ has the comultiplication
$$\Delta : R[x,x^{-1}] \longrightarrow R[x,x^{-1}]^{\otimes 2},\ x \longmapsto x \otimes x,$$
and counit
$$\varepsilon : R[x,x^{-1}] \longrightarrow R,\ x \longmapsto 1.$$
Now, we take the dualisation via the spectrum functor as follows:
\begin{enumerate}
\item we pick a minimial prime ideal $\mathfrak{p}$ over zero in $R$ and form a singleton $(\{\mathfrak{p}\},\mathfrak{p})$ and the map:
$$\mrm{Spec}(\varepsilon) : \{\mathfrak{p}\} \longrightarrow \mrm{Spec} R[x,x^ {-1}],\ \mathfrak{p} \longmapsto \left<x - 1\right>$$
Note that if $R$ is some integral domain this chosen prime ideal is naturally the zero ideal. This map becomes our inclusion $e : \ast \longrightarrow R^\times$ of the trivial subgroup.
\item Next, we note that the tensor product $R[x,x^{-1}] \otimes R[x,x^{-1}]$ is isomorphic to $R[x,x^{-1},y,y^{-1}] = S^{-1}_{x,y} R[x,y]$ and we therefore only need to discuss the prime ideals in the latter ring (actually only those in $R[x,x^{-1}]$ as only those will be in the preimage of $\Delta$):
$$\bao{rrcl}
\mrm{Spec}(\Delta) : &\mrm{Spec} (R[x,x^{-1}] \otimes R[x,x^{-1}]) &\longrightarrow &\mrm{Spec}(R[x,x^ {-1}])\\
& \left<a x - 1\right> \otimes \left<b x - 1\right> &\longmapsto & \left<a b x - 1\right>\\
\ea$$
\end{enumerate}


\chapter{Unit 6}
\section{Integration by Parts}
To integrate using parts, pick a $u$ and a $dv$ from the origianl integral. The
$dv$ ideally should be something that is easy to integrate, while the $u$ should
be easy to differentiate.

Once selected, simply apply the formula to yield your answer. You may have to do
parts more than once, or have the recursive case where the original integral
comes up again, and you have to move it to one side.

\begin{equation}
  \int u dv = uv - \int v du
\end{equation}

\section{Powers of Trigonometric Expressions}
\subsection{$\int \cos^n\theta\sin^m\theta d\theta$}
\begin{description}
  \item[$n$ or $m$ odd] convert all but one of that function to the other using
    a trigonometric identity. The remaining power is the $d\theta$.
  \item[$n$, $m$ both even] use a power reducing formula.
\end{description}

\subsection{$\int \sec^n\theta\tan^m\theta d\theta$}
\begin{description}
  \item[$n$ is even] convert all but one $\sec^2\theta$ to $\tan$s using a
    trigonometric identity. The remaining $\sec^2\theta$ will be your $d\theta$.
  \item[$n$ is odd] convert all but one to $\sec\theta$, saving a
    $\sec\theta\tan\theta$.
  \item[$n=0$, $m$ is anything] convert one $\tan^2\theta$ to $\sec^2\theta-1$
\end{description}

\subsection{Base-case}
If the trigonometric power expression you have is none of the above, you have to
convert everything to $\sin$s and $\cos$s and simplify from there.

\section{Triginometric Substitution}
You can substitute three cases of integrals into a triangle and then simplify
in terms of $\theta$. Integrate with respect to $d\theta$ and then convert your
answer back in terms of $x$, $y$, or $z$ using the original triangle.

\begin{align}
  \sqrt{a^2-u^2} &\to \sin\theta = \frac{u}{a} \\
  \sqrt{a^2+u^2} &\to \tan\theta = \frac{u}{a} \\
  \sqrt{u^2-a^2} &\to \sec\theta = \frac{u}{a}
\end{align}

\section{Partial Fractions}
If you have a integral where the denominator is factorable (or can be split to
become factorable) then you can use partial fractions.

\begin{equation}
  \int \frac{p(x)}{q(x)} dx = \int {\frac{A}{x}+\frac{B}{(x+1)}+\ldots\; } dx
\end{equation}

There are a few rules when dealing with expanding the denominator to use partial
fractions.
\begin{itemize}
  \item If you have a factor in the form $(px^2+q)$, the numerator is in the
    form $(Ax+B)$.
  \item If you have a factor in the form $(px+q)$ then then numerator is in the
    form of $A$.
\end{itemize}

To solve...
\begin{enumerate}
  \item Make the denominator factor-able.
  \item Break apart the partial fraction.
  \item Create the basic equation.
  \item Isolate terms by power.
  \item Solve the system.
  \item Integrate each part.
\end{enumerate}


\subsection{ResNet and pre-act ResNet structure}
Based on the notation for standard DNN structure, we now present
the definition for ResNet type models in DNNs.

\paragraph{ResNet}
The ResNet can be written as
\begin{equation}\label{ori-ResNet-dnn}
\begin{cases}
x^0 &= x, \\
x^{\ell} &= \sigma \left( P^\ell x^{\ell-1} + \mathcal{F}^{\ell} (x^{\ell-1}) \right), \quad \ell = 1:L ,\\
{\text{ResNet}}_{L}(x) &=   W^L x^{L}  + b^L.
\end{cases}
\end{equation}
Here
\begin{equation}\label{eq:F-ResNet}
\mathcal{F}^{\ell}  \in  \dnn(n_\ell, m_\ell),
\end{equation}
or more precisely,
\begin{equation}\label{key}
\mathcal{F}^{\ell}(x) = \xi^{\ell} \circ \sigma \circ \eta^{\ell} (x),
\end{equation}
where
\begin{equation}\label{key}
\eta^\ell: \mathbb{R}^{{\rm dim}_x} \mapsto \mathbb{R}^{n_\ell}, \quad \text{and} \quad \xi^{\ell}:  \mathbb{R}^{n_\ell} \mapsto \mathbb{R}^{m_\ell}.
\end{equation}
This kind of ResNet is consist with original ResNet CNN~\cite{he2016deep}, which is associated with a skip connection distant 2.
Here $P^i$ is use to fit the dimension as
\begin{equation}\label{eq:P^i}
P^\ell: \mathbb{R}^{{\rm dim}_{x^{\ell-1}}} \mapsto \mathbb{R}^{m_\ell}.
\end{equation}
In most cases, if ${\rm dim}_{x^{\ell-1}} = m_\ell$, then take $P^\ell = {\rm id}$.

\paragraph{pre-act ResNet}
The pre-act ResNet~\cite{he2016identity} can be written as:
\begin{equation}\label{ori-iResNet-dnn}
\begin{cases}
x^0 &= x, \\
x^{\ell} &=  P^\ell x^{\ell-1} + \mathcal{F}^{\ell} \left( \sigma (x^{\ell-1}) \right), \quad \ell = 1:L ,\\
{\text{pre-act ResNet}}_{L}(x) &=  W^L x^{L} + b^L.
\end{cases}
\end{equation}
Here $\mathcal{F}^{\ell}$ and $P^\ell$ share the same setup as in ResNet.

\subsection{A more general ResNet DNN}
In some literature, the next ResNet type DNN is more popular.
\begin{equation}\label{general-ResNet-dnn}
\begin{cases}
x^0 &= x, \\
x^{\ell} &=  P^\ell x^{\ell-1} + \mathcal{F}^{\ell} \left( x^{\ell-1} \right), \quad \ell = 1:L ,\\
{\text{gResNet}}_{L}(x) &=  W^L x^{L} + b^L.
\end{cases}
\end{equation}
Here $\mathcal{F}^{\ell}$ may have a more general version compared with classical ResNet.
For example,
\begin{equation}\label{key}
\mathcal{F}^{\ell} = \sigma \circ \theta^\ell, \quad \text{and} \quad P^\ell = {\rm id},
\end{equation}
or
\begin{equation}\label{key}
\mathcal{F}^{\ell} \in \dnn(n^\ell_1, n^\ell_2, \cdots, n^\ell_{L_\ell}).
\end{equation}

\subsection{DNN type DenseNet}
In fact, DenseNet might be simple for definition in DNN case.

Here use $\sigma \circ \theta$ type as example.
\begin{equation}\label{ori-DenseNet-dnn}
\begin{cases}
x^0 &= x, \\
x^{\ell} &=   \sigma \circ \theta^{\ell}([x^{\ell-1}, x^{\ell-2}, \cdots, x^0]) , \quad \ell = 1:L ,\\
{\text{DenseNet}}_{L}(x) &=  W^L x^{L} + b^L.
\end{cases}
\end{equation}

Here $[x^{\ell-1}, x^{\ell-2}, \cdots, x^0]$ means a long vector by collecting all
outputs from $x^0$ to $x^{\ell-1}$.

%, thus to say
%$$
%{\rm dim}([f^{i-1}, f^{i-2}, \cdots, f^0]) = \sum_{i=0}^{i-1} n_i.
%$$

\endinput
%\subsection{DNN type MgNet}
%Similar with ResNet, we can rewrite MgNet.
%
%Here use $\theta \circ \sigma$ type as example.
%\begin{equation}\label{ori-MgNetNet-dnn}
%\begin{cases}
%u^0 &= 0, \quad f^0 = \theta^0(x) \\
%u^{\ell} &=  P^\ell u^{\ell-1} + \mathcal{F}^{i} (f^{i-1} - ) , \quad i = 1:J ,\\
%{\rm MgNet}_{L} &= u^{L}.
%\end{cases}
%\end{equation}
%Here
%\begin{equation}\label{eq:F-MgNet}
%\mathcal{F}^{i} (f^{i-1}) = \xi^{i} \left( f^{i-1} +  \sigma \circ \eta^{i} \circ \sigma(f^{i-1}) \right).
%\end{equation}

%\newpage
%\input{juncai_dnn}


\subsection{Hyperboloids}
\noindent
A hyperboloid looks like a hyperbola that has been rotated and extruded about its center. It is also radially symmetric with circular level curves. Paraboloids have the form
\begin{equation*}
	d = \pm \frac{x^2}{a^2} \pm \frac{y^2}{b^2} \pm \frac{z^2}{c^2},
\end{equation*}
where one sign is different from the others. Depending on the signs and the value of $d$, one can get a hyperboloid of one sheet, two sheets, or a cone.

\input{./differentialMultivariableCalculus/hyperboloidOneSheet}
\input{./differentialMultivariableCalculus/hyperboloidTwoSheet}
\input{./differentialMultivariableCalculus/cone}


\chapter{Unit 2}
\section{Continuity, Intermediate Value Theroem}
\subsection{Continuity}
Let $f(x)$ be a function defined at $x=a$. $f$ is said to be continuous at $x=a$
if:
\begin{equation}
  \lim_{x\to{a^+}} f(x) = \lim_{x\to{a^-}} f(x) = f(a)
\end{equation}

\subsection{Intermediate Value Theorem}
Let $f(x)$ be defined over $x\in[a,b]$. Suppose that $f(a)$ and $f(b)$ both
exist. The intermediate value theorem states that there exists a $c\in[a,b]$
such that $f(a)<f(c)=V<f(b)$.

\section{$\delta$-$\epsilon$ proofs}
Preforming a $\delta$-$\epsilon$ proof means proving the existence of a limit
using the $\delta$-$\epsilon$ definition of a limit. That definition follows
below:

Suppose:
$$\lim_{x\to{c}} f(x) = L$$

It can be said, therefore:
\begin{equation}
  |x-c|<\delta \Rightarrow |f(x)-L|<\epsilon
\end{equation}

To prove the existence of a limit using the $\delta$-$\epsilon$ definition of
the limit, follow the process below:
\begin{enumerate}
  \item Write $|x-c|<\delta$ filling in known values of $c$.
  \item Write $|f(x|-L|<\epsilon$ filling in the values of $f(x)$ in terms of
    $x$ and $L$.
  \item Manipulate one inequality to look like the other, and set them equal.
  \item Determine the relationship between $\delta$ and $\epsilon$.
  \item Reverse to prove.
\end{enumerate}

\section{Indeterminate Forms \& L'Hopital's rule}
\subsection{Indeterminate Forms}
When evaluating a limit, it may become impossible to continue evaluation if the
inner-function of the limit is not defined.

For limits of the form:
$$\lim_{x\to{c}}\frac{p(x)}{q(x)}=L$$

If $\frac{p(x)}{q(x)}$ is one of the following forms: $\frac{0}{0}$ or
$\frac{\pm\infty}{\pm\infty}$, then L'Hopital's rule may be applied.

L'Hopital's rule states the following for limits with indeterminate forms:

\begin{equation}
  \lim_{x\to{c}} \frac{p(x)}{q(x)} = \lim_{x\to{c}} \frac{p'(x)}{q'(x)}
\end{equation}

...and so-on and so-forth until the limit exists (so long as $\frac{p(x)}{q(x)}$
is an indeterminate form).

\section{Limits and Continuity}
\section{Derivatives}
The derivative of a function is another function which is valued as the set of
instantenous rates of changes of its "parent" function. A definition follows:

\subsection{Limit Definition}
\begin{equation}
  f'(x)=\lim_{h\to{0}} \frac{f(x+h)-f(x)}{h}
\end{equation}

\subsection{Alternate Form}
The alternate form of the derivate follows:
\begin{equation}
  f'(c)=\lim_{x\to{c}} \frac{f(x)-f(c)}{x-c}
\end{equation}

\subsection{Implicit Differentation}
Differentiate an equation with respect to one variable, and other differentials
remain. You can move all differentials of a single kind over to one side to
solve. An example follows below:

\begin{align*}
  x^2+y^2 &= \sin(xy)-y^2 \\
  \frac{d}{dx}[x^2+y^2] &= \frac{d}{dt}[\sin(xy)-y^2] \\
  2x+2y\frac{dy}{dt} &= cos(xy)\left(y+x\frac{dy}{dt}\right)-2y\frac{dy}{dt}
\end{align*}

\subsection{Average Rate of Change}
The average rate of change gives the average rate of change of $f(x)$ on the
interval $[a,b]$.
\begin{equation}
  \frac{f(b)-f(a)}{b-a}
\end{equation}

\subsection{Instantenous Rate of Change}
The instantenous rate of change gives the rate of change at a particular point.
\begin{equation}
  f'(c)
\end{equation}

\subsection{Arctrig Proofs}
Suppose $y=\arctan(x)$, therefore $y'=\frac{dx}{1+x^2}$. Prove.

\begin{equation}
\begin{aligned}
  \tan(y) &= x \\
  \sec^2(y)\frac{dy}{dx} &= 1 \\
  \frac{1}{\sec^2(y)} &= \frac{dy}{dx} \\
  \frac{1}{1+\tan^2(y)} &= \\
  \frac{1}{1+x^2} &=
\end{aligned}
\end{equation}

\subsection{Differentiability}
Suppose $f(x)$ is a function such that $\{c\} \in D_{f(x)}$. While
differentiability at $x=c$ is an implication of continuity at $x=c$, the
converse is not necessairly true. Continuity implies differentiability in the
following scenarios:
\begin{itemize}
  \item $f(x)$ has normal behavior around $x=c$.
\end{itemize}

...but does not imply differentiability in the following scenarios:
\begin{itemize}
  \item $f(x)$ has a cusp at $x=c$.
  \item $f(x)$ has a vertical "jump" at $x=c$.
\end{itemize}

To prove differentiability of $f$ at a point $x=c$, the following steps must be
taken:
\begin{enumerate}
  \item $\lim_{x\to{c}} \frac{f(x)-f(c)}{x-c}$ must exist and "agree" from both
    sides.
\end{enumerate}

\subsection{Inverse Functions}
The derivative of an inverse function is defined:
\begin{equation}
  \frac{d}{dx}\left[f^{-1}(x)\right]=\frac{1}{f'(f^{-1}(x))}
\end{equation}

\section{Logarithmic and Exponential Functions}
If it is impossible to differentiate or take the limit of a function w.r.t. $x$,
it is sometimes useful to take the natural log of both sides such that
L'Hopital's rule may be applied. An example is shown below:
\begin{equation}
  \begin{aligned}
    L &= \lim_{x\to\infty} \left(1+\frac{1}{x}\right)^x \\
    \ln{L} &= \ln{\lim_{x\to\infty} \left(1+\frac{1}{x}\right)^x} \\
           &= \lim_{x\to\infty} \ln\left(\left(1+\frac{1}{x}\right)^x\right) \\
           &= \lim_{x\to\infty} x\ln\left(1+\frac{1}{x}\right) \\
           &= \lim_{x\to\infty} \frac{\ln\left(1+\frac{1}{x}\right)}{x^{-1}} \\
           &= \lim_{x\to\infty} \frac{0}{0} \\
           &= \lim_{x\to\infty} \frac{\frac{1}{1+\frac{1}{x}}x^{-2}}{-x^{-2}} \\
           &= \lim_{x\to\infty} \frac{1}{1+\frac{1}{x}} \\
           &= 1 \\
    L &= e.
  \end{aligned}
\end{equation}

\section{Derivates w.r.t Other Functions}
As we have seen so far, functions can be differentiated with respect to a
variable of that function (ex.: $f(x)$ and $\frac{d}{dx}(f(x))$).

However, it is possible to differentiate a function with respect to another
function. To differentiate a function $f(x)$ with respect to another function
$g(x)$, the following equation is used:
\begin{equation}
  \frac{d[f(x)]}{d[g(x)]}=\frac{d[f(x)]/dt}{d[g(x)]/dt}
\end{equation}


\documentclass{article}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}
    \usepackage{mathrsfs}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{tikz}
    \usepackage{fancyhdr}
    \usepackage{multicol}

    \usetikzlibrary{positioning}

\pagestyle{fancy}
\fancyhf{}
\rhead{Edgar Jacob Rivera Rios - A01184125}

\renewcommand{\labelenumi}{\alph{enumi})}

\begin{document}
\section*{5.1.1}
The Employment and Training Administration reported that the U.S. mean unemployment insurance benefit was \$238 per week (The World Almanac, 2003). A researcher in the state of Virginia anticipated that sample data would show evidence that the mean weekly unemployment insurance benefit in Virginia was below the national average.

\begin{enumerate}
  \item Develop appropriate hypotheses such that rejection of H0 will support the researcher’s contention.
  \begin{align*}
    H_{0} &= \mu \geq \mu_{l}\\
    H_{\alpha} &= \mu < \mu_{l}\\
  \end{align*}

  \item For a sample of 100 individuals, the sample mean weekly unemployment insurance benefit was \$231 with a sample standard deviation of \$80. What is the $p$-value?
  \begin{align*}
    s &= 80\\
    \mu &= 238\\
    \bar{x} &= 231\\
    n &= 100\\
    s_{\bar{x}} &= \frac{s}{\sqrt{n}}\\
    s_{\bar{x}} &= \frac{80}{\sqrt{100}}\\
    s_{\bar{x}} &= 8\\
    t &= \frac{\bar{x} - \mu}{s_{\bar{x}}}\\
    t &= \frac{231 - 238}{8}\\
    t &= -0.875\\
    p-value &= 0.19184589
  \end{align*}

  \item At $\alpha = .05$, what is your conclusion?

  As the $p-value$ is greater than 0.05, we cannot reject $H_{0}$

  \item Repeat the preceding hypothesis test using the critical value approach.

  \begin{align*}
    t &= -0.875\\
    \text{critical value} (t_{0}) &= -1.66039115
  \end{align*}
  As $t < t_{0}$ does not hold, we cannot reject $h_{0}$

\end{enumerate}
\pagebreak

\section*{5.1.2}
A shareholders’ group, in lodging a protest, claimed that the mean tenure for a chief executive office (CEO) was at least nine years. A survey of companies reported in The Wall Street Journal found a sample mean tenure of$\bar{X} = 7.27$ years for CEOs with a standard deviation of $s = 6.38$ years (The Wall Street Journal, January 2, 2007).
\begin{enumerate}
  \item Formulate hypotheses that can be used to challenge the validity of the claim made by the shareholders’ group.
  \begin{align*}
    H_{0} &= \mu \geq 9\\
    H_{\alpha} &= \mu < 9 \\
  \end{align*}

  \item Assume 85 companies were included in the sample. What is the $p$-value for your hypothesis test?
  \begin{align*}
    s &= 6.38\\
    \mu_{0} &= 9\\
    \bar{x} &= 7.27\\
    n &= 85\\
    s_{\bar{x}} &= \frac{s}{\sqrt{n}}\\
    s_{\bar{x}} &= \frac{6.38}{\sqrt{85}}\\
    s_{\bar{x}} &= 0.692008160441513\\
    t &= \frac{\bar{x} - \mu_{0}}{s_{\bar{x}}}\\
    t &= \frac{7.27 - 9}{0.692008160441513}\\
    t &= -2.49997051898381\\
    p-value &= 0.007182545042323
  \end{align*}

  \item At $\alpha = .01$, what is your conclusion?

  As the $p-value$ is less than 0.01, we can reject $H_{0}$, which means that the CEO's mean tenure is less than 9 years

\end{enumerate}
\pagebreak

\section*{5.1.3}
The Coca-Cola Company reported that the mean per capita annual sales of its beverages in the United States was 423 eight-ounce servings (Coca-Cola Company website, February 3, 2009). Suppose you are curious whether the consumption of Coca-Cola beverages is higher in Atlanta, Georgia, the location of Coca-Cola’s corporate headquarters.

A sample of 36 individuals from the Atlanta area showed a sample mean annual consumption of 460.4 eight-ounce servings with a standard deviation of $s = 101.9$ ounces.

Using $\alpha = .05$, do the sample results support the conclusion that mean annual consumption of Coca-Cola beverage products is higher in Atlanta?

\begin{align*}
  H_{0} &= \mu \leq 423\\
  H_{\alpha} &= \mu > 423 \\
  s &= 101.9\\
  \mu_{0} &= 423\\
  \bar{x} &= 460.4\\
  n &= 36\\
  s_{\bar{x}} &= \frac{s}{\sqrt{n}}\\
  s_{\bar{x}} &= \frac{101.9}{\sqrt{36}}\\
  s_{\bar{x}} &= 16.9833333333333\\
  t &= \frac{\bar{x} - \mu_{0}}{s_{\bar{x}}}\\
  t &= \frac{460.4 - 423}{16.9833333333333}\\
  t &= 2.20215897939156\\
  p-value &= 0.01716737427853
\end{align*}

As the $p-value$ is less than 0.05, we can reject $H_{0}$, which means that the Coke consumption it's higher in Atlanta than in the rest of the USA

\end{document}


\section*{Exercises}

\begin{ex} Give the complete solution to $x^{4}+16=0$.
\begin{sol}
 Solution is:
\[
(1-i) \sqrt{2},-(1+i) \sqrt{2},-(1-i)
\sqrt{2},(1+i) \sqrt{2}
\]
\end{sol}
\end{ex}

\begin{ex} \label{cube-roots} Find the complex cube roots of $8$.
\begin{sol}
The cube roots are the solutions to $%
z^{3}+8=0$, Solution is: $i\sqrt{3} +1,1-i\sqrt{3},-2$
\end{sol}
\end{ex}

\begin{ex} \label{cube-roots2} Find the four fourth roots of $16$.
\begin{sol}
The fourth roots are
the solutions to $z^{4}+16=0$, Solution is:
\[
(1-i) \sqrt{2},-(1+i) \sqrt{2},-(1-i)
\sqrt{2},(1+i)\sqrt{2}
\]
\end{sol}
\end{ex}

\begin{ex} \label{exer-complex1}De Moivre's theorem says $\mat{r(\cos
t+i\sin t)} ^{n}=r^{n}(\cos nt+i\sin nt) $ for $n$
a positive integer. Does this formula continue to hold for all integers $n$,
even negative integers? Explain.
\begin{sol}
Yes, it holds for all integers. First of
all, it clearly holds if $n=0$. Suppose now that $n$ is a negative integer.
Then $-n>0$ and so
\[
\mat{r(\cos t+i\sin t)} ^{n}=\frac{1}{\mat{r(
\cos t+i\sin t)} ^{-n}}=\frac{1}{r^{-n}(\cos (
-nt) +i\sin (-nt)) }
\]
\begin{eqnarray*}
&=&\frac{r^{n}}{(\cos (nt) -i\sin (nt))
}=\frac{r^{n}(\cos (nt) +i\sin (nt)) }{
(\cos (nt) -i\sin (nt)) (\cos
(nt) +i\sin (nt)) } \\
&=&r^{n}(\cos (nt) +i\sin (nt))
\end{eqnarray*}
because $(\cos (nt) -i\sin (nt)) (
\cos (nt) +i\sin (nt)) =1$.
\end{sol}
\end{ex}

\begin{ex} Factor $x^{3}+8$ as a product of linear factors. \textbf{Hint:} Use the result of {\eqref{cube-roots}}.
\begin{sol}
Solution
is: $i\sqrt{3}+1,1-i\sqrt{3},-2$ and so this polynomial equals
\[
(x+2) (x-(i\sqrt{3}+1)) (x-(
1-i\sqrt{3}))
\]
\end{sol}
\end{ex}

\begin{ex} Write $x^{3}+27$ in the form $(x+3) (
x^{2}+ax+b) $ where $x^{2}+ax+b$ cannot be factored any more using
only real numbers.
\begin{sol}
$x^{3}+27= (x+3) (
x^{2}-3x+9) $
\end{sol}
\end{ex}

\begin{ex} Completely factor $x^{4}+16$ as a product of linear factors. \textbf{Hint:} Use the result of {\eqref{cube-roots2}}.
\begin{sol}
Solution is:
\[
(1-i) \sqrt{2},-(1+i) \sqrt{2},-(1-i)
\sqrt{2},(1+i) \sqrt{2}.
\]
These are just the fourth roots of $-16$. Then to factor, you get
\begin{eqnarray*}
&&(x-((1-i) \sqrt{2})) (x-(
-(1+i) \sqrt{2})) \cdot \\
&&(x-(-(1-i) \sqrt{2})) (x-(
(1+i) \allowbreak \sqrt{2}))
\end{eqnarray*}
\end{sol}
\end{ex}

\begin{ex} Factor $x^{4}+16$ as the product of two quadratic polynomials each of
which cannot be factored further without using complex numbers.
\begin{sol}
$x^{4}+16=(x^{2}-2\sqrt{2}x+4) (x^{2}+2\sqrt{2}x+4) .
$ You can use the information in the preceding problem. Note that $(
x-z) (x-\conjugate{z}) $ has real coefficients.
\end{sol}
\end{ex}

\begin{ex} If $n$ is an integer, is it always true that $(\cos \theta
-i\sin \theta) ^{n}=\cos (n\theta) -i\sin (n\theta
)$? Explain.
\begin{sol}
Yes, this is true.
\begin{eqnarray*}
(\cos \theta -i\sin \theta) ^{n} &=&(\cos (-\theta
) +i\sin (-\theta)) ^{n} \\
&=&\cos (-n\theta) +i\sin (-n\theta) \\
&=&\cos (n\theta) -i\sin (n\theta)
\end{eqnarray*}
\end{sol}
\end{ex}

\begin{ex} Suppose $p(x) =a_{n}x^{n}+a_{n-1}x^{n-1}+\ldots+a_{1}x+a_{0}$ \ is a polynomial and it has $n$ zeros,
\begin{equation*}
z_{1},z_{2},\ldots,z_{n}
\end{equation*}
listed according to multiplicity. ($z$ is a root of multiplicity $m$ if the
polynomial $f(x) =(x-z) ^{m}$ divides $p(
x) $ but $(x-z) f(x) $ does not.) Show that
\begin{equation*}
p(x) =a_{n}(x-z_{1}) (x-z_{2}) \cdots
(x-z_{n})
\end{equation*}
\begin{sol}
$p(x) =(x-z_{1}) q(x) +r(x) $
where $r(x) $ is a non-zero constant or equal to $0$. However, $r(z_{1}) =0$ and so $r(x) =0$. Now do to $q(
x) $ what was done to $p(x) $ and continue until the
degree of the resulting $q(x) $ equals $0$. Then you have the
above factorization.
\end{sol}
\end{ex}


\subsection{Cell complexes}
\begin{defn}
A CW complex is a Hausdorff space $X$ constructed as follows:\end{defn}
\begin{enumerate}
\item Start with $X^{0}=\{\text{discrete set of points}\}$.
\item Form a graph $X^{n}$ from $X^{n-1}$ by attacheding n-cells via maps
$\phi_{\alpha}:S^{n-1}\to X^{n-1}$. So $X^{n}=(X^{n-1}\sqcup B_{\alpha}^{n})/(x\sim\phi_{\alpha}(x)\forall x\in\partial B_{\alpha}^{n})$\\
\\
$X^{n}$ has the quotient topology defined by $q:X^{n-1}\sqcup B_{\alpha}^{n}\to X^{n}$,
i.e. $U\subset X^{n}$ open iff $q^{-1}(U)$ open in $X^{n-1}\sqcup B_{\alpha}^{n}$.\\
\\
$X^{n}=\text{"n-skeleton"}=X^{n-1}\sqcup e_{\alpha}^{n}$ where $e_{\alpha}^{n}$
is an open $n$-ball, i.e. the image of $\text{int}B_{\alpha}^{n}$
under $q$.
\item $X=X^{n}$ for finite integer $n$ or $X=\bigcup_{n=0}^{\infty}X^{n}$
and $X$ has the ``weak topology''.
\end{enumerate}
Note: If $X$ is finite, we can define the Euler characteristic $\chi(X)=\sum_{i}(-1)^{i}(\text{\# of \ensuremath{i}-cells in \ensuremath{X})}$.
The dimension of $X$ is the maximum dimension of the cells.

Notes about CW complexs:
\begin{itemize}
\item $\chi(S^{2})=2$, $\chi(M_{g})=2-2g$, $\chi(N_{h})=2-h$.
\item $S^{n}=e^{-0}\cup e^{n}$
\item $\mathbb{R}P^{n}=\mathbb{R}P^{n-1}\cup(\text{open n-cell})=e^{0}\cup e^{1}\cup\cdots\cup e^{n}$.
\item $\chi(\mathbb{R}P^{n})=1-1+1\dots=\begin{cases}
1 & n\,\text{even}\\
0 & n\,\text{odd}
\end{cases}$
\item $\mathbb{C}P^{n}=\mathbb{C}P^{n-1}\cup(\text{copy of \ensuremath{\mathbb{C}^{n})=}}e^{0}\cup e^{2}\cup\cdots\cup e^{2n}.$
\item Products of finite CW complexes are CW complexes.\\
\\
Suppose $X=\bigcup_{\alpha}e_{\alpha},$ $Y=\bigcup_{\beta}e_{\beta}$,
then $X\times Y=\bigcup_{\alpha,\beta}(e_{\alpha}\times e_{\beta})$
has dimension $\dim(e_{\alpha})+\dim(e_{\beta})$. It's an exercise
to show $\chi(X\times Y)=\chi(X)\cdot\chi(Y)$.\\
\\
Example: $S^{2}\times S^{2}=(e^{0}\cup e^{2})\times(e^{0}\cup e^{2})=e^{0}\times e^{0}\cup e^{0}\times e^{2}\cup e^{2}\times e^{0}\cup e^{2}\times e^{2}$.
\end{itemize}


\subsection{Surface Integrals}
\begin{definition}
    Consider a map $f:\mathbb R^3\to\mathbb R$, then we can define a surface by $\{\underline{x}:f(\underline{x})=0\}$.
\end{definition}
In this case, the normal to the surface at $\underline{x}$ is $\nabla f(\underline{x})$.
\begin{definition}
    A surface thus defined is called regular if $\nabla f\neq \underline{0}$ everywhere on the surface.
\end{definition}
\begin{example}
    Consider $f(x,y,z)=x^2+y^2+z^2-1$, then it defines the unit sphere $S^2$.
    Note that $\nabla f=(2x,2y,2z)^\top$ which is certainly normal to $S^2$.
    It is also regular.\\
    In spherical polars, it is in the form $f(r,\theta,\phi)=r^2-1$, so $\nabla f=2r\underline{e_r}=2\underline{x}$.
\end{example}
Some surfaces have a boundary, for example a hemisphere.
In this case we write $\partial S$ to be the boundary of $S$.
In particular the boundary of a hemisphere defined by $x^2+y^2+z^2-1=0$ and $z=0$ is the unit circle in the $x-y$ plane.
If a surface does not have a boundary, we say the boundary is empty.
In this case, we call such a surface closed.\\
Often easiest is to give a local coordinate $u,v$, so $S=\{\underline{x}=\underline{x}(u,v)\}$.
In this case, we define a normal as
$$\frac{\partial\underline{x}}{\partial u}\times\frac{\partial\underline{x}}{\partial v}\left/\middle\|\frac{\partial\underline{x}}{\partial u}\times\frac{\partial\underline{x}}{\partial v}\right\|$$
assuming it is well-defined.
For regular surfaces, this is always well-defined.
It can define the normal consistenly (in terms of sign) or smoothly if the surface is orientable (which we may not define rigourously, sadly).
If the surface is indeed orientable, we use the convention for the orientation of the boundary curve that when we moving along the boundaries, normal vectors are on our left.
\begin{example}
    Consider the hemisphere again using spherical polars $S=\{(\cos\phi\sin\theta,\sin\phi\sin\theta,\cos\theta):\theta\in[0,\pi/2],\phi\in[0,2\pi]\}$
    By calculation we get exactly the vector $\underline{e_r}$ as the normal.
\end{example}
To calculate the area of a surface, we want to partition the surface by a rectangularization of the $u-v$ plane.
So the area of the piece that might look like a parallelogram when we zoom in have an approximated area of $\delta u\delta v\|(\partial \underline{x}/\partial u)\times(\partial \underline{x}/\partial v)\|$, so the area is (from intuition):
\begin{definition}
    The area of the surface $S$ is
    $$\int_S\mathrm dS=\int_S|\mathrm d\underline{S}|=\int_S\left\|\frac{\partial\underline{x}}{\partial u}\times\frac{\partial\underline{x}}{\partial v}\right\|\,\mathrm du\,\mathrm dv$$
\end{definition}
Where we write $\mathrm d\underline{S}=\underline{n}\,\mathrm dS$.
\begin{example}
    We (yet again) look at the hemisphere paramterized by $S=\{(R\cos\phi\sin\theta,R\sin\phi\sin\theta,R\cos\theta):\theta\in[0,\pi/2],\phi\in[0,2\pi]\}$, so
    $$\int_S\mathrm dS=\int_S|\mathrm d\underline{S}|=\int_SR^2\sin\theta\,\mathrm d\theta\,\mathrm d\phi=2\pi R^2$$
\end{example}
We want to use similar method to define the flux integral, which is like the amount of fluid passing though the surface $S$ in unit time.
\begin{definition}
    We define the integral of $f:\mathbb R^3\to\mathbb R$ to be
    $$\int_Sf\,\mathrm dS=\iint_Sf(\underline{x}(u,v))\left\|\frac{\partial\underline{x}}{\partial u}\times\frac{\partial\underline{x}}{\partial v}\right\|\,\mathrm du\,\mathrm dv$$
\end{definition}
Suppose $S:\underline{x}=\underline{x}(u,v),S':\underline{\tilde{x}}=\underline{\tilde{x}}(\tilde{u},\tilde{v})$ are two different parameterizations of the same surface $S$, then we have $\underline{x}(u,v)=\underline{\tilde{x}}(\tilde{u}(u,v),\tilde{v}(u,v))$, where we assume that $\tilde{u},\tilde{u}$ are smooth bijections with smooth inverse.
So we have, by calculus,
$$\frac{\partial\underline{x}}{\partial u}\times\frac{\partial\underline{x}}{\partial v}=\frac{\partial\underline{\tilde{x}}}{\partial\tilde{u}}\times\frac{\partial\underline{\tilde{x}}}{\partial\tilde{v}}\frac{\partial (\tilde{u},\tilde{v})}{\partial (u,v)}$$
So
\begin{align*}
    \int_S f\,\mathrm dS&=\iint_Sf(\underline{x}(u,v))\left\|\frac{\partial\underline{x}}{\partial u}\times\frac{\partial\underline{x}}{\partial v}\right\|\,\mathrm du\,\mathrm dv\\
    &=\iint_{S}f(\underline{\tilde{x}}(\tilde{u}(u,v),\tilde{v}(u,v)))\left\|\frac{\partial\underline{\tilde{x}}}{\partial\tilde{u}}\times\frac{\partial\underline{\tilde{x}}}{\partial\tilde{v}}\frac{\partial (\tilde{u},\tilde{v})}{\partial (u,v)}\right\|\,\mathrm du\,\mathrm dv\\
    &=\iint_{S}f(\underline{\tilde{x}}(\tilde{u}(u,v),\tilde{v}(u,v)))\left\|\frac{\partial\underline{\tilde{x}}}{\partial\tilde{u}}\times\frac{\partial\underline{\tilde{x}}}{\partial\tilde{v}}\right\|\left|\frac{\partial (\tilde{u},\tilde{v})}{\partial (u,v)}\right|\,\mathrm du\,\mathrm dv\\
    &=\iint_{S'}f(\underline{\tilde{x}}(\tilde{u},\tilde{v}))\left\|\frac{\partial\underline{\tilde{x}}}{\partial \tilde{u}}\times\frac{\partial\underline{\tilde{x}}}{\partial\tilde{v}}\right\|\,\mathrm d\tilde{u}\,\mathrm d\tilde{v}\\
    &=\int_{S'} f\,\mathrm dS'
\end{align*}
Just as the Fundamental Theorem of Calculus told us that the integration over a derivative depends only on its endpoints, the integral over a surface of some sort of derivative will only depend on the boundary of the surface.
\footnote{In fact, this is true in manifolds of even higher dimensions, which is known as Stokes' Theorem.}
Then, for a vector field $\underline{F}$, we define the flux integral of it over the surface $S$ by
$$\int_S\underline{F}\cdot\mathrm d\underline{S}=\int_S\underline{F}\cdot\underline{n}\,\mathrm dS$$