\chapter{Lecture 29 Jul 11th 2018}%
\label{chp:lecture_29_jul_11th_2018}
% chapter lecture_29_jul_11th_2018

\section{Polynomial Ring (Continued 2)}%
\label{sec:polynomial_ring_continued_2}
% section polynomial_ring_continued

\subsection{Factorization of Polynomials (Continued 2)}%
\label{sub:factorization_of_polynomials_continued_2}
% subsection factorization_of_polynomials_continued_2

\begin{note}
  If $d(x)$ and $d_1(x)$ satisfies \cref{propo:properties_of_the_greatest_common_divisor}, then in particular $(3)$ is satisfied, i.e.
  \begin{equation*}
    d(x) \, | \, d_1(x) \text{ and } d_1(x) \, | \, d(x),
  \end{equation*}
  then since $d_1(x) = d(x)$ by \cref{propo:f_div_g_and_g_div_f_implies_f_is_g}. Thus $d(x)$ is unique and is therefore called the greatest common divisor of $f(x)$ and $g(x)$, denoted by $\gcd\Big( f(x), \, g(x) \Big) = d(x)$.
\end{note}

\newthought{Note that} in integers, $p \in \mathbb{Z}$ is prime if $p \geq 2$ and whenever $p = ab$, then $a = \pm 1$ or $b = \pm 1$, where $a, \, b \in \mathbb{Z}$. We can have an ``analogous'' notion with polynomials.

\begin{defn}[Irreducible Polynomials]\index{Irreducible Polynomials}
\label{defn:irreducible_polynomials}
  Let $F$ be a field. A non-zero polynomial $l(x) \in F[x]$ is \hlnoteb{irreducible} if $\deg l \geq 1$ and if
  \begin{equation*}
    l(x) = l_1 (x) l_2 (x)
  \end{equation*}
  for $l_1(x), \, l_2 (x) \in F[x]$, then $\deg l_1 = 0$ or $\deg l_2 = 0$ \sidenote{Note that polynomials of degree $0$ are the units of $F[x]$.}.

  Polynomials that are not irreducible are called \hldefn{reducible polynomials}.
\end{defn}

