\section{The Winding Number}
We want to generalize Cauchy's Theorem to non-simply-connected domains and generalize Cauchy Integral Formula to general shapes.
\begin{definition}
    Let $\gamma:[a,b]\to\mathbb C$ be a closed curve and fix $w\in\mathbb C\setminus\operatorname{Im}\gamma$.
    For each $t$, we can write $\gamma(t)=w+r(t)e^{i\theta(t)}$.
    Where $r(t)=|\gamma(t)-w|$.
    If $\gamma$ is piecewise $C^1$, so is $r$.
    If we can find a continuous $\theta(t)$ such that the equation holds for every $t$, then we define the winding number (or index) if $\gamma$ about $w$ by
    $$I(\gamma;w)=\frac{\theta(b)-\theta(a)}{2\pi}$$
\end{definition}
Note that the winding number must be an integer.
It is easy to show that it is well-defined (i.e. independent of the choice of continuous $\theta$), and also hopefully we can show that it always exists.
\begin{lemma}
    If $\gamma$ is a piecewise $C^1$ curve and $w$ not in the image of $\gamma$, then there exists continuous piecewise $C^1$ real function $\theta$ such that
    $$\gamma(t)=w+r(t)e^{i\theta(t)}$$
    where $r(t)=|\gamma(t)-w|$.
\end{lemma}
If $\gamma$ is $C^1$ and the lemma holds, then $\gamma^\prime(t)=r^\prime(t)e^{i\theta(t)}+ir(t)\theta^\prime(r)e^{i\theta(t)}$, which rearranges to give
$$\theta^\prime(t)=\operatorname{Im}\left( \frac{\gamma^\prime(t)}{\gamma(t)-w} \right)\implies\theta(t)=\theta(a)+\operatorname{Im}\left( \int_a^t \frac{\gamma^\prime(x)}{\gamma(x)-w}\,\mathrm dx\right)$$
\begin{proof}
    Let $h(t)=\int_a^t\frac{\gamma^\prime(s)}{\gamma(s)-w}\,\mathrm ds$ where the singularities are skipped when integrate.
    So $h$ is continuous and differentiable wherever $\gamma$ is, where we will have $h^\prime(t)=\gamma^\prime(t)/(\gamma(t)-w)$.
    Except the singularities, we have
    $$\frac{\mathrm d}{\mathrm dt}\left( (\gamma(t)-w)e^{-h(t)} \right)=\gamma^\prime(t)e^{-h(t)}-(\gamma(t)-w)e^{-h(t)}h^\prime(t)=0$$
    So $(\gamma(t)-w)e^{-h(t)}$ is piecewise constant, but it is also continuous, so it is constant.
    $h(a)=0$, so if we let $\alpha$ be the argument of $\gamma(a)-w$, then
    \begin{align*}
        \gamma(t)-w&=(\gamma(a)-w)e^{h(t)}\\
        &=|\gamma(a)-w|e^{i\alpha}e^{\operatorname{Re}h(t)}e^{i\operatorname{Im}h(t)}\\
        &=|\gamma(a)-w|e^{\operatorname{Re}h(t)}e^{i(\alpha+\operatorname{Im}h(t))}
    \end{align*}
    Taking $\theta(t)=\alpha+\operatorname{Im}h(t)$ finishes the proof.
\end{proof}
\begin{corollary}
    If additionally $\gamma(t)$ is a closed curve, then
    $$I(\gamma;w)=\frac{1}{2\pi i}\int_\gamma\frac{\mathrm dz}{z-w}$$
\end{corollary}
\begin{proof}
    Follows directly from the above choice of $\theta$.
\end{proof}
\begin{remark}
    Actually the lemma holds for continuous $\gamma$ (where we want $r,\theta$ to be continuous).
    The proof is exercise.
\end{remark}
\begin{proposition}
    If $\gamma:[a,b]\to D_R(\alpha)$ be a piecewise $C^1$ closed curve and $w\notin D_R(\alpha)$, then $I(\gamma,w)=0$.
\end{proposition}
\begin{proof}
    Write out definition and use Cauchy.
\end{proof}
\begin{proposition}
    The function $w\mapsto I(\gamma,w)$ is locally constant.
\end{proposition}
\begin{proof}
    Immediate from continuity.
\end{proof}
\begin{definition}
    Let $U\subset\mathbb C$ is open.\\
    1. A closed curve $\gamma$ in $U$ is homologous to $0$ if $I(\gamma,w)$ is $0$ for all $w\notin U$.\\
    2. $U$ is simply connected if every closed curve in $U$ is homologous to $0$.
\end{definition}
So a disk is simply connected but an annulus is not.
\begin{theorem}[Cauchy Integral Formula]\label{general_cif}
    Let $U$ be open and let $\gamma:[a,b]\to U$ be closed and homologous to $0$, then for any holomorphic $f:U\to\mathbb C$ we have
    $$\frac{1}{2\pi i}\oint_\gamma\frac{f(z)}{z-w}\,\mathrm dz=I(\gamma,w)f(w)$$
    for any $w\in U$ that is not in the image of $\gamma$.
\end{theorem}
Note that we can assume $U$ is a bounded domain.
In particular, by integrating $F(z)=(z-w)f(z)$, we know from the theorem that
$$\int_\gamma f(z)\,\mathrm dz=0$$
\begin{corollary}
    If $U$ is simply connected, then for any closed curve $\gamma$ in $U$ and any holomorphic function $f$ on $U$, we have
    $$\int_\gamma f(z)\,\mathrm dz=0$$
\end{corollary}
\begin{proof}
    Immediate from the preceding theorem.
\end{proof}
\begin{remark}
    By the theorem, fix $\gamma$, if
    $$\oint_\gamma f(z)\,\mathrm dz=0$$
    holds for functions in the form $f(z)=1/(z-w)$, then it holds for every holomorphic $f$.
\end{remark}
\begin{proposition}
    Suppose $U\subset\mathbb C$ is open and $\phi:U\times [a,b]\to\mathbb C$ is continuous and each $z\mapsto \phi(z,s)$ is holomorphic, then
    $$\int_a^b\phi(z,s)\,\mathrm ds$$
    is holomorphic.
\end{proposition}
\begin{proof}
    Morera's and Fubini's on closed intervals.
\end{proof}
\begin{proof}[Proof of Theorem \ref{general_cif}]
    First define a function $g$ on $U\times U\to\mathbb C$ by
    $$g(z,w)=\begin{cases}
        (f(z)-f(w))/(z-w)\text{, if $z\neq w$}\\
        f^\prime(z)\text{, if $z=w$}
    \end{cases}$$
    which is continuous since $f$ is holomorphic.
    Also define
    $$h(w)=\oint_\gamma g(z,w)\,\mathrm dz,w\in U;h_1(w)=\oint_\gamma\frac{f(z)}{z-w}\,\mathrm dz,w\in\mathbb C\setminus\gamma([a,b])=U_1$$
    Also, $h=h_1$ on $U\cap U_1$ and $U\cup U_1=\mathbb C$ since $\gamma$ is homologous to zero, so we can define
    $$\phi(w)=\begin{cases}
        h(w)\text{, if $w\in U$}\\
        h_1(w)\text{, if $w\in U_1$}
    \end{cases}$$
    Then $\phi$ is entire by the preceding proposition, and since the behaviour of $\phi$ as $|w|\to\infty$ shall be that of $h_1$, and as $I(\gamma;w)=0$ for large enough $|w|$, we have
    $$|\phi(w)|\le\frac{\operatorname{length}(\gamma)\sup_\gamma|f|}{|w|-R}\to 0$$
    as $|w|\to\infty$.
    By Theorem \ref{holo_bdd_const}, $\phi$ is constantly zero, in particular, $h$ is constantly zero on $U$.
    This implies the result.
\end{proof}
The clever part of the proof that we used a global theorem (i.e. Liouville) to show a local result, which is pretty cool.
\begin{definition}
    Let $\gamma_0,\gamma_1:[a,b]\to\mathbb C$ be two closed piecewise $C^1$ curves.
    We say $\gamma_0$ is homotopic to $\gamma_1$ if there is a function $H:[a,b]\times [0,1]\to\mathbb C$ with $H(0,t)=\gamma_0(t),H(1,t)=\gamma_1(t)$, and for each $s\in [0,1]$, $H(t,s)$ is a closed piecewise $C^1$ curve, that is $\forall s\in[0,1],H(s,a)=H(s,b)$.
\end{definition}
\begin{theorem}\label{homotopy}
    For curves $\gamma_0,\gamma_1:[0,1]\to U$ be homotopic and $w\neq U$, we have $I(\gamma_0,w)=I(\gamma_1,w)$.
\end{theorem}
\begin{lemma}
    If $\gamma_0,\gamma_1:[0,1]\to\mathbb C$ are piecewise $C^1$, $w\in\mathbb C$ and
    $$\forall t\in [0,1],|\gamma_0(t)-\gamma_1(t)|<|w-\gamma_1(t)|$$
    then $I(\gamma_0;w)=I(\gamma_1;w)$.
\end{lemma}
\begin{proof}
    Exercise.
\end{proof}
We will show the $C^1$ case, but the general case is also true by similar idea.
\begin{proof}[Proof of Theorem \ref{homotopy}]
    Consider the continuous deformation $H:[0,1]\times [0,1]\to\mathbb C$.
    Now $[0,1]^2$ is compact, so $H$ has compact (hence closed) image and is uniformly continuous.
    So for $w\notin U$, so there is $\epsilon>0$, for any $(s,t)\in [0,1]^2$, $|H(s,t)-w|>2\epsilon$.
    Also there is $n\in\mathbb N$ such that
    $$|s-s'|+|t-t'|\le\frac{1}{n}\implies |H(s,t)-H(s',t')|<\epsilon$$
    Denote $H(s,t)$ by $\gamma_s(t)$, this would mean that for $k=1,2,\ldots,n$, we have $|\gamma_{(k-1)/n}(t)-\gamma_{k/n}(t)|<\epsilon$, but we also have $|w-\gamma_{k/n}(t)|>2\epsilon$, so
    $$|\gamma_{(k-1)/n}(t)-\gamma_{k/n}(t)|<\epsilon<2\epsilon<|w-\gamma_{k/n}(t)|$$
    So by the preceding lemma $I(\gamma_0;w)=I(\gamma_{1/n};w)=\cdots=I(\gamma_1;w)$.
\end{proof}
So a star-shaped domain is simply connected.
Given $\gamma:[0,1]\to U$ and let $p$ be the centre of $U$, then we can shrink $\gamma$ to $p$ in the obvious way.
\begin{remark}
    If a curve is null-homotopic, then by the theorem it is homologous to $0$, but the converse might not be true.
    The usual algebraic topological definition of simple-connectedness is that every closed curve is null-homotopic.
    So $U$ is simply-connected in the algebraic topological way implies that it is simply connected in the complex analysis way in terms of winding number.
    This then implies that Cauchy's Theorem (in simply connected domains) holds.\\
    The reverse implication holds but complex simply-connectedness implying topological simply-connectedness part is not trivial.
\end{remark}


\section{Lazy Code Motion Problem}
\begin{flushright}
\textit{Notes by Akshin Singh}
\end{flushright}

As discussed in the previous module, the PRE problem has two components
\begin{enumerate}
\item All redundant computations of expressions that can be eliminated without duplication are eliminated.
\item The optimized program does not perform any extra computations that were not in the original program \textit{execution}.
\end{enumerate}

Lazy Code Motion (or LCM for short) adds another component to it.

\begin{enumerate}
  \setcounter{enumi}{2}
\item Expressions are computed at the late as possible (this is where the name \textbf{LAZY} comes from).
\end{enumerate}

\begin{figure}[h]
\centering
\includegraphics[scale = 0.5]{images/mod_106_fig1.png}
\caption{Lazy Code Motion example}
\label {fig:mod_106_01}
\end{figure}

Let us understand this with the help of an example. In Figure \ref{fig:mod_106_01}(a), we see that \textbf{m+n} is redundant on the red path,i.e., the loop's back edge. We will formally define what a back edge means in a later module. For now, the back edge means the edge that takes the execution from the loop's body back to its body.

PRE allows \ref{fig:mod_106_01}(b) but LCM does not. \textbf{WHY?} For PRE, components 1 and 2 are satisified in \ref{fig:mod_106_01}(b). For LCM component 3 is not satisfied. We can see this from the fact that the loop consisting of b1 needs to hold the value of \textbf{m+n} even though it does not need it. LCM component 3 is satisfied in \ref{fig:mod_106_01}(c).

Notice how \ref{fig:mod_106_01}(c) is a solution for both LCM and PRE whereas \ref{fig:mod_106_01}(b) is a solution for PRE but not LCM. This example suggests that $PRE \subset LCM$. In other words, LCM subsumes PRE. This is expected since PRE shares both its rules with LCM, but LCM has extra conditions as well.

\subsection{Full vs Partial Redundancy}

As eluded to in the last module, an expression is fully redundant at a program point if it is redundant on all paths to that program point. If an expressions is redundant on some but not all paths, then that expression is partially redundant.

Another way to frame what PRE does is the following: \textit{Can we place additional copies of an expression e which is partially redundant at program point p, such that it becomes fully redundant at p?} A fully redundant expression can be easily eliminated using common subexpression elimination.

\textbf{For more examples see the lecture module 106 on YouTube}.


\section{Complex eigenvalues}

In some applications, a matrix may have eigenvalues
\index{complex eigenvalues} which are complex numbers. For example, this often occurs in
differential equations. These questions are approached in the same way as above.

Consider the following example.

\begin{example}{A real matrix with complex eigenvalues}{real-matrix-complex-eigenvalues}
Let
\begin{equation*}
A=\begin{mymatrix}{rrr}
1 & 0 &  0 \\
0 & 2 & -1 \\
0 & 1 &  2
\end{mymatrix}
\end{equation*}
Find the eigenvalues and eigenvectors of $A$.
\end{example}

\begin{solution}
We will first find the eigenvalues as usual by solving the following equation.

\begin{equation*}
\det \paren{
\eigenvar \begin{mymatrix}{rrr}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{mymatrix}
- \begin{mymatrix}{rrr}
1 & 0 &  0 \\
0 & 2 & -1 \\
0 & 1 &  2
\end{mymatrix}} =0
\end{equation*}
This reduces to $ (\eigenvar -1) (
\eigenvar^{2}-4 \eigenvar +5) =0$. The solutions are $\lambda_1
=1,\lambda_2 = 2+i$ and $\lambda_3 =2-i$.

There is nothing new about finding the eigenvectors for $\lambda_1 =1$ so
this is left as an exercise.

