\documentclass[12pt]{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\usepackage{Sweavel}

\usepackage[utf8]{inputenc}
%\usepackage{beamerthemebars}

%\usepackage[spanish]{babel}
\usepackage{fontenc}
\usepackage{graphics}
\usepackage{hyperref}
\usepackage{multimedia}
\usepackage{hyperref}
\usepackage{color}
\usepackage{pgfmath}
\usepackage{tikz}

\usepackage{latexsym} % Símbolos                        ı
\usepackage{amsmath}
\usepackage{amssymb}
\newtheorem{axiom}{Axiom}
\newtheorem{case}{Case}
\newtheorem{claim}{Claim}
\newtheorem{conclusion}{Conclusion}
\newtheorem{condition}{Condition}
\newtheorem{conjecture}{Conjecture}
\newtheorem{assumption}{Assumption}
\newtheorem{propiete}{Propi\'et\'e}
\newtheorem{proposition}{Proposition}
\newtheorem{question}{Question}
\newtheorem{remark}{Remark}

\title{SEIR Epidemiological Model: A Review}
\author{}
\date{
}

\begin{document}
\maketitle

\section{Basic SIR Model with demography}

Model Assumptions:

\begin{enumerate}
\item The infection circulates in a population of size $N,$ with a per capita background death rate, $\mu$ which is balanced by a birth rate $\mu N$ From the sum of Eqs. (2.1)– (2.3), $\frac{dN}{dt} = 0$ and $N = S + I + R$ is thus constant.
\item The infection causes acute morbidity (not mortality); That is, in this version of the SIR model we assume we can ignore disease-induced mortality. This is reasonable for certain infections like chickenpox, but certainly not for others like rabies, SARS, or ebola.
\item Individuals are recruited directly into the susceptible class at birth (so we ignore perinatal maternal immunity).
\item Transmission of infection from infectious to susceptible individuals is controlled by a bilinear contact term $\beta I S$ This stems from the assumption that the $I$ infectious individuals are independently and randomly mixing with all other individuals, so the fraction $S/N$ of the encounters is with susceptible individuals; $\beta$ is the contact rate times the probability of transmission given a contact between a susceptible and an infectious individual.
\item Chances of recovery or death is assumed not to change during the course of infection.
\item Infectiousness is assumed not to change during the course of infection.
\item Infected individuals move directly into the the infectious class (as opposed to the SEIR model;and remains there for an average infectious period of $1/\gamma$ (assuming $\mu << \gamma$).
\item The model assumes that recovered individuals are immune from reinfection for
life.
\end{enumerate}

The model is characterised taking into account the manifold of Susceptible, Infectious and Recovered individuals, namely
$$
\mathcal{M}_{SIR}(N) = \{(S,I,R) \in \mathbb{R}_+^3 : S+I+R = N\}
$$
together the parameter manifold
$$
\mathcal{P}_{SIR} = \{(\mu,\beta,\gamma) \in \mathbb{R}_+^3: \mu << \gamma\}.
$$
Then for a given an observation $(S_0,I_0,R_0) \in \mathcal{M}_N$ and $(\mu,\beta,\gamma) \in \mathcal{P}_{SIR}$ in time $t=0$ the
evolution of the system is characterised by the ODE:
\begin{align}
\dot{S}(t) & = \mu(N-S)-\beta S \frac{I}{N}, \label{SIR1}\\
\dot{I}(t) & = \beta S \frac{I}{N} - (\mu + \gamma)I, \label{SIR2}\\
\dot{R}(t) & = \gamma I - \mu R \label{SIR3}
\end{align}

For directly transmitted pathogens, $R_0$ is, per definition, the expected number of secondary cases that arise from a typical infectious index-case in a completely susceptible host population. $R_0$ plays a critical role for a number of aspects of disease dynamics and is therefore the focus of much study in historical and contemporary infectious disease dynamics. $(R_0)$ is a very important quantity in epidemiology. For this simple SIR model $$R_0 = \frac{\beta}{\gamma + \mu}$$

We need to load the library to solve numerically an ODE:

\begin{Schunk}
\begin{Sinput}
library(deSolve)
\end{Sinput}
\end{Schunk}

We construct the SIR model function:

\begin{Schunk}
\begin{Sinput}
sirmod = function(t, y, parms) {
  # Pull state variables from y vector
  S = y[1]
  I = y[2]
  R = y[3]
  # Pull parameter values from parms vector beta = parms["beta"]
  mu = parms["mu"]
  gamma = parms["gamma"]
  N = parms["N"]
  beta = parms["beta"]
  # Define equations
  dS = mu * (N - S) - beta * S * I/N
  dI = beta * S * I/N - (mu + gamma) * I
  dR = gamma * I - mu * R
  res = c(dS, dI, dR)
  # Return list of gradients
  list(res)
}
\end{Sinput}
\end{Schunk}

We introduce the parameter values

\begin{Schunk}
\begin{Sinput}
times = seq(0, 26, by = 1/10)
parms = c(mu = 0.2, N = 1, beta = 2, gamma = 1/2)
start = c(S = 0.999, I = 0.001, R = 0.1)
\end{Sinput}
\end{Schunk}

We integrate numerically \eqref{SIR1}-\eqref{SIR3}

\begin{Schunk}
\begin{Sinput}
out = ode(y=start, times=times, func=sirmod, parms= parms)
out = as.data.frame(out)
head(round(out, 3))
\end{Sinput}
\begin{Soutput}
  time     S     I     R
1  0.0 0.999 0.001 0.100
2  0.1 0.999 0.001 0.098
3  0.2 0.999 0.001 0.096
4  0.3 0.998 0.001 0.094
5  0.4 0.998 0.002 0.093
6  0.5 0.998 0.002 0.091
\end{Soutput}
\end{Schunk}

Finally, we plot the numerical solution

\begin{Schunk}
\begin{Sinput}
#Calculate R0
R0=parms["beta"]/(parms["gamma"]+parms["mu"])
#Adjust margins to accommodate a second right axis
par(mar = c(5,5,2,5))
#Plot state variables
plot(x=out$time, y=out$S, ylab="Fraction", xlab="Time",type="l")
lines(x=out$time, y=out$I, col="red")
lines(x=out$time, y=out$R, col="green")
#Add vertical line at turnover point
xx = out$time[which.max(out$I)]
lines(c(xx,xx), c(1/R0,max(out$I)), lty=3)
#prepare to superimpose 2nd plot
par(new=TRUE)
#plot effective reproductive ratio (w/o axes)
plot(x=out$time, y=R0*out$S, type="l", lty=2, lwd=2,
     col="black", axes=FALSE, xlab=NA, ylab=NA,
ylim=c(-.5, 4.5))
lines(c(xx, 26), c(1,1), lty=3)
#Add right-hand axis for RE
axis(side = 4)
mtext(side = 4, line = 4, expression(R[E])) #Add legend
legend("right", legend=c("S", "I", "R",expression(R[E])),
	lty=c(1,1,1, 2), col=c("black", "red", "green", "black"))
\end{Sinput}

\includegraphics[width=\maxwidth]{figure/unnamed-chunk-5-1} \end{Schunk}

\section{The SEIR Model}

We briefly introduce a refinement to the SIR model to take into account the latent period. The process of transmission often occurs due to an initial inoculation with a very small number of pathogen units (e.g., a few bacterial cells or virions). A period of time then ensues during which the pathogen reproduces rapidly within the host, relatively unchallenged by the immune system. During this stage, pathogen abundance is too low for active transmission to other susceptible hosts, and yet the pathogen is present. Hence, the host cannot be categorized as susceptible, infectious, or recovered; we need to introduce a new category for these individuals who are infected but not yet infectious. These individuals are referred to as Exposed and are represented by the variable $E$ in SEIR models.

We assume that:
\begin{itemize}
\item An average infective individual produces $\beta$ new infections per unit of time when all contacts are with susceptible but that otherwise, this rate is reduced by the ratio $S/N.$
\item Individuals in the exposed class $E$ progress to the infective class at the per capita rate $k.$
\item There is no disease-induced mortality or permanent immunity, and there is a mean infective period of $1/\sigma.$
\end{itemize}
Then the SEIR equations are:
\begin{align}
\dot{S}(t) & = \mu(N-S)-\beta S \frac{I}{N}, \label{SEIR1}\\
\dot{E}(t) & = \beta S \frac{I}{N} - (\mu+\sigma)E, \label{SEIR2} \\
\dot{I}(t) & = \sigma E - (\mu + \gamma)I, \label{SEIR3}\\
\dot{R}(t) & = \gamma I - \mu R \label{SEIR4}
\end{align}
The model is characterised taking into account the manifold of Susceptible, Exposed, Infectious and Recovered individuals, namely
$$
\mathcal{M}_{SEIR}(N) = \{(S,E,I,R) \in \mathbb{R}_+^4 : S+E+I+R = N\}
$$
together the parameter manifold
$$
\mathcal{P}_{SEIR} = \{(\mu,\beta,\gamma,\sigma) \in \mathbb{R}_+^4: \mu << \gamma\}.
$$

We construct the SIR model function:

\begin{Schunk}
\begin{Sinput}
seirmod = function(t, y, parms) {
  # Pull state variables from y vector
  S = y[1]
  E = y[2]
  I = y[3]
  R = y[4]
  # Pull parameter values from parms vector beta = parms["beta"]
  mu = parms["mu"]
  gamma = parms["gamma"]
  N = parms["N"]
  beta = parms["beta"]
  sigma = parms["sigma"]
  # Define equations
  dS = mu * (N - S) - beta * S * I/N
  dE = beta * S * I/N - (mu + sigma) * E
  dI = sigma * E  - (mu + gamma) * I
  dR = gamma * I - mu * R
  res = c(dS, dE, dI, dR)
  # Return list of gradients
  list(res)
}
\end{Sinput}
\end{Schunk}

We introduce the parameter values

\begin{Schunk}
\begin{Sinput}
times = seq(0, 30, by = 1/10)
parms = c(mu = 0.01, N = 1, beta = 2, gamma = 1/2, sigma = 0.1)
start = c(S = 0.999, E=0.05, I = 0.2, R = 0.1)
\end{Sinput}
\end{Schunk}

We integrate numerically \eqref{SEIR1}-\eqref{SEIR4}

\begin{Schunk}
\begin{Sinput}
out = ode(y=start, times=times, func=seirmod, parms= parms)
out = as.data.frame(out)
head(round(out, 3))
\end{Sinput}
\begin{Soutput}
  time     S     E     I     R
1  0.0 0.999 0.050 0.200 0.100
2  0.1 0.961 0.088 0.191 0.110
3  0.2 0.926 0.122 0.182 0.119
4  0.3 0.893 0.152 0.175 0.128
5  0.4 0.863 0.181 0.167 0.136
6  0.5 0.836 0.206 0.161 0.144
\end{Soutput}
\end{Schunk}

Finally, we plot the numerical solution

\begin{Schunk}
\begin{Sinput}
#Calculate R0
R0=(parms["beta"]*parms["sigma"])/
((parms["gamma"]+parms["mu"])*(parms["sigma"]+parms["mu"]))
#Adjust margins to accommodate a second right axis
par(mar = c(5,5,2,5))
#Plot state variables
plot(x=out$time, y=out$S, ylab="Fraction", xlab="Time",type="l")
lines(x=out$time, y=out$E, col="purple")
lines(x=out$time, y=out$I, col="red")
lines(x=out$time, y=out$R, col="green")
#Add vertical line at turnover point
xx = out$time[which.max(out$I)]
lines(c(xx,xx), c(1/R0,max(out$I)), lty=3)
#prepare to superimpose 2nd plot
par(new=TRUE)
#plot effective reproductive ratio (w/o axes)
plot(x=out$time, y=R0*out$S, type="l", lty=2, lwd=2,
     col="black", axes=FALSE, xlab=NA, ylab=NA,
ylim=c(-.5, 4.5))
lines(c(xx, 26), c(1,1), lty=3)
#Add right-hand axis for RE
axis(side = 4)
mtext(side = 4, line = 4, expression(R[E])) #Add legend
legend("topright", legend=c("S","E" ,"I", "R",expression(R[E])),
  lty=c(1,1,1,1,2), col=c("black","purple" , "red", "green", "black"))
\end{Sinput}

\includegraphics[width=\maxwidth]{figure/unnamed-chunk-9-1} \end{Schunk}

\end{document}


\section{Grammar Transformations}

\subsection{Expansion of a nonterminal}
Eliminate a nonterminal: $A \rarr \alpha B \gamma$, $B \rarr \beta_1 | \ldots | \beta_n$ becomes $A \rarr \alpha \beta_1 \gamma | \ldots | \alpha \beta_n \gamma$.

It's always possible to remove the axiom from the right part by adding a new axiom $S_0 \rarr S$.

\subsection{Elimination of empty rules}
A nonterminal is \textbf{nullable} iff it exists a derivation $A \Rightarrow^+ \epsilon$.
\begin{align*}
    A \in \text{Null} &\text{ if }A \rarr \epsilon \in P \\
    A \in \text{Null} &\text{ if } A \rarr A_1\ldots A_n \in P, A_i \in V \setminus \{A\} \land \forall A_i (A_i \in \text{Null})
\end{align*}
\textbf{Note}: Recursive rules cannot be used.

\paragraph{Construction of the non-nullable normal form}
\begin{itemize}
    \item Compute the Null set.
    \item For each rule add as alternative all the combinations of removing the nullable nonterminals.
    \item Remove all empty rules ($A \rarr \epsilon$) excpet for $A = S$.
    \item Clean the grammar and remove circularity.
\end{itemize}

\textbf{Example} $S \rarr SAB | AC$, $A \rarr aA | \epsilon$, $B \rarr bB | \epsilon$, $C \rarr cC | c$. Null is $\{A, B\}$, remove $A$ and $B$ from the rules in every combination: $S \rarr SAB | SA | SB | S | AC | C$, $A \rarr aA | a$, $B \rarr bB | b$, $C \rarr cC | c$. Then remove circularity ($S \rarr S$).

\subsection{Elimination of copy rules}
$\text{Copy}(A) = \{ B \in V | A \Rightarrow^* B \}$.

Assuming \textbf{grammar with no empty rules}, apply until a fixed point:
\begin{align*}
    A &\in \text{Copy}(A) \\
    C &\in \text{Copy}(A) \text{ if } B \in \text{Copy}(A) \land B \rarr C \in P
\end{align*}

\paragraph{Construction of the grammar without copy rules}

\begin{itemize}
    \item Remove copy rules: $P' := P \setminus \{A\rarr B | A, B \in P\}$
    \item Add compensating rules: $P' := P' \cup \{ A \rarr \alpha | \exists B (B \in \text{Copy}(A) \land (B \rarr \alpha \in P)) \}$
\end{itemize}

\textbf{Example} $E \rarr E + T | T$, $T \rarr T \times C | C$, $C \rarr 0|\ldots|9$.
$\text{Copy}(E)=\{E,T,C\}$, $\text{Copy}(T) = \{T, C\}$, $\text{Copy}(C) = \{C\}$.
The grammar becomes $E \rarr E+T|T\times C|0|\ldots|9$, $T \rarr T\times C|0|\ldots|9$, $C \rarr 0|\ldots|9$.

\subsection{Conversion from left to right recursion}

\paragraph{Immediate L-recursions} $A \rarr A\beta_1 | \ldots | A\beta_h$ where no $\beta_i$ is empty, $A \rarr \gamma_1 | \ldots | \gamma_k$. Introduce new nonterminal $A'$:
\begin{align*}
    A &\rarr \gamma_1A' | \ldots | \gamma_kA' | \gamma_1 | \ldots | \gamma_k \\
    A' &\rarr \beta_1A' | \ldots | \beta_hA' | \beta_1 | \ldots | \beta_h
\end{align*}

\textbf{Example} $E \rarr E+T | T$, $T \rarr T\times F | F$, $F \rarr (E) | i$. It becomes $E \rarr TE' | T$, $E' \rarr +TE'|+T$, $T \rarr FT'|F$, $T' \rarr \times FT'|\times F$, $F\rarr (E)i$.

\textbf{Non-Immediate L-recurions} are much harder and not covered by the slides.


\documentclass[main.tex]{subfiles}
\begin{document}

\section*{Fri Nov 22 2019}

We found the equation
%
\begin{align}
  \frac{1}{l^2} \qty(\dv{r}{\lambda })^2 + W _{\text{eff}}(r) = \frac{1}{b^2}
\,,
\end{align}
%
where
%
\begin{align}
  W _{\text{eff}} = \frac{1}{r^2} \qty(1 - \frac{2GM}{r})
\,
\end{align}
%
and
%
\begin{align}
  b^2 = \frac{l^2}{e^2}
\,,
\end{align}
%
\(l\) and \(e\) being the integrals corresponding to the Killing vectors of time translations and azimuthal angle rotations.

Now, we want to give the interpretation of \(b\) as the impact parameter.
We consider a BH at the origin of the \(x, y\) axes, and a photon approaching parallel to the \(x\) axis with impact parameter \(d\). The impact parameter is the distance between the two lines: the trajectory of the photon far away from the BH and the line parallel to the trajectory and passing through the BH.

We can calculate
%
\begin{align}
  \dv{\varphi }{t} = \underbrace{\dv{r}{t}}_{-1} \dv{\varphi }{r} = - \qty(-\frac{d}{r^2})
\,,
\end{align}
%
where we used a small angle approximation: \(\varphi \approx d/r\), which can be differentiated with respect to \(r\).
So
%
\begin{align}
  b = \frac{l}{e} = \frac{r^2 \dv*{\varphi }{\lambda }}{\dv*{t}{\lambda }} = r^2 \dv{\varphi }{t} = d
\,,
\end{align}
%
which means that \(b=d\): the ratio of \(l\) to \(e\) is the impact parameter.

So, the photon interacts with the BH and the angle of deflection of the path compared to a straight path is denoted as \(\delta \varphi \).
The total deflection angle is \(\Delta \varphi  = \pi + \delta \varphi \).

The parameter \(l\) is
%
\begin{align}
  l = r^2 \dv{\varphi }{\lambda }
\,,
\end{align}
%
so
%
\begin{align}
  \dv{}{\lambda } = \frac{l}{r^2} \dv{}{\varphi }
\,.
\end{align}

This allows us to change variables in our 1D equation, and we get
%
\begin{align}
\frac{1}{l^2} \frac{l^2}{r^2} \qty(\dv{r}{\varphi })^2 + \frac{1}{r^2}\qty(1 - \frac{2GM}{r}) = \frac{1}{b^2}
\,,
\end{align}
%
so if we change variables to \(u = r^{-1}\), with
%
\begin{align}
  \dv{r}{\varphi } = -\frac{1}{u^2} \dv{u }{\varphi }
\,,
\end{align}
%
we  get
%
\begin{align}
  u^{4} u^{-4} \qty(\dv{u}{\varphi } )^2+ u^2 \qty(1-2GMu) = \frac{1}{b^2}
\,,
\end{align}
%
and then, differentiating, we find
%
\begin{align}
  2 \dv{u}{\varphi } \dv[2]{u}{\varphi } + 2 u \dv{u}{\varphi } - 6GM u^2 \dv{u}{\varphi }= 0
\,,
\end{align}
%
and we can simplify as long as \(\dv*{u}{\varphi } \neq 0\), which only fails at one point. So in the end our equation is
%
\begin{align}
  \dv[2]{u}{\varphi } + u = 3GMu^2
\,.
\end{align}
%

We solve it perturbatively.
If \(GM=0\), there is no BH and we have a straight line: the impact parameter is constant and equal to  \(b = r \sin(\varphi )\), therefore
%
\begin{align}
  u = \frac{1}{b} \sin(\varphi )
\,,
\end{align}
%
is the most general solution to the harmonic oscillator which satisfies the boundary conditions.
So, we hypothesize that our solution satisfies
%
\begin{align}
  u (\varphi ) = \frac{1}{b} \qty(\sin(\varphi ) + W(\varphi ))
\,,
\end{align}
%
where \(W\) is small.

Then, we insert this:
%
\begin{align}
  -\frac{1}{b} \sin(\varphi ) + \frac{1}{b} \dv[2]{W}{\varphi } +
  \frac{1}{b} \sin(\varphi )
  + \frac{1}{b} W \approx 3GM \frac{\sin^2(\varphi )}{b}
\,,
\end{align}
%
but since the zeroth order equation is satisfied we find:
%
\begin{align}
  \dv[2]{W}{\varphi } + W \approx \frac{3GM}{b} \sin^2(\varphi )
\,.
\end{align}

Our ansatz is then:
%
\begin{align}
  W = A + B \sin^2 \varphi
\,,
\end{align}
%
which looks like it might solve the equation. Its second derivative is
%
\begin{align}
  \dv[2]{W}{\varphi } = 2B \qty(\cos^2 \varphi  - \sin^2 \varphi ) = 2B - 4B \sin^2\varphi
\,.
\end{align}

Inserting this we get:
%
\begin{align}
  2B - 4B \sin^2\varphi + A + B \sin^2\varphi
  = \frac{3GM}{b} \sin^2\varphi
\,,
\end{align}
%
which implies \(2B+A = 0\) and \(-3B = 3GM/b\). Then,
%
\begin{align}
  W = \frac{2GM}{b} - \frac{GM}{b} \sin^2\varphi
  = \frac{2GM}{b} \qty(1 - \frac{\sin^2\varphi}{2} )
\,
\end{align}
%
solves the perturbed equation.
We can see that our condition of \(W \ll 1\) is actually the physically meaningful \(GM \ll b\), or the impact parameter being much larger than the Schwarzschild radius.

Our complete solution is
%
\begin{align}
  u (\varphi ) = \frac{1}{b} \qty(\sin \varphi + \frac{2GM}{b} \qty(1 - \frac{\sin^2\varphi}{2}))
\,,
\end{align}
%
and we are interested in the asymptotic past and future, which correspond to \(u =0\).
Now, \(\varphi _{\text{in}}= 0\) and \(\varphi _{\text{out}} = \pi\) will not be a solution anymore. However, the deflection is small so we write the solution as \(\varphi _{\text{in}} = \epsilon _{\text{in}}\) and \(\varphi _{\text{out}} = \pi + \epsilon _{\text{out}}\).
We substitute these in to the equation \(u = 0\):
%
\begin{align}
  0 = \sin(\epsilon _{\text{in}}) + \frac{2GM}{b}
\,,
\end{align}
%
or \(\epsilon _{\text{in}} \approx - 2GM/b\), since the deflection is small.

For \(\epsilon _{\text{out}}\) we will have
%
\begin{align}
  0= \sin(\pi + \epsilon _{\text{out}}) + \frac{2GM}{b} \approx - \epsilon _{\text{out}} + \frac{2GM}{b}
\,,
\end{align}
%
so \(\delta \varphi = 2 \varphi _{\text{in}} = 2 \varphi _{\text{out}} = 4GM/b\).

This was one of the first tests of GR by Sir Eddington in 1919: during an eclipse he saw a shift in the apparent position of the stars. reinserting \(c\) we find that we must divide \(4GM/b\) by \(c^2\).
Our \(b\) is approximately the radius of the Sun: the calculation is
\begin{lstlisting}[language=Python]
from scipy.constants import c, G, pi
sun_mass = 2e30
sun_radius = 696e6
rad2arcsec = 3600 * 180 / pi
4*G*sun_mass/c**2 /sun_radius * rad2arcsec
\end{lstlisting}

\section{Horizons and coordinate systems}

For the rest of today, we will talk about the Schwarzschild horizon: recall the line element
%
\begin{align}
  \dd{s^2} = - \qty(1- \frac{2GM}{r}) \dd{t^2}
  + \qty(1 - \frac{2GM}{r})^{-1} \dd{r^2}
  + r^2 \dd{\Omega^2}
\,.
\end{align}
%

It is useful to plot light cones in order to understand the structure of the spacetime.
We restrict ourselves to radial motion of light. So, we have
%
\begin{align}
  0 = - \qty(1- \frac{2GM}{r}) \dd{t^2}
  + \qty(1 - \frac{2GM}{r})^{-1} \dd{r^2}
\,,
\end{align}
%
or
%
\begin{align}
  \dv{t}{r} = \pm \qty(1 - \frac{2GM}{r})^{-1}
\,.
\end{align}
%

The light cones become slimmer and slimmer as we approach the horizon, they are straight lines at \(r = 2GM\): the photon appears to cover less and less \(\dd{r}\) for a fixed \(\dd{t}\) as it approaches the horizon.
Massive particles are even slower.
Let us integrate this relation:
%
\begin{align}
  \int_0^t \dd{t} = - \int_{r_0}^{r(t)} \frac{\dd{r}}{1 - \frac{2GM}{r}}
\,,
\end{align}
%
which comes out, by a separation of fractions, to be
%
\begin{align}
  t = r_0 - r(t) + 2GM \log \qty(r_0 - 2GM) - 2GM \log \qty(r(t) - 2GM)
\,,
\end{align}
%
which diverges as \(r(t)\) approaches \(2GM\).

What do we really mean by \(t\)?
An observer which is far away and at rest has \(g_{\mu \nu } = \eta_{\mu \nu }\), and \(t = \tau \): \(t\) is the proper time, as measured on the clock of an observer who is far away.
They will measure the photon as going slower and slower, and becoming redder and redder.

Let us neglect Doppler redshift, which is due to motion.
The formula for gravitational redshift is
%
\begin{align}
  f _{\text{obs}} = f _{\text{emit}} \sqrt{\frac{- g_{00} (\text{emit})}{-g_{00} (\text{obs})}}
\,.
\end{align}
%

In our case we have
%
\begin{align}
  f _{\text{obs}} = f _{\text{emit}} \sqrt{1 - \frac{2GM}{r}}
\,,
\end{align}
%
which approaches zero as \(r \rightarrow 2GM\).
We see the infalling observer becoming redder and ultimately freezing.

We should use different coordinates to describe the infalling observer who passes through the event horizon.

First of all, we discuss Minkowski spacetime as seen by an accelerating observer: Rindler spacetime and the Riddler horizon.

Recall the exercise in sheet 2, about an accelerating observer: we now consider an observer moving with the position law
%
\begin{align}
  x(t) = \frac{1}{\kappa } \sqrt{1 + \kappa^2t^2}
\,,
\end{align}
%
(as opposed to the homework, we remove the constant added to this position).
Like in the homework we compute the proper time for the observer:
%
\begin{align}
  \dd{s^2} = - \dd{t^2} \qty(1 - \qty(\dv{x}{t})^2)
\,,
\end{align}
%
so
%
\begin{align}
  \dd{\tau} = \frac{ \dd{t}}{\sqrt{1 + \kappa^2t^2}}
\,,
\end{align}
%
which means
%
\begin{align}
t = \frac{1}{\kappa } \operatorname{sinh} (\kappa \tau )
    \qquad
\tau  = \frac{1}{\kappa } \operatorname{arcsinh} (\kappa \tau )
\,,
\end{align}
%
therefore
%
\begin{align}
  x = \frac{1}{\kappa } \cosh(\kappa \tau )
\,.
\end{align}
%

We want a coordinate system in which
\begin{enumerate}
    \item the observer is at constant spatial position;
    \item where, up to a constant, the time is equal to the proper time.
\end{enumerate}

Our change of variable is
%
\begin{subequations}
\begin{align}
  \begin{cases}
      t = \rho \sinh \eta  \\
      x = \rho \cosh \eta
  \end{cases}
\,.
\end{align}
\end{subequations}
%

the observer is at fixed spatial coordinate \(\rho_{*} = 1 / \kappa \), and the proper time measured is \(\tau = \eta / \kappa = \eta \rho_{*}\).

Let us consider a family of observers at different spatial locations in the new frame: each has a constant acceleration, this means varying \(\kappa \) or \(\rho \).

If instead we vary \(\eta \) we have:
%
\begin{align}
  \frac{t}{x} = \tanh \eta \implies
  \eta = \tanh^{-1} \qty(\frac{t}{x})
\,,
\end{align}
%
and we can see that since \(\tanh 0 = 0 \) we have that the \(t=0\) axis has \(\eta = 0\), while the lightspeed observers are at \(\eta = \pm \infty\).

These coordinates cover one quadrant of Minkowski spacetime.
The line element in these new coordinates is
%
\begin{subequations}
\begin{align}
  \dd{s^2} &= - \dd{t^2} + \dd{x^2}   \\
  &= - \qty(\dd{\rho^2} \sinh \eta  + \rho \cosh \eta \dd{\eta })^2
  + \qty( \dd{\rho^2} \cosh \eta + \rho \sinh \eta \dd{\eta })^2  \\
  &= - \rho^2 \dd{\eta^2} + \dd{\rho^2}
\,.
\end{align}
\end{subequations}
%

This is \emph{Rindler geometry}.

If we have an observer staying still at \(x_0 >0\), and there is a Rindler (accelerating) observer, then after a time \(x_0\) the observer exits the quarter of the plane covered by the Rindler coordinates: if event \(A\) is at \((0, x_0 )\), and \(B\) is at \((x_0 , x_0 )\) then the \(\eta \) of event \(A\) is zero, while the \(\eta \) of event \(B\) is infinite.

This means that after a time \(x_0 \) the stationary observer becomes causally disconnected from the Rindler observer.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{physics}
\usepackage{amsmath}
\usepackage{showlabels}
\usepackage{amssymb}
\renewcommand{\vec}[1]{\ensuremath{\mathbf{#1}}}
\newcommand{\uvec}[1]{\ensuremath{\hat{\mathbf{#1}}}}

\title{Statistical Computing for Scientists and Engineers\\[1em] Final Homework}
\author{Jiale Shi}
\date{Dec/10/2018}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\maketitle

\newpage
\section{EM algorithm}
Implement the EM algorithm for estimating the parameters of a mixture of Gaussians with isotropic covariances using the data provided on data resources. There are two data sets each of which is two-dimensional. You can write your own or use any available code for mixture of Gaussians (e.g. you ca use the code in the code director with some changes to account for the isotropic covariances. Also see the accompanying paper Unsupervised Learning of Finite Mixture Models, M.Figueiredo and A.K. Jain.)

Experiment with the number of mixtures and comment on the trade-off between the number of mixture and goodness of fit (i.e. log-likelihood) of the data. Plot the log-likelihood as a function of the number of components of a mixture of Gaussians to support your argument.

Find a fixed number of Gaussians that works well for each data set.

Plot the estimated Gaussians as one-sigma contours of each mixing component on top of the training data.

List the mean, covariance and mixing weights of each mixture component.

\textbf{Solution}:
Implement the EM algorithm for estimating the parameters of a mixture of Gaussians with isotropic covariances. The isotropic covariances maxtrix means the covariance matrix is diagonal and all elements on the diagonal is equal.

\begin{equation}
    \textbf{C}_{\mbox{isotropic}} = \lambda \textbf{I} = \left( \begin{array}{cc}  \lambda & 0 \\ 0 & \lambda
    \end{array} \right)
\end{equation}

The matlab code (covoption=1) can deal with diag covariances matrix.
For diag cov:
\begin{equation}
    \textbf{C}_{\mbox{diag}} =  \left( \begin{array}{cc}  \lambda_1 & 0 \\ 0 & \lambda_2
    \end{array} \right)
\end{equation}

\begin{equation}
\begin{aligned}
& \mbox{For diag cov}\\
& \sigma_{ik}^{2} = \frac{\sum_n \frac{1}{N}(x_{in}-\mu_{ik})^2}{\sum_{n=1}^{N} \frac{1}{N}} & i = 1,2 \\
& \mbox{For isotropic cov} \\
& \sigma_{k}^{2} = \frac{\sum_{n=1}^{N} \frac{1}{N}||\vec{x}_{n}-\vec{\mu}_{k}||^2}{2 \sum_n \frac{1}{N}} =  \frac{\sigma_{1k}^{2}+\sigma_{2k}^{2}}{2}
\end{aligned}
\end{equation}

Therefore, once getting the diag cov $\left( \begin{array}{cc}  \sigma_{1k}^{2} & 0 \\ 0 & \sigma_{2k}^{2}
    \end{array} \right)$, I change it based on the diga cov to get isotropic cov.
\begin{equation}
    \textbf{C}_{\mbox{isotropic},k}  = \left( \begin{array}{cc}  \sigma_{k}^{2} & 0 \\ 0 & \sigma_{k}^{2}
    \end{array} \right)  = \left( \begin{array}{cc}  \frac{\sigma_{1k}^{2}+\sigma_{2k}^{2}}{2} & 0 \\ 0 & \frac{\sigma_{1k}^{2}+\sigma_{2k}^{2}}{2}
    \end{array} \right)
\end{equation}
Other steps are the same as diag cov.
\newpage
For \textbf{data1train.dat}:

\begin{figure}[h!]
\centering
\includegraphics[scale=0.6]{fig/HW6P1_11.png}
\caption{log-likelihood as a function of the number of components of amixture of Gaussians for \textbf{data1train.dat}}
%\label{fig:HW6P1_1}
\end{figure}

fixed number of Gaussians that works well for \textbf{data1train.dat} is \textbf{14}.

\begin{figure}[h!]
\centering
\includegraphics[scale=0.6]{fig/HW6P1_12.png}
\caption{Optimal Gaussians mixtures that work for \textbf{data1train.dat}}
%\label{fig:HW6P1_1}
\end{figure}

\newpage
List the mean, covariance and mixing weights for each mixture component.

\begin{math}
\begin{aligned}
\mbox{best mean} & \mbox{best covariance} & \mbox{best mixing weight} \\
\left( \begin{array}{c}  1.3716  \\   -9.6936
\end{array} \right)
& \left( \begin{array}{cc}   0.8763 & 0 \\   0 & 0.8763
\end{array} \right)
&   0.0646  \\ %1
\left( \begin{array}{c}  6.1686 \\  8.0496
\end{array} \right)
& \left( \begin{array}{cc}   1.2756 & 0 \\   0 & 1.2756
\end{array} \right)
&   0.0667\\ %2
\left( \begin{array}{c}   -4.3445 \\  -8.8821
\end{array} \right)
& \left( \begin{array}{cc}   1.0669  & 0 \\   0 & 1.0669
\end{array} \right)
&   0.0471 \\ %3
\left( \begin{array}{c}  -9.7203 \\  -1.4463
\end{array} \right)
& \left( \begin{array}{cc}   1.4420 & 0 \\   0 & 1.4420
\end{array} \right)
&   0.0651\\ %4
\left( \begin{array}{c}  4.5894 \\  -8.5408
\end{array} \right)
& \left( \begin{array}{cc}    1.3080 &  0 \\   0 &   1.3080
\end{array} \right)
&   0.0724\\ %5
\left( \begin{array}{c}   9.3126 \\  4.0447
\end{array} \right)
& \left( \begin{array}{cc} 1.8687  & 0 \\   0 & 1.8687
\end{array} \right)
&   0.0747\\ %6
\left( \begin{array}{c}   -10.0447 \\  2.2659
\end{array} \right)
& \left( \begin{array}{cc}   1.6937  & 0 \\  0   & 1.6937
\end{array} \right)
&   0.0809\\ %7
\left( \begin{array}{c}  -1.4953 \\  -10.0566
\end{array} \right)
& \left( \begin{array}{cc}   0.8459   & 0 \\   0 & 0.8459
\end{array} \right)
&   0.0510 \\ %8
\left( \begin{array}{c}   9.8751 \\  -0.6106
\end{array} \right)
& \left( \begin{array}{cc}   1.9610 & 0 \\   0 & 1.9610
\end{array} \right)
&   0.1189 \\ %9
\left( \begin{array}{c} -7.6513 \\  -6.2702
\end{array} \right)
& \left( \begin{array}{cc}   1.3372 & 0 \\   0 & 1.3372
\end{array} \right)
&   0.0582\\ %10
\left( \begin{array}{c}  1.8042 \\  9.7706
\end{array} \right)
& \left( \begin{array}{cc}   1.7976 &  0 \\   0 &  1.7976
\end{array} \right)
&   0.0787\\ %11
\left( \begin{array}{c}   8.0447 \\  -5.6998
\end{array} \right)
& \left( \begin{array}{cc}   1.1455  & 0 \\   0 &  1.1455
\end{array} \right)
&   0.0753\\ %12
\left( \begin{array}{c}   -3.6905 \\  9.1483
\end{array} \right)
& \left( \begin{array}{cc}    1.9368 & 0 \\  0   &  1.9368
\end{array} \right)
&   0.0844\\ %13
\left( \begin{array}{c}  -7.8340 \\  6.0516
\end{array} \right)
& \left( \begin{array}{cc}    1.1180 & 0 \\   0 &  1.1180
\end{array} \right)
&   0.0621 %14
\end{aligned}
\end{math}

\newpage
For \textbf{data2train.dat}:

\begin{figure}[h!]
\centering
\includegraphics[scale=0.6]{fig/HW6P1_21.png}
\caption{log-likelihood as a function of the number of components of amixture of Gaussians for \textbf{data2train.dat}}
%\label{fig:HW6P1_1}
\end{figure}

fixed number of Gaussians that works well for \textbf{data1train.dat} is \textbf{10}.
\begin{figure}[h!]
\centering
\includegraphics[scale=0.6]{fig/HW6P1_22.png}
\caption{Optimal Gaussians mixtures that work for \textbf{data2train.dat}}
%\label{fig:HW6P1_1}
\end{figure}

\newpage
List the mean, covariance and mixing weights for each mixture component.
\begin{math}
\begin{aligned}
\mbox{best mean} & \mbox{best covariance} & \mbox{best mixing weight} \\
\left( \begin{array}{c}  -4.5860 \\ -1.6871
\end{array} \right)
& \left( \begin{array}{cc}  0.5758  &  0 \\ 0    & 0.5758
\end{array} \right)
&  0.0793   \\ %1
\left( \begin{array}{c} 2.1473  \\  -14.3782
\end{array} \right)
& \left( \begin{array}{cc}  1.3352  & 0 \\ 0   & 1.3352
\end{array} \right)
&  0.1471  \\ %2
\left( \begin{array}{c}   -4.8326  \\  0.7035
\end{array} \right)
& \left( \begin{array}{cc}   0.9502 & 0  \\ 0   &  0.9502
\end{array} \right)
&  0.1073 \\ %3
\left( \begin{array}{c}  -3.9271  \\  3.2646
\end{array} \right)
& \left( \begin{array}{cc}   0.4316 & 0 \\  0  & 0.4316
\end{array} \right)
&  0.0671   \\ %4
\left( \begin{array}{c} -1.6167   \\  4.6312
\end{array} \right)
& \left( \begin{array}{cc}   0.7350   & 0  \\  0  &  0.7350
\end{array} \right)
&  0.1017  \\ %5
\left( \begin{array}{c}  -3.3163 \\  -3.6515
\end{array} \right)
& \left( \begin{array}{cc}  0.6169    & 0 \\ 0   &  0.6169
\end{array} \right)
&   0.0778 \\ %6
\left( \begin{array}{c} 2.1058  \\  -5.6436
\end{array} \right)
& \left( \begin{array}{cc}  1.0695   & 0 \\  0  &  1.0695
\end{array} \right)
&  0.0964 \\ %7
\left( \begin{array}{c}   4.6136 \\  -11.3956
\end{array} \right)
& \left( \begin{array}{cc}   1.4347 & 0 \\  0  & 1.4347
\end{array} \right)
& 0.1386 \\ %8
\left( \begin{array}{c} 4.5414 \\ -7.9118
\end{array} \right)
& \left( \begin{array}{cc}   0.9857   & 0 \\  0  & 0.9857
\end{array} \right)
&  0.0924 \\ %9
\left( \begin{array}{c}  -0.7092 \\  -4.9507
\end{array} \right)
& \left( \begin{array}{cc}   0.9208   & 0  \\  0  & 0.9208
\end{array} \right)
&  0.0924  %10
\end{aligned}
\end{math}

\newpage
\section{Resampling}
Randomly generate 100 particles $x^{i}$ form some distribution $\pi$ of your choice, and 100 (positive) weights $w^{i}$. Normalize the weights such that $\sum_{i} w^{i} =1$, and the weighted samples ${x^{i},w^{i}}$ to estimate the mean $m$ of $\pi$, and denote this estimate by $\hat{m}$.

Resample the particles $x^i$ (from the weights $w^i$) using multinominal resampling, and estimate the mean from the resampled (now equally weighted) samples. Denote this estimate $\hat{m}_m$.

Repeat this for systematic resampling, and denote this estimate $\hat{m}_s$.

Repeat this for stratified resampling, and denote this estimate $\hat{m}_t$.

Repeat the items above multiple times, and report an estimate of the variance for $\hat{m}-\hat{m}_m$, $\hat{m}-\hat{m}_s$, and $\hat{m}-\hat{m}_t$ respectively, conditionally on $\hat{m}$ (that is, do not sample new particles from $\pi$, but only repeat the resampling step). Which resampling scheme appears to be the preferred one, in terms of variance?

\textbf{Solution}:

I randomly generate 100 particles $x^{i}$ using Gaussian distribution $G(0,1)$ and calculate weights $w^{i}$, normalize the weights by
\begin{equation}
    w^{i} = \frac{w^{i}}{sum(w^{i})}
\end{equation}

Then I use the same samples and normalized weights to resample multiple times.
The algorithms of multinomial resampling, systematic resampling and stratified resampling are in the code (see the attached code)

I repeat resample 1000 times to calculate the variance of $\hat{m}-\hat{m}_m$, $\hat{m}-\hat{m}_s$, and $\hat{m}-\hat{m}_t$.

\begin{figure}[h!]
\centering
\includegraphics[scale=0.4]{fig/P5_1.png}
\caption{$\hat{m}-\hat{m}_m$, $\hat{m}-\hat{m}_s$, and $\hat{m}-\hat{m}_t$ in 1000 iterations}
%\label{fig:HW6P1_1}
\end{figure}
In this Figure 5, we find that the variance of $\hat{m}-\hat{m}_m$ is the largest. The variance of $\hat{m}-\hat{m}_s$ is smallest.

Analytically, in our simulation, 1000 iterations.

Variance of $\hat{m}-\hat{m}_m$ is $0.005132756202771032$

Variance of $\hat{m}-\hat{m}_s$ is $0.0014684267477009338$

Variance of $\hat{m}-\hat{m}_t$ is $0.002763182508388841$

In terms of variance, systematic resampling is the preferred one.

\newpage
\section{EM algorithm}
Consider data,
\begin{equation}
    D = \left\{\left( \begin{array}{c}  1 \\  3
    \end{array} \right)
    \left( \begin{array}{c}  4 \\  5
    \end{array} \right)
    \left( \begin{array}{c}  2 \\  \star
    \end{array} \right)\right\}
\end{equation}
sampled from a two-dimensional (separable) distribution $p(x_1,x_2) = p_{x_{1}}(x_{1})p_{x_{2}}(x_{2})$ where:

\begin{equation}
p_{x_1}(x_1) = \left\{\begin{array}{rcl}
\frac{1}{\theta_1} \exp(-\frac{x_1}{\theta_1}) & \mbox{if} & x_1 \geq 0,\\
0  & \mbox{otherwise} ,
\end{array}\right.
\end{equation}

and

\begin{equation}
p_{x_2}(x_2) = \left\{\begin{array}{rcl}
\frac{1}{\theta_2}  & \mbox{if} & 0 \leq x_2 \leq \theta_2,\\
0  & \mbox{otherwise} ,
\end{array}\right.
\end{equation}

and $*$ in the dataset indicates a missing value.
\newline
(a) What can you infer from $\theta_2$ by looking at $D$?
\newline
\textbf{Solution}:

The  existing two pairs ($x_1$,$x_2$)  $\left( \begin{array}{c}  1 \\  3
    \end{array} \right)
    \left( \begin{array}{c}  4 \\  5
    \end{array} \right)$ mean that

    for $p_{x_2}(x_2)$,$p_{3}(3) \neq 0$ and $p_{5}(5) \neq 0$.

    $0 \leq 3 \leq 5 \leq \theta_2$

Therefore, $\theta_2$  should be greater or equal to 5.
\newline
(b) Start with an initial estimate $\theta^{0}= \left( \begin{array}{c}  3 \\  6
    \end{array} \right)$ and analytically calculate $Q(\theta | \theta^{0})$.
This is the expected joint data log-likelihood considered in class. For this problem to compute it, you effectively have to marginalize out the missing values. This is the estimate expectation step in the EM algorithm.
\newline
\textbf{Solution}:

\begin{math}
    D = \left\{\left( \begin{array}{c}  x_{11} = 1 \\  x_{12} = 13
    \end{array} \right)
    \left( \begin{array}{c}  x_{21} =  4 \\ x_{22} = 5
    \end{array} \right)
    \left( \begin{array}{c} x_{31} = 2 \\  x_{32} = \star
    \end{array} \right)\right\}
\end{math}

$\theta^{0}= \left( \begin{array}{c}  \theta_1^{0} = 3 \\ \theta_2^{0} =  6
    \end{array} \right) = (\theta_1^{0},\theta_2^{0})$

$\theta= \left( \begin{array}{c}  \theta_1  \\ \theta_2
    \end{array} \right) = (\theta_1,\theta_2)$

\begin{equation}
\begin{aligned}
    p(x_1,x_2,\theta) & = p_{x_1}(x_1 | \theta) p_{x_2}(x_2 | \theta) \\
                      & = \left\{\begin{array}{rcl}
\frac{1}{\theta_1} \exp(-\frac{x_1}{\theta_1}) \frac{1}{\theta_2}  & \mbox{if} & x_1 \geq 0  \&  0 \leq x_2 \leq \theta_2,\\
0  & \mbox{otherwise} ,
\end{array}\right.
\end{aligned}
\end{equation}

Then we need to use the expected joint data log-likelihood $Q(\theta | \theta^{0})$.

\begin{equation}
\begin{aligned}
& Q(\theta | \theta^{0})  = E_{x_{32}}[\ln{p(x_v,x_h,\theta)} | \mathcal{D},\theta^{0}] \\
& = \int_{-\infty}^{\infty} [\ln{p(x_{11},x_{12} | \theta)} + \ln{p(x_{21},x_{22} | \theta)}+ \ln{p(x_{31},x_{32} | \theta)}] p(x_{32} |x_{31},\theta^{0}) dx_{32} \\
& = \ln{p(x_{11},x_{12} | \theta)} + \ln{p(x_{21},x_{22} | \theta)} + \int_{-\infty}^{\infty} [ \ln{p(x_{31},x_{32} | \theta)}] \frac{p(x_{31},x_{32}|\theta^{0})}{\int_{-\infty}^{\infty} p(x_{31}, x_{32}^{'} |\theta^{0}) dx_{32}^{'} } dx_{32}
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
\ln{p(x_{11},x_{12} | \theta)} + \ln{p(x_{21},x_{22} | \theta)} & = \ln(\frac{1}{\theta_1 \theta_2}e^{-\frac{x_{11}}{\theta_1}}) +\ln(\frac{1}{\theta_1 \theta_2}e^{-\frac{x_{21}}{\theta_1}})  \\
& =\ln(\frac{1}{\theta_1 \theta_2}e^{-\frac{1}{\theta_1}}) +\ln(\frac{1}{\theta_1 \theta_2}e^{-\frac{4}{\theta_1}}) \\
& = -2 \ln{\theta_1 \theta_2} -\frac{5}{\theta_1}
\end{aligned}
\end{equation}

\begin{equation}
p(x_{31}=2,x_{32} | \theta^{0}) = p_{x_{31}}(x_{31}=2|\theta^{0}) p_{x_{32}}(x_{32}|\theta^{0})
\end{equation}

\begin{equation}
\int_{-\infty}^{\infty} p(x_{31}=2, x_{32}^{'} |\theta^{0}) dx_{32}^{'} = p_{x_{31}}(x_{31}=2|\theta^{0})
\end{equation}

\begin{equation}
\begin{aligned}
 \int_{-\infty}^{\infty} [ \ln{p(x_{31},x_{32} | \theta)}] \frac{p(x_{31},x_{32}|\theta^{0})}{\int_{-\infty}^{\infty} p(x_{31}, x_{32}^{'} |\theta^{0}) dx_{32}^{'} } dx_{32} = \int_{-\infty}^{\infty} [ \ln{p(x_{31},x_{32} | \theta)}] p_{x_{32}}(x_{32}|\theta^{0}) dx_{32}
\end{aligned}
\end{equation}

$[ \ln{p(x_{31},x_{32} | \theta)}] p_{x_{32}}(x_{32}|\theta^{0}) \neq 0$ if $ 0 \leq x_{32} \leq \min(\theta_2,  \theta_{2}^{0}=6) = \min(\theta_2,  6) $

\begin{equation}
\begin{aligned}
 \int_{-\infty}^{\infty} [ \ln{p(x_{31},x_{32} | \theta)}] p_{x_{32}}(x_{32}|\theta^{0}) dx_{32} = \int_{0}^{\min(\theta_2,  6)} ( -\ln{\theta_1 \theta_2} -\frac{2}{\theta_1}) (\frac{1}{6}) dx_{32}
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
 \mbox{for }   & 5 \leq \theta_2 <6, \theta_2=\min(\theta_2,  6) \\
& \int_{-\infty}^{\infty} [ \ln{p(x_{31},x_{32} | \theta)}] p_{x_{32}}(x_{32}|\theta^{0}) dx_{32} \\
& = \int_{0}^{\theta_2} ( -\ln{\theta_1 \theta_2} -\frac{2}{\theta_1}) (\frac{1}{6}) dx_{32} \\
& = \frac{\theta_2}{6}(-\ln{\theta_1 \theta_2} -\frac{2}{\theta_1})
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
 \mbox{for }   & 6 \leq \theta_2 , 6=\min(\theta_2,  6) \\
& \int_{-\infty}^{\infty} [ \ln{p(x_{31},x_{32} | \theta)}] p_{x_{32}}(x_{32}|\theta^{0}) dx_{32} \\
& = \int_{0}^{6} ( -\ln{\theta_1 \theta_2} -\frac{2}{\theta_1}) (\frac{1}{6}) dx_{32} \\
& = (-\ln{\theta_1 \theta_2} -\frac{2}{\theta_1})
\end{aligned}
\end{equation}

Therefore, combine (11)(16)(17).
\begin{equation}
\begin{aligned}
\mbox{for }   & 5 \leq \theta_2 <6 \\
& Q(\theta | \theta^{0})  = -2 \ln{\theta_1 \theta_2} -\frac{5}{\theta_1} + \frac{\theta_2}{6}(-\ln{\theta_1 \theta_2} -\frac{2}{\theta_1})    = -(2+\frac{\theta_2}{6})\ln{\theta_1 \theta_2} -(5+\frac{\theta_2}{3}) \frac{1}{\theta_1} \\
 \mbox{for }   & 6 \leq \theta_2  \\
& Q(\theta | \theta^{0})  = -2 \ln{\theta_1 \theta_2} -\frac{5}{\theta_1} +  (-\ln{\theta_1 \theta_2} -\frac{2}{\theta_1})
= -3\ln{\theta_1 \theta_2} -\frac{7}{\theta_1}
\end{aligned}
\end{equation}

(c) Find the $\theta$ that maximizes your  $Q(\theta | \theta^{0})$. This is the maximization step of the EM algorithm.
\newline
\textbf{Solution}:

\textbf{M} step: solving $ \bigtriangledown Q(\theta | \theta^{0})= 0 $ or get the maximization of
$Q(\theta | \theta^{0})$

We have the boundary conditions for $\theta_2$, so we start from $\theta_2$.

\begin{equation}
    \frac{\partial Q(\theta | \theta^{0})}{ \partial \theta_2 }
\end{equation}

\begin{equation}
\begin{aligned}
\mbox{for }   & 5 \leq \theta_2 <6 \\
& \frac{\partial Q(\theta | \theta^{0})}{ \partial \theta_2 } = -\frac{\ln \theta_1}{6}-\frac{1}{3\theta_1} -\frac{2}{\theta_2}- \frac{\ln \theta_2}{6}  -\frac{1}{6} \\
& = F(\theta_1)+G(\theta_2)-\frac{1}{6}
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
F(\theta_1) = -\frac{\ln \theta_1}{6}-\frac{1}{3\theta_1}
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
\frac{\partial F(\theta_1)}{ \partial \theta_1} = \frac{1}{6} (\frac{1}{\theta_1}-\frac{2}{(\theta_{1})^{2}}) = 0  \rightarrow \theta_1 = 2
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
F(\theta_1) \leq F(2) = -\frac{1}{6}(\ln2 +1) <0
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
G(\theta_2) = -\frac{2}{\theta_2}- \frac{\ln \theta_2}{6} < 0 & \mbox{for } & 5 \leq \theta_2 <6
\end{aligned}
\end{equation}

Therefore,

\begin{equation}
\begin{aligned}
\mbox{for }   & 5 \leq \theta_2 <6 \\
& \frac{\partial Q(\theta | \theta^{0})}{ \partial \theta_2 } < 0
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
\mbox{for }   & 6 \leq \theta_2  \\
& \frac{\partial Q(\theta | \theta^{0})}{ \partial \theta_2 } = -\frac{3}{\theta_2} < 0
\end{aligned}
\end{equation}

And $Q(\theta | \theta^{0})$ is continuous at $\theta=6$. Therefore, for $5 \leq \theta_2 <6$ and $6 \leq \theta_2 $, $Q(\theta | \theta^{0})$ is monotonically continuous decreasing.

When $\theta_2 = 5$, $Q(\theta | \theta^{0})$ gets the largest value $Q(\theta_1,\theta_2 = 5 | \theta^{0})$ . Then we fix $\theta_2 = 5$ and solve $\theta_1$.

\begin{equation}
\begin{aligned}
Q(\theta_1,\theta_2 = 5 | \theta^{0}) = -(\frac{17}{6})\ln{5\theta_1} -(\frac{20}{3}) \frac{1}{\theta_1}
\end{aligned}
\end{equation}

$\theta_1 > 0$

\begin{equation}
\begin{aligned}
& \frac{\partial Q(\theta_1,\theta_2 = 5 | \theta^{0})}{ \partial \theta_1 } = -(\frac{17}{6})\frac{1}{\theta_1} + (\frac{20}{3}) \frac{1}{(\theta_1)^2} = 0 \rightarrow \theta_1 = \frac{40}{17}
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
& \frac{\partial Q(\theta_1,\theta_2 = 5 | \theta^{0})}{ \partial \theta_1 } >0 & \mbox{if } & 0<\theta_1 < \frac{40}{17} \\
& \frac{\partial Q(\theta_1,\theta_2 = 5 | \theta^{0})}{ \partial \theta_1 } <0 & \mbox{if } & \frac{40}{17} < \theta_1
\end{aligned}
\end{equation}

$Q(\theta_1,\theta_2 = 5 | \theta^{0})$ gets the largest value when $\theta_1 = \frac{40}{17}$.

In conclusion, the $\theta= \left( \begin{array}{c}  \theta_1 \\  \theta_2
    \end{array} \right) =\left( \begin{array}{c}  \frac{40}{17} \\  5
    \end{array} \right) $ maximizes $Q(\theta | \theta^{0})$.

%\bibliographystyle{plain}
%\bibliography{references}
\end{document}


\subsection{Dimensionality Reduction}
\label{sec:dimreduce}

Using high dimensional vectors is problematic with many learning
algorithms because of the computational cost and the curse of
dimensionality.  In this section we investigate if there is a low
dimensional representation of the substitute vectors which still
preserve the neighborhood information necessary to learn syntactic
categories.  We first briefly describe then report experimental
results on principal components analysis (PCA), Isomap
\cite{tenenbaum2000global}, locally linear embedding (LLE)
\cite{roweis2000nonlinear}, and Laplacian eigenmaps
\cite{belkin2003laplacian}.

Each dimensionality reduction algorithm tries to preserve certain
aspects of the original vectors.  PCA is a linear method that
minimizes reconstruction error.  Isomap tries to preserve distances as
measured along a low dimensional submanifold assuming the input
vectors were sampled from the neighborhood of such a manifold.  LLE
most faithfully preserves the local linear structure of nearby input
vectors.  Laplacian eigenmaps most faithfully preserve proximity
relations, mapping nearby inputs to nearby outputs.

% we don't use KL2 anymore, we will probably use Jensen
% we may use fewer than 100 nearest neighbors, keeping it 100 for now
% we want to split 1m data into 10 pieces, but that may prove
%difficult to do so
We wanted to see how accuracy (based on the
k-nearest-neighbor supervised baseline as in the previous section)
changes based on the number of dimensions for each dimensionality
reduction algorithm.
%
%% For computational efficiency, we extracted the first 24K tokens of
%% Wall Street Journal Section of the Penn Treebak \cite{treebank3}.  We
%% applied each algorithm to each chunk and obtained average accuracy and
%% standard deviation for a given number of dimensions.
For algorithms that require a distance matrix rather than raw input
vectors we used the Jensen-Shannon divergence judged best by the
experiments of the previous section.  For graph based methods we built
neighborhood graphs using 100 nearest neighbors.  The low dimensional
output vectors were compared using the cosine distance metric for the
supervised k-nearest-neighbor algorithm.  Figure~\ref{fig:dimreduce}
plots supervised baseline accuracy vs. number of dimensions for each
algorithm.

% this graph needs to updated
% graph of dims vs accuracy
\begin{figure}[h] \centering
\includegraphics[width=0.6\textwidth]{baseline_graph_mono.png}
\caption{Supervised knn baselines for the four dimensionality
  reduction algorithms.}
\label{fig:dimreduce}
\end{figure}

% /scratch/esert/pos_ind/work/BASELINE_GRAPH/plot_data
% dimension PCA    LLE     ISO     LEM(Spectral)
% 2              0.2831 0.2272 0.3433 0.3480
% 4              0.4300 0.4547 0.5596 0.5019
% 8              0.4920 0.5968 0.6480 0.6234
% 16            0.5303 0.6500 0.6678 0.6572
% 32            0.5676 0.6617 0.6790 0.6708
% 64            0.6162 0.6680 0.6818 0.6774
% 128          0.6390 0.6735 0.6844 0.6838
% 256          0.6527 0.6747 0.6860 0.6914
% 512          0.6658 0.6774 0.6876 0.6948
% 1024        0.6720 0.6798 0.6891 0.7022
% 2048        0.6764 0.6811 0.6878 0.7070
%

% this should be about same
The graph based algorithms (Isomap, LLE, and Laplacian eigenmaps) all
outperform PCA.  They stay within 5\% of their peak accuracy with as
few as 16 dimensions.  In fact Laplacian eigenmaps outperform the
baseline with the original 12,672 dimensional vectors (68.95\%) when
allowed to retain more than about 250 dimensions.  Spectral clustering
uses the same transformation as the Laplacian eigenmaps algorithm and
we compare its performance to other clustering algorithms in the next
section.

%%


\documentclass{article}

\usepackage{../preamble}
\usepackage{../macros}

\title{Notes for High-Dimensional Probability:\\ Random Vectors in High Dimensions}

\author{Isak Falk}

\begin{document}

\maketitle

\section{Preliminaries}

\begin{proposition}
  \label{prop:sub-gauss-sub-exp-norm-relation}
  Let \(X\) be a real-values random variable and \(Y = \sqrt{\abs{X}}\). Then
  \(X\) is sub-exponential if and only if \(Y\) is sub-Gaussian, and in such
  case \(\norm{X}_{\psi_{1}} = \norm{Y}_{\psi_{2}}^{2}\)
\end{proposition}

\begin{proposition}
  \label{prop:centering-of-sub-exp-rvs}
  Let \(X\) be a real-valued sub-exponential variable, then the centered random
  variable \(X - \E X\) is sub-exponential and
  \begin{equation}
    \norm{X - \E X}_{\psi_{1}} \leq (1 + \frac{2}{\ln 2}) \norm{X}_{\psi_{2}}.
  \end{equation}
\end{proposition}

\begin{proof}
  The proof is analogous to the sub-Gaussian case but with different constant
  since the definition of the norm is different,
  \begin{equation}
    \norm{X - \E X}_{\psi_{1}} \leq \norm{X}_{\psi_{1}} + \norm{\E X}_{\psi_{1}},
  \end{equation}
  and
  \begin{equation}
    \norm{\E X}_{\psi_{1}} = \frac{\abs{\E X}}{\ln 2} \leq \frac{\E \abs{X}}{\ln 2} = \frac{\norm{X}_{1}}{\ln 2} \leq \frac{2}{\ln 2}\norm{X}_{\psi_{1}},
  \end{equation}
  where we have used the definition of sub-exponential norm for constant
  functions, Jensen and bound of \(L^{p}\) norm of \(X\) by sub-exponential
  norm. We thus have that
  \(\norm{X - \E X}_{\psi_{1}} \leq (1 + \frac{2}{\ln 2})\norm{X}_{\psi_{1}}\)
  which is what we wanted to show.

\end{proof}

\begin{theorem}[Bernstein's theorem]
  \label{thm:bernsteins-ineq}
  Let \((X_{i})_{i=1}^{n}\) be a sequence of independent real-valued zero-mean
  random variables such that \(\norm{X_{i}}_{\psi_{1}} < \infty\). Then, for
  every \(t > 0\)
  \begin{equation}
    \Pr(\abs*{\frac{1}{n}\sum_{i=1}^{n}X_{i}} > t) \leq 2\exp(-cn\min(\frac{t^{2}}{K^{2}}, \frac{t}{K})),
  \end{equation}
  where \(K = \max_{i}\norm{X_{i}}_{\psi_{1}}\) and \(c > 0\) is some absolute constant.
\end{theorem}

\section{Concentration of the Norm}
\begin{theorem}[Concentration of the \(L_{2}\) norm]
Let \(X = (X_{1}, \dots, X_{n}) \in \R^{n}\) be a random vector with independent
sub-gaussian coordinates \(X_{i}\) that satisfy \(\E X_{i}^{2} = 1\). Then
\begin{equation}
  \norm{\norm{X}_{2} - \sqrt{n}}_{\psi_{2}} \leq CK^{2},
\end{equation}
where \(K = \max_{i} \norm{X_{i}}_{\psi_{2}}\) and \(C\) is an absolute constant.
\end{theorem}

\begin{proof}
  We first note that \(K \geq 1\). By Jensen's Inequality we have that
  \(\E \exp(\frac{X_{i}^{2}}{t^{2}}) \geq \exp(\frac{\E X_{i}^{2}}{t^{2}}) = \exp(t^{-2})\)
  and using \(t = 1\) we see that \(\E \exp(X_{i}^{2}) \geq e > 2\) so
  \(\norm{X_{i}}_{\psi_{2}} \geq 1\) for all \(i \in \{1, \dots, n\}\). Since
  \(K = \max_{i}\norm{X_{i}}_{\psi_{2}} \geq 1\) we are done.

  Now consider the quantity \(\frac{1}{n}\norm{X}_{2}^{2} - 1\) which we can write as
  \begin{equation}
    \frac{1}{n}\norm{X}_{2}^{2} - 1 = \frac{1}{n}\sum_{i=1}^{n}(X_{i}^{2} - 1) = \frac{1}{n}\sum_{i=1}^{n}Y_{i},
  \end{equation}
  where \(Y_{i} = X_{i}^{2} - 1\). Since \(\E X_{i}^{2} = 1\) for any \(i\),
  \((Y_{i})_{i=1}^{n}\) is a vector of zero-centred random variables. Since
  \(\norm{X_{i}}_{\psi_{2}} < \infty\) we can show that
  \(\norm{Y_{i}}_{\psi_{1}} < \infty\) since
  \begin{align}
    \label{al:1}
    \norm{X_i^2 - 1}_{\psi_1} & \leq C\norm{X_i^2}_{\psi_1} \\
                              & = C\norm{X_i}_{\psi_2}^2 \\
                              & \leq C K^2,
  \end{align}
  using centring property of sub-exponentials
  \cref{prop:centering-of-sub-exp-rvs} (noting that \(C > 1\)),
  \cref{prop:sub-gauss-sub-exp-norm-relation} and definition of \(K\). Since
  \((Y_{i})_{i=1}^{n}\) are independent, zero-mean and by the above calculation sub-exponential, we can apply Bernstein's Inequality
  \cref{thm:bernsteins-ineq} which for any \(u \geq 0\) means that
  \begin{equation}
    \Pr(\abs*{\frac{1}{n}\sum_{i=1}^{n}Y_{i}} > u) \leq 2 \exp(-cn\min(\frac{u^{2}}{C^{2}K^{4}}, \frac{u}{CK^{2}}))
  \end{equation}
  for some \(c > 0\). Since \(C, K > 1\) we have that \(C^{2} > C\) and
  \(K^{4} > K^{2}\), so we can write
  \begin{equation}
    \label{eq:conc-norm-bernstein}
    \exp(-cn\min(\frac{u^{2}}{C^{2}K^{4}}, \frac{u}{CK^{2}})) \leq \exp(-\frac{cn}{C^{4}K^{4}}\min(u^{2}, u)).
  \end{equation}

  So far we have proved a concentration bound on \(\norm{X}_{2}^{2}\). We will
  finish the proof by relating \(\norm{X}_{2}\) to \(\norm{X}_{2}^{2}\). Note
  the following; for any \(z, \delta \geq 0\)
  \begin{equation}
    \label{eq:zp1-delta-observation}
    \abs{z - 1} \geq \delta \Rightarrow \abs{z^{2} - 1} \geq \max(\delta, \delta^{2}).
  \end{equation}
  We show it by first noting that
  \(\abs{z^{2} - 1} = \abs{z - 1}\abs{z + 1} \geq \abs{z + 1}\delta\), and see
  that if \(\delta \in [0, 1)\) then \(\abs{z + 1} \geq \delta\) and if
  \(\delta \geq 1\) then since \(\abs{z + 1} \geq 1\) we have that
  \(\abs{z^{2} + 1} \geq \delta\). We can write this compactly as
  \cref{eq:zp1-delta-observation}.

  Now, consider any \(\delta \geq 0\) and the expression
  \(\abs{\frac{1}{\sqrt{n}}\norm{X}_{2} - 1} \geq \delta\). Using
  \cref{eq:zp1-delta-observation} with \(z = \frac{1}{\sqrt{n}}\norm{X}_{2}\) we
  see that
  \begin{equation}
    \label{eq:4}
    \abs{\frac{1}{\sqrt{n}}\norm{X}_{2} - 1} \geq \delta \Rightarrow \abs{\frac{1}{n}\norm{X}^{2}_{2} - 1} \geq \max(\delta, \delta^{2}).
  \end{equation}
  In terms of events, this means that
  \begin{equation}
    \Pr(\abs*{\frac{1}{\sqrt{n}}\norm{X}_{2} - 1} \geq \delta) \leq \Pr(\abs*{\frac{1}{n}\norm{X}^{2}_{2} - 1} \geq \max(\delta, \delta^{2})) \leq 2 \exp(-\frac{cn}{C^{4}K^{4}}\delta^{2}),
  \end{equation}
  where in the final inequality we have used \cref{eq:conc-norm-bernstein}
  together with
  \begin{equation}
    \delta^{2} = \min(\max(\delta, \delta^{2}), \max(\delta, \delta^{2})^{2}).
  \end{equation}
  Letting \(t = \delta \sqrt{n}\) we obtain the bound
  \begin{equation}
    \Pr(\abs{\norm{X}_{2} - \sqrt{n}} \geq t) \leq 2\exp(-\frac{ct^{2}}{C^{4}K^{4}}),
  \end{equation}
  for any \(t \geq 0\) and this is equal to the conclusion of the theorem.
\end{proof}

\begin{remark}
  The above bound tells us that with high probability, \(X\) takes values very
  close to the sphere of radius \(\sqrt{n}\). In particular, for a fixed probability \(X\) stays
  within a constant distance from that sphere independently of the dimension
  \(n\). This is due to the fact that \(\norm{X}_{2}^{2}\) has mean \(n\) and
  standard deviation \(O(\sqrt{n})\) since
  \begin{equation}
    \V(\norm{X}_{2}^{2}) = \sum_{i=1}\V(X_{i}^{2}) = n \V(X_{1}^{2}),
  \end{equation}
  due to independence of the coordinates of \(X\), and so
  \(\sqrt{\V(\norm{X}_{2}^{2})} = \sqrt{n} \cdot \mathrm{std}(X_{1}^{2})\).
  \(\sqrt{n \pm O(\sqrt{n})} = \sqrt{n} \pm O(1)\) since by Taylor expansion
  around \(\sqrt{n}\) on the interval \([n - c \sqrt{n}, n + c \sqrt{n}]\) where
  \(c\) need to be chosen so that \(n - c \sqrt{n} \geq 0\) we see that
  \begin{align}
    \sqrt{n \pm c\sqrt{n}} = \sqrt{n} + R_{1}(c\sqrt{n})
  \end{align}
  where \(R_{1}(x) = \) [ Need to fill in ].
\end{remark}

\section{Covariance, second moments, whitening and isotropy}
\begin{definition}
  The covariance matrix of a random vector \(X \in \R^{n}\) is
  \begin{equation}
    \Cov(X) = \E(X - \E X)(X - \E X)\tran = \E X X\tran - \E X (\E X)\tran.
  \end{equation}
\end{definition}

\begin{definition}
  The second moment matrix of a random vector \(X \in \R^{n}\) is
  \begin{equation}
  \Sigma = \Sigma(X) = \E X X\tran.
  \end{equation}
\end{definition}

Note that if \(\E X = 0\), then \(\Cov(X) = \Sigma(X)\). So we can mostly
consider the second moment matrix without loss of generality in place of the
covariance as long as we center our variable \(X\). Both of the matrices are
symmetric and positive semi-definite.

\begin{definition}
  A random vector \(X \in \R^{n}\) is called \emph{isotropic} if
  \begin{equation}
    \Sigma(X) = \E X X\tran = I.
  \end{equation}
\end{definition}

For a random variable \(X \in \R^{n}\) with covariance matrix \(\Sigma\) and
mean \(\mu\) we can always put it into a isotropic form by whitening
\(Z = \Sigma^{-1/2}(X - \mu)\). Similarly, the transformation using a psd matrix
\(\Sigma\),
\(X = \mu + \Sigma^{1/2}Z\) of a mean-zero isotropic random vector
\(Z \in \R^{n}\) leads to a random vector with mean \(\mu\) and covariance
\(\Sigma\). This observation means that in many cases we may focus on random
isotropic mean-zero random vectors without loss of generality.

\begin{lemma}
  \label{lem:isotropy-marginal-characterisation}
  A random vector \(X \in \R^{n}\) is isotropic if and only if
  \begin{equation}
    \E \scal{X}{u} = \norm{u}^{2}
  \end{equation}
  for all \(u \in \R^{n}\). Equivalently we could have used
  \begin{equation}
    \E \scal{X}{u} = 1
  \end{equation}
  for all \(u \in S^{n-1}\), the \(n\)-dimensional unit sphere.
\end{lemma}

\begin{proof}
  Two symmetric matrices \(A, B\) are equal if and only if
  \(x\tran A x = x\tran B\) for any \(x \in \R^{n}\). Thus \(X\) is isotropic if
  and only if
  \begin{equation}
    u\tran \E X X\tran u = \E \scal{X}{u} = u\tran I u = \norm{u}_{2}^{2}
  \end{equation}
  for all \(u \in \R^{n}\) which is what we wanted to show.
\end{proof}

\begin{lemma}
  Let \(X\) be an isotropic random vector in \(\R^{n}\). The
  \begin{equation}
    \E \norm{X}^{2}_{2} = n.
  \end{equation}
  Moreover, if \(X, Y\) are two independent isotropic random vectors in
  \(\R^{n}\), then
  \begin{equation}
    \E\scal{X}{Y}^{2} = n
  \end{equation}
\end{lemma}

\begin{proof}
  First we have
  \begin{align}
    \E\norm{X}_{2}^{2} & = \E X\tran X \\
                       & = \E \Tr(X X\tran) \\
                       & = \Tr(\E X X\tran) \\
                       & = \Tr(I) \\
                       & = n.
  \end{align}
  For the second part we use the so called \emph{law of total expectation} to
  write
  \begin{equation}
    \E \scal{X}{Y} = \E_{X} \E_{Y}(\scal{x}{Y} | X = x) = \E_{X} \norm{X}_{2}^{2} = n
  \end{equation}
  by the \cref{lem:isotropy-marginal-characterisation} and reusing the first
  part of the proof.
\end{proof}

\begin{corollary}
  Let \(X, Y\) be independent mean-zero isotropic random vectors in \(R^{n}\),
  then
  \begin{equation}
    \E \norm{X - Y}_{2}^{2} = 2n
  \end{equation}
\end{corollary}

\begin{proof}
  \begin{align}
    \E \norm{X - Y}_{2}^{2} & = \E \norm{X}_{2}^{2} + \E \norm{Y}_{2}^{2} - 2 \E \scal{X}{Y} \\
                            & = 2n - 0 \\
                            & = 2n
  \end{align}
\end{proof}

\section{Examples of high-dimensional distributions}
Let \(X \sim \Unif(\sqrt{n} S^{n-1})\) mean that the law of \(X\) is the uniform
measure on the zero-centered sphere of radius \(\sqrt{n}\) in \(n\) dimensions.

\begin{theorem}[Isotropy of uniform random variable on \(\sqrt{n} S^{n-1}\)]

\end{theorem}

\end{document}


Before we can address the identification problem in the context of
  the identification workflow, we must define how images are grouped
  into occurrences.
In this section we propose a clustering algorithm to accomplish
  this task.

\paragraph{Occurrence definition}
The Darwin Core defines an \occurrence{} as a collection of evidence
  that shows an organism exists within specific location and span of
  time~\cite{wieczorek_darwin_2012}.
For our purposes this amounts to a cluster of images localized in space
  and time.
We propose that the \occurrence{} grouping algorithm should perform
  agglomerative clustering on the GPS coordinates and time specified in
  the image metadata.

\paragraph{Space-time image distance}
Towards this goal we define a space-time feature $\g_i$ for each image
  $i$, and a pairwise distance, $\Delta(\g_i, \g_j)$, between these
  features.
This feature will a two dimensional feature tuple, %
$\g_i = \paren{\time_i, \gps_i}$, where the first component is the
  POSIX timestamp $\time_i$, and the second component is a GPS coordinate %
$\gps_i = \brak{\lat_i, \lon_i}^{T}$, where the angles of latitude and
  longitude are measured in radians.
To compute this distance between two images $\g_i$ and $\g_j$ we first
  compute the distance in each component of the feature tuple.
The difference in time is the absolute value of the timedelta,  %
$\Delta_t(\g_i, \g_j) = \abs{\time_i - \time_j}$, which is in seconds.

% DISTANCE BETWEEN TWO IMAGES (space and final)
Next, the distance in space is computed by approximating the Earth as a
  sphere.
In general, the distance between two points on a sphere with radius $r$
  is a function of inverse haversines, and is expressed as:
\begin{equation}\label{eqn:geodistance}
    d(\gps_i, \gps_j, r) =
        2 r \asin{\sqrt{
            \haversine{\lat_i - \lat_j} +
            \haversine{\lon_i - \lon_j} +
            \cos\paren{\lat_i} \cos\paren{\lat_j}}}
\end{equation}
In the previous equation, $\haversine{\theta} = \haversineFULL{\theta}$
  is the half vertical sine function.
Thus, we arrive at the spatial distance between two images by
  estimating the radius of the earth to be $r=6367$ kilometers.
\begin{equation}
    \Delta_s(\g_i, \g_j) = d(\gps_i, \gps_j, 6367).
\end{equation}
This results in distance in seconds and a distance in kilometers, which
  are in incompatible units.
To combine these distances we convert kilometers to seconds by
  heuristically estimating the walking speed, $S$, of an animal (for
  zebras we use $S=2\sciE{-3}$ kilometers per second).
This allows us to cancel kilometers from the expression and express GPS
  distance as a unit of time:
$\frac{\Delta_s(\g_i, \g_j)}{S}$.
This distance can be interpreted as the total amount of time it would
  take an animal to move between two points.
The total distance between two images is the sum of these components.
\begin{equation}\label{eqn:imgdist}
    \Delta(\g_i, \g_j) =
        \Delta_t(\g_i, \g_j) + \frac{\Delta_s(\gps_i, \gps_j)}{S}
\end{equation}
Notice that if there is no difference in GPS location, then this
  measure becomes to a distance in time.

\paragraph{Clustering procedure}
Having defined pairwise a distance between two images, we proceed to
  describe the agglomerative clustering algorithm.
There are two inputs to the agglomerative clustering algorithm:
(1) The matrix of pairwise distance between images, and
(2) the minimum distance threshold between two images.
The matrix of distances is computed using~\cref{eqn:imgdist}, and we
  set the distance threshold to $600$ seconds.
Any pair of images that is within this threshold connected via a
  linkage matrix.
Connected components in this matrix form the final clusters that we use
  as \occurrences{}.

\paragraph{Discussion of occurrences}
These computed \occurrences{} are valuable measurements for multiple
  components of the IBEIS software.
At its core an \occurrence{} describes \wquest{when} a group of animals
  was seen and \wquest{where} that group was seen.
However, to answer the questions like \wquest{how many} animals there
  were, \wquest{who} an animal is, \wquest{who else} is an animal with,
  and \wquest{where else} have these animals been seen, the annotations
  in the \occurrence{} must be grouped into individual \encounters{} and
  then matched against the \masterdatabase{}.
The next section describes the first of these procedures:
the \intraoccurrence{} identification algorithm that produces
  \encounters{}.


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\subsection*{gTrig.m}

\begin{par}
\textbf{Summary:} Compute moments of the saturating function $e*sin(x(i))$ and \$ e*cos(x(i))\$, where $x \sim\mathcal N(m,v)$ and $i$ is a (possibly empty) set of $I$ indices. The optional  scaling factor $e$ is a vector of length $I$. Optionally, compute derivatives of the moments.
\end{par} \vspace{1em}

\begin{verbatim}  [M, V, C, dMdm, dVdm, dCdm, dMdv, dVdv, dCdv] = gTrig(m, v, i, e)\end{verbatim}
    \begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}
\begin{verbatim}m     mean vector of Gaussian                                    [ d       ]
v     covariance matrix                                          [ d  x  d ]
i     vector of indices of elements to augment                   [ I  x  1 ]
e     (optional) scale vector; default: 1                        [ I  x  1 ]\end{verbatim}
\begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}M     output means                                              [ 2I       ]
V     output covariance matrix                                  [ 2I x  2I ]
C     inv(v) times input-output covariance                      [ d  x  2I ]
dMdm  derivatives of M w.r.t m                                  [ 2I x   d ]
dVdm  derivatives of V w.r.t m                                  [4II x   d ]
dCdm  derivatives of C w.r.t m                                  [2dI x   d ]
dMdv  derivatives of M w.r.t v                                  [ 2I x d^2 ]
dVdv  derivatives of V w.r.t v                                  [4II x d^2 ]
dCdv  derivatives of C w.r.t v                                  [2dI x d^2 ]\end{verbatim}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modified: 2013-03-25
\end{par} \vspace{1em}

\begin{lstlisting}
function [M, V, C, dMdm, dVdm, dCdm, dMdv, dVdv, dCdv] = gTrig(m, v, i, e)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
d = length(m); I = length(i); Ic = 2*(1:I); Is = Ic-1;
if nargin == 3, e = ones(I,1); else e = e(:); end; ee = reshape([e e]',2*I,1);
mi(1:I,1) = m(i); vi = v(i,i); vii(1:I,1) = diag(vi);     % short-hand notation

M(Is,1) = e.*exp(-vii/2).*sin(mi); M(Ic,1) = e.*exp(-vii/2).*cos(mi);    % mean

lq = -bsxfun(@plus,vii,vii')/2; q = exp(lq);
U1 = (exp(lq+vi)-q).*sin(bsxfun(@minus,mi,mi'));
U2 = (exp(lq-vi)-q).*sin(bsxfun(@plus,mi,mi'));
U3 = (exp(lq+vi)-q).*cos(bsxfun(@minus,mi,mi'));
U4 = (exp(lq-vi)-q).*cos(bsxfun(@plus,mi,mi'));
V(Is,Is) = U3 - U4; V(Ic,Ic) = U3 + U4; V(Is,Ic) = U1 + U2;
V(Ic,Is) = V(Is,Ic)'; V = ee*ee'.*V/2;                               % variance

C = zeros(d,2*I); C(i,Is) = diag(M(Ic)); C(i,Ic) = diag(-M(Is)); % inv(v) * cov

if nargout > 3                                           % compute derivatives?
  dVdm = zeros(2*I,2*I,d); dCdm = zeros(d,2*I,d); dVdv = zeros(2*I,2*I,d,d);
  dCdv = zeros(d,2*I,d,d); dMdm = C';
  for j = 1:I
    u = zeros(I,1); u(j) = 1/2;
    dVdm(Is,Is,i(j)) = e*e'.*(-U1.*bsxfun(@minus,u,u')+U2.*bsxfun(@plus,u,u'));
    dVdm(Ic,Ic,i(j)) = e*e'.*(-U1.*bsxfun(@minus,u,u')-U2.*bsxfun(@plus,u,u'));
    dVdm(Is,Ic,i(j)) = e*e'.*(U3.*bsxfun(@minus,u,u') +U4.*bsxfun(@plus,u,u'));
    dVdm(Ic,Is,i(j)) = dVdm(Is,Ic,i(j))';
    dVdv(Is(j),Is(j),i(j),i(j)) = exp(-vii(j)) * ...
                               (1+(2*exp(-vii(j))-1)*cos(2*mi(j)))*e(j)*e(j)/2;
    dVdv(Ic(j),Ic(j),i(j),i(j)) = exp(-vii(j)) * ...
                               (1-(2*exp(-vii(j))-1)*cos(2*mi(j)))*e(j)*e(j)/2;
    dVdv(Is(j),Ic(j),i(j),i(j)) = exp(-vii(j)) * ...
                                   (1-2*exp(-vii(j)))*sin(2*mi(j))*e(j)*e(j)/2;
    dVdv(Ic(j),Is(j),i(j),i(j)) = dVdv(Is(j),Ic(j),i(j),i(j));
    for k = [1:j-1 j+1:I]
      dVdv(Is(j),Is(k),i(j),i(k)) = (exp(lq(j,k)+vi(j,k)).*cos(mi(j)-mi(k)) ...
                         + exp(lq(j,k)-vi(j,k)).*cos(mi(j)+mi(k)))*e(j)*e(k)/2;
      dVdv(Is(j),Is(k),i(j),i(j)) = -V(Is(j),Is(k))/2;
      dVdv(Is(j),Is(k),i(k),i(k)) = -V(Is(j),Is(k))/2;
      dVdv(Ic(j),Ic(k),i(j),i(k)) = (exp(lq(j,k)+vi(j,k)).*cos(mi(j)-mi(k)) ...
                         - exp(lq(j,k)-vi(j,k)).*cos(mi(j)+mi(k)))*e(j)*e(k)/2;
      dVdv(Ic(j),Ic(k),i(j),i(j)) = -V(Ic(j),Ic(k))/2;
      dVdv(Ic(j),Ic(k),i(k),i(k)) = -V(Ic(j),Ic(k))/2;
      dVdv(Ic(j),Is(k),i(j),i(k)) = -(exp(lq(j,k)+vi(j,k)).*sin(mi(j)-mi(k)) ...
                         + exp(lq(j,k)-vi(j,k)).*sin(mi(j)+mi(k)))*e(j)*e(k)/2;
      dVdv(Ic(j),Is(k),i(j),i(j)) = -V(Ic(j),Is(k))/2;
      dVdv(Ic(j),Is(k),i(k),i(k)) = -V(Ic(j),Is(k))/2;
      dVdv(Is(j),Ic(k),i(j),i(k)) = (exp(lq(j,k)+vi(j,k)).*sin(mi(j)-mi(k)) ...
                         - exp(lq(j,k)-vi(j,k)).*sin(mi(j)+mi(k)))*e(j)*e(k)/2;
      dVdv(Is(j),Ic(k),i(j),i(j)) = -V(Is(j),Ic(k))/2;
      dVdv(Is(j),Ic(k),i(k),i(k)) = -V(Is(j),Ic(k))/2;
    end
    dCdm(i(j),Is(j),i(j)) = -M(Is(j)); dCdm(i(j),Ic(j),i(j)) = -M(Ic(j));
    dCdv(i(j),Is(j),i(j),i(j)) = -C(i(j),Is(j))/2;
    dCdv(i(j),Ic(j),i(j),i(j)) = -C(i(j),Ic(j))/2;
  end
  dMdv = permute(dCdm,[2 1 3])/2;

  dMdv = reshape(dMdv,[2*I d*d]);
  dVdv = reshape(dVdv,[4*I*I d*d]); dVdm = reshape(dVdm,[4*I*I d]);
  dCdv = reshape(dCdv,[d*2*I d*d]); dCdm = reshape(dCdm,[d*2*I d]);
end
\end{lstlisting}


\subsection{The Statistical Definition of Entropy}
Entropy has a more fundamental definition (though that doesn't make the classical definition any less important) rooted in statistics. To understand it, we're going to start of my talking about macrostates and microstates.
\input{Entropy/microstates}
\input{Entropy/statdefinition}
\input{Entropy/arrowoftime}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fphw Assignment
% LaTeX Template
% Version 1.0 (27/04/2019)
%
% This template originates from:
% https://www.LaTeXTemplates.com
%
% Authors:
% Class by Felipe Portales-Oliva (f.portales.oliva@gmail.com) with template
% content and modifications by Vel (vel@LaTeXTemplates.com)
%
% Template (this file) License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%    PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[
    12pt, % Default font size, values between 10pt-12pt are allowed
    %letterpaper, % Uncomment for US letter paper size
    %spanish, % Uncomment for Spanish
]{fphw}

% Template-specific packages
\usepackage[utf8]{inputenc} % Required for inputting international characters
\usepackage[T1]{fontenc} % Output font encoding for international characters
\usepackage{fontspec,unicode-math} % Required for using utf8 characters in math mode
\usepackage{parskip}  % To add extra space between paragraphs
% \usepackage{mathpazo} % Use the Palatino font
\usepackage{graphicx} % Required for including images
\usepackage{booktabs} % Better horizontal rules in tables
\usepackage{hyperref} % For links (both internal and external)
% \usepackage{listings} % Required for insertion of code
\usepackage{enumerate}% To modify the enumerate environment
\usepackage{cleveref} % Better \ref command -> \cref
\usepackage{import}   % This 4 packages and the command allow importing pdf
\usepackage{xifthen}  % figures generated with inkscape
\usepackage{pdfpages} % Source: https://castel.dev/post/lecture-notes-2/
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{cancel}
\usepackage{transparent}
\newcommand{\incfig}[1]{%
    \def\svgwidth{0.95\columnwidth}
    \small
        \import{./images/}{#1.pdf_tex}
}

\setlength{\parindent}{15pt}
\setlength{\headheight}{22.66pt}

%----------------------------------------------------------------------------------------
%    ASSIGNMENT INFORMATION
%----------------------------------------------------------------------------------------

\title{Task 8 \\ Helix Surface} % Assignment title

\author{Emilio Domínguez Sánchez} % Student name

\date{December 26th, 2020} % Due date

\institute{University of Murcia \\ Faculty of Mathematics} % Institute or school name

\class{Geometría de Superficies} % Course or class name

\professor{Dr. Pascual Lucas Saorin} % Professor or teacher in charge of the assignment

%----------------------------------------------------------------------------------------
%    Definitions
%----------------------------------------------------------------------------------------

\usepackage{physics}
\newcommand{\R}{\mathbb{R}}
\newcommand{\basis}[2]{\qty{#1,\ #2}}
\DeclareMathOperator{\sgn}{sgn}
\DeclareMathOperator{\Id}{Id}
\newcommand{\inner}[2]{\left\langle #1, \; #2 \right\rangle}
\newcommand{\n}{\vectorbold{n}}

\begin{document}

\maketitle % Output the assignment title, created automatically using the information in the custom commands above

%----------------------------------------------------------------------------------------
%    ASSIGNMENT CONTENT
%----------------------------------------------------------------------------------------

\section*{Problem}

\begin{problem}
    Let $β : I \subset \R \to \R^2 \subset \R^3$ be a planar, parametrized by arc, curve
    with curvature $κ_β$ and tangent and normal vectors $T_β$ and $N_β$ respectively.
    Let $\n = (T_β \cross N_β)(s)$ be the unitary vector
    orthogonal to the plane that contains $β$.
    Then, given $φ \in [0, \frac{π}{2}]$, we define
    the \textit{helix surface $S_φ$ built over $β$}
    as the surface parametrized by
    %
    \begin{equation*}
        X(s, t) = β(s) + t(\cos{φ}N_β(s) + \sin{φ}\n).
    \end{equation*}

    \begin{enumerate}
        \item \label{stm:i} Determine the unitary field $N(s, t)$ normal to $S_φ$.

        \item \label{stm:ii} Compute the Gaussian curvature and the mean curvature
        and classify its points.

        \item \label{stm:iii} Answer whether $β$, seen as belonging to $S_φ$,
        is a line of curvature or an asymptotic curve.

        \item \label{stm:iv} What type of surface do we obtain when $φ = \frac{π}{2}$?
        And when $φ = 0$?
    \end{enumerate}
\end{problem}

%----------------------------------------------------------------------------------------

\subsection*{Answer}

    It is unclear by the statement over which domain is $X$ defined.
One option is to assume that $t$ moves over a range which makes $S_φ$ not self intersect.
Another option is to think of $S_φ$ as a surface in a manifold of higher dimension,
where it does not intersect.
That is, $X : \R^2 \to \R^5 = [\R^2 | \R^3]$,
%
\begin{equation*}
    X(s, t) = [(s, t) | β(s) + t(\cos{φ}N_β(s) + \sin{φ}\n)].
\end{equation*}
%
We can proceed without fixing a domain,
merely requiring that it is an open set in $\R^2$.

\subsubsection*{Item \ref{stm:i}}
    To compute a normal unitary field to $S_φ$,
we will normalize the cross product of $X_s$ and $X_t$.
%
\begin{align*}
    X_s(s, t) &= T_β(s) - \cos{φ}tκ_β(s)T_β(s) = (1 - \cos{φ}tκ_β(s))T_β(s) \qq{and} \\
    X_t(s, t) &= \cos{φ}N_β(s) + \sin{φ}\n,
\end{align*}
%
where we have used that $N_β'(s) = -κ_β(s)T_β(s)$.
In addition, we can also see that $\inner{X_s}{X_t} = 0$.
To perform the cross product mentally fast,
note that $T_β$, $N_β$ and $\n$ are a normal basis in $\R^3$ and hence
$T_β \cross N_β = \n$, $N_β \cross \n = T_β$ and $\n \cross T_β = N_β$\footnote{
    Actually, it is a positive oriented basis because $\n \coloneqq T_β \cross N_β$.
    Otherwise we would have the same equalitites but with a minus sign.
}.
Therefore
%
\begin{equation*}
    X_s(s, t) \cross X_t(s, t) =
    (1 - \cos{φ}tκ_β(s))(-\sin{φ}N_β(s) + \cos{φ}\n).
\end{equation*}
%
We can see that the right part, $-\sin{φ}N_β(s) + \cos{φ}\n$, is unitary,
but in order to preserve the sign of $X_s \cross X_t$,
we need to know the sign of $1 - \cos{φ}tκ_β(s)$.

    Now it is clear that we need to study the domain of $X$,
because depending on the sign of $1 - \cos{φ}tκ_β(s)$,
the normal is $-\sin{φ}N_β(s) + \cos{φ}\n$ or it is $\sin{φ}N_β(s) - \cos{φ}\n$.
What is more, when $φ \ne \frac{π}{2}$ and $κ_β(s) \neq 0$,
$X_s\qty(s, \frac{1}{\cos{φ}κ_β(s)}) = 0$.
This means that the value $t = \frac{1}{\cos{φ}κ_β(s)}$ is problematic because
$S_φ$ stops being a surface.
Below and above that value, we have two connected components with opposite normal vectors.
In general,
%
\begin{equation*}
    N_{S_φ} = \mp \sin{φ}N_β \pm \cos{φ}\n,
\end{equation*}
%
where the sign is given by the sign of $1 - \cos{φ}tκ_β(s)$.

\subsubsection*{Item \ref{stm:ii}}

The Gaussian curvature and the mean curvature are defined in terms of the
principal curvatures of the surface,
which are given by the eigen vectors of $-\dd{N_{S_φ}}$.
In this case, it will be easy to compute both from
the expression $N_{S_φ}(s, t)$ we derived.
%
\begin{align*}
        N \circ X\;(s, t) = {}&
            \mp \sin{φ}N_β(s) \pm \cos{φ}\n. \\
    \dd{N}(X(s, t))\qty(X_t(s, t)) = \qquad
        \pdv{N \circ X}{t}\,(s, t) = {}&
            0. \\
    \dd{N}(X(s, t))\qty(X_s(s, t)) = \qquad
        \pdv{N \circ X}{s}\,(s, t) = {}&
            \pm \sin{φ}κ_β(s)T_β(s). \\
\end{align*}
%
From the first equality we obtain that one eigen value is $0$,
and therefore the Gaussian curvature is also $0$,
while the second can be obtained from the last identity, because
$\dd{N}(X(s, t))\qty(X_s)$ is proportional to $X_s$
(both are an elongation of $T_β(s)$).
Divide the second result by the coefficient which acompanies $X_s$,
$1 - t\cos{φ}κ_β$,
to get the second eigen value.
%
\begin{gather*}
    \qty{κ_1(s, t), κ_2(s, t)} =
    \qty{0, \mp \frac{\sin{φ}κ_β(s)}{1 - t\cos{φ}κ_β(s)} }. \\
    K(s, t) = 0 \qquad H(s, t) = \mp \frac{\sin{φ}κ_β(s)}{2(1 - t\cos{φ}κ_β(s))}.
\end{gather*}

\subsubsection*{Item \ref{stm:iii}}

From the previous item we can also deduce that $X_t$ and $X_s$ are
eigen vectors associated with the principal curvatures.
This means that $β(s) = X(s, 0)$ is a line of curvature,
because $β'(s) = X_s(s, 0)$,
but it is not an asymptotic curve unless
%
\begin{equation*}
    -\frac{\sin{φ}κ_β(s)}{2(1 - t\cos{φ}κ_β(s))} = 0.
\end{equation*}
%
That is, when $φ = 0$ or $κ_β(s) = 0$.

\subsubsection*{Item \ref{stm:iv}}

When $φ = \frac{π}{2}$,
%
\begin{equation*}
    X(s, t) = β(s) + t\n,
\end{equation*}
%
an extrusion of $β$ in perpendicular to the plain it is contained in.
For example, if $β$ is a fraction of a circle,
$S_{\frac{π}{2}}$ would be a fraction of a cylinder.
We call the surface obtained with any curve a generalized cilinder.

When $φ = 0$,
%
\begin{equation*}
    X(s, t) = β(s) + tN_β(s),
\end{equation*}
%
an extension of $β$ over the plane it is contained in.
$S_0$ would be contained in a plane.
That would make $S_0$ a fraction of a plane.

%----------------------------------------------------------------------------------------

\end{document}


\section{Overview}

%\subsection{Pipeline Overview}

%\subsection{The Joint Loss Function}

% Our pipeline is as follows (Fig.~\ref{fig:arch_content}):
Our pipeline consists of the following steps (illustrated in Fig.~\ref{fig:arch_content}):

\begin{enumerate}
\item Fit a 3D model to extract static albedo textures from each frame in the source video sequence and the single RGB target image (Section 4).
\item Infer dynamic textures and retarget the per-frame texture expressions from the source video frames onto the target image texture using a generative adversarial framework (Section 5).
\item Composite the target mesh with the generated dynamic textures into each frame in the source video (Section 6).
\end{enumerate}

% \vfill\eject

\section{Fitting the Face Model}

We model the face shape $S$ and albedo as a multilinear PCA model with $n = 53k$ vertices and $106k$ faces:
\begin{equation}
S(\beta_{id}, \beta_{exp}) = \hat{S} + B_{id}\beta_{id} + B_{exp} \beta_{exp}
\end{equation}
\begin{equation}
I(\alpha_{alb}) = \hat{I} + A_{alb} \cdot \alpha_{alb}
\end{equation}
The identity and expression are represented as a multivariate normal distribution with $B_{id} \in \mathbf{R}^{3n \times 80} $, $B_{exp} \in \mathbf{R}^{3n \times 29}$ and $A_{alb} \in \mathbf{R}^{3n \times 80}$.  The dimensions of the mean shape are $\hat{S} = \hat{S}_{id} + \hat{S}_{exp} \in \mathbf{R}^{3n}$, and the mean albedo is given by $\hat{I} \in \mathbf{R}^{3n}$.  The standard deviations are given by: $\sigma_{id} \in \mathbf{R}^{80}$, $\sigma_{exp} \in \mathbf{R}^{29}$ and $\sigma_{alb} \in \mathbf{R}^{80}$.

We use the Basel Face Model ~\cite{blanz1999} for $B_{id}$, $A_{alb}$, $\hat{S}$ and $\hat{I}$ as well as FaceWarehouse ~\cite{cao2014facewarehouse} for $B_{exp}$. We model the illumination using second order Spherical Harmonics and assume Lambertian surface reflectance.  We denote the illumination as $L \in \mathbf{R}^{27}$.

Following the optimization scheme of ~\cite{f2f}, we jointly solve for all the unknowns $\textbf{Y} = \{ S, I, R, t, P, L \}$ leveraging the Gauss-Newton method applied to iteratively re-weighted least squares with three levels of image pyramid, where P are the camera parameters. One can refer to ~\cite{f2f} for details of this optimization.  In short, our objective function is:
\begin{equation}
E(\textbf{Y}) = w_{col} E_{col}(\textbf{Y}) + w_{lan} E_{lan}(\textbf{Y}) + w_{reg}E_{reg}(\textbf{Y})
\end{equation}
We use energy weights $w_{col} = 1$, $w_{lan} = 10$ and $w_{reg} = 2.5 \times 10^{-5}$.
The photo-consistency term is given by
\begin{equation}
E_{col}(\textbf{Y}) = \frac{1}{| M | } \sum_{p \in M}  ||C_{gt}(p) - C_{render}(p)||_2
\end{equation}
 where $C_{gt}$ is the input image and $C_{render}$ is the synthesized image.  $p \in M$ denotes pixel visibility in the source image.
The landmark term is given by:
\begin{equation}
E_{lan}(\textbf{Y}) = \frac{1}{|F|} \sum_{f_i \in F}  || f_i - \Pi_P(RS_i + t) ||_2^2
\end{equation}
 where $f_i \in F$ is a 2D facial feature following the method presented in ~\cite{kazemi2014one}.
The regularization $E_{reg}$ term ensures that faces stay close to the normal distribution.  This term prevents degenerative faces when performing the fitting:
\begin{equation}
E_{reg}(\textbf{Y}) = \sum_{i = 1}^{80} [ (\frac{\beta_{id, i}}{\sigma_{id,i}})^2 + (\frac{\alpha_{alb, i}}{\sigma_{alb, i}})^2] + \sum_{i =1}^{29} ( \frac{\beta_{exp,i}}{\sigma_{exp,i}})^2
\end{equation}

\section{ Dynamic Texture Synthesis}

\subsection{Deep Learning Framework}

\begin{figure*}[th]
	\centering
	\includegraphics[width=1\linewidth]{figures/network/network5.pdf}
	\caption{The network architecture. The encoder takes as input the identity texture and the expression texture, and the decoder outputs the dynamic texture. The adversarial discriminator is used during training to judge whether the output texture looks real or not.}\label{fig:network}
	\vspace{-0.15in}
\end{figure*}

The core of our dynamic texture synthesis pipeline for inferring fine details is a Conditional Generative Adversarial Network used to infer deformations from a source texture onto a target texture.
Broadly speaking, a Generative Adversarial Network (GAN) $G$ is a function which produces ``realistic'' output from a noise vector.
That is, given a distribution $M$, a variable $z \sim M$, and a set of ground truth ``real'' examples $X$,  we would like $G(z)$ to be indistinguishable between
any $x \sim X$.  Formally, by indistinguishable we mean to say that the generator function fools as best as possible a discriminator function, $D$,
trained precisely to separate the generated output $G(z)$ from the real output drawn from $X$. A conditional GAN, on the other hand, takes as input both the noise vector $z$, along with additional input $y$ in order to produce the output $x$.
Notice that $y$ and $x$ are not necessarily from the same set.

In our setting, we attempt to learn a target texture deformation given a source texture deformation.
That is, given a source texture $U_{source}$ that encodes a given expression, we would like to transfer this expression to a neutral-pose target texture $N_{target}$.
For example, the source texture expression might contain a wrinkle on the left cheek that we would like to synthesize onto the target neutral texture.
In this case, the output is $U_{target}$, the target texture with the wrinkle, and we are conditioning on $U_{source}$ and $N_{target}$.

Note that neural networks are inclined to be heavily affected by noise and variation within the training corpus.  For this reason, we work in the UV texture space of the captured facial albedos.  This provides many benefits during training.  First of all, it minimizes variations in the input due to factors such as lighting, image background and head pose. In UV space, the mouth, eyes, and nose are in the exact same location in each image - the only thing that changes is the content of those locations. Working in this space makes it possible to retarget mouth motion, blinking, scrunching, and various other skin deformations that would be much harder to learn otherwise.

\subsection{Loss Function}

The energy function we minimize is given by
\begin{equation} \label{eqn:1}
\begin{split}
L_{cGAN}(G,D)& = E_{{x,y}\sim p_{data}({x,y}),z\sim p_z{z}}[\log D({x,y})]\\
& +E_{{x,y}\sim p_{data}(x),z\sim p_z(z)}[\log(1-D(G(x,z)))]
\end{split}
\end{equation}

In our formulation, $x$ is the pair $(U_{source}, N_{target})$ and $y$ is given by $U_{target}$.
In addition to this energy, the generator $G$ also attempts to minimize the reconstruction loss to the target $y$ in the $\ell_1$ sense.
That is, $L_{\ell_1}(G)= E[\parallel y-G(x,z)\parallel_1]$ ~\cite{pix2pix}.

$G$ attempts to minimize this objective while $D$ attempts to maximize it.  In other words:
$ G_{opt}=\text{arg}\min_{G}\max_{D} L_{cGAN}(G,D)+\lambda L_{\ell_1}(G)$, where $\lambda$
encodes the relative weighting of the different errors ~\cite{pix2pix}.  In our implementation of the conditional GAN,
we do not input any noise during generation, but otherwise the optimization program is accurate.  We set $\lambda = 0.001$ in all experiments.
This parameter can be viewed as a balancing between the need to reconstruct accurate wrinkles with the need to have the final texture
remain "realistic".

\subsection{Network Architecture}

We use a similar architecture as \cite{pix2pix}.
First, we concatenate the driver expression and the neutral target identity, $(U_{source}, N_{target})$, along their color-channel dimension as input.
The generator output is the target expression $y$, which is the expression transferred from the source to target texture.

Second, we use a masked prior to define the $\ell_1$ and adversarial loss.
The mask is applied on the $\ell_1$ loss such that the the loss around the mouth and eye regions is ten times higher than in other areas - we
do this because wrinkles, blinking, and most texture deformations are a lot more prevalent in these areas.
For the discriminator, we adopt a Markovian Discriminator
(PatchGAN) with patch size set to $70 \times 70$. We found that adding skip connections between
encoder layers and decoder layers, skipping around the code layer, greatly reduces noise in the output image.
%Finally, we do not input any
The parameters are
optimized using ADAM with back-propagation. Our input and output resolution are $256\times 256$.

%%\begin{eqnarray*}
%%G^*&=&arg\min\max L_{cGAN}(G,D)+\lambda L_{L1}(G) \\
%%L_{L1}(G)&=& E[M\cdot\parallel y-G({x,y},z)\parallel_1] \\
%%L_{GAN}(G,D)&=& E_{{x,y}\sim p_{data}({x,y}),z\sim p_z{z}}[\log D({t})]+E_{{x,y}\sim p_{data}(x),z\sim p_z(z)}[\log(1-D(G(x,z)))]
%%\label{eqn:1}
%%\end{eqnarray*}

\subsection{Mouth Synthesis}

\begin{figure}[h]
	\centering
	\includegraphics[width=1\linewidth]{figures/flow/opticalflow.pdf}
	\caption{Visualization of flow-upsampling from source mouth to low-resolution inferred mouth.  Left is source mouth, top is target mouth, bottom is flow field, right is final result.}\label{fig:flow}
	\vspace{-0.05in}
\end{figure}
We model the mouth interior region as a part of the UV texture.  When the mouth is closed, the area between the lips get projected onto this area to
make a pink color.  When the mouth is open, the mouth interior, including the teeth and tongue, are projected to this region (Fig.~\ref{fig:flow}).

Using our deep-learning framework, we are able to transfer open-mouth expressions onto the closed-mouth neutral target textures and infer the
inner mouth region.  However, due to lack of training data for the mouth interior, the inferred texture here tends to be of rather low resolution.  In order
to improve this area, we use SIFT-Flow ~\cite{siftflow} to redraw the target mouth using the source mouth for each frame.  In particular, after
computing SIFT features at the pixel level, we perform matching based on the following energy term:
\begin{equation}
\begin{split}
E(w)& = \sum_p \min (||s_1(p) - s_2(p + w(p))||_1, t)  \\
&\hspace{-6mm} +\sum \eta(|u(p)| + |v(p)|)  \\
&\hspace{-6mm} +\sum_{(p,q) \in \epsilon} min(\alpha |u(p) - u(q)|, d) + min(\alpha|v(p) - v(q)|, d)
\end{split}
\end{equation}
The terms in the equation are, in order, the data term, the small displacement term, and the spatial regularization term ~\cite{sift},
where $w(p) = (u(p), v(p))$ is the flow vector at a point $p = (x,y)$, and $s_1$ and $s_2$ are the sift features
computed for either image 1 or image 2 at the point $p$.  The second and third term regularize the matching by pushing
nearby points to get mapped to each other.

Inferring the inner mouth also has the added benefit of improving the lip texture around the mouth during tracking failure of the original video: tracking of tight-lipped
expressions such as kissing often fail and the lip texture gets projected to the interior region in UV space.  During rendering, this causes
the lips to be thinner than they should be, which gives an unnatural appearance.  By inferring this inner-mouth region, we are able to synthesize realistic
kissing faces on the target even when lip tracking fails on the source.

%\subsection{Fitting to Video Sequence}

%We adopt a similar optimization scheme as ~\cite{f2f} to obtain the per-frame mesh and per-frame texture of the source video sequence at run-time.  In particular:

%%IMPLEMENTATION DETAILS OF F2F

%Using the per-frame texture, we synthesize per-frame textures and mouth flows using the above framework on the single RGB target image, and then
%are able render a realistic animation of the target.

\section{Video Face Replacement via Blending}

Once we have the per-frame textures and retargeted mesh, we are also able to transfer the target appearance back onto the source video
sequence for a detailed and realistic animation (Fig.~\ref{fig:blend}). We use a graph-cut approach similar to ~\cite{replace} in order
to achieve this.  In particular, for blending the retargeted faces to the source video in a photorealistic manner,
we first find a graph-cut to partition each frame into two regions, which determines whether
a pixel comes from the frame in the source video or the image of the rendered retargeted face model(Fig.~\ref{fig:blend}a-c).
We then linearly blend the target and source regions of the images along the seam to achieve a smooth and realistic result (Fig.~\ref{fig:blend}d).

\begin{figure}[t]
  \centering
  \includegraphics[width=0.6\linewidth]{figures/blending/blending.pdf}
  \caption{a) The input consists of the frames of the source video (left) and the rendered retargeted mesh (right). b) Naively projecting all of the target mesh's vertices onto the source frame results in an unrealistic and incoherent result (left) when a more optimal partition is outlined in red on the image on the right.
	c) The composition of the two images using the optimal scene.
	d) Linearly blending the pixels along the seam gives a smooth and realistic result.}
	\label{fig:blend}
  \vspace{-0.15in}
\end{figure}

\subsection{Graph-Cut}
Similar to ~\cite{replace},
we optimize the partition so as to ensure spatial coherence between neighboring pixel values
and temporal coherence between frames.

If we naively project the mesh back onto the frame of the source video, as seen in the left-hand image of Fig~\ref{fig:blend}b, the result is not smooth or realistic.
In order to maintain spatial coherence across large variations in pose and expression,
 we construct a graph-cut problem on each of the frames in the source video and their corresponding retargeted mesh as in ~\cite{graphcut}. For each frame, the graph cut algorithm labels each vertex on the mesh as either a source vertex or target vertex in a manner that minimizes the difference in pixel values across neighboring vertices, as shown on the right-hand image of Fig.~\ref{fig:blend}b. We then project the seam of the labeled mesh onto the image (Fig.~\ref{fig:blend}c).

The nodes of the graph represent the vertices on the mesh for each frame in the source video.
Let each vertex be denoted as $V_{t,i}$, where $t$ refers to the $t$th frame, and $i$ refers to the $i$th vertex on the mesh.
%and construct the following graph to calculate the seam in the rest region.

In order to minimize the difference between source and target pixels along the seam, for each frame, we set weights on the graph for each edge between each pair of vertices $V_{t,u}$ and  $V_{t,v}$ that share an edge on the mesh as:
\begin{equation}
\begin{split}
W_m(V_{t,u}, V_{t,v}) = & \|I_{s}(V_{t,u})-I_{s}(V_{t,v})\|\\
& + \|I_{t}(V_{t,u})-I_{t}(V_{t, v})\|
\end{split}
\end{equation}

 $I_{s}$is the source image, $I_{w}$ is the target image, $I_{s}(V)$ is the color at the pixel where $V$ is projected onto the source frame, and $I_{t}(V)$ is the color at the pixel V is projected onto in the rendered target image.
 For temporal coherence, we set edges between vertices $V_{t,i}$ and $V_{t+1,i}$ for all $i$ and $t$, with weight as:
\begin{equation}
\begin{split}
W_f(V_{t,i}, V_{t+1,i})& = \lambda \|I_{s}(V_{t,i})-I_{s}(V_{t+1,i} +1)\|^{-1}\\
              & + \|I_{t}(V_{t,i})-I_{t}(V_{t+1,i} + 1)\|^{-1}
\end{split}
\end{equation}

After computing the seam, we can composite the target appearance onto the source video and linearly blend the pixels across the seam for a realistic novel reenactment.