\begin{propo}[Euclid's Lemma for Polynomials]
\label{propo:euclid_s_lemma_for_polynomials}
Let $F$ be a field and $a(x), b(x) \in F[x]$. If $l(x) \in F[x]$ is irreducible and $l(x) \, | \, a(x) b(x)$, then $l(x) \, | \, a(x)$ or $l(x) \, | \, b(x)$.
\end{propo}
\marginnote{This is a good proof for an exercise.
\begin{ex}
  Prove \cref{propo:euclid_s_lemma_for_polynomials}.
\end{ex}}

\begin{proof}
  Suppose $l(x) \, | \, f(x) g(x)$ and $l(x) \not| \, f(x)$. Since $l(x) \not| f(x)$, we have $\gcd[ l(x), f(x) ] = 1$. Then by \cref{propo:properties_of_the_greatest_common_divisor}, $\exists s(x), t(x) \in F[x]$ such that
  \begin{equation*}
    l(x) s(x) + f(x) t(x) = 1.
  \end{equation*}
  Multiplying the equation by $g(x)$, and since $F[x]$ is a field, we have
  \begin{equation*}
    l(x) s(x) g(x) + f(x) g(x) t(x) = g(x).
  \end{equation*}
  Since $l(x) \, | \, f(x) g(x)$ by assumption, we have that $l(x)$ divides the right hand side, and so it must also divide the left hand side, i.e. $l(x) \, | \, g(x)$.\qed
\end{proof}

\begin{thm}[Unique Factorization Theorem for Polynomials]
\index{Unique Factorization Theorem for Polynomials}
\label{thm:unique_factorization_theorem_for_polynomials}
Let $F$ be a field and $f(x) \in F[x]$ with $\deg f \geq 1$. Then we can write
\begin{equation*}
  f(x) = c l_1(x) l_2(x) \hdots l_m(x)
\end{equation*}
where $c \in F^*$ is a unit, and for $1 \leq i \leq m$, $l_i(x)$ is a irreducible monic polynomial. This factorization is unique up to the order of $l_i$.
\end{thm}
\marginnote{This is, yet again, a good proof for an exercise.
\begin{ex}
  Proof \cref{thm:unique_factorization_theorem_for_polynomials}.
\end{ex}}

\begin{proof}
  We shall only prove for when $f(x)$ is a monic polynomial, for if $f(x)$ is not monic, then it has some leading coefficient $a \neq 1 \in F$. Then since $F$ is a field, we have that $a^{-1} f(x)$ is a monic polynomial for which we can continue our consideration.

  Suppose $f(x)$ is a monic polynomial that has the least degree such that it cannot be expressed as a product of irreducible monic polynomials. Clearly, $f(x)$ cannot be irreducible itself, or it would trivially be expressible as a product of irreducible monic polynomials. Therefore, $\exists s(x), t(x) \in F[x]$ such that
  \begin{equation*}
    f(x) = s(x) t(x)
  \end{equation*}
  where $1 \leq \deg s, \, \deg t \leq \deg f$. Since $f(x)$ is the polynomial of the least degree that cannot be expressed as a product of irreducible monic polnomials, $r(x)$ and $t(x)$ must be expressible as a product of irreducible monic polynomials. But this would contradict the fact that $f(x)$ is not expressible as a product of irreducible monic polynomials, and so $f(x)$ must be
  \begin{equation*}
    f(x) = l_1(x) l_2(x) \hdots l_m(x)
  \end{equation*}
  where $l_i(x)$ is an irreducible monic polynomial, for $1 \leq i \leq m$. For the case where $f(x)$ is not monic, say with $a$ as its leading coefficient, we would have
  \begin{equation*}
    f(x) = a l_1(x) l_2(x) \hdots l_m(x).
  \end{equation*}

  For uniqueness, suppose
  \begin{equation*}
    f(x) = c l_1(x) l_2(x) \hdots l_m(x) = d k_1(x) k_2(x) \hdots k_n(x)
  \end{equation*}
  for units $c, d \in F^*$ and irreducible monic polynomials $l_i, \, k_j$ for $1 \leq i \leq m$ and $1 \leq j \leq n$. Since $l_1(x) \, | \, f(x)$, by \cref{propo:euclid_s_lemma_for_polynomials}, $l_1(x) \, | \, k_j(x)$ for some $1 \leq j \leq n$. Relabelling the indices for the $k_j$'s if necessary, we can have that $l_1(x) \, | \, k_1(x)$. Since $k_1(x)$ is irreducible and monic, we must have that $l_1(x) = k_1(x)$.

  Now if we continue this line of argument for $i = 2, 3, ..., m$, and end up with $l_2(x) = k_2(x), \, l_3(x) = k_3(x), \, \hdots , \, l_m(x) = k_m(x)$, where, WLOG, we suppose that $m \leq n$. However, we must have that $n = m$, otherwise we would have some $k_j$, where $m < j \leq n$ that cannot divide any of the $l_i$'s.\qed
\end{proof}

\newthought{For the sake of comparison with} $\mathbb{Z}$, observe the table below:

{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{l | c | c}
                & $\mathbb{Z}$                                                                & $F[x]$ \\
  \hline
  elements      & $m$                                                                         & $f(x)$ \\
  \hline
  size          & $\abs{m}$                                                                   & $\deg f$ \\
  \hline
  units         & $\{ \pm 1 \}$                                                               & $F^*$ \\
                & $\Big( \mathbb{Z} \setminus \{0\} \Big) \Big/ \{ \pm 1 \} \cong \mathbb{N}$ & $\Big( F[x] \setminus \{0\} \Big) \Big/ F^* \cong \{ h : h \text{ is monic } \}$ \\
  \hline
  unique        & $m = \pm 1 p_1^{\alpha_1} \hdots p_n^{\alpha_n}$                            & $f(x) = c l_1(x)^{\alpha_1} \hdots l_n(x)^{\alpha_n} $ \\
  factorization & $p_i$ prime                                                                 & $\deg f \geq 1$ and $l_i$ are irreducible \\
  \hline
  ideals        & $\lra{n} : n \in \mathbb{N}$                                                & $\lra{h(x)} : h$ monic \\
                & $\faktor{\mathbb{Z}}{\lra{n}}$ is a field                                   & $\faktor{F[x]}{\lra{h(x)}}$ is a field \\
                & iff $n$ prime                                                               & iff $h(x)$ is irreducible
\end{tabular}}

In the next section, we will be investigating if the analogy given in the last row for polynomials holds.

% subsection factorization_of_polynomials_continued_2 (end)

\subsection{Quotient Rings of Polynomials}%
\label{sub:quotient_rings_of_polynomials}
% subsection quotient_rings_of_polynomials

\begin{propo}[Ideals of {$F[x]$} are Principal Ideals]
\label{propo:ideals_of_f_x_are_principal_ideals}\index{Principal Ideal}
If $F$ is a field. Then all ideas of $F[x]$ are of the form
\begin{equation*}
  \lra{h(x)} = h(x) F[x] \enspace \text{ for any } h(x) \in F[x].
\end{equation*}
If $\lra{h(x)} \neq \{0\}$ and $h(x)$ is monic, then it is uniquely determined.
\end{propo}

\begin{proof}
  Let $A$ be an ideal of $F[x]$. If $A = \{0\}$, then $A = \lra{0}$. If $A \neq \{0\}$, then it contains a non-zero polynomial. Since $A$ is an ideal, it has a monic polynomial\sidenote{If $f(x) \in A$ has a leading coefficient $a$, then we know that $a^{-1} \in F$, and so $a^{-1} f(x) \in F f(x) \subseteq A$ is monic.}. Amongst all monic polynomials in $A$, choose $h(x) \in A$ that has the minimal degree. Clearly, $\lra{h(x)} \subseteq A$. To prove for $\supseteq$, note that for $f(x) \in A$, by \cref{propo:division_algorithm_for_polynomials},
  \begin{equation*}
    \exists q(x), r(x) \in F[x] \quad f(x) = q(x) h(x) + r(x) \quad \deg r < \deg h.
  \end{equation*}
  If $r(x) \neq 0$, then let $u \neq 0$ be the leading coefficient of $r(x)$. Then since $A$ is an ideal and $f(x), h(x) \in A$, we have
  \begin{align*}
    u^{-1} r(x) &= u^{-1}\left( f(x) - q(x) h(x) \right) \\
                &= u^{-1} f(x) - u^{-1} q(x) h(x) \in A.
  \end{align*}
  Then we have that $\deg u^{-1} r = \deg r < \deg h$ is a monic polynomial in $A$, contradicting the minimality of $\deg h$. Thus $r(x) = 0$ and so $f(x) = q(x) h(x) \in \lra{h(x)}$. Therefore $A \subseteq \lra{h(x)}$ and so $A = \lra{h(x)}$.

  Now suppose that $A = \lra{h(x)} = \lra{k(x)}$. Then we must have $h(x) \, | \, k(x)$ and $k(x) \, | \, h(x)$. Since $h(x)$ and $k(x)$ are both monic, by \cref{propo:f_div_g_and_g_div_f_implies_f_is_g}, we have that $h(x) = k(x)$.\qed
\end{proof}

% subsection quotient_rings_of_polynomials (end)

% section polynomial_ring_continued_2 (end)

% chapter lecture_29_jul_11th_2018 (end)


%This is wonderful
\newpage
\section{Train Interpolation for AMG}
Here what we want do is to use the basic idea in Prod. Xu's big paper in ActaNumerica about the AMG method.

\subsection{Basic Methods}
The original problem is set as:
%\begin{problem}
	Find $P \in \mathbb{R}^{n\times n_c}$ as
	\begin{equation}\label{min-tra}
	\min_{P \in \mathcal{X}_eta} \rm{Tr} (P^\top \bar{R}^{\frac{1}{2}} A \bar R^{\frac{1}{2}} P)
	\end{equation}
	where
	\begin{equation}\label{min-con}
	\mathcal{X}_{\eta} = \{P \in \mathbb{R}^{n\times n_c} ~:~ P \bm{1} =  \sqrt{n_c\eta}\hat \xi_1, \quad (Pv, Pv) \ge \eta (v,v) \}.
	\end{equation}
%\end{problem}

But we want to train some GMG interpolation from AMG. So we can change those model little.
\subsection{Train GMG from AMG}
To verify that GMG can be obtained from this abstract AMG theory, we may ask the next problem:
\subsubsection{Original form}
%\begin{problem}
	Find $P \in \mathbb{R}^{n\times n_c}$ as
	\begin{equation}\label{min-tra}
	\min_{P \in \mathcal{X}} \rm{Tr} (P^\top \bar{R}^{\frac{1}{2}} A \bar R^{\frac{1}{2}} P)
	\end{equation}
	where
	\begin{equation}\label{min-con}
	\mathcal{X}_{} = \{P \in \mathbb{R}^{n\times n_c} ~:~ P \bm{1} =  \bm{1}, \quad \text{Sparse}(P) = \text{Sparse}(P^{GMG}) \}.
	\end{equation}
%\end{problem}
\subsubsection{Approximated form}
	Find $P \in \mathbb{R}^{n\times n_c}$ as
\begin{equation}\label{min-tra}
\min_{P \in \mathcal{X}} \rm{Tr} (P^\top X A \bar X^\top)
\end{equation}
where
\begin{equation}\label{min-con}
\mathcal{X}_{} = \{P \in \mathbb{R}^{n\times n_c} ~:~ P \bm{1} =  \bm{1}, \quad \text{Sparse}(P) = \text{Sparse}(P^{GMG}) \}.
\end{equation}
Here $\bar R = XX\top$ which we can adopt the exact formula as
\begin{itemize}
	\item G-S
	\begin{equation}\label{XXT:G-S}
	\bar R^{-1} = (D-L)D(D-U) \Rightarrow X = (D-L)D^{1 \over 2}.
	\end{equation}
	\item ILU
		\begin{equation}\label{XXT:ILU}
		\bar R^{-1} = XX^T \Rightarrow X = ILU.
	\end{equation}
\end{itemize}

\subsubsection{Few parameters form}
We can both solve the above two problems with $P$ only have d.o.f like
\begin{itemize}
	\item FDM:
\begin{itemize}
	\item Same sparsity w.r.t the ``graph": $5$
	\item Same sparsity w.r.t the GMG: $9$
	\item More connection w.r.t GMG: $16$
\end{itemize}
	\item FEM
\begin{itemize}
		\item Same sparsity w.r.t the ``graph": $7$
		\item Same sparsity w.r.t the GMG: $7$
		\item More connection w.r.t GMG: $9$ (add the symmetry property)
	\end{itemize}
\end{itemize}

\subsection{Implement Detail}
We use online PCA method to solve \eqref{min-tra}. Write $\tilde{A} = \bar{R}^{\frac 12}A\bar{R}^{\frac 12}$, and $P^S = (p^S_{ij}), p^S_{ij}=1 \text{ iff } p^{GMG}_{ij}\neq 0$. Let $\odot$ is entry-wise multiply, i.e.$(A\odot B)_{ij}=(a_{ij}b_{ij})$. Let $\|M\|_*=\sum_{i,j} |M_{ij}|$.
The algorithm is
\begin{algorithm}
	\caption{Online PCA for Prolongation of GMG}
	\label{Online PCA for Prolongation of GMG}
	\begin{algorithmic}
		\STATE choose a series of learning rate $\eta_t$, max iteration steps $M_{\text{step}}$ and convergence threshold $\epsilon$ and batchsize $s$.
		\STATE $t \leftarrow 0$ and choose a random initial $P^0$ from distribution $U(0,1)$.
		\FOR{ each row $\mathbf p$ of $P^{0}$}
		\STATE $\mathbf p \leftarrow \dfrac 1{\|\mathbf p\|_1}\mathbf{p}$
		\ENDFOR
		\STATE $P^0\leftarrow P^S\odot P^0$
		\REPEAT
		\STATE Sample vectors $V=(\mathbf v_1,...,\mathbf v_s)$ from distribution $N(0,\tilde{A})$
		\STATE $P^{t+1} \leftarrow P^t - \dfrac{\eta_t}{s} V V^T P$
		\STATE $P^{t+1}\leftarrow P^S\odot \hat{P}^{t+1}$
		\FOR{ each row $\mathbf p$ of $P^{t+1}$}
		\STATE $\mathbf p \leftarrow \dfrac 1{\|\mathbf p\|_1}\mathbf{p}$
		\ENDFOR
		\STATE $t\leftarrow t+1$
		\UNTIL{$t>M_{\text{step}}$ or $\|P^{t+1}-P^{t}\|_*<\epsilon \|P^{t+1}\|_* $ }
	\end{algorithmic}
\end{algorithm}
\begin{remark}
	We can use any $X$ such that $\bar{R} =XX^T$ to replace $\bar{R}^\frac{1}{2}$. For GS smoother, because $\bar{R}=(D-U)^{-1}D(D-L)^{-T}$, we can easily set $X=(D-L)^{-1}D^\frac 12$. For general $\bar{R}$, we can choose $X$ is the incomplete Cholesky factorization to make $X$ sparse.
\end{remark}
\newpage
\subsection{Numerical Experiment}
We choose $A$ is the five points  difference form of Possion equation and smoother is GS.

Let $P^*$ is the result of using \eqref{Online PCA for Prolongation of GMG} to solve \eqref{min-tra}. The numerical experiment shows $\dfrac{\|P^{GMG}-P^*\|_*}{\|P^{GMG}\|_*}<8\%$ and if we use $P^*$ in two level multigrid method to solve $Ax=b$, the stop iteration step of $P^*$ is the same as $P^{GMG}$. If we do more iteration, we can get a better  $P^*$ than $P^{GMG}$.
%which means $\rm{Tr} ((P^*)^\T \tilde{A} P^*) <\rm{Tr} ((P^{GMG})^\T \tilde{A} P^{GMG})$ and the stop iteration step of multigrid method is less than $P^{GMG}$.

\section{A Proof for Training $\lambda$ smoother for AMG}
Give some distribution for $v$, and some $v_i$ as $i = 1:m$, we can compute
\begin{equation}
	\frac{\sum_i \|Av_i\|^2}{\sum_i \|Av_i\|_A^2}.
\end{equation}

Now we sample $v_i$ form the distribution as:
\begin{equation}\label{sample}
v_i^{(j)} \sim N(0,1),  \quad \text{or}\quad  v_i^{(j)}  \sim U[-1,1],
\end{equation}
then we will have the next


\section{Constructing the queuing model}\label{sec:model}

Owing to a lack of available data on the system and its patients, the options
for the queuing model used are limited compared to those employed in some modern
works. However, there is a precedent for simplifying healthcare systems to a
single node with parallel servers that emulate resource
availability.~\cite{Steins2013}~and~\cite{Williams2015} provide good examples of
how this approach, when paired with discrete event simulation, can expose the
resource needs of a system beyond deterministic queuing theory models. In
particular,~\cite{Williams2015} shows how a single node, multiple server queue
can be used to accurately predict bed capacity and length of stay distributions
in a critical care unit using administrative data.

Following in the suit of recent literature, a single node using a \(M/M/c\)
queue is employed to model a hypothetical ward of patients presenting COPD.\ In
addition to this, the grouping found in Section~\ref{subsec:overview} provides
a set of patient classes in the queue. Under this model, the following
assumptions are made:
\begin{enumerate}
    \item Inter-arrival and service times of patients are each exponentially
        distributed with some mean. This is in spite of the system time
        distributions shown in Figure~\ref{fig:cluster_los} in order to
        simplify the model parameterisation.
    \item There are \(c \in \mathbb{N}\) servers available to arriving patients
        at the node representing the overall resource availability including bed
        capacity and hospital staff.
    \item There is no queue or system capacity. In~\cite{Williams2015}, a
        queue capacity of zero is set under the assumption that any surplus
        arrivals would be sent to another suitable ward or unit. As this
        hypothetical ward represents COPD patients potentially throughout a
        hospital, this assumption is not held.
    \item Without the availability of expert clinical knowledge, a first-in
        first-out service policy is employed in lieu of some patient priority
        framework.
\end{enumerate}

Each group of patients has its own arrival distribution. The parameter of this
distribution is taken to be the reciprocal of the mean inter-arrival times for
that group and is denoted by \(\lambda_i\) for each cluster \(i\).

Like arrivals, each group of patients has its own service time distribution.
Without full details of the process order or idle periods during a spell, some
assumption must be made about the true `service' time of a patient in hospital.
It is assumed here that the mean service time of a group of patients may be
approximated via their mean length of stay, i.e.\ the mean time spent in the
system. For simplicity, this work assumes that for each cluster, \(i\), the mean
service time of that cluster, \(\frac{1}{\mu_i}\), is directly proportional
to the mean total system time of that cluster, \(\frac{1}{\phi_i}\), such that:
\begin{equation}\label{eq:services}
    \mu_i = p_i \phi_i
\end{equation}

\noindent where \(p_i \in \interval[open left]{0}{1}\) is some parameter to be
determined for each group.

One of the few ground truths available in the provided data is the distribution
of the total length of stay. Given that the length of stay and resource
availability are connected, the approach here will be to simulate the length of
stay distribution for a range of values \(p_i\) and \(c\) in order to find the
parameters that best match the observed data. A diagram depicting the process
described in this section is provided in Figure~\ref{fig:process}.

The statistical comparison of two or more distributions can be done in a number
of ways. Such methods include the Kolmogorov-Smirnov test, a variety of
discrepancy approaches such as summed mean-squared error, and \(f\)-divergences.
A popular choice amongst the latter group (which may be considered
distance-like) is the Kullback-Leibler divergence which measures relative
information entropy from one probability distribution to
another~\cite{Kullback1951}. The key issue with many of these methods is that
they lack interpretability which is paramount when conveying information to
stakeholders. Interpretability not just from explaining how something works but
how its results may be explained also.

As such, a reasonable candidate is the (first) Wasserstein metric, also known as
the `earth mover' or `digger' distance~\cite{Vaserstein1969}. The Wasserstein
metric satisfies the conditions of a formal mathematical metric (like the
typical Euclidean distance), and its values take the units of the distributions
under comparison (in this case: days). Both of these characteristics can aid
understanding and explanation. In simple terms, the distance measures the
approximate `minimal work' required to move between two probability
distributions where `work' can be loosely defined as the product of how much of
the distribution's mass is to be moved and the distance it must be moved
by. More formally, the Wasserstein distance between two probability
distributions \(U\) and \(V\) is defined as:
\begin{equation}\label{eq:wasserstein}
    W(U, V) = \int_{0}^{1} \left\vert F^{-1}(t) - G^{-1}(t) \right\vert dt
\end{equation}

\noindent where \(F\) and \(G\) are the cumulative density functions of \(U\)
and \(V\) respectively. A proof of~\eqref{eq:wasserstein} is presented
in~\cite{Ramdas2017}. The parameter set with the smallest maximum distance
between any cluster's simulated system time distribution and the overall
observed length of stay distribution is then taken to be the most appropriate.
To be specific, let \(T\) denote the system time distribution of all of
the observed data and let \(T_{i,c,p}\) denote the system time distribution for
cluster \(i\) obtained from a simulation with \(c\) servers and
\(p := \left(p_0, p_1, p_2, p_3\right)\). Then the optimal parameter set
\(\left(c^*, p^*\right)\) is given by:
\begin{equation}\label{eq:parameters}
    \left(c^*, p^*\right) = \argmin_{c, p} \left\{%
        \max_{i} \left\{ W\left(T_{i,c,p}, T\right) \right\}%
    \right\}
\end{equation}

\begin{figure}
    \centering%
    \resizebox{\imgwidth}{!}{%
        \includestandalone{tex/process}
    }
    \caption{%
        A diagrammatic depiction of the queuing parameter recovery process.
    }\label{fig:process}
\end{figure}

The parameter sweep included values of each \(p_i\) from \(0.5\)
to \(1.0\) with a granularity of \(5.0 \times 10^{-2}\) and values of \(c\) from
\(40\) to \(60\) at steps of \(5\).  These choices were informed by the
assumptions of the model and formative analysis to reduce the parameter space
given the computational resources required to conduct the simulations. Each
parameter set was repeated \(50\) times with each simulation running for four
years of virtual time. The warm-up and cool-down periods were taken to be
approximately one year each leaving two years of simulated data from each
repetition.

\begin{figure}
    \centering%
    \begin{subfigure}{.5\imgwidth}
        \includegraphics[width=\linewidth]{best_params}
        \caption{}\label{fig:best_params}
    \end{subfigure}\hfill%
    \begin{subfigure}{.5\imgwidth}
        \includegraphics[width=\linewidth]{worst_params}
        \caption{}\label{fig:worst_params}
    \end{subfigure}
    \caption{Histograms of the simulated and observed length of stay data for
             the (\subref{fig:best_params}) best and (\subref{fig:worst_params})
             worst parameter sets.}\label{fig:params}
\end{figure}

The results of this parameter sweep can be summarised in
Figure~\ref{fig:params}. Each plot shows a comparison of the observed lengths
of stay across all groups and the newly simulated data with the best and worst
parameter sets respectively. It can be seen that, in the best case, a very close
fit has been found. Meanwhile, Figure~\ref{fig:worst_params} highlights the
importance of good parameter estimation under this model since the likelihood of
short-stay patient arrivals has been inflated disproportionately against the
tail of the distribution. Table~\ref{tab:comparison} reinforces these results
numerically, showing a clear fit by the best parameters across the board.

\begin{table}
    \centering
    \resizebox{\tabwidth}{!}{\input{tex/comparison}}
    \caption{A comparison of the observed data, and the best and worst simulated
        data based on the model parameters and summary statistics for length of
    stay (LOS).}\label{tab:comparison}
\end{table}

In this section, the clustering has been used to enrich the overall queuing
model and to recover the parameters for several classes within that queue to a
high standard. Now, using this model, the next section conducts an investigation
into the underlying system by adjusting the parameters of the queue with the
clustering.


\documentclass[10pt]{report}

\usepackage{enumerate} % for enumerate counter
\usepackage{subcaption} % for subfigures
\usepackage{amsthm} % for QED
\usepackage{mathtools} % for delimiter

\usepackage{listings} % for code
\lstset{
	language=R,
	basicstyle=\footnotesize\ttfamily,
	numbers=none,
	stepnumber=1,
	numbersep=8pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	frame=single,
	tabsize=2,
	captionpos=t,
	breaklines=true,
	breakatwhitespace=false
}

\usepackage{float} % for figure [H]
\usepackage{booktabs} % for tabular
\usepackage{caption} % for \caption*
\usepackage[export]{adjustbox} % for valign=t
\usepackage{array} % for column type m
\usepackage{verbatim}
\usepackage{graphicx}
%\graphicspath{ {imgs/} }
\usepackage{fancyhdr}
\usepackage{amssymb}
\usepackage{amsmath}

%%%%%%Pagination
\setlength{\topmargin}{-.3 in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textheight}{9.in}
\setlength{\textwidth}{6.5in}

%Cover
\newcommand{\hwTitle}{Homework \#7}
\newcommand{\hwCourse}{Applied Statistics}
\newcommand{\hmClassInstructor}{Professor Lulu Kang}

\title{
	\vspace{2in}
	\textmd{\textbf{\hwCourse\\\hwTitle}}\\
	\vspace{0.3in}\large{\textit{\hmClassInstructor}}
	\vspace{3in}
}
\author{\textbf{Zhihao Ai}}
\date{}

%Header
\pagestyle{fancy}
\fancyhead[L]{Zhihao Ai}
\fancyhead[C]{Math 484}
\fancyhead[R]{Homework 7}
%%%%%%

%Global settings
%\everymath{\displaystyle}
\setlength\parindent{0pt}

%Custom commands
\newcommand{\ds}{\displaystyle}
\newcommand{\ts}{\textstyle}

\newcolumntype{N}{>$ c <$}
\newcolumntype{M}[1]{>{\centering\arraybackslash $}m{#1}<{$}}

\newcommand{\abs}[1] {\left| #1 \right|}

\DeclarePairedDelimiter\autoparen{(}{)}
\newcommand{\pa}[1]{\autoparen*{#1}}

\newcommand{\var} {\text{var}}

\newcommand{\m}[1] {\mathbf{#1}}

\begin{document}

\maketitle

\section*{Problem 1}
(Ex. 14.14) \textbf{Flu shots}\\
Multiple logistic regression model (14.41) with three predictor variables in first-order terms is assumed to be appropriate.1
\begin{enumerate}[a.]
	\item
	Find the maximum likelihood estimates of $\beta_0$, $\beta_1$, $\beta_2$ and $\beta_3$. State the fitted response function.
	\lstinputlisting{p1/14a.txt}
	The fitted response function is
	\[
	\hat{\pi} = \frac{\exp(-1.17716 + 0.07279 X_1 - 0.09899 X_2 + 0.43397 X_3)}{1 + \exp(-1.17716 + 0.07279 X_1 - 0.09899 X_2 + 0.43397 X_3)}
	\]
	The maximum likelihood estimate of $\beta_0$, $\beta_1$, $\beta_2$ and $\beta_3$ are $b_0 = -1.17716$, $b_1 = 0.07279$, $b_2 = -0.09899$ and $b_3 = 0.43397$, respectively.

	\item
	Obtain $\exp(b_1)$, $\exp(b_2)$, and $\exp(b_3)$. Interpret these numbers.

	The odds ratio $\widehat{OR}_1 = \exp(b_1) = 1.075505$ means that the odds of a client receiving a flu shot are estimated to increase by about 7.6 percent with each unit increase in the age of the client, with other variables fixed.\\
	The odds ratio $\widehat{OR}_2 = \exp(b_2) = 0.9057518$ means that the odds of a client receiving a flu shot are estimated to decrease by about 9.4 percent with each unit increase in the health awareness index of the client, with other variables fixed.\\
	The odds ratio $\widehat{OR}_3 = \exp(b_3) = 1.543373$ means that the odds of a male client receiving a flu shot are about 1.54 times as great as for a female client, with other variables fixed.

	\item
	What is the estimated probability that male clients aged 55 with a health awareness index of 60 will receive a flu shot?

	The estimated probability is $\hat{\pi}(55, 60, 1) = \frac{\exp(-1.17716 + 0.07279\cdot 55 - 0.09899\cdot 60 + 0.43397\cdot 1)}{1 + \exp(-1.17716 + 0.07279\cdot 55 - 0.09899\cdot 60 + 0.43397\cdot 1)} = 0.06422$.
\end{enumerate}

\section*{Problem 2}
(Ex. 14.20) Refer to \textbf{Flu shots}
\begin{enumerate}[a.]
	\item
	Obtain joint confidence intervals for the age odds ratio $\exp(30\beta_1)$ for male clients whose ages differ by 30 years and for the health awareness index odds ratio $\exp(25\beta_2)$ for male clients whose health awareness index differs by 25, with family confidence coefficient of approximately .90. Interpret your intervals.

	With 2 parameters to be estimated and family confidence coefficient being approximately .90
	\[
	B = z(1 - 0.1/2/2) = 1.959964
	\]
	The joint Bonferronic confidence limits are
	\begin{align*}
		b_1 &\pm B\cdot s(b_1) = 0.07279 \pm 0.05954\\
		b_2 &\pm B\cdot s(b_2) = -0.09899 \pm 0.06562
	\end{align*}
	The confidence intervals for the odds ratio are
	\begin{align*}
		1.4881 \le &\exp(30\cdot \beta_1) \le 52.97923\\
		0.01632 \le &\exp(25\cdot \beta_2) \le 0.4342
	\end{align*}
	The intervals mean that, with 90 percent confidence we estimate that the odds of male clients receiving flu shots increase by between 48.8 percent and 5197.8 percent with an increase in age by 30 years, and decrease by between 56.6 percent and 98.4 percent with an increase in health awareness index by 25.

	\item
	Use the Wald test to determine whether $X_3$, client gender, can be dropped from the regression model; use $\alpha=.05$. State the alternatives, decision rule, and conclusion. What is the approximate $P$-value of the test?

	The alternatives are
	\begin{align*}
		H_0 &: \beta_3 = 0\\
		H_a &: \beta_3 \ne 0
	\end{align*}
	The test statistic is
	\[
	z^* = \frac{b_3}{s(b_3)} = \frac{0.43397}{0.52179} = 0.832
	\]
	The decision rule is
	\begin{align*}
	\text{If } \abs{z^*} \le z(1-\alpha/2) = 1.959964, \text{ conclude } H_0\\
	\text{If } \abs{z^*} > z(1-\alpha/2) = 1.959964, \text{ conclude } H_a
	\end{align*}
	Since $z^* = 0.832 < 1.959964$, we conclude $H_0$, that $X_3$ can be dropped from the regression model. The $P$-value of the test is $P(\abs{Z} > \abs{z^*}) = 0.40558$.

	\item
	Use the likelihood ratio test to determine whether $X_3$, client gender, can be dropped from the regression model; use $\alpha=.05$. State the full and reduced models, decision rule, and conclusion. What is the approximate $P$-value of the test? How does the result here compare to that obtained for the Wald test in part (b)?

	The full model is
	\[
	\log\pa{\frac{\pi(x)}{1-\pi(x)}} = \beta_0 + X_1 \beta_1 + X_2 \beta_2 + X_3 \beta_3
	\]
	The reduced model is
	\[
	\log\pa{\frac{\pi(x)}{1-\pi(x)}} = \beta_0 + X_1 \beta_1 + X_2 \beta_2
	\]
	\lstinputlisting{p2/20c.txt}
	The alternatives are
	\begin{align*}
		H_0 &: \beta_3 = 0\\
		H_a &: \beta_3 \ne 0
	\end{align*}
	The test statistic is
	\[
	G^2 = -2 \log\pa{\frac{L(R)}{L(F)}} =  0.70221
	\]
	The decision rule is
	\begin{align*}
		\text{If } G^2 \le \chi^2(1-\alpha; p-q) = 3.841459, \text{ conclude } H_0\\
		\text{If } G^2 > \chi^2(1-\alpha; p-q) = 3.841459, \text{ conclude } H_a
	\end{align*}
	Since $G^2 = 0.70221 < 3.841459$, we conclude $H_0$, that $\beta_3$ can be dropped from the regression model. The $P$-value of the test is 0.4020421. The result is the same as what was obtained in part (b).

	\item
	Use the likelihood ratio test to determine whether the following three second-order terms, the square of age, the square of health awareness index, and the two-factor interaction effect between age and health awareness index, should be added simultaneously to the regression model containing age and health awareness index as first-order terms; use $\alpha=.05$. State the alternatives, full and reduced models, decision rule, and conclusion. What is the approximate $P$-value of the test?

	The full model is
	\[
	\log\pa{\frac{\pi(x)}{1-\pi(x)}} = \beta_0 + X_1 \beta_1 + X_2 \beta_2 + X_1^2 \beta_3 + X_2^2 \beta_4 + X_1 X_2 \beta_5
	\]
	The reduced model is
	\[
	\log\pa{\frac{\pi(x)}{1-\pi(x)}} = \beta_0 + X_1 \beta_1 + X_2 \beta_2
	\]
	\lstinputlisting{p2/20d.txt}
	The alternatives are
	\begin{align*}
	H_0 &: \beta_3 = \beta_4 = \beta_5 = 0\\
	H_a &: \text{not all $\beta_k$ in $H_0$ equal zero}
	\end{align*}
	The test statistic is
	\[
	G^2 = -2 \log\pa{\frac{L(R)}{L(F)}} =  1.5339
	\]
	The decision rule is
	\begin{align*}
	\text{If } G^2 \le \chi^2(1-\alpha; p-q) = 7.814728, \text{ conclude } H_0\\
	\text{If } G^2 > \chi^2(1-\alpha; p-q) = 7.814728, \text{ conclude } H_a
	\end{align*}
	Since $G^2 = 1.5339 < 7.814728$, we conclude $H_0$, that the three second-order terms should not be added simultaneously to the regression model. The $P$-value of the test is 0.6744686.
\end{enumerate}

\section*{Problem 3}
(Ex. 14.22) Refer to \textbf{Flu shots} where the pool of predictors consist of all first-order terms and all second-order terms in age and health awareness index.
\begin{enumerate}[a.]
	\item
	Use forward selection to decide which predictor variables enter into the regression model. Control the $\alpha$ risk at .10 at each stage. Which variables are entered into the regression model?

	$X_2$ and $X_1 X_2$ are entered into the regression model.

	\item
	Use backware elimination to decide which predictor variables can be dropped from the regression model. Control the $\alpha$ risk at .10 at each stage. Which variables are retained? How does this compare to your results in part (a)?

	$X_2$ and $X_1 X_2$ are retained. It's the same as the results in part (b).

	\item
	Find the best model according to the $AIC_p$ criterion. How does this compare to your results in part (a) and part (b)?

	According to the $AIC_p$ criterion, the best model contains $X_2$ and $X_1 X_2$, with $AIC_p = 111.789$. It's the same as the previous results.

	\item
	Find the best model according to the $SBC_p$ criterion. How does this compare to your results in part (a), part (b) and part (c)?

	According to the $SBC_p$ criterion, the best model contains $X_2$ and $X_1 X_2$, with $SBC_p = 120.9957$. It's the same as the previous results.
\end{enumerate}

\end{document}


\subsection{Autoregressive Distributed Lag (ARDL) model}

Include lagged y and lagged x (and current x)

If the processes are stationary, then we can use OLS. THIS IS A BROADER POINT! INTRO??


\setlength{\parindent}{0.5cm}
\chapter{Electromagnetic Fields}\label{sec:electromagneticfields}
A field is a spatial distribution of a quantity.  Variations in a field can be represented graphically by directed field lines called flux lines or stream lines.  Flux lines may arise from a vortex source which causes a circulation or curl of the field around it, or from a flow source which causes a net outward flow or divergence of the field from it.\footnote{The material of this chapter -- definitions and equations and their development -- has been sourced from Cheng \cite{Cheng1989}.}  The following sections develop models that describe steady electric fields arising from static electric charges, steady magnetic fields arising from electric charges in motion, steady electromagnetic fields arising from steady electric currents in conductors, and time-varying electromagnetic fields.

\section{The Electrostatic Model}\label{sec:electrostatics}

\section{The Magnetostatic Model}\label{sec:magnetostatics}
An electric charge $q$ (C) with velocity $\mathbf{v}$ (m/s) experiences a force $\mathbf{F_m}$ (N) when placed in a steady magnetic field represented by $\mathbf{B}$:
\begin{equation}\label{F m}
\mathbf{F_m} = q \mathbf{v} \times \mathbf{B}
\end{equation}
Units for $\mathbf{B}$: $\left( \frac{\mathrm{N}}{\mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}} \right)$ or $\left( \frac{\mathrm{J}}{\mathrm{A} \cdot \mathrm{m^2}} \right)$ or (Wb/m$^2$) or (T).\footnote{Although $\mathbf{B}$ is described commonly as a density of the flux of the magnetic field (magnetic flux density), it is a direct measure of the \emph{strength} of the field, since it measures the \emph{force} exerted by the magnetic field on moving electric charges.  Similarly, $\mathbf{H}$ is described commonly as the magnetic field strength but is only indirectly a measure of the strength, since it is the product of the electric charges and their velocities per unit area of a surface through which the magnetic field they generate passes (see main text below); and so it may be better described as a density of the flux of the field (magnetic flux density).  Calling $\mathbf{B}$ the \emph{magnetic field strength} and $\mathbf{H}$ the \emph{magnetic flux density} has an analog in electric field theory
where $\mathbf{E}$, commonly described as the \emph{electric field strength}, measures the force exerted by the electric field on electric charges, and $\mathbf{D}$, commonly described as the \emph{electric flux density}, measures the electric charge per unit area of an enclosing surface through which the electric field that the charges generate passes.%they pass and around whose contour the magnetic field circulates.  Although calling $\mathbf{H}$ the \emph{magnetic flux density} is at odds with currently popular names, it does have an analog in electric field theory where $\mathbf{D}$, commonly described as the \emph{electric flux density}, measures the electric charge per unit area of an enclosing surface through which the electric field flows.
}

%Although calling $\mathbf{B}$ the \emph{magnetic field strength} is at odds with currently popular names, it does have an analog in electric field theory where $\mathbf{E}$, commonly described as the \emph{electric field strength}, measures the force exerted by the electric field on electric charges.

For a steady magnetic field in the absence of a magnetizable material the curl of $\mathbf{B}$ is proportional to the current density $\mathbf{J}$ (A/m$^2$) arising from free electric charges:
\begin{equation}\label{curl B}
\mathbf{\nabla} \times \mathbf{B} = \mu_0 \mathbf{J}
\end{equation}
or in integral form:
\begin{equation}\label{int B.dl}
\int_S (\mathbf{\nabla} \times \mathbf{B}) \cdot d\mathbf{s} = \oint_C \mathbf{B} \cdot d\mathbf{l} =  \mu_0 \int_S \mathbf{J} \cdot d\mathbf{s} = \mu_0 I
\end{equation}
{\noindent}where the surface integral of the circulation of $\mathbf{B}$ over the open surface $S$ equals the closed line integral of $\mathbf{B}$ along the contour $C$ bounding the surface by Stokes's theorem (or, the curl theorem); $\mu_0$, equal to $4 \pi \times 10^{-7}$ H/m (or N/A$^2$), is the magnetic field constant or the permeability of free space; and $I$ (A) is the current corresponding to $\mathbf{J}$.\\

For a steady magnetic field in the presence of a magnetizable material the curl of $\mathbf{B}$ also depends on an equivalent magnetization current density $\mathbf{J_m}$ (A/m$^2$) arising from the magnetization of a medium from bound electric charges:
\begin{equation}\label{curl B in}
\mathbf{\nabla} \times \mathbf{B} = \mu_0 (\mathbf{J} + \mathbf{J_m})
\end{equation}
or in integral form:
\begin{equation}\label{int B.dl in}
\int_S (\mathbf{\nabla} \times \mathbf{B}) \cdot d\mathbf{s} = \oint_C \mathbf{B} \cdot d\mathbf{l} =  \mu_0 \int_S (\mathbf{J} + \mathbf{J_m}) \cdot d\mathbf{s} = \mu_0 (I + I_m)
\end{equation}
{\noindent}where $I_m$ (A) is the equivalent magnetization current corresponding to $\mathbf{J_m}$. $\mathbf{J_m}$ is the circulation of the magnetic polarization $\mathbf{M}$ (A/m) which is the density of magnetic dipole moments arising from the atoms in a material:
\begin{equation}\label{J m}
\mathbf{J_m} = \mathbf{\nabla} \times \mathbf{M}
\end{equation}
Substituting Eq.~\ref{J m} into Eq.~\ref{curl B in} yields one of the governing equations for the magnetostatic model:
\begin{equation}\label{curl H}
\mathbf{\nabla} \times \mathbf{H} = \mathbf{J}
\end{equation}
or in integral form:
\begin{equation}\label{int H.dl}
\int_S (\mathbf{\nabla} \times \mathbf{H}) \cdot d\mathbf{s} = \oint_C \mathbf{H} \cdot d\mathbf{l} =  \int_S \mathbf{J} \cdot d\mathbf{s} = I
\end{equation}
where the steady magnetic field is now represented by $\mathbf{H}$:
\begin{equation}\label{H}
\mathbf{H} = \frac{\mathbf{B}}{\mu_0} - \mathbf{M}
\end{equation}
Units for $\mathbf{H}$: $\left( \frac{\mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}}{\mathrm{m^2}} \right)$ or (A/m).\footnote{We have, then, in $\mathbf{B}$ and $\mathbf{H}$, two ways of taking the measure of a magnetic field.  One measure ($\mathbf{B}$) is calculation of the force exerted by the field on a unit electric charge having unit velocity $\left( \frac{\mathrm{N}}{\mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}} \right)$.  The other measure ($\mathbf{H}$) is calculation of the product of  the electric charges themselves and their velocities per unit area $\left( \frac{\mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}}{\mathrm{m^2}} \right)$ of the surface through which the generated magnetic field passes.  These two measures of the magnetic field have an analog in the measures of an electric field, where $\mathbf{E}$, the electric field strength, is a measure of the field as a force per unit electric charge (N/C), and $\mathbf{D}$, the electric flux density (or electric displacement), is a measure of
the field as the electric charges themselves per unit area (C/m$^2$) of the enclosing surface through which the generated electric field passes.}\\

Because the divergence of the curl of a vector field is zero, finding the divergence of Eq.~\ref{curl H} yields:
\begin{equation}\label{div J}
\mathbf{\nabla} \cdot \mathbf{J} = 0
\end{equation}
{\noindent}The zero divergence of the current density requires steady currents in the magnetostatic model, although in the more general electromagnetic model currents may vary with time and may therefore have nonzero divergence.

There are no flow sources for a magnetic field, only vortex sources: moving electric charges, around which magnetic fields circulate.  Magnetic flux lines in free space always close upon themselves, making them solenoidal.  Using $\mathbf{B}$ to represent the magnetic field, the divergence of $\mathbf{B}$ will always be zero, yielding the other governing equation for the magnetostatic model:
\begin{equation}\label{div B}
\mathbf{\nabla} \cdot \mathbf{B} = 0
\end{equation}
or in integral form:
\begin{equation}\label{int B.ds}
\int_V \mathbf{\nabla} \cdot \mathbf{B}\,dv = \oint_S \mathbf{B} \cdot d\mathbf{s} = 0
\end{equation}
{\noindent}where the volume integral of the divergence of $\mathbf{B}$ over the volume $V$ equals the total flux of $\mathbf{B}$ through the surface $S$ bounding the volume by Gauss's theorem (or, the divergence theorem).\\

Within a magnetizable material the magnetic field is affected by the magnetic polarization $\mathbf{M}$ of the medium, which is not necessarily divergenceless.  Using $\mathbf{H}$ to represent the magnetic field, including the magnetization arising from bound charges, the divergence of $\mathbf{H}$ will not always be zero:
\begin{equation}\label{div H}
\mathbf{\nabla} \cdot \mathbf{H} = \frac{1}{\mu_0} \mathbf{\nabla} \cdot \mathbf{B} - \mathbf{\nabla} \cdot \mathbf{M} = - \mathbf{\nabla} \cdot \mathbf{M}
\end{equation}
Because the magnetic polarization does not exist outside a magnetizable material, in free space $\mathbf{H}$ is proportional to $\mathbf{B}$ and is then divergenceless:
\begin{equation}\label{H free space}
\mathbf{H} = \frac{\mathbf{B}}{\mu_0}
\end{equation}

Because $\mathbf{B}$ is divergenceless, and because the divergence of the curl of a vector field is also zero, $\mathbf{B}$ can be expressed as the curl of another vector field, the vector magnetic potential $\mathbf{A}$:
\begin{equation}\label{curl A}
\mathbf{B} = \mathbf{\nabla} \times \mathbf{A}
\end{equation}
Units for $\mathbf{A}$: $\left( \frac{\mathrm{J}}{\mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}} \right)$ or (N/A) or (Wb/m) or ($\mathrm{T} \cdot \mathrm{m}$).\\
{\noindent}Thus:
\begin{equation}\label{curl curl A}
\mathbf{\nabla} \times \mathbf{\nabla} \times \mathbf{A} = \mu_0 \mathbf{J}
\end{equation}
{\noindent}Using the vector identity:
\begin{equation}\label{identity}
\mathbf{\nabla} \times \mathbf{\nabla} \times \mathbf{A} = \mathbf{\nabla} (\mathbf{\nabla} \cdot \mathbf{A}) - \mathbf{\nabla}^2 \mathbf{A}
\end{equation}
{\noindent}and choosing $\mathbf{A}$ to be divergenceless yields a vector Poisson's equation:
\begin{equation}\label{vector Poisson}
\mathbf{\nabla}^2 \mathbf{A} = -\mu_0\mathbf{J}
\end{equation}
{\noindent}The vector magnetic potential can therefore be expressed in terms of the volume integral of the current density:
\begin{equation}\label{A}
\mathbf{A} = \frac{\mu_0}{4 \pi} \int_V \frac{\mathbf{J}}{R}\,dv
\end{equation}
{\noindent}where $R$ is the distance from the moving electric charges that are generating the magnetic field.\\

The vector magnetic potential $\mathbf{A}$ can thus be obtained from the current density $\mathbf{J}$, and $\mathbf{B}$ can then be obtained from $\mathbf{A}$.\\

Although there is no net flow of the magnetic field in free space through an enclosing surface (see Eq.~\ref{int B.ds}), through a surface not bounding a volume there will be a net flow.  If $\mathbf{B}$, the force exerted on or by moving electric charges, is used to quantify the magnetic field, then  $\Phi_{\mathbf{B}}$ is the integral of $\mathbf{B}$ over an area of surface, which relates to the integral of the vector magnetic potential over a contour bounding the surface using the curl theorem:
\begin{equation}\label{Phi B}
\Phi_{\mathbf{B}} = \int_S \mathbf{B} \cdot d\mathbf{s} = \int_S (\mathbf{\nabla} \times \mathbf{A}) \cdot d\mathbf{s} = \oint_C \mathbf{A} \cdot d\mathbf{l}
\end{equation}
{\noindent}Units for $\Phi_{\mathbf{B}}$: $\left( \frac{\mathrm{N} \cdot \mathrm{m^2}}{\mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}} \right)$ or (J/A) or (Wb).\\
The integral of $\mathbf{B}$ through an open surface (commonly called the flux of $\mathbf{B}$) is proportional (by the permeability of free space) to the product of the electric charges and their velocities which are generating the magnetic field through the surface.  This bears a resemblance to one form of Gauss's law which states that the net flux of $\mathbf{E}$ (electric field strength) in free space through a closed surface is proportional (by the permittivity of free space) to the electric charges enclosed by the surface.\\

If $\mathbf{H}$, the density of moving electric charges, is used to quantify the magnetic field, then the flow of the magnetic field in free space through an open surface may also be expressed as the flux $\Phi_{\mathbf{H}}$ of $\mathbf{H}$ (which is now proportional to $\mathbf{B}$):
\begin{equation}\label{H flux}
\Phi_{\mathbf{H}} = \int_S \mathbf{H} \cdot d\mathbf{s} = \frac{1}{\mu_0} \int_S (\mathbf{\nabla} \times \mathbf{A}) \cdot d\mathbf{s} = \frac{1}{\mu_0} \oint_C \mathbf{A} \cdot d\mathbf{l}
\end{equation}
{\noindent}Units for $\Phi_{\mathbf{H}}$: $\left( \mathrm{C} \cdot \frac{\mathrm{m}}{\mathrm{s}}\right)$ or ($\mathrm{A} \cdot \mathrm{m}$).\\
The flux of $\mathbf{H}$ in free space through an open surface equals the electric charges multiplied by their velocities which are generating the magnetic field through the surface.  This also bears a resemblance to another form of Gauss's law which states that the net flux of $\mathbf{D}$ (electric flux density) through a closed surface equals the electric charges enclosed by the surface.\\

When the magnetic properties of a medium are linear, the magnetization $\mathbf{M}$ is directly proportional to the magnitude of $\mathbf{H}$; when the properties are  also isotropic, $\mathbf{M}$ is also independent of the direction of $\mathbf{H}$ and the magnetization is formulated as:
\begin{equation}\label{chi m}
\mathbf{M} = \chi_m \mathbf{H}
\end{equation}
{\noindent}where $\chi_m$ is a dimensionless variable called the magnetic susceptibility, which is a function only of coordinates (that is, $\chi_m \equiv \chi_m(x,y,z)$ in a cartesian coordinate system) of the linear, isotropic medium.
{\noindent}$\mathbf{H}$ and $\mathbf{B}$ are now related in the magnetostatic model (for linear, isotropic media) by the constitutive relation:
\begin{equation}\label{H chi m}
\mathbf{H} = \frac{1}{\mu} \mathbf{B}
\end{equation}
{\noindent}where $\mu = \mu_0 \mu_r$ (H/m) is the permeability of the medium and $\mu_r =  1 + \chi_m$, another dimensionless variable, is the relative permeability of the medium.  When, besides being linear and isotropic, the medium is homogeneous (a simple medium), the magnetic susceptibility, permeability and relative permeability, besides being independent of $\mathbf{H}$, are also independent of space coordinates and are thus constant.

If there are no free currents, $\mathbf{H}$ does not circulate and Eq.~\ref{curl H} becomes:
\begin{equation}\label{curl H J=0}
\mathbf{\nabla} \times \mathbf{H} = 0
\end{equation}
{\noindent}and $\mathbf{H}$ can be expressed as the gradient of a scalar field, the scalar magnetic potential $W$ (A):
\begin{equation}\label{grad W}
\mathbf{H} = -\mathbf{\nabla}W
\end{equation}

\section{The Electromagnetostatic Model}\label{sec:electromagnetostatics}

\section{The Electromagnetodynamic Model}\label{sec:electromagnetodynamics}


\documentclass[a4paper,11pt,final]{article}
\usepackage{fancyvrb, color, graphicx, hyperref, amsmath, url}
\usepackage{palatino}
\usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}

\hypersetup
{   pdfauthor = {Matti Pastell},
  pdftitle={FIR filter design with Python and SciPy},
  colorlinks=TRUE,
  linkcolor=black,
  citecolor=blue,
  urlcolor=blue
}

\setlength{\parindent}{0pt}
\setlength{\parskip}{1.2ex}

\title{FIR filter design with Python and SciPy}
\author{Matti Pastell \\ \url{http://mpastell.com}}
\date{15th April 2013}

\begin{document}
\maketitle

\section{Introduction}

This an example of a document that can be published using
\href{http://mpastell.com/pweave}{Pweave}. Text is written
using \LaTeX{} and code between \texttt{<<>>} and \texttt{@} is executed
and results are included in the resulting document.

You can define various options for code chunks to control code
execution and formatting (see
\href{http://mpastell.com/pweave/usage.html\#code-chunk-options}{Pweave
docs}).

\section{FIR Filter Design}

We'll implement lowpass, highpass and ' bandpass FIR filters. If you
want to read more about DSP I highly recommend
\href{http://www.dspguide.com/}{The Scientist and Engineer's Guide to
Digital Signal Processing} which is freely available online.

\subsection{Functions for frequency, phase, impulse and step response}

Let's first define functions to plot filter properties.

\begin{verbatim}
from pylab import *
import scipy.signal as signal

#Plot frequency and phase response
def mfreqz(b,a=1):
    w,h = signal.freqz(b,a)
    h_dB = 20 * log10 (abs(h))
    subplot(211)
    plot(w/max(w),h_dB)
    ylim(-150, 5)
    ylabel('Magnitude (db)')
    xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
    title(r'Frequency response')
    subplot(212)
    h_Phase = unwrap(arctan2(imag(h),real(h)))
    plot(w/max(w),h_Phase)
    ylabel('Phase (radians)')
    xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
    title(r'Phase response')
    subplots_adjust(hspace=0.5)

#Plot step and impulse response
def impz(b,a=1):
    l = len(b)
    impulse = repeat(0.,l); impulse[0] =1.
    x = arange(0,l)
    response = signal.lfilter(b,a,impulse)
    subplot(211)
    stem(x, response)
    ylabel('Amplitude')
    xlabel(r'n (samples)')
    title(r'Impulse response')
    subplot(212)
    step = cumsum(response)
    stem(x, step)
    ylabel('Amplitude')
    xlabel(r'n (samples)')
    title(r'Step response')
    subplots_adjust(hspace=0.5)
\end{verbatim}
\begin{verbatim}
---------------------------------------------------------------------------ModuleNotFoundError
Traceback (most recent call last)<ipython-input-1-cb4bd4296452> in
<module>()
      1 from pylab import *
----> 2 import scipy.signal as signal
      3
      4 #Plot frequency and phase response
      5 def mfreqz(b,a=1):
ModuleNotFoundError: No module named 'scipy'
\end{verbatim}

\subsection{Lowpass FIR filter}

Designing a lowpass FIR filter is very simple to do with SciPy, all you
need to do is to define the window length, cut off frequency and the
window.

The Hamming window is defined as:
$w(n) = \alpha - \beta\cos\frac{2\pi n}{N-1}$, where $\alpha=0.54$ and
$\beta=0.46$

The next code chunk is executed in term mode, see the source document
for syntax. Notice also that Pweave can now catch multiple
figures/code chunk.

\begin{verbatim}
n = 61
a = signal.firwin(n, cutoff = 0.3, window = "hamming")
\end{verbatim}
\begin{verbatim}
---------------------------------------------------------------------------NameError
Traceback (most recent call last)<ipython-input-1-0edf555780c5> in
<module>()
----> 1 a = signal.firwin(n, cutoff = 0.3, window = "hamming")
NameError: name 'signal' is not defined
\end{verbatim}

\begin{verbatim}
#Frequency and phase response
mfreqz(a)
\end{verbatim}
\begin{verbatim}
---------------------------------------------------------------------------NameError
Traceback (most recent call last)<ipython-input-1-4b99431c27e4> in
<module>()
----> 1 mfreqz(a)
NameError: name 'mfreqz' is not defined
\end{verbatim}

\begin{verbatim}
show()
#Impulse and step response
figure(2)
\end{verbatim}
\begin{verbatim}
<Figure size 1200x800 with 0 Axes>
\end{verbatim}
\begin{verbatim}
<Figure size 1200x800 with 0 Axes>
\end{verbatim}

\begin{verbatim}
impz(a)
\end{verbatim}
\begin{verbatim}
---------------------------------------------------------------------------NameError
Traceback (most recent call last)<ipython-input-1-e3d06ad960d4> in
<module>()
----> 1 impz(a)
NameError: name 'impz' is not defined
\end{verbatim}

\begin{verbatim}
show()
\end{verbatim}

\subsection{Highpass FIR Filter}

Let's define a highpass FIR filter:

\begin{verbatim}
n = 101
a = signal.firwin(n, cutoff = 0.3, window = "hanning", pass_zero=False)
mfreqz(a)
show()
\end{verbatim}
\begin{verbatim}
---------------------------------------------------------------------------NameError
Traceback (most recent call last)<ipython-input-1-f567ff2f04e1> in
<module>()
      1 n = 101
----> 2 a = signal.firwin(n, cutoff = 0.3, window = "hanning",
pass_zero=False)
      3 mfreqz(a)
      4 show()
NameError: name 'signal' is not defined
\end{verbatim}

\subsection{Bandpass FIR filter}

Notice that the plot has a caption defined in code chunk options.

\begin{verbatim}
n = 1001
a = signal.firwin(n, cutoff = [0.2, 0.5], window = 'blackmanharris', pass_zero = False)
mfreqz(a)
show()
\end{verbatim}
\begin{verbatim}
---------------------------------------------------------------------------NameError
Traceback (most recent call last)<ipython-input-1-360bebdd9724> in
<module>()
      1 n = 1001
----> 2 a = signal.firwin(n, cutoff = [0.2, 0.5], window =
'blackmanharris', pass_zero = False)
      3 mfreqz(a)
      4 show()
NameError: name 'signal' is not defined
\end{verbatim}
\begin{figure}[htpb]
\center
\caption{Bandpass FIR filter.}
\label{fig:None}
\end{figure}

\end{document}


\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{amsthm}
\usepackage{amsfonts,amsmath, amssymb,latexsym,mathrsfs}
\usepackage[margin=1.15in]{geometry}
\usepackage{enumitem}
\setlength{\parindent}{0pt}
\usepackage{tikz-cd}
\usepackage{fancyhdr}

\theoremstyle{definition}
\newtheorem{thm}{Theorem}[section]

\newtheorem{prop}{Proposition}[section]

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

\theoremstyle{remark}
\newtheorem*{remark}{Remark}

\theoremstyle{definition}
\newtheorem{example}{Example}[section]

\theoremstyle{definition}
\newtheorem{lem}{Lemma}[section]

\theoremstyle{definition}
\newtheorem{cor}{Corollary}[section]

\date{}
\title{Chapter 8: Application of integral in real life}

\begin{document}
\maketitle

\section{Probability}
\subsection{PDF and CDF}
\begin{definition}
A function $p(x)$ is a \textbf{probability density function} or PDF if it satisfies the following conditions
\begin{itemize}
\item $p(x) \geq 0$ for all $x$.
\item $\int_{-\infty}^\infty p(x) = 1.$
\end{itemize}
\end{definition}

\begin{definition}
A function $P(t)$ is a \textbf{Cumulative Distribution Function} or cdf, of a density function $p(t)$, is defined by
\[P(t) =\int_{-\infty}^t p(x) dx \]
Which means that $P(t)$ is the antiderivative of $p(t)$ with the following properties:
\begin{itemize}
\item $P(t)$ is increasing and $0\leq P(t)\leq 1$ for all $t$.
\item $\lim_{t \to \infty}P(t)=1.$
\item $\lim_{t \to -\infty}P(t)=0.$
\end{itemize}
\end{definition}

Moreover, we have $\int_a^b p(x)dx=P(b)-P(a)$.

\subsection{Probability, mean and median}

\subsubsection*{Probability}
Let us denote $X$ to be the quantity of outcome that we care ($X$ is in fact, called the random variable).
\[\mathbb{P}\{a\leq X\leq b\}=\int_a^b p(x)dx=P(b)-P(a)\]
\[\mathbb{P}\{X\leq t\}=\int_{-\infty}^t p(x)dx=P(t)\]
\[\mathbb{P}\{X\geq s\}=\int_{s}^\infty p(x)dx=1-P(s)\]

\subsubsection*{The mean and median}
\begin{definition}
A \textbf{median} of a quantity $X$ is a value $T$ such that the probability of $X\leq T$ is $1/2$. Thus we have  $T$ is defined by the value such that
\[ \int_{-\infty}^T p(x) dx=1/2 \] or \[P(T)=1/2\].
\end{definition}
\begin{definition} A \textbf{mean} of a quantity $X$ is the value given by
	\[ Mean= \frac{\text{Probability of all possible quantity}}{\text{Total probability}}= \frac{\int_{-\infty}^{\infty}xp(x)dx}{\int_{-\infty}^{\infty}p(x)dx}=\frac{\int_{-\infty}^{\infty}xp(x)dx}{1}=\int_{-\infty}^{\infty}xp(x)dx. \]
\end{definition}

\subsubsection*{Normal Distribution}
\begin{definition}
A normal distribution has a density function of the form
\[p(x)=\frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}\] where $\mu$ is the mean of the distribution and $\sigma$ is the standard deviation, with $\sigma > 0$.
The case $\mu = 0$, $\sigma = 1$ is called the standard normal distribution.

\end{definition}
\end{document}


\section{Policy Gradient}

\subsection{Policy Approximation}

The action $\pi(a|s, \theta ) = \mathbb{P}\{ A_t=a|S_t=s,\theta_t=\theta \}$ is a random variable.

The problem is to maximize performance measure $\mathcal{J}(\theta)$:
\begin{equation}\label{maxperformancemeasure}
	\theta_{t+1} = \theta_t + \alpha \nabla \widehat{\mathcal{J}(\theta_t)}
\end{equation}

All methods that follow this schema is called \cindex{policy gradient methods}.

The benefit of \cindex{policy gradient}:
\begin{itemize}
	\item continuous action space
	\item can learn stochastic policy
	\item no maximization cost which is slow
\end{itemize}

The disadvantage of \cindex{policy gradient}:
\begin{itemize}
	\item converge to local optimum rather than global
\end{itemize}

why it is a maximization problem:
\begin{itemize}
	\item in value prediction, the policy is fixed and the value need to converge to a theoretical result. So the error need to be minimized.
	\item in value control, the optimal policy is incrementally updated. However, the optimal policy is a deterministic result of updated value function.
	\item in policy gradient, the goal is to maximize result.
\end{itemize}

In practice, in order to ensure exploration we require the policy is never deterministic, i.e., $\pi(a|s, \theta) \in (0,1)$.

If the action space is discrete and not too large, we can use \cindex{softmax} for numerical preference state-action pair $e^{h(s,a,\theta)}$:
\begin{equation}
	\pi(a|s, \theta) = \frac{e^{h(s,a,\theta)}}{\sum_b e^{h(s,a,\theta)}}
\end{equation}

The \cindex{softmax} could be used with $\varepsilon$-greedy to achieve non-deterministic policy. For some problem the best approximate policy may be stochastic.

For some problem the action value function may have simpler presentation, while for some the policy gradient is simpler.

\subsection{Policy Gradient Theorem}

The \cindex{policy gradient theorem} says:
\begin{equation}
	\nabla \mathcal{J}(\theta ) \propto \sum_s \mu(s) \sum_a q_\pi (s,a) \nabla \pi(a|s, \theta )
\end{equation}

In episodic case, the constant of proportionality is the average length of the episode. In continuous case it is $1$. The $\mu$ here is the distribution of $s$ under policy $\pi$.

The proof is:
\begin{equation}
	\begin{aligned}
		\nabla v_\pi(s) &= \nabla \left[\sum_a \pi(a|s) q_\pi(s,a) \right] \\
		&= \sum_a \Big[ \nabla \pi(a|s) q_\pi(s,a) + \pi(a|s) \nabla q_\pi(s,a) \Big] \\
		&= \sum_a \Big[ \nabla \pi(a|s) q_\pi(s,a) + \pi(a|s) \nabla \sum_{s^{'},r} p(s',r|s,a) (r + v_\pi(s'))\Big] \\
		&= \sum_a \Big[ \nabla \pi(a|s) q_\pi(s,a) + \pi(a|s)\sum_{s'} p(s'|s,a) \nabla v_\pi(s') \Big] \\
		&= \sum_a \Big[  \nabla \pi(a|s) q_\pi(s,a) + \pi(a|s) \sum_{s'} p(s'|s,a) \\
		& \ \ \ \sum_{a'}\big[ \nabla \pi(a'|s')q_\pi(s',a') + \pi(a'|s') \sum_{s''} p(s''|s',a') \nabla v_\pi(s'') \big] \Big] \\
		&= \sum_{x \in \mathcal{S}} \left( \sum_{k=0}^\infty \mathbb{P}(s \rightarrow x, k, \pi ) \right) \sum_a \nabla \pi(a|x) q_\pi(x,a)
	\end{aligned}
\end{equation}

where $\mathbb{P}(s \rightarrow x, k, \pi )$ is the probability of transition from state $s$ to state $x$ in $k$ steps under policy $\pi$. So:
\begin{equation}
	\begin{aligned}
		\nabla \mathcal{J}(\theta ) &= \nabla v_\pi(s_0) \\
		&= \sum_{s} \left( \sum_{k=0}^\infty \mathbb{P}(s_0 \rightarrow x, k, \pi ) \right) \sum_a \nabla \pi(a|x) q_\pi(x,a) \\
		&= \sum_s \eta(s) \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
		&= \sum_{s'} \eta(s') \sum_s \frac{\eta(s)}{\sum_{s'} \eta{s'}} \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
		&= \sum_{s'} \eta(s') \sum_s \eta{s} \sum_a \nabla \pi(a|s) q_\pi(s,a) \\
		&\propto \sum_s \mu(s) \sum_a \nabla \pi(a|s) q_\pi(s,a)
	\end{aligned}
\end{equation}

\subsection{REINFORCE: Monte Carlo Policy Gradient}

\cindex{REINFORCE} has \cindex{actor} but no \cindex{critic}. It use Monte Carlo method to calculate the $G$ directly without calculating the value function.

\begin{equation}
	\begin{aligned}
		\nabla \mathcal{J}(\theta ) &\propto \sum_s \mu(s) \sum_a \nabla \pi(a|s,\mathbb{\theta} ) q_\pi(s,a) \\
		&= \mathbb{E}_\pi \left[ \sum_a q_\pi(S_t,a) \nabla \pi(a|S_t,\mathbb{\theta}) \right] \\
		&= \mathbb{E}_\pi \left[ \sum_a q_\pi(S_t,a) \pi(a|S_t,\theta) \frac{\nabla \pi(a|S_t,\mathbb{\theta})}{\pi(a|S_t,\mathbb{\theta})} \right] \\
		&= \mathbb{E}_\pi \left[ q_\pi(S_t,A_t) \frac{\nabla \pi(A_t|S_t,\mathbb{\theta})}{\pi(A_t|S_t,\mathbb{\theta})} \right] \\
		&= \mathbb{E}_\pi \left[ G_t \frac{\nabla \pi(A_t|S_t,\mathbb{\theta})}{\pi(A_t|S_t,\mathbb{\theta})} \right] \\
		&= \mathbb{E}_\pi \Big[ G_t \nabla \log \pi(A_t|S_t,\mathbb{\theta}) \Big]
	\end{aligned}
\end{equation}

So formula (\ref{maxperformancemeasure}) is now:
\begin{equation}\label{policygradientexpectation}
	\begin{aligned}
		\theta_{t+1} &= \theta_t + \alpha \nabla \widehat{\mathcal{J}(\theta_t)} \\
		&= \theta_t + \alpha  G_t \nabla \log \pi(A_t|S_t,\mathbb{\theta})
	\end{aligned}
\end{equation}

Here $G_t$ is the return in Monte Carlo cases.

Algorithm (\ref{algo:reinforcemontecarlo}) contains detail.

Formula (\ref{policygradientexpectation}) has many forms:
\begin{equation}
	\begin{aligned}
		\nabla \widehat{\mathcal{J}(\theta_t )} &= \mathbb{E}_\pi \Big[ \nabla \log \pi(A_t|S_t,\mathbb{\theta}) G_t \Big] \\
		&= \mathbb{E}_\pi \Big[  \nabla \log \pi(A_t|S_t,\mathbb{\theta})v_t \Big]  \\
		&= \mathbb{E}_\pi \Big[ \nabla \log \pi(A_t|S_t,\mathbb{\theta})q_t(s,a)  \Big] \\
		&= \mathbb{E}_\pi \Big[\nabla \log \pi(A_t|S_t,\mathbb{\theta}) \delta_t  \Big]
	\end{aligned}
\end{equation}

\begin{algorithm}
	\caption{REINFORE: Monte Carlo control for $\pi_*$}\label{algo:reinforcemontecarlo}

	\begin{algorithmic}[1]
		\State $ \theta \in \mathbb{R}^d $
		\State $\gamma$: the discount rate

		\Statex

		\Loop
			\State generate episode $S_0,A_0,R_1,\dots,S_{T-1},A_{T-1},R_T$ following $\pi(\cdot|\cdot, \theta)$
			\For{$t \in [0,1,\dots,T-1]$}
				\State $G \gets \sum_{k=t+1}^T \gamma^{k-t-1} R_k$
				\State $\theta \gets \theta + \alpha \gamma^t G \nabla \log \pi(A_t|S_t,\theta) $
			\EndFor
		\EndLoop
	\end{algorithmic}
\end{algorithm}

\subsection{Baseline}

A \cindex{baseline} is an arbitrary function $b(s)$ that does not vary with $a$:
\begin{equation}
	\nabla \mathcal{J}(\theta ) \propto \sum_s \mu(s) \sum_a \nabla \pi(a|s,\mathbb{\theta} ) \Big( q_\pi(s,a) - b(s)\Big)
\end{equation}

It is ok to add \cindex{baseline} because it has no effect:
\begin{equation}
	\begin{aligned}
		\sum_a b(s)\nabla \pi(a|s,\theta) &= b(s) \nabla \sum_a \pi(a|s,\theta) \\
		&= b(s) \nabla 1 \\
		&= 0
	\end{aligned}
\end{equation}

So the REINFORCE update now becomes:
\begin{equation}
	\theta_{t+1} = \theta_t + \alpha \Big( G_t - b(S_t) \Big) \nabla \log \pi(A_t|S_t,\mathbb{\theta})
\end{equation}

One good choice of $b(s)$ is the state value $\widehat{v}(S_t,\mathbf{W})$. It is called \cindex{advantage function}:

\begin{equation}
	A(s,a) = Q(s,a) - V(s)
\end{equation}

For optimal $A^*$, the \cindex{advantage function} becomes:
\begin{equation}
	\begin{aligned}
		A^*(s,a) &= Q^*(s,a) - V^*(s)\\
		&= \begin{cases}
			0 & \text{, if } a = a^* \\
			<0 & \text{, if } a \neq a^*
		\end{cases}
	\end{aligned}
\end{equation}

Because REINFORCE is a Monte Carlo learning algorithm, it is natural to learn $\widehat{v}$ using Monte Carlo as well.

Algorithm (\ref{algo:reinforcemontecarlowithbase}) contains detail. In algorithm, in linear case $\alpha^\mathbf{W} = \frac{0.1}{\mathbb{E} \big[ \| \nabla \widehat{v}(S_t,\mathbf{W}) \|_\mu^2 \big]}$, while the best value of $\alpha^\theta$  depends on the problem.

\begin{algorithm}
	\caption{REINFORE with baseline for $\pi_*$}\label{algo:reinforcemontecarlowithbase}

	\begin{algorithmic}[1]
		\State $ \theta \in \mathbb{R}^d $ such as $0$
		\State $\gamma$: the discount rate
		\State $\alpha^\theta > 0$
		\State $\alpha^{\mathbf{W}} > 0$

		\Statex

		\Loop
			\State generate episode $S_0,A_0,R_1,\dots,S_{T-1},A_{T-1},R_T$ following $\pi(\cdot|\cdot, \theta)$
			\For{$t \in [0,1,\dots,T-1]$}
				\State $G \gets \sum_{k=t+1}^T \gamma^{k-t-1} R_k$
				\State $\delta \gets G - \widehat{v}(S_t, \mathbf{W})$
				\State $\mathbf{W} \gets \mathbf{W} + \alpha^{\mathbf{W}} \gamma^t \delta \nabla \widehat{v}(S_t, \mathbf{W}) $ \Comment{learn $\widehat{v}$ by TD(0) }
				\State $\theta \gets \theta + \alpha^\theta \gamma^t G \nabla \log \pi(A_t|S_t,\theta) $
			\EndFor
		\EndLoop
	\end{algorithmic}
\end{algorithm}

\subsection{Actor-Critic Methods}

In \cindex{actor-critic methods}, \cindex{actor} refers to learned policy and \cindex{critic} refers to learned value function, usually a state-value function:
\begin{description}
	\item [actor] update $Q$ by policy gradient
	\item [critic] update $w$ by TD(0)
\end{description}

In REINFORCE with baseline case it only calculate policy, but not value function. And it is not a bootstrapping method. Bootstrapping is good because it reduce variance and accelerate learning speed, although it is biased.

\cindex{actor} learning alone is slow, so need the help of \cindex{critic}.

The change is to use $G_{t:t+1}$ instead of $G_t$:

\begin{equation}
	\begin{aligned}
		\theta_{t+1} &= \theta_t + \alpha \Big( G_{t:t+1} - b(S_t) \Big) \nabla \log \pi(A_t|S_t,\mathbb{\theta}) \\
		&= \theta_t + \alpha \Big( R_{t+1} + \gamma \widehat{v}(S_{t+1},\mathbf{W}) - b(S_t) \Big) \nabla \log \pi(A_t|S_t,\mathbb{\theta})
	\end{aligned}
\end{equation}

The forward algorithm is (\ref{algo:actorcriticforward}) and the backward algorithm is (\ref{algo:actorcriticbackward}).

\begin{algorithm}
	\caption{one-step Actor-Critic forward view for $\pi_*$}\label{algo:actorcriticforward}

	\begin{algorithmic}[1]
		\State $ \theta \in \mathbb{R}^d $ such as $0$
		\State $\gamma$: the discount rate
		\State $\alpha^\theta > 0$
		\State $\alpha^{\mathbf{W}} > 0$

		\Statex

		\Loop
			\State initialize $S$
			\State $I \gets 1$
			\Repeat
				\State $A \sim \pi(\cdot|S,\theta)$
				\State take action $A$, observe $S', R$
				\State $\delta \gets R + \gamma \widehat{v}(S',\mathbf{W}) - \widehat{v}(S,\mathbf{W})$ \Comment{$\widehat{v}(S',\mathbf{W})=0$ if $S'$ is terminal}
				\State $\mathbf{W} \gets \mathbf{W} + \alpha^{\mathbf{W}} I \delta \nabla \widehat{v}(S, \mathbf{W}) $
				\State $\theta \gets \theta + \alpha^\theta I \delta \nabla \log \pi(A|S,\theta) $
				\State $I \gets \gamma I$
				\State $S \gets S'$
			\Until{$S$ is terminal}
		\EndLoop
	\end{algorithmic}
\end{algorithm}

\begin{algorithm}
	\caption{Actor-Critic with eligibility trace, backward view for $\pi_*$}\label{algo:actorcriticbackward}

	\begin{algorithmic}[1]
		\State $ \theta \in \mathbb{R}^d $ such as $0$
		\State $\gamma$: the discount rate
		\State $\alpha^\theta > 0$
		\State $\alpha^{\mathbf{W}} > 0$

		\Statex

		\Loop
			\State initialize $S$
			\State $I \gets 1$
			\Repeat
				\State $A \sim \pi(\cdot|S,\theta)$
				\State take action $A$, observe $S', R$
				\State $\delta \gets R + \gamma \widehat{v}(S',\mathbf{W}) - \widehat{v}(S,\mathbf{W})$ \Comment{$\widehat{v}(S',\mathbf{W})=0$ if $S'$ is terminal}
				\State $z^\mathbf{W} \gets \gamma \lambda^\theta z^\mathbf{W} + I \nabla \widehat{v}(S,\mathbf{W})$
				\State $z^\theta \gets \gamma \lambda^\theta z^\theta + I \nabla \log \pi(A|S, \theta)$
				\State $\mathbf{W} \gets \mathbf{W} + \alpha^\mathbf{W} \delta z^\mathbf{W}$
				\State $\theta \gets \theta + \alpha^\theta \delta z^\theta$
				\State $I \gets \gamma I$
				\State $S \gets S'$
			\Until{$S$ is terminal}
		\EndLoop
	\end{algorithmic}
\end{algorithm}


\section{Concept symbols}

\begin{itemize}
\item [$A$] wave amplitude, area
\item [$\vec{a}$] acceleration
\item [$c$] speed of light
\item [$D$] distance, diameter
\item [$d$] distance, diameter, depth, differential (e.g. $dx$)
\item [$E$] energy
\item [$\vec{F}$] force
\item [$f$] (linear) frequency, factor
\item [$G$] gravitational constant
\item [$g$] accleration due to gravity
\item [$H$] height
\item [$h$] height
\item [$I$] moment of inertia
\item [$i$] index counter
\item [$\hat{i}$] unit vector for $x$
\item [$\vec{J}$] impulse
\item [$j$] index counter
\item [$\hat{j}$] unit vector for $y$
\item [$K$] kinetic energy
\item [$k$] spring constant, restoring force constant, angular wavenumber, index counter
\item [$\hat{k}$] unit vector for $z$
\item [$L$] length
\item [$\vec{L}$] angular momentum, length
\item [$l$] length
\item [$\ell$] length
\item [$M$] mass
\item [$m$] mass
\item [$\vec{N}$] normal force
\item [$n$] antinode number
\item [$\vec{n}$] unit normal
\item [$P$] power
\item [$p$] pressure
\item [$\vec{p}$] linear momentum
\item [$q$] generic variable
\item [$\vec{q}$] generic multi-variable
\item [$R$] radius
\item [$r$] radius, radial position
\item [$\vec{r}$] position
\item [$s$] arc length
\item [$\vec{s}$] displacement
\item [$T$] period
\item [$\vec{T}$] tension
\item [$t$] time
\item [$U$] potential energy
\item [$V$] volume
\item [$\vec{v}$] velocity
\item [$W$] work
\item [$w$] weight, width
\item [$x$] position coordinate 1
\item [$y$] position coordinate 2
\item [$z$] position coordinate 3
\item [$\alpha$] angular acceleration, generic angle
\item [$\beta$] generic angle
\item [$\Delta$] coarse change (e.g. $\Delta x$)
\item [$\delta$] uncertainty, small change
\item [$\epsilon$] strain, small value
\item [$\theta$] generic angle
\item [$\kappa$] restoring torque constant
\item [$\lambda$] wavelength, linear mass density
\item [$\mu$] coefficient of friction, linear mass density
\item [$\rho$] volumetric mass density
\item [$\sigma$] areal mass density
\item [$\vec{\tau}$] torque
\item [$\phi$] phase angle, generic angle
\item [$\omega$] angular frequency
\item [$\vec{\Omega}$] precession velocity
\item [$\vec{\omega}$] angular velocity
\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Master Thesis in Mathematics
% "Immersions and Stiefel-Whitney classes of Manifolds"
% -- Chapter 0: Introduction --
%
% Author: Gesina Schwalbe
% Supervisor: Georgios Raptis
% University of Regensburg 2018
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Introduction}
% a survey of literature with comments related to the current research questions
% popular presentation of project and its results
% involved fields/theories, preliminary knowledge, notation
%Timeline:
%\begin{description}
%\item[1944] Whitney's immersion theorem \cite{whitneyimmersiontheorem}
%\item[1950] Wu's theorem \cite{wu}
%\item[1954] Structure of the cobordism ring by Thom \cite{thomfrench}
%\item[1959] Hirsch-Smale theorem by M.~W.~Hirsch \cite{hirschimmersions} (PhD thesis)
%\item[1960] Massey's theorem \cite{massey}
%\item[1971] R.~L.~Brown's theorem \cite{brown}
%\item[1985] proof of immersion conjecture by R.~L.~Cohen \cite{immersionconj}
%\end{description}

Immersions are roughly speaking mappings between smooth
manifolds that locally look like embeddings, globally however
allow self-intersection, or \enquote{worse} in case the domain is not
compact.
First cornerstones of modern immersion theory are Whitney's papers on this
topic, remarkably his embedding and immersion
theorems~\cite{whitneyimmersiontheorem} published in 1944, making
immersions of certain codimension into real space always available for the
investigation of manifolds
\cite{immersiontheoryhistory,hirsch}.

Attempts to reduce the above codimension of $n-1$ for $n$ manifolds
were heated up, when a theorem of
M.~W.~Hirsch~\cite{hirschimmersions} building on important work of
Smale allowed to answer the question about the existence of an
immersion of certain codimension by the
existence of a vector bundle monomorphism into Euclidean space of
certain dimension.
Using the theory of characteristic classes the latter can again be
reformulated to the existence of a vector bundle of this certain
dimension which is dual to the tangent bundle, \idest looks
like the pullback of a normal bundle.
Amongst other valuable tooling, this brought in obstructions by
Stiefel-Whitney classes.

In 1960, Massey~\cite{massey} showed with the help of Wu's
theorem~\cite{wu} from 1950 that these obstructions for compact
$n$\nbd{}manifolds vanish for exactly all codimensions greater than
$n-\alpha(n)$, where $\alpha(n)$ is the number of ones in the binary
notation of $n$.
This motivated the conjecture that the answer to the question
\begin{quote}
    What is the minimum codimension $k$, such that for every
    $n$\nbd{}dimensional compact manifold there exists an
    immersion $M\immto\R^{n+k}$ into Euclidean space?
\end{quote}
is $k=n-\alpha(n)$, later known as the immersion conjecture
\cite{cohen}.

The guess was partly reassured in 1971 by a result of R.~L.~Brown's
investigations of the cobordism ring~\cite{brown}, proving the
conjecture up to cobordism. It heavily relies on Thom's results from
1954 on the structure of the cobordism ring \cite{thomfrench},
and its idea is to provide generators of the
cobordism ring that fulfill the immersion property, partly inspired by
constructions introduced in Dold's work on a complete generating set
of the cobordism ring published in 1956 \cite{dold}.
However, the key point prior to the construction of generators is to
find a criterion for indecomposability of cobordism classes, which is
given by an indicator characteristic class.

After a long series of steps, notably by E.~H.~Brown~Jr. and
F.~P.~Peterson beginning in 1963 (see \cite{cohen}), the proof of the
conjecture was eventually finalized in 1985 by R.~L.~Cohen, by showing
that all stable normal bundles of $n$\nbd{}manifolds factor over
$\B\Orth(n-\alpha(n))$.
Its main idea is to refine Steenrod's classification theorem for
vector bundles by finding for each $n$ a classifying space specifically
for stable normal bundles of $n$\nbd{}manifolds, over which all classifying
maps factor. It is then shown that this factorization admits a lift to
$\B\Orth(n-\alpha(n))$.
Important intermediate steps are to prove both, the existence and the
lift, up to cobordism, and then \emph{de-Thom-ify}, as they called it,
the results.
In \cite{brownpeterson} E.~H.~Brown~Jr. and F.~P.~Peterson conducted
all these steps except for de-Thom-ifying the lift,
which was done by R.~L.~Cohen in \cite{cohen}.

The focus of this thesis is on the results by Massey and
R.~L.~Brown, especially the role of characteristic classes in tackling
these problems.
The needed reformulation using the Hirsch-Smale theorem, as well as
the obstruction by characteristic classes, is given in
\autoref{chap:reformulation}.
A detailed proof of Massey's theorem follows in \autoref{chap:massey},
and a proof of Brown's theorem finalizes the efforts in
\autoref{chap:brown}.
A final, more detailed outlook on the proof of the immersion
conjecture can be found in \autoref{chap:outlook}.

The reader is assumed to be familiar with the concepts of Steenrod
squares, characteristic classes, especially Stiefel-Whitney classes,
and the unoriented cobordism ring. Those theories will merely be
recapitulated replacing a couple of proofs by references.

\section*{Notation}
If not stated otherwise, the following notation is used.
\begin{compactitemize}[leftmargin=*]
\item \enquote{Space} always means topological space, and all maps
  between spaces are continuous.
\item For $X$, $Y$ spaces, $[X,Y]$ denotes the set of homotopy classes
  of base point preserving maps $X\to Y$.
\item All manifolds are smooth and closed.
\item Cobordism always means unoriented cobordism.
\item All vector bundles are real.
\item The bundle $\trivbdl^r$ over a space $X$ is the rank-$r$ trivial
  bundle $X\times\R^r\to X$.
\item $\oplus$ denotes the usual Whitney sum of vector bundles.
\item For a fiber bundle $\xi\colon E\to B$ denote by:
  $\E\xi\coloneqq E$ the total space,
  $\B\xi\coloneqq B$ the base space,
  $p_\xi\coloneqq p$ the underlying surjection,
  $\E_b\coloneqq p^{-1}(b)$ the fiber over a point $b\in \B$,
  $0_\xi$ the zero section if it is exists, and by
  $\minuszerosec{E}\coloneqq E\setminus\Im\zerosec{\xi}$
  the total space without the zero section.
\item $\Zmod2 \coloneqq \Z/2\Z$,
  $\Orth(n)$ is the $n$\nbd{}dimensional real orthogonal group, and
  $\Orth$ their limit, \idest the group of all orthogonal matrices
  over $\R$.
\item All homology groups have $\Zmod2$ coefficients. Singular
  cohomology in degree $r$ of a pair $(X,A)$ with
  coefficients in a ring $R$ is denoted by $\H^r(X,A;R)$, with the
  short form $\H^r(X;R)\coloneqq\H^r(X,\emptyset;R)$. Relative cohomology
  of a pointed space $(X,\pt)$ is denoted by
  $\relH^r(X;R)\cong\H^r(X,\pt;R)$.
  Analogously for singular homology.
\item For pairs of spaces $(X,A)$, $(X,B)$, $R$ a unital commutative
  ring, and $i,j\in\Nat$ denote
  \begin{compactitemize}
  \item the (relative) cup-product by
    \begin{align*}
      -\cup-\colon
      H^i(X,A;R)\times H^j(X,B;R) &\longto H^{i+j}(X,A\cup B;R)\\
      (x, y) &\longmapsto x\cup y\;,
    \end{align*}
  \item and the (relative) cap-product by
    \begin{align*}
      -\cap-\colon
      H^i(X,A;R)\times H_{i+j}(X,A\cup B;R) &\longto H_j(X,B;R)\\
      (x, \alpha) &\longmapsto x\cap \alpha \eqqcolon \capped x \alpha
                    \;.
    \end{align*}
  \item For a manifold $M^n$, $\fundcl M\in\H_n(M)$ is the unique
    fundamental class and generator.
  \end{compactitemize}
\end{compactitemize}

% Nomenclature:
%\nomenclature{$H^i(-; R)$}{Singular cohomology of degree $i$ with coefficients
%  in $R$}
%\nomenclature{$K(i,G)$}{$ith$ Eilenberg-MacLane space over group $G$}
%\nomenclature{$\GL n$}{Group of $n$\nbd{}dimensional matrices over $\R$}
%\nomenclature{$\Orth(n)$}{Group of $n$\nbd{}dimensional orthogonal matrices over $\R$}
%\nomenclature{$\Orth$}{Group of orthogonal matrices over $\R$}
%\nomenclature{$\Sigma$}{Suspension of a space $X$,
%  respectively suspension isomorphism on co-/homology
%}
%\nomenclature{$\zerosec\xi$}{Zero section of a vector bundle $\xi$}
%\nomenclature{$\cap$}{cap-product $H^i(X;R)\times H_j(X;R) \to H_{j-i}(X;R)$}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "thesis"
%%% ispell-local-dictionary: "en_US"
%%% End:


\section{Conclusion}
\label{sec:conclusion}

This article introduces the ARX ciphers, which only consist of three primitives, modular
addition, intraword rotation and bitwise exclusive-or. ARX cipher is a kind of lightweight
ciphers hence is suitable for hardware that equipped with scarce computational resources.
The use of such lightweight ciphers also accelerate software performance, such as to
provide secure transmissions for IO-intensive channels. Given three primitives, it is shown
that ARX cipher can simulate all permutations (and functions) over $\mathbb{Z}_{2^n}$.
Finally, two instances, \textsc{Speck} and ChaCha are introduced. They jointly show the
practicability of ARX ciphers at both hardware and software levels.


\documentclass[t]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, array, tikz, bm, pgfplots, tcolorbox, graphicx,multirow}
\pgfplotsset{compat = 1.16}
\usepgfplotslibrary{statistics}
\usetikzlibrary{trees}

\title{Probability: AND}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
\maketitle
\end{frame}

\section{Calculate probabilities using the Multiplication Rule}

\begin{frame}{Example 1}
You flip a coin and then roll a single die. What is the probability that you flip heads {\color{blue}\textbf{and}} roll a 5?	\newline\\
\onslide<2->{
Sample space:	\newline\\
\begin{center}
\begin{tabular}{c|cccccc}
 & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} \\ \hline
\textbf{Heads} & H1 & H2 & H3 & H4 & H5 & H6 \\
\textbf{Tails} & T1 & T2 & T3 & T4 & T5 & T6
\end{tabular}
\end{center}}
\onslide<3->{\[P(\text{heads and 5}) = \frac{1}{12}\]}
\end{frame}

\begin{frame}{Multiplication Rule}
In the previous example, the probability of flipping heads was $\frac{1}{2}$	\newline\\	\pause
The probability of rolling a 5 was $\frac{1}{6}$	\newline\\	\pause
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Multiplication Rule}]
If $P(A)$ is the probability of event $A$ occurring, and $P(B)$ is the probability of event $B$ occurring, then
\[P(A\text{ and } B) = P(A) \times P(B)\]
\end{tcolorbox}
\end{frame}

\begin{frame}{Tree Diagram}
\begin{center}
\tikzstyle{level 1} = [level distance = 3cm, sibling distance = 10cm]
\tikzstyle{level 2} = [level distance = 3cm, sibling distance = 1.25cm]
\begin{tikzpicture}[scale=0.5]
\node {}
	child {node {H}
		child {node {1}}
		child {node {2}}
		child {node {3}}
		child {node {4}}
		child {node {5}}
		child {node {6}}}
	child {node {T}{
		child {node {1}}
		child {node {2}}
		child {node {3}}
		child {node {4}}
		child {node {5}}
		child {node {6}} }
		};
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Venn Diagram -- AND}
\begin{center}
\begin{tikzpicture}
\def\circleA{(2,2) circle [radius = 1.5cm]}
\def\circleB{(4,2) circle [radius = 1.5cm]}
\draw (0,0) rectangle (6,4) node [below left] {$\mathcal{S}$};
\draw \circleA;
\node at (1.25,3) {$A$};
\draw \circleB;
\node at (4.75,3) {$B$};
\begin{scope}
\clip \circleA;
\fill[red!70] \circleB;
\end{scope}
\draw [<-, >=stealth, ultra thick] (3,2) -- (3,-1) node [below] {$P(A\text{ and } B)$};
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Venn Diagram -- AND}
\begin{center}
\begin{tikzpicture}
\def\circleA{(2,2) circle [radius = 1.5cm]}
\def\circleB{(4,2) circle [radius = 1.5cm]}
\draw (0,0) rectangle (6,4) node [below left] {$\mathcal{S}$};
\draw \circleA;
\node at (1.25,3) {\textbf{coin}};
\draw \circleB;
\node at (4.75,3) {\textbf{die}};
\node at (1.25,1.5) {T};
\node at (3,2.25) {H};
\node at (3,1.75) {5};
\node at (4,1) {1};
\node at (4.75,1.25) {2};
\node at (5.25,1.75) {3};
\node at (4,2) {4};
\node at (4.5,2.25) {6};
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Mutually Exclusive Events}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Mutually Exclusive Events}]
Two events are \textbf{mutually exclusive} if they can not happen together. In other words,
\[P(A\text{ and } B) = 0\]
\end{tcolorbox}
\end{frame}

\section{Find probabilities of independent events}

\begin{frame}{Independent Events}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Independent Events}]
Two events are \textbf{independent} if the outcome of the second is not affected by the first happening.
\end{tcolorbox}
\vspace{8pt}