Consider now the eigenvalue $\lambda_2 =2+i$. As usual, we solve the equation $(\lambda I -A) X = 0$ as given by
\begin{equation*}
(
(2+i) \begin{mymatrix}{rrr}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{mymatrix} -
\begin{mymatrix}{rrr}
1 & 0 & 0 \\
0 & 2 & -1 \\
0 & 1 & 2
\end{mymatrix}
)
X
 =\begin{mymatrix}{r}
0 \\
0 \\
0
\end{mymatrix}
\end{equation*}
In other words, we need to solve the system represented by the augmented matrix
\begin{equation*}
\begin{mymatrix}{crr|r}
1+i &  0 & 0 & 0 \\
0   &  i & 1 & 0 \\
0   & -1 & i & 0
\end{mymatrix}
\end{equation*}

We now use our row operations to solve the system.
Divide the first row by $(1+i) $ and then take
$-i$ times the second row and add to the third row. This yields
\begin{equation*}
\begin{mymatrix}{rrr|r}
1 & 0 & 0 & 0 \\
0 & i & 1 & 0 \\
0 & 0 & 0 & 0
\end{mymatrix}
\end{equation*}
Now multiply the second row by $-i$ to obtain the {\rref}, given by
\begin{equation*}
\begin{mymatrix}{rrr|r}
1 & 0 &  0 & 0 \\
0 & 1 & -i & 0 \\
0 & 0 &  0 & 0
\end{mymatrix}
\end{equation*}
Therefore, the eigenvectors are of the form
\begin{equation*}
t\begin{mymatrix}{r}
0 \\
i \\
1
\end{mymatrix}
\end{equation*}
and the basic eigenvector is given by
\begin{equation*}
X_2 =
\begin{mymatrix}{r}
0 \\
i \\
1
\end{mymatrix}
\end{equation*}

As an exercise, verify that the eigenvectors for $\lambda_3 =2-i$ are of the form
\begin{equation*}
t\begin{mymatrix}{r}
 0 \\
-i \\
 1
\end{mymatrix}
\end{equation*}
Hence, the basic eigenvector is given by
\begin{equation*}
X_3 = \begin{mymatrix}{r}
 0 \\
-i \\
 1
\end{mymatrix}
\end{equation*}

As usual, be sure to check your answers! To verify, we check that
$AX_3 = (2 - i) X_3$ as follows.
\begin{equation*}
\begin{mymatrix}{rrr}
1 & 0 &  0 \\
0 & 2 & -1 \\
0 & 1 &  2
\end{mymatrix} \begin{mymatrix}{r}
0 \\
-i \\
1
\end{mymatrix} = \begin{mymatrix}{c}
0 \\
-1-2i \\
2-i
\end{mymatrix} =(2-i) \begin{mymatrix}{r}
0 \\
-i \\
1
\end{mymatrix}
\end{equation*}

Therefore, we know that this eigenvector and eigenvalue are correct.
\end{solution}

Notice that in Example~\ref{exa:real-matrix-complex-eigenvalues}, two of the eigenvalues were given by
$\lambda_2 = 2 + i$ and $\lambda_3 = 2-i$. You may recall that these two complex numbers are \textbf{conjugates}.
It turns out that whenever a matrix containing real entries has a complex eigenvalue $\lambda$, it also has an eigenvalue
equal to $\conjugate{\lambda}$, the conjugate of $\lambda$.


\section{Recovery of Primitive Variables}
In order to recover the primitive from the conserved variables we need to solve the nonlinear equation:
\begin{equation}
    f\left(p\right)=p-\ol{p}\left(p\right)=0,
\end{equation}
where $\ol{p}\left(p\right)$ is the pressure as obtained via the ideal gas equation of state with an initial guess, $p$:
\begin{equation}
    \ol{p}=\left(\Gamma-1\right)\rho\,\epsilon,
\end{equation}
where
\begin{equation}
    \rho=\rho\left(\bU,p\right),\hspace{1em}\epsilon=\epsilon\left(\bU,p\right).
\end{equation}

In order to solve this equation we make use of the bisection method, and therefore need bounds on our initial guess for the pressure.

\subsection{Upper and Lower Bounds for Pressure}
We obtain a lower bound for the pressure with:
\begin{equation}
\tau=D\left(h\,W-1\right)-p\implies p=-\left(\tau+D\right)+D\,h\,W\geq-\left(\tau+D\right)+D\,h\,W\,\sqrt{v^{i}\,v_{i}}=-\left(\tau+D\right)+\sqrt{S^{i}\,S_{i}}.
\end{equation}
So, since the pressure must be non-negative, we have:
\begin{equation}
p\geq\text{MAX}\left[-\left(\tau+D\right)+\sqrt{S^{i}\,S_{i}},\text{SqrtTiny}\right].
\end{equation}

For an upper bound, we first note that:
\begin{equation}
h=1+\f{e+p}{\rho}=1+\f{\Gamma}{\Gamma-1}\f{p}{\rho}=1+\f{\Gamma}{\Gamma-1}\,\f{p\,W}{D},
\end{equation}
so,
%\begin{equation}
%    \tau=D\left(W+\f{\Gamma}{\Gamma-1}\f{p\,W^{2}}{D}-1\right)-p=D\left(W-1\right)+p\left(\f{\Gamma}{\Gamma-1}W^{2}-1\right).
%\end{equation}
%So,
%\begin{equation}
%    p=\f{\tau-D\left(W-1\right)}{\f{\Gamma}{\Gamma-1}W^{2}-1}.
%\end{equation}
%We also have:
%\begin{equation}
%    W=\left(1-v^{i}\,v_{i}\right)^{-1/2}=\left(1-\f{S^{i}\,S_{i}}{\left(\tau+D+p\right)^{2}}\right)^{-1/2}.
%\end{equation}
%Treating $p$ as an independent variable \sd{is this valid?}, we have:
%\begin{equation}
%    W\Big|_{p\rightarrow\infty}=1,
%\end{equation}
%which gives us an upper limit:
%\begin{equation}
%    p\leq\f{\Gamma-1}{\Gamma}\,\tau.
%\end{equation}
%Just to be safe, in the code we multiply this by two, so that:
%\begin{equation}
%    p\leq2\,\f{\Gamma-1}{\Gamma}\,\tau.
%\end{equation}
\begin{equation}
    \tau=D\left(W+\f{\Gamma}{\Gamma-1}\f{p\,W^{2}}{D}-1\right)-p=D\left(W-1\right)+p\left(\f{\Gamma}{\Gamma-1}W^{2}-1\right)>p\left(\f{\Gamma}{\Gamma-1}-1\right)=\f{p}{\Gamma-1}.
\end{equation}
So,
\begin{equation}
    p<\left(\Gamma-1\right)\tau.
\end{equation}
Typically, $\Gamma-1<3$. So, we end up with:
\begin{equation}
    p<3\,\tau.
\end{equation}


\subsection{A* search}

If the heuristic is admissible, then a* is optimal. Intuitively because the the heuristic steers away from any suboptimal solutions.

Admissible? For all nodes n , h(n)<=h*(n). where h* is true cost

\(f(n)=g(n)+h(n)\)

\(g(n)\) is the cost to reach \(n\) from the current position.

Informed: Yes

Time: Exponential

Space: Big, all nodes kept in memory

Complete: Yes

Optimal: Yes, if the heuristic is admissible


\subsubsection{Isobaric Processes}
Isobaric processes are compression/expansion processes in which the pressure of the gas does not change ($\Delta P = 0$). For example, consider a box of gas where the top face is a piston. If I heat up the gas with a candle, then the piston gets pushed up by the warming, expanding gas, while the pressure of the gas stays constant.  \\

First, let us determine the work done on the gas in this process:
\[W = -\int_{V_1}^{V_2} P(V)dV \]
As $P$ is a constant, we can just take it outside the integral, and this becomes a very straightforward integration:
\[W = -P\int_{V_1}^{V_2} dV = -P \cdot \left. V \right|_{V_1}^{V_2} = -P(V_2-V_1) = -P\Delta V\]
\begin{equation}
    W = -P\Delta V
\end{equation}
Which is exactly as we would have expected. \\

Now that we know what the work done in an isobaric process is, we can consider the heat flow. We return to the first law of thermodynamics:
\[ \Delta E = W + Q \]
We recall that the following formula (from section 1) always holds true for change in energy:
\[ \Delta E = nc_v\Delta T = n \frac{\chi}{2}R\Delta T \]
We substitute the formulas for work and change in energy we have obtained:
\[ n \frac{\chi}{2}R\Delta T  = -P\Delta V + Q \]
Now, we use the ideal gas law to recognize that:
\[ P\Delta V = nR \Delta T \]
Making this subtitution, we have:
\[ n \frac{\chi}{2}R\Delta T = -nR \Delta T + Q \]
Combining the like terms, we have:
\[ Q = nR\left(\frac{\chi}{2}+1\right)\Delta T \]
Now, let us define the heat capacity at constant pressure:
\begin{equation}
c_p = \left( \frac{\chi}{2}+1 \right)R = c_v+R
\end{equation}
Which gives us the formula for the heat in an isobaric process:
\begin{equation}
    Q = nc_p \Delta T
\end{equation}
You may very well recognize this formula from high school physics or chemistry! Something of interest to point out here; we notive that $c_p>c_v$, or that the heat capacity at constant pressure is higher than the heat capacity at constant volume. This tells us that when something is allowed to expand as it is heated (retaining constant pressure), it requires more energy to increase its temperature than if it is kept at a fixed volume.
\\
Finally, let's consider what an isobaric process looks like on a PV diagram. As you might suspect, since we fix $P$ and let $V$ vary throughout the whole process, we yet again get a straight line, except this time a horizontal one. Pictured below is an isothermal expansion:

\begin{center}
    \begin{tikzpicture}
 \draw[stealth-stealth] (0,5) node[below left]{$P$} |- (5,0) node[below left]{$V$};
\draw[thick,->] (1,2.5) -- (2.5,2.5);
\draw[thick] (2.5,2.5) -- (4,2.5);
\draw[dashed] (0,2.5) -- (1,2.5);
\draw[dashed] (1,0) -- (1,2.5);
\draw[dashed] (4,0) -- (4,2.5);
\filldraw (1,2.5) circle (2pt);
\filldraw (4,2.5) circle (2pt);
\node[below] at (4,0) {$V_2$};
\node[below] at (1,0) {$V_1$};
\node[left] at (0,2.5) {$P_1$};
\node[above] at (1,2.5) {$T_1$};
\node[above] at (4,2.5) {$T_2$};
\end{tikzpicture}
\end{center}
Again, for visual confirmation of our result for work during an isobaric process, we can see that the (negative) area under the curve is $-P_1\Delta V$, by inspection.


