%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Fault System}
\label{Fault System}
The \class{FaultSystem} class provides an easy-to-use interface to handle 2D
and 3D fault systems\index{faults} as used for instance in simulating fault
ruptures. The main purpose of the class is to provide a parameterization of
an individual fault in the system of faults.
In case of a 2D fault the fault is parameterized by a single value $w_{0}$ and
in the case of a 3D fault two parameters $w_{0}$ and $w_{1}$ are used.
This parameterization can be used to impose data (e.g. a slip distribution)
onto the fault. It can also be a useful tool to visualize or analyze the
results on the fault if the fault is not straight.

\begin{figure}
\centering
\includegraphics{FaultSystem2D}
\caption{\label{FAULTSYSTEM2D}Two dimensional fault system with one fault
named `t` in the $(x_{0},x_{1})$ space and its parameterization in the
$w_{0}$ space. The fault has three segments.}
\end{figure}

A fault $t$ in the fault system is represented by a starting point $V^{t0}$
and series of directions, called strikes\index{strike}, and the lengths $(l^{ti})$.
The strike of segment $i$ is defined by the angle $\sigma^{ti}$ between the
$x_{0}$-axis and the direction of the fault, see Figure~\ref{FAULTSYSTEM2D}.
The length and strike defines the polyline $(V^{ti})$ of the fault by
\begin{equation}
V^{ti} = V^{t(i-1)} +
l^{ti} \cdot  S^{ti}
\mbox{ with }
S^{ti} =
\left[
\begin{array}{c}
 cos(\sigma^{ti})  \\
 sin(\sigma^{ti}) \\
 0
\end{array}
\right]
\label{eq:fault 00}
\end{equation}
In the 3D case each fault segment $i$ has an additional dip\index{dip}
$\theta^{ti}$ and at each vertex $i$ a depth $\delta^{ti}$ is given.
The fault segment normal $n^{ti}$ is given by
\begin{equation}
n^{ti} =
\left[
\begin{array}{c}
 -sin(\theta^{ti}) \cdot S^{ti}_{1} \\
 sin(\theta^{ti}) \cdot S^{ti}_{0} \\
 cos(\theta^{ti})
\end{array}
\right]
\label{eq:fault 0}
\end{equation}
At each vertex we define a depth vector $d^{ti}$ defined as the intersect of
the fault planes of segment $(i-1)$ and $i$ where for the first segment and
last segment the vector orthogonal to strike vector $S^{ti}$\index{strike}
and the segment normal $n^{ti}$ is used. The direction $\tilde{d}^{ti}$ of the
depth vector is given as
\begin{equation}
\tilde{d}^{ti} = n^{ti} \times n^{t(i-1)}
\label{eq:fault b}
\end{equation}
If $\tilde{d}^{ti}$ is zero the strike vectors $L^{t(i-1)}$ and $L^{ti}$ are
collinear and we can set $\tilde{d}^{ti} = l^{ti} \times n^{ti}$.
If the two fault segments are almost orthogonal $\tilde{d}^{ti}$ is pointing
in the direction of $L^{t(i-1)}$ and $L^{ti}$. In this case no depth can be
defined. So we will reject a fault system if
\begin{equation}
min(\| \tilde{d}^{ti}  \times  L^{t(i-1)} \|,\| \tilde{d}^{ti}  \times  L^{ti} \|)
\le 0.1 \cdot \| \tilde{d}^{ti} |
\label{eq:fault c}
\end{equation}
which corresponds to an angle of less than $10^o$ between the depth vector and
the strike. We then set
\begin{equation}
d^{ti}=\delta^{ti} \cdot \frac{\tilde{d}^{ti}}{\|\tilde{d}^{ti}\|}
\label{eq:fault d}
\end{equation}
We can then define the polyline $(v^{ti})$ for the bottom of the fault as
\begin{equation}
v^{ti}= V^{ti}+d^{ti}
\label{eq:fault e}
\end{equation}
In order to simplify working on a fault $t$ in a fault system a
parameterization $P^t: (w_{0},w_{1}) \rightarrow (x_{0},x_{1},x_{2})$ over a
rectangular domain is introduced such that
\begin{equation}
0\le w_{0} \le w^t_{0 max} \mbox{ and }  -w^t_{1max}\le w_{1} \le 0
\label{eq:fault 1}
\end{equation}
with positive numbers $w^t_{0 max}$ and $w^t_{1 max}$. Typically one chooses
$w^t_{0 max}$ to be the unrolled length of the fault and $w^t_{1 max}$ to be
the mean value of segment depth. Moreover we have
\begin{equation}
P^t(W^{ti})=V^{ti}\mbox{ and } P^t(w^{ti})=v^{ti}\
\label{eq:fault 2}
\end{equation}
where
\begin{equation}
W^{ti}=(\Omega^{ti},0) \mbox{ and } w^{ti}=(\Omega^{ti},-w^t_{1 max})
\label{eq:fault 3}
\end{equation}
and $\Omega^{ti}$ is the unrolled distance of $W^{ti}$ from $W^{t0}$, i.e.
$l^{ti}=\Omega^{t(i+1)}-\Omega^{ti}$. In the 2D case $w^t_{1 max}$ is set to
zero and therefore the second component is dropped, see Figure~\ref{FAULTSYSTEM2D}.

In the 2D case the parameterization $P^t$ is constructed as follows:
The line connecting $V^{t(i-1)}$ and $V^{ti}$ is given by
\begin{equation}
x=V^{ti} + s  \cdot  ( V^{t(i+1)}- V^{ti} )
\label{eq:2D line 1}
\end{equation}
where $s$ is between $0$ and $1$. The point $x$ is on $i$-th fault segment if
and only if such an $s$ exists. Assuming $x$ is on the fault it can be
calculated as
\begin{equation}
s = \frac{ (x- V^{ti})^t \cdot (V^{t(i+1)}- V^{ti}) }{ \|V^{t(i+1)}- V^{ti}\|^2}
\label{eq:2D line 1b}
\end{equation}
We then can set
\begin{equation}
w_{0}=\Omega^{ti}+s \cdot (\Omega^{ti}-\Omega^{t(i-1)})
\label{eq:2D line 2}
\end{equation}
to get $P^t(w_{0})=x$.
It remains the question if the given $x$ is actually on the segment $i$ of
fault $t$. To test this $s$ is restricted between $0$ and $1$ (so if $s<0$, $s$
is set to $0$ and if $s>1$, $s$ is set to $1$) and then we check the residual
of \eqn{eq:2D line 1}, i.e. $x$ has been accepted to be in the segment if
\begin{equation}
\|x-V^{ti} - s \cdot (V^{t(i+1)}- V^{ti}) \| \le tol \cdot
max(l^{ti}, \|x-V^{ti} \|)
\label{eq:2D line 3}
\end{equation}
where $tol$ is a given tolerance.

In the 3D case the situation is a bit more complicated: we split the fault
segment across the diagonal $V^{ti}$-$v^{t(i+1)}$ to produce two triangles.
In the upper triangle we use the parameterization
\begin{equation}
x= V^{ti} + s \cdot (V^{t(i+1)}-V^{ti})  + r \cdot (v^{t(i+1)}-V^{t(i+1)})
\mbox{ with } r \le s;
\label{eq:2D line 4}
\end{equation}
while in the lower triangle we use
\begin{equation}
x= V^{ti} +  s \cdot (v^{t(i+1)}-v^{ti}) + r \cdot (v^{ti}-V^{ti})
\mbox{ with } s \le r;
\label{eq:2D line 4b}
\end{equation}
where $0\le s,r \le 1$. Both equations are solved in the least-squares sense
e.g. using the Moore-Penrose pseudo-inverse for the coefficient matrices.
The resulting $s$ and $r$ are then restricted to the unit square. Similar to
the 2D case (see \eqn{eq:2D line 3}) we identify $x$ to be in the upper
triangle of the segment if
\begin{equation}
\|x- V^{ti} - s \cdot (V^{t(i+1)}-V^{ti})  - r \cdot (v^{t(i+1)}-V^{t(i+1)}) \|
\le tol \cdot  max(\|x-V^{ti} \|,\|v^{t(i+1)}-V^{t(i)})\|)
\label{eq:2D line 4c}
\end{equation}
and in the lower part
\begin{equation}
\|x-V^{ti} -  s \cdot (v^{t(i+1)}-v^{ti}) - r \cdot (v^{ti}-V^{ti}) \|
\le tol \cdot  max(\|x-V^{ti} \|,\|v^{t(i+1)}-V^{t(i)})\|)
\label{eq:2D line 4d}
\end{equation}
after the restriction of $(s,t)$ to the unit square.
Note that $\|v^{t(i+1)}-V^{t(i)})\|$ is the length of the diagonal of the
fault segment. For those $x$ which have been located in the $i$-th segment we
then set
\begin{equation}
w_{0}=\Omega^{ti}+s \cdot (\Omega^{ti}-\Omega^{t(i-1)})
\mbox{ and }
w_{1}=w^t_{1max} (r-1)
\label{eq:2D line 5}
\end{equation}

\subsection{Functions}

\begin{classdesc}{FaultSystem}{\optional{dim =3}}
creates a fault system in the \var{dim} dimensional space.
\end{classdesc}

\begin{methoddesc}[FaultSystem]{getMediumDepth}{tag}
returns the medium depth of fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getTags}{}
returns a list of the tags used by the fault system.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getStart}{tag}
returns the starting point of fault \var{tag} as a \numpyNDA object.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getDim}{}
returns the spatial dimension.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getDepths}{tag}
returns the list of the depths of the segments in fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getTopPolyline}{tag}
returns the polyline used to describe the fault tagged by \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getStrikes}{tag}
returns the list of strikes $\sigma^{ti}$ of the segments in fault
$t=$\var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getStrikeVectors}{tag}
returns the strike vectors $S^{ti}$ of fault $t=$\var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getLengths}{tag}
returns the lengths $l^{ti}$ of the segments in fault $t=$\var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getTotalLength}{tag}
returns the total unrolled length of fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getDips}{tag}
returns the list of the dips of the segments in fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getBottomPolyline}{tag}
returns the list of the vertices defining the bottom of the fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getSegmentNormals}{tag}
returns the list of the normals of the segments in fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getDepthVectors}{tag}
returns the list of the depth vectors $d^{ti}$ for fault $t=$\var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getDepths}{tag}
returns the list of the depths of the segments in fault \var{tag}.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getW0Range}{tag}
returns the range of the parameterization in $w_{0}$.
For tag $t$ this is the pair $(\Omega^{t0},\Omega^{tn})$ where $n$ is the
number of segments in the fault.
In most cases one has $(\Omega^{t0},\Omega^{tn})=(0,w^t_{0 max})$.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getW1Range}{tag}
returns the range of the parameterization in  $w_{1}$.
For tag $t$ this is the pair $(-w^t_{1max},0)$.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getW0Offsets}{tag}
returns the offsets for the parameterization of fault \var{tag}.
For tag \var{tag}=$t$ this is the list $[\Omega^{ti}]$.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getCenterOnSurface}{}
returns the center point of the fault system at the surfaces.
In 3D the calculation of the center is considering the top edge of the faults
and projects the edge to the surface (the $x_{2}$ component is assumed to be
0). An \numpyNDA object is returned.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getOrientationOnSurface}{}
returns the orientation of the fault system in RAD on the surface
($x_{2}=0$ plane) around the fault system center.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{transform}{\optional{rot=0, \optional{shift=numpy.zeros((3,)}}}
applies a shift \var{shift} and a consecutive rotation in the $x_{2}=0$ plane.
\var{rot} is a float number and \var{shift} an \numpyNDA object.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getMaxValue}{f\optional{, tol=1.e-8}}
returns the tag of the fault where \var{f} takes the maximum value and a
\class{Locator} object which can be used to collect values from \Data objects
at the location where the maximum is taken, e.g.
\begin{python}
       fs=FaultSystem()
       f=Scalar(..)
       t, loc=fs.getMaxValue(f)
       print("maximum value of f on the fault %s is %s at location %s."%(t, \
             loc(f), loc.getX()))
\end{python}
\var{f} must be a \Scalar. When the maximum is calculated only
\DataSamplePoints are considered which are on a fault in the fault system in
the sense of condition~\ref{eq:2D line 3} or \ref{eq:2D line 4d}, respectively.
In the case no \DataSamplePoints are found the returned tag is \var{None} and
the maximum value as well as the location of the maximum value are undefined.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getMinValue}{f\optional{, tol=1.e-8}}
returns the tag of the fault where \var{f} takes the minimum value and a
\class{Locator} object which can be used to collect values from \Data objects
at the location where the minimum is taken, e.g.
\begin{python}
  fs=FaultSystem()
  f=Scalar(..)
  t, loc=fs.getMinValue(f)
  print("minimum value of f on the fault %s is %s at location."%\
      (t,loc(f),loc.getX()))