\onslide<2->{\[P(A \text{ and } B) = P(A) \times P(B)\]}

\onslide<3->{When selecting items from a collection, independent events often contain selections made \alert{\underline{with replacement}}.}
\end{frame}

\begin{frame}{Example 2}
A jar contains 10 blue, 12 black, and 15 red marbles.	\newline\\
(a)	\quad What is the probability of selecting a black marble, putting it back, and then selecting a blue marble?
\begin{align*}
	\onslide<2->{P(\text{black and blue}) &= \frac{12}{37} \times \frac{10}{37}} \\[8pt]
	\onslide<3->{&= \frac{120}{1,369}}
\end{align*}
\end{frame}

\begin{frame}{Example 2}

A jar contains 10 blue, 12 black, and 15 red marbles.	\newline\\
(b)	\quad What is the probability of selecting a red marble, putting it back, and then selecting another red marble?
\begin{align*}
	\onslide<2->{P(\text{red and red}) &= \frac{15}{37} \times \frac{15}{37}} \\[8pt]
	\onslide<3->{&= \frac{225}{1,369}}
\end{align*}
\end{frame}

\begin{frame}{Example 3}
A certain blood test can determine the presence of a bloodborne pathogen 97\% of the time (that is, if 100 people have the pathogen, the test will confirm true for 97 of them). If 4 people with the pathogen are given the test, find the probability that the test is accurate for all of them.
\begin{align*}
\onslide<2->{P(\text{accurate for all}) &= P(\text{first}) \cdot P(\text{second}) \cdot P(\text{third}) \cdot P(\text{fourth})} \\[6pt]
\onslide<3->{&= (0.97)(0.97)(0.97)(0.97)} \\[6pt]
\onslide<4->{&\approx 0.8853}
\end{align*}
\onslide<5->{There is about an 88.53\% chance the test is accurate for all four people.}
\end{frame}