\section*{Exercise 4.1}
\enum{
\item
Since
\[
    \textbf{AX}=(X_1,X_2)=\left(\begin{aligned}
        &a_{11}X_1+a_{12}X_2\\
        &a_{21}X_1+a_{22}X_2
    \end{aligned}\right).
\]
then
\begin{align*}
    E[\textbf{AX}]&=E\bigg[\left(\begin{aligned}
        &a_{11}X_1+a_{12}X_2\\
        &a_{21}X_1+a_{22}X_2
    \end{aligned}\right)\bigg]
    =\left(\begin{aligned}
        &E[a_{11}X_1+a_{12}X_2]\\
        &E[a_{21}X_1+a_{22}X_2]
    \end{aligned}\right)\\
    &=\left(\begin{aligned}
        &a_{11}E[X_1]+a_{12}E[X_2]\\
        &a_{21}E[X_1]+a_{22}E[X_2]
    \end{aligned}\right)
    =\textbf{A}\left(\begin{aligned}
        &E[X_1]\\
        &E[X_2]
    \end{aligned}\right)
    =\textbf{A}E[\textbf{X}].
\end{align*}

\item
\begin{align*}
    \var(\textbf{AX})=\left(\begin{aligned}
        &\var(a_{11}X_1+a_{12}X_2) & \cov(a_{11}X_1+a_{12}X_2,a_{21}X_1+a_{22}X_2)\\
        &\cov(a_{21}X_1+a_{22}X_2,a_{11}X_1+a_{12}X_2) & \var(a_{21}X_1+a_{22}X_2)
    \end{aligned}\right)
\end{align*}
We denote that
\begin{align*}
    &t=\cov(X_1,X_2),\\
    &\cov(a_{21}X_1+a_{22}X_2,a_{11}X_1+a_{12}X_2)\\
    =&E[(a_{21}X_1+a_{22}X_2)(a_{11}X_1+a_{12}X_2)]-E[a_{21}X_1+a_{22}X_2]E[a_{11}X_1+a_{12}X_2]\\
    =&(a_{21}a_{12}+a_{22}a_{11})(E[X_1X_2]-E[X_1]E[X_2])+a_{11}a_{21}(E[X_1^2]-E[X_1]^2)+a_{22}a_{12}(E[X_2^2]-E[X_2]^2)\\
    =&(a_{21}a_{12}+a_{22}a_{11})\cov(X_1,X_2)+a_{11}a_{21}\var(X_1)+a_{22}a_{12}\var(X_2)\\
    =&(a_{21}a_{12}+a_{22}a_{11})t+a_{11}a_{21}\var(X_1)+a_{22}a_{12}\var(X_2),
\end{align*}
then
\begin{align*}
    \var(\textbf{AX})
    &=\left(\begin{aligned}
        &a_{11}^2\var(X_1)+a_{12}^2\var(X_2)+2a_{11}a_{12}t & \cov(a_{21}X_1+a_{22}X_2,a_{11}X_1+a_{12}X_2)\\
        &\cov(a_{21}X_1+a_{22}X_2,a_{11}X_1+a_{12}X_2) & a_{21}^2\var(X_1)+a_{22}^2\var(X_2)+2a_{21}a_{22}t
    \end{aligned}\right).
\end{align*}

Also,
\begin{align*}
    \textbf{A}\var(\textbf{X})&=\left(\begin{aligned}
        &a_{11}\var(X_1)+a_{12}\cov(X_1,X_2) & a_{11}\cov(X_1,X_2)+a_{12}\var(X_2)\\
        &a_{21}\var(X_1)+a_{22}\cov(X_1,X_2) & a_{21}\cov(X_1,X_2)+a_{22}\var(X_2)\\
    \end{aligned}\right).\\
    \textbf{A}^T&=\left(\begin{aligned}
        &a_{11} & a_{21}\\
        &a_{12} & a_{22}
    \end{aligned}\right).
\end{align*}
Hence, we know
\[
    \var(\textbf{AX})=\textbf{A}(\var(\textbf{X}))\textbf{A}^T.
\]

\item
Since $X_1$ and $X_2$ follow independent normal distributions, then $\varrho_{X}=0$.
\spl{
    f_X(x_1,x_2)=\frac{1}{2\pi\sigma_1\sigma_2}e^{-\frac{1}{2}\big[\frac{(x_1-\mu_1)^2}{\sigma_1^2}+\frac{(x_2-\mu_2)^2}{\sigma_2^2}\big]}.
}
\begin{align*}
    \Sigma_X=\left(\begin{aligned}
        &\sigma_1^2 & 0\\
        &0 & \sigma_2^2
    \end{aligned}\right), \quad
    \Sigma_X^{-1}=\frac{1}{\sigma_1^2\sigma_2^2}\left(\begin{aligned}
        &\sigma_2^2 & 0\\
        &0 & \sigma_1^2
    \end{aligned}\right)=\left(\begin{aligned}
        &\sigma_1^{-2} & 0\\
        &0 & \sigma_2^{-2}
    \end{aligned}\right),
\end{align*}

Hence,
\begin{align*}
    &\sqrt{\det\Sigma_X}=\sqrt{\sigma_1^2\sigma_2^2}=\sigma_1\sigma_2.\\
    &\Sigma_X^{-1}(x-\mu_X)=\left(\begin{aligned}
        &\sigma_1^{-2} & 0\\
        &0 & \sigma_2^{-2}
    \end{aligned}\right)
    \left(\begin{aligned}
        &x_1-\mu_1\\
        &x_2-\mu_2
    \end{aligned}\right)=
    \left(\begin{aligned}
        &\frac{x_1-\mu_1}{\sigma_1^2}\\
        &\frac{x_2-\mu_2}{\sigma_2^2}
    \end{aligned}\right).
\end{align*}
Then,
\begin{align*}
    \bigg<x-\mu_X,\Sigma_X^{-1}(x-\mu_X)\bigg>=\frac{(x_1-\mu_1)^2}{\sigma_1^2}+\frac{(x_2-\mu_2)^2}{\sigma_2^2}.
\end{align*}

Thus,
\spl{
    f_X(x)=f_X(x_1,x_2)=\frac{1}{2\pi\sqrt{\det\Sigma_X}}e^{-\frac{1}{2}\big<x-\mu_X,\Sigma_X^{-1}(x-\mu_X)\big>}.
}

\item
Since \textbf{A} is invertible, then $X=A^{-1}Y$.
\begin{align*}
    f_Y(y)=f_X(x)|\det(A^{-1})|=f_X(A^{-1}y)|\det(A^{-1})|.
\end{align*}

\begin{align*}
    \det\Sigma_{Y}&=\det\left(\begin{aligned}
        &\var(Y_1) & \cov(Y_1,Y_2)\\
        &\cov(Y_2,Y_1) & \var(Y_2)
    \end{aligned}\right)=\sigma_{Y_1}^2\sigma_{Y_2}^2-\cov(Y_1,Y_2)^2\\
    &=(a_{11}^2\sigma_1^2+a_{12}^2\sigma_2^2)(a_{21}^2\sigma_1^2+a_{22}^2\sigma_2^2)-(a_{11}a_{21}\sigma_1^2+a_{22}a_{12}\sigma_2^2)^2\\
    &=(a_{11}a_{22}-a_{12}a_{21})^2\sigma_1^2\sigma_2^2\\
    &=\det(A)^2\det(\Sigma_X).
\end{align*}

Hence,
\begin{align*}
    &\sqrt{|\det\Sigma_Y|}=\sqrt{|\det(A)^2\det(\Sigma_X)|},\\
    &\sqrt{\det\Sigma_X}=\frac{1}{|\det(A)|}\sqrt{|\det\Sigma_Y|}.\\
    &\Sigma_Y^{-1}(y-\mu_Y)=\var(Y)^{-1}
    \left(\begin{aligned}
        &y_1-\mu_{Y_1}\\
        &y_2-\mu_{Y_2}
    \end{aligned}\right)\\
    =&(A^{T})^{-1}\Sigma_X^{-1}A^{-1}\left(\begin{aligned}
        &y_1-\mu_{Y_1}\\
        &y_2-\mu_{Y_2}
    \end{aligned}\right)
\end{align*}

Since
\begin{align*}
    \left(\begin{aligned}
        &y_1-\mu_{Y_1}\\
        &y_2-\mu_{Y_2}
    \end{aligned}\right)=
    \left(\begin{aligned}
        &a_{11}X_1+a_{12}X_2-(a_{11}\mu_1+a_{12}\mu_2)\\
        &a_{21}X_1+a_{22}X_2-(a_{21}\mu_1+a_{22}\mu_2)
    \end{aligned}\right)=
    A\left(\begin{aligned}
        &x_1-\mu_{_1}\\
        &x_2-\mu_{_2}
    \end{aligned}\right),
\end{align*}
then
\begin{align*}
    \Sigma_Y^{-1}(y-\mu_Y)=(A^{T})^{-1}\Sigma_X^{-1}(x-\mu_X).
\end{align*}

\begin{align*}
    \big<y-\mu_Y,\Sigma_Y^{-1}(y-\mu_Y)\big>&=\big<A(x-\mu_X),(A^T)^{-1}\Sigma_X^{-1}(x-\mu_X)\big>\\
    &=(x-\mu_X)^TA^T(A^T)^{-1}\Sigma_X^{-1}(x-\mu_X)\\
    &=\big<x-\mu_X,\Sigma_X^{-1}(x-\mu_X)\big>.
\end{align*}

Finally we plug in the terms,
\spl{
    f_Y(y)&=f_X(A^{-1}y)\det(A^{-1})\\
    &=\frac{|\det(A)|}{2\pi\sqrt{|\det\Sigma_Y|}}e^{-\frac{1}{2}\big<y-\mu_Y,\Sigma_Y^{-1}(y-\mu_Y)\big>}|\det(A^{-1})|\\
    &=\frac{1}{2\pi\sqrt{|\det\Sigma_Y|}}e^{-\frac{1}{2}\big<y-\mu_Y,\Sigma_Y^{-1}(y-\mu_Y)\big>}.
}

\item
We denote that $\varrho=\cov(Y_1,Y_2)/(\sigma_{Y_1}\sigma_{Y_2})$, then
\begin{align*}
    1-\varrho^2&=1-\frac{\cov^2(Y_1,Y_2)}{\sigma_{Y_1}^2\sigma_{Y_2}^2}\\
    &=\frac{\sigma_{Y_1}^2\sigma_{Y_2}^2-\cov^2(Y_1,Y_2)}{\sigma_{Y_1}^2\sigma_{Y_2}^2}\\
    &=\frac{\det\Sigma_Y}{\sigma_{Y_1}^2\sigma_{Y_2}^2}
\end{align*}

Hence,
\begin{align*}
    \sqrt{|\det\Sigma_Y|}=\sigma_{Y_1}\sigma_{Y_2}\sqrt{1-\varrho^2}.
\end{align*}

Also,
\begin{align*}
    &\big<y-\mu_Y,\Sigma_Y^{-1}(y-\mu_Y)\big>\\
    =&\frac{1}{1-\varrho^2}\frac{1}{\sigma_{Y_1}^2\sigma_{Y_2}^2}\big<y-\mu_Y,\Sigma_Y^*(y-\mu_Y)\big>\\
    =&\frac{1}{1-\varrho^2}\frac{1}{\sigma_{Y_2}^2\sigma_{Y_2}^2}(\sigma_{Y_1}^2(y_1-\mu_{Y_1})^2-2\cov(Y_1,Y_2)(y_1-\mu_{Y_1})(y_2-\mu_{Y_2})+\sigma_{Y_1}^2(y_2-\mu_{Y_2})^2)\\
    =&\frac{1}{1-\varrho^2}\big[\frac{(y_1-\mu_{Y_1})^2}{\sigma_{Y_1}^2}-2\varrho(\frac{y_1-\mu_{Y_1}}{\sigma_{Y_1}})(\frac{y_2-\mu_{Y_2}}{\sigma_{Y_2}})+\frac{(y_2-\mu_{Y_2})^2}{\sigma_2^2}\big]
\end{align*}
Hence,
\begin{align*}
    f_Y(y_1,y_2)=\frac{1}{2\pi\sigma_{Y_1}\sigma_{Y_2}\sqrt{1-\varrho^2}}e^{-\frac{1}{2(1-\varrho^2)}\big[\frac{(y_1-\mu_{Y_1})^2}{\sigma_{Y_1}^2}-2\varrho(\frac{y_1-\mu_{Y_1}}{\sigma_{Y_1}})(\frac{y_2-\mu_{Y_2}}{\sigma_{Y_2}})+\frac{(y_2-\mu_{Y_2})^2}{\sigma_2^2}\big]}.
\end{align*}

}

\section*{Exercise 4.2}
\begin{align*}
    E[Y]=E\bigg[\left(\begin{aligned}
        a_{11}X_1+a_{12}X_2\\
        a_{21}X_1+a_{22}X_2
    \end{aligned}\right)\bigg]=
    \left(\begin{aligned}
        a_{11}E[X_1]+a_{12}E[X_2]\\
        a_{21}E[X_1]+a_{22}E[X_2]
    \end{aligned}\right)=
    A\left(\begin{aligned}
        E[X_1]\\
        E[X_2]
    \end{aligned}\right)=AE[X].
\end{align*}

\begin{align*}
    \var(Y)&=\left(\begin{aligned}
        &\var(Y_1) & \cov(Y_1,Y_2)\\
        &\cov(Y_2,Y_1) & \var(Y_2)
    \end{aligned}\right)
    =\left(\begin{aligned}
        &(a_{11}^2+a_{12}^2)\sigma^2 & (a_{11}a_{21}+a_{22}a_{12})\sigma^2\\
        &(a_{11}a_{21}+a_{22}a_{12})\sigma^2 & (a_{21}^2+a_{22}^2)\sigma^2
    \end{aligned}\right)\\
    &=\sigma^2\left(\begin{aligned}
        &a_{11}^2+a_{12}^2 & a_{11}a_{21}+a_{22}a_{12}\\
        &a_{11}a_{21}+a_{22}a_{12} & a_{21}^2+a_{22}^2
    \end{aligned}\right).
\end{align*}

Since $A^T=A^{-1}$, then
\begin{align*}
    AA^T=I=\left(\begin{aligned}&1&0\\&0&1\end{aligned}\right)
        =\left(\begin{aligned}
        &a_{11}^2+a_{12}^2 & a_{11}a_{21}+a_{22}a_{12}\\
        &a_{11}a_{21}+a_{22}a_{12} & a_{21}^2+a_{22}^2
    \end{aligned}\right).
\end{align*}

Hence,
\begin{align*}
    \var(Y)=\sigma^2\left(\begin{aligned}&1&0\\&0&1\end{aligned}\right).
\end{align*}

\section*{Exercise 4.3}
Denote that $x=z_{\alpha_1}$ and $y=z_{\alpha_2}$. Then we obtain
\spl{
    &\Phi(-x)+\Phi(-y)=\alpha,\\
    &g(x,y):=\Phi(-x)+\Phi(-y)-\alpha=0.
}

Now we want to calculate the \textbf{conditional extreme} values of
\spl{
    f(x,y):=\frac{(x+y)\sigma}{\sqrt{n}}.
}

Thus we have the \textbf{partial differentiations} of $F(x,y,\lambda)=f(x,y)+\lambda g(x,y)$.

