\documentclass[a4,compress]{beamer}

%!TeX spellcheck = en-GB

% chktex-file 18

\usepackage{beamerthemesplit}

\usepackage{amsmath, amsfonts, amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{physics}

\usepackage{graphicx}
\graphicspath{{../Manuscript/Title/img/} {../External_plots/} {../Output/} {../Statistics/}}

\usepackage[utf8]{inputenc}

\usepackage{times}
\usepackage[T1]{fontenc}

\usepackage[english]{isodate}

\newcommand{\ee}{\operatorname{e}}          % Euler's number
\newcommand{\ii}{\mathrm{i}}                % imaginary unit
\newcommand{\ad}[1]{a_{#1}^{\dagger}}       % creation operator
\newcommand*\mean[1]{\overline{#1}}         % mean

\mode<presentation>
{
  \usetheme{Warsaw}
  \setbeamercovered{transparent}
}

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

\addtobeamertemplate{navigation symbols}{}{%
    \usebeamerfont{footline}%
    \usebeamercolor[fg]{footline}%
    \hspace{1em}%
    \insertframenumber/\inserttotalframenumber
}

\addtobeamertemplate{title page}{\includegraphics[width=1.3cm]{unibuc} \hfill \includegraphics[width=1.6cm]{fizica}}{}

\title[Fingerprints of classical structures in quantum chaos]{Fingerprints of classical phase space structure in quantum chaos}
\subtitle{Bachelor Thesis}

\author{
  Sebastian MICLUȚA-CÂMPEANU \\
  \hspace{6cm} {\small	Scientific Advisers:} \\
  \hspace{6.35cm} {\small Prof.~dr.~Virgil BĂRAN} \\
  \hspace{6.35cm} {\small Lect.~dr.~Roxana ZUS}
}

\institute[Universitatea din București]
{
  Universitatea din București \\
  Facultatea de Fizică
}
\date{\small Bucharest, 29 \printdayoff\today}

\begin{document}

%%%%%%%%%%%%%%%%%%%% slide 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[plain]
 \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Outline}
  \tableofcontents[]%pausesections]
\end{frame}

\section[Intro]{Introduction}

%%%%%%%%%%%%%%%%%%%% slide 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The aim of this thesis is to study the correspondence
    between the classical chaotic behaviour of nuclear surface vibrations
    and the quantum features of energy spectra of same system.
  \end{itemize}
\end{frame}

\section[Chaos]{Chaotic dynamics}

\subsection{Classical Chaos}

%%%%%%%%%%%%%%%%%%%% slide 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% \subsection{Fundamental notions}

\begin{frame}
  \begin{itemize}
    \item Hamilton's equations
    \begin{align*}
      \dot{q_k} = \pdv{\mathcal{H}}{p_k}, &&
      \dot{p_k} = -\pdv{\mathcal{H}}{q_k}
    \end{align*}
    \item Action-angle variables
    \begin{align*}
      I_k = \frac{1}{2\pi}\oint p_k \dd{q_k}, &&
      \theta_k = \omega_k(I_k) \Delta t
    \end{align*}
    \item Integrability: A system is \emph{integrable} if the number of degrees
    of freedom is equal to the number of constants of the motion
    (in involution which each other).
    \item For integrable systems with $n$ degrees of freedom, the trajectory
    in phase space is constrained to a $n$-torus.
  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{K.A.M. Theorem}
  \begin{itemize}
    \item If the motion of a system described by an integrable
    Hamiltonian is perturbed with a small non-integrable term and
    the frequencies (\(\omega_k = \dot{\theta}_k\)) of the unperturbed motion
    are such that for \(k=1,\dotsc,n\), we have
    \(m_k\, \omega_k \neq 0, \forall\ m_k \in \mathbb{Z}\),
    then the motion will remain on the $n$-torus, except for a small
    set of initial conditions, for which the trajectories escape on the
    energy hyper-surface.

  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Indicators of chaos}
  \begin{columns}[c]
  \begin{column}{0.6\textwidth}
    \begin{itemize}
      \item Poincaré sections: Slices of the phase space which reduce the
      dimensionality by one.
      \item Liapunov exponent
      \[
        \lambda = \lim\limits_{t \to \infty} \frac{1}{t} \ln{\frac{\dd{x}(t)}{\dd{x}(0)}}
      \]
      \item Chaos is characterised by sensitivity to the initial conditions
      (\(\lambda > 0\)).
    \end{itemize}
  \end{column}
  \begin{column}{0.4\textwidth}
    \includegraphics[width=0.9\textwidth]{ponicare-sections-e_120}
  \end{column}
  \end{columns}

\end{frame}

\subsection{Quantum Chaos}

%%%%%%%%%%%%%%%%%%%% slide 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item In Quantum Mechanics we can no longer speak of trajectories
    or sensitivity to the initial conditions.
    \item We need a way to compare spectra from different systems
    in order to search for common patterns or features.
    \item We can use the level spacings (\(E_{n+1} - E_n\)) to build
    the nearest neighbour level spacing distributions.
  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Giving meaning to quantum chaos}
  \begin{itemize}
    \item Berry-Tabor conjecture: The quantum counterpart of a classically integrable
    system has a Poissonian nearest neighbour distribution.
    \[
      P_P(s) = \ee^{-s}
    \]
    \item Bohigas-Gianoni-Schmit conjecture: The nearest neighbour distribution
    of a quantum system with a classically chaotic counterpart is given by
    the Wigner distribution.
    \[
      P_W(s) = \frac{\pi}{2} s \exp(-\frac{\pi}{4} s^2)
    \]
  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Other measures}
  \begin{itemize}
    \item Cumulative spacing distribution
    \[
      I(s) = \int_0^s P(x) \dd{x}
    \]
  \end{itemize}
\end{frame}

\section[Model]{Surface vibrations}

%%%%%%%%%%%%%%%%%%%% slide 10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The radius of the nuclear surface
    \[
      R(\theta, \varphi) = R_0 \left( 1 + \sum_{\lambda, \mu}
          \alpha_{\lambda,\mu}^* Y_{\lambda,\mu}(\theta, \varphi) \right)
    \]
    \item For \(\lambda = 2\) we describe the surface in terms of
    quadrupole vibrations.
    \item \(\alpha_{\lambda,\mu}\) become dynamical degrees of freedom
  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item We want to study only the vibrations of the surface, so we will
    use the intrinsic reference frame of the nucleus. Thus we will only have
    2 degrees of freedom.
    \item The Hamiltonian of the system will be given by
    \[
    \begin{split}
      \mathcal{H}(q_0,p_0,q_2,p_2) &= \frac{A}{2}(p_0^2 + p_2^2 + q_0^2 + q_2^2) \\
      &+ \frac{B}{\sqrt{2}}q_0(3q_2^2 - q_0^2)
      + \frac{D}{4}{(q_0^2 + q_2^2)}^2
    \end{split}
    \]
  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item Quantised Hamiltonian
    \footnotesize
    \[
    \begin{split}
      &H = A \left( \ad{1} a_1 + \ad{2} a_2 \right)
        + \frac{B}{4} \bigg[ \left( 3 \ad{1} {\ad{2}}^2 + 3 a_1 a_2^2
                                   - {\ad{1}}^3 - a_1^3 \right)   \\
      & + 3 \left( a_1 {\ad{2}}^2 + \ad{1} a_2^2 - \ad{1} a_1^2 - {\ad{1}}^2 a_1
                 + 2 a_1 \ad{2} a_2 + 2 \ad{1} \ad{2} a_2
              \right) \bigg]  \\
      & + \frac{D}{16} \bigg[ 6 \left( {\ad{1}}^2 a_1^2 + {\ad{2}}^2 a_2^2 \right)
                            + 2 \left( a_1^2 {\ad{2}}^2 + {\ad{1}}^2 a_2^2 \right)
                            + 8 \ad{1} a_1 \ad{2} a_2  \\
      & + 4 \left(\ad{1} a_1^3 + {\ad{1}}^3 a_1 + \ad{2} a_2^3 + {\ad{2}}^3 a_2
         + a_1^2 \ad{2} a_2 + {\ad{1}}^2 \ad{2} a_2 + \ad{1} a_1 a_2^2 + \ad{1} a_1 {\ad{2}}^2
            \right)  \\
      & + \left( {\ad{1}}^4 + a_1^4 + {\ad{2}}^4 + a_2^4
         + 2 {\ad{1}}^2 {\ad{2}}^2 + 2 a_1^2 a_2^2
          \right)
                            \bigg]
    \end{split}
    \]
  \end{itemize}
\end{frame}

\section{Procedure}
%%%%%%%%%%%%%%%%%%%% slide 13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{columns}[c]
    \begin{column}{0.4\textwidth}
      \begin{itemize}
        \item Computing the Hamiltonian
        \item Diagonalisation
        \item Stability
        \item Separating the irreducible representations
        \item Statistics
      \end{itemize}
    \end{column}
    \begin{column}{0.6\textwidth}
      \includegraphics[scale=0.22]{largest_matrix_m}
      \includegraphics[scale=0.22]{all_matrices_m}

      \includegraphics[scale=0.22]{largest_matrix_b}
      \includegraphics[scale=0.22]{all_matrices_b}
    \end{column}
  \end{columns}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Stability}
  \centering
  \includegraphics[scale=0.6]{"B0.55 D0.4 N260"/bar_E_diff}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Separating the irreducible representations}
  \includegraphics[width=\textwidth]{"B0.55 D0.4 N260"/bar_delta}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{The nearest neighbour distribution}
  \begin{columns}[c]
  \begin{column}{0.3\textwidth}
    \(P(s)\) for \(B=0.55\), \({D=0.4, N=260}\) compared with the Poisson
    and Wigner distributions both in continuous and discrete forms.
  \end{column}
  \begin{column}{0.7\textwidth}
    \includegraphics[scale=0.5]{"B0.55 D0.4 N260"/P(s)_st_1e-09_eps_1e-08}  % chktex 36
  \end{column}
  \end{columns}
\end{frame}

\section{Results}

%%%%%%%%%%%%%%%%%%%% slide 17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The nearest neighbour distribution as a linear superposition
    of the Poisson and Wigner distributions
    \[
      P(s) = \alpha P_P(s) + (1-\alpha) P_W(s)
    \]
  \end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \includegraphics[width=\textwidth]{"B0.55 D0.4 N260"/P(s)_fit_1e-09_eps_1e-08}  % chktex 36
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{columns}[c]
  \begin{column}{0.35\textwidth}
    \includegraphics[height=0.45\textheight]{ponicare-sections-e_30}

    \includegraphics[height=0.45\textheight]{ponicare-sections-e_240}
  \end{column}
  \begin{column}{0.15\textwidth}
    \small \centering \(E = 30A\) \\
    \vspace{3cm}
    \(E = 240A\)
  \end{column}
  \begin{column}{0.35\textwidth}
    \includegraphics[height=0.45\textheight]{ponicare-sections-e_120}

    \includegraphics[height=0.45\textheight]{ponicare-sections-e_400}
  \end{column}
  \begin{column}{0.15\textwidth}
    \small \(E = 120A\) \\
    \vspace{3cm}
    \(E = 400A\)
  \end{column}
  \end{columns}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{columns}[c]
  \begin{column}{0.35\textwidth}
    \includegraphics[height=0.45\textheight]{ponicare-sections-e_240}

    \includegraphics[height=0.45\textheight]{ponicare-sections-e_600}
  \end{column}
  \begin{column}{0.15\textwidth}
    \small \centering \(E = 240A\) \\
    \vspace{3cm}
    \(E = 600A\)
  \end{column}
  \begin{column}{0.33\textwidth}
    \includegraphics[height=0.45\textheight]{ponicare-sections-e_400}

    \includegraphics[height=0.45\textheight]{ponicare-sections-e_1000}
  \end{column}
  \begin{column}{0.17\textwidth}
    \small \centering \(E = 400A\) \\
    \vspace{3cm}
    \(E = 1000A\)
  \end{column}
  \end{columns}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The global phase space structure is reflected in the plot of
    \(\alpha \) as a function of \(\Delta E\)
  \end{itemize}
  \begin{columns}[c]
  \begin{column}{0.5\textwidth}
    \includegraphics[width=\textwidth]{{"alpha_e_B[0.55, 0.6, 0.63]"_N[260]}.pdf}  % chktex 36
  \end{column}
  \begin{column}{0.5\textwidth}
    \includegraphics[width=\textwidth]{liapunov}
  \end{column}
  \end{columns}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The nontrivial dependence of \(\alpha \) on the non-integrability
    parameter \(B\) for \(N=220, 240, 260\)
  \end{itemize}
  \includegraphics[width=0.9\textwidth]{"alpha_N[220, 240, 260]".pdf}  % chktex 36
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The qualitative stability of the shape of \(\alpha(B)\) when
    considering \(N=260\) and different values for the energy interval
  \end{itemize}
  \begin{columns}[c]
  \begin{column}{0.5\textwidth}
    \includegraphics[width=\textwidth]{{alpha_N[260]_max_e_"[100.0, 120.0, 140.0]"}.pdf}  % chktex 36
  \end{column}
  \begin{column}{0.5\textwidth}
    \includegraphics[width=\textwidth]{{alpha_N[260]_max_e_"[140.0, 160.0, 0.0]"}.pdf}  % chktex 36
  \end{column}
  \end{columns}