\section{Find conditional probabilities}

\begin{frame}{Conditional Probability}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Conditional Probability}]
\textbf{Conditional probability} is found by determining a probability \emph{based on a previous event happening}.
\end{tcolorbox}
\vspace{8pt}

\onslide<2->{The notation for the probability that event $B$ occurs given that event $A$ has occurred is
\[ P(B \mid A)\]}
\onslide<3->{With conditional probability, the denominator will often be the total of something that follows the words ``if", ``suppose", or ``given that".}
\end{frame}

\begin{frame}{Example 4}
The table below lists the types and numbers of cars sold at Lemon Autos along with their ages. Find each probability.	\newline\\
\begin{center}
\begin{tabular}{c|ccccc}
					&	\textbf{0--2} & \textbf{3--5} & \textbf{6--10} & \textbf{Over 10} & \textbf{Total} \\ \hline
\textbf{Import} 	& 37 & 21 & 12 & 30 & 100 \\
\textbf{Domestic} 	& 35 & 23 & 11 & 31 & 100 \\ \hline
\textbf{Total}   	& 72 & 44 & 23 & 61 & 200
\end{tabular}
\end{center}
(a) If a domestic car is randomly selected, what is the probability that it is 6--10 years old?
\begin{align*}
\onslide<2->{P(\text{6--10 years old $|$ it is a domestic car}) &= \frac{11}{100}}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
\begin{center}
\begin{tabular}{c|ccccc}
					&	\textbf{0--2} & \textbf{3--5} & \textbf{6--10} & \textbf{Over 10} & \textbf{Total} \\ \hline
\textbf{Import} 	& 37 & 21 & 12 & 30 & 100 \\
\textbf{Domestic} 	& 35 & 23 & 11 & 31 & 100 \\ \hline
\textbf{Total}   	& 72 & 44 & 23 & 61 & 200
\end{tabular}
\end{center}
(b) What is the probability of selecting a domestic car given that the car is 6--10 years old?
\begin{align*}
\onslide<2->{P(\text{domestic car $|$ it is 6--10 years old}) &= \frac{11}{23}}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
\begin{center}
\begin{tabular}{c|ccccc}
					&	\textbf{0--2} & \textbf{3--5} & \textbf{6--10} & \textbf{Over 10} & \textbf{Total} \\ \hline
\textbf{Import} 	& 37 & 21 & 12 & 30 & 100 \\
\textbf{Domestic} 	& 35 & 23 & 11 & 31 & 100 \\ \hline
\textbf{Total}   	& 72 & 44 & 23 & 61 & 200
\end{tabular}
\end{center}
(c) Suppose a new car is selected, what is the probability that it is an import?
\begin{align*}
\onslide<2->{P(\text{import $|$ it is 0--2 years old}) &= \frac{37}{72}}
\end{align*}
\end{frame}

\section{Find probabilities of dependent events}

\begin{frame}{Dependent Events}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Dependent Events}]
Two events are \textbf{dependent} if the outcome of the second is affected by the first happening.
\end{tcolorbox}
\vspace{8pt}