\[
    \left\{\begin{aligned}
        &F_x=\frac{\sigma}{\sqrt{n}}-\lambda f_N(-x)=0\\
        &F_y=\frac{\sigma}{\sqrt{n}}-\lambda f_N(-y)=0\\
        &F_\lambda=\Phi(-x)+\Phi(-y)-\alpha=0\\
    \end{aligned}\right. ,
\]

where $f_N(\cdot)$ is the density of a standard normal distribution.

We find that $x=y$, which means $z_{\alpha_1}=z_{\alpha_2}$. Also, $\alpha_1+\alpha_2=\alpha$.

Thus $\alpha_1=\alpha_2=\alpha/2$.

\section*{Exercise 4.4}
Since $(n-1)s^2/sigma_2$ follows a chi-squared distribution, then
\spl{
    1-\alpha&=P[\chi_{1-\alpha/2,n-1}^2\leq(n-1)s^2\sigma^2\leq\chi_{\alpha/2,n-1}^2]\\
    &=P\bigg[\frac{(n-1)s^2}{\chi_{\alpha/2,n-1}^2}\leq\sigma^2\leq\frac{(n-1)s^2}{\chi_{1-\alpha/2,n-1}^2}\bigg]\\
    &=P\bigg[\sqrt{\frac{(n-1)s^2}{\chi_{\alpha/2,n-1}^2}}\leq\sigma\leq\sqrt{\frac{(n-1)s^2}{\chi_{1-\alpha/2,n-1}^2}}\bigg].
}

Since $\alpha=0.05$, then
\spl{
    \chi_{0.025,50}^2&=71.42,\\
    \chi_{0.975,50}^2&=32.36.
}

Therefore,
\spl{
    \sqrt{\frac{50\cdot0.37^2}{71.42}}\leq&\sigma\leq\sqrt{\frac{50\cdot0.37^2}{32.36}}\\
    0.31\leq&\sigma\leq0.46.
}

Hence, the 95\% two-sided confidence interval for $\sigma$ is [0.31,0.46].

\section*{Exercise 4.5}
\enum{
\item
We consider the exclusive situations. If all the samples are greater than $M$ or less than $M$, then $M$ will not fall between $X_{min}$ and $X_{max}$.

Since $F(M)=\frac{1}{2}$, which means the probability that a sample is less or greater than $M$ is both $\frac{1}{2}$.

Hence the probability that all the samples are greater than $M$ or less than $M$ is
\spl{
    \bigg(\frac{1}{2}\bigg)^n+\bigg(\frac{1}{2}\bigg)^n=\bigg(\frac{1}{2}\bigg)^{n-1}.
}

Therefore, the probability that $M$ falls between $X_{min}$ and $X_{max}$ is
\spl{
    P[X_{min}\leq M \leq X_{max}]=1-\bigg(\frac{1}{2}\bigg)^{n-1}.
}

\item
For $P[X_{k+1}\leq M\leq X_{n-k}]$, we can also consider the exclusive situations. In this case, the probability that the samples $X_{k+1}$ to $X_{n-k}$ are greater than $M$ or less than $M$ is
\spl{
    \sum_{x=1}^k\binom{n}{x}\bigg(\frac{1}{2}\bigg)^{x}\bigg(\frac{1}{2}\bigg)^{n-x}+\sum_{x=1}^k\binom{n}{n-x}\bigg(\frac{1}{2}\bigg)^{x}\bigg(\frac{1}{2}\bigg)^{n-x}=\sum_{x=1}^k\binom{n}{x}\bigg(\frac{1}{2}\bigg)^{n-1}.
}

\spl{
    P[X_{k+1}\leq M\leq X_{n-k}]=1-\sum_{x=1}^k\binom{n}{x}\bigg(\frac{1}{2}\bigg)^{n-1}.
}
}


\section{Neural Nets}

\paragraph{Neural Net} A neural net is a representation, that is a
arithmetic constraint net in which:
\begin{itemize}
  \item Operation frames denote arithmetic constraints modeling
    synapses and neurons
  \item Demon procedures propagate stimuli through synapses and
    neurons
\end{itemize}
With demon procedures defines assignment:
\begin{itemize}
  \item When a value is written into a synapse's input slot, write
    the product of the value and the synapse's weight into the
    synapse's output slot
  \item When a value is written into a synapse's output slot,
    check the following neuron to see whether all its input
    synapses' outputs have values:
    \begin{itemize}
      \item If they do, add the outputs values of the input
        synapses together, pass the sum through the activation
        function, and write the appropriate value into the
        neuron's output slot
      \item If they don't, do nothing
    \end{itemize}
\end{itemize}

To do back propagation to train a neural net:
\begin{itemize}
  \item Pick a rate parameter $r$
  \item Until performance is satisfactory,
    \begin{itemize}
      \item For each input,
        \begin{itemize}
          \item Compute the resulting output
          \item Compute $\beta$ for nodes in the output layers
            using $\beta_{x} = d_x - o_x$
          \item Compute $\beta$ for all other nodes using:
            \begin{math}
              \beta_j = \sum_{k}{w_{jk}o_k(1-o_k)\beta_k}
            \end{math}
          \item Compute weight changes for all weight using:
            \begin{math}
              \Delta{w_{ij}} = ro_io_k(1-o_k)\beta_j
            \end{math}
        \end{itemize}
      \item Add up the weight changes to all sample inputs and
        changes the weights
    \end{itemize}
\end{itemize}

\subsection{Back-propagation characteristics}

\begin{itemize}
  \item Training may require thousands of back propagations
  \item Back-propagation can be done in stages
  \item Back-propagation can train a net to learn to recognize
    multiple concepts simultaneously
  \item Trained neural nets can make predictions
  \item Excess weights lead to overfitting. Rule of thumb: be sure
    that the number of trainable weights influencing any
    particular output is smaller than the number of training
    samples
  \item Neural-bet training is an article
\end{itemize}

\subsection{Perceptrons}

\paragraph{Perceptron} A perceptron is a representation, that is a
neural net in which:
\begin{itemize}
  \item There is only one neuron
  \item The input sare binary
  \item Logic boxes may be interposed between the perceptron's
    inputs and the perceptron's weights. Each logic box can be
    viewed as a table that produces an output value of 0s or 1 for
    each combination os 0s and 1s that can appear at its inputs
  \item The output of the perceptron is 0s or 1 depending on
    whether the weighted sum of the logic-box outputs is greater
    than the threshold.
\end{itemize}

The perceptron convergence procedure guarantees success whenever
success is possible.

To train a perceptron:
\begin{itemize}
  \item Until the perceptron yields the correct result for each
    training sample, for each sample,
    \begin{itemize}
      \item If the perceptron yields the wrong answer,
        \begin{itemize}
          \item If the perceptron says no when it should say yes,
            add the logic-box output vector to the weight vector
          \item Otherwise, subtract the logic-box output vector
            from the weight vector
        \end{itemize}
      \item Otherwise, do nothing
    \end{itemize}
\end{itemize}


In the general linear regression setting, we cannot assume that the data is
centered. We might have a persistent constant offset vector added to the input
features or the output which will cause the centered recursive least squares
estimator to be inaccurate or have worse generalization. In the static data
regime, estimation of this constant offset can be done prior to solving the
least squares problem by calculating the feature and output means. In online
estimation, we need to not only update the means at each timestep but also to
correct the previous parameter estimate with respect to the new mean estimate.
While the algebra becomes a bit more complex, the eventual structure of the
update equations is remarkably similar to the uncentered case.

In order to contain the complexity of the following derivation, we proceed in
stages. First, we will consider the case when the input features are centered
but the output is not. Then we will consider the inverse case, where the input
features are not centered but the output is. We will then see how these two
cases can be combined in the general uncentered recursive least squares
estimator.

\subsection{Centered X, Uncentered Y}
In this case we want to solve the problem
\begin{equation}
  (y_T - \mu_y(T)) = \varphi_T\hat\Theta(T)
\end{equation}
When the output data we are provided by $F(t)$ are not already centered, we
center it prior to solving the least squares problem. Note that it is simple to calculate the output mean online, since
\begin{align}
  \mu_y(T) &= \frac{1}{T}\sum_{t=1}^T y_t \\
           &= \frac{T - 1}{T}\mu_y(T - 1) + \frac{1}{T}y_T \\
  \implies T\mu_y(T) &= (T - 1)\mu_y(T - 1) + y_T \\
                     &= T \mu_y(T - 1) + y_T - \mu_y(T - 1) \\
  \implies \mu_y(T) &= \mu_y(T - 1) + \frac{1}{T} (y_T - \mu_y(T-1))
\end{align}
Thus the normal equation in this case is given by
\begin{align}
  (X_T^\top X_T)^{-1} X_T^\top \left(Y_T - \bar{1}\mu_y(T)\right) &= (X_T^\top X_T)^{-1} X_T^\top \left(Y_T - \bar{1}\frac{1}{T}\sum_{t=1}^Ty_t\right) \\
                                                                  &= (X_T^\top X_T)^{-1} X_T^\top Y_T - (X_T^\top X_T)^{-1} X_T^\top \bar{1}\frac{1}{T}\sum_{t=1}^Ty_t \label{eq:split_uncentered_y}
\end{align}
Note that the first term in Equation~\ref{eq:split_uncentered_y} is just the
normal equation for the centered least squares problem that we already derived
recursive update equations for, so all that remains is expanding the second
term as
\begin{align}
  (X_T^\top X_T)^{-1} X_T^\top \bar{1}\mu_y(T) &= P_T \sum_{t=1}^T \varphi_t^\top \mu_y(T) \\
                                               &= T \cdot P_T \mu_x(T)^\top \mu_y(T) \label{eq:correction_1}
\end{align}
Of course, in the current case $\mu_x = \bar{0}$, so this correction term is
actually zero and we recover the same update equations as previously derived.
However, Equation~\ref{eq:correction_1} will come in handy later when we derive
the general uncentered update.

Even though the update equations are the same, the final prediction of our
model includes a constant offset term that we can derive from the model
equation
\begin{align}
  (y_T - \mu_y(T)) &= \varphi_T\hat\Theta_{LS}(T) \\
  \implies y_T &= \varphi_T\hat\Theta_{LS}(T) + \mu_y(T)
\end{align}

\subsection{Uncentered X, Centered Y}
In this case we want to solve the problem
\begin{equation}
  y_T = (\varphi_T - \mu_x(T))\hat\Theta(T)
\end{equation}
In the same way that we calculated the update equation for $\mu_y$, we calculate
\begin{equation}
  \mu_x(T) = \mu_x(T - 1) + \frac{1}{T}(\varphi_T - \mu_x(T - 1))
\end{equation}
The normal equation for this case is given by
\begin{multline}
  \label{eq:expanded_uncentered_x_normal}
  \left[(X_T - \bar{1}\mu_x(t))^\top(X_T - \bar{1}\mu_x(t))\right]^{-1}(X_T - \bar{1}\mu_x(t))^\top Y = \\ \left[(X_T^\top X_T - X_T^\top \bar{1}\mu_x(T) - (\bar{1}\mu_x(T))^\top X_T + \mu_x(T)^\top\mu_x(T)\right]^{-1}(X_T - \bar{1}\mu_x(t))^\top Y
\end{multline}
Note that
\begin{align}
  X_T^\top \bar{1}\mu_x(T) &= \sum_{t=1}^T \varphi_t^\top \mu_x(T) \\
                           &= T \mu_x(T)^\top \mu_x(T)
\end{align}
Thus Equation~\ref{eq:expanded_uncentered_x_normal} becomes
\begin{multline}
  \left[(X_T^\top X_T - 2T\mu_x(T)^\top\mu_x(T) + \mu_x(T)^\top\mu_x(T)\right]^{-1}(X_T - \bar{1}\mu_x(t))^\top Y = \\ \left[(P_T^{-1} - (2T - 1)\mu_x(T)^\top\mu_x(T)\right]^{-1}(X_T - \bar{1}\mu_x(t))^\top Y
\end{multline}
Let us define, analogously to $P_T$ from before,
\begin{equation}
  Q_T := \left[P_T^{-1} - (2T - 1)\mu_x(T)^\top\mu_x(T)\right]^{-1}
\end{equation}

\subsubsection{Deriving the $Q_T$ Update}
As with $P_T$, we begin by developing an update for $Q_T^{-1}$ in terms of $Q_{T-1}^{-1}$
\begin{align}
  Q_T^{-1} &= P_T^{-1} - (2T - 1)\mu_x(T)^\top\mu_x(T) \\
           &= P_{T-1}^{-1} + \varphi_T^\top\varphi_T - \frac{2T - 1}{T^2}((T-1)\mu_x(T-1) + \varphi_T)^\top((T-1)\mu_x(T-1) + \varphi_T) \label{eq:Q_T_partial}
\end{align}
As a side computation, and to avoid stacking even longer equations, let $\mu := \mu_x(T - 1)$ and note
\begin{align}
  ((T - 1)\mu + \varphi_T)^\top((T-1)\mu + \varphi_T) &= (T - 1)^2\mu^\top\mu + (T-1)\mu^\top\varphi_T + (T-1)\varphi_T^\top\mu + \varphi_T^\top\varphi_T
\end{align}
With this, Equation~\ref{eq:Q_T_partial} can be written as
\begin{align}
  Q_T^{-1} &= (P_{T-1}^{-1} - (2T - 1)\mu^\top\mu + 2\mu^\top\mu) - 2\mu^\top\mu + \varphi_T^\top\varphi_T- \frac{2T - 1}{T^2}\left[(-2T + 1)\mu^\top\mu + (T-1)\mu^\top\varphi_T + (T-1)\varphi_T^\top\mu + \varphi_T^\top\varphi_T\right] \\
           &= (P_{T-1}^{-1} - (2(T-1) - 1)\mu^\top\mu) - 2\mu^\top\mu + \varphi_T^\top\varphi_T + \frac{2T - 1}{T^2}\left[(-2T + 1)\mu^\top\mu + (T-1)\mu^\top\varphi_T + (T-1)\varphi_T^\top\mu + \varphi_T^\top\varphi_T\right] \\
           &= Q_{T-1}^{-1} - 2\mu^\top\mu + \varphi_T^\top\varphi_T- \frac{2T - 1}{T^2}\left[(-2T + 1)\mu^\top\mu + (T-1)\mu^\top\varphi_T + (T-1)\varphi_T^\top\mu + \varphi_T^\top\varphi_T\right]
\end{align}
One final expansion:
\begin{equation}
  Q_T^{-1} = Q_{T-1}^{-1} + \frac{1}{T^2}\left[((2T+1)^2 - 2T^2)\mu^\top\mu - (2T - 1)(T - 1)\mu^\top\varphi_T - (2T - 1)(T - 1)\varphi_T^\top\mu + (T^2 - 2T + 1)\varphi_T^\top\varphi_T\right]
\end{equation}
And now we can see that this can be written as
\begin{equation}
  Q_T^{-1} = Q_{T-1}^{-1} + \frac{1}{T^2}
  \begin{bmatrix}
    \mu_x(T-1)^\top & \varphi_T^\top
  \end{bmatrix}
  \begin{bmatrix}
    (2T - 1)^2 - 2T^2 & -(2T - 1)(T - 1) \\
    -(2T - 1)(T - 1) & (T - 1)^2
  \end{bmatrix}
  \begin{bmatrix}
    \mu_x(T - 1) \\
    \varphi_T
  \end{bmatrix}
\end{equation}
Let us define
\begin{align}
  C_T &:= \frac{1}{T^2}
  \begin{bmatrix}
    (2T - 1)^2 - 2T^2 & -(2T - 1)(T - 1) \\
    -(2T - 1)(T - 1) & (T - 1)^2
  \end{bmatrix} \\
  V_T &:=
  \begin{bmatrix}
    \mu_x(T - 1) \\
    \varphi_T
  \end{bmatrix} \\
  R_T &:= V_T^\top C_T V_T
\end{align}
So that
\begin{equation}
  \label{eq:Q_T_inv}
  Q_T^{-1} = Q_{T - 1}^{-1} + R_T
\end{equation}
The Woodbury matrix identity (Equation~\ref{eq:woodbury}) gives us, at the end
of all this, an update rule for $Q_T$:
\begin{equation}
  Q_T = Q_{T - 1} - Q_{T - 1}V_T^\top\left(C_T^{-1} + V_TQ_{T-1}V_T^\top\right)^{-1}V_TQ_{T-1} \label{eq:uncentered_Q_T_update}
\end{equation}
Note that this is a rank-2 update to $Q_{T-1}$, since we are using both the
sample mean at time $T - 1$ and the new data at time $T$ to compute the update.

\subsubsection{Deriving the $\hat\Theta$ Update}
Returning to the normal equation
\begin{align}
  \hat\Theta_{LS}(T) &= Q_T (X_T - \bar{1} \mu_x(T))^\top Y \\
                     &= Q_TX^\top Y - Q_T \mu_x(T)^\top\bar{1}^\top Y \\
                     &= Q_T\left[Q_{T-1}^{-1}(t-1)\hat\Theta_{LS}(T-1) + \varphi_T^\top y_T\right] - TQ_T\mu_x(T)^\top \mu_y(T) \\
                     &= Q_T \varphi_T^\top y_T + Q_T\left[Q_{T}^{-1} - R_T\right]\hat\Theta_{LS}(T-1) - TQ_T\mu_x(T)^\top\mu_y(T) \\
                     &= \hat\Theta_{LS}(T-1) + Q_T\left[\varphi_T^\top y_T - R_T\hat\Theta_{LS}(T-1)\right] - TQ_T\mu_x(T)^\top \mu_y(T) \label{eq:uncentered_x_theta_with_correction}
\end{align}
Since in the current case we are assuming that $Y$ is already centered,
$\mu_y(T) = \bar{0}$ and the above reduces to
\begin{equation}
  \hat\Theta_{LS}(T) = \hat\Theta_{LS}(T-1) + Q_T\left[\varphi_T^\top y_T - R_T\hat\Theta_{LS}(T-1)\right] \label{eq:uncentered_x_theta_update}
\end{equation}
This corresponds roughly to the $\hat\Theta$ update in the centered case (Equation~\ref{eq:centered_theta_update}).

\subsection{Uncentered X, Uncentered Y}
We have finally arrived at the general case, in which our problem is expressed as
\begin{equation}
  (y_T - \mu_y(T)) = (\varphi_T - \mu_x(T))\hat\Theta(T)
\end{equation}
The normal equation for this case is
\begin{align}
  \left[(X_t - \bar{1}\mu_x(T))^\top(X_T - \bar{1}\mu_x(T))\right]^{-1}(X_T &- \bar{1}\mu_x(T))^\top (Y_T - \bar{1}\mu_y(T)) \nonumber \\ &= Q_T(X_T - \bar{1}\mu_x(T))^\top (Y_T - \bar{1}\mu_y(T)) \\
                                                                            &= Q_T (X_T - \bar{1}\mu_x(T))^\top Y - Q_T(X_T - \bar{1}\mu_x(T))^\top\bar{1}\mu_y(T) \label{eq:uncentered_xy_normal}
\end{align}
Note that the first term in Equation~\ref{eq:uncentered_xy_normal} is the
normal equation for the uncentered X, centered Y case previously analyzed. Thus
all that we need to do in order to derive the update for $\hat\Theta$ is expand the second term. This is easy, though, because
\begin{align}
  Q_T(X_T - \bar{1}\mu_x(T))^\top\bar{1}\mu_y(T) &= Q_TX_T^\top\bar{1}\mu_y(T) - Q_T\mu_x(T)^\top\bar{1}^\top\bar{1}\mu_y(T)\\
                                                 &= TQ_T\mu_x(T)^\top \mu_y(T) - Q_T\mu_x(T)^\top\mu_y(T) \\
                                                 &= (T - 1)Q_T \mu_x(T)^\top \mu_y(T)
\end{align}
This, combined with the correction term in
Equation~\ref{eq:uncentered_x_theta_with_correction} (which is now nonzero
since we assume that $\mu_Y(T) \neq 0$), gives us a total correction of $(2T -
1) Q_T \mu_x(T)^\top \mu_y(T)$.

Since the update equation for $Q_T$ depends only on $\varphi_T$ and $\mu_x(T)$,
it only remains to give the final update equation for $\hat\Theta(T)$ in the
uncentered X, uncentered Y case. Combining Equation~\ref{eq:uncentered_x_theta_update} with the previously stated correction term, we get the following update:
\begin{align}
  \hat\Theta_{RAW}(T) &= \hat\Theta_{RAW}(T-1) + Q_T\left[\varphi_T^\top y_T - R_T\hat\Theta_{RAW}(T-1)\right]\\
  \hat\Theta_{LS}(T) &= \hat\Theta_{RAW}(T) - (2T - 1)Q_T\mu_x(T)^\top \mu_y(T)
\end{align}

Recall that our original problem in the uncentered case was
\begin{equation}
  (y_T - \mu_y(T)) = (\varphi_T - \mu_x(T))\hat\Theta
\end{equation}
Thus the prediction of the recursive least squares filter in the uncentered X, uncentered Y case for a new $\varphi'$ is given by
\begin{align}
  (\hat{y}' - \mu_y(T)) &= (\varphi' - \mu_x(T))\hat\Theta_{LS}(T) \\
  \implies \hat{y}' &= \varphi'\hat\Theta_{LS}(T) + (\mu_y(T) - \mu_x(T)\hat\Theta_{LS}(T))
\end{align}


\section{The Integral Test}\label{sec:IntegralTest}

It is generally quite difficult, often impossible, to determine
the value of a series exactly. In many cases it is possible at least
to determine whether or not the series converges, and so we will spend
most of our time on this problem.

If all of the terms $\ds a_n$ in a series are non-negative, then clearly
the sequence of partial sums $\ds s_n$ is non-decreasing. This means that
if we can show that the sequence of partial sums is bounded, the
series must converge. Many useful and interesting series have this property,
and they are among the easiest to understand. Let's look at
an example.

\begin{example}{}{}
Show that $\ds\sum_{n=1}^\infty {1\over n^2}$ converges.
\end{example}
\begin{solution}
The terms $\ds 1/n^2$ are  positive and decreasing, and since
$\ds\lim_{x\to\infty} 1/x^2=0$, the terms $\ds 1/n^2$ approach zero. We
seek an upper bound for all the partial sums, that is, we want to find
a number $N$ so that $s_n\le N$ for every $n$. The upper bound is
provided courtesy of integration, and is illustrated in
figure~\xrefn{fig:integral test for one over n squared}.

\begin{figure}[H]
\centerline{
%\texonly
\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <2truecm,2truecm>
\setplotarea x from 0 to 5, y from 0 to 2
\axis left ticks numbered from 0 to 2 by 1 /
\axis bottom  ticks numbered from 0 to 5 by 1 /
\setquadratic
\plot 0.750 1.778 0.892 1.258 1.033 0.937 1.175 0.724 1.317 0.577
1.458 0.470 1.600 0.391 1.742 0.330 1.883 0.282 2.025 0.244
2.167 0.213 2.308 0.188 2.450 0.167 2.592 0.149 2.733 0.134
2.875 0.121 3.017 0.110 3.158 0.100 3.300 0.092 3.442 0.084
3.583 0.078 3.725 0.072 3.867 0.067 4.008 0.062 4.150 0.058
4.292 0.054 4.433 0.051 4.575 0.048 4.717 0.045 4.858 0.042
5.000 0.040 /
\putrule from 1 0 to 1 1
\putrule from 2 0 to 2 0.25
\putrule from 3 0 to 3 0.1111
\putrule from 0 1 to 1 1
\putrule from 1 0.25 to 2 0.25
\putrule from 2 0.1111 to 3 0.1111
%\sevenpoint
\put {$A=1$} at 0.5 0.5
\put {$A=1/4$} at 1.5 0.125
\endpicture}}
%\endtexonly
%\htmlfigure{Sequences_series-integral_test_upper_bound.html}
\caption{Graph of $\ds y=1/x^2$ with rectangles.}
\label{fig:integral test for one over n squared}
\end{figure}

The figure shows the graph of $\ds y=1/x^2$ together with some rectangles
that lie completely below the curve and that all have base length
one. Because the heights of the rectangles are determined by the
height of the curve, the areas of the rectangles are $\ds 1/1^2$, $\ds 1/2^2$,
$\ds 1/3^2$, and so on---in other words, exactly the terms of the
series. The partial sum $\ds s_n$ is simply the sum of the areas of the
first $n$ rectangles. Because the rectangles all lie between the curve
and the $x$-axis, any sum of rectangle areas is less than the
corresponding area under the curve, and so of course any sum of
rectangle areas is less than the area under the entire curve. Unfortunately,
because of the asymptote at $x=0$, the integral $\int_0^{\infty}\frac{1}{x^2}$
is infinite, but we can deal with this by separating the first term from
the series and integrating from 1:
\[\ds s_n=\sum_{i=1}^{n}\frac{1}{i^2}=1+\sum_{i=2}^{n}\frac{1}{i^2}<1+\int_1^n\frac{1}{x^2}\,dx<1+\int_1^{\infty}\frac{1}{x^2}\,dx=1+1=2\]
(Recalling that we computed this improper integral in
section~\ref{sec:ImproperIntegrals}). Since the sequence of partial
sums $\ds s_n$ is increasing and bounded above by 2, we know that
$\ds\lim_{n\to\infty}s_n=L<2$, and so the series converges to some
number less than 2. In fact, it is possible, though difficult, to show
that $\ds L=\pi^2/6\approx 1.6$.
\end{solution}

We already know that $\sum 1/n$ diverges. What goes wrong if we try to
apply this technique to it? Here's the calculation:
$$
  s_n={1\over 1}+{1\over 2}+{1\over 3}+\cdots+{1\over n}
  < 1 + \int_1^n {1\over x}\,dx < 1+\int_1^\infty {1\over x}\,dx
  =1+\infty.
$$
The problem is that the improper integral doesn't converge. Note
that this does \emph{not} prove that $\sum 1/n$ diverges, just that
this particular calculation fails to prove that it converges. A slight
modification, however, allows us to prove in a second way that $\sum
1/n$ diverges.

Consider a slightly altered version of Figure~\ref{fig:integral
test for one over n squared}, shown in Figure~\ref{fig:integral test
for one over n}.

\begin{figure}[H]
\centerline{
%\texonly
\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <2truecm,2truecm>
\setplotarea x from 0 to 5, y from 0 to 2
\axis left ticks numbered from 0 to 2 by 1 /
\axis bottom  ticks numbered from 0 to 5 by 1 /
\setquadratic
\plot 0.500 2.000 0.650 1.538 0.800 1.250 0.950 1.053 1.100 0.909
1.250 0.800 1.400 0.714 1.550 0.645 1.700 0.588 1.850 0.541
2.000 0.500 2.150 0.465 2.300 0.435 2.450 0.408 2.600 0.385
2.750 0.364 2.900 0.345 3.050 0.328 3.200 0.312 3.350 0.298
3.500 0.286 3.650 0.274 3.800 0.263 3.950 0.253 4.100 0.244
4.250 0.235 4.400 0.227 4.550 0.220 4.700 0.213 4.850 0.206
5.000 0.200 /
\putrule from 1 0 to 1 1
\putrule from 2 0 to 2 1
\putrule from 3 0 to 3 0.5
\putrule from 4 0 to 4 0.3333
\putrule from 1 1 to 2 1
\putrule from 2 0.5 to 3 0.5
\putrule from 3 0.3333 to 4 0.3333
%\sevenpoint
\put {$A=1$} at 1.5 0.5
\put {$A=1/2$} at 2.5 0.25
\put {$A=1/3$} at 3.5 0.1666
\endpicture}}
%\endtexonly
%\figrdef{fig:integral test for one over n}
%\htmlfigure{Sequences_series-integral_test_lower_bound.html}
\caption{Graph of $y=1/x$ with rectangles.}
\label{fig:integral test for one over n}
\end{figure}

This time the rectangles are above the curve, that is, each rectangle
completely contains the corresponding area under the curve. This means
that
$$s_n = {1\over 1}+{1\over 2}+{1\over 3}+\cdots+{1\over n}
> \int_1^{n+1} {1\over x}\,dx = \ln x\Big|_1^{n+1}=\ln(n+1).$$
As $n$ gets bigger, $\ln(n+1)$ goes to infinity, so the sequence of
partial sums $\ds s_n$ must also go to infinity, so the harmonic series
diverges.

The key fact in this example is that
\[\ds\lim_{n\to\infty}\int_1^{n+1}\frac{1}{x}\,dx=\int_1^{\infty}\frac{1}{x}\,dx=\infty\]
So these two examples taken together indicate that we can prove that a
series converges or prove that it diverges with a single calculation
of an improper integral. This is known as the \dfont{integral test},
which we state as a theorem.

\begin{theorem}{Integral Test}{IntegralTestTheorem}
Suppose that $f(x)>0$ and is decreasing on the infinite interval
$[1,\infty)$ and that $\ds a_n=f(n)$. Then the series
$\ds\sum_{n=1}^\infty a_n$ converges if and only if the improper
integral $\ds\int_{1}^\infty f(x)\,dx$ converges.
\end{theorem}

The two examples we have seen are called
\dfont{$p$-series}; a $p$-series is
any series of the form $\ds \sum 1/n^p$. If $p\le0$, $\ds\lim_{n\to\infty}
1/n^p\not=0$, so the series diverges. For positive values of $p$ we
can determine precisely which series converge.

\begin{theorem}{$p$-Series Convergence}{pSeriesConvTheorem}
A $p$-series with $p>0$ converges if and only if $p>1$.
\end{theorem}
\begin{proof}
We use the integral test; we have already done $p=1$, so assume that
$p\not=1$.
$$
  \int_1^{\infty} {1\over x^p}\,dx=\lim_{D\to\infty} \left.{x^{1-p}\over
  1-p}\right|_{1}^D=\lim_{D\to\infty} {D^{1-p}\over 1-p}-{1\over 1-p}.
$$
If $p>1$ then $1-p<0$ and $\ds\lim_{D\to\infty}D^{1-p}=0$, so the
  integral converges. If $0<p<1$ then $1-p>0$ and
$\ds\lim_{D\to\infty}D^{1-p}=\infty$, so the integral diverges.
\end{proof}

\begin{example}{$p$-Series}{pSeriesOne}
Show that $\ds\sum_{n=1}^\infty {1\over {n^3}}$ converges.
\end{example}
\begin{solution}
We could of course use
the integral test, but now that we have the theorem we may simply note
that this is a $p$-series with $p>1$.
\end{solution}

\begin{example}{$p$-Series}{pSeriesTwo}
Show that $\ds\sum_{n=1}^\infty {5\over n^4}$ converges.
\end{example}
\begin{solution}
We know that if
$\ds \sum_{n=1}^\infty 1/n^4$ converges then $\ds \sum_{n=1}^\infty 5/n^4$
also converges, by Theorem~\ref{thm:SeriesLinear}. Since
$\ds \sum_{n=1}^\infty 1/n^4$ is a convergent $p$-series,
 $\ds \sum_{n=1}^\infty 5/n^4$ converges also.
\end{solution}

\begin{example}{$p$-Series}{pSeriesThree}
Show that $\ds\sum_{n=1}^\infty {5\over \sqrt{n}}$ diverges.
\end{example}
\begin{solution}
This also follows from Theorem~\ref{thm:SeriesLinear}: Since $\ds\sum_{n=1}^\infty
{1\over \sqrt{n}}$ is a $p$-series with $p=1/2<1$, it diverges, and so
does $\ds\sum_{n=1}^\infty {5\over \sqrt{n}}$.
\end{solution}

Since it is typically difficult to compute the value of a series
exactly, a good approximation is frequently required. In a real sense,
a good approximation is only as good as we know it is, that is, while
an approximation may in fact be good, it is only valuable in practice
if we can guarantee its accuracy to some degree. This guarantee is
usually easy to come by for series with decreasing positive terms.

\begin{example}{}{}
Approximate $\ds \sum 1/n^2$ to within 0.01.
\end{example}
\begin{solution}
Referring to Figure~\ref{fig:integral test for one over n squared},
if we approximate the sum by $\ds \sum_{n=1}^N 1/n^2$, the size of the error we make is the
total area of the remaining rectangles, all of which lie under the
curve $\ds 1/x^2$ from $x=N$ to infinity. So we know the true value of
the series is larger than the approximation, and no bigger than the
approximation plus the area under the curve from $N$ to
infinity. Roughly, then, we need to find $N$ so that
$$\int_N^\infty {1\over x^2}\,dx < 1/100.$$
We can compute the integral:
$$\int_N^\infty {1\over x^2}\,dx = {1\over N},$$
so if we choose $N=100$ the error will be less than 0.01.  Adding up the first 100 terms
gives approximately $1.634983900$. In fact, we can do a bit better. Since we know that
the correct value is between our approximation and our approximation plus the error
(not minus), we can cut our error bound in half by taking the value midway between
these two values. If we take $N=50$, we get a sum of 1.6251327 with an error of at most 0.02,
so the correct value is between 1.6251327 and 1.6451327, and therefore the value
halfway between these, 1.6351327, is within 0.01 of the correct value. We have mentioned
that the true value of this series can be shown to be $\pi^2/6\approx 1.644934068$ which
is 0.0098 more than our approximation, and so (just barely) within the required error.
Frequently approximations will be even better than the
``guaranteed'' accuracy, but not always, as this example demonstrates.
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:IntegralTest}}