\end{python}
\var{f} must be a \Scalar. When the minimum is calculated only
\DataSamplePoints are considered which are on a fault in the fault system in
the sense of condition~\ref{eq:2D line 3} or \ref{eq:2D line 4d}, respectively.
In the case no \DataSamplePoints are found the returned tag is \var{None} and
the minimum value as well as the location of the minimum value are undefined.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getParametrization}{x,tag \optional{\optional{, tol=1.e-8}, outsider=None}}
returns the argument $w$ of the parameterization $P^t$ for \var{tag}=$t$ to
provide \var{x} together with a mask indicating where the given location if on
a fault in the fault system by the value $1$ (otherwise the value is set to $0$).
\var{x} needs to be a \Vector or \numpyNDA object.
\var{tol} defines the tolerance to decide if given \DataSamplePoints are on
fault \var{tag}. The value \var{outside} is the value used as a replacement
value for $w$ where the corresponding value in \var{x} is not on a fault.
If \var{outside} is not present an appropriate value is used.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getSideAndDistance}{x,tag}
returns the side and the distance at locations \var{x} from the fault \var{tag}.
\var{x} needs to be a \Vector or \numpyNDA object.
Positive values for side means that the corresponding location is to the right
of the fault, a negative value means that the corresponding location is
to the left of the fault. The value zero means that the side is undefined.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{getFaultSegments}{tag}
returns the polylines used to describe fault \var{tag}. For \var{tag}=$t$ this
is the list of the vertices $[V^{ti}]$ for the 2D and the pair of lists of the
top vertices $[V^{ti}]$ and the bottom vertices $[v^{ti}]$ in 3D.
Note that the coordinates are represented as \numpyNDA objects.
\end{methoddesc}

\begin{methoddesc}[FaultSystem]{addFault}{
strikes\optional{,
ls\optional{,
V0=[0.,0.,0.]\optional{,
tag=None\optional{,
dips=None\optional{,
depths= None\optional{,
w0_offsets=None\optional{,
w1_max=None}}}}}}}}
adds the fault \var{tag} to the fault system.
\var{V0} defines the start point of fault named $t=$\var{tag}.
The polyline defining the fault segments on the surface are set by the strike
angles \var{strikes} (=$\sigma^{ti}$, north = $\pi/2$, the orientation is
counterclockwise.) and the length \var{ls} (=$l^{ti}$).
In the 3D case one also needs to define the dip angles \var{dips}
(=$\delta^{ti}$, vertical=$0$, right-hand rule applies.) and the depth
\var{depths} for each segment.
\var{w1_max} defines the range of $w_{1}$.
If not present the mean value over the depth of all segment edges in the fault
is used.
\var{w0_offsets} sets the offsets $\Omega^{ti}$. If not present it is chosen
such that $\Omega^{ti}-\Omega^{t(i-1)}$ is the length of the $i$-th segment.
In some cases, e.g. when kinks in the fault are relevant, it can be useful
to explicitly specify the offsets in order to simplify the assignment of values.
\end{methoddesc}

\subsection{Example}
See \Sec{Slip CHAP}.


\chapterimage{head2.png} % Chapter heading image
\chapter{Bayesian Inference Framework 3}
\section{Four spaces}
\begin{center}
    \includegraphics[scale=0.4]{ch8/fourspaces.pdf}
\end{center}

\section{Photon Operator and Time Evolution}
\begin{definition}[Cartesian Space: Single Photon]
Given $p(x_1)$, and if we observe $y_1$
\begin{center}
    \includegraphics[scale=0.7]{ch8/singlephoton_x.pdf}
\end{center}
Actually, $p(y_1|x_1)$ is a matrix
\begin{equation}
    p(y_1(j)|x_1(i)) =
    \begin{bmatrix}
    p(y_1(1)|x_1(1)) & p(y_1(2)|x_1(1)) & \cdots & p(y_1(N)|x_1(1)) \\
    p(y_1(1)|x_1(2)) & p(y_1(2)|x_1(2)) & \cdots & p(y_1(N)|x_1(2)) \\
    \vdots & \vdots & \cdots & \vdots \\
    p(y_1(1)|x_1(N)) & p(y_1(2)|x_1(N)) & \cdots & p(y_1(N)|x_1(N))
    \end{bmatrix}
\end{equation}
\begin{center}
    \includegraphics[scale=0.4]{ch8/single_photon_x_2.pdf}
\end{center}
\end{definition}

\begin{definition}[Build Photon Matrix $\left<\psi_i|\textbf{y}|\psi_j\right>$]
\begin{center}
    \includegraphics[scale=0.4]{ch8/build_photon_mat.pdf}
\end{center}
\end{definition}

\begin{definition}[Eigenspace $\rho$: Single Photon]
We can express $\rho(x_1)$ as the linear combination of eigenfunctions, which fully determined by $p_{\rm{eq}}(x)$
\begin{equation}
    \rho(x_1) = \sum_{k=1}^{N_v} a_k \psi_{k}(x)
\end{equation}
Therefore, in eigenspace, we have a vector:
\begin{equation}
    \left< \rho_1 \right| = \begin{bmatrix}
        a_1 & a_2 & \cdots & a_{N_v}
    \end{bmatrix}
\end{equation}
Note that $a_1=\pm 1$ because
\begin{equation}
    a_1 = \int \rho(x_1)\psi_1(x_1)dx_1 = \int \frac{p(x_1)}{\rho_{\text{eq}}(x_1)}\psi_1(x_1) dx_1 = \pm \int p(x_1) dx_1 = \pm 1
\end{equation}
\begin{center}
    \includegraphics[scale=0.7]{ch8/singlephoton_s.pdf}
\end{center}
Note that
\begin{align}
    p(y_1) &= \left< \rho_1|\textbf{y}_1|\psi_1 \right> = \int \frac{p(x_1,y_1)}{\rho_{\text{eq}}(x_1)} \psi_1(x_1) dx_1 \\
    \left<\hat{\alpha}_1|\psi_1\right> &= \int \frac{p(x_1|y_1)}{\rho_{\text{eq}}(x_1)} \psi_1(x_1) dx_1 = \pm 1
\end{align}
\begin{center}
    \includegraphics[scale=0.35]{ch8/rho_y1.pdf}
\end{center}
\end{definition}

\begin{definition}[Cartesian: Time Evolution]
\begin{align}
    \frac{p(x_1, x_2, y_1)}{\rho_{\text{eq}}(x)} &= \rho(x_1, x_2, y_1) = \sum_{k=1}^{N_v} \left< \alpha_1|\psi_k \right> e^{-D \lambda'_k \Delta t} \psi_k(x_2) \\
    \frac{p(x_2 | x_1, y_1)}{\rho_{\text{eq}}(x)} &= \rho(x_2 | x_1, y_1) = \sum_{k=1}^{N_v} \left< \hat{\alpha}_1|\psi_k \right> e^{-D \lambda'_k \Delta t} \psi_k(x_2)
\end{align}
\end{definition}

\begin{definition}[Eigenspace: Time Evolution]
Then, we see the time propagation
\begin{center}
    \includegraphics[scale=0.7]{ch8/time_evolution_graphical.pdf}
\end{center}
\begin{center}
    \includegraphics[scale=0.45]{ch8/alpha_hat_s1_edt.pdf}
\end{center}
\end{definition}

\section{Forward Algorithm}
\begin{definition}[$\alpha(x_1)$]
\begin{align}
    \alpha(x_1) &= p(x_1,y_1) \\
    \frac{\alpha(x_1)}{\rho_{\text{eq}}(x)} &= \left< \alpha_1 | x \right> =  \left< \rho_1 | \textbf{y}_1 | x \right>\\
    \hat{\alpha}(x_1) &= p(x_1 | y_1) \\
    \frac{\hat{\alpha}(x_1)}{\rho_{\text{eq}}(x)} &= \left< \hat{\alpha}_1 | x \right>
\end{align}
\end{definition}

\begin{definition}[$\alpha(x_t)$]
\begin{align}
    \alpha(x_t) &= p(x_t,y_1,...,y_t) \\
    \frac{\alpha(x_t)}{\rho_{\text{eq}}(x)} &= \left< \alpha_t | x \right> =  \left< \alpha_{t-1} | e^{-\textbf{H}\Delta t}\textbf{y}_t | x \right>\\
    \hat{\alpha}(x_t) &= p(x_t | y_1,...,y_t) \\
    \frac{\hat{\alpha}(x_t)}{\rho_{\text{eq}}(x)} &= \left< \hat{\alpha}_t | x \right>
\end{align}
\end{definition}

\begin{definition}[Normalized Constant: $p(y_1)$]
\begin{align}
    p(y_1) &= \left<\alpha_1|\psi_1\right> \\
    \left< \hat{\alpha}_1 \right| &= \frac{\left< \alpha_1 \right|}{\left<\alpha_1|\psi_1\right>}
\end{align}
\end{definition}

\begin{definition}[Normalized Constant: $p(y_t|y_1,...,y_{t-1})$]
\begin{equation}
    \left< \hat{\alpha}_{t-1} | e^{-\textbf{H}\Delta t}\textbf{y}_t | \psi_1 \right> = \int \frac{p(x_t,y_t|y_1,...,y_{t-1})}{\rho_{\text{eq}}(x_t)} \psi_1(x_t) dx_t = p(y_t|y_1,...,y_{t-1})
\end{equation}
so
\begin{equation}
        \left< \hat{\alpha}_t \right| = \frac{\left< \hat{\alpha}_{t-1} \right| e^{-\textbf{H}\Delta t}\textbf{y}_t }{\left< \hat{\alpha}_{t-1} | e^{-\textbf{H}\Delta t}\textbf{y}_t | \psi_1 \right>}
\end{equation}
\end{definition}

\section{Backward Algorithm}
\begin{definition}[Posterior probability $\gamma(x_t)$]
\begin{equation}
    \gamma(x_t) = p(x_t|\textbf{Y}) = \left< \hat{\alpha}_{\tau} |x\right> \left<x|\hat{\beta}_{\tau}\right>
\end{equation}
\end{definition}

\begin{definition}[Posterior probability in Eigenspace]
\begin{equation}
    \left< \gamma_t | \psi_i \right> = \left< \hat{\alpha}_{\tau} |\psi_i \right> \left< \psi_i|\hat{\beta}_{\tau}\right>
\end{equation}
and
\begin{equation}
    \int \gamma(x_t) dx_t = \sum_i \left< \gamma_t | \psi_i \right> = \sum_i \left< \hat{\alpha}_{\tau} |\psi_i \right> \left< \psi_i|\hat{\beta}_{\tau}\right> = \left< \hat{\alpha}_{\tau} | \hat{\beta}_{\tau}\right>
\end{equation}
\end{definition}

\begin{definition}[$\beta(x_{\tau})$]
We need a starting condition for the $\beta$-recursion, and because the posterior probability
\begin{equation}
    p(x_{\tau}|\textbf{Y}) = \frac{\left< \alpha_{\tau} |x\right> \left<x|\beta_{\tau}\right>}{p(\textbf{Y})} = \frac{\frac{\alpha(x_{\tau})}{\rho_{\text{eq}}(x)} \left<x|\beta_{\tau}\right>}{p(\textbf{Y})} = \frac{\alpha(x_{\tau})}{p(\textbf{Y})}
\end{equation}
where $\alpha(x_{\tau})=p(x_{\tau},y_1,...,y_{\tau})$. Therefore, $\left<x|\beta_{\tau}\right>$ can be naturally defined by
\begin{equation}
    \left<x|\beta_{\tau}\right> = \rho_{\text{eq}}(x) = \psi_1(x)
\end{equation}
so
\begin{equation}
    \left| \beta_{\tau}\right> = \left| \psi_{1}\right>
\end{equation}
\end{definition}

\begin{definition}[$\beta(x_t)$]
\begin{align}
    \beta(x_t) &= p(y_{t+1},...,y_{\tau}|x_t) \\
    \left| \beta_t \right> &= e^{-\textbf{H}\Delta t} \textbf{y}_{t+1} \left| \beta_{t+1} \right> \\
    \left| \hat{\beta}_t \right> &= \frac{e^{-\textbf{H}\Delta t} \textbf{y}_{t+1} \left| \hat{\beta}_{t+1} \right>}{p(y_{t+1}|y_1,...,y_t)} \\
    \left| \hat{\beta}_t \right> &= \frac{\left| \beta_t \right>}{p(y_{t+1},...,y_{\tau}|y_1,...,y_{t})}
\end{align}
\end{definition}

\section{Likelihood Function}
\begin{definition}[$p(Y(t))$]
\begin{equation}
    p(Y(t)) = p(y_1,...,y_{\tau}) = \left<\alpha_{\tau} | \beta_{\tau} \right> =  \left<\alpha_{\tau} | \psi_{1} \right>
\end{equation}
Because
\begin{align*}
    \left<\alpha_{\tau} | x \right> &= \frac{p(x_{\tau}, y_1,...,y_{\tau})}{\rho_{\text{eq}}(x)} \\
    \left<\alpha_{\tau} | \psi_{1} \right> &= \int \frac{p(x_{\tau}, y_1,...,y_{\tau})}{\rho_{\text{eq}}(x_{\tau})} \psi_1(x_{\tau})dx_{\tau} = p(y_1,...,y_{\tau})
\end{align*}
Therefore, we also can write
\begin{equation}
    P(Y(t) ; \theta) = \mathcal{L}[\theta]= \langle \alpha_{1} |e^{-\textbf{H}\Delta t}\textbf{y}_2 e^{-\textbf{H}\Delta t} \textbf{y}_3 \ldots e^{-\textbf{H}\Delta t}\textbf{y}_{\tau} | \beta_{\tau} \rangle.
\end{equation}
\end{definition}

\section{Complete Likelihood Function}
\begin{definition}[Complete Likelihood Function]
The complete likelihood function is
\begin{equation}
    p(y_1,\cdots,y_{\tau},x_1,\cdots,x_{\tau}) = p(Y(t),X(t)) = p(x_1)\left[\prod_{t=2}^{\tau}p(x_t|x_{t-1})\right]\prod_{t=1}^{\tau}p(y_t|x_t)
\end{equation}
In bra-ket form:
\begin{align}
    p(Y,X) &= \left< \rho_{1} | x_1 \right> \left< x_1 | \textbf{y}_1 | y_1 \right> \left< x_1 | e^{-\textbf{H}\Delta t} | x_2 \right>  \left< x_2 | \textbf{y}_2 | y_2 \right> \left< x_2 | e^{-\textbf{H}\Delta t} | x_3 \right>  \cdots \left<x_\tau|\beta_{\tau}\right> \\
    &= \left< \rho_{1} | x_1 \right> \left< x_1 | \textbf{y}_1 | x_1 \right> \left< x_1 | e^{-\textbf{H}\Delta t} | x_2 \right>  \left< x_2 | \textbf{y}_2 | x_2 \right> \left< x_2 | e^{-\textbf{H}\Delta t} | x_3 \right>  \cdots \left<x_\tau|\beta_{\tau}\right>\\
    &= \left< \rho_{1} | x_1 \right>  \left< x_1 | \textbf{y}_1 | x_1 \right> \left( \prod_{t=1}^{\tau-1} \left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right> \left< x_{t+1} | \textbf{y}_{t+1} | x_{t+1} \right> \right) \left<x_\tau|\beta_{\tau}\right>
\end{align}
Note that $\left< \rho_{1} | x_1 \right> = \frac{p(x_1)}{\rho_{\text{eq}}(x)}$ and $\left<x_\tau|\beta_{\tau}\right>  = \rho_{\text{eq}}(x)$, so $\rho_{\text{eq}}(x)$ will cancel out by itself.
\begin{center}
    \includegraphics[scale=0.8]{ch9/hmm_graphical_xspace.pdf}
\end{center}
The same form in the eigenspace is
\begin{equation}
    p(Y,X) = \left< \rho_{1} | \psi_{i_1} \right> \left< \psi_{i_1} | \textbf{y}_1 | \psi_{j_1} \right> \left( \prod_{t=1}^{\tau-1} \left< \psi_{j_t} | e^{-\textbf{H}\Delta t} | \psi_{i_{t+1}} \right> \left< \psi_{i_{t+1}} | \textbf{y}_{t+1} | \psi_{j_{t+1}} \right> \right) \left<\psi_{j_{\tau}}|\beta_{\tau}\right>
\end{equation}
and the shorthand notation for $p(Y,X)$ is
\begin{equation}
    p(Y,X) = \prod_{\tau} \left<|\right>
\end{equation}
Furthermore,
\begin{align}
    p(Y(t)) &= \int  p(Y(t),X(t)) \mathcal{D}X \\
    &= \sum_{\{i_t\}}\sum_{\{j_t\}} p(Y(t),X(t))
\end{align}
we convert the path integral to a tractable summations.
\end{definition}

\begin{definition}[$p(X(t)|Y(t))$]
\begin{equation}
    p(X(t)|Y(t)) = \frac{p(X(t),Y(t))}{p(Y(t))} = \frac{\prod^k_{\tau} \langle |\rangle}{\mathcal{L}^k}
\end{equation}
The expectation over the latent trajectory
\begin{align}
    \mathbb{E}^k_{X|Y}[\cdot] \equiv \sum_{\substack{\{i_t\}}} \sum_{\substack{\{j_t\}}} \frac{\prod^k_{\tau} \langle |\rangle}{\mathcal{L}^k}\cdot.
\end{align}

\end{definition}

\begin{definition}[Complete Log-likelihood Function]
\begin{equation}
    \ln{p(Y(t),X(t))} = \ln{p(x_1)} + \sum_{t=2}^{\tau} \ln{p(x_t|x_{t-1})} + \sum_{t=1}^{\tau} \ln{p(y_t|x_t)}
\end{equation}
\end{definition}

\begin{definition}[Q: First Part]
\begin{align*}
    Q &= \int  p(X(t)|Y(t)) \ln{p(Y(t),X(t))} \mathcal{D}X \\
    &= \int p(X(t)|Y(t)) \ln{p(x_1)} \mathcal{D}X + \sum_{t=1}^{\tau-1} \int p(X(t)|Y(t)) \ln{p(x_{t+1}|x_{t})} \mathcal{D}X \\
    &+ \sum_{t=1}^{\tau} \int p(X(t)|Y(t)) \ln{p(y_t|x_t)} \mathcal{D}X
\end{align*}
Because
\begin{equation}
    P(X,Y) = \left< \rho_{1} | x_1 \right> \left< x_1 | \textbf{y}_1 | x_1 \right> \left< x_1 | e^{-\textbf{H}\Delta t} | x_2 \right>  \left< x_2 | \textbf{y}_2 | x_2 \right> \left< x_2 | e^{-\textbf{H}\Delta t} | x_3 \right>  \cdots \left<x_\tau|\beta_{\tau}\right>
\end{equation}
so
\begin{equation}
    \ln{P(X,Y)} = \ln{\left< \rho_{1} | x_1 \right>} + \sum_{t=1}^{\tau-1} \ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>} + \sum_{t=1}^{\tau} \left< x_t | \textbf{y}_t | x_t \right>
\end{equation}
Therefore, we only need to consider $\sum_{t=1}^{\tau-1} \ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>}$
\begin{align}
    \mathbb{E}^k_{X|Y}[\ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>}] &= \int p(x_1,...,x_t,x_{t+1},...,x_{\tau}|Y)\ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>} \mathcal{D}X \\
    & =\int \int p(x_t,x_{t+1}|Y)  \ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>} dx_t dx_{t+1}