\end{frame}

%%%%%%%%%%%%%%%%%%%% slide 24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \begin{itemize}
    \item The qualitative stability of the shape of \(\alpha(B)\) when
    considering \(N=220, 240, 260\) and different values for the energy interval
  \end{itemize}
  \begin{columns}[c]
  \begin{column}{0.5\textwidth}
    \includegraphics[width=\textwidth]{{"alpha_N[220, 240, 260]"_max_e_[100.0]}.pdf}  % chktex 36
  \end{column}
  \begin{column}{0.5\textwidth}
    \includegraphics[width=\textwidth]{{"alpha_N[220, 240, 260]"_max_e_[140.0]}.pdf}  % chktex 36
  \end{column}
  \end{columns}
\end{frame}

\section{Conclusions}

%%%%%%%%%%%%%%%%%%%% slide 25 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Conclusions}
  \begin{itemize}
    \item We emphasised a correlation between the global phase space
    structure of the classical system and its quantum corespondent.
    \item We proposed a description of the nearest neighbour distribution of
    our spectra through a \emph{superposition of Poisson and Wigner distributions}.
    \item A non-trivial dependence of the superposition coefficient on the
    non-integrability parameter was revealed.
    \item Through our analysis we showed a correlation between the tori volume
    in classical phase space and the deviation from the Wigner distribution
    in the quantum system.
  \end{itemize}
\end{frame}

\end{document}


\documentclass[11pt]{article}

\usepackage{common}
\title{Practical 0: Sample Assignment}
\author{Alexander Rush \\ srush@seas.harvard.edu }
\begin{document}

\maketitle{}

\noindent This is the template you should use for submitting your practical assignments.
A full-credit assignment will go through each of these points and provide a thoughtful
clear answer.  Note that the limit for the report is 4 pages, please prioritize quality over
quantity in your submission.

\section{Technical Approach}

How did you tackle the problem? Credit will be given for:

  \begin{itemize}
  \item Diving deeply into a method (rather than just trying
    off-the-shelf tools with default settings). This can mean
    providing mathematical descriptions or pseudo-code.
  \item Making tuning and configuration decisions using thoughtful experimentation.
    This can mean carefully describing features added or hyperparameters tuned.
  \item Exploring several methods. This can contrasting two approaches
    or perhaps going beyond those we discussed in class.
  \end{itemize}

  \noindent Thoughtfully iterating on approaches is key.
  If you used existing packages or referred to papers or blogs for ideas,
  you should cite these in your report.

  \begin{table}
    \centering
    \begin{tabular}{@{}lll@{}}
      \toprule
      &\multicolumn{2}{c}{Mention Features  } \\
      & Feature & Value Set\\
      \midrule
      & Mention Head & $\mcV$ \\
      & Mention First Word & $\mcV$ \\
      & Mention Last Word & $\mcV$ \\
      & Word Preceding Mention & $\mcV$ \\
      & Word Following Mention & $\mcV$\\
      & \# Words in Mention & $\{1, 2, \ldots \}$ \\
      & Mention Type & $\mathcal{T}$ \\
      \bottomrule

    \end{tabular}
    \caption{Feature lists are a good way of illustrating problem specific tuning.}
  \end{table}

\section{Results}
This section should report on the following questions:

\begin{itemize}
\item Did you create and submit a set of
  predictions?

\item  Did your methods give reasonable performance?
\end{itemize}

\noindent You must have \textit{at least one plot or table}
that details the performances of different methods tried.
Credit will be given for quantitatively reporting (with clearly
labeled and captioned figures and/or tables) on the performance of the
methods you tried compared to your baselines.

\begin{table}
\centering
\begin{tabular}{llr}
 \toprule
 Model &  & Acc. \\
 \midrule
 \textsc{Baseline 1} & & 0.45\\
 \textsc{Baseline 2} & & 2.59 \\
 \textsc{Model 1} & & 10.59  \\
 \textsc{Model 2} & &13.42 \\
 \textsc{Model 3} & & 7.49\\
 \bottomrule
\end{tabular}
\caption{\label{tab:results} Result tables can compactly illustrate absolute performance, but a plot may be more effective at illustrating a trend.}
\end{table}

\section{Discussion}

End your report by discussing the thought process behind your
analysis. This section does not need to be as technical as the others
but should summarize why you took the approach that your did. Credit will be given for:

  \begin{itemize}
  \item Explaining the your reasoning for why you seqentially chose to
    try the approaches you did (i.e. what was it about your initial
    approach that made you try the next change?).
  \item Explaining the results.  Did the adaptations you tried improve
    the results?  Why or why not?  Did you do additional tests to
    determine if your reasoning was correct?
  \end{itemize}

\end{document}


\subsection{Related work}

Variance of stock returns plays an important role in many fields of finance.
For example, it is crucial in capital asset pricing, portfolio management, and contingent claim pricing~\citep{hull1987pricing,skiadas2009asset,chow2014study}.
\citet{Christie1982} wrote in 1982 that even though volatility was pivotal in these topics, little work was being done for understanding its properties.

\subsubsection*{GARCH models}

Then, already in 1982,~\citet{Engle1982} introduced the Autoregressive Conditional Heteroskestacity (ARCH) model that assumes non-constant variance conditional on past errors.
The ARCH model was later extended in several different ways\footnote{For a more extensive overview, see~\citet{bollerslev1994arch}.}: generalised ARCH (GARCH) models variance with higher flexibility, integrated GARCH was designed for non-stationary volatility processes, and continuous GARCH for continuous time modelling, to mention some of the popular ones~\citep{Bollerslev1986,engle1986modelling,box1994time,kluppelberg2004continuous}.

\citet{Nelson1991} introduced the first variant featuring asymmetric volatility, the exponential GARCH.
It models the logarithm of the variance by a suitable function of past shocks.
Since then, quite a few versions have been developed targeting asymmetric volatility including Family GARCH, which is an omnibus nesting a variety of the earlier ones~\citep{engle1993measuring,glosten1993relation,zakoian1994threshold,sentana1995quadratic,hentschel1995all}.

\subsubsection*{Stochastic Volatility models}

Still in 1982, the Stochastic Volatility (SV) model was presented in~\citet{Taylor1982}, which has an unconditionally random variance process.
Later, \citet{Harvey1996} and \citet{Jacquier2004} proposed two different formulations for the asymmetric SV model, both containing a correlation parameter that estimates the leverage effect, but they correlate different terms.
~\citet{yu2005leverage} compared the two setups and concluded that the earlier one is appealing both in theory and in practice.

The SV model is a promising alternative to GARCH-type models, in the literature SV's in-sample-fit is at least as good as a GARCH model with heavy-tailed errors.
SV's main drawback is the lack of easily accessible and comprehensible estimation methods, compared to the less involved approaches for GARCH.
Therefore, a lot of effort has been put into the development of efficient algorithms giving good fit for SV~\citep{Kim1998,jacquier2002bayesian,Omori2007,Kastner2014,Chan2016}.

\subsubsection*{The leverage effect}

Even before~\citet{black1976studies} first described the leverage effect, the original~\citet{black1973pricing} paper already discussed the impact of changing LR on stock price behaviour.
Their reasoning was based on the popular structural model in corporate finance from~\citet{modigliani1958cost}.
A short calculation based on Chapter 14 of~\citet{berk2007corporate} demonstrates the phenomenon.
\begin{description}
	\item[Example 1.1. Leverage effect in Modiglinani--Miller.]~

	The Modigliani--Miller (MM) world is defined by the Modigliani--Miller theorem stating that if there are no taxes, bankruptcy, or agency costs, and every agent has the same information, then in an efficient market the capital structure of the firm does not affect its value~\citep{modigliani1958cost}.
	The firm's capital structure is the amount of debt and equity.

	By definition, $\text{Leverage ratio}=\frac{\text{Market equity}}{\text{Debt}}$. In our example, there is one firm in two consecutive years with the same debt but an increased LR, and one type of investment project in both years that has two outcomes depending on the strength of the economy that year.
	Debt is risk-free and so has the same return (5\%) in both states.
	The project has a net value of \$$-100$ or \$$300$ in the weak and the strong economies, respectively.
	Income that does not go to debt holders goes to equity holders.

	Table~\ref{tab:modigl} contains the details of the calculation. Return sensitivity is a proxy for measuring volatility here, it is actually the range of the returns.

	The important conclusion is that in MM a positive return on equity from 1999 to 2000 implies decreasing volatility.
	A similar calculation about the other direction would also work, moreover, this conclusion holds in general in MM and even more general setups~\citep{Christie1982}.
\end{description}

\begin{table}[]
	\fontsize{9}{12}\selectfont
	\centering
	\begin{tabular}{lcccccc}
		\multirow{2}{*}{} & \textbf{Date 0} & \multicolumn{2}{c}{\textbf{Date 1: Cash Flows}} & \multicolumn{2}{c}{\textbf{Date 1: Returns}} & \\
		& \begin{tabular}[c]{@{}c@{}}Initial\\ Value\end{tabular} & \begin{tabular}[c]{@{}c@{}}Strong\\ Economy\end{tabular} & \begin{tabular}[c]{@{}c@{}}Weak\\ Economy\end{tabular} & \begin{tabular}[c]{@{}c@{}}Strong\\ Economy\end{tabular} & \begin{tabular}[c]{@{}c@{}}Weak\\ Economy\end{tabular} & \begin{tabular}[c]{@{}c@{}}Return\\ Sensitivity\end{tabular} \\ \hline
		\multicolumn{1}{l|}{Debt} & \multicolumn{1}{c|}{\$500} & \$525 & \$525 & 5\% & \multicolumn{1}{c|}{5\%} & 0\% \\ \hline
		\multicolumn{1}{l|}{Equity in 1999} & \multicolumn{1}{c|}{\$500} & \$775 & \$375 & 55\% & \multicolumn{1}{c|}{-25\%} & 80\% \\ \hline
		\multicolumn{1}{l|}{Equity in 2000} & \multicolumn{1}{c|}{\$1000} & \$1275 & \$875 & 27.5\% & \multicolumn{1}{c|}{-12.5\%} & 40\%
	\end{tabular}
	\caption{Calculations for Example 1.1.}
	\label{tab:modigl}
\end{table}
\normalsize

The first statistical tests about the leverage effect were probably done in~\citet{Christie1982}.
There, the price $S$ is modelled as a diffusion process, and the instantaneous volatility is $\lambda S^\theta$, with $\lambda>0$ and $\theta$ being model parameters.
Thus, a negative $\theta$ means that if $S$ is decreasing then the volatility is increasing, which is the leverage effect.
Evidence was found for the existence of the leverage effect, and also for its strong relationship with LR, on a dataset of 379 firms.

However, a universally accepted explanation still does not exist.
The relationship with LR, and thus the LR hypothesis as well, was questioned by~\citet{figlewski2000leverage}.
They fitted a linear regression model on the changes in volatility against the changes in LR, and they found inconsistencies on different LR levels.

Different explanations have also been considered. \citet{dennis2006stock} showed that the return-volatility relation is not a firm phenomenon but a market one.
That inspired \citet{Hibbert2008}, who then proposed a trader-behavioural explanation.
Their lagged linear models, based on VIX, intraday returns and realised volatility estimations, showed problems with the LR hypothesis and resulted in consistence with their behavioural model.
\citet{bollerslev2011volatility} aims at matching the variance's autocorrelation structure using their equilibrium-based, continuous time model, and at the same time they predict the leverage effect as well.