\onslide<2->{Dependent events utilize conditional probability:}
\onslide<3->{\[P(A\text{ and } B) = P(A) \times P(B \mid A)\]}
\onslide<4->{\emph{Note}: When selecting items from a collection, dependent events often contain selections made \alert{\underline{without replacement}}.}
\end{frame}

\begin{frame}{Example 5}
You are dealt a card from a standard deck and then you are dealt another (without replacement). Find the probability that	\newline\\
(a)	\quad	The first card is an ace and the second card is a ten.
\begin{align*}
	\onslide<2->{P(\text{ace and ten}) &= P(\text{ace}) \times P(\text{ten }|\text{ ace})} \\[8pt]
	\onslide<3->{&= \frac{4}{52} \times \frac{4}{51}}	\\[8pt]
	\onslide<4->{&= \frac{4}{663} \\[8pt]
	&\approx 0.603\% }
\end{align*}
\end{frame}

\begin{frame}{Example 5}
(b) \quad The first card is an ace and the second card is an ace.
\begin{align*}
	\onslide<2->{P(\text{ace and ace}) &= P(\text{ace}) \times P(\text{ace }|\text{ ace})} \\[8pt]
	\onslide<3->{&= \frac{4}{52} \times \frac{3}{51}}	\\[8pt]
	\onslide<4->{&= \frac{1}{221} \\[8pt]
	&\approx 0.452\% }