\end{align}
where we do the marginalization
\begin{equation}
    \int p(x_1,...,x_t,x_{t+1},...,x_{\tau}|Y) \mathcal{D}X  = \int\int p(x_t,x_{t+1}|Y) dx_t dx_{t+1}
\end{equation}
\end{definition}

\begin{definition}[Expectation in eigenspace]
\begin{align*}
    \mathbb{E}^k_{X|Y}[\ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>}] &= \int \int p(x_t,x_{t+1}|Y)  \ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>} dx_t dx_{t+1}
\end{align*}
\begin{align}
    \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{i_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right>}] &= \sum_{\{i_t, j_t\}} p(\psi_{i_t},\psi_{j_{t+1}}|Y)  \ln{\left< \psi_{i_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}}\right>} \\
    &= \sum_{\{i_t, j_t\}} p(\psi_{i_t},\psi_{j_{t+1}}|Y) \ln{ \left( e^{-D_j \lambda'_j \Delta t} \left< \psi_{i_t} |\psi_{j_{t+1}} \right>\right)}
\end{align}
If $i \neq j$,
\begin{align*}
    \left< \psi_{i_t} |\psi_{j_{t+1}}\right> &= 0 \\
    \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{i_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right>}] &= \sum_{\{i_t, j_t\}} p(\psi_{i_t},\psi_{j_{t+1}}|Y) \ln{0} = -\infty
\end{align*}
If $i=j=1$
\begin{align*}
    \left< \psi_{1_t} |\psi_{1_{t+1}}\right> &= 1 \\
    \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{1_t} | e^{-\textbf{H}\Delta t} | \psi_{1_{t+1}} \right>}] &= p(\psi_{1_t},\psi_{1_{t+1}}|Y) \ln{1} = 0