\begin{enumialphparenastyle}

Determine whether each series converges or diverges.

\begin{multicols}{2}
\begin{ex}
$\ds\sum_{n=1}^\infty {1\over n^{\pi/4}}$
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {n\over n^2+1}$
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {\ln n\over n^2}$
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {1\over n^2+1}$
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {1\over e^n}$
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=1}^\infty {n\over e^n}$
\begin{sol}
converges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=2}^\infty {1\over n\ln n}$
\begin{sol}
diverges
\end{sol}
\end{ex}

\begin{ex}
$\ds\sum_{n=2}^\infty {1\over n(\ln n)^2}$
\begin{sol}
converges
\end{sol}
\end{ex}

\end{multicols}

\begin{ex}
Find an $N$ so that
$\ds\sum_{n=1}^\infty {1\over n^4}$ is between
$\ds\sum_{n=1}^N {1\over n^4}$ and
$\ds\sum_{n=1}^N {1\over n^4} + 0.005$.
\begin{sol}
$N=5$
\end{sol}
\end{ex}

\begin{ex}
Find an $N$ so that
$\ds\sum_{n=0}^\infty {1\over e^n}$ is between
$\ds\sum_{n=0}^N {1\over e^n}$ and
$\ds\sum_{n=0}^N {1\over e^n} + 10^{-4}$.
\begin{sol}
$N=10$
\end{sol}
\end{ex}