Recent empirical studies have shown interesting properties of the leverage effect.
Based on intraday data and the retarded volatility model~\citep{Bouchaud2001}, positive return-variance correlation was found in Chinese indices by~\citet{Shen2009}, contradicting experience from other parts of the world.
Later, \citet{Christensen2015} were probably the first ones to check the time-dependence of the leverage effect.
Using a GARCH-type model, they found that the effect is significantly stronger throughout crises in the S\&P500 index.

We do not know the roots of the leverage effect.
Since we do not want to filter out any of the possibilities, we will base our research in this master thesis on stock prices and not on indices.
The reason is that the LR hypothesis is only interpretable for single firms and their stocks, and the market-based and behavioural hypotheses are interpretable for both stocks and indices.
Finally, based on the comparison of GARCH and SV models above, since efficient algorithms are already available, the latter is preferred due to the better in-sample-fit.


\documentclass[color=usenames,dvipsnames]{beamer}\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

\definecolor{fgcolor}{rgb}{0, 0, 0}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.69,0.494,0}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.749,0.012,0.012}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.514,0.506,0.514}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0,0.341,0.682}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.004,0.004,0.506}{#1}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
%\documentclass[color=usenames,dvipsnames,handout]{beamer}

\usepackage[sans]{../../lab1}
\usepackage{bm}

\hypersetup{pdftex,pdfstartview=FitV}

%% New command for inline code that isn't to be evaluated
\definecolor{inlinecolor}{rgb}{0.878, 0.918, 0.933}
\newcommand{\inr}[1]{\colorbox{inlinecolor}{\texttt{#1}}}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

\begin{frame}[plain]
  \centering \huge
  Linear mixed-effects models \\
  \vspace{1cm}
  \Large
  November 7, 2018 \\
  FANR 6750 \\
  \vfill
  \large
  Richard Chandler and Bob Cooper
\end{frame}

\section{Introduction}

\begin{frame}
  \frametitle{Fixed vs random effects, revisited}
%  \large
  {\bf In the context of mixed-effects models\dots \par}
  \vspace{0.5cm}
{\bf Fixed effect \par}
A parameter that would not change if you repeated your study

\pause
\vspace{1cm}

{\bf Random effect \par}
A parameter that would change if you repeated your study

\pause
\vfill
{\centering \bf
\alert{Random effects will always be associated with a probability
distribution} \par}
\end{frame}

\begin{frame}
  \frametitle{Previous examples}
  \Large
%  \begin{itemize}
%    \item
  ANOVA with random block effects
  \pause
  \vfill
%    \item[]
%    \item
  Nested ANOVA
  \pause
  \vfill
%    \item[]
%    \item
  Split-plot ANOVA
%    \item[]
%    \item Repeated measures ANOVA
%  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{A simple example}
  \large
  \[
    y_{ij} = \beta_0 + \beta_1 x_{i1} + \gamma_j + \varepsilon_{ij}
  \]
  where
  \[
    \gamma_j \sim \mbox{Normal}(0, \sigma^2_d)
  \]
  and
  \[
    \varepsilon_{ij} \sim \mbox{Normal}(0, \sigma^2)
  \]
  \pause
  \vfill
  \centering
  This amounts to a linear regression with a random intercept \\
\end{frame}

\section{Repeated measures with serial correlation}

%% \begin{frame}
%%   \frametitle{Split-plot model}
%%   The expanded additive model:
%%   \[
%%     y_{ijk} = \beta0 + \beta_1 + \beta_{ij} + \gamma_k +
%%     \alpha\gamma_{ik} + \varphi y_{ijk-1} + \varepsilon_{ijk}
%%   \]
%% \end{frame}

\begin{frame}
  \centering \Huge
  \color{PineGreen}{Mixed effects approach to repeated measures} \par
\end{frame}

\begin{frame}[fragile]
  \frametitle{Repeated measures revisited}
  {\bf Recall the plant data}
  \small
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plantData} \hlkwb{<-} \hlkwd{read.csv}\hlstd{(}\hlstr{"plantData.csv"}\hlstd{)}
\hlstd{plantData}\hlopt{$}\hlstd{week} \hlkwb{<-} \hlkwd{factor}\hlstd{(plantData}\hlopt{$}\hlstd{week)}
\hlkwd{head}\hlstd{(plantData)}
\end{alltt}
\begin{verbatim}
##   plant fertilizer week leaves
## 1     1          L    1      4
## 2     1          L    2      5
## 3     1          L    3      6
## 4     1          L    4      8
## 5     1          L    5     10
## 6     2          L    1      3
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
  {\bf \normalsize Standard split-plot approach}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(nlme)}
\hlstd{lme1} \hlkwb{<-} \hlkwd{lme}\hlstd{(leaves} \hlopt{~} \hlstd{fertilizer}\hlopt{*}\hlstd{week, plantData,}
            \hlkwc{random}\hlstd{=}\hlopt{~}\hlnum{1}\hlopt{|}\hlstd{plant)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{View the parameter estimates}

\small
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(lme1)}
\end{alltt}
\begin{verbatim}
## Linear mixed-effects model fit by REML
##  Data: plantData
##        AIC      BIC    logLik
##   140.9644 161.2309 -58.48219
##
## Random effects:
##  Formula: ~1 | plant
##         (Intercept) Residual
## StdDev:    1.098863     0.65
##
## Fixed effects: leaves ~ fertilizer * week
##                   Value Std.Error DF   t-value p-value
## (Intercept)         4.6 0.5709641 32  8.056549  0.0000
## fertilizerL         0.0 0.8074652  8  0.000000  1.0000
## week2               2.2 0.4110961 32  5.351547  0.0000
## week3               4.4 0.4110961 32 10.703094  0.0000
## week4               5.4 0.4110961 32 13.135615  0.0000
## week5               7.4 0.4110961 32 18.000657  0.0000
## fertilizerL:week2  -1.0 0.5813777 32 -1.720052  0.0951
## fertilizerL:week3  -1.8 0.5813777 32 -3.096094  0.0041
## fertilizerL:week4  -1.6 0.5813777 32 -2.752084  0.0097
## fertilizerL:week5  -1.4 0.5813777 32 -2.408073  0.0220
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}
  \frametitle{Matrix representation}
  \Large
  \[
    {\bf y} = {\bf X}{\bm \beta} + {\bm \gamma} + {\bm \varepsilon}
  \]
  \pause
  \[
    {\bm \gamma} \sim \mbox{Normal}(0, \sigma^2_d)
  \]
  \[
    {\bm \varepsilon} \sim \mbox{Normal}(0, \sigma^2)
  \]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Extract the estimates}
  {\bf Fixed effects (the $\bm \beta$ estimates)}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{round}\hlstd{(beta} \hlkwb{<-} \hlkwd{fixef}\hlstd{(lme1),} \hlnum{2}\hlstd{)}
\end{alltt}
\begin{verbatim}
##       (Intercept)       fertilizerL             week2             week3
##               4.6               0.0               2.2               4.4
##             week4             week5 fertilizerL:week2 fertilizerL:week3
##               5.4               7.4              -1.0              -1.8
## fertilizerL:week4 fertilizerL:week5
##              -1.6              -1.4
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\vfill
  {\bf \normalsize Random effects (the $\bm \gamma$ estimates)}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{round}\hlstd{(gamma} \hlkwb{<-} \hlkwd{ranef}\hlstd{(lme1,} \hlkwc{which}\hlstd{=}\hlnum{1}\hlstd{),} \hlnum{2}\hlstd{)}
\end{alltt}
\begin{verbatim}
##    (Intercept)
## 1        -0.67
## 2        -1.61
## 3         1.38
## 4         1.01
## 5        -0.11
## 6        -0.64
## 7        -1.01
## 8         1.23
## 9         0.11
## 10        0.30
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Computing the expected values}
  {\bf Ignoring the random effects}
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{X} \hlkwb{<-} \hlkwd{model.matrix}\hlstd{(}\hlopt{~} \hlstd{fertilizer}\hlopt{*}\hlstd{week, plantData)}
\hlstd{E1} \hlkwb{<-} \hlstd{X} \hlopt{%*%} \hlstd{beta}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
  {\bf Including the random effects}
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{nWeeks} \hlkwb{<-} \hlnum{5}
\hlstd{gamma.long} \hlkwb{<-} \hlkwd{rep}\hlstd{(gamma[[}\hlnum{1}\hlstd{]],} \hlkwc{each}\hlstd{=nWeeks)}
\hlstd{E2} \hlkwb{<-} \hlstd{E1} \hlopt{+} \hlstd{gamma.long}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
\small
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{head}\hlstd{(}\hlkwd{cbind}\hlstd{(plantData,} \hlkwc{E1}\hlstd{=E1,} \hlkwc{E2}\hlstd{=E2))}
\end{alltt}
\begin{verbatim}
##   plant fertilizer week leaves   E1       E2
## 1     1          L    1      4  4.6 3.927090
## 2     1          L    2      5  5.8 5.127090
## 3     1          L    3      6  7.2 6.527090
## 4     1          L    4      8  8.4 7.727090
## 5     1          L    5     10 10.6 9.927090
## 6     2          L    1      3  4.6 2.992492
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{The ANOVA table}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{anova}\hlstd{(lme1)}
\end{alltt}
\begin{verbatim}
##                 numDF denDF  F-value p-value
## (Intercept)         1    32 483.0495  <.0001
## fertilizer          1     8   2.6037  0.1453
## week                4    32 158.2249  <.0001
## fertilizer:week     4    32   3.0059  0.0326
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\vfill
\Large
{\centering \bf \color{PineGreen}{Are these $P$-values valid?} \par}
\pause
\vfill
{\centering \bf Not if there is serial correlation \par}
\end{frame}

\begin{frame}
  \frametitle{Auto-regressive models}
  \bf
  An AR($p$) model allows for lag effects of order $p$ \par
  \vfill
  If $p=1$, we say the value of $y$ at time $k$ depends on the value
  of $y$ at time $k-1$. \par
  \pause
  \vfill
  \Large
  \[
    y_{ijk} = \beta_0 + \varphi_1 y_{ijk-1} + \gamma_j + \varepsilon_{ijk}
  \]
\end{frame}

\begin{frame}
  \frametitle{Auto-regressive models}
  \bf
  An AR($p$) model allows for lag effects of order $p$ \par
  \vfill
  If $p=2$, we say the value of $y$ at time $k$ depends on the value
  of $y$ at time $k-1$ and at time $k-2$. \par
  \vfill
  \pause
  \Large
  \[
    y_{ijk} = \beta_0 + \varphi_1 y_{ijk-1} + \varphi_2 y_{ijk-2} + \gamma_j + \varepsilon_{ijk}
  \]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Auto-regressive models}