\end{align*}
\end{frame}

\begin{frame}{Conditional Probability Revisited}
The formula for dependent events
\[P(A\text{ and } B) = P(A) \times P(B \mid A)\]
leads us to the following formula for conditional probability:
\onslide<2->{\[P(B \mid A) = \frac{P(A \text{ and } B)}{P(A)}\]}
\end{frame}

\begin{frame}{Tabular Data}
When finding \texttt{AND} probabilities using tabular data, look for the intersection of a row and column. \newline\\	\pause

Two rows (likewise, two columns) will never intersect, so their probabilities are \emph{mutually exclusive}.
\end{frame}

\begin{frame}{Example 6}
The table below lists the types and numbers of cars sold at Lemon Autos along with their ages. Find each probability.	\newline\\
\begin{center}
\begin{tabular}{c|ccccc}
					&	\textbf{0--2} & \textbf{3--5} & \textbf{6--10} & \textbf{Over 10} & \textbf{Total} \\ \hline
\textbf{Import} 	& 37 & 21 & 12 & 30 & 100 \\
\textbf{Domestic} 	& 35 & 23 & 11 & 31 & 100 \\ \hline
\textbf{Total}   	& 72 & 44 & 23 & 61 & 200
\end{tabular}
\end{center}
(a) If a car is randomly selected, what is the probability that it is a 6--10 year old import?
\begin{align*}
\onslide<2->{P(\text{6--10 years old and foreign}) &= \frac{12}{200}}	\\[8pt]
\onslide<3->{&= \frac{3}{50}}
\end{align*}
\end{frame}