\begin{ex}
Find an $N$ so that
$\ds\sum_{n=1}^\infty {\ln n\over n^2}$ is between
$\ds\sum_{n=1}^N {\ln n\over n^2}$ and
$\ds\sum_{n=1}^N {\ln n\over n^2} + 0.005$.
\begin{sol}
$N=1687$
\end{sol}
\end{ex}

\begin{ex}
Find an $N$ so that
$\ds\sum_{n=2}^\infty {1\over n(\ln n)^2}$ is between
$\ds\sum_{n=2}^N {1\over n(\ln n)^2}$ and
$\ds\sum_{n=2}^N {1\over n(\ln n)^2} + 0.005$.
\begin{sol}
any integer greater than $\ds e^{200}$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\subsection{Topological distinguishability}

If two points have the same neighbourhoods then they are topologically indistinguishable.

For example in the trivial topology, all points are topologically indistinguishable.


\chapter{Getting started}
\thispagestyle{fancy}
\label{ch:getting-started}

\hrule
\begin{itemize}
\footnotesize
\item[]{Aims:}
\begin{itemize}
\item{to understand differences between commonly used local search methods;}
\item{to be aware of the assumptions in the linear approximation of parameter
uncertainty:}
\item{to experience the limitations of local search methods for complex response
surfaces;}
\item{to be aware of the existence of multiple minima in complex response
surfaces;}
\item{to acknowledge the added value of global search methods;}
\item{to understand the need for efficient search methodologies.}
\end{itemize}
\end{itemize}
\hrule
\vspace{1em}

\section{Slug injection: manual calibration}

A classic method in hydrology for determining the transmissivity and storage
coefficient of an aquifer is called the slug test. A known volume of water
$Q$~[\textsf{m$^3$}] (the slug) is injected into a well, and the resulting
effect on the head $h$~[\textsf{m}] (i.e.~water table elevation) at an
observation well a distance $d$~[\textsf{m}] away from the injection is
monitored at times $t$~[\textsf{hr}]. The measured head typically increases
rapidly and then decreases more slowly. We wish to determine the storage
coefficient $S$~[\textsf{m$^3\cdot{}$m$^{-3}$}] (a measure of the ability of the
aquifer to store water) and the transmissivity
$T$~[\textsf{m$^2\cdot{}$hr$^{-1}$}] (a measure of the ability of the aquifer to
conduct water). The mathematical model for the slug test is:
\begin{equation}
\label{eq:slug-inj}
h=\frac{Q}{4\cdot{}\pi\cdot{}T\cdot{}t}e^{-d^2\cdot{}S/(4\cdot{}T\cdot{}t)}
\end{equation}

\smallq{Use the MATLAB editor to open `mancal\_sluginj.m' from
`./exercises/manual-calibration-slug-injection/'. Run the script by pressing F5.
A Graphical User Interface will appear. Use this interface to calibrate
parameters $S$ and $T$ of Equation~\ref{eq:slug-inj}. Assume
$Q$~=~50~[\textsf{m$^3$}] and $d$~=~60~[\textsf{m}].}

\smallq{How do $S$ and $T$ affect the simulated pressure head?}

Since this is a 2-parameter problem, we can easily visualize the objective
function. Run `respsurf.m'. The objective function is the sum of squared
residuals (SSR).

\smallq{Were you able to identify the ``optimal'' model parameters using manual
calibration?}

\smallq{Looking at the response surface (Figure~\ref{fig:respsurf-sluginj}), do
you think that the optimal model parameters are correlated?}
\begin{figure}[htbp]
  \centering
    \includegraphics[width=1.0\textwidth]{./eps/converted/respsurf-sluginj}
  \caption{Response surface for the slug injection test. Dotted lines represent
the 95\% confidence interval of the model parameters when the standard deviation
of the head measurement is 0.01~[\textsf{m}].}
  \label{fig:respsurf-sluginj}
\end{figure}

\smallq{The dotted lines in Figure~\ref{fig:respsurf-sluginj} indicate the 95\%
confidence intervals of the model parameters when the standard deviation of the
head measurement is 0.01~[\textsf{m}]. Do you think this is a reasonable
approximation for this nonlinear problem?}

\section{Optimization using local methods}

\subsection{Gauss-Newton and Levenberg-Marquardt}

\smallq{Two algorithms---Gauss-Newton (GN) and Levenberg-Marquardt (LM)--have
been implemented to automatically find the optimal parameters for the slug test.
To run these algorithms for the slug test problem, set your work directory to
`./exercises/local-methods-sluginj'. This directory contains a function that
performs a Gauss-Newton as well as a Levenberg-Marquardt optimization. This
function requires an input argument containing a starting point
(e.g.~\mcode{[0.15,0.4]}). For each method, the function returns an array with a
record of parameter sets and their objective score. You can call the function
from the command line using:

\mcode{[P_GN,P_LM]=Run_Optimization([0.15,0.4]);}
}

\smallq{Have a look at the code and interpret the output.}

\smallq{How quickly does the GN method converge?}

\smallq{How does this compare to the LM method?}

\smallq{Play around with different starting points for the two algorithms. Does
the GN method always converge to the optimum? What about the convergence of the
LM method?}

\smallq{Find a starting point where the GN and LM methods differ and study the
iterations in detail (\mcode{P_GN} and \mcode{P_LM}).}

\smallq{What is the key difference between the iterations of the GN and LM
method?}

\subsection{Multi-start simplex}

HYMOD \citep{boyl-gupt-soro2000} is a simple rainfall-runoff model with 5 model
parameters (see Figure~\ref{fig:hymod}).

\begin{figure}[htbp]
  \centering
    \includegraphics[width=1.0\textwidth]{./eps/converted/hymod}
  \caption{Structure of the HYMOD rainfall-runoff model.}
  \label{fig:hymod}
\end{figure}
\begin{tabular}{ll}
with:&\\
$C_{max}$&Maximum storage of the watershed\\
$b_{exp}$&Spatial variability of soil moisture capacity\\
$Alpha$&Partitioning factor of excess rainfall into slow and quick flow\\
$R_{s}$&Residence time of the slow tank\\
$R_{q}$&Residence time of the quick tanks\\
\end{tabular}

In this section, we will use a multi-start simplex \citep{neld-mead1965} to
optimize the parameters of the HYMOD model. Specifically, we investigate whether
the objective function (SSR) of this model has local minima that may complicate
the use of local optimization methods such as Gauss-Newton, or
Levenberg-Marquardt. To do this, 20 simplex runs are started from randomly
determined starting points, which are iterated until convergence is achieved.