\vspace{0.5cm}
{\bf Assume an AR(1) correlation structure}
\pause
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{lme2} \hlkwb{<-} \hlkwd{lme}\hlstd{(leaves} \hlopt{~} \hlstd{fertilizer}\hlopt{*}\hlstd{week, plantData,}
            \hlkwc{random}\hlstd{=}\hlopt{~}\hlnum{1}\hlopt{|}\hlstd{plant,}
            \hlkwc{correlation}\hlstd{=}\hlkwd{corARMA}\hlstd{(}\hlkwc{p}\hlstd{=}\hlnum{1}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\vspace{1cm}
{\bf Assume an AR(3) correlation structure}
\pause
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{lme3} \hlkwb{<-} \hlkwd{lme}\hlstd{(leaves} \hlopt{~} \hlstd{fertilizer}\hlopt{*}\hlstd{week, plantData,}
            \hlkwc{random}\hlstd{=}\hlopt{~}\hlnum{1}\hlopt{|}\hlstd{plant,}
            \hlkwc{correlation}\hlstd{=}\hlkwd{corARMA}\hlstd{(}\hlkwc{p}\hlstd{=}\hlnum{3}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Autocorrelation function}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(}\hlkwd{ACF}\hlstd{(lme2),} \hlkwc{alpha}\hlstd{=}\hlnum{0.05}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
%\begin{center}
\centering
  \includegraphics[width=0.9\textwidth]{figure/plant2-acf-1} \\
%\end{center}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Autocorrelation function}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(}\hlkwd{ACF}\hlstd{(lme3),} \hlkwc{alpha}\hlstd{=}\hlnum{0.05}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
%\begin{center}
\centering
  \includegraphics[width=0.9\textwidth]{figure/plant3-acf-1} \\
%\end{center}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Parameter estimates}
%<<summary-lme3,size='tiny'>>=
%summary(lme3)
%@
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(lme3)}
\end{alltt}
\tiny
\begin{verbatim}
## Linear mixed-effects model fit by REML
##  Data: plantData
##        AIC      BIC    logLik
##   142.7793 168.1125 -56.38965
##
## Random effects:
##  Formula: ~1 | plant
##         (Intercept)  Residual
## StdDev:    0.955924 0.8227638
##
## Correlation Structure: ARMA(3,0)
##  Formula: ~1 | plant
##  Parameter estimate(s):
##       Phi1       Phi2       Phi3
##  0.3339878  0.5086039 -0.2573006
## Fixed effects: leaves ~ fertilizer * week
##                   Value Std.Error DF   t-value p-value
## (Intercept)         4.6 0.5640445 32  8.155385  0.0000
## fertilizerL         0.0 0.7976794  8  0.000000  1.0000
## week2               2.2 0.4039417 32  5.446331  0.0000
## week3               4.4 0.3532792 32 12.454737  0.0000
## week4               5.4 0.4867909 32 11.093060  0.0000
## week5               7.4 0.4614437 32 16.036626  0.0000
## fertilizerL:week2  -1.0 0.5712598 32 -1.750517  0.0896
## fertilizerL:week3  -1.8 0.4996123 32 -3.602794  0.0011
## fertilizerL:week4  -1.6 0.6884262 32 -2.324142  0.0266
## fertilizerL:week5  -1.4 0.6525799 32 -2.145331  0.0396
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{ANOVA tables}
  {\bf Model that ignores serial correlation}
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{anova}\hlstd{(lme1)}
\end{alltt}
\begin{verbatim}
##                 numDF denDF  F-value p-value
## (Intercept)         1    32 483.0495  <.0001
## fertilizer          1     8   2.6037  0.1453
## week                4    32 158.2249  <.0001
## fertilizer:week     4    32   3.0059  0.0326
\end{verbatim}
\end{kframe}
\end{knitrout}
  {\bf Model that accounts for serial correlation}
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{anova}\hlstd{(lme3)}
\end{alltt}
\begin{verbatim}
##                 numDF denDF  F-value p-value
## (Intercept)         1    32 502.8341  <.0001
## fertilizer          1     8   1.6959  0.2291
## week                4    32 107.5671  <.0001
## fertilizer:week     4    32   3.3171  0.0221
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\section{Summary}

\begin{frame}
  \frametitle{Summary}
  \large
%  \begin{itemize}[<+->]
%    \item
  Linear mixed-effects models are useful anytime you have
  fixed effects and normally-distriubted random effects \\
  \pause
  \vfill
%    \item
  They allow for (serial, spatial, etc\dots) correlation among
  observations within a level of the grouping variable \\
  \pause
  \vfill
%    \item
  These models can be used for repeated measures data as an
  alternative to MANOVA or the split-plot method with adjusted
  $P$-values \\
  \pause
  \vfill
%    \item
  We do not want you to use this approach for the repeated
  measures questions in the exam. \\
  \pause
  \vfill
%    \item
  But we will expect you to know how to fit linear
      mixed-effects models using {\tt lme}, as we have already done
%  \end{itemize}
\end{frame}

\end{document}

\begin{frame}[fragile]
  \frametitle{Hawk home range size example}
  \scriptsize
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{hawkData} \hlkwb{<-} \hlkwd{read.csv}\hlstd{(}\hlstr{"hawkData.csv"}\hlstd{)}
\hlstd{hawkData}\hlopt{$}\hlstd{hawk} \hlkwb{<-} \hlkwd{factor}\hlstd{(hawkData}\hlopt{$}\hlstd{hawk)}
\hlstd{hawkData}\hlopt{$}\hlstd{season} \hlkwb{<-} \hlkwd{factor}\hlstd{(hawkData}\hlopt{$}\hlstd{season)}
\hlkwd{head}\hlstd{(hawkData,} \hlnum{15}\hlstd{)}
\end{alltt}
\begin{verbatim}
##    hawk season sex homerange
## 1     4      1   F     127.1
## 2     4      2   F      86.6
## 3     4      3   F     116.0
## 4     4      4   F      78.4
## 5     5      1   F     178.4
## 6     5      2   F      84.7
## 7     5      3   F      48.2
## 8     5      4   F      47.9
## 9     8      1   M     158.8
## 10    8      2   M     173.6
## 11    8      3   M      62.1
## 12    8      4   M      54.7
## 13   12      1   F     241.5
## 14   12      2   F     250.4
## 15   12      3   F     162.9
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Mixed-effects models}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{lme.hawk1} \hlkwb{<-} \hlkwd{lme}\hlstd{(homerange} \hlopt{~} \hlstd{sex}\hlopt{*}\hlstd{season,} \hlkwc{data}\hlstd{=hawkData,}
                 \hlkwc{random}\hlstd{=}\hlopt{~}\hlnum{1}\hlopt{|}\hlstd{hawk)}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{lme.hawk2} \hlkwb{<-} \hlkwd{lme}\hlstd{(homerange} \hlopt{~} \hlstd{sex}\hlopt{*}\hlstd{season,} \hlkwc{data}\hlstd{=hawkData,}
                 \hlkwc{random}\hlstd{=}\hlopt{~}\hlnum{1}\hlopt{|}\hlstd{hawk,}
                 \hlkwc{correlation}\hlstd{=}\hlkwd{corARMA}\hlstd{(}\hlkwc{p}\hlstd{=}\hlnum{1}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Compare ANOVA tables}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{anova}\hlstd{(lme.hawk1)}
\end{alltt}
\begin{verbatim}
##             numDF denDF  F-value p-value
## (Intercept)     1    14 74.37777  <.0001
## sex             1     9  2.37407  0.1578
## season          3    14  7.28236  0.0035
## sex:season      3    14  0.05892  0.9805
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{anova}\hlstd{(lme.hawk2)}
\end{alltt}
\begin{verbatim}
##             numDF denDF  F-value p-value
## (Intercept)     1    14 76.66605  <.0001
## sex             1     9  2.66784  0.1368
## season          3    14  4.92350  0.0154
## sex:season      3    14  0.14536  0.9309
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Autocorrelation function (ACF)}
  \tiny
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(}\hlkwd{ACF}\hlstd{(lme.hawk1),} \hlkwc{alpha}\hlstd{=}\hlnum{0.05}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{center}
  \includegraphics[width=7cm]{figure/lme-hawk1-1}
\end{center}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Autocorrelation function (ACF)}
  \tiny
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(}\hlkwd{ACF}\hlstd{(lme.hawk2),} \hlkwc{alpha}\hlstd{=}\hlnum{0.05}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{center}
  \includegraphics[width=7cm]{figure/lme-hawk2-1}
\end{center}
\end{frame}

\end{document}


\documentclass{article}
\usepackage{fullpage}
\usepackage{parskip}
\usepackage{physics}
\usepackage{amssymb}
\usepackage{xcolor}
\usepackage[colorlinks,urlcolor=blue]{hyperref}
\usepackage{array}
\usepackage{longtable}
\usepackage{multirow}

\newcolumntype{M}{>{$\displaystyle}c<{$}}
\newcolumntype{L}{>{$\displaystyle}l<{$}}