\begin{frame}{Example 6}
The reasoning behind our answer for Example 6a:
\begin{align*}
P(\text{6--10 and import}) &= P(\text{6--10}) \times P(\text{import} \mid \text{6--10 years old}) \\
\onslide<2->{&= \frac{23}{200} \times \frac{12}{23}} \\[8pt]
\onslide<3->{&= \frac{12}{200}} \\[8pt]
\onslide<4->{&= \frac{3}{50}}
\end{align*}
\end{frame}

\begin{frame}{Example 6}
\begin{center}
\begin{tabular}{c|ccccc}
					&	\textbf{0--2} & \textbf{3--5} & \textbf{6--10} & \textbf{Over 10} & \textbf{Total} \\ \hline
\textbf{Import} 	& 37 & 21 & 12 & 30 & 100 \\
\textbf{Domestic} 	& 35 & 23 & 11 & 31 & 100 \\ \hline
\textbf{Total}   	& 72 & 44 & 23 & 61 & 200
\end{tabular}
\end{center}
(b) If a car is randomly selected, what is the probability that it is a domestic car that is 0--2 years old?
\begin{align*}
\onslide<2->{P(\text{0--2 years old and domestic}) &= \frac{35}{200}}	\\[8pt]
\onslide<3->{&= \frac{7}{40}}
\end{align*}
\end{frame}