\smallq{First, have a look at the code of `Multistart\_Simplex.m' in
`./exercises/local-methods-hymod/'. As you can see, the code uses the built-in
MATLAB function \mcode{fminsearch}, which is an implementation of the simplex
search algorithm. Run \mcode{Multistart\_Simplex} (it'll run for a few minutes).
Store the output by copy-pasting screenshots into a PowerPoint presentation.}

\smallq{Do all the Simplex runs converge to the same point in the parameter
space?}

\smallq{What does this tell you about the presence of local minima?}

\smallq{For the slug injection test, we visualized the response surface. Can we
make a similar figure here?}

\smallq{When you look at the results of the simulations with optimized
parameters, why do you see just 3 or 4 time series and not 20?}

\smallq{How do you value the results of the various models?}

\smallqo{In the previous exercise, a relatively short data set (3 months) was
used to calibrate HYMOD. Extend the calibration period to 12 months by changing
the values of \mcode{Extra.calPeriod} in  \mbox{`Multistart\_Simplex.m'} and run
the optimization again.}

\smallqo{For this new subset of data, why did the number of local minima
increase or decrease?}

\smallqo{What is the quality of the model runs corresponding to the local
minima?}

\smallqo{If we discard the poor models, can you give an interpretation why these
local minima occurred?}

\section{Global methods: Differential Evolution}

Differential Evolution~\citep{stor-pric1997} is a basic global optimization
algorithm that uses a population of points in the parameter space to find the
global maximum (minimum, if appropriate) of an objective function. Generally
speaking, the population's properties are used to generate more points in those
parts of the parameter space which are most promising with regard to yielding
the global optimum.

\smallq{Use the MATLAB editor to open
`./exercises/differential-evolution/respsurf.m'. This script contains a few
lines of code to help you get started on your algorithm. Run the script to
visualize the response surface for the benchmark function `6' (Shifted
Rosenbrock's  Function) for \mcode{x = [65:0.1:80]} and \mcode{y = [35:0.1:45]}
(Figure~\ref{fig:respsurf-rosenbrock}).}
\begin{figure}[htbp]
  \centering
    \includegraphics[width=1.0\textwidth]{./eps/converted/respsurf-rosenbrock}
  \caption{Response surface for the shifted Rosenbrock function.}
  \label{fig:respsurf-rosenbrock}
\end{figure}

The \mcode{benchmark_func} function offers not just 1, but 25 functions that can
be visualized in the same way as you already did for the Rosenbrock function.
You can choose different functions by changing the \mcode{funcFlag}. Each
function has its own limits on the parameter space (see
Table~\ref{tab:benchmark-func-data}).

\begin{table}[t]
\centering
\scriptsize
\begin{tabular}{lp{9cm}l}
\mcode{funcFlag}&\textbf{description}&\textbf{limits}\\
\multicolumn{3}{l}{Unimodal Functions (5):}\\
1  &Shifted Sphere Function                                                              & [-100,100]\\
2  &Shifted Schwefel's Problem 1.2                                                       & [-100,100]\\
3  &Shifted Rotated High Conditioned Elliptic Function                                   & [-100,100]\\
4  &Shifted Schwefel's Problem 1.2 with Noise in Fitness                                 & [-100,100]\\
5  &Schwefel's  Problem 2.6 with Global Optimum on Bounds                                & [-100,100]\\
\multicolumn{3}{l}{Multimodal Functions (20):}\\
\multicolumn{3}{l}{Basic Functions (7):}\\
6  &Shifted Rosenbrock's Function                                                        & [-100,100]\\
7  &Shifted Rotated Griewank's Function without Bounds                                   & [0,600]\\
8  &Shifted Rotated Ackley's Function with Global Optimum on Bounds                      & [-32,32]\\
9  &Shifted Rastrigin's Function                                                         & [-5,5]\\
10  &Shifted Rotated Rastrigin's  Function                               & [-5,5]\\
11  &Shifted Rotated Weierstrass Function                                & [-0.5,0.5]\\
12  &Schwefel's  Problem 2.13                                            & [-100,100]\\
\multicolumn{3}{l}{Expanded Functions (2):}\\
13  &Expanded Extended Griewank's  plus Rosenbrock's  Function (F8F2)                    & [-3,1]\\
14  &Expanded Rotated Extended Scaffe's  (F6)                                & [-100,100]\\
\multicolumn{3}{l}{Hybrid Composition Functions (11):}\\
15  &Hybrid Composition Function 1                                       & [-5,5]\\
16  &Rotated Hybrid Composition Function 1                               & [-5,5]\\
17  &Rotated Hybrid Composition Function 1 with Noise in Fitness                     & [-5,5]\\
18  &Rotated Hybrid Composition Function 2                               & [-5,5]\\
19  &Rotated Hybrid Composition Function 2 with a Narrow Basin for the Global Optimum    & [-5,5]\\
20  &Rotated Hybrid Composition Function 2 with the Global Optimum on the Bounds         & [-5,5]\\
21  &Rotated Hybrid Composition Function 3                           & [-5,5]\\
22  &Rotated Hybrid Composition Function 3 with High Condition Number Matrix         & [-5,5]\\
23  &Non-Continuous Rotated Hybrid Composition Function 3                        & [-5,5]\\
24  &Rotated Hybrid Composition Function 4                               & [-5,5]\\
25  &Rotated Hybrid Composition Function 4 without Bounds                            & [-2,5]\\
\end{tabular}
\caption{}
\label{tab:benchmark-func-data}
\end{table}

\smallq{Experiment with different functions to get some idea of what their
response surfaces look like.}

%\hspace*{0.01mm}
%\vfill
%\hspace*{0.01mm}

\needspace{8\baselineskip}
In the next few exercises, you will write your differential evolution algorithm.
We will take you through the following basic steps (see also
\textsf{Fig.~\ref{fig:diff-evo-principle}} and \textsf{Code
Snippets~\ref{list:generating-uniform-random-sample}} and
\textsf{\ref{list:generating-proposals}}):
\begin{enumerate}
\item{generate the initial sample;}
\item{assign the initial sample to a new array \mcode{parents};}
\item{for each sample in \mcode{parents}, calculate the corresponding objective
score;}
\item{use \mcode{parents} to calculate new \mcode{proposals};}
\item{for each sample in \mcode{proposals}, calculate the corresponding objective
score;}
\item{accept either a sample from \mcode{proposals} or the corresponding sample
from \mcode{parents} as the child, thus making an array \mcode{children} of the
same size as \mcode{parents};}
\item{assign \mcode{children} to \mcode{parents} for the next generation, and go
back to step 4.}
\end{enumerate}

\begin{figure}[htbp]
  \centering
    \includegraphics[width=1.0\textwidth]{./eps/converted/diff-evo-principle}
  \caption{Differential evolution in a nutshell, using a population of
  \texttt{nPop = 10} to avoid cluttering the figures. \textbf{A}:~Generation of
  the initial sample. \textbf{B}:~Generation of proposal points.
  \textbf{C}:~Accepting either a sample from \texttt{parents} or from
  \texttt{proposals} as a row in \texttt{children}. \textbf{D}:~The
  \texttt{children} from one generation are the \texttt{parents} for the
  next generation.}
  \label{fig:diff-evo-principle}
\end{figure}

\lstinputlisting[float=ht,caption={Generating samples drawn from a uniform
distribution spanning the parameter
space.},label=list:generating-uniform-random-sample]{./m/snippet-diff-evo-generating-uniform-random-sample.m}

\lstinputlisting[float=ht,caption={Generating proposal points given a parent
population.},label=list:generating-proposals]{./m/snippet-diff-evo-generating-proposal-points.m}

\smallq{As a first step, save your \mcode{respsurf} script under a new name
`runDiffEvo.m'.}

\smallq{Extend `runDiffEvo.m' by creating an array \mcode{parents}, which has
\mcode{nPop=50} rows, each of which represents a uniform random sample from the
parameter space (see also \textsf{Code
Snippet~\ref{list:generating-uniform-random-sample}}). \mcode{parents} has
\mcode{nDims+1} columns, i.e. the number of dimensions for which you want to
solve \mcode{benchmark_func} plus one column to store the objective score. Refer
to Table~\ref{tab:benchmark-func-data} for the parameter space limits. For now,
stick with \mcode{nDims=2} and \mcode{funcFlag=6}, but you can change that
later.}

\smallq{Calculate the last column of \mcode{parents} by running the objective
function for each row.}

\smallq{Generate the \mcode{proposals} array (same size as \mcode{parents}). To
do this, select the first sample from \mcode{parents}, as well as three other
samples (\mcode{r1}, \mcode{r2}, \mcode{r3}), chosen at random from
\mcode{parents} (MATLAB's built-in function \mcode{randperm} can be useful for
this; see \textsf{Code Snippet~\ref{list:generating-proposals}}). The proposal
is calculated as the position of the parent + F*\mcode{dist1} + K*\mcode{dist2},
in which  \mcode{dist1} is the distance between the parent and \mcode{r1},
\mcode{dist2} is the distance between \mcode{r2} and \mcode{r3}, and F and K are
settings that are part of the Differential Evolution algorithm. In the
literature, F=0.6 and K=0.4 are common. Repeat for all samples in
\mcode{parents}.}

\smallq{For each member in \mcode{proposals}, calculate the objective score.}

\smallq{Determine whether to choose the $i^{th}$ sample of \mcode{parents} or
the $i^{th}$ sample of \mcode{proposals}, depending on which one has a better
objective score. Assign to the $i^{th}$ row in a new array \mcode{children}.}

\smallq{Assign \mcode{children} to \mcode{parents} for the next generation.}

\smallq{Wrap most of the above steps in a \mcode{for} loop, such that your
script will repeatedly generate new (and hopefully better) children for 250
generations.}

\smallq{It's a little bit difficult to see if your script is doing what it is
supposed to be doing, so you need to do some visualization. Copy and paste from
`vis-helper.txt' to create a figure similar to Figure~\ref{fig:diffevo-result}.}

\begin{figure}[htbp]
  \centering
    \includegraphics[width=1.0\textwidth]{./eps/converted/diffevo-result}
  \caption{Visualization results for the Shifted Rosenbrock function after 250
  generations of 50 samples each, optimized using the Differential Evolution
  algorithm.}
  \label{fig:diffevo-result}
\end{figure}

\smallq{During the optimization, you may notice that there are some points that
are really persistent, even though they are well away from the global optimum.
Explain why these points are so persistent.}

\smallq{The histogram in Figure~\ref{fig:diffevo-result} is bimodal. How does
that relate to the answer to the previous question?}

\section{Differential Evolution with Metropolis}

Over the next few exercises, you will add Metropolis acceptance to your
Differential Evolution optimization, such that it can be used to quantify parameter
uncertainty.

\smallqo{Clean up `runDiffEvo.m' and save it as `runDiffEvoMetro.m'. You will be
making your edits in the latter script.}

The benchmark functions that we have been using so far do not represent
probability density functions, so they are not suited to demonstrate the
Metropolis scheme. So instead we'll use a benchmark function of our own:
`doublegauss.m'. This function takes one argument, \texttt{x}, and calculates
the probability density at that value of \texttt{x} by adding two Gaussian
probability density distributions:

\begin{align}
\label{eq:double-normal}
p = \frac{1}{2}\cdot{}\frac{1}{\sqrt{2\pi\sigma_1^2}}\:e^\mathlarger{-\frac{1}{2}\left(\frac{x-\mu_1}{\sigma_1}\right)^2} & +\dots \\ \nonumber
    \frac{1}{2}\cdot{}\frac{1}{\sqrt{2\pi\sigma_2^2}}\:e^\mathlarger{-\frac{1}{2}\left(\frac{x-\mu_2}{\sigma_2}\right)^2}
\end{align}

with $\mu_1 = -10$, $\sigma_1 = 3$, $\mu_2 = 5$, $\sigma_2 = 1$, respectively.
$x$ is the only parameter that is optimized; as an example, for $x=4.5$, $p =
0.1760$ (See Fig.~\ref{fig:diffevo-metro-result}C).

\smallqo{Replace any calls to \texttt{benchmark\_func} with calls to
\texttt{doublegauss}. You don't need to supply any other arguments besides
\texttt{parVec}. Set the population size to 200 and configure the algorithm to
run for 100 generations. Constrain the parameter space to $[-20,20]$ and make
sure to adjust the number of dimensions.}

Scroll down to where `runDiffEvoMetro.m' accepts either a member from
\texttt{parents} or a member from \texttt{proposals} as a member of
\texttt{children}. Currently, the proposal will only be accepted when it is
better than the parent. In contrast, the Metropolis acceptance scheme is
crucially different in that it will \textit{sometimes} accept a proposal parameter
combination that is actually \textit{worse} than the parent.

\needspace{15\baselineskip}
You will use the following rules for Metropolis acceptance:
\begin{itemize}
    \item{if the proposal is better than the parent, accept the proposal as the
    child;}
    \item{if the proposal is worse than the parent, calculate \texttt{alfa} as the
    proposal/parent ratio. Next, draw a random number \texttt{Z}
    from the [0,1] interval using a uniform distribution.}
    \begin{itemize}
        \item{if \texttt{alfa} is greater than \texttt{Z}, accept the proposal;}
        \item{otherwise, re-accept the parent.}
    \end{itemize}
\end{itemize}

The nice property of Metropolis acceptance is that the frequency with
which points get accepted is proportional to their probability (provided that
you have sampled long enough for the parameter distribution to become stable).
We will use this later to calculate the confidence interval associated with the
prediction.

\smallqo{Adapt the acceptance scheme in `runDiffEvoMetro.m' in accordance with
the Metropolis rules and run the script.}

\smallqo{Adapt your script in such a way that it stores the entire array
\texttt{parents} for each generation in a new array \texttt{evalResults}. Note
that \texttt{evalResults} has \texttt{nPop*nGenerations} rows and
\texttt{nDims+1} columns and that it can be preallocated with \mcode{NaN} values
using MATLAB's built-in \mcode{repmat} function.}

\smallqo{Visualize the information from \texttt{evalResults} in a similar
fashion as in Fig.~\ref{fig:diffevo-metro-result}.}

\begin{figure}[htbp]
  \centering
    \includegraphics[width=1.0\textwidth]{./eps/converted/diffevo-metro-result}
    \caption{\textbf{A}:~20000 samples of the parameter space, as taken by the
    Differential Evolution Metropolis algorithm. \textbf{B}:~A histogram of all
    samples. The histogram serves as an approximation of the true distribution
    from Fig.~\ref{fig:diffevo-metro-result}C. \textbf{C}:~The true
    distribution. \textbf{D}:~The objective scores associated with each sample.}
  \label{fig:diffevo-metro-result}
\end{figure}


% file: equal.tex

\section{DH Problem 5.9: $\equal(S_1, S_2)$~\cite{DH}}  \label{section:problem-5.9}
  Construct a function $\equal(S_1, S_2)$ that tests whether the strings $X$ and $Y$ are equal.
  It should return true or false accordingly.

  You may use the following operations:
  \begin{itemize}
    \item $\texttt{head}(X)$
    \item $\texttt{tail}(X)$
    \item $\texttt{last}(X)$
    \item $\texttt{all-but-last}(X)$
    \item $\texttt{eq}(s,t)$
  \end{itemize}
%%%%%%%%%%%%%%%%%%%%
\subsection{Solution}

The algorithm $\equal(S_1, S_2)$ is shown in Algorithm~\ref{alg:equal},
with appropriate assertions attached.

The partial correctness of $\equal$ can be denoted as
\marginnote{We are using the notations of Hoare logic developed by Tony Hoare.}

\begin{marginfigure}%
  \includegraphics[width=0.60\linewidth]{figs/tony-hoare}
  \label{fig:hoare}
\end{marginfigure}

\marginnote{\cite{Hoare:CACM69}}

\[
  P \;\set{\equal}\; Q,
\]
meaning that if the input satisfies the precondition $P$
and the algorithm $\equal$ eventually terminates,
then the postcondition $Q$ must hold.
To this end, we show that:

\input{algs/equal}

\begin{enumerate}[(i)]
  \item $I$ is a loop invariant. That is,
    \[
      P \;\set{\texttt{Lines~\ref{line:init-begin}--\ref{line:init-end}}}\; I
    \]
    \[
      \big(I \land (X \neq \epsilon \land Y \neq \epsilon \land E = \top)\big) \;\set{\texttt{Lines~\ref{line:while-begin}--\ref{line:while-end}}}\; I
    \]
  \item $(2)$ is an invariant. It suffices to show that $(2)$ can be derived from
    \marginnote{This is a rather formal way. Be patient.}
    \renewcommand{\theequation}{\alph{equation}}
    \setcounter{equation}{0}
    \begin{gather}
      I \land \lnot (X \neq \epsilon \land Y \neq \epsilon \land E = \top) \nonumber\\
      = I \land (X = \epsilon \lor Y = \epsilon \lor E = \bot) \nonumber\\
      = \big(I \land (X = \epsilon)\big) \lor \big(I \land (Y = \epsilon)\big) \lor \big(I \land (E = \bot)\big) \label{eq:1st}
    \end{gather}
    in propositional logic by showing that each disjunct of (\ref{eq:1st}) implies $(2)$.

    Consider the first disjunct
    \renewcommand{\theequation}{\alph{equation}1}
    \setcounter{equation}{0}
    \begin{gather}
      I \land (X = \epsilon) \nonumber\\
      = (S_1 = S_2 \iff X = Y \land E = \top) \land (X = \epsilon)  \label{eq:1st-1st}
    \end{gather}
    It is required to prove both
    \[
      \text{(\ref{eq:1st-1st})} \implies \Big(S_1 = S_2 \implies \big((X = \epsilon \land Y = \epsilon) \land E = \top \big)\Big)
    \]
    and
    \[
      \text{(\ref{eq:1st-1st})} \implies \Big(\big((X = \epsilon \land Y = \epsilon) \land E = \top \big) \implies S_1 = S_2 \Big)
    \]
    \marginnote{Do you still remember how to prove an implication? What is the given? And what is the conclusion?
    Furthermore, when you derive $(2)$ from the third disjunct,
    keep in mind that \emph{falsity implies everything}.}
    Now it is your (YES, it is YOU) job! It is also your job to deal with the second and the third disjuncts in a similar way.

    % \renewcommand{\theequation}{\alph{equation}3}
    % \setcounter{equation}{0}
    % \begin{gather}
    %   I \land (E = \bot) \nonumber\\
    %   = (S_1 = S_2 \iff X = Y \land E = \top) \land (E = \bot)  \label{eq:1st-3rd}
    % \end{gather}
  \item (3.1) is an invariant. It suffices to show that
    \[
      \big((2) \land \lnot (X = \epsilon \land Y = \epsilon) \land E = \bot\big) \implies (3.1).
    \]
    Stop reading! Take your pencil and paper!
    \marginnote{\emph{Writing is nature’s way of letting you know how sloppy your thinking is.} \\ \hfill --- Richard Guindon (cartoon), 1989}
    Write down your arguments to convince yourself that the formula above indeed holds.
  \item (3.2) is an invariant. It suffices to show that
    \[
      \big((2) \land (X = \epsilon \land Y = \epsilon)\big) \implies (3.2).
    \]
    You already know how to prove this formula in propositional logic. Don't you?
    We now show you a more intuitive way to reason about programs:

    \marginnote{It proceeds \emph{semantically} rather than \emph{syntactically} as we have done before.}
    \emph{At point (2), we know that $S_1 = S_2$ if and only if $(X = \Lambda \land Y = \Lambda \land E = \top)$.
    At point (3.2), we further know that $(X = \Lambda \land Y = \Lambda)$.
    Thus, at this point, we only need to check whether $E = \top$ to decide whether $S_1 = S_2$ holds.}
  \item $Q$ is an invariant. It suffices to show that
    \[
      \big((3.1) \implies Q\big) \land \big((3.2) \implies Q\big).
    \]
    It should be easy now for you to prove this formula.
\end{enumerate}


\problemname{Flight Plan Evaluation}

%\illustration{0.42}{sample1}{The second sample input}
When flying between two places, constructing a good flight plan is
important.  In general there is a wide range of different factors to
consider, the most important being fuel consumption and weather
forecasts (especially winds).  In this problem, we will evaluate
flight plans with respect to a third statistic, namely how much of the
flight is over water, and how much is over ground.  This statistic is
not relevant per se, yet many passengers seem to prefer flying over
land -- either because they are afraid of flying over water, or simply
because the view tends to be slightly more interesting when flying
over land.

For this problem, we assume that the earth is a perfect sphere with
radius $6370$ km.  We model each continent of the earth as a polygon
on this sphere -- a closed sequence of line segments, where a line
segment between two points consists of the shortest spherical arc
between these two points.  The two end-points of a line segment can
not be the same point, or antipodal (diametrically opposite) points.
Similarly a flight route is modeled as a sequence of waypoints
connected by line segments, but unlike the line segments of a polygon
these line segments may cross themselves and will not necessarily end
up where they started.

\begin{figure}[!h]
  \centering
  \includegraphics[width=0.55\textwidth]{sample1}
  \caption{The second sample input}
\end{figure}

In order to simplify the problem, we additionally make the following
two assumptions:
\begin{itemize}
\item No waypoint of a flight route lies within $0.1$ km of any shoreline (a line segment that is part of a polygon).
\item No vertex of any continent polygon lies within $0.1$ km of the flight route.
\end{itemize}

All coordinates on the sphere are represented as a pair of latitude and
longitude (both in degrees).  A point with latitude $\pm 90$ is the
north/south pole, and points with latitude $0$ are the points on the
equator.

\section*{Input}

The input consists of:

\begin{itemize}
\item one line with an integer $1 \le c \le 30$, the number of continents;
\item $c$ lines, each describing a continent. Each such line starts
  with an integer $3 \le n \le 30$, the number of vertices in the
  polygon describing the continent.  This is followed by $n$ pairs of
  integers $\phi_1, \lambda_1, \ldots, \phi_n, \lambda_n$, where $-90
  \le \phi_i \le 90$ and $0 \le \lambda_i \le 359$ are the latitude and
  longitude of the $i$th vertex of the continent;
\item one line describing the flight plan.  The line starts with an
  integer $2 \le m \le 30$, the number of waypoints.  This is followed
  by $m$ pairs of integers $\phi_1, \lambda_1, \ldots, \phi_m,
  \lambda_m$, where $-90 \le \phi_i \le 90$ and $0 \le \lambda_i \le
  359$ are the latitude and longitude of the $i$th waypoint of the
  route.
\end{itemize}

A continent cannot cross itself.  No continent will touch or contain
any other continent.  Continents are given in counterclockwise order,
in the sense that
if you go from the first vertex of the polygon to
the second one, the interior of the continent is on your left hand
side.
%if the first point of a continent is $P$, and the
%second one is $Q$, then the interior of the continent is on the left
%side when going from $P$ to $Q$.

The first and last waypoints of the route will always be inside a
continent (but not necessarily the same continent).

\section*{Output}

Output two real numbers $l$ and $w$, where $l$ is
the total length of the flight (in km), and $w$ is the percentage of
the flight that is over water.  The numbers should be accurate to an
absolute or relative error of at most $10^{-6}$.


\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\usepackage{subcaption}
\begin{document}

	\title{Introduction to Circuitry}
	\author{}

	\maketitle

	\begin{abstract}
	In this article we will take a look at the \textit{rms} value of an AC signal and when it's used.
	Subsequently we'll start writing software code that represent electrical signals in a software program.

	\end{abstract}

	\section{Root mean square}
	Root mean square for a set of discrete numbers, which is commonly called by it's abbreviation \textbf{rms} is defined as:

	$$ X_{rms} = \sqrt{\frac{1}{n}(x_1^2 + x_2^2 + x_3^2 + ... + x_n^2 )} $$

	\textbf{ex.} Calculate the \textit{rms} of numbers $4$, $5$, $2$ and $7$.

	\textbf{ans.} $$ X_{rms} = \sqrt{\frac{1}{4}(4^2 + 5^2 + 2^2 + 7^2)} \approx 4.847 $$

	We've seen the definition of \textit{rms} for the cases that we have some discrete numbers.
	If we had a continuous function instead, we'd use the following definition:

	$$ f_{rms} = \sqrt{\frac{1}{T_2 - T_1} \int_{T_1}^{T_2} [f(dx)]^2 dx } $$

	\textbf{ex.} Calculate the \textit{rms} of function $f(x) = x^3$ from $x = 1$ to $x = 4$.

	\textbf{ans.} Plugging it into formula:

	$$ f_{rms} = \sqrt{\frac{1}{1 - 4} \int_{1}^{4} [x^3]^2 dx } \approx 27.9 $$

	\subsection{When do we use rms?}
	When the source of electrical power in not a fixed DC value and it's alternating over time, how would we calculate the \textit{average power} consumed by a resistive element? From the formula of power we have:

	$$P(t) = I(t).V(t)$$

	We also know from Ohm's law that $I(t) = \frac{V(t)}{R}$, therefore substituting it into the formula we get:

	$$P(t) = \frac{V(t)^2}{R}$$

	This formula gives the power at each point in time.
	Since $V(t)$ is alternating, the power varies over time as well and doesn't have a fixed value.
	So to calculate the \textit{average} power we should use integral for this.

	$$P_{avg} = \frac{1}{R}.\frac{\int_{T_0}^{T_1} V(t)^2 dt }{T_1 - T_0}$$

	This is all we need for calculating the average power.
	However if we take the integral term (with it's denominator $T_1 - T_0$) and take it's square root, it turns into the \textit{rms} value we defined earlier.

	$$V_{rms} = \sqrt{\frac{\int_{T_0}^{T_1} V(t)^2 dt }{T_1 - T_0}} $$

	Now by having this value, to calculate the average power we should reverse the effect of taking the square root by raise it to power of $2$ (and division by resistivity $R$):

	$$ P_{avg} = \frac{V^2_{rms}}{R} $$

	Why do we take the square root of an expression just to raise it to the power of $2$ later?
	Because if we somehow know the $V_{rms}$ of an AC signal, we would use the \textit{same} formula for calculating average power as if we had a fixed DC voltage over time ($P_{avg} = \frac{V^2_{dc}}{R}$); Namely raising it to power of $2$ then dividing it by $R$.
	So \textit{rms} value of a voltage or current is a convenient way to describe an AC signal besides its peak value and frequency that we would normally do.

	When we're speaking about AC signals, we are most likely dealing with \textit{Sinusoidal} AC signals.
	So let's investigate and see if the \textit{rms} of a general sinusoidal AC signal yields anything constant.
	If so, then we'd omit the whole integration process every time we want to calculate the \textit{rms} of a sinusoidal signal and simply multiply that constant by some term.

	General sinusoidal function has the following form in one cycle:

	$$ V(t) = V_{Max} \sin(\omega t) $$

	Plugging it into the definition of \textit{rms},

	$$V_{rms} = \sqrt{\frac{\int_{T_0}^{T_1} (V_{Max} \sin \omega t) ^ 2 dt} {T_1 - T_0}}$$

	$V_{Max}$ is just a constant and goes out of the square root. So we get:

	$$V_{rms} = V_{Max} \sqrt{\frac{\int_{T_0}^{T_1} (\sin \omega t) ^ 2 dt }{T_1 - T_0}}$$

	We also know from basic properties of trigonometric functions:

	$$ \sin^2 x = \frac{1 + \cos 2x}{2} $$

	Substituting the sine term with this property:

	\begin{align}
	V_{rms} = V_{Max} \sqrt{\frac{1}{{T_1 - T_0}}{\int_{T_0}^{T_1} \frac{1 + \sin \omega t dt}{2}}} = \\
	 V_{Max} \sqrt{\frac{1}{T_1 - T_0} \left[ \frac{t}{2} - \frac{\sin 2 \omega t}{4 \omega}\right]_{T_0}^{T_1} }
	\end{align}

	Now since the range $T_0$ to $T_1$ is one period of the sinusoidal function, the sine integral is just $0$. Therefore:

	\begin{equation}
	\begin{gathered}
	V_{rms} = V_{Max} \sqrt{\frac{1}{T_1 - T_0} \left[ \frac{t}{2} \right]_{T_0}^{T_1}} = \\
	V_{Max}\sqrt{\frac{1}{T_1 - T_0} . \frac{T_1 - T2}{2}} = \\
 V_{Max} \frac{1}{\sqrt{2}}
	\end{gathered}
	\end{equation}

	Which proves that for calculating the \textit{rms} of a sinusoidal signal, we can skip the integration process and simply divide its peak value by $\sqrt{2}$.

	\section{Representing electrical signals in software}
	Before continuing with the more advanced EE concepts, let's implement the ideas we discussed so far in form of a software package.
	We would therefore use these implementations readily in the future articles and projects as well.
	We use the C++ language to write these constructs since it is widely used in microcontroller and system programming; And also supports advanced language features and paradigms.

	One of the fundamental concepts that we would want to implement, is a construct that would represent an \textit{electrical signal} in our programs.
	This idea can be implemented conveniently with OOP\footnote[1]{Object Oriented Programming} paradigm; i.e. if we have a \textit{class} that represents the type of a signal in our program, instantiating it with:

	\begin{verbatim}
		Signal s(<function_body>);
	\end{verbatim}

	Would capture the signal's function, and with the appropriate definition of our class this instance $s$ would readily contain all the frequently used operations such as calculating its $rms$ and etc.
	Hence:

	\begin{verbatim}
	s.getRms()
	\end{verbatim}

	Would return the function's \textit{rms} or:

	\begin{verbatim}
	s.fourierTransform()
	\end{verbatim}

	Would apply Fourier transform on our signal (an important math operation that we will discuss in later articles) and so on.

	Let's start by defining the base class:

	\begin{verbatim}
	#include <cmath>
	#include <functional>
class Signal {
protected:
    using rftype =  std::function<double(double)>;
    rftype function;
public:
    Signal(rftype f): function(f) {}
    double output(double x) { return function(x); }
    rftype getFunction() {return function; }
};
	\end{verbatim}
	This class can be used as:

	\begin{verbatim}
Signal s([](double t) {return t*t;});
std::cout << s.output(3) << '\n';
std::cout << s.output(0.4) << '\n';
	\end{verbatim}
	Running the program we get:

	\begin{verbatim}
9
0.16
	\end{verbatim}

	As we see, the object $s$ can now represents a signal in our program.
	For now, writing $s.output(x)$ gives the signal's output at time $x$ and $s.getFunction()$ returns the signal's function definition if we needed somewhere in our program.

	Few remarks here:

	1. We included the header \textbf{functional} to make the type \textit{std::function} available in our program. With this type we captured the function's body in the constructor.

	2. We defined a type alias for a real-valued math function that have real-valued inputs and outputs ($\Re\rightarrow\Re$). We called this alias \textbf{rftype} for \textit{\textbf{r}eal-valued \textbf{f}unction \textbf{type}.}

	3. We declared the member variable $function$, as a protected member because later on we want to derive more specific types of signal namely DC, AC and Sinusoidal AC from this class.

	Now let's move on to defining derived classes. The first one is for the DC signals which has a straightforward definition.

	\begin{verbatim}
class DcSignal: public Signal {
public:
    DcSignal(double dcValue): Signal([dcValue](double t) -> double {
        return dcValue;
    }){}
};
	\end{verbatim}

	It can be used as:

	\begin{verbatim}
	DcSignal s(4);
	\end{verbatim}

	For AC signal class:
	\begin{verbatim}
class AcSignal: public Signal {
public:
    AcSignal(rftype func, double p): Signal([func,p](double t) -> double {
        if(std::abs(t) <= std::abs(p)) return func(t);
        else return func(t - (int)(t/p) * p);
    }), f(1/p), p(p){}

    double getRms() {
        return std::sqrt(1/p * numerical_integration(function, 0, p));
    }

    double getFrequency() { return f; }
    double getPeriod() { return p; }
protected:
    double f, p;
};
	\end{verbatim}

	This class is also straightforward.
	It's constructor accepts a function and period in which the function would repeat after that point.
	Note that in $getRms$ we uses \textit{numerical\_integration} for estimating the definite integral.
	This function can be implemented in various ways.
	A quick one is to use the trapezoidal method:

	\begin{verbatim}
double numerical_integration(std::function<double(double)> f,
                      double a, double b, double N = 10000) {
    double h = (b - a) / N;
    double tmp = 0;
    tmp += f(a);
    tmp += f(b);
    while(a < b) {
        tmp += 2 * f(a);
        a += h;
    }
    return (h/2)*tmp;
}
	\end{verbatim}
	Example usage of \textit{AcSignal}:

	\begin{verbatim}
AcSignal s([](double t) {return t*t;}, 2);
cout << s.output(0.5) << '\n';
cout << s.output(2.5) << '\n';
	\end{verbatim}

	Produces:

	\begin{verbatim}
0.25
0.25
	\end{verbatim}

	Now let's define a class that we'd likely use the most: Sinusoidal signals.
	Since this class is a type of AC signal and it's periodic, we derive it from the \textit{AcSignal}.

	\begin{verbatim}
class SinusoidalAcSignal: public AcSignal {
public:
    SinusoidalAcSignal(double amplitude, double frequency,
                       double phase = 0):
     AcSignal([amplitude, frequency, phase](double t) -> double {
     return amplitude * std::sin(t*2*3.14159268*frequency + phase);
    }, 1/frequency), a(amplitude) {}

    double getRms() { return a/std::sqrt(2); }

    double getAmplitude() { return a; }
private:
    double a;
};
	\end{verbatim}

	(The definition of these classes are presented in a single file inside this article's directory.)

	We have now defined some boilerplate classes that we'd use in situations such as when we want to generate some sinusoidal signal for an output pin of a microcontroller.
	For another example when we want to simulate an input signal for analyzing circuits with a software.

	\section{What's Next?}
	In the follow up article, we will discuss some concepts behind AC circuit analysis; Namely phasor diagrams and complex number representation.

	These documents are published under an open license (see the project's root directory for more info), and were intended to be part of an open and a collaborative project.
	Feel free to fork this document, send pull request and also give your feedback.
	Thanks for reading!
\end{document}