\newcommand\Vtextvisiblespace[1][.3em]
{%
	\mbox{\kern.06em\vrule height.3ex}%
	\vbox{\hrule width#1}%
	\hbox{\vrule height.3ex}
}

\newcommand{\cbox}[2][cyan]
{\mathchoice
	{\setlength{\fboxsep}{0pt}\colorbox{#1}{$\displaystyle#2$}}
	{\setlength{\fboxsep}{0pt}\colorbox{#1}{$\textstyle#2$}}
	{\setlength{\fboxsep}{0pt}\colorbox{#1}{$\scriptstyle#2$}}
	{\setlength{\fboxsep}{0pt}\colorbox{#1}{$\scriptscriptstyle#2$}}
}

\newcommand{\typical}{\cbox{\phantom{A}}}
\newcommand{\tall}{\cbox{\phantom{A^{\vphantom{x^x}}_x}}}
\newcommand{\grande}{\cbox{\phantom{\frac{1}{xx}}}}
\newcommand{\venti}{\cbox{\phantom{\sum_x^x}}}

% physics 1.30
\title{The \texttt{physics} package}
\author{Sergio C. de la Barrera \\ \texttt{physics.tex@gmail.com}}

\begin{document}
\maketitle

\tableofcontents

\section{Before you start}
\subsection{The purpose of this package}
The goal of this package is to make typesetting equations for physics simpler, faster, and more human-readable. To that end, the commands included in this package have names that make the purpose of each command immediately obvious and remove any ambiguity while reading and editing \texttt{physics} code. From a practical standpoint, it is handy to have a well-defined set of shortcuts for accessing the long-form of each of these commands. The commands listed below are therefore defined in terms of their long-form names and then shown explicitly in terms of the default shorthand command sequences. These shorthand commands are meant make it easy to remember both the shorthand names and what each one represents.

\subsection{Other required packages}
The \texttt{physics} package requires \texttt{xparse} and \texttt{amsmath} to work properly in your \LaTeX~document. The \texttt{amsmath} package comes standard with most \LaTeX~distributions and is loaded by \texttt{physics} for your convenience. You may also already have \texttt{xparse} installed on your system as it is a popular package for defining \LaTeX macros, however, if you are unsure you can either install it again using your local package manager (comes with most distributions) or by visiting the \href{http://www.ctan.org/}{CTAN} online package database, or you could even just try to use \texttt{physics} without worrying about it. Many modern \LaTeX compilers will locate and offer to download missing packages for you.
\subsection{Using \texttt{physics} in your \LaTeX~document}
To use the \texttt{physics} package, simply insert \verb|\usepackage{physics}| in the preamble of your document, before \verb|\begin{document}| and after \verb|\documentclass{class}|:
\begin{verbatim}
\documentclass{class}
...
\usepackage{physics}
...
\begin{document}
	content...
\end{document}
\end{verbatim}

\section{List of commands}
\subsection{Automatic bracing}
\begin{longtable}[l]{ l l p{6cm} }
\verb|\quantity| & \verb|\qty(\typical)| $\displaystyle\rightarrow \qty(\typical)$ & automatic $\qty(\;)$ braces \\
& \verb|\qty(\tall)| $\displaystyle\rightarrow \qty(\tall)$ & \\
& \verb|\qty(\grande)| $\displaystyle\rightarrow \qty(\grande)$ & \\
& \verb|\qty[\typical]| $\rightarrow \qty[\typical]$ & automatic $\qty[\;]$ braces \\
& \verb+\qty|\typical|+ $\rightarrow \qty|\typical|$ & automatic $\qty|\;|$ braces \\
& \verb|\qty{\typical}| $\rightarrow \qty{\typical}$ & automatic $\qty{\;}$ braces \\
& \verb|\qty\big{}| $\rightarrow \qty\big{}$ & \multirow{2}{*}{\parbox{6cm}{manual sizing (works with any of the above bracket types)}} \\
& \verb|\qty\Big{}| $\rightarrow \qty\Big{}$ & \\
& \verb|\qty\bigg{}| $\rightarrow \qty\bigg{}$ & \\
& \verb|\qty\Bigg{}| $\rightarrow \qty\Bigg{}$ & \\
& \verb|\pqty{}| $\leftrightarrow$ \verb|\qty()| & \multirow{2}{*}{\parbox{6cm}{alternative syntax; robust and more \LaTeX-friendly}} \\
& \verb|\bqty{}| $\leftrightarrow$ \verb|\qty[]| & \\
& \verb+\vqty{}+ $\leftrightarrow$ \verb+\qty||+ & \\
& \verb|\Bqty{}| $\leftrightarrow$ \verb|\qty{}| & \\
\verb|\absolutevalue| & \verb|\abs{a}| $\rightarrow \abs{a}$ & automatic sizing; equivalent to \verb|\qty| \!\!\texttt{|a|} \\
& \verb|\abs\Big{a}| $\rightarrow \abs\Big{a}$ & inherits manual sizing syntax from \verb|\qty| \\
& \verb|\abs*{\grande}| $\displaystyle\rightarrow \abs*{\grande}$ & star for no resize \\
\verb|\norm| & \verb|\norm{a}| $\rightarrow \norm{a}$ & automatic sizing \\
& \verb|\norm\Big{a}| $\rightarrow \norm\Big{a}$ & manual sizing \\
& \verb|\norm*{\grande}| $\displaystyle\rightarrow \norm*{\grande}$ & star for no resize \\
\verb|\evaluated| & \verb|\eval{x}_0^\infty| $\displaystyle\rightarrow \eval{x}_0^\infty$ & vertical bar for evaluation limits \\
& \verb|\eval(x| \!\!\texttt{|}\!\! \verb|_0^\infty| $\displaystyle\rightarrow \eval(x|_0^\infty$ & alternate form \\
& \verb|\eval[x| \!\!\texttt{|}\!\! \verb|_0^\infty| $\displaystyle\rightarrow \eval[x|_0^\infty$ & alternate form \\
& \verb|\eval[\venti| \!\!\texttt{|}\!\! \verb|_0^\infty| $\displaystyle\rightarrow \eval[\venti|_0^\infty$ & automatic sizing\\
& \verb|\eval*[\venti| \!\!\texttt{|}\!\! \verb|_0^\infty| $\displaystyle\rightarrow \eval*[\venti|_0^\infty$ & star for no resize \\
\verb|\order| & \verb|\order{x^2}| $\rightarrow \order{x^2}$ & order symbol; automatic sizing and space handling \\
& \verb|\order\Big{x^2}| $\rightarrow \order\Big{x^2}$ & manual sizing \\
& \verb|\order*{\grande}| $\displaystyle\rightarrow \order*{\grande}$ & star for no resize \\
\verb|\commutator| & \verb|\comm{A}{B}| $\rightarrow \comm{A}{B}$ & automatic sizing \\
& \verb|\comm\Big{A}{B}| $\rightarrow \comm\Big{A}{B}$ & manual sizing \\
& \verb|\comm*{A}{\grande}| $\displaystyle\rightarrow \comm*{A}{\grande}$ & star for no resize \\
\verb|\anticommutator| & \verb|\acomm{A}{B}| $\rightarrow \acomm{A}{B}$ & same as \verb|\poissonbracket| \\
%& \verb|\acommutator{A}{B}| $\rightarrow \acommutator{A}{B}$ & alternate name \\
\verb|\poissonbracket| & \verb|\pb{A}{B}| $\rightarrow \pb{A}{B}$ & same as \verb|\anticommutator|
\end{longtable}

\subsection{Vector notation}
The default del symbol $\vnabla$ used in \texttt{physics} vector notation can be switched to appear with an arrow $\vec{\vnabla}$ by including the option \texttt{arrowdel} in the document preamble $\rightarrow$ \verb|\usepackage[arrowdel]{physics}|.
\begin{longtable}[l]{ l l p{6cm} }
\verb|\vectorbold| & \verb|\vb{a}| $\rightarrow \vb{a}$ & upright/no Greek \\
& \verb|\vb*{a}|, \verb|\vb*{\theta}| $\rightarrow \vb*{a}$, $\vb*{\theta}$ & italic/Greek \\
\verb|\vectorarrow| & \verb|\va{a}| $\rightarrow \va{a}$ & upright/no Greek \\
& \verb|\va*{a}|, \verb|\va*{\theta}| $\rightarrow \va*{a}$, $\va*{\theta}$ & italic/Greek \\
\verb|\vectorunit| & \verb|\vu{a}| $\rightarrow \vu{a}$ & upright/no Greek \\
& \verb|\vu*{a}|, \verb|\vu*{\theta}| $\rightarrow \vu*{a}$, $\vu*{\theta}$ & italic/Greek \\
\verb|\dotproduct| & \verb|\vdot| $\rightarrow \vdot$ as in $\vb{a} \vdot \vb{b}$ & note: \verb|\dp| is a protected \TeX\ primitive \\
\verb|\crossproduct| & \verb|\cross| $\rightarrow \cross$ as in $\vb{a} \cross \vb{b}$ & alternate name \\
& \verb|\cp| $\rightarrow \cp$ as in $\vb{a} \cp \vb{b}$ & shorthand name \\
\verb|\gradient| & \verb|\grad| $\rightarrow \grad$ & \\
& \verb|\grad{\Psi}| $\rightarrow \grad{\Psi}$ & default mode \\
& \verb|\grad(\Psi+\tall)| $\displaystyle\rightarrow \grad(\Psi+\tall)$ & long-form (like \verb|\qty| but also handles spacing) \\
& \verb|\grad[\Psi+\tall]| $\displaystyle\rightarrow \grad[\Psi+\tall]$ & \\
\verb|\divergence| & \verb|\div| $\rightarrow \div$ & note: \texttt{amsmath} symbol $\divisionsymbol$ renamed \verb|\divisionsymbol| \\
& \verb|\div{\vb{a}}| $\rightarrow \div{\vb{a}}$ & default mode \\
& \verb|\div(\vb{a}+\tall)| $\displaystyle\rightarrow \div(\vb{a}+\tall)$ & long-form \\
& \verb|\div[\vb{a}+\tall]| $\displaystyle\rightarrow \div[\vb{a}+\tall]$ & \\
\verb|\curl| & \verb|\curl| $\rightarrow \curl$ & \\
& \verb|\curl{\vb{a}}| $\rightarrow \curl{\vb{a}}$ & default mode \\
& \verb|\curl(\vb{a}+\tall)| $\displaystyle\rightarrow \curl(\vb{a}+\tall)$ & long-form \\
& \verb|\curl[\vb{a}+\tall]| $\displaystyle\rightarrow \curl[\vb{a}+\tall]$ & \\
\verb|\laplacian| & \verb|\laplacian| $\rightarrow \laplacian$ & \\
& \verb|\laplacian{\Psi}| $\rightarrow \laplacian{\Psi}$ & default mode \\
& \verb|\laplacian(\Psi+\tall)| $\displaystyle\rightarrow \laplacian(\Psi+\tall)$ & long-form \\
& \verb|\laplacian[\Psi+\tall]| $\displaystyle\rightarrow \laplacian[\Psi+\tall]$ &
\end{longtable}

\subsection{Operators}
The standard set of trig functions is redefined in \texttt{physics} to provide automatic braces that behave like \verb|\qty()|. In addition, an optional power argument is provided. This behavior can be switched off by including the option \texttt{notrig} in the preamble $\rightarrow$ \verb|\usepackage[notrig]{physics}|.

\begin{tabular}[l]{ l l p{8cm} }
\multicolumn{3}{l}{Example trig redefinitions:} \\
\verb|\sin| & \verb|\sin(\grande)| $\displaystyle\rightarrow \sin(\grande)$ & automatic braces; old \verb|\sin| renamed \verb|\sine| \\
& \verb|\sin[2](x)| $\rightarrow \sin[2](x)$ & optional power \\
& \verb|\sin x| $\rightarrow \sin x$ & can still use without an argument
\end{tabular}

The full set of available trig functions in \texttt{physics} includes:

\begin{tabular}{llll}
\verb|\sin(x)| & \verb|\sinh(x)| & \verb|\arcsin(x)| & \verb|\asin(x)| \\
\verb|\cos(x)| & \verb|\cosh(x)| & \verb|\arccos(x)| & \verb|\acos(x)| \\
\verb|\tan(x)| & \verb|\tanh(x)| & \verb|\arctan(x)| & \verb|\atan(x)| \\
\verb|\csc(x)| & \verb|\csch(x)| & \verb|\arccsc(x)| & \verb|\acsc(x)| \\
\verb|\sec(x)| & \verb|\sech(x)| & \verb|\arcsec(x)| & \verb|\asec(x)| \\
\verb|\cot(x)| & \verb|\coth(x)| & \verb|\arccot(x)| & \verb|\acot(x)|
\end{tabular}$\Rightarrow$
\begin{tabular}{MMMM}
\sin(x) & \sinh(x) & \arcsin(x) & \asin(x) \\
\cos(x) & \cosh(x) & \arccos(x) & \acos(x) \\
\tan(x) & \tanh(x) & \arctan(x) & \atan(x) \\
\csc(x) & \csch(x) & \arccsc(x) & \acsc(x) \\
\sec(x) & \sech(x) & \arcsec(x) & \asec(x) \\
\cot(x) & \coth(x) & \arccot(x) & \acot(x)
\end{tabular}

The standard trig functions (plus a few that are missing in \texttt{amsmath}) are available without any automatic bracing under a new set of longer names:

\begin{tabular}{llll}
\verb|\sine| & \verb|\hypsine| & \verb|\arcsine| & \verb|\asine| \\
\verb|\cosine| & \verb|\hypcosine| & \verb|\arccosine| & \verb|\acosine| \\
\verb|\tangent| & \verb|\hyptangent| & \verb|\arctangent| & \verb|\atangent| \\
\verb|\cosecant| & \verb|\hypcosecant| & \verb|\arccosecant| & \verb|\acosecant| \\
\verb|\secant| & \verb|\hypsecant| & \verb|\arcsecant| & \verb|\asecant| \\
\verb|\cotangent| & \verb|\hypcotangent| & \verb|\arccotangent| & \verb|\acotangent|
\end{tabular}

Similar behavior has also been extended to the following functions:

\begin{tabular}{l>{$}l<{$}ll}
\verb|\exp(\tall)| & \exp(\tall) & & \verb|\exponential| \\
\verb|\log(\tall)| & \log(\tall) & & \verb|\logarithm| \\
\verb|\ln(\tall)| & \ln(\tall) & old definitions $\Rightarrow$ & \verb|\naturallogarithm| \\
\verb|\det(\tall)| & \det(\tall) & & \verb|\determinant| \\
\verb|\Pr(\tall)| & \Pr(\tall) & & \verb|\Probability|
\end{tabular}

\begin{longtable}[l]{ l l p{8cm} }
\multicolumn{3}{l}{New operators:} \\
\verb|\trace| or \verb|\tr| & \verb|\tr\rho| $\rightarrow \tr\rho$ also \verb|\tr(\tall)| $\rightarrow \tr(\tall)$  & trace; same bracing as trig functions \\
\verb|\Trace| or \verb|\Tr| & \verb|\Tr\rho| $\rightarrow \Tr\rho$ & alternate \\
\verb|\rank| & \verb|\rank M| $\rightarrow \rank M$ & matrix rank \\
\verb|\erf| & \verb|\erf(x)|$\rightarrow \erf(x)$ & Gauss error function \\
\verb|\Res| & \verb|\Res[f(z)]|$\rightarrow \Res[f(z)]$ & residue; same bracing as trig functions \\
\verb|\principalvalue| & \verb|\pv{\int f(z) \dd{z}}|$\rightarrow \pv{\int f(z) \dd{z}}$ & Cauchy principal value \\
& \verb|\PV{\int f(z) \dd{z}}|$\rightarrow \PV{\int f(z) \dd{z}}$ & alternate \\
\verb|\Re| & \verb|\Re{z}| $\rightarrow \Re{z}$ & old \verb|\Re| renamed to \verb|\real| $\rightarrow \real$ \\
\verb|\Im| & \verb|\Im{z}| $\rightarrow \Im{z}$ & old \verb|\Im| renamed to \verb|\imaginary| $\rightarrow \imaginary$
\end{longtable}

\subsection{Quick quad text}
This set of commands produces text in math-mode padded by \verb|\quad| spacing on either side. This is meant to provide a quick way to insert simple words or phrases in a sequence of equations. Each of the following commands includes a starred version which pads the text only on the right side with \verb|\quad| for use in aligned environments such as \texttt{cases}.

\begin{tabular}[l]{ l l p{6cm} }
General text: & & \\
\verb|\qqtext| & \verb|\qq{}| & general quick quad text with argument \\
& \verb|\qq{word or phrase}| $\rightarrow$\Vtextvisiblespace[1em]$\text{word or phrase}$\Vtextvisiblespace[1em] & normal mode; left and right \verb|\quad| \\
& \verb|\qq*{word or phrase}| $\rightarrow \text{word or phrase}$\Vtextvisiblespace[1em] & starred mode; right \verb|\quad| only
\end{tabular}

\begin{longtable}[l]{ l l }
Special macros: & \\
\verb|\qcomma| or \verb|\qc| $\rightarrow ,$\Vtextvisiblespace[1em] & right \verb|\quad| only \\
\verb|\qcc| $\rightarrow$\Vtextvisiblespace[1em]$\text{c.c.}$\Vtextvisiblespace[1em] & complex conjugate; left and right \verb|\quad| unless starred \verb|\qcc*| $\rightarrow \text{c.c.}$\Vtextvisiblespace[1em] \\
\verb|\qif| $\rightarrow$\Vtextvisiblespace[1em]$\text{if}$\Vtextvisiblespace[1em] & left and right \verb|\quad| unless starred \verb|\qif*| $\rightarrow \text{if}$\Vtextvisiblespace[1em]
\end{longtable}
\begin{longtable}[l]{ l }
Similar to \verb|\qif|: \\
\verb|\qthen|, \verb|\qelse|, \verb|\qotherwise|, \verb|\qunless|, \verb|\qgiven|, \verb|\qusing|, \verb|\qassume|, \verb|\qsince|, \\
\verb|\qlet|, \verb|\qfor|, \verb|\qall|, \verb|\qeven|, \verb|\qodd|, \verb|\qinteger|, \verb|\qand|, \verb|\qor|, \verb|\qas|, \verb|\qin|
\end{longtable}

\subsection{Derivatives}
The default differential symbol $\dd$ which is used in \verb|\differential| and \verb|\derivative| can be switched to an italic form $d$ by including the option \texttt{italicdiff} in the preamble $\rightarrow$ \verb|\usepackage[italicdiff]{physics}|.
\begin{longtable}[l]{ l l p{6cm} }
\verb|\differential| & \verb|\dd| $\rightarrow \dd$ & \\
& \verb|\dd x| $\rightarrow \dd x$ & no spacing (not recommended) \\
& \verb|\dd{x}| $\rightarrow$ \textvisiblespace\,$\dd{x}$\textvisiblespace & automatic spacing based on neighbors \\
& \verb|\dd[3]{x}| $\rightarrow \dd[3]{x}$ & optional power \\
& \verb|\dd(\cos\theta)| $\rightarrow \dd(\cos\theta)$ & long-form; automatic braces \\
\verb|\derivative| & \verb|\dv{x}| $\displaystyle\rightarrow \dv{x}$ & one argument \\
& \verb|\dv{f}{x}| $\displaystyle\rightarrow \dv{f}{x}$ & two arguments \\
& \verb|\dv[n]{f}{x}| $\displaystyle\rightarrow \dv[n]{f}{x}$ & optional power \\
& \verb|\dv{x}(\grande)| $\displaystyle\rightarrow \dv{x}(\grande)$ & long-form; automatic braces, spacing \\
& \verb|\dv*{f}{x}| $\displaystyle\rightarrow \dv*{f}{x}$ & inline form using \verb|\flatfrac| \\
\verb|\partialderivative| & \verb|\pderivative{x}| $\displaystyle\rightarrow \pderivative{x}$ & alternate name \\
& \verb|\pdv{x}| $\displaystyle\rightarrow \pdv{x}$ & shorthand name \\
& \verb|\pdv{f}{x}| $\displaystyle\rightarrow \pdv{f}{x}$ & two arguments \\
& \verb|\pdv[n]{f}{x}| $\displaystyle\rightarrow \pdv[n]{f}{x}$ & optional power \\
& \verb|\pdv{x}(\grande)| $\displaystyle\rightarrow \pdv{x}(\grande)$ & long-form \\
& \verb|\pdv{f}{x}{y}| $\displaystyle\rightarrow \pdv{f}{x}{y}$ & mixed partial \\
& \verb|\pdv*{f}{x}| $\displaystyle\rightarrow \pdv*{f}{x}$ & inline form using \verb|\flatfrac| \\
\verb|\variation| & \verb|\var{F[g(x)]}| $\rightarrow \var{F[g(x)]}$ & functional variation (works like \verb|\dd|) \\
& \verb|\var(E-TS)| $\rightarrow \var(E-TS)$ & long-form \\
\verb|\functionalderivative| & \verb|\fdv{g}| $\displaystyle\rightarrow \fdv{g}$ & functional derivative (works like \verb|\dv|) \\
& \verb|\fdv{F}{g}| $\displaystyle\rightarrow \fdv{F}{g}$ & \\
& \verb|\fdv{V}(E-TS)| $\displaystyle\rightarrow \fdv{V}(E-TS)$ & long-form \\
& \verb|\fdv*{F}{x}| $\displaystyle\rightarrow \fdv*{F}{x}$ & inline form using \verb|\flatfrac|
\end{longtable}

\subsection{Dirac bra-ket notation}
The following collection of macros for Dirac notation contains two fundamental commands, \verb|\bra| and \verb|\ket|, along with a set of more specialized macros which are essentially combinations of the fundamental pair. The specialized macros are both useful and descriptive from the perspective of generating \texttt{physics} code, however, the fundamental commands are designed to contract with one another algebraically when appropriate and are thus suggested for general use. For instance, the following code renders correctly\footnote{Note the lack of a space between the bra and ket commands. This is necessary is order for the bra to find the corresponding ket and form a contraction.}
\begin{displaymath}
\verb|\bra{\phi}\ket{\psi}| \rightarrow \bra{\phi}\ket{\psi} \qq{as opposed to} \bra{\phi} \ket{\psi}
\end{displaymath}
whereas a similar construction with higher-level macros will not contract in a robust manner
\begin{displaymath}
\verb|\bra{\phi}\dyad{\psi}{\xi}| \rightarrow \bra{\phi}\dyad{\psi}{\xi}.
\end{displaymath}
On the other hand, the correct output can be generated by sticking to the fundamental commands,
\begin{displaymath}
\verb|\bra{\phi}\ket{\psi}\bra{\xi}| \rightarrow \bra{\phi}\ket{\psi}\bra{\xi}
\end{displaymath}
allowing the user to type out complicated quantum mechanical expressions without worrying about bra-ket contractions. That being said, the high-level macros do have a place in convenience and readability, as long as the user is aware of rendering issues that may arise due to an absence of automatic contractions.
\begin{longtable}[l]{ l L p{6cm} }
\verb|\ket| & \verb|\ket{\tall}| \rightarrow \ket{\tall} & automatic sizing \\
& \verb|\ket*{\tall}| \rightarrow \ket*{\tall} & no resize \\
\verb|\bra| & \verb|\bra{\tall}| \rightarrow \bra{\tall} & automatic sizing \\
& \verb|\bra*{\tall}| \rightarrow \bra*{\tall} & no resize \\
& \verb|\bra{\phi}\ket{\psi}| \rightarrow \bra{\phi}\ket{\psi} & automatic contraction \\
& \verb|\bra{\phi}\ket{\tall}| \rightarrow \bra{\phi}\ket{\tall} & contraction inherits automatic sizing \\
& \verb|\bra{\phi}\ket*{\tall}| \rightarrow \bra{\phi}\ket*{\tall} & \multirow{2}{*}{\parbox{6cm}{a star on either term in the contraction prohibits resizing}} \\
& \verb|\bra*{\phi}\ket{\tall}| \rightarrow \bra*{\phi}\ket{\tall} & \\
& \verb|\bra*{\phi}\ket*{\tall}| \rightarrow \bra*{\phi}\ket*{\tall} & \\
\verb|\innerproduct| & \verb|\braket{a}{b}| \rightarrow \braket{a}{b} & two-argument braket \\
& \verb|\braket{a}| \rightarrow \braket{a} & one-argument (norm) \\
& \verb|\braket{a}{\tall}| \rightarrow \braket{a}{\tall} & automatic sizing \\
& \verb|\braket*{a}{\tall}| \rightarrow \braket*{a}{\tall} & no resize \\
& \verb|\ip{a}{b}| \rightarrow \ip{a}{b} & shorthand name \\
\verb|\outerproduct| & \verb|\dyad{a}{b}| \rightarrow \dyad{a}{b} & two-argument dyad \\
& \verb|\dyad{a}| \rightarrow \dyad{a} & one-argument (projector) \\
& \verb|\dyad{a}{\tall}| \rightarrow \dyad{a}{\tall} & automatic sizing \\
& \verb|\dyad*{a}{\tall}| \rightarrow \dyad*{a}{\tall} & no resize \\
& \verb|\ketbra{a}{b}| \rightarrow \ketbra{a}{b} & alternative name \\
& \verb|\op{a}{b}| \rightarrow \op{a}{b} & shorthand name \\
\verb|\expectationvalue| & \verb|\expval{A}| \rightarrow \expval{A} & implicit form \\
& \verb|\expval{A}{\Psi}| \rightarrow \expval{A}{\Psi} & explicit form \\
& \verb|\ev{A}{\Psi}| \rightarrow \ev{A}{\Psi} & shorthand name \\
& \verb|\ev{\grande}{\Psi}| \rightarrow \ev{\grande}{\Psi} & default sizing ignores middle argument \\
& \verb|\ev*{\grande}{\tall}| \rightarrow \ev*{\grande}{\tall} & single star does no resizing whatsoever \\
& \verb|\ev**{\grande}{\Psi}| \rightarrow \ev**{\grande}{\Psi} & double star resizes based on all parts \\
\verb|\matrixelement| & \verb|\matrixel{n}{A}{m}| \rightarrow \matrixel{n}{A}{m} & requires all three arguments \\
& \verb|\mel{n}{A}{m}| \rightarrow \mel{n}{A}{m} & shorthand name \\
& \verb|\mel{n}{\grande}{m}| \rightarrow \mel{n}{\grande}{m} & default sizing ignores middle argument \\
& \verb|\mel*{n}{\grande}{\tall}| \rightarrow \mel*{n}{\grande}{\tall} & single star does no resizing whatsoever \\
& \verb|\mel**{n}{\grande}{m}| \rightarrow \mel**{n}{\grande}{m} & double star resizes based on all parts
\end{longtable}

\subsection{Matrix macros}
The following matrix macros produce unformatted rows and columns of matrix elements for use as separate matrices as well as blocks within larger matrices. For example, the command \verb|\identitymatrix{2}| which has also has the shortcut \verb|\imat{2}| produces the elements of a $2 \times 2$ identity matrix $\smqty{\imat{2}}$ without braces or grouping. This allows the command to also be used within another matrix, as in:

\begin{minipage}{3cm}
\begin{verbatim}
\begin{pmatrix}
\imat{2} \\ a & b
\end{pmatrix}
\end{verbatim}
\end{minipage}
\begin{minipage}{6cm}
\begin{displaymath}
\Rightarrow\qquad
\begin{pmatrix}
\imat{2} \\ a & b
\end{pmatrix}
\end{displaymath}
\end{minipage}

To specify elements on the right of left sides of our \verb|\imat{2}| sub-matrix we use the grouping command \verb|\matrixquantity| or \verb|\mqty| to effectively convert \verb|\imat{2}| into a single matrix element of a larger matrix:

\begin{minipage}{9cm}
\begin{verbatim}
\begin{pmatrix}
\mqty{\imat{2}} & \mqty{a\\b} \\ \mqty{c & d} & e
\end{pmatrix}
\end{verbatim}
\end{minipage}
\begin{minipage}{6cm}
\begin{displaymath}
\Rightarrow\qquad
\begin{pmatrix}
\mqty{\imat{2}} & \mqty{a\\b} \\ \mqty{c & d} & e
\end{pmatrix}
\end{displaymath}
\end{minipage}

The extra \verb|\mqty| groups were required in this case in order to get the $a$ and $b$ elements to behave as a single element, since \verb|\mqty{\imat{2}}| also acts like a single matrix element (the same can be said of the grouped $c$ and $d$ elements). Finally, the outermost \texttt{pmatrix} environment could have also been replaced with the \texttt{physics} macro \verb|\mqty()|, allowing the above example to be written on one line:

\begin{minipage}{9cm}
\begin{verbatim}
\mqty(\mqty{\imat{2}} & \mqty{a\\b} \\ \mqty{c & d} & e)
\end{verbatim}
\end{minipage}
\begin{minipage}{6cm}
\begin{displaymath}
\Rightarrow\qquad
\mqty(\mqty{\imat{2}} & \mqty{a\\b} \\ \mqty{c & d} & e)
\end{displaymath}
\end{minipage}

\begin{longtable}[l]{ l L p{6cm} }
\verb|\matrixquantity| & \verb|\mqty{a & b \\ c & d}| \rightarrow \mqty{a & b \\ c & d} & groups a set of matrix elements into a single object \\
& \verb|\mqty(a & b \\ c & d)| \rightarrow {\mqty(a & b \\ c & d)} & parentheses \\
& \verb|\mqty*(a & b \\ c & d)| \rightarrow {\mqty*(a & b \\ c & d)} & alternate parentheses \\
& \verb|\mqty[a & b \\ c & d]| \rightarrow {\mqty[a & b \\ c & d]} & square brackets \\
& \verb|\mqty| \texttt{|} \verb|a & b \\ c & d| \texttt{|} \rightarrow {\mqty|a & b \\ c & d|} & vertical bars \\
& \verb|\pmqty{}| \leftrightarrow \verb|\mqty()| & \multirow{2}{*}{\parbox{6cm}{alternative syntax; robust and more \LaTeX-friendly}} \\
& \verb|\Pmqty{}| \leftrightarrow \verb|\mqty*()| & \\
& \verb|\bmqty{}| \leftrightarrow \verb|\mqty[]| & \\
& \verb|\vmqty{}| \leftrightarrow \verb+\mqty||+ & \\
\verb|\smallmatrixquantity| & \verb|\smqty{a & b \\ c & d}| \rightarrow \smqty{a & b \\ c & d} & the \texttt{smallmatrix} form of \verb|\mqty| \\
& \verb|\smqty()| \qor \verb|\spmqty{}| & small version of \verb|\mqty()| \\
& \verb|\smqty*()| \qor \verb|\sPmqty{}| & small version of \verb|\mqty*()|\\
& \verb|\smqty[]| \qor \verb|\sbmqty{}| & small version of \verb|\mqty[]| \\
& \verb+\smqty||+ \qor \verb|\svmqty{}| & small version of \verb+\mqty||+ \\
\verb|\matrixdeterminant| & \verb|\mdet{a & b \\ c & d}| \rightarrow {\mdet{a & b \\ c & d}} & matrix determinant \\
& \verb|\smdet{a & b \\ c & d}| \rightarrow {\smdet{a & b \\ c & d}} & small matrix determinant \\
\verb|\identitymatrix| & \verb|\imat{n}| & elements of $n \times n$ identity matrix \\
& \verb|\smqty(\imat{3})| \rightarrow \smqty(\imat{3}) & formatted with \verb|\mqty| or \verb|\smqty| \\
\verb|\xmatrix| & \verb|\xmat{x}{n}{m}| & elements of $n \times m$ matrix filled with $x$ \\
& \verb|\smqty(\xmat{1}{2}{3})| \rightarrow \smqty(\xmat{1}{2}{3}) & formatted with \verb|\mqty| or \verb|\smqty| \\
& \verb|\smqty(\xmat*{a}{3}{3})| \rightarrow \smqty(\xmat*{a}{3}{3}) & star for element indices \\
& \verb|\smqty(\xmat*{a}{3}{1})| \rightarrow \smqty(\xmat*{a}{3}{1}) & as a vector with indices \\
& \verb|\smqty(\xmat*{a}{1}{3})| \rightarrow \smqty(\xmat*{a}{1}{3}) & \\
\verb|\zeromatrix| & \verb|\zmat{n}{m}| & $n \times m$ matrix filled with zeros \\
& \verb|\smqty(\zmat{2}{2})| \rightarrow \smqty(\zmat{2}{2}) & equivalent to \verb|\xmat{0}{n}{m}| \\
\verb|\paulimatrix| & \verb|\pmat{n}| & $n^\text{th}$ Pauli matrix \\
& \verb|\smqty(\pmat{0})| \rightarrow \smqty(\pmat{0}) & $n\in \lbrace 0,1,2,3$ or $x,y,z \rbrace$ \\
& \verb|\smqty(\pmat{1})| \rightarrow \smqty(\pmat{1}) & \\
& \verb|\smqty(\pmat{2})| \rightarrow \smqty(\pmat{2}) & \\
& \verb|\smqty(\pmat{3})| \rightarrow \smqty(\pmat{3}) & \\
\verb|\diagonalmatrix| & \verb|\dmat{a,b,c,...}| & \multirow{2}{*}{\parbox{6cm}{specify up to eight diagonal or block diagonal elements}} \\
& \verb|\mqty(\dmat{1,2,3})| \rightarrow \mqty(\dmat{1,2,3}) & \\
& \verb|\mqty(\dmat[0]{1,2})| \rightarrow \mqty(\dmat[0]{1,2}) & optional argument to fill spaces \\
& \verb|\mqty(\dmat{1,2&3\\4&5})| \rightarrow \mqty(\dmat{1,2&3\\4&5}) & \parbox{6cm}{enter matrix elements for each block as a single diagonal element} \\
\verb|\antidiagonalmatrix| & \verb|\admat{a,b,c,...}| & same as syntax as \verb|\dmat| \\
& \verb|\mqty(\admat{1,2,3})| \rightarrow \mqty(\admat{1,2,3}) & \\
\end{longtable}

\end{document}


\subsection*{\LaTeX~Homework Bonus Points}

Part of good mathematics is its beautiful presentation. Thus, \ingreen{there will be a 1--5 point bonus} added to your theory homework grade  for typing up your homework using the \LaTeX ~typesetting system based on the elegance of your presentation. The bonus is arbitrarily determined by me.

I recommend using \href{http://overleaf.com}{overleaf} to write up your homeworks (make sure you upload both the hw\#.tex and the preamble.tex file). This has the advantage of (a) not having to install anything on your computer and thus not having to maintain your \LaTeX ~installation (b) allowing easy collaboration with others (c) alway having a backup of your work since it's always on the cloud. If you insist to have \LaTeX ~running on your computer, you can download it for Windows \href{http://www.miktex.org/download}{here} and for MAC \href{http://www.tug.org/mactex/}{here}. For editing and producing PDF's, I recommend \TeX works which can be downloaded \href{http://www.tug.org/texworks/#Getting_TeXworks}{here}. Please use the \LaTeX ~code provided on the \coursewebpagelink ~for each homework assignment.

If you are handing in homework this way, read the comments in the code; there are two lines to comment out and you should replace my name with yours and write your section. The easiest way to use overleaf is to copy the raw text from hwxx.tex and preamble.tex into two new overleaf tex files with the same name. If you are asked to make drawings, you can take a picture of your handwritten drawing and insert them as figures or leave space using the \qu{$\backslash$vspace} command and draw them in after printing or attach them stapled.

Since this is extra credit, do not ask me for help in setting up your computer with \LaTeX~ in class or in office hours. Also, \textbf{never share your \LaTeX~code with other students} --- it is cheating and if you are found I will take it seriously.


% MARINA VON STEINKIRCH
% http://mysbfiles.stonybrook.edu/~mvonsteinkir/

\documentclass[10pt]{article} % Good sizes are usually 10pt or 11pt. You can use [10pt,twocolumn] to generate a document with two columns.
\title{An Article in Latex for Physics}
\author{ \texttt{ Marina von Steinkirch, steinkirch@gmail.com}\\
	   \texttt{State University of New York at Stony Brook}}
\date{\today} % Or type the date you want

%%%%%%%%%%%%%%%   PACKAGES   %%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{amsmath}    % Package  for subequations
\usepackage{graphicx}   % Package for figures
\usepackage{verbatim}   % Package  for program listings
\usepackage{color}      % Package to insert  color in text
\usepackage{hyperref}   % Package  for hypertext links, external documents and URLs
\usepackage{amssymb} % For mathematical constructions
\usepackage{latexsym} % Package to generate mathematical symbols
\usepackage{makeidx} % Package to generate an index in the end

\makeindex % To Generate an index in the end

%%%%%%%%%%%%%%%  BEGIN DOCUMENT   %%%%%%% %%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\maketitle
\numberwithin{equation}{section} % To give the number of each equation related to the section
\tableofcontents % to print the index

\begin{abstract}
A very brief introduction to articles in latex.
\end{abstract}

%%%%%%%%%%%%%%    BODY    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Some Basic Code} % You can write \section* to have it without numeration

\subsection{Basic Commands}

\begin{itemize}
\item This is how you include footnotes \footnote{My footnote}
\item To make a reference to some item in the bibliography, use \cite{steinkirch}. To make a reference to an equation, use \eqref{myequation}. To make a reference to a table or figure, use \ref{mytable}.
\item If you want to index topics of your text, you can use \index{How to make an idex}.
\end{itemize}

\subsubsection*{Font Styles}

\begin{itemize}
\item To write in bold, {\bf writing in bold }
\item To write in italic, {\it writing in bold }
\item To write in typing letters, {\texttt typing letters}
\item To write in small {\small small text}, or very small {\tiny tiny text}.
\item To start a quotation \begin{quotation} Quoting mamma... \end{quotation}
\item To use calligraph letters, use $\mathcal{L}$.
\end{itemize}

\subsection{Declaring Equations} % You can write \subsection* to have it without numeration

You can include equations in the middle of the tex using $2\pi$.

Equations in different lines without numerations can be written as
$$1.19 \times 10^{57}$$

Equations with numeration can be declared as
\begin{equation} % however, if you write equation*  you lose the numeration
P \Big (  \frac{nRT}{P} \Big ) = 1.
\label{myequation} % labeling equations to make references in the text
\end{equation}

or when you have an array of equations,
\begin{eqnarray} % however, if you write eqnarray*  you lose the numeration
P \Big (  \frac{nRT}{P} \Big ) &=& 1 + k,\\  % The symbol & makes the indentation
&=& 0.
\end{eqnarray}

Multi-line equations can be written as
\begin{equation}
\label{myequation_in_many_lines}
\begin{split}
a& = b+c\\
& \quad + d - e\\
& =1
\end{split}
\end{equation}

\subsection{Including Tables, Lists, and Figures}

This is the code for including tables:
\begin{table}[htdp]
\begin{center}
\begin{tabular}{c|c} % This parameter says how many columns and their respective lines, another example {|cccc|}
\hline % Vertical lines
Testing 1 & Testing 2\\
Testing 4 & Testing 3 \\
\hline % Vertical lines
\end{tabular}
\end{center}
\caption{Data for the Problem.}
\label{mytable}
\end{table}

This is the code for including lists:
\begin{itemize}
\item Item one
\item Item two
\end{itemize}

This is the code for including enumerated lists:
\begin{enumerate}
\item First Item
\item Second Item
\end{enumerate}

This is the code for including figures:
\begin{figure}[htbp]
\begin{center}
%\includegraphics[scale=0.5]{fig.jpg} % you can set the scale or length and width
\label{myfigure}
\end{center}
\end{figure}

\section{The End of The Document}
%%%%%%%%%%%%%%%%%%%%%%%%%  END OF DOCUMENT %%%%%%%%%%%%%%%%%%%%%%%%

\begin{thebibliography}{2} % Declaration of bibliography
\bibitem{atiyah} ATIYAH,  The Geometry and Physics of Knots,1990.
\bibitem{steinkirch} Marina Von Steinkirch,  Templates in Latex, \url{http://mysbfiles.stonybrook.edu/~mvonsteinkir}.
\end{thebibliography}

\printindex % Print the index of words that you had indexed by \index{my-indexation}.

\end{document}


\section{Appendix}
\subsection{Acronyms used in this document}
\label{acronyms}
\begin{acronym}[Bash]
	\acro{ASR}{Automatic Speech Recognition}
	\acro{BAS}{Bavarian Archive for Speech Signals}
	\acro{CNN}{Convolutional Neural Network}
	\acro{CTC}{Connectionist Temporal Classification}
	\acro{CV}{CommonVoice}
	\acro{DS}{Deep Speech}
	\acro{E2E}{end-to-end}
	\acro{FA}[FA]{Forced Alignment}
	\acro{FHNW}{University of Applied Sciences}
	\acro{GCS}{Google Cloud Speech}
	\acro{GPU}[GPU]{Graphics Processing Unit}
	\acro{GRU}{Gated Recurrent Unit}
	\acro{GSA}{Global Sequence Alignment}
	\acro{IQR}{Inter-Quartile Range}
	\acro{LER}{Label Error Rate}
	\acro{LM}{Language Model}
	\acro{LS}{LibriSpeech}
	\acro{LSTM}{Long Short Term Memory}
	\acro{LSA}{Local Sequence Alignment}
	\acro{MFCC}{Mel-Frequency Cepstral Coefficients}
	\acro{NN}{Neural Network}
	\acro{RL}{ReadyLingua}
	\acro{RNN}{Recurrent Neural Network}
	\acro{SA}{Sequence Alignment}
	\acro{SGD}{Stochastic Gradient Descent}
	\acro{STT}{Speech-To-Text}
	\acro{OOV}{Out Of Vocabulary}
	\acro{SRILM}{the SRI Language Modelling Toolkit}
	\acro{SW}{Smith Waterman}
	\acro{VAD}{Voice Activity Detection}
	\acro{WER}{Word Error Rate}
\end{acronym}

\subsection{The simple spell checker in detail}
\label{spellchecker}

Let $S$ be a sentence as a sequence of words, $V$ the vocabulary of known words used by a \ac{LM}, $p_{LM}(g)$ the score (log-based probability, likelihood) calculated by the \ac{LM} for $n$-gram $g$, $ed(s_1,s_2)$ the \textit{Levenshtein Distance} between string $s_1$ and $s_2$ and $b$ the beam width use for beam search:

\begin{itemize}
	\item for each word $w_i \in S$ check the spelling by generating the set $W'$ of possible corrections by looking it up in $V$ as follows:
	\begin{itemize}
		\item if $w_i \in V$ its spelling is already correct and $w_i$ is kept as the only possible correction, i.e.
		\begin{equation*}
		W_i = W_i^0\{ w_i \}
		\end{equation*}
		\item if $w_i \not\in V$ generate $W_i^{'}$ as the set of all possible words $w_i^{'}$ with $ed(w_i, w_i^{'}) = 1$. This is the combined set of all possible words with one character inserted, deleted or replaced. Keep the words from this combined set that appear in $V$, i.e.
		\begin{equation*}
		W_i = W_i^{'} = \left \{ w_i^{'} \mid ed(w_i, w_i^{'}) = 1 \land w_i^{'} \in V \right \}
		\end{equation*}
		\item if $W_i^{'} = \emptyset$ generate $W_i^{''}$ as the set of all possible words $w_i^{''}$ with $ed(w_i, w_i^
		{''}2) = 2$. $W_i^{''}$ can be recursively calculated from $W_i^{''}1$. Again only keep the words that appear in $V$, i.e.
		\begin{equation*}
		W_i = W_i^{''} = \left \{ w_i^{''} \mid ed(w_i, w_i^{''}) = 2 \land w_i^{''} \in V) \right \}
		\end{equation*}
		\item if $W_i^{''} = \emptyset$ keep $w_i$ as the only word, accepting that it might be either misspelled, a wrong word, gibberish or simply has never been seen by the \ac{LM}, i.e.
		\begin{equation*}
		W_i = W_i{>2} = \left \{ w_i  \right \}
		\end{equation*}
	\end{itemize}
	\item for each possible continuation in $W_i$ build the set $P$ of likelihoods for all possible 2-grams with the possible spellings in the next word as the cartesian product of all words, i.e.
	\begin{flalign*}
	&	P = \{ p_{LM}(w_j, w_{j+1}) | w_j \in W_j \land w_{j+1} \in W_{j+1} \}{,} && W_j \in \{W_i^0, C_i^{''}, C_i^{''}, W_i^{>2} \}
	\end{flalign*}
	\item keep the $b$ 2-grams with the highest likelihood and continue with recursively with the next word
\end{itemize}

\subsection{How CTC works}
\label{ctc-summary}

This is only a very short summary of how \ac{CTC} works. Awni Hannun, one of the co-authors of the \textit{DeepSpeech} paper, wrote a very comprehensive explanation and put it online\footnote{\url{https://distill.pub/2017/ctc/}}.

In a nutshell, \ac{CTC} aligns the $T_y$ characters from a known transcript (\textit{label} or \textit{ground truth}) with the $T_x$ frames from the input audio signal during training. $T_x$ is typically much larger than $T_y$ and must not be shorter. The characters (\textit{tokens}) in the label must come from an alphabet of size $V$, which for English are the 26 lowercased ASCII characters $a..z$, the space character and the apostrophe (because this character is very common in contracted words like e.g. \textit{"don't"} or \textit{"isn't"}). Additionally, \ac{CTC} introduces a special token $\epsilon$, called the \textit{blank token}, which can be used to label unknown/silent frames or prevent collapsing (see below). Consequently, the number of characters in the alphabet used by the \ac{ASR} in this project to recognize English is $|V|=26+1+1+1=29$.

\ac{CTC} is \textit{alignment-free}, i.e. it does not require any prior alignment between the characters of a transcript and the frames of an audio signal. The only thing needed is the audio signal $X$ itself plus its ground truth $Y$. Each token in the ground truth can be aligned with any number of frames in the input signal. Vice versa, repeated sequences of the same characters can be collapsed, whereas the $\epsilon$ token acts as a boundary within sequences of a token to prevent collapsing into one, when there should be two (such as in \textit{f-f-o-o-$\epsilon$-o-o-o-o-d-d-d}, which should collapse to \textit{food} and not \textit{fod}).

For each frame input signal \ac{CTC} calculates a probability distribution over the $|V|$ characters in the alphabet. This yields a $|V| \times T_x$ probability matrix for the input signal. Because $T_x \ggg T_y$, there is usually a vast amount of different valid alignments collapsing to the same ground truth. The probability of each valid alignment can now simply be calculated by traversing the probability matrix from left to right and multiplying the probabilities of each character. Because calculating the probability of each valid alignment individually would be too slow and identical prefixes between valid alignments yield identical probabilities, a dynamic programming approach is usually chosen to calculate the probabilities whereas the intermediate probability for each prefix is saved once computed.

The most probable alignment is calculated by marginalizing (i.e. summing up) over the probabilities of the individual valid alignments. This calculation yields the CTC loss as a sum of products, which is differentiable and can therefore be optimized.

\subsection{n-Gram Language Models}\label{n-gram-summary}

\ac{LM} are probabilistic models that model the likelihood of a given sequence of characters or words. The most widely used type for word-based models \ac{LM}s are $n$-gram \ac{LM}. However, such models can estimate probabilities only for words that appear in the vocabulary of the corpus they were trained on. All other words are \ac{OOV} words with a probability of $0$. The probability of a sentence can be computed using conditional probability by calculating the probabilities of each word ($1$-grams) given all its preceding words in the sentence. Getting statistically relevant high numbers for each combination of words requires huge text corpora. However, language is dynamic and new sentences can be created all the time so that no corpus would be big enough. To handle this, $n$-grams approximate the probability of a combination of words by only considering the history of the last $n$ words ($n$ denoting the order). However, above problem is still valid for $n$-grams of any order: Because of combinatorial explosion $n$-grams suffer from sparsity with increasing order.

\subsubsection{Perplexity, discount and smoothing}

To evaluate an $n$-gram \ac{LM} a metric called \textit{perplexity} is usually used, which is the normalized inverse probability on a test set. The perplexity can be interpreted as the grade to which the \ac{LM} is "confused" by a certain $n$-gram. A high perplexity therefore corresponds to a low probability. Since the perplexity carries the probability of a certain $n$-gram in the denominator, the perplexity for \ac{OOV}-$n$-grams cannot be calculated (division by zero). To handle this efficiently, a technique called \textit{smoothing} is applied. A very rudimentary form of smoothing is \textit{Laplace Smoothing}, which assigns a minimal count of $1$ to every $n$-gram. All other counts are also increased by adding $1$. This prevents counts of zero for $n$-grams that do not appear in the training corpus. Smoothing therefore shaves off a bit of the probability mass from the known $n$-grams and moves it to the unknown $n$-grams. The factor with which the probability of a known $n$-gram is reduced is called \textit{discount}.

\subsubsection{Kneser-Ney Smoothing}

Although with Laplace Smoothing a low probability is assigned to previously unseen $n$-grams (which results in a high perplexity), it performs poorly in application because it discounts frequent $n$-grams too much (i.e. gives too much probability to unknown $n$-grams). A better way of smoothing is achieved using \textit{Kneser-Ney Smoothing}. For unseen $n$-grams, \textit{Kneser-Ney Smoothing} estimates the probability of a particular word $w$ being the continuation of a context based on the number of contexts it appears in the training corpus. For any previously unseen $n$-gram, a word that appears in only few contexts (e.g. the word \textit{Kong}, which only follows the words \textit{King} or \textit{Hong} in most corpora) will yield a lower probability than a word that has appeared in many contexts, even if the word itself may be very frequent. The intuition behind this is that such a word is assumed less likely to be the novel continuation for any new $n$-gram than a word that has already proved to be the continuation of many $n$-grams.


\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{a4}
\usepackage[cm]{fullpage}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{amsthm}
\usepackage[T1]{fontenc}
\usepackage{lmodern} % Latin modern font family
\usepackage{enumitem}
\usepackage{fitch} % http://folk.uio.no/johanw/FitchSty.html

\newtheorem*{lemma}{Lemma}
\newtheorem*{theorem}{Theorem}

\newcommand{\dotmin}{\buildrel\textstyle.\over{\hbox{\vrule height3pt depth0pt width0pt}{\smash-}}}

% Sans-serif fonts
%\usepackage[T1experimental,lm]{sfmath} % http://dtrx.de/od/tex/sfmath.html
%\renewcommand{\familydefault}{\sfdefault}

% Some configuration for listings
\renewcommand{\labelenumi}{\arabic{enumi}.}
\renewcommand{\labelenumii}{(\alph{enumii})}

\newcounter{firstcounter}
\newcommand{\labelfirst}{(\roman{firstcounter})}
%\newcommand{\spacingfirst}{\usecounter{firstcounter}\setlength{\rightmargin}{\leftmargin}}
\newcommand{\spacingfirst}{\usecounter{firstcounter}}

\newcounter{secondcounter}
\newcommand{\labelsecond}{(\arabic{secondcounter})}
%\newcommand{\spacingsecond}{\usecounter{secondcounter}\setlength{\rightmargin}{\leftmargin}}
\newcommand{\spacingsecond}{\usecounter{secondcounter}}

\title{Recursion Theory (UvA autumn 2008)\\
\normalsize{Exercises Part 11 -- Martijn Vermaat (mvermaat@cs.vu.nl)}}

%\author{Martijn Vermaat (mvermaat@cs.vu.nl)}
%\date{Updated 5th December 2005}
\date{}

\begin{document}

\maketitle

\paragraph{Exercise 3.1.17}

Given c.e. sets $A$ and $B$ prove that $B \leq_T A$ iff there is a computable
function $h$ such that for $D_y$ as in Definition 2.3.4 we have:
\begin{align*}
  x \in \overline{B} \quad \Longleftrightarrow \quad (\exists y) \; [ \; y \in W_{h(x)} \quad \& \quad D_y \subseteq \overline{A} \; ] \text{ .}
\end{align*}

\begin{proof}
($\Rightarrow$)
We code finite strings $\sigma$ over $\{0, 1\}$ as canonical indexes of
finite sets as follows:
\begin{align*}
  \sigma_y(x) = 1 \quad & \Longleftrightarrow \quad x \in D_y \text{ ,} \\
  \sigma_y(x) = 0 \quad & \Longleftrightarrow \quad x \notin D_y \quad \& \quad \exists v \! > \! x (v \in D_y) \text{ .}
\end{align*}
From $B \leq_T A$ we have $B \leq_T \overline{A}$.
Let $\chi_B = \Phi^{\overline{A}}_e$, then
\begin{align*}
  x \in \overline{B} \quad \Longleftrightarrow \quad (\exists y) \; [ \; \Phi^{\sigma_y}_e(x) = 0 \quad \& \quad \sigma_y \prec \chi_{\overline{A}} \; ] \text{ .}
\end{align*}
Since $f(y) = \sigma_y$ is computable, the relation $\Phi^{\sigma_y}_e(x) = 0$ is c.e. on $(y, e, x)$.
Let it be the domain of $\phi_i(e, x, y)$ and take $h(x) = s^2_1(i, e, x)$. We have
\begin{align*}
  \Phi^{\sigma_y}_e(x) = 0 \quad \Longleftrightarrow \quad \phi_i(e, x, y) \! \downarrow \quad \Longleftrightarrow \quad y \in W_{h(x)} \text{ .}
\end{align*}
Furthermore, $\sigma_y \prec \chi_{\overline{A}}$ implies $D_y \subseteq \overline{A}$.
However, not the other way around (a prefix of $\sigma_y$ may mis $1$'s), but this does not
matter here because it still uses only information from $\overline{A}$. Combined, we get
\begin{align*}
  (\exists y) \; [ \; \Phi^{\sigma_y}_e(x) = 0 \quad \& \quad \sigma_y \prec \chi_{\overline{A}} \; ] \quad \Longleftrightarrow \quad (\exists y) \; [ \; y \in W_{h(x)} \quad \& \quad D_y \subseteq \overline{A} \; ] \text{ .}
\end{align*}

($\Leftarrow$)
\ldots
\end{proof}

\paragraph{Exercise 3.1.18}

Consider $A = \overline{K}$ and $R^A = \{ (x,y) | x \in \overline{K} \}$ which is $A$-computable.
If there was a p.c. function $\psi$ with $\psi(x)\!\downarrow \; \Leftrightarrow \; \exists y R^A(x,y)$,
$R^A$ would be c.e. and hence also $\overline{K}$. But $\overline{K}$ is not c.e.

\paragraph{Exercise 3.1.19}

\ldots

\end{document}