\begin{frame}{Example 6}
\begin{center}
\begin{tabular}{c|ccccc}
					&	\textbf{0--2} & \textbf{3--5} & \textbf{6--10} & \textbf{Over 10} & \textbf{Total} \\ \hline
\textbf{Import} 	& 37 & 21 & 12 & 30 & 100 \\
\textbf{Domestic} 	& 35 & 23 & 11 & 31 & 100 \\ \hline
\textbf{Total}   	& 72 & 44 & 23 & 61 & 200
\end{tabular}
\end{center}
(c) If a car is randomly selected, what is the probability that it is 0--2 years and 6--10 years old?
\begin{align*}
\onslide<2->{P(\text{0--2 years and 6--10 years old}) &= \frac{0}{200}}	\\[8pt]
\onslide<3->{&= 0}
\end{align*}
\end{frame}

\end{document}


\chapter{Draw diagrams}

You can find a lot of examples here: \url{http://www.tn-home.de/TUGDD/Stuff/TikZ_final.pdf}

\section{simple}

\begin{figure}[h!]
\begin{tikzpicture}
	\path            (0,0) circle (2ex);
	\path[draw]      (1,0) circle (2ex);
	\path[fill]      (2,0) circle (2ex);
	\path[fill=yellow,draw=red] (3,0) circle (2ex);
	\path[clip]         (4,0) circle (2ex);
	\fill[color=orange] (3.5,-.5) rectangle (4.5,.5);
\end{tikzpicture}
\end{figure}

\section{inline graphic}
This is an inline graphic \tikz \draw[fill=orange] (0,0) circle (1ex);

\section{not so simple}
\begin{figure}[h!]
% Its better to put them in a figure
	\begin{center}
		\begin{tikzpicture}
		% Draws a rectangle at -2.5,2.5 with the dimensions -1.5,1.5 and prints a black Start below
		\draw [red,dashed] (-2.5,2.5) rectangle (-1.5,1.5) node [black,below] {Start};
		% Draws a thick line from -2,2 to 2,2 with in out angle of 0 and an in angle of 190 degrees
		\draw [thick] (-2,2)
		to [out=0,in=190] (2,2)
		to [out=10,in=90] (6,0)
		to [out=-90,in=30] (-2,-2);
		\draw [fill] (5,0.1) rectangle (7,-0.1) node [black,right] {Obstacle}; % Draws another rectangle
		\draw [red,fill] (-2,-2) circle [radius=0.2] node [black,below=4] {Point of interest}; % Draws a circle
		\end{tikzpicture}
		\caption{Example graphic made with tikz.}
	\end{center}
\end{figure}

\section{GNUPlot}

\begin{tikzpicture}
\begin{axis}[domain=0:100,legend pos=outer north east]
	\addplot {sin(deg(x))};
	\addplot {cos(deg(x))};
	\legend{$\sin(x)$,$\cos(x)$}
\end{axis}
\end{tikzpicture}


\documentclass{article}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

\numberwithin{equation}{section}

\newtheorem{theorem}{Theorem}
\newtheorem{proposition}{Proposition}

\begin{document}

Hello World!

To test label completion you need to compile the document first!

\section{Section 1}

\begin{theorem}
  \label{th:first}
  test
\end{theorem}

\begin{equation}
  \tag{1.0}
  f(x) = 42
  \label{eq:main-is-working}
\end{equation}

\begin{align}
  \label{eq:test1}
  a &= 1 + x \\
  \label{eq:aligned}
  b &= x^2
\end{align}

\section{Section 2}

\begin{subequations}
  \begin{align}
    \label{eq:sub1}
    a &= 1 + x \\
    \label{eq:sub2}
    b &= x^2
  \end{align}
\end{subequations}

\section{Section 2}

\begin{theorem}
  \label{th:second}
  test
\end{theorem}

\begin{proposition}
  \label{prop:first}
  test
\end{proposition}

\begin{proposition}
  \label{prop:second}
  test
\end{proposition}

\begin{figure}
  \centering
  \rule{1cm}{1cm}
  \caption{Test 1}
  \label{fig:test1}
\end{figure}

\begin{figure}
  \centering
  \rule{1cm}{1cm}
  \caption{Test 2}
  \label{fig:test2}
\end{figure}

\end{document}


% !TEX root = index.tex

\section{Appendix: Fundamental Forms}

Now for some hardcore differential geometry. We learned to compute curvatures for curvatures in the special case when the surface is the graph of a function and the point is a critical point. This is not a practical way to compute curvatures and we need a more ``coordinate independent'' way of doing this. This is achieved using the so called \textbf{fundamental forms}.

\textbf{Curves:} We did a similar thing for curves, we first found the curvature as acceleration if the speed is constant and then discovered that ...

First we start with parametrizing surfaces. We'll describe a surface $ S$ as the image of a function
\begin{align*}
  \Phi: \R^2_{u,v} \rightarrow \R^3
\end{align*}
The domain can be an open subset of $ \R^2$. For example,

{\color{red} \textbf{ examples of parametrizations }}.

If you unravel the definition of a derivative then you'll see that the vectors
\begin{align}
  \Phi_{u}, \Phi_{v}
\end{align}
are tangent to the surface $ S$. We can find the \textbf{unit normal vector} to the surface geometrically or using the formula
\begin{align}
  \vec{\mathbf{n}} = \dfrac{\Phi_{u} \times \Phi_{v}}{||\Phi_{u} \times \Phi_{v}||}
\end{align}

Now we can define the fundamental forms:

\subsection{First Fundamental Form}
  The first fundamental form is defined as
  \begin{align}
    \mathbb{F} :=
    \begin{bmatrix}
      \Phi_u \cdot \Phi_u & \Phi_u \cdot \Phi_v \\
      \Phi_u \cdot \Phi_v & \Phi_v \cdot \Phi_v
    \end{bmatrix}signed
  \end{align}
  If you know inner products, this is the inner product {\color{red} \textbf{ ... }}

  \subsection{Second Fundamental Form}
  The second fundamental form is defined as:
  \begin{align}
    \mathbb{S} :=
    \begin{bmatrix}
      \Phi_{uu} \cdot \vec{\mathbf{n}} & \Phi_{uv} \cdot \vec{\mathbf{n}} \\
      \Phi_{uv} \cdot \vec{\mathbf{n}} & \Phi_{vv} \cdot \vec{\mathbf{n}}
    \end{bmatrix}
  \end{align}

  The curvatures are now given by
  \begin{align}
    H &= \tr{\mathbb{S} \mathbb{F}^{-1}}/2 \\
    \kappa &= \det{\mathbb{S} \mathbb{F}^{-1}}
  \end{align}

Unfortunately, the principal directions are harder to find using this definition, but the principal curvatures themselves are easily found from the Mean and Gaussian curvatures.

\subsection{Gaussian Curvature using Fundamental Forms}

\iffalse
\section{Examples}

\subsection{Sphere}
    \begin{align}
      x^2 + y^2 + z^2 &= r^2
    \end{align}

  Parametrization:
  \begin{align}
    z &= r \cos \phi\\
    x &= r \cos \theta \sin \phi\\
    y &= r \sin \theta \sin \phi
  \end{align}

  First Derivatives:
  \begin{align}
    \Phi_{\theta} =
    \begin{bmatrix}
      0 \\
      -r \sin \theta \sin \phi \\
      r \cos \theta \sin \phi
    \end{bmatrix}
    &&
    \Phi_{\phi} =
    \begin{bmatrix}
      -r \sin \phi \\
      r \cos \theta \cos \phi \\
      r \sin \theta \cos \phi
    \end{bmatrix}
  \end{align}

  First Fundamental Form:
  \begin{align}
    \Phi_{\theta}.\Phi_{\theta} &= r^2 \sin^2 \phi \\
    \Phi_{\theta}.\Phi_{\phi} &= 0 \\
    \Phi_{\phi}.\Phi_{\phi} &= r^2
  \end{align}

  Normal vector:
  \begin{align}
    \vec n = \begin{bmatrix} \cos \phi\\
    \cos \theta \sin \phi\\
    \sin \theta \sin \phi \end{bmatrix}
  \end{align}

  Second Derivatives:
  \begin{align}
    \Phi_{\theta \theta} =
    \begin{bmatrix}
      0 \\
      -r \cos \theta \sin \phi \\
      -r \sin \theta \sin \phi
    \end{bmatrix}
    &&
    \Phi_{\phi \theta} =
    \begin{bmatrix}
      0 \\
      -r \sin \theta \cos \phi \\
      r \cos \theta \cos \phi
    \end{bmatrix}
    &&
    \Phi_{\phi \phi} =
    \begin{bmatrix}
      -r \cos \phi \\
      -r \cos \theta \sin \phi \\
      -r \sin \theta \sin \phi
    \end{bmatrix}
  \end{align}

  Second Fundamental form:
  \begin{align}
    \Phi_{\theta \theta}.\vec n &= -r \cos^2 \phi \\
    \Phi_{\theta \phi}.\vec n &= 0 \\
    \Phi_{\phi \phi}.\vec n &= -r
  \end{align}
\fi