%!TEX root = ../thesis.tex
% ******************************************
% 			Thesis Appendix B
% ******************************************

\chapter{Computational methods}

\section{Addressing the mean confounding effect \\ for differential variability testing}
\markboth{Computational methods}{B.1 Addressing the mean confounding effect for differential variability testing}
\label{appB.1}

\subsection{Prior specifications of the extended BASiCS model}

\begin{align*}
\mu_i &\ind \mbox{log-Normal}\left(0, s^2_{\mu}\right) \\
\delta_i| \mu_i,\beta,\sigma^2, \lambda_i, \eta &\ind \text{log-Normal}\left( \text{f}(\mu_i),\frac{\sigma^2}{\lambda_i} \right)\\
\lambda_i|\eta &\ind \text{Gamma}\left(\frac{\eta}{2},\frac{\eta}{2}\right)\\
\beta|\sigma^2 & \sim \textnormal{Normal}(m_\beta,\sigma^2V_\beta),\\
\sigma^2 & \sim  \textnormal{Inv-Gamma}(a_{\sigma^2},b_{\sigma^2}),\\
s_j & \iid  \textnormal{Gamma}(a_s,b_s) \\
(\phi_1, \ldots, \phi_n)' & \sim  n \times \textnormal{Dirichlet}(a_\phi),\\
\theta & \sim  \textnormal{Gamma}(a_\theta,b_\theta)
\end{align*}

\newpage

\subsection{Starting values for hyper-parameters}
\label{appB.1.hyper}

\begin{align*}
m_{\beta} & = \mathbf{0}_L \hspace{0.2cm} \text{(an $L$-dimensional vector of zeroes)}\\
V_{\beta} & = \mathbf{I}_L \hspace{0.2cm} \text{(an $L$-dimensional identity matrix)}\\
a_{\sigma^2} & = 2\\
b_{\sigma^2} & = 2\\
s^2_{\mu}& =  0.5\\
a_s& = 1\\
b_s& = 1\\
a_\phi& =  \mathbf{1}_n\\
a_\theta& = 1\\
b_\theta& = 1
\end{align*}

\subsection{Likelihood of the extended BASiCS model}

The likelihood function of the extended BASiCS model takes the form:

\begin{align}
\Lagr = & \left[\prod_{i=1}^{q_0}\prod_{j=1}^n\frac{\Gamma(x_{ij}+\frac{1}{\delta_i})}{\Gamma(\frac{1}{\delta_i})x_{ij}!}\left(\frac{\frac{1}{\delta_i}}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^\frac{1}{\delta_i}\left(\frac{\phi_j\nu_j\mu_i}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^{x_{ij}}\right] \nonumber\\
&\times\left[\prod_{i=q_0+1}^{q}\prod_{j=1}^n\frac{(\nu_j\mu_i)^{x_{ij}}}{x_{ij}!}\exp\lbrace-\nu_j\mu_i\rbrace\right]\times{}\left[\prod_{j=1}^n\frac{(s_j\theta)^{-\frac{1}{\theta}}}{\Gamma(\frac{1}{\theta})}\nu_j^{\frac{1}{\theta}-1}\exp\left\lbrace-\frac{\nu_j}{s_j\theta}\right\rbrace\right].
\end{align}

\subsection{Derivation of full conditionals for the extended BASiCS model}
\label{appB.1.derivation}

To calculate the full conditionals ($\pi^*(\cdot)$) for Gibbs sampling, the likelihood ($\Lagr_j$ for cell-specific likelihood, $\Lagr_i$ for gene-specific likelihood) is multiplied by the relevant prior specifications ($\pi(\cdot)$).
$q_0$ indicates the number of biological genes while $q$ is the number of biological and spike-in genes.
$L$ is the number of Gaussian Radial Basis Functions. $\Lambda$ is a diagonal matrix with elements $(\lambda_1, \ldots, \lambda_{q_0})$ and $Y = (\log(\delta_1), \ldots, \log(\delta_{q_0}))'$.

\newpage

\subsubsection{Helper functions}

For simplicity, the distributions of the joint prior specification, the product of this distribution across all biological genes $q_0$ and the multivariate Normal distribution for the prior on $\beta$ take the form:

\begin{align*}
\text{log-Normal}(\text{f}(\mu_i),\frac{\sigma^2}{\lambda_i})&\propto{}(\frac{\lambda_i}{\sigma^2})^{\frac{1}{2}}\exp\lbrace-\frac{\lambda_i}{2\sigma^2}(\log(\delta_i)-x_{i,\ast}^T\beta)^2\rbrace\\
\prod_{i=1}^{q_o}\text{log-Normal}(\text{f}(\mu_i),\frac{\sigma^2}{\lambda_i})&\propto{}(\frac{1}{\sigma^2})^{\frac{q_0}{2}}(\prod_{i=1}^{q_0}\lambda_i)^{\frac{1}{2}}\exp\lbrace-\frac{1}{2\sigma^2}[(Y-X\beta)^T\Lambda(Y-X\beta)\\
\textnormal{Normal}(m_\beta,\sigma^2V_\beta)&\propto{}(\frac{1}{\sigma^2})^{\frac{L+2}{2}}\exp\lbrace-\frac{1}{2\sigma^2}(\beta-m_\beta)^TV_\beta^{-1}(\beta-m_\beta)\rbrace\\
\end{align*}

Here, $x_{i,\ast}^T$ is the transposed vector of the $i$th row in the model matrix $X$.

\subsubsection{Full conditionals}

Full conditional for $\mu_i$ across all cells:
\begin{fleqn}
\begin{align*}
&\pi^*(\mu_i|\cdot)&&\propto{} \Lagr_i\times\pi(\mu_i)\times\pi(\delta_i|\mu_i,\beta,\sigma^2,\eta)\\
& &&\propto{}\left[\prod_{j=1}^n\left(\frac{1}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^\frac{1}{\delta_i}\left(\frac{\mu_i}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^{x_{ij}}\right]\times\exp\left(-\frac{(\log(\mu_i)-0)^2}{2a_\mu^2}\right)\frac{1}{\mu_i}\\
& &&\times\exp\left\lbrace-\frac{\lambda_i}{2\sigma^2}(\log(\delta_i)-f(\mu_i))^2\right\rbrace\\
& &&\propto\left[\prod_{j=1}^n{}\frac{(\mu_i)^{x_{ij}}}{(\phi_j\nu_j\mu_i+\frac{1}{\delta_i})^{\frac{1}{\delta_i}+x_{ij}}}\right]\times{}\exp\left(-\frac{(\log(\mu_i))^2}{2a_\mu^2}-\frac{\lambda_i(\log(\delta_i)-f(\mu_i))^2}{2\sigma^2}\right)\frac{1}{\mu_i}\\
& &&\propto{}\frac{\mu_i^{\sum_{j=1}^n{}x_{ij}}}{\prod_{j=1}^n{}(\phi_j\nu_j\mu_i+\frac{1}{\delta_i})^{\frac{1}{\delta_i}+x_{ij}}}\times{}\exp\left(-\frac{(\log(\mu_i))^2}{2a_\mu^2}-\frac{\lambda_i(\log(\delta_i)-f(\mu_i))^2}{2\sigma^2}\right)\frac{1}{\mu_i}
\end{align*}
\end{fleqn}

Full conditional for $\delta_i$ across all cells:
\begin{fleqn}
\begin{align*}
&\pi^*(\delta_i|\cdot)&&\propto{} \Lagr_i\times\pi(\delta_i|\mu_i,\beta,\sigma^2,\eta)\\
& &&\propto{}\left[\prod_{j=1}^n\frac{\Gamma(x_{ij}+\frac{1}{\delta_i})}{\Gamma(\frac{1}{\delta_i})}\left(\frac{\frac{1}{\delta_i}}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^\frac{1}{\delta_i}\left(\frac{1}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^{x_{ij}}\right]\\
& &&\times\exp\left\lbrace-\frac{\lambda_i(\log(\delta_i)-f(\mu_i))^2}{2\sigma^2}\right\rbrace\frac{1}{\delta_i}\\
& &&\propto\left[\prod_{j=1}^n\frac{\Gamma(x_{ij}+\frac{1}{\delta_i})}{\Gamma(\frac{1}{\delta_i})}\frac{(\frac{1}{\delta_i})^{\frac{1}{\delta_i}}}{(\phi_j\nu_j\mu_i+\frac{1}{\delta_i})^{\frac{1}{\delta_i}+x_{ij}}}\right]\times{}\exp\left\lbrace-\frac{\lambda_i(\log(\delta_i)-f(\mu_i))^2}{2\sigma^2}\right\rbrace\frac{1}{\delta_i}\\
\end{align*}
\end{fleqn}

Full conditional for $\beta$ across all cells and genes:
\begin{fleqn}
\begin{align*}
\pi^*(\beta|\cdot)&\propto{} \Lagr\times\pi(\delta|\mu,\beta,\sigma^2,\eta)\times\pi(\beta)\\
&\propto{}\exp\lbrace-\frac{1}{2\sigma^2}[(Y-X\beta)'\Lambda(Y-X\beta)\rbrace\times\\
&\exp\lbrace-\frac{1}{2\sigma^2}(\beta-m_\beta)'V_\beta^{-1}(\beta-m_\beta)\rbrace\\
&\propto{}\exp\lbrace-\frac{1}{2\sigma^2}[(Y-X\beta)'\Lambda(Y-X\beta)+(\beta-m_\beta)'V_\beta^{-1}(\beta-m_\beta)]\rbrace\\
&\propto{}\exp\lbrace-\frac{1}{2\sigma^2}[Y'\Lambda{}Y-2(X\beta)'\Lambda{}Y+(X\beta)'\Lambda{}X\beta\\
&+\beta'V_\beta^{-1}\beta-2m_\beta'V_\beta^{-1}\beta+m_\beta{}V_\beta^{-1}m_\beta{}]\rbrace\\
&\propto{}\exp\lbrace-\frac{1}{2\sigma^2}[\beta'X'\Lambda{}X\beta+\beta'V_\beta^{-1}\beta-2X'\Lambda{}Y\beta-2V_\beta^{-1}m_\beta\beta]\rbrace\\
&\propto{}\exp\lbrace-\frac{1}{2\sigma^2}[\beta'(X'\Lambda{}X+V_\beta^{-1})\beta-2(X'\Lambda{}Y+V_\beta^{-1}m_\beta)\beta]\rbrace\\
&\propto{}N(m^*_\beta,\sigma^2V^*_\beta)
\end{align*}
\end{fleqn} With
\begin{fleqn}
\begin{align*}
V^*_\beta&=(X'\Lambda{}X+V_\beta^{-1})^{-1}\\
m^*_\beta&=(X'\Lambda{}X+V_\beta^{-1})^{-1}(X'\Lambda{}Y+V_\beta^{-1}m_\beta{})
\end{align*}

Full conditional for $\lambda_i$ across all cells:
\begin{fleqn}
\begin{align*}
\pi^*(\lambda_i|\cdot)&\propto{}\Lagr_i\times\pi(\delta_i|\mu,\beta,\sigma^2,\eta)\times\pi(\lambda_i)\\
&\propto{}\lambda_i^{1/2}\exp\lbrace-\frac{\lambda_i}{2\sigma^2}(\log(\delta_i)-f(\mu_i))^2\rbrace\cdot\lambda_i{}^{\frac{\eta}{2}-1}\exp(-\lambda_i{}\frac{\eta}{2})\\
&\propto{}\lambda_i^{\frac{\eta+1}{2}-1}\exp\lbrace-\frac{\lambda_i}{2}(\eta+\frac{1}{\sigma^2}(\log(\delta_i)_i-f(\mu_i)))\rbrace\\
&\propto{}\textnormal{Gamma}(a^*_\lambda,b^*_\lambda)
\end{align*}
\end{fleqn}
With
\begin{align*}
a^*_\lambda&=\frac{\eta+1}{2}\\
b^*_\lambda&=\frac{1}{2}\left[\frac{1}{\sigma^2}(\log(\delta_i)-f(\mu_i))^2+\eta\right]
\end{align*}
\end{fleqn}

Full conditional for $\sigma^2$ across all cells and genes:
\begin{fleqn}
\begin{align*}
\pi^*(\sigma^2|\cdot)&\propto{}\Lagr\times\pi(\delta|\mu,\beta,\sigma^2,\eta)\times\pi(\sigma^2)\\
&\propto{}\left(\frac{1}{\sigma^2}\right)^{\frac{q_0}{2}}\exp\lbrace-\frac{1}{2\sigma^2}(Y-X\beta)'\Lambda(Y-X\beta)\rbrace\\
&\cdot\left(\frac{1}{\sigma^2}\right)^{L+2}\exp\lbrace-\frac{1}{2\sigma^2}(\beta-m_\beta{})'V_\beta^{-1}(\beta-m_\beta{})\rbrace\\
&\cdot\left(\frac{1}{\sigma^2}\right)^{a_{\sigma^2}+1}\exp\lbrace-\frac{b_{\sigma^2}}{\sigma^2}\rbrace\\
&\propto{}\left(\frac{1}{\sigma^2}\right)^{\frac{q_0+L+2}{2}+a_{\sigma^2}+1}\exp\lbrace-\frac{1}{\sigma^2}[b_{\sigma^2}+\frac{1}{2}[(Y-X\beta)'\Lambda(Y-X\beta) \\
&+ (\beta-m_\beta{})'V_\beta^{-1}(\beta-m_\beta{})]]\rbrace
\end{align*}
\end{fleqn}
After completing the squares
\begin{fleqn}
\begin{align*}
&\propto{}\left(\frac{1}{\sigma^2}\right)^{\frac{q_0+L+2}{2}+a_{\sigma^2}+1}\exp\lbrace-\frac{1}{\sigma^2}[b_{\sigma^2}+\frac{1}{2}(Y'\Lambda{}Y+m_\beta{}'V_\beta{}^{-1}m_\beta{}\\
&+(\beta-m^*_{\beta})'(V^*_{\beta})^{-1}(\beta-m^*_{\beta})-(m^*_{\beta})'(V^*_{\beta})^{-1}m^*_{\beta})]\rbrace\\
&\propto{}(\frac{1}{\sigma^2})^{a_{n,\sigma^2}+1}\exp(-\frac{b^*_{\sigma^2}}{\sigma^2})\\
&\propto{}\textnormal{Inv-Gamma}(a^*_{\sigma^2},b^*_{\sigma^2})
\end{align*}
\end{fleqn}
With
\begin{fleqn}
\begin{align*}
a^*_{\sigma^2}&=\frac{q_0+L+2}{2}+a_{\sigma^2}\\
b^*_{\sigma^2}&=b_{\sigma^2}+\frac{1}{2}(Y'\Lambda{}Y+m_\beta'V_\beta^{-1}m_\beta+(\beta-m^*_{\beta})'(V^*_{\beta})^{-1}(\beta-m^*_{\beta})-(m^*_{\beta})'(V^*_{\beta})^{-1}m^*_{\beta})\\
&\equiv{}b_{\sigma^2}+\frac{1}{2}(Y'\Lambda Y+ m'_{\beta} V_\beta^{-1} m_\beta + \beta' (V^*_{\beta})^{-1} \beta - 2 \beta' (V^*_{\beta})^{-1} m^*_{\beta} )
\end{align*}
\end{fleqn}

Full conditional for $s_j$ across all genes:
\begin{fleqn}
\begin{align*}
\pi^*(s_j|\cdot)&\propto{}\Lagr_j\times\pi(s_j)\\
&\propto{}s_j{}^{a_s-1}\exp\lbrace-b_ss_j\rbrace{}s_j{}^{-\frac{1}{\theta}}\exp\lbrace-\frac{\nu_j}{s_j\theta}\rbrace\\
&\propto{}s_j{}^{a_s-\frac{1}{\theta}-1}\exp\lbrace-\frac{\nu_j}{s_j\theta}-b_ss_j\rbrace
\end{align*}
\end{fleqn}

Full conditional for $\phi$ across all genes and cells:
\begin{fleqn}
\begin{align*}
\pi^*(\phi_j|\cdot)&\propto{}\Lagr_j\times\pi(\phi_j)\\
&\propto{}\prod_{i=1}^{q_0}\prod_{j=1}^{n}\left(\frac{1}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^\frac{1}{\delta_i}\left(\frac{\phi_j}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^{x_{ij}}\times{}\pi(\phi_j)\\
&\propto{}\frac{\prod_{i=1}^{q_0}\prod_{j=1}^{n}\phi_j{}^{x_{ij}}}{\prod_{i=1}^{q_0}\prod_{j=1}^{n}(\phi_j\nu_j\mu_i+\frac{1}{\delta_i})^{\frac{1}{\delta_i}+x_{ij}}}\times{}\pi(\phi_j)\\
&\propto{}\frac{\prod_{i=1}^{q_0}\phi_j{}^{\sum_{j=1}^nx_{ij}}}{\prod_{i=1}^{q_0}\prod_{j=1}^{n}(\phi_j\nu_j\mu_i+\frac{1}{\delta_i})^{\frac{1}{\delta_i}+x_{ij}}}\times{}\pi(\phi_j)
\end{align*}
\end{fleqn}

Full conditional for $\nu_j$ across all genes:
\begin{fleqn}
\begin{align*}
\pi^*(\nu_j|\cdot)&\propto{}\Lagr_j\times\pi(\nu_j)\\
&\propto{}\left[\prod_{i=1}^{q_0}\left(\frac{1}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^\frac{1}{\delta_i}\left(\frac{\nu_j}{\phi_j\nu_j\mu_i+\frac{1}{\delta_i}}\right)^{x_{ij}}\right]\left[\prod_{i=q_0+1}^{q}\nu_j{}^{x_{ij}}\exp\lbrace-\nu_j\mu_i\rbrace\right]\\
&\times{}\nu_j^{\frac{1}{\theta}-1}\exp\lbrace-\frac{\nu_j}{s_j\theta}\rbrace
\end{align*}
\end{fleqn}

Full conditional for $\theta$ across all genes and cells:

\begin{fleqn}
\begin{align*}
\pi^*(\theta|\cdot)&\propto{}\Lagr\times\pi(\theta)\\
&\propto{}\left[\prod_{j=1}^{n}\frac{(s_j\theta)^{-\frac{1}{\theta}}}{\Gamma(\frac{1}{\theta})}\nu_j^{\frac{1}{\theta}-1}\exp\lbrace-\frac{\nu_j}{s_j\theta}\rbrace\right]\times{}\theta^{a_\theta-1}\exp\lbrace-b_\theta\theta\rbrace\\
&\propto{}\left[\prod_{j=1}^{n}\frac{(s_j\theta)^{-\frac{1}{\theta}}}{\Gamma(\frac{1}{\theta})}\frac{1}{\nu_j}^{-\frac{1}{\theta}}\exp\lbrace-\frac{\nu_j}{s_j\theta}\rbrace\right]\times\theta^{a_\theta-1}\exp\lbrace-b_\theta\theta\rbrace\\
&\propto{}\left[\prod_{j=1}^{n}\frac{\frac{s_j}{\nu_j}^{-\frac{1}{\theta}}}{\Gamma(\frac{1}{\theta})}\theta^{-\frac{1}{\theta}}\exp\lbrace-\frac{\nu_j}{s_j\theta}\rbrace\right]\times{}\theta^{a_\theta-1}\exp\lbrace-b_\theta\theta\rbrace\\
&\propto{}\frac{\left(\prod_{j=1}^{n}\frac{s_j}{\nu_j}\right)^{-\frac{1}{\theta}}}{\Gamma{}^n(\frac{1}{\theta})}\theta^{-\frac{n}{\theta}}\exp\lbrace-\frac{1}{\theta}\sum_{j=1}^n\frac{\nu_j}{s_j}\rbrace\theta^{a_\theta-1}\exp\lbrace-b_\theta\theta\rbrace\\
&\propto{}\frac{\left(\prod_{j=1}^{n}\frac{s_j}{\nu_j}\right)^{-\frac{1}{\theta}}}{\Gamma{}^n(\frac{1}{\theta})}\theta^{a_\theta-\frac{n}{\theta}-1}\exp\lbrace-\frac{1}{\theta}\sum_{j=1}^n\frac{\nu_j}{s_j}-b_\theta\theta\rbrace
\end{align*}
\end{fleqn}


\chapter{Suffix structures}

\section{Suffix array}

There are several algoirthms for the Suffix Array construction. The most prominent is DC3 (Difference Cover, size 3) and which is a $O(n)$ time complexity divide-and-conquer algorithm.

For $T=abaabababbabbb$ calculate Suffix Array (\textit{SA}) and Longest Common Prefix (\textit{LCP}).

\begin{table}[h]
  \footnotesize
	\begin{tabular}{c|cccccccccccccccc}
		        $i$ 		& -1 & 0   & 1   & 2   & 3   & 4   & 5   & 6   & 7   & 8   & 9   & 10  & 11  & 12  & 13  & 14 \\
		\hline $T[i]$ 	& \# & $a$ & $b$ & $a$ & $a$ & $b$ & $a$ & $b$ & $a$ & $b$ & $b$ & $a$ & $b$ & $b$ & $b$ & \$ \\
		\hline $SA[i]$ 	& 14 & 2   & 0   & 3   & 5   & 7   & 10  & 13  & 1   & 4   & 6   & 9   & 12  & 8   & 11  & (-1) \\
		\hline $LCP[i]$ & 0  & 0   & 1   & 3   & 4   & 2   & 3   & 0   & 1   & 2   & 3   & 4   & 1   & 2   & 2   & 0 \\
	\end{tabular}
  \caption{Suffix Array for $T=abaabababbabbb$.}
\end{table}

Suffix Array can be constructed through simple sorting by adding padding characters \# and \$, where \$ $\leq a \in \Sigma \leq$ \#:\\
$T_{padded}=$\#$abaabababbabbb\$$.

Suffixes can be easily sorted, see~\ref{sortingsuffixes}.

\begin{table}[h]
  \footnotesize
	\begin{tabular}{c|l}
		i & unsorted \\ \hline
		-1 & \#abaabababbabbb\$ \\
		0 & abaabababbabbb\$ \\
		1 & baabababbabbb\$ \\
		2 & aabababbabbb\$ \\
		3 & abababbabbb\$ \\
		4 & bababbabbb\$ \\
		5 & ababbabbb\$ \\
		6 & babbabbb\$ \\
		7 & abbabbb\$ \\
		8 & bbabbb\$ \\
		9 & babbb\$ \\
		10 & abbb\$ \\
		11 & bbb\$ \\
		12 & bb\$ \\
		13 & b\$ \\
		14 & \$ \\
	\end{tabular}
  \hspace*{1cm}
	\begin{tabular}{c|l}
		SA[i] & sorted \\ \hline
		14 & \$ \\
		2 & aabababbabbb\$ \\
		0 & abaabababbabbb\$ \\
		3 & abababbabbb\$ \\
		5 & ababbabbb\$ \\
		7 & abbabbb\$ \\
		10 & abbb\$ \\
		13 & b\$ \\
		1 & baabababbabbb\$ \\
		4 & bababbabbb\$ \\
		6 & babbabbb\$ \\
		9 & babbb\$ \\
		12 & bb\$ \\
		8 & bbabbb\$ \\
		11 & bbb\$ \\
		-1 & \#abaabababbabbb\$ \\
	\end{tabular}
	\label{sortingsuffixes}
	\caption{Sorting suffixes of $T_{padded}=$\#$abaabababbabbb\$$.}
\end{table}

For $P=abaa$ find all occurances using \textit{SA Simple Search}.

\begin{enumerate}
	\item \begin{itemize}
			\item $d = -1$, $f = n = 14$
		\end{itemize}
	\item \begin{itemize}
		\item $i \leftarrow (-1 + 14)/2 = 6$
		\item $L_6 = T[SA[6] .. 13] = T[13 .. 13] = b$
		\item $l \leftarrow lcp(x, L_6) = 0$
		\item $L_6[0] > x[0] \Rightarrow b > a \Rightarrow f \leftarrow i = 6$
	\end{itemize}
	\item \begin{itemize}
		\item $i \leftarrow (-1 + 6)/2 = 2$
		\item $L_2 = T[SA[2] .. 13] = T[3 .. 13] = abababbabbb$
		\item $l \leftarrow lcp(x, L_2) = 3$
		\item $L_2[3] > x[3] \Rightarrow b > a \Rightarrow f \leftarrow i = 2$
	\end{itemize}
\item \begin{itemize}
		\item $i \leftarrow (-1 + 2)/2 = 0$
		\item $L_0 = T[SA[0] .. 13] = T[2 .. 13] = aabababbabbb$
		\item $l \leftarrow lcp(x, L_0) = 1$
		\item $L_0[1] < x[1] \Rightarrow a < b \Rightarrow d \leftarrow i = 0$
	\end{itemize}
\item \begin{itemize}
		\item $i \leftarrow (0 + 2)/2 = 1$
		\item $L_1 = T[SA[1] .. 13] = T[0 .. 13] = abaabababbabbb$
		\item $l \leftarrow lcp(x, L_1) = 4$
		\item $l = m \wedge l \neq |L_1| \Rightarrow f \leftarrow i = 1$
	\end{itemize}
\item \begin{itemize}
		\item $d+1 \not< f \Rightarrow (d, f) = (0, 1)$
	\end{itemize}
\end{enumerate}

\section{Suffix trie}

For $T=ababbab$ construct Suffix Trie.

\tikzstyle{a} = [above]
\tikzstyle{c} = [circle, draw, minimum width=0.4cm]
\tikzstyle{e} = [circle, draw, minimum width=0.4cm, accepting]
\tikzstyle{level 1} = [level distance=1cm, sibling distance=2cm]
\tikzstyle{level 2} = [level distance=1cm, sibling distance=1cm]
\tikzstyle{level 3} = [level distance=1cm, sibling distance=1cm]
\tikzstyle{level 4} = [level distance=1cm, sibling distance=1cm]
\begin{tikzpicture}[grow=right]
	\node[c] {}
	child {
		node[c] {}
		child {
			node[e] {}
			child {
				node[c] {}
				child {
					node[c] {}
					child {
						node[c] {}
						child {
							node[c] {}
							child {
								node[e] {}
								edge from parent
								node[a] {$b$}
							}
							edge from parent
							node[a] {$a$}
						}
						edge from parent
						node[a] {$b$}
					}
					edge from parent
					node[a] {$b$}
				}
				edge from parent
				node[a] {$a$}
			}
			child {
				node[c] {}
				child {
					node[c] {}
					child {
						node[e] {}
						edge from parent
						node[a] {$b$}
					}
					edge from parent
					node[a] {$a$}
				}
				edge from parent
				node[a] {$b$}
			}
			edge from parent
			node[a] {$b$}
		}
		edge from parent
		node[a] {$a$}
	}
	child {
		node[e] {}
		child {
			node[c] {}
			child {
				node[e] {}
				child {
					node[c] {}
					child {
						node[c] {}
						child {
							node[e] {}
							edge from parent
							node[a] {$b$}
						}
						edge from parent
						node[a] {$a$}
					}
					edge from parent
					node[a] {$b$}
				}
				edge from parent
				node[a] {$b$}
			}
			edge from parent
			node[a] {$a$}
		}
		child {
			node[c] {}
			child {
				node[c] {}
				child {
					node[e] {}
					edge from parent
					node[a] {$b$}
				}
				edge from parent
				node[a] {$a$}
			}
			edge from parent
			node[a] {$b$}
		}
		edge from parent
		node[a] {$b$}
	};
\end{tikzpicture}

\section{Suffix tree}

For $T=ababbab$ construct Suffix Tree.

\tikzstyle{a} = [above]
\tikzstyle{c} = [circle, draw, minimum width=0.4cm]
\tikzstyle{e} = [circle, draw, minimum width=0.4cm, accepting]
\tikzstyle{level 1} = [level distance=1cm, sibling distance=2cm]
\tikzstyle{level 2} = [level distance=1cm, sibling distance=1cm]
\tikzstyle{level 3} = [level distance=1cm, sibling distance=1cm]
\tikzstyle{level 4} = [level distance=1cm, sibling distance=1cm]
\begin{tikzpicture}[grow=right]
	\node[c] {}
	child {
		node[e] {}
		child {
			node[e] {}
			edge from parent
			node[above right] {$abbab$}
		}
		child {
			node[e] {}
			edge from parent
			node[a] {$bab$}
		}
		edge from parent
		node[above right] {$ab$}
	}
	child {
		node[e] {}
		child {
			node[e] {}
			child {
				node[e] {}
				edge from parent
				node[a] {$bab$}
			}
			edge from parent
			node[above right] {$ab$}
		}
		child {
			node[e] {}
			edge from parent
			node[a] {$bab$}
		}
		edge from parent
		node[a] {$b$}
	};
\end{tikzpicture}

\section{Suffix automaton}

For $T=ababbab$ construct Suffix Automaton.

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto]
	\tikzstyle{every state} = [circle,draw,minimum size=0.4cm]
	\node[initial,state]	(a)					{};
	\node[state]			(b) [right of=a]	{};
	\node[state,accepting]	(c) [right of=b]	{};
	\node[state]			(d) [right of=c]	{};
	\node[state]			(e) [right of=d]	{};
	\node[state]			(f) [right of=e]	{};
	\node[state]			(g) [right of=f]	{};
	\node[state,accepting]	(h) [right of=g]	{};
	\node[state,accepting]			(i) [below of=c]	{};
	\node[state]			(j) [below of=d]	{};
	\node[state,accepting]	(k) [below of=e]	{};
	\path	(a) edge node {$a$} (b)
				edge [below] node {$b$} (i)
			(b) edge node {$b$} (c)
			(c) edge node {$a$} (d)
				edge [bend left] node {$b$} (f)
			(d) edge node {$b$} (e)
			(e) edge node {$b$} (f)
			(f) edge node {$a$} (g)
			(g) edge node {$b$} (h)
			(i) edge [below] node {$b$} (f)
				edge [below] node {$a$} (j)
			(j) edge [below] node {$b$} (k)
			(k) edge [below] node {$b$} (h);
\end{tikzpicture}

\section{Compressed Suffix automaton}

For $T=ababbab$ construct Compressed Suffix Automaton.

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto]
	\tikzstyle{every state} = [circle,draw,minimum size=0.4cm]
	\node[initial,state]	(a)					{};
	\node[state,accepting]	(c) [right of=b]	{};
	\node[state]			(f) [right of=e]	{};
	\node[state,accepting]	(h) [right of=g]	{};
	\node[state,accepting]			(i) [below of=c]	{};
	\node[state,accepting]	(k) [below of=e]	{};
	\path	(a) edge node {$ab$} (c)
				edge node {$b$} (i)
			(c) edge node {$abb$} (f)
				edge [bend left] node {$b$} (f)
			(f) edge node {$ab$} (h)
			(i) edge node {$b$} (f)
				edge node {$ab$} (k)
			(k) edge node {$b$} (h);
\end{tikzpicture}

\clearpage
\section{Suffix Automaton Construction from Suffix Trie}

For $T=ababbab$ construct Suffix Automaton from its Suffix Trie.

States of Suffix Trie are labeled as if it was an automaton.

\tikzstyle{a} = [above]
\tikzstyle{c} = [circle, draw, minimum width=0.4cm]
\tikzstyle{e} = [circle, draw, minimum width=0.4cm, accepting]
\tikzstyle{level 1} = [level distance=1cm, sibling distance=2cm]
\tikzstyle{level 2} = [level distance=1cm, sibling distance=1cm]
\tikzstyle{level 3} = [level distance=1cm, sibling distance=1cm]
\tikzstyle{level 4} = [level distance=1cm, sibling distance=1cm]
\begin{tikzpicture}[grow=right]
	\node[c] {$0$}
	child {
		node[c] {$10$}
		child {
			node[e] {$11$}
			child {
				node[c] {$15$}
				child {
					node[c] {$16$}
					child {
						node[c] {$17$}
						child {
							node[c] {$18$}
							child {
								node[e] {$19$}
								edge from parent
								node[a] {$b$}
							}
							edge from parent
							node[a] {$a$}
						}
						edge from parent
						node[a] {$b$}
					}
					edge from parent
					node[a] {$b$}
				}
				edge from parent
				node[a] {$a$}
			}
			child {
				node[c] {$12$}
				child {
					node[c] {$13$}
					child {
						node[e] {$14$}
						edge from parent
						node[a] {$b$}
					}
					edge from parent
					node[a] {$a$}
				}
				edge from parent
				node[a] {$b$}
			}
			edge from parent
			node[a] {$b$}
		}
		edge from parent
		node[a] {$a$}
	}
	child {
		node[e] {$1$}
		child {
			node[c] {$5$}
			child {
				node[e] {$6$}
				child {
					node[c] {$7$}
					child {
						node[c] {$8$}
						child {
							node[e] {$9$}
							edge from parent
							node[a] {$b$}
						}
						edge from parent
						node[a] {$a$}
					}
					edge from parent
					node[a] {$b$}
				}
				edge from parent
				node[a] {$b$}
			}
			edge from parent
			node[a] {$a$}
		}
		child {
			node[c] {$2$}
			child {
				node[c] {$3$}
				child {
					node[e] {$4$}
					edge from parent
					node[a] {$b$}
				}
				edge from parent
				node[a] {$a$}
			}
			edge from parent
			node[a] {$b$}
		}
		edge from parent
		node[a] {$b$}
	};
\end{tikzpicture}

Transition function is iteratively analyzed. In each iteration the states are put into groups based on their similarity. In the initial interation $g_0$ states are split into two groups: final states and non-final states. The groups are labeled by a first state in the group for convenience. In following in iterations $g_i, i \geq 1$ states are split into groups based on the previsous group and groups to which transions for all symbols of the alphabet lead.

\begin{table}[h]
  \footnotesize
	\begin{tabular}{|c||c|c||c||c|c||c||c|c||c||c|c||c||c|c||c||}
		\hline
		$\delta$ & a & b & $g_0$ & a & b & $g_1$ & a & b & $g_2$ & a & b & $g_3$ & a & b & $g_4$\\ \hline
		0  & 10 & 1  & 0 & 0 & 1 & 0 & 0 & 1 & 0  & 0  & 1  & 0 & 0  & 1  & 0 \\
		1  & 5  & 2  & 1 & 0 & 0 & 1 & 0 & 2 & 1  & 0  & 2  & 1 & 0  & 2  & 1 \\
		2  & 3  & -  & 0 & 0 & 0 & 2 & 0 & 2 & 2  & 0  & -  & 2 & 0  & -  & 2 \\
		3  & -  & 4  & 0 & 0 & 1 & 0 & 2 & 1 & 0  & -  & 4  & 3 & -  & 4  & 3 \\
		4  & -  & -  & 1 & 0 & 0 & 1 & 2 & 2 & 4  & -  & -  & 4 & -  & -  & 4 \\
		5  & -  & 6  & 0 & 0 & 1 & 0 & 2 & 1 & 0  & -  & 1  & 5 & -  & 1  & 5 \\
		6  & -  & 7  & 1 & 0 & 0 & 1 & 2 & 2 & 1  & -  & 2  & 6 & -  & 2  & 6 \\
		7  & 8  & -  & 0 & 0 & 0 & 2 & 0 & 2 & 2  & 0  & -  & 2 & 0  & -  & 2 \\
		8  & -  & 9  & 0 & 0 & 1 & 0 & 2 & 1 & 0  & -  & 4  & 3 & -  & 4  & 3 \\
		9  & -  & -  & 1 & 0 & 0 & 1 & 2 & 2 & 4  & -  & -  & 4 & -  & -  & 4 \\
		10 & -  & 11 & 0 & 0 & 1 & 0 & 2 & 1 & 0  & -  & 11 & 10& -  & 11 & 10 \\
		11 & 15 & 12 & 1 & 0 & 0 & 1 & 2 & 2 & 11 & 15 & 2  & 11& 15 & 2  & 11\\
		12 & 13 & -  & 0 & 0 & 0 & 2 & 0 & 2 & 2  & 0  & -  & 2 & 3  & -  & 2 \\
		13 & -  & 14 & 0 & 0 & 1 & 0 & 2 & 1 & 0  & -  & 4  & 3 & -  & 4  & 3 \\
		14 & -  & -  & 1 & 0 & 0 & 1 & 2 & 2 & 4  & -  & -  & 4 & -  & -  & 4 \\
		15 & -  & 16 & 0 & 0 & 0 & 2 & 2 & 2 & 15 & -  & 2  & 15& -  & 2  & 15\\
		16 & -  & 17 & 0 & 0 & 0 & 2 & 2 & 2 & 15 & -  & 15 & 16& -  & 15 & 16\\
		17 & 18 & -  & 0 & 0 & 0 & 2 & 0 & 2 & 2  & 0  & -  & 2 & 3  & -  & 2 \\
		18 & -  & 19 & 0 & 0 & 1 & 0 & 2 & 1 & 0  & -  & 4  & 3 & -  & 4  & 3 \\
		19 & -  & -  & 1 & 0 & 0 & 1 & 2 & 2 & 4  & -  & -  & 4 & -  & -  & 4 \\
		-  & -  & -  & 0 & 0 & 0 & 2 & 2 & 2 & -  & -  & -  & - & -  & -  & - \\
		\hline
	\end{tabular}
	\caption{Minimization of Suffix Trie for string $T=ababbab$.}
\end{table}

The algorithm ends when either each state is in its own group -- the automaton in already minimal or if two consecutive iterations $g_i$ and $g_{i+1}$ split the states the same way.

States which end up in the same group are equivalent:
\begin{itemize}
	\item $2 \Leftrightarrow 7 \Leftrightarrow 12 \Leftrightarrow 17$,
	\item $3 \Leftrightarrow 8 \Leftrightarrow 13 \Leftrightarrow 18$,
	\item $4 \Leftrightarrow 9 \Leftrightarrow 14 \Leftrightarrow 19$.
\end{itemize}

The equivalent states can be merged together and minimal automaton can be constructed.

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto]
	\tikzstyle{every state} = [circle,draw,minimum size=0.4cm]
	\node[initial,state]	(a)					{$0$};
	\node[state]			(b) [right of=a]	{$10$};
	\node[state,accepting]	(c) [right of=b]	{$11$};
	\node[state]			(d) [right of=c]	{$15$};
	\node[state]			(e) [right of=d]	{$16$};
	\node[state]			(f) [right of=e]	{$2$};
	\node[state]			(g) [right of=f]	{$3$};
	\node[state,accepting]	(h) [right of=g]	{$4$};
	\node[state,accepting]			(i) [below of=c]	{$1$};
	\node[state]			(j) [below of=d]	{$5$};
	\node[state,accepting]	(k) [below of=e]	{$6$};
	\path	(a) edge node {$a$} (b)
				edge [below] node {$b$} (i)
			(b) edge node {$b$} (c)
			(c) edge node {$a$} (d)
				edge [bend left] node {$b$} (f)
			(d) edge node {$b$} (e)
			(e) edge node {$b$} (f)
			(f) edge node {$a$} (g)
			(g) edge node {$b$} (h)
			(i) edge [below] node {$b$} (f)
				edge [below] node {$a$} (j)
			(j) edge [below] node {$b$} (k)
			(k) edge [below] node {$b$} (h);
\end{tikzpicture}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage{tikz}
\usepackage{wrapfig}
\usepackage[margin=15mm]{geometry}

\newcommand{\U}{\mathcal{U}}
\renewcommand{\l}{\lambda}
\newcommand{\rec}{\text{rec}}

\begin{document}
\begin{center}
\subsection*{Notes on W-types}
\end{center}

Heman Gandhi
\hfill
April 2020\\

\subsubsection*{Lists}

From 5.1, we says lists are:
\begin{itemize}
\item $nil: List(A)$
\item $cons: A \to List(A) \to List(A)$
\end{itemize}

With W-types, this is simplified to $List(A) :\equiv W_{x: 1 + A}\ \rec_{1 + A}(\U, 0, \l a. 1, x)$.
This means that lists are either a null-ary empty thing or something that takes one (1)
argument. This seems at odds with ``cons'' above, but is reconciled by ``sup'':
$cons(a, l) = sup(inr(a), \l \star. l)$.

If we have a list $<a, b, c>: List(A)$, we can write:
\[
sup(inr(a), \l \star. sup(inr(b), \l \star. sup(inr(c), \l \star. sup(inl(\star), \l x. empty))))
\]
which is suitably ugly and where $empty$ is the 0-induction to get us the nil at the end of the list.

\subsubsection*{Binary Trees}

With bullet-points, we'd probably write:
\begin{itemize}
\item $nil: BinTree(A)$
\item $node: A \to BinTree(A) \to BinTree(A) \to BinTree(A)$
\end{itemize}

With W-types, we'd get something very similar to lists:
\[
BinTree(A) :\equiv W_{a: 1 + A}\ \rec_{1 + A}(\U, 0, 2, a)
\]

Where the sort of ``cons''-ing operator would combine two trees under a root with:
\begin{equation}
\begin{split}
cons&'(a, l, r) = sup(inr(a), C)\\
&where\ C\ 0_2 = l;\ C\ 1_2 = r
\end{split}
\end{equation}

(Writing out an actual tree is hecking ugly.)

\subsubsection*{Proofs over W-types}

(I don't think this bit actually matters -- will just grab from the book.)

\subsubsection*{All Proofs of the Same Thing Over a W-type are the Same}

``Proofs of the same thing'' means that $g, h: \prod_{w: W_{x: A} B(X)} E(w)$
and where $t = g, h$ have $G_t: \prod_{a, f} t(sup(a, f)) = e(a, f, \l b. t(f(b)))$.

In order to prove this, we consider our own dependent type over our W-type. We put
$D: \prod_{w: W_{x: A} B(x)} g(w) = h(w)$ which would prove $g = h$ by function extensionality.
In order to prove this, by induction on W-types, we need only inhabit
\[
d: \prod_{a: A} \prod_{f: B(a) \to W_{x: A} B(x)} \prod_{g': \prod_{b: B(a)} D(f(b))} D(sup(a, f))
\]
To do so, consider $e$ above. $g'$ gives us that $g(f(b)) = h(f(b))$ and then function
extensionality makes $p: \l b. g(f(b)) = \l b. h(f(b))$. $ap_{e(a, f)}(p)$ gives us that
$e(a, f, \l b. g(f(b))) = e(a, f, \l b. h(f(b)))$ and by the given $G_t$, we get
$g(sup(a, f)) = h(sup(a, f))$, which gives us $d$ as desired.

\end{document}


\section{\module{cmath} ---
         Mathematical functions for complex numbers}

\declaremodule{builtin}{cmath}
\modulesynopsis{Mathematical functions for complex numbers.}

This module is always available.  It provides access to mathematical
functions for complex numbers.  The functions are:

\begin{funcdesc}{acos}{x}
Return the arc cosine of \var{x}.
There are two branch cuts:
One extends right from 1 along the real axis to \infinity, continuous
from below.
The other extends left from -1 along the real axis to -\infinity,
continuous from above.
\end{funcdesc}

\begin{funcdesc}{acosh}{x}
Return the hyperbolic arc cosine of \var{x}.
There is one branch cut, extending left from 1 along the real axis
to -\infinity, continuous from above.
\end{funcdesc}

\begin{funcdesc}{asin}{x}
Return the arc sine of \var{x}.
This has the same branch cuts as \function{acos()}.
\end{funcdesc}

\begin{funcdesc}{asinh}{x}
Return the hyperbolic arc sine of \var{x}.
There are two branch cuts, extending left from \plusminus\code{1j} to
\plusminus-\infinity\code{j}, both continuous from above.
These branch cuts should be considered a bug to be corrected in a
future release.
The correct branch cuts should extend along the imaginary axis,
one from \code{1j} up to \infinity\code{j} and continuous from the
right, and one from -\code{1j} down to -\infinity\code{j} and
continuous from the left.
\end{funcdesc}

\begin{funcdesc}{atan}{x}
Return the arc tangent of \var{x}.
There are two branch cuts:
One extends from \code{1j} along the imaginary axis to
\infinity\code{j}, continuous from the left.
The other extends from -\code{1j} along the imaginary axis to
-\infinity\code{j}, continuous from the left.
(This should probably be changed so the upper cut becomes continuous
from the other side.)
\end{funcdesc}

\begin{funcdesc}{atanh}{x}
Return the hyperbolic arc tangent of \var{x}.
There are two branch cuts:
One extends from 1 along the real axis to \infinity, continuous
from above.
The other extends from -1 along the real axis to -\infinity,
continuous from above.
(This should probably be changed so the right cut becomes continuous from
the other side.)
\end{funcdesc}

\begin{funcdesc}{cos}{x}
Return the cosine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{cosh}{x}
Return the hyperbolic cosine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{exp}{x}
Return the exponential value \code{e**\var{x}}.
\end{funcdesc}

\begin{funcdesc}{log}{x}
Return the natural logarithm of \var{x}.
There is one branch cut, from 0 along the negative real axis to
-\infinity, continuous from above.
\end{funcdesc}

\begin{funcdesc}{log10}{x}
Return the base-10 logarithm of \var{x}.
This has the same branch cut as \function{log()}.
\end{funcdesc}

\begin{funcdesc}{sin}{x}
Return the sine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{sinh}{x}
Return the hyperbolic sine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{sqrt}{x}
Return the square root of \var{x}.
This has the same branch cut as \function{log()}.
\end{funcdesc}

\begin{funcdesc}{tan}{x}
Return the tangent of \var{x}.
\end{funcdesc}

\begin{funcdesc}{tanh}{x}
Return the hyperbolic tangent of \var{x}.
\end{funcdesc}

The module also defines two mathematical constants:

\begin{datadesc}{pi}
The mathematical constant \emph{pi}, as a real.
\end{datadesc}

\begin{datadesc}{e}
The mathematical constant \emph{e}, as a real.
\end{datadesc}

Note that the selection of functions is similar, but not identical, to
that in module \refmodule{math}\refbimodindex{math}.  The reason for having
two modules is that some users aren't interested in complex numbers,
and perhaps don't even know what they are.  They would rather have
\code{math.sqrt(-1)} raise an exception than return a complex number.
Also note that the functions defined in \module{cmath} always return a
complex number, even if the answer can be expressed as a real number
(in which case the complex number has an imaginary part of zero).

A note on branch cuts: They are curves along which the given function
fails to be continuous.  They are a necessary feature of many complex
functions.  It is assumed that if you need to compute with complex
functions, you will understand about branch cuts.  Consult almost any
(not too elementary) book on complex variables for enlightenment.  For
information of the proper choice of branch cuts for numerical
purposes, a good reference should be the following:

\begin{seealso}
  \seetext{Kahan, W:  Branch cuts for complex elementary functions;
           or, Much ado about nothings's sign bit.  In Iserles, A.,
           and Powell, M. (eds.), \citetitle{The state of the art in
           numerical analysis}. Clarendon Press (1987) pp165-211.}
\end{seealso}