\end{align*}
If $i=j\neq 1$
\begin{align*}
    \left< \psi_{j_t} |\psi_{j_{t+1}}\right> &= 1 \\
    \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{j_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right>}] &= p(\psi_{j_t},\psi_{j_{t+1}}|Y) \ln{e^{-D_j \lambda'_j \Delta t}} \\
    &= p(\psi_{j_t},\psi_{j_{t+1}}|Y) \ln{A_{j}}
\end{align*}
\end{definition}

\begin{definition}[$\xi(x_t,x_{t+1})$]
\begin{equation}
    \xi(x_t,x_{t+1}) = p(x_t, x_{t+1} | Y) = \frac{p(x_t,x_{t+1},Y)}{p(Y)}
\end{equation}
where
\begin{align*}
    p(x_t,x_{t+1},Y) &= p(x_t, y_1,...,y_t) p(x_{t+1}|x_t) p(y_{t+1}|x_{t+1}) p(y_{t+2},...,y_{\tau}|x_{t+1}) \\
    &= \left< \alpha_t | x_t \right> \left<x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right> \left< x_{t+1} | \textbf{y}_{t+1}| x_{t+1} \right> \left< x_{t+1} | \beta_{t+1} \right>
\end{align*}
and
\begin{align*}
    p(\psi_{i_t},\psi_{j_{t+1}},Y) &= \left< \alpha_t | \psi_{i_t} \right> \left< \psi_{i_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}}| \textbf{y}_{t+1}| \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}} | \beta_{t+1} \right> \\
    &= e^{-D_j \lambda'_j \Delta t} \left< \alpha_t | \psi_{i_t} \right> \left< \psi_{i_t} | \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}}| \textbf{y}_{t+1}| \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}} | \beta_{t+1} \right>
\end{align*}
If $i \neq j$
\begin{align*}
    p(\psi_{i_t},\psi_{j_{t+1}},Y) = 0
\end{align*}
If $i=j$
\begin{align*}
    p(\psi_{j_t},\psi_{j_{t+1}},Y) &= e^{-D_j \lambda'_j \Delta t} \left< \alpha_t | \psi_{j_t} \right> \left< \psi_{j_t} | \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}}| \textbf{y}_{t+1}| \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}} | \beta_{t+1} \right> \\
    &= e^{-D_j \lambda'_j \Delta t} \left< \alpha_t | \psi_{j_t} \right> \left< \psi_{j_{t+1}}| \textbf{y}_{t+1}| \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}} | \beta_{t+1} \right>
\end{align*}
Because
\begin{align*}
    \left<\hat{\alpha}\right| &= \frac{\left< \alpha_t \right|}{p(y_1,...,y_t)}\\
    \left | \hat{\beta}_{t+1} \right> &= \frac{\left | \beta_{t+1} \right>}{p(y_{t+2},y_{t+3},...,y_{\tau}|y_1,...,y_{t+1})} \\
    c_{t+1} &= p(y_{t+1}|y_1,...,y_{t})
\end{align*}
so
\begin{align}
    p(\psi_{j_t},\psi_{j_{t+1}}|Y) &=  \frac{p(\psi_{j_t},\psi_{j_{t+1}},Y)}{p(Y)} \\
    &= \frac{e^{-D_j \lambda'_j \Delta t} \left< \hat{\alpha}_t | \psi_{j_t} \right> \left< \psi_{j_{t+1}}| \textbf{y}_{t+1}| \psi_{j_{t+1}} \right> \left< \psi_{j_{t+1}} | \hat{\beta}_{t+1} \right>}{c_{t+1}}
\end{align}
\end{definition}

\begin{definition}[Q: Second Part]
\begin{equation}
    Q = \mathbb{E}_{X|Y}[\ln{\left< \rho_{1} | x_1 \right>}] + \sum_{t=1}^{\tau-1} \mathbb{E}_{X|Y}[\ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>}] + \sum_{t=1}^{\tau} \mathbb{E}_{X|Y}[\left< x_t | \textbf{y}_t | x_t \right>]
\end{equation}
because $p_{\text{eq}}(x)$ and $D$ are buried in $e^{-\textbf{H}\Delta t}$, we only care about
\begin{align*}
    \sum_{t=1}^{\tau-1} \mathbb{E}_{X|Y}[\ln{\left< x_t | e^{-\textbf{H}\Delta t} | x_{t+1} \right>}]
\end{align*}
Recall that
\begin{equation}
    \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{j_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right>}] = p(\psi_{j_t},\psi_{j_{t+1}}|Y) \ln{A_{j}}
\end{equation}
so
\begin{align}
    \tilde{Q} &= \sum_{t=1}^{\tau-1} \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{j_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right>}] = \sum_{t=1}^{\tau-1} \mathbb{E}^k_{X|Y}[\ln{A_j}] = \sum_{t=1}^{\tau-1} p(\psi_{j_t},\psi_{j_{t+1}}|Y) \ln{A_{j}}
\end{align}
\end{definition}

\begin{definition}[No analytical form for updating $D$: Part 1]
There three forms of
\begin{align*}
    Q = \int  p(X(t)|Y(t)) \ln{p(Y(t),X(t))} \mathcal{D}X
\end{align*}
in this framework. The first is
\begin{equation}
    \tilde{Q} = \sum_{t=1}^{\tau-1} \mathbb{E}^k_{X|Y}[\ln{\left< \psi_{j_t} | e^{-\textbf{H}\Delta t} | \psi_{j_{t+1}} \right>}] = \sum_{t=1}^{\tau-1} p(\psi_{j_t},\psi_{j_{t+1}}|Y) \ln{e^{-D_j \lambda'_j \Delta t}}
\end{equation}
If we take derivative of $\tilde{Q}$ with respect to $D_j$, we get
\begin{equation}
    \frac{d\tilde{Q}}{d D_j} = -\sum_{t=1}^{\tau-1} p(\psi_{j_t},\psi_{j_{t+1}}|Y) \lambda'_j \Delta t
\end{equation}
We can not find $D_{\text{max}}$ which make $\frac{d\tilde{Q}}{d D_j}=0$ through the above equation.
\end{definition}

\begin{definition}[No analytical form for updating $D$: Part 2]
The second is
\begin{equation}
    \tilde{Q} = \sum_{t=1}^{\tau-1} p(\psi_{j_t},\psi_{j_{t+1}}|Y) \ln{A_{j}}
\end{equation}
The matrix $A$ is
\begin{equation}
    A = \begin{bmatrix}
        1 & 0 & \cdots & 0 \\
        0 & e^{-D_2\lambda'_2 \Delta t} & \cdots & 0 \\
        \vdots & \vdots & \cdots & \vdots \\
        0 & 0 & \cdots & e^{-D_{N_v}\lambda'_{N_v} \Delta t}
    \end{bmatrix}
\end{equation}
Actually, we have no constraint for $A_j$ like the conventional stochastic matrix. Therefore, we can not find $D_{\text{max}}$ through the method of Lagrange multipliers.
\end{definition}

\begin{definition}[No analytical form for updating $D$: Part 3]
The third is JPCB paper's form
\begin{equation}
    \tilde{Q} = \sum_{t=1}^{\tau-1} \ln{\left< \hat{\alpha}_t | e^{-\textbf{H}\Delta t} \textbf{y}_{t+1} | \hat{\beta}_{t+1} \right>} - \sum_{t=1}^{\tau-1}\ln{c_{t+1}}
\end{equation}
Because
\begin{align*}
    \left< \hat{\alpha}_t | e^{-\textbf{H}\Delta t} \textbf{y}_{t+1} | \hat{\beta}_{t+1} \right> &= \sum_{i,j,k} \left< \hat{\alpha}_t \right| \psi_i \left> \right<\psi_i| e^{-\textbf{H}\Delta t} | \psi_j \left> \right<\psi_j| \textbf{y}_{t+1} | \psi_k \left> \right<\psi_k \left| \hat{\beta}_{t+1} \right> \\
    &= \sum_{i,j,k} e^{-D_j \lambda_j\Delta t} \left< \hat{\alpha}_t \right| \psi_i \left> \right<\psi_i | \psi_j \left> \right<\psi_j| \textbf{y}_{t+1} | \psi_k \left> \right<\psi_k \left| \hat{\beta}_{t+1} \right> \\
    &= \sum_{j,k} e^{-D_j \lambda_j\Delta t} \left< \hat{\alpha}_t \right| \psi_j \left> \right<\psi_j | \psi_j \left> \right<\psi_j| \textbf{y}_{t+1} | \psi_k \left> \right<\psi_k \left| \hat{\beta}_{t+1} \right> \\
    &= \sum_{j,k} e^{-D_j \lambda_j\Delta t} \left< \hat{\alpha}_t \right| \psi_j \left> \right<\psi_j| \textbf{y}_{t+1} | \psi_k \left> \right<\psi_k \left| \hat{\beta}_{t+1} \right>
\end{align*}
so
\begin{equation}
    \frac{d\tilde{Q}}{d D_j} = \sum_{t=1}^{\tau-1} \lambda_j\Delta t \sum_{j,k}  \left< \hat{\alpha}_t \right| \psi_j \left> \right<\psi_j| \textbf{y}_{t+1} | \psi_k \left> \right<\psi_k \left| \hat{\beta}_{t+1} \right>
\end{equation}
We can not find $D_{\text{max}}$ which make $\frac{d\tilde{Q}}{d D_j}=0$ through the above equation.
\end{definition}

\section{EM-peq}
\begin{center}
    \includegraphics[scale=0.45]{ch8/em_newscheme.pdf}
\end{center}
\begin{center}
    \includegraphics[scale=0.45]{ch9/first_peq_em.pdf}
\end{center}

\section{Kevin's Document}
\begin{definition}[Alpha-Beta]
\begin{align}
    p_{\theta}(x_t|\textbf{y})&=\frac{P_{\theta}(\textbf{y}_{[0,t]}|x_t)P_{\theta}(\textbf{y}_{(t,T]}|x_t)}{P_{\theta}(\textbf{y})}p_{\theta}^{\text{eq}}(x_t) \\
    \alpha_{\theta}(x_t) &= P_{\theta}(\textbf{y}_{[0,t]}|x_t) \sqrt{p_{\theta}^{\text{eq}}(x_t)} \\
    \beta_{\theta}(x_t) &= P_{\theta}(\textbf{y}_{(t,T]}|x_t) \sqrt{p_{\theta}^{\text{eq}}(x_t)}
\end{align}
\end{definition}

\begin{definition}[Likelihood Path Integral]
\begin{align}
    \alpha_{\theta}(x_t) &= \left<X_t|x_t\right> \\
    \beta_{\theta}(x_t) &= \left<x_t|X_t\right> \\
    P_{\theta}(\textbf{y}) &= L_{\theta}(\textbf{y})= \langle X_{t_0} |\textbf{y}_0 e^{-\textbf{H}\Delta t}\textbf{y}_1 e^{-\textbf{H}\Delta t} \textbf{y}_2 \ldots e^{-\textbf{H}\Delta t}\textbf{y}_{T}  | X_{t_T} \rangle \\
    l_{\theta}(\textbf{y}) &= \ln{(L_{\theta}(\textbf{y}))} \\
    \frac{\delta l_{\theta}(\textbf{y})}{\delta F(x)} &= \frac{1}{L_{\theta}}\int_0^{T} dt \frac{\delta \langle X_{t} | \textbf{H}| X_{t}\rangle}{\delta F(x)}
\end{align}
\end{definition}

\begin{definition}[Likelihood]
\begin{equation}
    P_{\theta}(\textbf{y}) = L_{\theta}(\textbf{y})= \langle X_{t_0} |\textbf{y}_0 e^{-\textbf{H}\Delta t}\textbf{y}_1 e^{-\textbf{H}\Delta t} \textbf{y}_2 \ldots e^{-\textbf{H}\Delta t}\textbf{y}_{T}  | X_{t_T} \rangle
\end{equation}

\end{definition}


\documentclass[11pt]{article}

\usepackage{graphicx}
\usepackage{courier}
\usepackage{underscore}

\title{Turing Machine Definitions}
\author{Adam Yedidia}

\begin{document}

\maketitle

This document explains the formal definitions of Turing Machines as generated by this project. Note that most of this document also appears in \emph{A Relatively Small Turing Machine Whose Behavior Is Independent of Set Theory}, which can be found at: \\ \\
\texttt{parsimony/tex/busybeaver/busybeaver.pdf}

\section{2-Symbol Turing Machines}

This section explores how the 2-symbol Turing machines that are generated by this project are defined.

There are many slightly different definitions of Turing machines. \ For example, some definitions allow the machine to have multiple tapes; others only allow it to have one; some allow an arbitrarily large alphabet, while others allow only two symbols, and so on. \ In most research regarding Turing machines, mathematicians don't concern themselves with which of these models to use, because any one can simulate the others (usually efficiently). \ However, because this work is concerned with upper-bounding the exact number of states required to perform certain tasks, it's important to define the model precisely. \ The model we choose here is traditional for the Busy Beaver function.

Formally, a $k$-state Turing machine is a 7-tuple $M = (Q, \Gamma, b, \Sigma, \delta, q_0, F)$, where: \\ \\
$Q$ is the set of $k$ \emph{states} $\{q_0, q_1, \dots, q_{k-2}, q_{k-1}\}$ \\
$\Gamma = \{a, b\}$ is the set of \emph{tape alphabet symbols} \\
\texttt{a} is the \emph{blank symbol} \\
$\Sigma = \empty$ is the set of \emph{input symbols} \\\
$\delta = Q \times \Gamma \rightarrow (Q \cup F) \times \Gamma \times \{L, R\}$ is the \emph{transition function} \\
$q_0$ is the \emph{start state} \\
$F = \{\textrm{HALT}, \textrm{ERROR}\}$ is the set of \emph{halting transitions}. \\

A Turing machine's \emph{states} make up the Turing machine's easily-accessible, finite memory. \ The Turing machine's state is initialized to $q_0$.

The \emph{tape alphabet symbols} correspond to the symbols that can be written on the Turing machine's infinite tape.

In this work, all Turing machines are run on the all-\texttt{a} input.

The \emph{transition function} encodes the Turing machine's behavior. \ It takes two inputs: the current state of the Turing machine (an element of $Q$) and the symbol read off the tape (an element of $\Gamma$). \ It outputs three instructions: what state to enter (an element of $Q$), what symbol to write onto the tape (an element of $\Gamma$) and what direction to move the head in (an element of $\{L, R\}$). \ A transition function specifies the entire behavior of the Turing machine in all cases.

The \emph{start state} is the state that the Turing machine is in at initialization.

A \emph{halting transition} is a transition that causes the Turing machine to halt.

\section{4-Symbol Turing Machines}

This section explores how the 4-symbol Turing machines that are generated by this project are defined.

a $k$-state, 4-symbol Turing machine is a 7-tuple $M = (Q, \Gamma, b, \Sigma, \delta, q_0, F)$, where: \\ \\
$Q$ is the set of $k$ \emph{states} $\{q_0, q_1, \dots, q_{k-2}, q_{k-1}\}$ \\
$\Gamma = \{\texttt{_}, \texttt{1}, \texttt{H}, \texttt{E}\}$ is the set of \emph{tape alphabet symbols} \\
\texttt{_} is the \emph{blank symbol} \\
$\Sigma = \empty$ is the set of \emph{input symbols} \\\
$\delta = Q \times \Gamma \rightarrow (Q \cup F) \times \Gamma \times \{L, -, R\}$ is the \emph{transition function} \\
$q_0$ is the \emph{start state} \\
$F = \{\textrm{HALT}, \textrm{ERROR}\}$ is the set of \emph{halting transitions}. \\

\end{document}


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 9, Section 2.4: 4(b), 6, 7, 16 }
\author{Alex Gordon}
\date{\today}
\maketitle
\section*{Induction Murdering Time.}
\subsection*{4. B)}
For $n=1$ , it is given that $n^3-n=0 $ is divisible by 3. Let it be true for $n=k$.
So $k^3-k$ is divisible by $3k$. Let us check that it is also divisible for $n=k+1$.\
\begin{align*}
\\&= (k+1)^3-(k+1)
\\  &= k^3+3k^2+3k+1-k-1
\\ &=(k^3-k) + 3k(k+1)\\
\end{align*}
First factor is divisible by 3k. Second factor is multiple of 3 hence, divisible by 3. By induction it is true.
\subsection*{6.}
This proof will be mostly mathematical. However, for the first part of it, let us try some examples. Suppose that $n = 2$. It then follows that:\\
\begin{align*}
\\&= 2^6 - 1
\\  &= 63\\
\end{align*}
63 isn't prime. Let us supposed that $2^{3n} - 1$ isn't prime all the way through m + 1.
\begin{align*}
\\ & =  2^{3(m+1)} - 1
\\&= 2^3 \times 2^{3n} - 1
\\&= 7 \times 2^{3n} +  2^{3(n)} - 1
\\&= 7 \times 2^{3n} +  2^{3n} - 1\\
\end{align*}
Now before I continue, I'd just like to note that $a^n - 1 = (a - 1)(a^{n-1} + a^{n-2} + .. + 1)$. As such, it follows that;
\begin{align*}
\\2{3(n+1)}  &= 7 \times 2^{3n} + (2^3 -1)((2^3)^{n-1} + (n^3)^{n-2} + ... + 1)
\\&= 7 \times (2^{3n} + (2^3)^{n-1} + (2^3)^{n-2} + ... + 1)\\
\end{align*}
The math above shows that since $2^{3(n+1)}$ is a composite number, so it follows that  $2^{3n} - 1$ is a composite number for all $n \geq 2$ because $n + 1$ is equal to any integer.
\subsection*{7.}
The recurrence can be rewritten as\\
$p_n − p_n−1 = p_{n−1} + p_{n−2}$\\
This can also be written as
$(p_{n+1} − p_n)^2 − 2p_{n}{^2} = (−1)^n$\\
Let S(n) be the statement given by the previous equation. Let me no show that they are both true. Now assume that the first equation, the second and so on to S(m − 1) have all been validated. Now, continuing, we'll set the left side of the equation equal to
$−p^2_m + 2p_m p_m −1 + p^2_{m−1}$ Factoring out a 1 and completing the square, we get\\
$-(p_m -p_{m-1})^2$
\subsection*{16.}
Suppose that $n ≥ 28$ and $n = 8a + 5b$\\
for some nonnegative integers $a$ and $b$. \\
\\ If $a = 1$ then since $n \geq 28$, we must have $5b  \geq 20$, so that $b  \geq 4$. In this case we may replace 3 of the 5 cent stamps with 2 of the 8 cent stamps to get $n+1 = 8(a+2)+5(b−3)$.
If $a = 2$, then since $n \geq 28$, we must have $5b \geq 12$, and since $b$ is an integer, then $b \geq 3$. Again, in this case, we may replace 3 of the 5 cent stamps with 2 of the 8 cents stamps to get $n+1 = 8(a+2)+5(b−3)$.
 If $a \geq 3$, then we may replace 3 of the 8 cent stamps with 5 of the 5 cent stamps to get $n+1 = 8(a−3)+5(b+5)$.
Thus in all cases, we may write $n + 1 = 8a′ + 5b′$ for some nonnegative integers $a′$ and$ b′$.
Therefore, by the principle of mathematical induction, for any integer $n \geq  = 28$, we can use a combination of 5 cent and 8 cent stamps to obtain n cents in postage.\\ Take that induction.
\end{document}


\documentclass{report}
\input{preamble}

\begin{document}

\thispagestyle{FirstPage}
\begin{center}
\textbf{\large Notes}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TOPIC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Solutions to the point reactor kinetics equations}

\subsection*{The inhour equation}

The point reactor kinetics equations (PRKEs) are given by the following equations for the rate of change of both power, $P(t)$ and concentration of delayed neutron precursor group $j$, $C_j(t)$:
\begin{align*}
\frac{dP(t)}{dt}	&= \frac{\rho_0 - \beta}{\Lambda} P(t) + \sum_{j=1}^6 \lambda_j C_j(t) \\
\frac{dC_j(t)}{dt}	&= \frac{\beta_j}{\Lambda} P(t) - \lambda_j C_j(t) , \qquad j= 1,2,3,...,6
\end{align*}
We assume here that we are using the conventional 6 delayed neutron precursor groups, each with delayed neutron fraction $\beta_j$, average decay constant $\lambda_j$, a mean generation time $\Lambda \equiv \frac{\ell}{k}$, and constant reactivity insertion $\rho_0$.

We had previously determined that the solutions of these equations are of the general form
$$ P(t) = Pe^{st} \quad\text{and}\quad C_j(t) = C_je^{st} ,$$
where $P$ and $C_j$ are constant coefficients, and $s$ is also a constant related to the time-dependent nature of the system. Note that since we have mutliple differential equations there will be an equivalent number of possible solutions. For the 6 group case there are 7 equations---1 power equation and 6 precursor equations. The complete solution will therefore be a combination (superposition) of all 7 possible solutions.
$$ P(t) = \sum_{i=1}^7 P_i \, e^{s_i t} $$
Frequently, reactor operators are interested in knowing what the effect would be of introducing something into the system that would change how the neutron population (or power production) evolves over time. For example, inserting a control rod or adding a soluble absorber to the moderator would constitute a reactivity adjustment of this type. If we decide not to solve for the constant coefficients for power and precursor group concentration, $P$ and $C_j$, we can instead make some simplifications to learn about the reactor's behavior over time for some arbitary reactivity insertion. We will solve for this reactivity, $\rho_0$.

First, we start by plugging the general form of the solutions back into the original PRKEs, taking the derivative on the left side of each equation, and dividing out the factor of $e^{st}$ from every term gives the following:
\begin{align*}
sP	&= \frac{\rho_0 - \beta}{\Lambda} P + \sum_{j=1}^6 \lambda_j C_j \\
sC_j	&= \frac{\beta_j}{\Lambda} P - \lambda_j C_j , \qquad j= 1,2,3,...,6
\end{align*}
Then, we take the second of these equations and solve for $C_j$ in terms of $P$,
$$ C_j = \frac{\beta_j}{\Lambda(s+\lambda_j)}P ,$$
and substitute this expression back into the first of these equations to eliminate the $C_j$ term altogether.
$$ sP = \frac{\rho_0 - \beta}{\Lambda} P + \sum_{j=1}^6 \frac{\beta_j \lambda_j}{\Lambda(s+\lambda_j)}P $$
At this point, we can eliminate $P$ by dividing it out of both sides of the equation, as well as factor out $\frac{1}{\Lambda}$ from all of the terms.
$$ s = \frac{1}{\Lambda}\left(\rho_0 - \beta + \sum_{j=1}^6 \frac{\beta_j \lambda_j}{(s+\lambda_j)}\right) $$
We note that $\beta = \sum_{j=1}^6 \beta_j$ and we can move the $\beta$ term into the summation as
$$ s = \frac{1}{\Lambda}\left(\rho_0 + \sum_{j=1}^6 \frac{\beta_j \lambda_j}{(s+\lambda_j)}-\beta_j\right) $$
or more simply
$$ s = \frac{1}{\Lambda}\left(\rho_0 - \sum_{j=1}^6 \frac{\beta_j s}{(s+\lambda_j)}\right) .$$
Here it is important to recall that $\Lambda \equiv \frac{\ell}{k}$, and $\rho_0 = \frac{k-1}{k}$. Since both terms are dependent on $k$ and our final goal is to solve for $\rho_0$, we will eliminate $\Lambda$. Manipulation of the reactivity formula leads us to the expression
$$ k = \frac{1}{1-\rho_0} $$
and so using this in our formula for the mean generation time gives
$$ \Lambda = \ell(1-\rho_0) $$
We substitute this in for $\Lambda$ in our equation for $s$ to get
$$ s = \frac{1}{\ell(1-\rho_0)}\left(\rho_0 - \sum_{j=1}^6 \frac{\beta_j s}{(s+\lambda_j)}\right) .$$
Finally, we solve algebraically for $\rho_0$ to get the \textbf{inhour equation}:
$$ \rho_0 = \frac{s\ell}{s\ell + 1} + \frac{1}{s\ell + 1}\sum_{j=1}^6 \frac{\beta_j s}{(s+\lambda_j)} .$$

When this equation is plotted, we can see that there are 7 possible solutions ($s_1$, $s_2$, $s_3$, ... $s_7$) for any value of $\rho_0$. There are also 7 vertical asymptotes, each corresponding to a value of $s$ for which the denominator of any term in the 6-group inhour equation goes to zero (at $s=-\frac{1}{\ell}$ and $s=-\lambda_j$ for all $j$).
%\includegraphic[width=10cm]{inhour-6group.jpg}

If we only had 1 delayed neutron group rather than 6, our equation would be
$$ \rho_0 = \frac{s\ell}{s\ell + 1} + \frac{1}{s\ell + 1}\frac{\beta_j s}{(s+\lambda)} .$$
There would now only be two possible solutions ($s_1$ and $s_2$) and two vertical asymptotes (at $s=-\frac{1}{\ell}$ and $s=-\lambda$).
%\includegraphic[width=10cm]{inhour-1group.jpg}

\end{document}


Figure \ref{fig:cooperation_M} shows cooperation levels achieved after $N=200$ iterations [i.e., $200 \times 49^2 = 480200$ Monte Carlo Steps (MCS)] for migration Moore's distances $M = \{1,3,5,7,9,11,13 \}$, as a function of the grid density $d$ and the probability of property violation $s$. At initialization ($i=0$), there is a $50\%$ chance that a player will be a cooperation (resp. defector). For all $M$, the cooperation exhibits a sharp drop for $d>d^*$  and $s > s^*$ with $(d^*,s^*)$ being a function of $M$. High levels of cooperation ($c > 0.8$) can be sustained for any migration range $M$ (note that for $0.02 < s < 0.05$, the cooperation level drops already to $c \approx 0.9$ for all $M$). However, the less migration capabilities (i.e., $M$ small), the more restrictive the space defined by $(d^*,s^*)$ and hence, the more sensitive the game to property violations (as shown on Figure  \ref{fig:cooperation_M}).\\

For high grid density ($d>0.9$), cooperation cannot be sustained for any level of migration $M>0$, even with low property violation ($s > 0$). Note that in the limit $d=1$, property violation reduces to swapping sites, since the only available site in the Moore area for the expelled player is the site left by the property violator.\\

\subsection*{Phase Transitions}
The smaller $M$, the sharper the transition between sustained cooperation and $c=0$: The probability to witness a given level of cooperation $c > 0$ (after $N=200$ iterations) decreases steadily (faster as $M \rightarrow 1$), until a point of abrupt break beyond which cooperation can no longer not strive (see Figure \ref{fig:phase_transition} for $0.4 < d < 0.6$). For $M \leqslant 5$, we observe a sharp phase transition from a high cooperation level $c \gtrsim 0.6$ to no cooperation left ($c=0$). For $M \geqslant 7$, we observe 3 possible states: high cooperation level $c \gtrapprox 0.6$ for property violation $s \lessapprox 0.4$ small, moderate cooperation $ 0.35 \lessapprox s  \lessapprox 0.5 $, and no cooperation.\\

These possible states and their stability are best represented (c.f., Figure \ref{fig:tseries}) from the cooperation time series for values $(M,d^*,s^*)$ with $M = \{ 5,7,11,13 \}$. For $M=5$, the intermediary state is not sustainable, and it seems that if a certain level of cooperation $c > 0.5$ cannot be achieved quickly cooperation ultimately disappears. For $M = \{7,11\}$ , an intermediary state appears with simulations displaying quasi-stationarity of cooperation $0.35 < c < 0.55$. Yet the stationary of this intermediary state may be broken after an arbitrary number of iterations (see e.g., simulation 12 for $M=7$, which drops after 170 iterations, or simulation 3 for $M=11$, which drops after 140 iterations). {\bf It also looks like that for $M=7$, the state of high cooperation is reached quickly and at high level, but this may be a pure selection bias}. For $M=13$ and for $s^* = 0.67(2)$, the intermediary state is stable (no deviation after an arbitrary number of iterations is observed), and the percentage of cooperators remains consistently below 0.5 (more precisely XX on average, standard deviation = XX). In other words, for a large enough migration range, cooperation can be sustained at rather high levels of property violation (e.g., $M=13, s^* = 0.67\pm0.2$), although cooperators are in minority.\\

\subsection*{Typical Spatial Configurations}

{\bf [Here we show typical spatial configurations]}

\begin{itemize}
  \item rapid extinction of cooperators in presence of $s > s^*$.
  \item well sustained cooperation in presence of property violators, yet with sufficiently high mobility $M$ and low enough grid density $d$ $\rightarrow$ $M=11$.
  \item cooperation at quasi-stationarity with level consistently below $0.5$ (e.g., $M=13$).
\end{itemize}

\subsection*{Case Study of Sudden Drops Cooperation Drops}

{\bf [Here, we study the 2 sudden changes of regime mentioned above] }


\chapter{Causality}

\begin{multicols}{2}[\subsubsection*{Contents of this chapter}]
   \printcontents{}{1}{\setcounter{tocdepth}{2}}
\end{multicols}

\section{Generalized Random Forests}
Notes on \citeasnoun{athey2016generalized}. Like most regression techniques, random forests are normally used to estimate the conditional mean of some data generating distribution, i.e. $\mu(x) = \mathbb{E}\left[Y|X=x \right]$. Athey, Tibshirani and Wager proposed generalized random forests, which estimate, more generally, some quantity $\theta(x)$. The main aim, and the reason why I am grouping this method under ``causality", is the estimation of heterogenous treatment effects.


\documentclass{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\usepackage{matlab-prettifier}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
%
% Basic Document Settings
%
\lstMakeShortInline[style=Matlab-editor]"

\topmargin=-1in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

%
% Homework Details
%   - Title
%   - Due date
%   - Class
%   - Section/Time
%   - Instructor
%   - Author
%

\newcommand{\hmwkTitle}{AMATH 482 Homework 2: G\'abor transforms}
\newcommand{\hmwkDueDate}{January 25, 2019}
\newcommand{\hmwkClassInstructor}{Professor Nathan Kutz}
\newcommand{\hmwkAuthorName}{\textbf{Skyler Hallinan}}

%
% Title Page
%

\title{
    \textmd{\textbf{\text{ } \hmwkTitle}}\\
}

\author{\hmwkAuthorName}
\date{}

\begin{document}
\maketitle

\section*{\fontsize{19}{15}\selectfont Abstract}
	We started with data (music) in the time domain. Using the Gabor transform, we were able to translate this data into the time and frequency domain, creating a spectogram that allows us to visualize frequencies at specific points in time. We experimented with the different parameters used in the transform, and used it to transform to differentiate between the music of a piano and a recorder.
\section*{\fontsize{19}{15}\selectfont Introduction and Overview}
	In data such as music, we often collect it in in the time domain - Although we can tell when things happen, it is hard to tell what is going on in terms of frequencies. The Fourier Transform is a solution lets us transport this data into the frequency domain. However, with this transformed data, we now know what frequencies occur, but we have do not now when they occur in time. The Gabor transform allows us to take small chunks of data across our time domain, which are isolated by multiplying a filter around, then taking the Fourier transform of this data. Combining this frequency data across the small chunks of time allows us to have both a time and frequency visualization of data: We are now able to see what frequencies dominate at each small time interval. \\ \\
	In this project, we will first look at the effects of changing specific paramaters of the Gabor transform, like sampling rate, as well as window size (how much of the entire domain it captures at each timepoint). In addition, we will experiment with other filters other than the standardly used Gaussian one. Finally, we will look at data taken from a recorder and piano, remove the overtones, and then reproduce the music score, and note the differences between the two instruments.
\section*{\fontsize{19}{15}\selectfont Theoretical Background}
	We have seen that the fourier transform is an integral transform that decomposes a time defined function into its frequency components, and is defined by
	\begin{equation} \label{eq:2a}
		F(k) = \frac{1}{\sqrt2\pi} \int_{-\infty}^{\infty} e^{-ikx} f(x) dx
	\end{equation}
		\begin{equation} \label{eq:2b}
		f(x) = \frac{1}{\sqrt2\pi} \int_{-\infty}^{\infty} e^{ikx} F(k) dk
	\end{equation}
	The fast fourier transform, which is accessible in things like MATLAB, assumes that the user is working on a $2\pi$ periodic domain. "fftshift" must be used on the transformed data to undo the shifts naturally incurred by the fast fourier transform algorithm. \\ \\
A downside to the fourier transform is that in changing the domain to the frequency domain, it completely removes all time data from the equation. There is no way to visualize what is going on in terms of a time scale. G\'abor resolved this issue. \\
The Gabor transform is defined by $\mathcal { G } [ f ] ( t , \omega ) = \tilde { f } _ { g } ( t , \omega ) = \int _ { - \infty } ^ { \infty } f ( \tau ) \overline { g } ( \tau - t ) e ^ { - i \omega \tau } d \tau = \left( f , \overline { g } _ { t , \omega } \right)$. We see that this GAabor function localizes signal with the fourier transform over specific small windows of time. The $\tau$ paramater is able to slide the window across the entire domain, resulting in a capturing of both time and frequency domain. Because of the nature of this transform, it is also called the short time fourier transform. \\ \\
There are limitations to this method: It is not possible to have perfect resolution in both the frequency and the time domain; increasing time resolution decreases frequency resolution and vice versa. Just like the Heisenberg Uncertainty Principle, it is impossible to know both of these things at the same time. However, one can adjust the parameters of the transform in order to get more data towards one direction or the other. \\ \\
Wavelets are used in these transforms to highlight specific parts of the signal. Wavelets like the Gaussian and the Mexican Hat wavelet are shifted across the time domain and multiplied by the data so that specific fourier transform information can be taken, and then plotted later.
\section*{\fontsize{19}{15}\selectfont Algorithm Implementation and Development}
\textbf{Part One} \\
We first found the length of our data and the number of time points at which it occurred, then created a vector of time.
In addition, we rescaled our wavenumbers $k$ by $\frac{2\pi}{L}$ as the fast fourier transform assumes $2\pi$ periodic signals. We then defined a vector "tslide" which corresponded to the length of time of the recording of music, in 0.1 s increments. \\ \\
We then used a for loop to iterate through the length of "tslide", creating and adjusting a Gaussian filter so that it was centered around "t-tslide" the entire time. We multiplied this filter by our data, then took the fast fourier transform of it. Finally, we entered it back into our spectogram array after taking the absolute value of the "fftshift" of this. We repeated this with larger and smaller increments in our "tslide", different filters (Mexican Hat and Step function), and with different widths of our Gaussian filter. Finally, we used "pcolor" to visualize our G\'abor Transforms in the form of spectograms. We had to divide by $2pi$ to get accurate frequency information since we multiplied by $2pi$ in our original wavenumbers. \\ \\
\textbf{Part Two} \\
We repeated the same initial data preparation used for part one: Defining the length of the data, creating our "tslide" variable, and rescaling our wavenumbers. We did the same loop to fill our spectogram with data, using a Gaussian filter with a sigma value of -100. We then used $pcolor$ to plot the side by side spectograms, once again dividing by $2pi$ to get accurate frequency values. \\ \\
We also did some more work in the same G\'abor transform loop to generate the music scores. In order to remove overtones from the data, we found the index of the max value of the fourier transform at a certain time. The max value indicated that this particular frequency is the strongest at this time point, so we could effectively extract the corect frequency while removing some overtone noise. We did this by using the "max" command during the G\'abor transform loop, then plugging this index into "ks". We then, once again, divided by $2pi$ to get our frequency numbers.
\begin{figure}[H]
\begin{center}
\includegraphics[width = 10cm]{normal}
\caption{\label{fig:scaled_diss} (left) Spectogram of normally made data}
\end{center}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width = 8cm]{undersampling}
\includegraphics[width = 8cm]{oversampling}
\caption{\label{fig:scaled_diss} (left) Spectogram of Undersampled Data}
\caption{\label{fig:scaled_diss} (right) Spectogram of Oversampled Data}
\end{center}
\end{figure}

\section*{\fontsize{19}{15}\selectfont Computational Results}
	\textbf{Part One} \\
	We were able to find a balance of the correct level of accuracy in the time and frequency domain by using a value of -100 for our sigma in our Gaussian filter. We first generated a spectogram of our data using this accurate filter, as well as a reasonable sampling amount (100). We then compared it to our undersampled and oversampled data. We see that in when we undersample, the quality of the data dramatically dips, and we are left with an incoherent spectogram, both in the time and the frequency domain. Oversampling, on the other hand, produces a slightly clearer spectogram. Although this may sound good in principle, the benefits of oversampling were minimal - the spectograms of the normally sampled and oversampled data looked almost identical - the negatives were that run time severely increased because we were introducing so much more data.\\ \\
\begin{figure}[h]
\begin{center}
\includegraphics[width = 8cm]{shannon}
\includegraphics[width = 8cm]{mxhat}
\caption{\label{fig:scaled_diss} (left) Spectogram of Shannon Filtered Data}
\caption{\label{fig:scaled_diss} (right) Spectogram of Mexican Hat Filtered Data}
\end{center}
\end{figure}

\begin{figure}[h]
\begin{center}
\includegraphics[width = 8cm]{largewindow}
\includegraphics[width = 8cm]{smallwindow}
\caption{\label{fig:scaled_diss} (left) Spectogram of Large Window Filtered Data}
\caption{\label{fig:scaled_diss} (right) Spectogram of Small Window Filtered Data}
\end{center}
\end{figure}
We see that when we decreased the window, we got very precise measurements in the frequency domain. This is because we were essentially taking the fourier transform of a huge area, which will give us lots of information about the frequency, but not the time data. The convserse was true for a large window; We were able to see lots of information in the time domain, but because our window size was so small, we couldn't get as precise frequency measurements. \\ \\
The Mexican Hat function and Step function (Shannon) performed similarly to the Gaussian filter. We see in the spectograms that although they are similar to eachother, while the Shannon filtered data seems to have a bit more accuracy in the frequency domain at the sacrifice of less resolution in the time domain. The time domain is more continuous for the Mexican Hat filtered data, but there is less resolution in the frequency domain. However, these are both useful filters like the Gaussian filter. In both of these, we used a sigma value of 0.05, which changed the width of the filter. We also observed the same window size and undersampling/oversampling effects with these filters.

\begin{figure}[h]
\begin{center}
\includegraphics[width = 16cm]{pianorecorder}
\caption{\label{fig:scaled_diss} (right) Piano and Recorder Spectogram}
\end{center}
\end{figure}

\begin{figure}[h]
\begin{center}
\includegraphics[width = 8cm]{piano}
\includegraphics[width = 8cm]{recorder}
\caption{\label{fig:scaled_diss} (left) Piano Score}
\caption{\label{fig:scaled_diss} (right) Recorder Score}
\end{center}
\end{figure}
\textbf{Part Two} \\
We were able to determine the score of the music from the piano data, after filtering out overtones, and see that they follow the same general pattern, but are at different frequencies. From the spectograms of the data between the recorder and the piano, we see that there seem to be more overtones and undertones on the piano than there are in a recorder. In the case of the piano, we see that outside of the brightly colored frequency areas, there are lots of residual frequencies above and below it. In recorders, we see that most of the other frequencies seem to be below the note.

\section*{\fontsize{19}{15}\selectfont Summary and Conclusions}
We started with music data purely in the time domain. In order to visualize this data in the time and frequency domain, we employed the Ga\'bor transform, which allowed us to create spectograms to visualize the data. We were able to see how changing parameters in the filter, as well as changing the filter itself, affected the transformation. Finally, from music data from a piano and a recorder, we took the G\'abor transform and filtered out overtones in order to reproduce the score.
\section*{\fontsize{19}{15}\selectfont Appendix A}
\subsection*{MATLAB functions used and implementation}
"abs(X)" : Returns the absolute value of every element in X, or complex magnitude if the element is complex. We used this function to normalize the data before plotting our spectogram.\\ \\
"fft(X)" : Performs a fast fourier transform on "X", a 2-D array. We used this fourier transform the filter multiplied by the data at each of our time points.\\ \\
"fftshift(X)" : Rearranges the contents of "X" by placing the zero-frequency component to the center of the array. If "X" is a vector, shifts the left and right halves of "X", while if "X" is a matrix, the $I$ and $III$ quadrants are switched, as well as the $II$ and $IV$ quadrants. We used this to change our transformed data and our axes for proper visualization.  \\ \\
"linspace(x1,x2,n)" : Creates a vector of "n" evenly spaced points from "x1" to "x2". We used this to create our linearly spaced data.  \\ \\
"[M,I] = max(A)" : Returns the maximum value "M" and the index "I"  of the matrix. We used this in part 2 to filter out the overtones by determining the index of the max value of a fourier transform of a time slice.
\pagebreak
\section*{\fontsize{19}{15}\selectfont Appendix B}
\subsection*{MATLAB code}
\begin{lstlisting}[style=Matlab-editor]
%Part One
clear all; close all; clc;
clear all; close all; clc;

load handel
v = y'/2;
v(end) = [];
vt = fft(v);

%My code
L = 9; n = length(v);
t2 = linspace(0, L, n+1); t= t2(1:n);
k = (2*pi/L) * [0:n/2-1 -n/2:-1]; ks = fftshift(k);

subplot(2,1,1)
plot(t,v);
xlabel('Time [sec]');
ylabel('Amplitude');
title('Signal of Interest, v(n)');

subplot(2,1,2)
plot(ks, abs(fftshift(vt)));

%Gaussian Window
tslide= 0:0.1:9;
spc = [];
spcsmallwindow = [];
spclargewindow = [];
spcmxhat = [];
spcshn = [];

figure(2)
for j=1:length(tslide)
    g = exp(-100*(t-tslide(j)).^2);
    gsm = exp(-1*(t-tslide(j)).^2);
    glg = exp(-1000*(t-tslide(j)).^2);

    omega = 0.05;
    mxhat = (2/ (sqrt(3*omega) * pi^(0.25))).* (1-((t-tslide(j))/omega).^2) .* exp(-((t-tslide(j)).^2)/(2 * omega^2));

    sig = 0.05;
    shn = abs(t-tslide(j)) <= sig/2;

    vg = g.*v;
    vgt = fft(vg);

    vgsm = gsm.*v;
    vgsmt =fft(vgsm);

    vglg = glg.*v;
    vglgt =fft(vglg);

    vmx = mxhat.*v;
    vmxt = fft(vmx);

    vshn = shn.*v;
    vshnt = fft(vshn);

%     subplot(2,1,1)
%     plot(t,v,'k-', t, mxhat, 'Linewidth', 2);
%     axis([0 9 -0.5 1])
%
%     subplot(2,1,2)
%     plot(t,vg, 'Linewidth', 2);
%     axis([0 9 -0.4 0.4])
%
    spc = [spc; abs(fftshift(vgt))];
    spcsmallwindow = [spcsmallwindow; abs(fftshift(vgsmt))];
    spclargewindow = [spclargewindow; abs(fftshift(vglgt))];
    spcmxhat = [spcmxhat; abs(fftshift(vmxt))];
    spcshn = [spcshn; abs(fftshift(vshnt))];
    pause(0.00001);
end

tslideover = 0:0.01:9;
spcover = []
for j=1:length(tslideover)
    g = exp(-200*(t-tslideover(j)).^2);
    vg = g.*v;
    vgt = fft(vg);

    spcover = [spcover; abs(fftshift(vgt))];
    pause(0.00001);
end

tslideunder = 0:1:9;
spcunder = []
for j=1:length(tslideunder)
    g = exp(-200*(t-tslideunder(j)).^2);
    vg = g.*v;
    vgt = fft(vg);

    spcunder = [spcunder; abs(fftshift(vgt))];
    pause(0.00001);
end

figure;
%frequency here?
pcolor(tslide,ks./(2*pi),spc.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Normal sampling, normal window");

figure;
pcolor(tslide,ks./(2*pi),spcsmallwindow.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Normal sampling, small window");

figure;
pcolor(tslide,ks./(2*pi),spclargewindow.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Normal sampling, large window");

figure;
pcolor(tslide,ks./(2*pi),spcmxhat.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Mexican Hat, omega = 0.05");

figure;
pcolor(tslide,ks./(2*pi),spcshn.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Step Function (Shannon), sigma = 0.05");

figure;
pcolor(tslideover,ks./(2*pi),spcover.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Oversampling");

figure;
pcolor(tslideunder,ks./(2*pi),spcunder.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Undersampling");

%Part Two
close all; clear all; clc;

pia=(audioread('music1.wav')).';
% plot((1:length(pia))/Fs,pia);
% xlabel('Time [sec]'); ylabel('Amplitude');
% title('Mary had a little lamb (piano)'); drawnow

Lpia=16;  % record time in seconds%
n = length(pia);
Fspia=length(pia)/Lpia;
t2 = linspace(0, Lpia, n+1); tpia= t2(1:n);
kpia = (2*pi/Lpia) * [0:n/2-1 -n/2:-1]; kspia = fftshift(kpia);
tslidepia = 0:0.2:16;

spcpia = [];
pianotes = [];
for j=1:length(tslidepia)
    g = exp(-100*(tpia-tslidepia(j)).^2);

    vgpia = g.*pia;
    vgpiat = fft(vgpia);
    [M,I] = max(vgpiat);

    pianotes = [pianotes; abs(kpia(I))/(2*pi)];
    spcpia = [spcpia; abs(fftshift(vgpiat))];
end

%figure(2)
rec=(audioread('music2.wav')).';
% plot((1:length(rec))/Fs,rec);
% xlabel('Time [sec]'); ylabel('Amplitude');
% title('Mary had a little lamb (recorder)');

Lrec=14;  % record time in seconds
Fsrec=length(rec)/Lrec;
n = length(rec);
t2 = linspace(0, Lrec, n+1); trec= t2(1:n);
krec = (2*pi/Lpia) * [0:n/2-1 -n/2:-1]; ksrec = fftshift(krec);
tsliderec = 0:0.2:16;

spcrec = [];
recnotes = [];
for j=1:length(tsliderec)
    g = exp(-100*(trec-tsliderec(j)).^2);

    vgrec = g.*rec;
    vgrect = fft(vgrec);

    [M,I] = max(vgrect);
    recnotes = [recnotes; abs(krec(I))/(2*pi)];

    spcrec = [spcrec; abs(fftshift(vgrect))];
end

% figure;
%
subplot(2,1,1)
pcolor(tslidepia,(kspia/(2*pi)),spcpia.'), shading interp
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Piano");
ylim([0 400])
subplot(2,1,2)
pcolor(tsliderec,ksrec/(2*pi),spcrec.'), shading interp, colormap(hot)
xlabel("Time (s)");ylabel("Frequency (Hz)"); title("Recorder");
ylim([600 1000])

plot(tslidepia,pianotes,'o','MarkerFaceColor', 'b');
yticks([246.9417,261.6256,277.1826,293.6648,311.127,329.6276,349.2282]);
yticklabels({'B3','C4','C#4','D4','E4','F4'});
ylim([246 350])
title("Score for Piano Music (~250-350Hz");
xlabel("Time (s)"); ylabel("Notes corresponding to frequency (Hz)");

plot(tsliderec,recnotes,'o', 'MarkerFaceColor', 'b')
yticks([698.4565,739.9888,783.9909,830.6094,880,932.3275]);
yticklabels({'F5','F#5','G5','G#5','A5','A#5'});
ylim([680 950])
title("Score for Recorder Music (~680-950Hz)");
xlabel("Time (s)"); ylabel("Notes corresponding to frequency (Hz)");
\end{lstlisting}

\end{document}


\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{algpseudocode}
\usepackage{algorithm}

% Geometry
\usepackage{geometry}
\geometry{letterpaper, left=15mm, top=20mm, right=15mm, bottom=20mm}

% Fancy Header
\usepackage{fancyhdr}
\renewcommand{\footrulewidth}{0.4pt}
\pagestyle{fancy}
\fancyhf{}
\chead{CSC 360 - Analysis of Algorithms}
\lfoot{CALU Fall 2021}
\rfoot{RDK}

% Add vertical spacing to tables
\renewcommand{\arraystretch}{1.4}

% Macros
\newcommand{\definition}[1]{\underline{\textbf{#1}}}

\newenvironment{rcases}
  {\left.\begin{aligned}}
  {\end{aligned}\right\rbrace}

% Begin Document
\begin{document}

\section*{Notes Week 6}

\begin{itemize}

    \item Read Chapters 22, Chapter 9 for Week 7

\end{itemize}

\subsection*{Rules for Big O Notation (Upper Bounds)}

\begin{itemize}

    \item \textbf{Transitivity}: If $f = O(g)$ and $g = O(h)$ then $f = O(h)$

    \item \textbf{Sums}: If $f_1 = O(g_1)$ and $f_2 = O(g_2)$ then $f_1(n) + f_2(n) = O( max(f, g) )$

    \item \textbf{Products}: If $f_1 = O(g_1)$ and $f_2 = O(g_2)$ then $f_1 \times f_2 = O(g_1 \times g_2)$

    \item If the largest term is a polynomial of degree $k$, then the whole thing is $\Theta(n^k)$

    \item $log^kn = O(n) \rightarrow$ a log raised to any constant power is still just $O(n)$

    \item $\lim{n\to\infty} \frac{f(n)}{g(n)} \rightarrow$ If the limit is:
    \begin{itemize}
        \item \textbf{0} $\rightarrow f=o(g) \rightarrow g(n) > f(n) \rightarrow g$ dominates $f$
        \item \textbf{$c \neq 0$} $\rightarrow f = \Theta(g(n)) \rightarrow g(n) = \Theta(f(n))$
        \item \textbf{$\infty$} $\rightarrow g(n) = o(f(n))$
    \end{itemize}

    \item Example: $f(n) = \frac{n}{log(n), g(n) = n^{\frac{1}{2}}log^2n}$

\end{itemize}

\subsection*{Rules for Big $\Omega$ Notation (Lower Bounding)}

\begin{itemize}

    \item \textbf{Comparison-based Solution} $\omega(nlogn) \rightarrow$ it's been proven that comparison-based sorting cannot be faster

    \item Some problems are $\Omega(2^n) \rightarrow$ Super slow

\end{itemize}

\subsection*{Quick Maths}

\begin{itemize}

    \item $b^{log_ba} = a$

    \item $a^{log_bn} = n^{log_ba}$

    \item Any exponential function dominates any polynomial
    \begin{itemize}
        \item Exponential function $\rightarrow 2^n \rightarrow$ variable in the exponent
        \item Polynomial function $\rightarrow n^{234} \rightarrow$ exponent is a constant
    \end{itemize}

    \item $\sum(ca_k + b_k) = c\sum(a_k) + \sum(b_k)$

\end{itemize}

\section*{Recurrences - Recurrence Relations}

\begin{itemize}

    \item A \definition{Recurrence} is used when dealing with recursion.

    \item Merge Sort: \\

    \begin{tabular}{l c c}
        \textbf{Case} & \textbf{Formula} & \textbf{Big O} \\
        base case & $T(n) = \Theta(1)$ & $\Theta(1)$ \\
        not base case & $2T(\frac{n}{2}) + \Theta(n)$ & ?
    \end{tabular}

    \item How does $2T(\frac{n}{2})$ explain the recurrence of merge sort?
    \begin{itemize}
        \item Each division splits the array in half
        \item There are two pieces in each call, one for each half
        \item So the array is split in half, and each is operated on recursively
    \end{itemize}

    \item So what is the $O($merge-sort$)$
    \begin{itemize}

        \item $T(n) = 2T(\frac{n}{2}) + n \rightarrow$ Merge Sort Recurrence
        \item Solve using iteration method: ``Unfold the recurrence''
        \begin{enumerate}

            \item If there's a constant, write out that constant with square brackets empty to indicate something needs done
            \begin{equation}
                2[\ \ \ ] + n
            \end{equation}

            \item Plug the $\frac{n}{2}$ back into the original
            \begin{equation}
                2[ 2T(\frac{n}{4}) + \frac{n}{2} ] + n
            \end{equation}

            \item Multiply it out
            \begin{equation*}
                4T(\frac{n}{4}) + 2n
            \end{equation*}

            \item Take the previous iteration (2) and repeat
            \begin{equation}
                4[ 2T(\frac{n}{8}) + \frac{n}{4} ] + 2n
            \end{equation}

            \item Simplify
            \begin{equation*}
                8T(\frac{n}{8}) + 3n
            \end{equation*}

            \item Repeat the process until you can spot the general case. The next iteration would be
            \begin{equation}
                16T(\frac{n}{16}) + 4n
            \end{equation}

            \item Write down the general case in terms of $k$
            \begin{equation*}
                2^k(\frac{n}{2^k}) + kn
            \end{equation*}

            \item How many times does the recurrence occur? $k = log_2n$

            \item Plug in k
            \begin{equation*}
                2^{log_2n} T(\frac{n}{2^{log_2n}}) + nlog_2n
            \end{equation*}

            \item Simplify
            \begin{equation*}
                n T(1) + nlog_2n = O(nlog_2n)
            \end{equation*}

        \end{enumerate}

    \end{itemize}

\end{itemize}

\end{document}


\documentclass{article}
%\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{color}
\usepackage{tabu}
\usepackage{longtable}
\usepackage{mathrsfs}
\usepackage{enumerate}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{Final 04}
\rhead{Jon Allen}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}
\newcommand{\degree}{\ensuremath{^\circ}}

\begin{document}
\subsubsection*{PDE C.}
\begin{align*}
  \text{PDE.}&&\frac{\partial u}{\partial t}&=\frac{\partial^2u}{\partial x^2}&&\text{for}&0&<x<\infty,&0&<t<\infty\\
  \text{BC.}&&\frac{\partial u}{\partial x}(0,t)&=u(0,t)-\frac{1}{\sqrt{\pi t}}&&\text{for}&&&0&<t<\infty\\
  \text{IC.}&&u(x,0)&=0&&\text{for}&0&<x<\infty
\end{align*}

Solve PDE C completely by a Laplace transform with respect to $t$. Use the BC as stated -- do not transform to homogeneous BC. (The necessary inverse Laplace transform is not in the textbook table but is on the handout list of transforms.)

\begin{align*}
  sU(x)-0&=\frac{\mathrm{d}^2U}{\mathrm{d}x^2}(x)\\
  \frac{\mathrm{d}U}{\mathrm{d}x}(0)&=U(0)-\mathcal{L}\left\{\frac{1}{\sqrt{\pi t}}\right\}\\
  &=U(0)-\frac{1}{\sqrt{s}}\qquad\text{used computer}\\
  0&=\frac{\mathrm{d}^2U}{\mathrm{d}x^2}(x)-sU(x)\\
  0&=r^2+0r-s\\
  r&=\frac{\pm\sqrt{4s}}{2}=\pm\sqrt{s}\\
  U(x)&=c_1e^{x\sqrt{s}}+c_2e^{-x\sqrt{s}}\\
  U'(x)&=c_1\sqrt{s}e^{x\sqrt{s}}-c_2\sqrt{s}e^{-x\sqrt{s}}\\
  U'(0)&=c_1\sqrt{s}-c_2\sqrt{s}=c_1+c_2-\frac{1}{\sqrt{s}}\\
  c_1\sqrt{s}-c_1&=c_2+c_2\sqrt{s}-\frac{1}{\sqrt{s}}\\
  \intertext{used computer to help find convenient values}
  c_1(\sqrt{s}-1)&=\frac{1}{s+\sqrt{s}}+\frac{\sqrt{s}}{s+\sqrt{s}}-\frac{1}{\sqrt{s}}\\
  c_1(\sqrt{s}-1)&=\frac{\sqrt{s}+s}{\sqrt{s}(s+\sqrt{s})}-\frac{s+\sqrt{s}}{\sqrt{s}(s+\sqrt{s})}\\
  c_1=0\\
  c_2&=\frac{1}{s+\sqrt{s}}\\
  U(x)&=\frac{1}{s+\sqrt{s}}e^{-x\sqrt{s}}\\
  \intertext{from handout}
  u(x,t)&=e^{x+t}\text{erfc}\left(\sqrt{t}+\frac{x}{2\sqrt{t}}\right)
\end{align*}
\end{document}


\chapter{Introduction}
\label{chap:intro}

Although Turing machines are a simple (but not quite simplistic) model of computation, there are not many \textit{rigorous} proofs about Turing
machines in the literature.  We think that the following points are reasons for that.  First, their semantics is \textit{unstructured}: from each
state of the machine, the execution can proceed in every other state, similar to the infamous \textit{goto} statement~\cite{dijkstra2002go}.  But even
worse, Turing machines are not \textit{compositional}.  Sequential composition or loops of Turing machines are not \textit{per~se} available.  Even
the formal specification of machines is a burden, because complex machines may have a huge number of internal states.  Last but not least, they are
\textit{low-level}, because the operation on tapes are primitive: read a symbol from the tape, write a symbol to the tape, or move the (read/write)
head in a direction.

For the above reasons, textbooks like Boolos et~al.~\cite{boolos2007computability} leave out detailed proofs of correctness.  They also often only
give an informal description of machines, which obviously makes formal reasoning impossible.  Even if they define the whole machine, they leave out
formal specifications of invariants to figure out for the reader.  To establish that a function is Turing computable, authors often give an informal
description of the algorithm and conclude, using the \textit{Church-Turing thesis}, that the function is Turing-computable. Or they switch to another
abstract machine model, but define the compilation function between the models of computation only informally.

In this thesis, we aim to define, specify, and formally verify Turing machines in a framework built in the theorem prover Coq~\cite{Coq}.  First of
all, we address the problems above.  Instead of defining machines in terms of transition tables, we compose machines using functions of Coq's
dependent type theory -- the \textit{Calculus of (Co)Inductive Constructions} (also known as CIC).  For example, we define a function that builds the
sequential composition of two machines.  To eliminate the need to reason about concrete machine states, we give all states a label (e.g.\ $\true$ or
$\false$) and only have to reason about these labels.  The number of labels is always reasonable small, compared to the potential huge amount of
states.  We address the problem that machines are low-level, by introducing abstractions, so that we can define Turing machines that directly
manipulate values of arbitrary encodable types.  This gives the advantages of register machines, but we are not restricted to natural numbers.

There are many variants of Turing machines.  All variants can be shown to be computationally equivalent.  In this thesis, we choose multi-tape Turing
machines with arbitrary finite alphabets.  Our plan is that each tape should contain a value.  We choose a finite model of tapes.  This means that
each tape has only finitely (but arbitrarily) many symbols.

\section{Contributions}
\label{sec:contributions}

We formalise a variant of deterministic multi-tape Turing machines in the interactive theorem prover Coq.  We build a framework for programming and
formally verifying correctness and time complexity of Turing machines.  Our framework extends the framework by Asperti and
Ricciotti~\cite{asperti2015} in the interactive theorem prover Matita~\cite{asperti2011matita}.  Compared to their framework, we eliminate the need to
reason about concrete machine states and introduce more general control-flow operators.  We increase the level of programming abstraction and make it
possible that Turing machines can directly manipulate values of arbitrary encodable types.  We show that our framework is strong enough to implement
and verify a Turing machine that simulates a two-stack machine for a variant of the $\lambda$-calculus.  We formally prove that the halting problem of
this abstract machine reduces to the halting problem of multi-tape Turing machines.  Thereby, this work is the last step to formally prove that
multi-tape Turing machines can simulate the $\lambda$-calculus.

\section{Related Work}
\label{sec:relatedwork}

Asperti and Ricciotti~\cite{asperti2012} formalise single-tape Turing machines over arbitrary finite alphabets in the interactive theorem prover
Matita.  Matita uses the same constructive type theoretic foundation as Coq.  In~\cite{asperti2015}, they formalise multi-tape Turing machines in
Matita.  They introduce the notion of \textit{realisation} for specifying the semantics of concrete Turing machines.  They define and verify a
universal Turing machine and also formalise the reduction from multi-tape Turing machines to single-tape Turing machines.  Furthermore, they propose
the formalisation of Turing machines as a benchmark for comparing proof assistants.

Xu, Zhang, and Urban~\cite{xu2013} formalise single-tape Turing machines over a binary alphabet in Isabelle/HOL.  They follow the textbook of Boolos
et~al.~\cite{boolos2007computability} and use Hoare-logic to specify the semantics of concrete Turing machines.  They implement formally verified
translation functions from \textit{abacus programs} and \textit{partial recursive functions} to Turing machines and prove the undecidability of the
halting problem of Turing machines.

Ciaffaglione et~al.~\cite{ciaffaglione2016} define tapes of Turing machines as infinite streams.  They verify Turing machines using induction and
co-induction and also show the undecidability of the halting problem in Coq.

Forster, Heiter, and Smolka~\cite{forster2018verification} formally reduce the halting problem of single-tape Turing machines to the \textit{Post
  correspondence problem} (PCP) in Coq.  They use the same definition of Turing machines as we use, but restricted to one tape.  This definition of
single-tape Turing machines was originally presented in~\cite{asperti2012}.

There are other mechanisations of abstract machine models.  For example, Forster and Smolka~\cite{forster2017weak} formalise the theory of computation
in Coq, based on the language~$L$, which is also known as the (weak) call-by-value $\lambda$-calculus.  Norrish~\cite{norrish2011mechanised}
formalises computability theory in HOL4.  He considers a variant of the $\lambda$-calculus and recursive functions, and show that both models of
computation are computationally equivalent.  Kunze et~al.~\cite{KunzeEtAl:2018:Formal} formalise reductions from the programming language $L$ to
several stack-machines.  The stack-machine for that we build a simulator is a variant of a machine of this paper.

\section{Outline}
\label{sec:outline}

In Chapter~\ref{chap:definitions}, we define the notion of multi-tape Turing machines.  We also introduce means to specify the semantics of concrete
machines.  In Chapter~\ref{chap:basic}, we define primitive machines, on which all our machines are based.  In Chapter~\ref{chap:combining}, we define
control-flow operators.  In Chapter~\ref{chap:lifting}, we show how to combine machines with different alphabets and numbers of tapes.  We build
simple machines in Chapter~\ref{chap:compound}.  In Chapter~\ref{chap:programming}, we introduce abstractions that enable the programmer to directly
manipulate values, and we show complex case-studies.  In Chapter~\ref{chap:heap}, we develop our final case-study where we implement and verify a
Turing machine that simulates a two-stack machine for $L$ and show that the halting problem of this machine reduces to the halting problem of
multi-tape Turing machines.  We conclude and discuss possible future work in Chapter~\ref{chap:conclusion}.  In the appendix, we present pearls of the
Coq development of this thesis.

Throughout the thesis, we use mathematical notation, the reader is not required to be expert in type theory.  In the PDF version of this thesis, all
definitions and lemmas are hyperlinked to the documented online source code of the Coq implementation.  The source code is tested to compile with Coq
versions 8.7 and 8.8.  The home page of this thesis contains the PDF version, the source code, and online documentation:
\begin{center}
  \url\homepage
\end{center}

%%% Local Variables:
%%% TeX-master: "thesis"
%%% End:


\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{listings}
\begin{document}
\section{GDA}
\subsection{Abstract}
In this chapter, we will learn an algorithm of linear classification - soft output - probability generation model: GDA (Gaussian Discriminant Analysis).
\subsection{Idea}
In the last chapter, the logistic regression algorithm we learned belongs to probability discriminant model, so the difference between the discriminant model and the generation model is:
\begin{itemize}
	\item the discriminant model is used to model the probability $p(y|x)$ directly to obtain its truly probability value.
	\item the generation model is used to model the joint distribution $(x,y)$ via converting $p(y|x)$ to $p(x|y)p(y)$ according to bayes theorem: $p(y|x)=\frac{p(x|y)p(y)}{p(x)}$. Since $p(x)$ has nothing to do with $y$, it can be omitted. So, finally we get:$$
p(y|x)\propto p(x|y)p(y)=p(x;y)
$$
\end{itemize}
when we are to predict any samples, we just need to compare $p(y=0|x)$ and $p(y=1|x)$.
\subsection{Algorithm}
Firstly, let's make some assumption about the model:
$$
y\in \{0,1\}\quad y\sim Bernuolli(\phi)\quad p(y)=\phi^y(1-\phi)^{1-y}\\\left \{\begin{aligned}x|y=1 \quad \sim \quad N(\mu_1,\Sigma)\\x|y=0 \quad \sim \quad N(\mu_2,\Sigma)\end{aligned}\right.
$$
$$
\Longrightarrow p(x|y)=N(\mu_1,\Sigma)^yN(\mu_2,\Sigma)^{1-y}
$$
so all the parameters $\theta$ of the model are:
$$
\theta=(\phi, \mu_1, \mu_2, \Sigma)
$$
Then given the loss function of the model:
$$
\begin{aligned}
J(\theta)=log(p(Y|X))&=log(\prod_{i=1}^n p(y_i|x_i))\\
&=\sum_{i=1}^n log(p(y_i|x_i))\\
\end{aligned}
$$
so:
$$
\begin{aligned}
\hat{\theta}=argmax(J(\theta))&=argmax(\sum_{i=1}^nlog(\frac{p(x_i|y_i)p(y_i)}{p(x_i)}))\\
&=argmax(\sum_{i=1}^n log(p(x_i|y_i)p(y_i)))\\
&=argmax(\sum_{i=1}^n y_i\ log(N(\mu_1,\Sigma))+(1-y_i)\ log(N(\mu_2,\Sigma))+log(\phi^{y_i} (1-\phi)^{1-y_i}))
\end{aligned}
$$
\subsubsection{Solve $\phi$}
differentiate $\phi$:
$$
\sum_{i=1}^{N} \frac{y_{i}}{\phi}+\frac{y_{i}-1}{1-\phi}=0
\Longrightarrow \phi=\frac{\sum_{i=1}^{N} y_{i}}{N}=\frac{N_{1}}{N}
$$
In the formula, $N,N_1,N_2$ denote the number of all samples, positive samples, negative samples.
\subsubsection{Solve $\mu$}
make some derivations based on $J(\theta)$:
$$
\begin{aligned} \hat{\mu_{1}}
&=\underset{\mu_{1}}{argmax} \sum_{i=1}^{N} y_{i} \log N\left(\mu_{1}, \Sigma\right) \\
&=\underset{\mu_1}{argmax} \sum_{i=1}^{N} y_i \log (\frac{1}{(2\pi)^{\frac{p}{2}}|\Sigma|^{\frac{1}{2}}}exp(-\frac{1}{2}(x_i-\mu_1)^T(\Sigma)^{-1}(x_i-\mu_1)))\\
&=\underset{\mu_{1}}{argmin} \sum_{i=1}^{N} y_{i}\left(x_{i}-\mu_{1}\right)^{T} \Sigma^{-1}\left(x_{i}-\mu_{1}\right)
\end{aligned}
$$
In the above derivations, we quote the probability density function of multivariate Gaussian distribution:
$$
p(x)=\frac{1}{(2\pi)^{\frac{p}{2}}|\Sigma|^{\frac{1}{2}}}exp(-\frac{1}{2}(x_i-\mu_1)^T(\Sigma)^{-1}(x_i-\mu_1))
$$
In the function, $p$ denote the number of random variables. Readers can multiply the probability density function of univariate Gaussian distribution, and derive the multivariate formula with the knowledge of linear algebra.\\
then differentiate the formula:
$$
\frac{\partial \Delta}{\partial \mu_1}=\sum_{i=1}^N -2y_i (\Sigma)^{-1}(x_i-\mu_1)=0\\
\Longrightarrow \mu_{1}=\frac{\sum_{i=1}^{N} y_{i} x_{i}}{\sum_{i=1}^{N} y_{i}}=\frac{\sum_{i=1}^{N} y_{i} x_{i}}{N_{1}}
$$
Since the positive samples and the negative samples are symmetrical, therefore:
$$
\mu_{2}=\frac{\sum_{i=1}^{N}\left(1-y_{i}\right) x_{i}}{N_{2}}
$$
\subsubsection{Solve $\Sigma$}
observe the first two terms of the formula:
$$
\hat{\theta}=argmax(\sum_{i=1}^n y_i\ log(N(\mu_1,\Sigma))+(1-y_i)\ log(N(\mu_2,\Sigma))+log(\phi^{y_i} (1-\phi)^{1-y_i}))
$$
We note that when $y=0$, the first term equals to $0$; when $y=1$, the second term equals to $0$.\\
thus the formula can be updated to:
$$
\begin{aligned}
\hat{\theta}
&=argmax(\sum_{(x_i,y_i)\in C_1} \ log(N(\mu_1,\Sigma))+\sum_{(x_i,y_i)\in C_2}\ log(N(\mu_2,\Sigma)))\\
&=argmax(\sum_{(x_i,y_i)\in C_1} -\frac{1}{2}\log|\Sigma|-\frac{1}{2}(x_i-\mu_1)^T(\Sigma)^{-1}(x_i-\mu_1)\ +\\
&\sum_{(x_i,y_i)\in C_2} -\frac{1}{2}|\Sigma|-\frac{1}{2}(x_i-\mu_2)^T(\Sigma)^{-1}(x_i-\mu_2))
\end{aligned}
$$
Note the shape of $(x_i-\mu)^T(\Sigma)^{-1}(x_i-\mu)$ : $(1,p)* (p,p) * (p,1)=(1,1)$, therefore, the trace(tr) operation can be applied to it, and it can be regarded as a matrix. Within the trace, the order of matrices can be exchanged at will.
$$
\begin{aligned}
\hat{\theta}
&=argmax(-\frac{N}{2}\log|\Sigma|-\frac{1}{2}tr(\sum_{(x_i,y_i)\in C_1}(x_i-\mu_1)^T(\Sigma)^{-1}(x_i-\mu_1))\\
&-\frac{1}{2}tr(\sum_{(x_i,y_i)\in C_2}(x_i-\mu_2)^T(\Sigma)^{-1}(x_i-\mu_2)))\\
&=argmax(-\frac{N}{2}\log|\Sigma|-\frac{1}{2}tr(\sum_{(x_i,y_i)\in C_1}(x_i-\mu_1)^T(x_i-\mu_1)(\Sigma)^{-1})\\
&-\frac{1}{2}tr(\sum_{(x_i,y_i)\in C_2}(x_i-\mu_2)^T(x_i-\mu_2)(\Sigma)^{-1}))\\
&=argmax(-\frac{N}{2}\log|\Sigma|-\frac{1}{2}tr(N_1 S_1(\Sigma)^{-1})
-\frac{1}{2}tr(N_2 S_2(\Sigma)^{-1}))\\
\end{aligned}
$$
In the formula, $S$ denote the co-variance matrix.\\
differentiate the formula:
$$
\frac{\partial \Delta}{\partial \Sigma}=-\frac{1}{2}(N \frac{1}{|\Sigma|}|\Sigma|(\Sigma)^{-1}-N_1S_1(\Sigma)^{-2}-N_2S_2(\Sigma)^{-2})=0
$$
then we obtain the $\hat{\Sigma}$:
$$
N \Sigma^{-1}-N_{1} S_{1}^{T} \Sigma^{-2}-N_{2} S_{2}^{T} \Sigma^{-2}=0\\
\Longrightarrow \hat{\Sigma}=\frac{N_{1} S_{1}+N_{2} S_{2}}{N}
$$
Finally, when we are to predict any samples, we just need to compare $p(x|y=0)p(y=0)$ and $p(x|y=1)p(y=1)$.
\subsection{Implement}
\begin{lstlisting}[language={python}]
import numpy as np
import os
os.chdir("../")
from models.linear_models import GDA

n1 = 1000
n_test = 100
x = np.linspace(0, 10, n1 + n_test)
w1, w2 = 0.3, 0.5
b1, b2 = 0.1, 0.2
x1 = x[:n1]
x_test = x[n1:]
v1 = x1 * w1 + b1
v2 = x1 * w2 + b2
cla_1 = np.c_[x1, v1]
cla_2 = np.c_[x1, v2]
l1 = np.ones(shape=(cla_1.shape[0], 1))
l2 = np.zeros(shape=(cla_2.shape[0], 1))
train_data = np.r_[cla_1, cla_2]
train_label = np.r_[l1, l2]

v_test = x_test * w2 + b2
data_test = np.c_[x_test, v_test]

model = GDA()
model.fit(train_data, train_label)
print(model.get_params())
print("accuary:", model.evaluate(data_test, 0))
\end{lstlisting}
\end{document}