\documentclass{beamer}

\mode<presentation>
{
  \usetheme{Hawke}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{multimedia}

%%%%%%
% My Commands
%%%%%%

\newcommand{\bb}{{\boldsymbol{b}}}
\newcommand{\bx}{{\boldsymbol{x}}}
\newcommand{\by}{{\boldsymbol{y}}}
\newcommand{\bfm}[1]{{\boldsymbol{#1}}}
\newcommand{\pda}[2]{\frac{\partial{#1}}{\partial{#2}}}

%%%%

\title[Lecture 22] % (optional, use only with long paper titles)
{Lecture 22 - Finite difference methods for Boundary Value Problems}

\author[I. Hawke] % (optional, use only with lots of authors)
{I.~Hawke}

\institute[University of Southampton] % (optional, but mostly needed)
{
%  \inst{1}%
  School of Mathematics, \\
  University of Southampton, UK
}

\date[Semester 1] % (optional, should be abbreviation of conference name)
{MATH3018/6141, Semester 1}

\subject{Numerical methods}

\pgfdeclareimage[height=0.5cm]{university-logo}{mathematics_7469}
\logo{\pgfuseimage{university-logo}}

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

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\section{Finite difference methods}

\subsection{Background}

\begin{frame}
  \frametitle{Boundary Value Problems}

  Considering simple boundary value problem
  \begin{equation*}
    y'' = f(x, y, y'), \quad y(a) = A, \,\, y(b) = B, \quad x \in [a,b].
  \end{equation*} \pause
  Boundary conditions are only examples here. \pause

  \vspace{1ex}

  Standard approach: first try \emph{shooting} method.  Convert BVP to
  IVP with some initial data free. Free data then modified, using root
  finding, to satisfy boundary conditions. \pause

  \vspace{1ex}

  Shooting is straightforward and efficient -- when it
  works. Otherwise use \emph{relaxation} methods based on \emph{finite
    differences}.

\end{frame}

\subsection{Finite differences for linear problems}

\begin{frame}
  \frametitle{The linear problem}

  Consider the linear problem
  \begin{equation*}
    y'' + p(x) y' + q(x) y =  f(x), \quad y(a) = A, \,\, y(b) = B,
    \quad x \in [a,b].
  \end{equation*} \pause
  %
  Introduce a \emph{grid}, evenly spaced over the interval,
  %
  \begin{equation*}
    x_i = a + i h, \quad i = 0, 1, \dots, n + 1, \quad h = \frac{b -
      a}{n + 1}.
  \end{equation*}
  %
  Contains $n$ \emph{interior} points and 2 boundary points.
  \pause The value of $y$ at the boundary points given by
  boundary conditions,
  %
  \begin{align*}
    y_0 & = y(a) = A, \\
    y_{n+1} & = y(b) = B.
  \end{align*} \pause
  %
  Value of $y$ at interior points unknown; these
  give approximate solution. Require approximation to converge to
  true solution as $h \rightarrow 0$.

\end{frame}

\begin{frame}
  \frametitle{Finite differences}

  Given grid $\{x_j\}$, approximate derivatives using standard finite
  difference formulas. \pause Typically use centred differencing
  formulas
  \begin{align*}
    y' (x_i) & = \frac{y_{i+1} - y_{i-1}}{2 h} + {\cal O} (h^2), \\
    y'' (x_i) & = \frac{y_{i+1} + y_{i-1} - 2 y_i}{h^2} + {\cal O} (h^2),
  \end{align*}
  which are second order accurate. \pause

  \vspace{1ex}

  Substituting into BVP equation
  \begin{equation*}
    y'' + p(x) y' + q(x) y =  f(x), \quad y(a) = A, \,\, y(b) = B,
    \quad x \in [a,b]
  \end{equation*}
  for \emph{interior} points gives finite difference formula ($q_i =
  q(x_i)$ etc.)
  \begin{equation*}
    y_{i-1} \left( 1 - \tfrac{h}{2} p_i \right) +  y_{i} \left( h^2
      q_i - 2 \right) + y_{i+1} \left( 1 + \tfrac{h}{2} p_i \right) =
    h^2 f_i.
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Constructing the linear system}

  Have a system of ($n+2$) linear algebraic equations
  \begin{align*}
    y_0 & = A, \\
    y_{i-1} \left( 1 - \tfrac{h}{2} p_i \right) +  y_{i} \left( h^2
      q_i - 2 \right) + y_{i+1} \left( 1 + \tfrac{h}{2} p_i \right) & =
    h^2 f_i, \quad 1 \le i \le n \\
    y_{n+1} & = B.
  \end{align*} \pause
  This is a linear system. Can simplify using the boundary conditions
  to eliminate $y_0, y_{n+1}$ everywhere.

  \vspace{1ex}

  \begin{overlayarea}{\textwidth}{0.4\textheight}
    \only<3|handout:1>
    {
      Take $n=3$ for example. In the interior
      {\small
        \begin{equation*}
          \begin{pmatrix}
            \dots & \dots & \dots \\
            1 - \tfrac{h}{2} p_2 & -2 + h^2 q_2 & 1 + \tfrac{h}{2} p_2 \\
            \dots & \dots & \dots
          \end{pmatrix}
          \begin{pmatrix}
            y_1 \\ y_2 \\ y_3
          \end{pmatrix} =
          \begin{pmatrix}
            \dots \\ h^2 f_2 \\ \dots
          \end{pmatrix}.
        \end{equation*}
      }
    }
    \only<4|handout:2>
    {
      Using boundary conditions replace $y_0, y_{n+1}$.  Full system
      is
      {\small
        \begin{equation*}
          \begin{pmatrix}
            -2 + h^2 q_1 & 1 + \tfrac{h}{2} p_1 & 0 \\
            1 - \tfrac{h}{2} p_2 & -2 + h^2 q_2 & 1 + \tfrac{h}{2} p_2 \\
            0 & 1 - \tfrac{h}{2} p_3 & -2 + h^2 q_3
          \end{pmatrix}
          \begin{pmatrix}
            y_1 \\ y_2 \\ y_3
          \end{pmatrix} =
          \begin{pmatrix}
            h^2 f_1 - A ( 1 - \tfrac{h}{2} p_1 ) \\ h^2 f_2
            \\ h^2 f_3 - B ( 1 + \tfrac{h}{2} p_3 )
          \end{pmatrix}.
        \end{equation*}
      }
    }
  \end{overlayarea}

\end{frame}

\begin{frame}
  \frametitle{Points about the linear system}

  Important point: provided central differencing is used, the matrix
  is \emph{tridiagonal}.  Very efficient algorithms for solving the
  linear system. \pause

  \vspace{1ex}

  Also note that here the matrix $T$ is not modified by the boundary
  conditions.  Always true for Dirichlet type conditions which fix
  $y(a)$.  Not true for Neumann type conditions which fix $y'(a)$,
  for example.

\end{frame}

\begin{frame}
  \frametitle{Example}

  For the problem
  \begin{equation*}
    y'' + y' + 1 = 0, \quad y(0) = 0, \,\, y(1) = 1, \quad x \in [0, 1]
  \end{equation*}
  we have $p(x) = 1$, $q(x) = 0$, $f(x) = -1$. \pause  Look at grid
  with 3 points ($n = 1, h = 1/2$). \pause Gives the equations
  \begin{align*}
    y_0 & = 0, \\
    y_0 \left( 1 - \tfrac{h}{2} \cdot 1 \right) + y_1 \left( h^2 \cdot
      0 - 2 \right) + y_2 \left( 1 + \tfrac{h}{2} \cdot 1 \right) & =
    h^2 \cdot (-1), \\
    y_2 & = 1.
  \end{align*} \pause
  %
  Boundary points $y_0, y_2$ given by boundary conditions and are
  exact. \pause Central point $y_1$ has value
  \begin{equation*}
    y_1 = \tfrac{3}{4}; \quad y_e(x = 1/2) = 0.74492.
  \end{equation*}
  Good accuracy for such a coarse grid, as $y_e(x)$ close to linear.

\end{frame}

\begin{frame}
  \frametitle{Example: 2}

  \begin{columns}
    \begin{column}{0.4\textwidth}
      The problem
      \begin{align*}
        y'' + y' + 1 &= 0, \\ y(0) &= 0, \\ y(1) &= 1, \\ x &\in [0, 1]
      \end{align*}
      solved with finite differences is impressively accurate. \pause

      \vspace{1ex}

      Increase $n$ the result converges \pause well. \pause

      \vspace{1ex}

      The convergence is second order in $h$.
    \end{column}
    \begin{column}{0.6\textwidth}
      \begin{center}
        \includegraphics<1|handout:1>[width=\textwidth]{figures/FDBVP1}
        \includegraphics<2|handout:0>[width=\textwidth]{figures/FDBVP2}
        \includegraphics<3|handout:0>[width=\textwidth]{figures/FDBVP3}
        \includegraphics<4|handout:2>[width=\textwidth]{figures/FDBVPDirichletConvergence1}
      \end{center}
    \end{column}
  \end{columns}

\end{frame}

\subsection{Boundary conditions}

\begin{frame}
  \frametitle{Boundary conditions}

  When constructing the linear system
  \begin{equation*}
    T \by = \bfm{F}
  \end{equation*}
  using Dirichlet type boundary conditions, could give value of $y_0,
  y_{n+1}$ everywhere; this only modified $\bfm{F}$.

  \vspace{1ex}

  \begin{overlayarea}{\textwidth}{0.6\textheight}
    \only<2-5|handout:1>
    {
      If instead have Neumann type boundary conditions, e.g.\
      $y'(b) = \alpha$, the situation changes.
    }
    \only<3-5|handout:1>
    {
      First need finite difference approximation of
      \emph{boundary condition itself}.
    }
    \only<4-5|handout:1>
    {
      For example could use first order differencing:
      \begin{equation*}
        \frac{y_{n+1} - y_n}{h} = \alpha.
      \end{equation*}
    }
    \only<5|handout:1>
    {
      Then rearrange to find condition on boundary point $y_{n+1}$:
      \begin{equation*}
        y_{n+1} = y_n + h \alpha.
      \end{equation*}
    }
    \only<6|handout:0>
    {
      \begin{equation*}
        y'(b) = \alpha \rightarrow y_{n+1} = y_n + h \alpha.
      \end{equation*}
      \vspace{1ex}
      Note that
      \begin{enumerate}
      \item The value of $y_{n+1}$ is now \emph{not exact} as we
        specify it by finite differences.
      \end{enumerate}
    }
    \only<7|handout:2>
    {
      \begin{equation*}
        y'(b) = \alpha \rightarrow y_{n+1} = y_n + h \alpha.
      \end{equation*}
      \vspace{1ex}
      Note that
      \begin{enumerate}
      \item The value of $y_{n+1}$ is now \emph{not exact} as we
        specify it by finite differences.
      \item Use condition on $y_{n+1}$ to construct the linear system,
        giving new terms including $y_n$; hence matrix $T$ is modified
        as well as $\bfm{F}$.
      \end{enumerate}
    }
  \end{overlayarea}
\end{frame}

\begin{frame}
  \frametitle{Example}

  For the problem
  \begin{equation*}
    y'' + y' + 1 = 0, \quad y(0) = 0, \,\, y'(1) = \tfrac{3 - e}{e -
      1}, \quad x \in [0, 1]
  \end{equation*}
  have same solution as before. \pause Look at grid with 3 points ($n
  = 1, h = 1/2$). \pause Gives equations (two match Dirichlet case)
  \begin{align*}
    y_0 & = 0, \\
    y_0 \left( 1 - \tfrac{h}{2} \cdot 1 \right) + y_1 \left( h^2 \cdot
      0 - 2 \right) + y_2 \left( 1 + \tfrac{h}{2} \cdot 1 \right) & =
    h^2 \cdot (-1), \\
    y_2 & = y_1 + h \tfrac{3 - e}{e - 1}.
  \end{align*}  \pause
  %
  Substituting in expressions for $y_0, y_2$, find central
  point $y_1$ has value
  \begin{align*}
&&    y_1 & = \tfrac{1}{3} + \tfrac{5 \alpha}{6} = 0.470; &  y_e(x =
    1/2) &= 0.74492. \\
\Rightarrow && y_2 &= 0.470 + h \alpha = 0.552; & y_e(x =
    1) &= 1.
  \end{align*}
  This result is much worse than the Dirichlet case.

\end{frame}

\begin{frame}
  \frametitle{Example: 2}

  \begin{columns}
    \begin{column}{0.4\textwidth}
      The approximate solution to
      \begin{align*}
        y'' + y' + 1 &= 0, \\ y(0) &= 0, \\ y'(1) &= \tfrac{3 - e}{e -
      1}, \\ x &\in [0, 1]
      \end{align*}
      is not as good as Dirichlet case. \pause \vspace{1ex}

      Increasing $n$ the result converges \pause eventually. \pause
      \vspace{1ex}

       Convergence only first order in $h$.
    \end{column}
    \begin{column}{0.6\textwidth}
      \begin{center}
        \includegraphics<1|handout:1>[width=\textwidth]{figures/FDBVPNeumann1}
        \includegraphics<2|handout:0>[width=\textwidth]{figures/FDBVPNeumann2}
        \includegraphics<3|handout:0>[width=\textwidth]{figures/FDBVPNeumann3}
        \includegraphics<4|handout:2>[width=\textwidth]{figures/FDBVPNeumannConvergence1}
      \end{center}
    \end{column}
  \end{columns}

\end{frame}

\begin{frame}
  \frametitle{Example: 3}

  \begin{columns}
    \begin{column}{0.4\textwidth}
      Take same problem
      \begin{align*}
        y'' + y' + 1 &= 0, \\ y(0) &= 0, \\ y'(1) &= \tfrac{3 - e}{e -
      1}, \\ x &\in [0, 1]
      \end{align*}
      using second order approximation to derivative in
      boundary condition gives better results.\pause

      \vspace{1ex}

      Increasing $n$ the result converges \pause faster. \pause

      \vspace{1ex}

      Convergence is second order in $h$.
    \end{column}
    \begin{column}{0.6\textwidth}
      \begin{center}
        \includegraphics<1|handout:1>[width=\textwidth]{figures/FDBVPNeumann2_1}
        \includegraphics<2|handout:0>[width=\textwidth]{figures/FDBVPNeumann2_2}
        \includegraphics<3|handout:0>[width=\textwidth]{figures/FDBVPNeumann2_3}
        \includegraphics<4|handout:2>[width=\textwidth]{figures/FDBVPNeumann2Convergence1}
      \end{center}
    \end{column}
  \end{columns}

\end{frame}

\subsection{Error analysis}

\begin{frame}
  \frametitle{Error analysis}

  Central differencing methods normally converge at second order.
  Examples above suggest second order convergence for the BVP. \pause

  \vspace{1ex}

  Define error vector $\bfm{e}$ and look at linear system $T
  \bfm{e}$:
  \begin{align*}
    \left( y(x_{i-1}) - y_{i-1} \right)& \left( 1 - \tfrac{h}{2} p_i
    \right) - \\
    \left( y(x_{i}) - y_{i} \right) & \left( 2 - h^2 q_i \right) + \\
    \left( y(x_{i+1}) - y_{i+1} \right) & \left( 1 + \tfrac{h}{2} p_i
    \right)
  \end{align*}
  for the interior entries. \pause

  \vspace{1ex}

  Numerical terms ($y_i$ etc.) give $-h^2 f_i$. \pause Taylor
  expanding exact solution $y(x_i)$ gives original ODE (times $h^2$)
  plus terms $\propto h^4$ and higher order in $h$, as all other terms
  cancel.

\end{frame}

\begin{frame}
  \frametitle{Error analysis: 2}

  Hence error also satisfies a linear system
  \begin{equation*}
    T \bfm{e} = h^4 \bfm{G}
  \end{equation*}
  where $T$ is the same tridiagonal matrix that defines the method and
  $\bfm{G}$ is independent of $h$. \pause

  \vspace{1ex}

  Hence bound the error using
  \begin{align*}
    \| \bfm{e} \| & = h^4 \| T^{-1} \| \cdot \| \bfm{G} \| \\
    & \le h^4 G \| T^{-1} \|
  \end{align*}
  as $\bfm{G}$ is a constant vector. \pause As $T$ is a matrix of size
  $n \propto h^{-1}$ the best bound is
  \begin{equation*}
     \| T^{-1} \| \le K n^2 \quad \implies \quad
    \| \bfm{e} \| \le \alpha h^2, \quad \alpha \text{ const.}
  \end{equation*}

\end{frame}

\section{Summary}

\subsection{Summary}

\begin{frame}
  \frametitle{Summary}

  \begin{itemize}
  \item First shoot, then relax.
  \item Finite difference methods are typically less accurate and
    efficient, and for nonlinear problems are more complex to
    implement. However, they are much more likely to work in complex
    cases.
  \item For linear problems finite difference methods convert the ODE
    to a linear system $T \by = \bfm{F}$.
    \begin{itemize}
    \item The matrix will be tri-diagonal (when using centred
      differencing).
    \item The boundary conditions are normally directly encoded in the
      known vector $\bb$ (but more complex boundary conditions may
      require modification of $A$ as well).
    \end{itemize}
  \end{itemize}

\end{frame}

\end{document}


\documentclass[sigconf,screen]{acmart}
\pdfoutput=1

% %%%%%%%%%%%%%%%%%%%%%%%%
% %Hack to remove some irrelevant copyright info.
% \settopmatter{printacmref=false} % Removes citation information below abstract
% \renewcommand\footnotetextcopyrightpermission[1]{} % removes footnote with conference information in first column
% \pagestyle{plain} % removes running headers

% \setlength{\headheight}{27pt}
% \setlength{\footskip}{14pt}

% \makeatletter
% \renewcommand\@formatdoi[1]{\ignorespaces}
% \makeatother
% %%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{natbib}
\usepackage{algorithm}
\usepackage[noend]{algorithmic}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{verbatim}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{cleveref}
\usepackage{multirow}
\usepackage{microtype}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{hyperref}
%\usepackage[
% subtle
 %moderate
%]{savetrees}
%\usepackage{blindtext}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
    citecolor=orange,
}
\urlstyle{same}
\usepackage{flushend}

%\usepackage{biblatex}
%\DeclareMathSizes{10}{9}{6}{5}
\newtheorem*{theorem*}{Theorem}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}{Proposition}[section]
\newtheorem{claim}{Claim}[section]
\newtheorem{definition}{Definition}[section]
\newtheorem{remark}{Remark}[section]
\newtheorem{observation}{Observation}[section]
\newtheorem{condition}{Condition}[section]

\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}

\newcommand{\INPUT}{\item[{\bf Input:}]}
\newcommand{\OUTPUT}{\item[{\bf Output:}]}

\newcommand{\partdiff}[2]{\frac{\partial {#1}}{\partial {#2}}}
\newcommand{\secdiff}[2]{\frac{\partial^2 {#1}}{\partial {#2}^2}}
\newcommand{\mixdiff}[3]{\frac{\partial^2 {#1}}{{\partial {#2}}{\partial {#3}}}}
\newcommand{\iprod}[2]{\left\langle {#1},
{#2} \right \rangle}
\newcommand{\etal}{et al.\ }
\newcommand{\ceil}[1]{\lceil #1 \rceil}
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\bigfloor}[1]{\left\lfloor #1 \right\rfloor}
\newcommand{\bigceil}[1]{\left\lceil #1 \right\rceil}
\newcommand{\eps}{\varepsilon}
\newcommand{\E}{\mathbf{E}}
\newcommand{\Var}{\mathbf{Var}}
\newcommand{\poly}{\mathrm{poly}}
\newcommand{\rank}{\mathrm{rank}}
\newcommand{\tr}[1]{\mathrm{Tr}\left( #1 \right)}

\newcommand{\cA}{\mathcal{A}}
\newcommand{\cI}{\mathcal{I}}
\newcommand{\cM}{\mathcal{M}}
\newcommand{\cF}{\mathcal{F}}
\newcommand{\cD}{\mathcal{D}}
\newcommand{\cP}{\mathcal{P}}
\newcommand{\cE}{{\mathcal{E}}}
\newcommand{\cX}{\mathcal{X}}
\newcommand{\cH}{{\mathcal{H}}}
\newcommand{\cR}{\mathcal{R}}
\newcommand{\cB}{\mathcal{B}}
\newcommand{\cQ}{\mathcal{Q}}
\newcommand{\cZ}{\mathcal{Z}}
\newcommand{\cS}{\mathcal{S}}
\newcommand{\RR}{{\mathbb R}}
\newcommand{\ZZ}{{\mathbb Z}}
\newcommand{\NN}{{\mathbb N}}
\newcommand{\id}[1]{\mathbbm{1}_{#1}}

\newcommand{\vx}{\mathbf{x}}
\newcommand{\vy}{\mathbf{y}}
\newcommand{\xdot}{\dot \vx}
\newcommand{\ydot}{\dot \vy}
\newcommand{\dt}{\mathrm{d}t}
\newcommand{\dx}{\mathrm{d}x}
\newcommand{\dF}{\mathrm{d}F}
\newcommand{\ddp}{\frac{\mathrm{d}}{\mathrm{d}p}}

%\newcommand{\qed}{\hfill{\rule{2mm}{2mm}}}

\def\b1{{\bf 1}}
\def\be{{\bf e}}
\def\ba{{\bf a}}
\def\bg{{\bf g}}
\def\bX{{\bf X}}
\def\bb{{\bf b}}
\def\bc{{\bf c}}
\def\bx{{\bf x}}
\def\by{{\bf y}}
\def\bv{{\bf v}}
\def\bV{{\bf V}}
\def\bw{{\bf w}}
\def\bz{{\bf z}}
\def\tx{\tilde{\bf x}}

\newcommand{\paul}[1]{{\color{blue} [[PAUL: #1]]}}
\newcommand{\aram}[1]{{\color{violet} [[ARAM: #1]]}}
\DeclareMathOperator\sech{sech}
\DeclareMathOperator\erf{erf}

\title{Elo-MMR: A Rating System for Massive Multiplayer Competitions}

%\title{An Elo-like System for Massive Multiplayer Competitions}

\author{Aram Ebtekar}
\affiliation{%
  %unaffiliated: \institution{University of British Columbia}
  \city{Vancouver}
  \state{BC}
  \country{Canada}
}
\email{aramebtech@gmail.com}

\author{Paul Liu}
\affiliation{%
  \institution{Stanford University}
  \city{Stanford}
  \state{CA}
  \country{USA}
}
\email{paul.liu@stanford.edu}

\copyrightyear{2021}
\acmYear{2021}
\setcopyright{iw3c2w3}
\acmConference[WWW '21]{Proceedings of the Web Conference 2021}{April 19--23, 2021}{Ljubljana, Slovenia}
\acmBooktitle{Proceedings of the Web Conference 2021 (WWW '21), April 19--23, 2021, Ljubljana, Slovenia}
\acmPrice{}
\acmDOI{10.1145/3442381.3450091}
\acmISBN{978-1-4503-8312-7/21/04}

\settopmatter{printacmref=true}

\begin{CCSXML}
<ccs2012>
<concept>
<concept_id>10002951.10003317.10003338.10003343</concept_id>
<concept_desc>Information systems~Learning to rank</concept_desc>
<concept_significance>500</concept_significance>
</concept>
<concept>
<concept_id>10010147.10010257.10010293.10010300</concept_id>
<concept_desc>Computing methodologies~Learning in probabilistic graphical models</concept_desc>
<concept_significance>500</concept_significance>
</concept>
</ccs2012>
\end{CCSXML}

\ccsdesc[500]{Information systems~Learning to rank}
\ccsdesc[500]{Computing me-thodologies~Learning in probabilistic graphical models}

\keywords{rating system, skill estimation, mechanism design, competition, bayesian inference, robust, incentive-compatible, elo, glicko, trueskill}

\begin{document}

%\everypar{\looseness=-1}
\linepenalty=200

\input{sections/abstract}

\maketitle
% \pagestyle{plain}

\input{sections/s1_intro}
\input{sections/s2_model}
\section{Skill estimation in two phases}
\label{sec:main-alg}
    \input{sections/s3_1_perf}
    \input{sections/s3_2_update}
\input{sections/s4_evolution}
\input{sections/s5_properties}
\input{sections/s6_experiments}
\input{sections/s7_conclusions}

% IMPORTANT: This conference is double blind, which (aside from anonymous authors) means that we cannot post links to our git or have acknowledgements.

\section*{Acknowledgements}
The authors are indebted to Daniel Sleator and Danica J. Sutherland for initial discussions that helped inspire this work, and to Nikita Gaevoy for the open-source improved TrueSkill upon which our implementation is based. Experiments in this paper are funded by a Google Cloud Research Grant. The second author is supported by a VMWare Fellowship and the Natural Sciences and Engineering Research Council of Canada.

\balance

%\appendix
%\input{sections/appendix}

\bibliographystyle{ACM-Reference-Format}
\bibliography{main}

\end{document}


\documentclass[12pt]{article}

\newcommand{\m}[1]{{\bf{#1}}}       % for matrices and vectors
\newcommand{\tr}{^{\sf T}}          % transpose

\topmargin 0in
\textheight 9in
\oddsidemargin 0pt
\evensidemargin 0pt
\textwidth 6.5in

%-------------------------------------------------------------------------------
\begin{document}
%-------------------------------------------------------------------------------

\title{User Guide for LDL, a concise sparse Cholesky package}
\author{Timothy A. Davis\thanks{
email: DrTimothyAldenDavis@gmail.com,
http://www.suitesparse.com.
This work was supported by the National
Science Foundation, under grant CCR-0203270.
Portions of the work were done while on sabbatical at Stanford University
and Lawrence Berkeley National Laboratory (with funding from Stanford
University and the SciDAC program).
}}

\date{VERSION 2.2.1, Oct 10, 2014}

\maketitle

%-------------------------------------------------------------------------------
\begin{abstract}
The {\tt LDL} software package is a set of short, concise routines for
factorizing symmetric positive-definite sparse matrices, with some
applicability to symmetric indefinite matrices.  Its primary purpose is
to illustrate much of the basic theory of sparse matrix algorithms in as
concise a code as possible, including an elegant method
of sparse symmetric factorization that computes the factorization row-by-row
but stores it column-by-column.  The entire symbolic and numeric factorization
consists of less than 50 lines of code.  The package is written in C,
and includes a MATLAB interface.
\end{abstract}
%-------------------------------------------------------------------------------

%-------------------------------------------------------------------------------
\section{Overview}
%-------------------------------------------------------------------------------

{\tt LDL} is a set of short, concise routines that compute the $\m{LDL}\tr$
factorization of a sparse symmetric matrix $\m{A}$.  Its primary purpose is
to illustrate much of the basic theory of sparse matrix algorithms in as
compact a code as possible, including an elegant method of
sparse symmetric factorization (related to \cite{Liu86c,Liu91}).
The lower triangular factor $\m{L}$ is computed row-by-row, in contrast to the
conventional column-by-column method.
Although it does not achieve the same level of performance
as methods based on dense matrix kernels
(such as \cite{NgPeyton93,RothbergGupta91}),
its performance is competitive with column-by-column methods that do not
use dense kernels \cite{GeorgeLiu79, GeorgeLiu, GilbertMolerSchreiber}.

Section~\ref{Algorithm} gives a brief description of the algorithm
used in the symbolic and numeric factorization.  A more detailed tutorial-level
discussion may be found in \cite{Stewart03}.  Details
of the concise implementation of this method are given in
Section~\ref{Implementation}.  Sections~\ref{MATLAB}~and~\ref{C} give an
overview of how to use the package in MATLAB and in a stand-alone C program.

%-------------------------------------------------------------------------------
\section{Algorithm}
\label{Algorithm}
%-------------------------------------------------------------------------------

The underlying numerical algorithm is described below.  The $k$th
step solves a lower triangular system of dimension $k-1$ to compute the
$k$th row of $\m{L}$ and the $d_{kk}$ entry of the diagonal matrix $\m{D}$.
Colon notation is used for submatrices.  For example,
$\m{L}_{k,1:k-1}$ refers to the first $k-1$ columns of
the $k$th row of $\m{L}$.  Similarly, $\m{L}_{1:k-1,1:k-1}$ refers to
the leading $(k-1)$-by-$(k-1)$ submatrix of $\m{L}$.
%---------------
\vspace{-0.2in}
\begin{tabbing}
\hspace{2em} \= \hspace{2em} \= \hspace{2em} \= \\
{\bf Algorithm~1
($\m{LDL}\tr$ factorization of a $n$-by-$n$ symmetric matrix $\m{A}$)} \\
\> {\bf for} $k = 1$ {\bf to} $n$ \\
\>\> (step 1) Solve $\m{L}_{1:k-1,1:k-1}\m{y} = \m{A}_{1:k-1,k}$ for $\m{y}$ \\
\>\> (step 2) $\m{L}_{k,1:k-1} = (\m{D}_{1:k-1,1:k-1}^{-1} \m{y})\tr$ \\
\>\> (step 3) $l_{kk} = 1$ \\
\>\> (step 4) $d_{kk} = a_{kk} - \m{L}_{k,1:k-1}\m{y}$ \\
\> {\bf end for}
\end{tabbing}
%---------------

The algorithm computes an $\m{LDL}\tr$ factorization without numerical pivoting.
It can thus factorize any symmetric positive definite matrix, and any
symmetric indefinite matrix whose leading minors are all well-conditioned.

When $\m{A}$ and $\m{L}$ are sparse, step 1 of Algorithm~1 requires a
triangular solve of the form $\m{Lx}=\m{b}$, where all three terms in
the equation are sparse.  This is the most costly step of the Algorithm.
Steps 2 through 4 are fairly straightforward.

Let ${\cal X}$ and ${\cal B}$ refer to the set of indices of nonzero entries
in $\m{x}$ and $\m{b}$, respectively, in the lower triangular system
$\m{Lx}=\m{b}$.  To compute $\m{x}$ efficiently
the nonzero pattern ${\cal X}$ must be found first.
In the general case when $\m{L}$ is arbitrary \cite{GilbertPeierls88},
the nonzero
pattern ${\cal X}$ is the set of nodes reachable via paths in the graph $G_L$
from all nodes in the set ${\cal B}$, and where the graph $G_L$ has
$n$ nodes and a directed edge $(j,i)$ if and only if $l_{ij}$ is nonzero.
To compute the numerical solution to $\m{Lx}=\m{b}$ by accessing the columns of
$\m{L}$ one at a time, ${\cal X}$ can be traversed
in any topological order of the subgraph of $G_L$ consisting of nodes in
${\cal X}$.  That is, $x_j$ must be computed before $x_i$ if there is a path
from $j$ to $i$ in $G_L$.  The natural order ($1, 2, \ldots, n$) is one such
ordering, but that requires a costly sort of ${\cal X}$.
With a graph traversal and topological sort, the solution of $\m{Lx}=\m{b}$
can be computed using Algorithm~2 below.
The computation of ${\cal X}$ and $\m{x}$ both take
time proportional to the floating-point operation count.
%---------------
\vspace{-0.2in}
\begin{tabbing}
\hspace{2em} \= \hspace{2em} \= \hspace{2em} \= \\
{\bf Algorithm~2
(Solve $\m{Lx}=\m{b}$, where $\m{L}$ is lower triangular with unit diagonal)} \\
\> ${\cal X} = \mbox{Reach}_{G_L} ({\cal B})$ \\
\> $\m{x} = \m{b}$ \\
\> {\bf for} $i \in {\cal X}$ in any topological order \\
\>\> $\m{x}_{i+1:n} = \m{x}_{i+1:n} - \m{L}_{i+1:n,i} x_i$ \\
\> {\bf end for}
\end{tabbing}
%---------------

The general result also governs the pattern of $\m{y}$ in Algorithm~1.
However, in this case $\m{L}$ arises from a sparse Cholesky factorization,
and is governed by the elimination tree \cite{Liu90a}.
A general graph traversal is not required.
In the elimination tree, the parent of node $i$ is the smallest $j > i$
such that $l_{ji}$ is nonzero.  Node $i$ has no parent if column $i$ of
$\m{L}$ is completely zero below the diagonal; $i$ is a root of the
elimination tree in this case.  The nonzero pattern of $\m{x}$ is the
union of all the nodes on the paths from any node $i$ (where $b_i$ is nonzero) to the
root of the elimination tree \cite[Thm 2.4]{Liu86c}.  It is referred to here as a tree,
but in general it can be a forest.

Rather than a general topological sort of the subgraph of $G_L$ consisting
nodes reachable from nodes in ${\cal B}$, a simpler
tree traversal can be used.  First, select any nonzero entry $b_i$
and follow the path from $i$ to the root of tree.
Nodes along this path are marked and placed in a stack,
with $i$ at the top of the
stack and the root at the bottom.
Repeat for every other nonzero entry in $b_i$, in arbitrary order, but stop
just before reaching a marked node (the result can be empty if $i$ is already
in the stack).  The stack now contains ${\cal X}$, a topological ordering of
the nonzero pattern of $\m{x}$, which can be used in Algorithm~2 to solve
$\m{Lx}=\m{b}$.  The time to compute ${\cal X}$
using an elimination tree traversal is much faster than the general graph
traversal, taking time proportional to the size of ${\cal X}$ rather than the
number of floating-point operations required to compute $\m{x}$.

In the $k$th step of the factorization, the set ${\cal X}$ becomes the
nonzero pattern of row $k$ of $\m{L}$.  This step requires the elimination
tree of $\m{L}_{1:k-1,1:k-1}$, and must construct the elimination tree of
$\m{L}_{1:k,1:k}$ for step $k+1$.  Recall that the parent of $i$ in the
tree is the smallest $j$ such that $i < j$ and $l_{ji} \ne 0$.
Thus, if any node $i$ already has a parent $j$, then $j$ will remain the
parent of $i$ in the elimination trees of all other larger leading submatrices
of $\m{L}$, and in the elimination tree of $\m{L}$ itself.
If $l_{ki} \ne 0$ and $i$ does not have a parent in the elimination tree of
$\m{L}_{1:k-1,1:k-1}$, then the parent of $i$ is $k$
in the elimination tree of $\m{L}_{1:k,1:k}$.
Node $k$ becomes the parent of any node $i \in {\cal X}$ that does not yet
have a parent.

Since Algorithm~2 traverses $\m{L}$ in column order, $\m{L}$ is stored in a
conventional sparse column representation.  Each column $j$ is stored as a list
of nonzero values and their corresponding row indices.  When row $k$ is
computed, the new entries can be placed at the end of each list.  As
a by-product of computing $\m{L}$ one row at a time,
the columns of $\m{L}$ are computed in a sorted manner.  This is a convenient
form of the output.
MATLAB requires the columns of its sparse matrices to be sorted, for example.
Sorted columns improve the speed of Algorithm~2, since the memory access
pattern is more regular.  The conventional column-by-column algorithm
\cite{GeorgeLiu79,GeorgeLiu} does not produce columns of $\m{L}$ with
sorted row indices.

A simple symbolic pre-analysis can be obtained by repeating the subtree traversals.
All that is required to compute the nonzero pattern of
the $k$th row of $\m{L}$ is the partially constructed elimination tree
and the nonzero pattern of the $k$th column of $\m{A}$.  This is computed
in time proportional to the size of this set, using the elimination tree
traversal.  Once constructed, the number of nonzeros in each column of
$\m{L}$ is incremented, for each entry in ${\cal X}$, and then ${\cal X}$
is discarded.  The set ${\cal X}$ need not be constructed in topological
order, so no stack is required.  The run time of the symbolic analysis
algorithm is thus proportional to the number of nonzeros in $\m{L}$.
This is more costly than the optimal algorithm \cite{GilbertNgPeyton94},
which takes time essentially proportional to the number of nonzeros in $\m{A}$.
The memory requirements are just the matrix $\m{A}$ and a few size-$n$ integer
arrays.  The result of the algorithm is the elimination tree, a count
of the number of nonzeros in each column of $\m{L}$, and
the cumulative sum of the column counts.

%-------------------------------------------------------------------------------
\section{Implementation}
\label{Implementation}
%-------------------------------------------------------------------------------

Because of its simplicity, the implementation of this algorithm leads to
a very short, concise code.  The symbolic analysis routine {\tt ldl\_symbolic}
shown in Figure~\ref{ldlsymbolic}
consists of only 18 lines of executable C code.
This includes 5 lines of code to allow for a
sparsity-preserving ordering $\m{P}$ so that either $\m{A}$ or $\m{PAP}\tr$
can be analyzed, 3 lines of code to compute the cumulative sum of
the column counts, and one line of code to speed up a {\tt for} loop.
An additional line of code allows for a more general form of the input
sparse matrix $\m{A}$.

The {\tt n}-by-{\tt n} sparse matrix $\m{A}$ is provided in compressed column
form as an {\tt int} array {\tt Ap} of length {\tt n+1},
an {\tt int} array {\tt Ai} of length {\tt nz},
and a {\tt double} array {\tt Ax} also of length {\tt nz},
where {\tt nz} is the number of entries in the matrix.
The numerical values of entries in column $j$ are stored in
{\tt Ax[Ap[j]} $\ldots$ {\tt Ap[j+1]-1]}
and the corresponding row indices are in
{\tt Ai[Ap[j]} $\ldots$ {\tt Ap[j+1]-1]}.
With {\tt Ap[0] = 0}, the number of entries in the matrix is {\tt nz = Ap[n]}.
If no fill-reducing ordering {\tt P} is provided,
only entries in the upper triangular part of $\m{A}$ are considered.
If {\tt P} is provided and row/column {\tt i} of the
matrix $\m{A}$ is the {\tt k}-th row/column of $\m{PAP}\tr$, then {\tt P[k]=i}.
Only entries in the upper
triangular part of $\m{PAP}\tr$ are considered.  These entries may be
in the lower triangular part of $\m{A}$, so to ensure that the correct matrix
is factorized, all entries of $\m{A}$ should be provided when using the
permutation input {\tt P}.

The outputs of {\tt ldl\_symbolic} are three size-{\tt n} arrays:
{\tt Parent} holds the elimination tree,
{\tt Lnz} holds the counts of the number of entries in each column of
$\m{L}$, and
{\tt Lp} holds the cumulative sum of {\tt Lnz}.
The size-{\tt n} array {\tt Flag} is used as workspace.
None of the output or workspace arrays need to be initialized.

\begin{figure}
\caption{{\tt ldl\_symbolic:} finding the elimination tree and column counts}
\label{ldlsymbolic}
{\scriptsize
\begin{verbatim}
void ldl_symbolic
(
    int n,              /* A and L are n-by-n, where n >= 0 */
    int Ap [ ],         /* input of size n+1, not modified */
    int Ai [ ],         /* input of size nz=Ap[n], not modified */
    int Lp [ ],         /* output of size n+1, not defined on input */
    int Parent [ ],     /* output of size n, not defined on input */
    int Lnz [ ],        /* output of size n, not defined on input */
    int Flag [ ],       /* workspace of size n, not defn. on input or output */
    int P [ ],          /* optional input of size n */
    int Pinv [ ]        /* optional output of size n (used if P is not NULL) */
)
{
    int i, k, p, kk, p2 ;
    if (P)
    {
        /* If P is present then compute Pinv, the inverse of P */
        for (k = 0 ; k < n ; k++)
        {
            Pinv [P [k]] = k ;
        }
    }
    for (k = 0 ; k < n ; k++)
    {
        /* L(k,:) pattern: all nodes reachable in etree from nz in A(0:k-1,k) */
        Parent [k] = -1 ;           /* parent of k is not yet known */
        Flag [k] = k ;              /* mark node k as visited */
        Lnz [k] = 0 ;               /* count of nonzeros in column k of L */
        kk = (P) ? (P [k]) : (k) ;  /* kth original, or permuted, column */
        p2 = Ap [kk+1] ;
        for (p = Ap [kk] ; p < p2 ; p++)
        {
            /* A (i,k) is nonzero (original or permuted A) */
            i = (Pinv) ? (Pinv [Ai [p]]) : (Ai [p]) ;
            if (i < k)
            {
                /* follow path from i to root of etree, stop at flagged node */
                for ( ; Flag [i] != k ; i = Parent [i])
                {
                    /* find parent of i if not yet determined */
                    if (Parent [i] == -1) Parent [i] = k ;
                    Lnz [i]++ ;                         /* L (k,i) is nonzero */
                    Flag [i] = k ;                      /* mark i as visited */
                }
            }
        }
    }
    /* construct Lp index array from Lnz column counts */
    Lp [0] = 0 ;
    for (k = 0 ; k < n ; k++)
    {
        Lp [k+1] = Lp [k] + Lnz [k] ;
    }
}
\end{verbatim}
}
\end{figure}

The {\tt ldl\_numeric} numeric factorization routine shown
in Figure~\ref{ldlnumeric} consists of only 31 lines of
executable code.  It includes this same subtree traversal algorithm
as {\tt ldl\_symbolic},
except that each path is placed on a stack that holds
nonzero pattern of the $k$th row of $\m{L}$.
This traversal is followed by a sparse forward solve
using this pattern, and all of the nonzero entries in
the resulting $k$th row of $\m{L}$ are appended to their respective columns
in the data structure of $\m{L}$.

\begin{figure}
\caption{{\tt ldl\_numeric:} numeric factorization}
\label{ldlnumeric}
{\scriptsize
\begin{verbatim}
int ldl_numeric         /* returns n if successful, k if D (k,k) is zero */
(
    int n,              /* A and L are n-by-n, where n >= 0 */
    int Ap [ ],         /* input of size n+1, not modified */
    int Ai [ ],         /* input of size nz=Ap[n], not modified */
    double Ax [ ],      /* input of size nz=Ap[n], not modified */
    int Lp [ ],         /* input of size n+1, not modified */
    int Parent [ ],     /* input of size n, not modified */
    int Lnz [ ],        /* output of size n, not defn. on input */
    int Li [ ],         /* output of size lnz=Lp[n], not defined on input */
    double Lx [ ],      /* output of size lnz=Lp[n], not defined on input */
    double D [ ],       /* output of size n, not defined on input */
    double Y [ ],       /* workspace of size n, not defn. on input or output */
    int Pattern [ ],    /* workspace of size n, not defn. on input or output */
    int Flag [ ],       /* workspace of size n, not defn. on input or output */
    int P [ ],          /* optional input of size n */
    int Pinv [ ]        /* optional input of size n */
)
{
    double yi, l_ki ;
    int i, k, p, kk, p2, len, top ;
    for (k = 0 ; k < n ; k++)
    {
        /* compute nonzero Pattern of kth row of L, in topological order */
        Y [k] = 0.0 ;               /* Y(0:k) is now all zero */
        top = n ;                   /* stack for pattern is empty */
        Flag [k] = k ;              /* mark node k as visited */
        Lnz [k] = 0 ;               /* count of nonzeros in column k of L */
        kk = (P) ? (P [k]) : (k) ;  /* kth original, or permuted, column */
        p2 = Ap [kk+1] ;
        for (p = Ap [kk] ; p < p2 ; p++)
        {
            i = (Pinv) ? (Pinv [Ai [p]]) : (Ai [p]) ;   /* get A(i,k) */
            if (i <= k)
            {
                Y [i] += Ax [p] ;  /* scatter A(i,k) into Y (sum duplicates) */
                for (len = 0 ; Flag [i] != k ; i = Parent [i])
                {
                    Pattern [len++] = i ;   /* L(k,i) is nonzero */
                    Flag [i] = k ;          /* mark i as visited */
                }
                while (len > 0) Pattern [--top] = Pattern [--len] ;
            }
        }
        /* compute numerical values kth row of L (a sparse triangular solve) */
        D [k] = Y [k] ;             /* get D(k,k) and clear Y(k) */
        Y [k] = 0.0 ;
        for ( ; top < n ; top++)
        {
            i = Pattern [top] ;     /* Pattern [top:n-1] is pattern of L(:,k) */
            yi = Y [i] ;            /* get and clear Y(i) */
            Y [i] = 0.0 ;
            p2 = Lp [i] + Lnz [i] ;
            for (p = Lp [i] ; p < p2 ; p++)
            {
                Y [Li [p]] -= Lx [p] * yi ;
            }
            l_ki = yi / D [i] ;     /* the nonzero entry L(k,i) */
            D [k] -= l_ki * yi ;
            Li [p] = k ;            /* store L(k,i) in column form of L */
            Lx [p] = l_ki ;
            Lnz [i]++ ;             /* increment count of nonzeros in col i */
        }
        if (D [k] == 0.0) return (k) ;      /* failure, D(k,k) is zero */
    }
    return (n) ;        /* success, diagonal of D is all nonzero */
}
\end{verbatim}
}
\end{figure}

After the matrix is factorized, the {\tt ldl\_lsolve}, {\tt ldl\_dsolve},
and {\tt ldl\_ltsolve} routines shown in Figure~\ref{ldlsolve}
are provided to solve
$\m{Lx}=\m{b}$, $\m{Dx}=\m{b}$, and $\m{L}\tr\m{x}=\m{b}$, respectively.
Together, they solve $\m{Ax}=\m{b}$, and consist of only 10 lines of executable
code.  If a fill-reducing permutation is used,
{\tt ldl\_perm} and {\tt ldl\_permt} must be used to permute $\m{b}$ and
$\m{x}$ accordingly.

\begin{figure}
\caption{Solve routines}
\label{ldlsolve}
{\scriptsize
\begin{verbatim}
void ldl_lsolve
(
    int n,              /* L is n-by-n, where n >= 0 */
    double X [ ],       /* size n.  right-hand-side on input, soln. on output */
    int Lp [ ],         /* input of size n+1, not modified */
    int Li [ ],         /* input of size lnz=Lp[n], not modified */
    double Lx [ ]       /* input of size lnz=Lp[n], not modified */
)
{
    int j, p, p2 ;
    for (j = 0 ; j < n ; j++)
    {
        p2 = Lp [j+1] ;
        for (p = Lp [j] ; p < p2 ; p++)
        {
            X [Li [p]] -= Lx [p] * X [j] ;
        }
    }
}

void ldl_dsolve
(
    int n,              /* D is n-by-n, where n >= 0 */
    double X [ ],       /* size n.  right-hand-side on input, soln. on output */
    double D [ ]        /* input of size n, not modified */
)
{
    int j ;
    for (j = 0 ; j < n ; j++)
    {
        X [j] /= D [j] ;
    }
}

void ldl_ltsolve
(
    int n,              /* L is n-by-n, where n >= 0 */
    double X [ ],       /* size n.  right-hand-side on input, soln. on output */
    int Lp [ ],         /* input of size n+1, not modified */
    int Li [ ],         /* input of size lnz=Lp[n], not modified */
    double Lx [ ]       /* input of size lnz=Lp[n], not modified */
)
{
    int j, p, p2 ;
    for (j = n-1 ; j >= 0 ; j--)
    {
        p2 = Lp [j+1] ;
        for (p = Lp [j] ; p < p2 ; p++)
        {
            X [j] -= Lx [p] * X [Li [p]] ;
        }
    }
}
\end{verbatim}
}
\end{figure}

In addition to appearing as a Collected Algorithm of the ACM \cite{Davis05},
{\tt LDL} is available at http://www.suitesparse.com.

%-------------------------------------------------------------------------------
\section{Using LDL in MATLAB}
\label{MATLAB}
%-------------------------------------------------------------------------------

The simplest way to use {\tt LDL} is within MATLAB.  Once the {\tt ldlsparse}
mexFunction is compiled and installed, the MATLAB statement
{\tt [L, D, Parent, fl] = ldlsparse (A)} returns the sparse factorization
{\tt A = (L+I)*D*(L+I)'}, where {\tt L} is lower triangular, {\tt D} is a
diagonal matrix, and {\tt I} is the {\tt n}-by-{\tt n}
identity matrix ({\tt ldlsparse} does not return the unit diagonal of {\tt L}).
The elimination tree is returned in {\tt Parent}.
If no zero on the diagonal of {\tt D} is encountered, {\tt fl} is the
floating-point operation count.  Otherwise, {\tt D(-fl,-fl)} is the first
zero entry encountered.  Let {\tt d=-fl}.  The function returns the
factorization of {\tt A (1:d,1:d)}, where rows {\tt d+1} to {\tt n} of {\tt L}
and {\tt D} are all zero.  If a sparsity preserving permutation {\tt P} is
passed, {\tt [L, D, Parent, fl] = ldlsparse (A,P)}
operates on {\tt A(P,P)} without
forming it explicitly.

The statement {\tt x = ldlsparse (A, [ ], b)} is roughly equivalent to
{\tt x = A}$\backslash${\tt b}, when {\tt A} is sparse, real, and symmetric.
The $\m{LDL}\tr$ factorization of {\tt A} is performed.  If {\tt P} is
provided, {\tt x = ldlsparse (A, P, b)} still performs
{\tt x = A}$\backslash${\tt b}, except that {\tt A(P,P)} is factorized
instead.

%-------------------------------------------------------------------------------
\section{Using LDL in a C program}
\label{C}
%-------------------------------------------------------------------------------

The C-callable {\tt LDL} library consists of nine user-callable routines
and one include file.

\begin{itemize}
\item {\tt ldl\_symbolic}:  given the nonzero pattern of a sparse symmetric
    matrix $\m{A}$ and an optional permutation $\m{P}$, analyzes either
    $\m{A}$ or $\m{PAP}\tr$, and returns the elimination tree, the
    number of nonzeros in each column of $\m{L}$, and the {\tt Lp} array
    for the sparse matrix data structure for $\m{L}$.
    Duplicate entries are allowed in the columns of $\m{A}$, and the
    row indices in each column need not be sorted.
    Providing a sparsity-preserving ordering is critical for obtaining
    good performance.  A minimum degree ordering
    (such as AMD \cite{AmestoyDavisDuff96,AmestoyDavisDuff03})
    or a graph-partitioning based ordering are appropriate.
\item {\tt ldl\_numeric}:  given {\tt Lp} and the elimination tree computed
    by {\tt ldl\_symbolic}, and an optional permutation $\m{P}$,
    returns the numerical factorization of $\m{A}$ or $\m{PAP}\tr$.
    Duplicate entries are allowed in the columns of $\m{A}$
    (any duplicate entries are summed), and the
    row indices in each column need not be sorted.
    The data structure for $\m{L}$ is the same as $\m{A}$, except that
    no duplicates appear, and each column has sorted row indices.
\item {\tt ldl\_lsolve}:  given the factor $\m{L}$ computed by
    {\tt ldl\_numeric}, solves the linear system $\m{Lx}=\m{b}$, where
    $\m{x}$ and $\m{b}$ are full $n$-by-1 vectors.
\item {\tt ldl\_dsolve}:  given the factor $\m{D}$ computed by
    {\tt ldl\_numeric}, solves the linear system $\m{Dx}=\m{b}$.
\item {\tt ldl\_ltsolve}:  given the factor $\m{L}$ computed by
    {\tt ldl\_numeric}, solves the linear system $\m{L}\tr\m{x}=\m{b}$.
\item {\tt ldl\_perm}: given a vector $\m{b}$ and a permutation $\m{P}$,
    returns $\m{x}=\m{Pb}$.
\item {\tt ldl\_permt}: given a vector $\m{b}$ and a permutation $\m{P}$,
    returns $\m{x}=\m{P}\tr\m{b}$.
\item {\tt ldl\_valid\_perm}:  Except for checking if the diagonal of
    $\m{D}$ is zero, none of the above routines check their inputs for errors.
    This routine checks the validity of a permutation $\m{P}$.
\item {\tt ldl\_valid\_matrix}:  checks if a matrix $\m{A}$ is valid as input
    to {\tt ldl\_symbolic} and {\tt ldl\_numeric}.
\end{itemize}

Note that the primary input to the {\tt ldl\_symbolic} and
{\tt ldl\_numeric} is the sparse matrix $\m{A}$.  It is provided in
column-oriented form, and only the upper triangular part is accessed.
This is slightly different than the primary output: the matrix $\m{L}$,
which is lower triangular in column-oriented form.
If you wish to factorize a symmetric matrix $\m{A}$ for which only the lower
triangular part is supplied, you would need to transpose $\m{A}$ before
passing it {\tt ldl\_symbolic} and {\tt ldl\_numeric}.

An additional set of routines is available for use in a 64-bit environment.
Each routine name changes uniformly; {\tt ldl\_symbolic} becomes
{\tt ldl\_l\_symbolic}, and each {\tt int} parameter becomes type
{\tt SuiteSparse\_long}.  The {\tt SuiteSparse\_long} type is {\tt long}, except for
Microsoft Windows 64, where it becomes {\tt \_\_int64}.

\begin{figure}
\caption{Example of use}
\label{ldlsimple}
{\scriptsize
\begin{verbatim}
#include <stdio.h>
#include "ldl.h"
#define N 10    /* A is 10-by-10 */
#define ANZ 19  /* # of nonzeros on diagonal and upper triangular part of A */
#define LNZ 13  /* # of nonzeros below the diagonal of L */

int main (void)
{
    /* only the upper triangular part of A is required */
    int    Ap [N+1] = {0, 1, 2, 3, 4,   6, 7,   9,   11,      15,     ANZ},
           Ai [ANZ] = {0, 1, 2, 3, 1,4, 5, 4,6, 4,7, 0,4,7,8, 1,4,6,9 } ;
    double Ax [ANZ] = {1.7, 1., 1.5, 1.1, .02,2.6, 1.2, .16,1.3, .09,1.6,
                     .13,.52,.11,1.4, .01,.53,.56,3.1},
           b [N] = {.287, .22, .45, .44, 2.486, .72, 1.55, 1.424, 1.621, 3.759};
    double Lx [LNZ], D [N], Y [N] ;
    int Li [LNZ], Lp [N+1], Parent [N], Lnz [N], Flag [N], Pattern [N], d, i ;

    /* factorize A into LDL' (P and Pinv not used) */
    ldl_symbolic (N, Ap, Ai, Lp, Parent, Lnz, Flag, NULL, NULL) ;
    printf ("Nonzeros in L, excluding diagonal: %d\n", Lp [N]) ;
    d = ldl_numeric (N, Ap, Ai, Ax, Lp, Parent, Lnz, Li, Lx, D, Y, Pattern,
        Flag, NULL, NULL) ;

    if (d == N)
    {
        /* solve Ax=b, overwriting b with the solution x */
        ldl_lsolve (N, b, Lp, Li, Lx) ;
        ldl_dsolve (N, b, D) ;
        ldl_ltsolve (N, b, Lp, Li, Lx) ;
        for (i = 0 ; i < N ; i++) printf ("x [%d] = %g\n", i, b [i]) ;
    }
    else
    {
        printf ("ldl_numeric failed, D (%d,%d) is zero\n", d, d) ;
    }
    return (0) ;
}
\end{verbatim}
}
\end{figure}

The program in Figure~\ref{ldlsimple}
illustrates the basic usage of the {\tt LDL} routines.
It analyzes and factorizes the sparse symmetric positive-definite matrix
{\small
\[
\m{A} = \left[
\begin{array}{cccccccccc}
      1.7 &   0 &   0 &   0 &   0 &   0 &   0 &   0 & .13 &   0 \\
        0 &  1. &   0 &   0 & .02 &   0 &   0 &   0 &   0 & .01 \\
        0 &   0 & 1.5 &   0 &   0 &   0 &   0 &   0 &   0 &   0 \\
        0 &   0 &   0 & 1.1 &   0 &   0 &   0 &   0 &   0 &   0 \\
        0 & .02 &   0 &   0 & 2.6 &   0 & .16 & .09 & .52 & .53 \\
        0 &   0 &   0 &   0 &   0 & 1.2 &   0 &   0 &   0 &   0 \\
        0 &   0 &   0 &   0 & .16 &   0 & 1.3 &   0 &   0 & .56 \\
        0 &   0 &   0 &   0 & .09 &   0 &   0 & 1.6 & .11 &   0 \\
      .13 &   0 &   0 &   0 & .52 &   0 &   0 & .11 & 1.4 &   0 \\
        0 & .01 &   0 &   0 & .53 &   0 & .56 &   0 &   0 & 3.1 \\
\end{array}
\right]
\]
}
and then solves a system $\m{Ax}=\m{b}$ whose true solution is
$x_i = i/10$.  Note that {\tt Li} and {\tt Lx} are statically allocated.
Normally they would be allocated after their size, {\tt Lp[n]},
is determined by {\tt ldl\_symbolic}.
More example programs are included with the {\tt LDL} package.

\section{Acknowledgments}

I would like to thank Pete Stewart for his comments on an earlier draft
of this software and its accompanying paper.

\newpage
\bibliographystyle{plain}
\bibliography{ldl}

\end{document}


\section{Optimizing EvtGen code}
\label{sect:optimization}
\index{optimization}

Broadly speaking there are two different categories of
optimization. The firt type concerns just making the code
more efficient, i.e., there are no changes to the algorithm.
A very simple example of such optimization is the order in
which operations are performed, let a be a double and p and k
four-vectors.
\begin{verbatim}
   a*(p*k);
\end{verbatim}
is more efficient then
\begin{verbatim}
   (a*p)*k;
\end{verbatim}


\section{Updates: 04/13/2020}%
\label{sec:updates_2020_04_13}
%
\subsection{Changes to Network}%
\label{subsec:changes_to_network}
%
\begin{itemize}
  \item Use Cartesian representation \(\left[{\cos\phi_\mu(x),
    \sin\phi_\mu(x)}\right]\) instead of angular representation \(\phi_{\mu}(x)
    \in [{0, 2\pi})\).
    \begin{itemize}
      \item While this doubles the size of our inputs, it avoids complications
        that arise from angles near \(0\) and \(2\pi\).
    \end{itemize}
  % \item \color{blue}{Under this new representation, the bias in the average
  %   plaquette no longer seems to be an issue.}\color{black}
  % \item However, there is no noticeable improvement in the tunneling rate when
  %   compared to generic HMC.\@
  % \color{red}{\item \textbf{TODO:}}\color{black}
  % \begin{todolist}
  %   \item Continue testing with additional hidden layers.
  %   \item Add convolutional/pooling layers at beginning of network to ensure
  %     translational invariance.
  % \end{todolist}
\end{itemize}

\subsection{Changes to the loss function}%
\label{subsec:changes_to_loss_fn}
%
Since our main goal is to obtain a sampler that is able to efficiently sample
from different topological sectors, we can design a loss function around this
idea.
%
Recall that the topological charge \(\Q \in \mathbb{Z}\) is computed as
%
\begin{equation}
  \mathcal{Q} \equiv \frac{1}{2\pi}\sum_{\substack{{x; \mu, \nu}\\{\nu > \mu}}}
    \sin\left(\phi_{\mu\nu}(x)\right)
\end{equation}
%
for
%
\begin{equation}
  \phi_{\mu\nu}(x) = \phi_{\mu}(x) + \phi_{\nu}(x+\hat{\mu}) -
  \phi_{\mu}(x+\hat{\nu}) - \phi_{\nu}(x)
\end{equation}
%
Instead of maximizing the expected squared jump distance (ESJD) between
configurations, it makes more sense to maximize quantities related to the
plaquette sums, e.g.\ the \emph{plaquette distance}, \(\delta_{P}(\xip, \xi)\)
%
\begin{equation}
  \delta_{P}\left(\xip, \xi\right)
  = \sum 1 - \cos\left(\phi^{\prime}_{\mu\nu}(x) - \phi_{\mu\nu}(x)\right) \\
  % \hspace{6em}\text{
  %   (\emph{plaquette distance})
  % }\\
  \label{eq:plaq_diff}
\end{equation}
  % \vspace{1em}
  % -----------------------------------------------
or the topological charge difference squared, \(\delta_{\Q}(\xip, \xi)\)
%
\begin{align}
  \delta_{\Q}(\xip, \xi)
  &= \bigg[\overbrace{\frac{1}{2\pi}\sum
    \sin\left(\phi^{\prime}_{\mu\nu}(x)\right)}^{\Q^{\prime}}
  - \overbrace{\frac{1}{2\pi}\sum
    \sin\left(\phi_{\mu\nu}(x)\right)}^{\Q}\bigg]^{2} \\
  &= {(\Q^{\prime} - \Q)}^2
  % \hspace{10.6em}\text{
  %   (\emph{topological charge difference})
  % }%
  \label{eq:charge_diff}
\end{align}
%
where \(\phi_{\mu\nu}^{\prime}(x)\) denotes the proposed configuration (before
applying Metropolis-Hastings accept/reject).
%
From these we can then define
%
\begin{align}
  \ell_{\lambda_{P}}\left(\xip, \xi, A(\xip|\xi)\right)
  &= \frac{\lambda_{P}^{2}}{\delta_{P}\cdot A(\xi^{\prime}|\xi)}
    -  \frac{\delta_{P}\cdot A(\xi^{\prime}|\xi)}{\lambda_{P}^{2}} \\
  \ell_{\lambda_{\Q}}\left(\xip, \xi, A(\xip|\xi)\right)
  &= \frac{\lambda_{\Q}^{2}}{\delta_{\Q}\cdot A(\xi^{\prime}|\xi)}
    -  \frac{\delta_{\Q}\cdot A(\xi^{\prime}|\xi)}{\lambda_{\Q}^{2}}
\label{eq:ell_lambda}
\end{align}
%
where \(\lambda_{P}, \lambda_{\Q}\) are scaling factors used to control the
contribution from each of the \(\delta_{P}, \delta_{\Q}\) terms.
%
Finally, our loss function becomes
%
\begin{equation}
  \mathcal{L}{(\theta)} = \mathbb{E}_{p(\xi)}\left[
    \alpha_{P} \cdot \ell_{\lambda_{P}} + \alpha_{\Q} \cdot \ell_{\lambda_{\Q}}
  \right]
  + \mathbb{E}_{q(\xi)}\left[
    \alpha_{P} \cdot \ell_{\lambda_{P}} + \alpha_{\Q} \cdot \ell_{\lambda_{\Q}}
  \right]
\end{equation}
%
where \(\alpha_{P}, \alpha_{\Q}\) are weights to control the respective terms
contribution to the overall loss function.

%
% \begin{table}[ht!]
%   \centering
%   \begin{tabular}{@{}rccc@{}}
%   % \cmidrule(l){1-4}
%   \multicolumn{1}{l}{} & \multicolumn{1}{l}{\textbf{tunneling events}} & \multicolumn{1}{l}{\textbf{tunneling rate}} & \multicolumn{1}{l}{\textbf{accept prob}} \\
%   \cmidrule(l){1-4}
%    \multicolumn{1}{r}{\textit{chain 1}} & 1 & 0.000125 & 0.684 \(\pm\) 0.003 \\
%    \multicolumn{1}{r}{\textit{chain 2}} & 4 & 0.000500 & 0.695 \(\pm\) 0.003 \\
%    \multicolumn{1}{r}{\textit{chain 3}} & 4 & 0.000500 & 0.696 \(\pm\) 0.003 \\
%    \multicolumn{1}{r}{\textit{chain 4}} & 3 & 0.000375 & 0.698 \(\pm\) 0.003 \\
%   \cmidrule(l){1-4}
%    \multicolumn{1}{r}{\textbf{average}} & \textbf{2.4} & \textbf{0.0003} &
%    \textbf{0.696 \(\pm\) 0.003}
%   \end{tabular}
%   \caption{Inference results for trained \textbf{L2HMC} sampler ran for \(N = 1\times
%   10^{4}\) accept/reject steps at \(\beta = 5\).}%
%   \label{tab:l2hmc_inference}
% \end{table}
% %
% \begin{table}[ht!]
%   \centering
%   \begin{tabular}{@{}rccc@{}}
%   % \cmidrule(l){2-4}
%   \multicolumn{1}{l}{} & \multicolumn{1}{l}{\textbf{tunneling events}} & \multicolumn{1}{l}{\textbf{tunneling rate}} & \multicolumn{1}{l}{\textbf{accept prob}} \\
%   \cmidrule(l){1-4}
%   \multicolumn{1}{r}{\textit{chain 1}} & 2 & 0.000250 & 0.279 \(\pm\) 0.004 \\
%   \multicolumn{1}{r}{\textit{chain 2}} & 1 & 0.000125 & 0.271 \(\pm\) 0.004 \\
%   \multicolumn{1}{r}{\textit{chain 3}} & 3 & 0.000375 & 0.281 \(\pm\) 0.004 \\
%   \multicolumn{1}{r}{\textit{chain 4}} & 4 & 0.000500 & 0.286 \(\pm\) 0.004 \\
%   \cmidrule(l){1-4}
%   \multicolumn{1}{r}{\textbf{average}} & \textbf{2} & \textbf{0.00025} &
%   \textbf{0.282 \(\pm\) 0.004}
%   \end{tabular}
%   \caption{Inference results for generic \textbf{HMC} sampler ran for \(N = 1\times
%   10^{4}\) accept/reject steps at \(\beta = 5\).}%
%   \label{tab:hmc_inference}
% \end{table}
% %
% \clearpage
% %
% \begin{figure}[ht!]
%   \centering
%   \includegraphics[width=0.7\linewidth]{figures/updates_2020_04_13/l2hmc.png}
%   \caption{Inference results from trained \textbf{L2HMC} sampler.}%
%   \label{fig:l2hmc_inference}
% \end{figure}
% %
% \begin{figure}[ht!]
%   \centering
%   \includegraphics[width=0.7\linewidth]{figures/updates_2020_04_13/hmc.png}
%   \caption{Inference results from generic \textbf{HMC} sampler.}%
%   \label{fig:hmc_inference}
% \end{figure}
% %
% %
% \begin{figure}[htpb]
%  \centering
%  \begin{subfigure}[t]{0.48\textwidth}
%    \caption{Using gradient clipping.}
%    \includegraphics[width=\textwidth]{grid_plots/lf1/tunn_rate_vs_bias_lf1_clip10.png}
%  \end{subfigure}
%  \begin{subfigure}[t]{0.48\textwidth}
%    \caption{Without gradient clipping.}
%    \includegraphics[width=\textwidth]{grid_plots/lf1/tunn_rate_vs_bias_lf1.png}
%  \end{subfigure}
%  \caption{Plot of the tunneling rate $\gamma$ vs $\delta \phi_{P}$.}
% \end{figure}
% (
% [

% \begin{figure}[htpb!]
%   \centering
%   \begin{subfigure}[t]{0.57\textwidth}
%     \includegraphics[width=\textwidth]{updates_2020_04_28/losses_beta40}%
%     % \caption{\(\beta = 4\).}%
%   \label{fig:losses_beta4}
%   \end{subfigure}
%   \begin{subfigure}[t]{0.57\textwidth}
%     \includegraphics[width=\textwidth]{updates_2020_04_28/losses_beta5}%
%     % \caption{\(\beta = 5\).}%
%     \label{fig:losses_beta5}
%   \end{subfigure}
%   \begin{subfigure}[t]{0.57\textwidth}
%     \includegraphics[width=\textwidth]{updates_2020_04_28/losses_beta55}%
%     % \caption{\(\beta = 5.5\).}%
%     \label{fig:losses_beta55}
%   \end{subfigure}
%   \caption{Loss comparisons between L2HMC and HMC at different values of
%   \(\beta\).}
% \end{figure}


\documentclass[jou]{apa6}

\usepackage[american]{babel}

\usepackage{csquotes}
\usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{bibliography.bib}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Discrete Structures
%% The start of RBS stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Working internal and external links in PDF
\usepackage{hyperref}
% Extra math symbols in LaTeX
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{amssymb}
% Enumerations with (a), (b), etc.
\usepackage{enumerate}

\let\OLDitemize\itemize
\renewcommand\itemize{\OLDitemize\addtolength{\itemsep}{-6pt}}

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=3pt \partopsep=3pt }
\makeatother

% These sizes redefine APA for A4 paper size
\oddsidemargin 0.0in
\evensidemargin 0.0in
\textwidth 6.27in
\headheight 1.0in
\topmargin -24pt
\headheight 12pt
\headsep 12pt
\textheight 9.19in

\setlength\parindent{0pt}

\title{Midterm, 2020-02-17}
\author{Discrete Structures, Fall 2020}
\affiliation{RBS}

\leftheader{Midterm, 2020-02-17}

\abstract{%
}

%\keywords{}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Midterm, 2020-02-17}

\vspace{6pt}
{\bf Question 1 (Boolean Expressions).}\\
Consider Boolean expression:
$$E_0 = (p \rightarrow q \rightarrow r) \wedge (q \rightarrow r \rightarrow p) \wedge (r \rightarrow p \rightarrow q)$$
\begin{figure}[!htb]
\center{\includegraphics[width=2in]{midterm/venn-circles.png}}
\caption{\label{fig:venn-circles} Venn diagram for 3 sets.}
\end{figure}

\begin{enumerate}[(A)]
\item Copy the Venn diagram's circles in your solution and shade those regions in the diagram that make $E_0$ true
(being inside each circle $P,Q,R$ means that the respective variable $p,q,r$ is true; being outside the circle means
that the variable is false).
\item In the truth table of $E_0$ how many entries are $\mathtt{True}$?\\
({\em Note.} Building the truth table is optional. Regardless whether you build one or not, you should justify your answer.)
\item Rewrite the Boolean expression $E_0$ into an equivalent one, using
only conjunctions ($\wedge$) and negations ($\neg$).
\end{enumerate}

Assume that implication ($\rightarrow$) is right-associative
and conjunction ($\wedge$) has higher precedence than implication.

\vspace{10pt}
{\bf Question 2 (Nested Quantifiers).}\\
Verify, if the following predicate/quantifier expressions are true for the given predicate.
The predicate $P$ is defined on $A \times A$, where
$A = \{ \mathtt{a},\mathtt{b},\mathtt{c},\mathtt{d},\mathtt{e},\mathtt{f} \}$.
Predicate $P(\mathtt{a},\mathtt{b})$ is true iff the square on row $\mathtt{a}$
and column $\mathtt{b}$ is shaded in Figure~\ref{fig:relation-set6}. Predicate $P(\mathtt{a},\mathtt{b})$
is false, if that square is white.
\begin{figure}[!htb]
\center{\includegraphics[width=1.2in]{midterm/relation-set6.png}}
\caption{\label{fig:relation-set6} 2 argument predicate.}
\end{figure}

\begin{enumerate}[(A)]
\item Does the predicate $P$ satisfy the logic formula:
$$\forall i \in A,\;P(i,i).$$
\item Does the predicate $P$ satisfy the logic formula:
$$\forall i \in A,\,\forall j \in A,\;P(i,j) \rightarrow P(j,i).$$
\item Does the predicate $P$ satisfy the logic formula:
$$\forall i,j,k \in A,\;P(i,j) \wedge P(j,k) \rightarrow P(i,k).$$
\item Does the predicate $P$ satisfy the logic formula:
$$\forall i,j \in A,\;P(i,j) \vee P(j,i).$$
\item Does the predicate $P$ satisfy the logic formula:
$$\forall i \in A,\, \exists j \in A,\;P(i,j).$$
\end{enumerate}

\vspace{10pt}
{\bf Question 3 (Estimate with Big-O Notation).}\\
Define the sequence $S(n)$ as a sum of squares from $1^2$ to $n^2$:
$$S(n) = \sum\limits_{i=1}^n i^2.$$
We have $S(1) = 1^2 = 1$, $S(2) = 1^2 + 2^2 = 5$,
$S(3) = 1^2 + 2^2 + 3^2 = 14$,
and so on.

\begin{enumerate}[(A)]
\item Is the function $S(n)$ in $O(n^1)$?
Is it in $O(n^2)$? Is it in $O(n^3)$? Is it in $O(n^4)$?
Explain your reasoning.
\item Pick any one of the notations from the previous items
($g(n)$ is either $O(n^1)$, or $O(n^2)$, or $O(n^3)$, or $O(n^4)$).
Check the definition of Big-O notation: Find the {\em witness}: the value $k$ and the constant $C$
such that the absolute value of $S(n)$ does not exceed $C\cdot{}|g(n)|$ for all $n > k$.
\end{enumerate}

\vspace{10pt}
{\bf Question 4 (Chinese Remainder Theorem).}

Consider the following system of three congruences:
$$\left\{ \begin{array}{l}
x \equiv 1\;(\text{mod}\,5),\\
x \equiv 2\;(\text{mod}\,7),\\
x \equiv 3\;(\text{mod}\,9).
\end{array} \right.$$

\begin{enumerate}[(A)]
\item Does it have a solution? Will it have solution, even if we replace $1,2,3$ with other numbers
on the right sides of the equation.
\item Find an arithmetic progression (what is its first member $A$, difference $B$) where all members satisfy
the first two congruences from the system.
\item Find an arithmetic progression (what is its first member $C$, difference $D$) where all members satisfy
all three congruences in the system.
\end{enumerate}

{\em Note.} Arithmetic progression is an infinite sequence where every next member can be obtained
by adding the same number (the difference) to the previous one. For example,
$$A,\;A+B,\;A+2B,\;A+3B,\ldots$$
is an arithmetic progression with the first member $A$ and the difference $B$.

\vspace{10pt}
{\bf Question 5 (Binary notation).}

Somebody has written two binary fractions on the board: $\alpha$ is infinite, $\beta$ is finite (just $6$ digits
after the point):
$$\left\{ \begin{array}{l}
\alpha = 0.(011110)_2 = 0.011110011110011110\ldots_2\\
\beta = 0.011110_2.
\end{array} \right.$$

\begin{enumerate}[(A)]
\item Express the number $\beta$ as a sum of some negative powers of $2$;
namely, show how to add up some of the numbers
$$\{ 2^{-1}, 2^{-2}, 2^{-3}, \ldots \}$$
to get $\beta$.
\item Express $\beta$ as an irreducible fraction {\tt P/Q}; write this in the regular decimal notation.
\item Write the product $64_{10} \cdot \alpha = 1000000_2 \cdot \alpha$ in the binary notation.
\item Express $\alpha$ as an irreducible fraction {\tt P/Q} in decimal notation.
\end{enumerate}

\vspace{10pt}
{\bf Question 6 (Truth-tellers and Liars).}
Among the people $A,B,C$ one is a truth-teller,
the other two are liars.
Every person ($A$, $B$, and $C$) has a closed box
in front of himself/herself. Exactly one of the
boxes has a candy inside. $A,B,C$ know everything
about each other and the location of candy.

Someone else (person $D$) approaches all of them. $D$ knows, who are
people $A$,$B$, and $C$ (it is written on their name-cards), but $D$ does
not know anything about their lying behavior or the location of the candy.
$D$ is allowed to ask YES/NO questions to one or more people.

\begin{enumerate}[(A)]
\item Can $D$ find out who has the candy by asking three questions?
\item Can $D$ find out who has the candy by asking two questions?
\item Can $D$ find out who has the candy by asking one question?
\end{enumerate}

Justify your answers (by construction or by showing that it is impossible).

\vspace{10pt}
{\bf Question 7 (Time Complexity of Truth Tables).}

Assume that there is a Boolean expression $E$ with $n$ variables:
$$E = E(a_1,a_2,\ldots,a_n).$$
The expression $E$ contains $2n$ Boolean operators (such as $\neg$, $\wedge$, $\vee$).
Variables $a_1,a_2,\ldots,a_n$ can independently take values
$\mathtt{True}$ or $\mathtt{False}$.

Consider the following algorithm to find, if $E$ is a tautology by
building the truth table. We will either find a false value, or
establish that all values were true (in this case $E$ is a tautology).

{\bf (1)} \hspace{0.0in} For each assignment of $n$ truth values to $a_1,\ldots,a_n$:\\
{\bf (2)} \hspace{0.2in} For each of the $2n$ Boolean operators in $E$:\\
{\bf (3)} \hspace{0.4in} Compute the value of that Boolean operator\\
{\bf (4)} \hspace{0.2in} If $E$ has value $\mathtt{False}$:\\
{\bf (5)} \hspace{0.4in} Return ``$E$ is not a tautology.''\\
{\bf (6)} \hspace{0.2in} If $E$ has value $\mathtt{True}$:\\
{\bf (7)} \hspace{0.4in} Continue loop on Line {\bf (1)}.\\
{\bf (8)} \hspace{0.0in} Return ``$E$ is a tautology.''

\begin{enumerate}[(A)]
\item Find the worst-case runtime $T(n)$ for this algorithm as an expression of $n$.
(Assume that evaluating one Boolean operator $\neg$, $\wedge$, $\vee$ takes $1$ unit of time.)
\item Find a function $g(n)$ such that $T(n)$ is in $O(g(n))$.
\end{enumerate}

\vspace{10pt}
{\bf Question 8 (About Rational and Irrational).}

We denote two real numbers by $p$ and $q$.
Prove or disprove statements about the rational and irrational numbers.

\begin{enumerate}[(A)]
\item If $p + q$ is rational, then either both $p,q$ are rational, or both are irrational.
\item If $pq$ is rational, then either both $p,q$ are rational, or both are irrational.
\item If $p^2$ and $q^2$ are both rational, then the product $(p+q)(p-q)$ is rational.
\item If $p^3$ and $p^5$ are both rational, then $p$ is rational.
\item If $pq$ and $p+q$ are both rational, then $p$ and $q$ are both rational.
\end{enumerate}

\newpage

\subsection{Answers}

{\em Note. Grading criteria (see the tables below) should not be considered as a dogma,
since every work is different. They provide some guidelines how the points typically split.
There are 10 points (maximum) for any given problem. If the problem has multiple parts
({\bf (A)}, {\bf (B)}, etc.) each part is allocated certain portion of these points.
There are 1-2 points that can be added or subtracted depending on how clearly the
partial solution is written.}

{\em
The theoretical maximum for the whole midterm is 80 points (all 8 problems solved for 10 points each).
Since the midterm accounts for 150~\textperthousand{} (150 promilles or 15 percent) of your
total grade; we get the total grade (in promilles) by multiplying your points by 2
(and cutting it to the maximum value 150~\textperthousand{}, if necessary).
}

\vspace{6pt}
{\bf Question 1}\\
{\bf (A)} The only way to have $p \rightarrow q \rightarrow r = p \rightarrow (q \rightarrow r)$ evaluate to
$\mathtt{False}$ is $p = \mathtt{True}$ and $(q \rightarrow r) = \mathtt{False}$ (i.e.\
$q = \mathtt{True}$ and $r = \mathtt{False}$).\\
If we consider also $q \rightarrow r \rightarrow p$ and $r \rightarrow p \rightarrow q$, there are two
more ways to get the whole expression $E_0$ false. Each of these ways means that exactly two variables
are $\mathtt{True}$ and the third one is $\mathtt{False}$.
All the other areas make the expression true. Shaded areas are shown in Figure~\ref{fig:circles-shaded2}. There are just $3$
regions where the expression $E_0$ evaluates to false (white, not shaded).

\begin{figure}[!htb]
\center{\includegraphics[width=2in]{midterm/circles-shaded2.png}}
\caption{\label{fig:circles-shaded2} Venn diagram with shading.}
\end{figure}

{\bf (B)} There are exactly $8-3 = 5$ entries in the truth table which make the expression true
(there are only $3$ ways to make the expression false, as shown in {\bf (A)}).

{\bf (C)} Let us just transform one subexpression:
\begin{align}
 & p \rightarrow (q \rightarrow r) = \nonumber \\
= & \neg p \vee (q \rightarrow r) = \nonumber \\
= & \neg p \vee (\neg q \vee r) = \nonumber \\
= & \neg p \vee \neg q \vee r  = \nonumber \\
= & \neg (p \wedge q \wedge \neg r). \nonumber
\end{align}

If we combine all $3$ subexpressions (where $p,q,r$ switch their order), we get
a longer conjuction for $E_0$:

$$\neg (p \wedge q \wedge \neg r) \wedge \neg (q \wedge r \wedge \neg p) \wedge \neg (r \wedge p \wedge \neg q).$$

{\footnotesize
\begin{tabular}{|l|l|} \hline
{\bf (A)} Shading is correct & 3 points \\ \hline
{\bf (A)} Shading incorrect, but matches truth table & 2 points \\ \hline
{\bf (B)} Truth table or explanation for the number of ``true'' & 3 points \\ \hline
{\bf (B)} Partial truth table or incomplete justification & 2 points \\ \hline
{\bf (C)} Correct formula with conjunctions, negations & 4 points \\ \hline
\end{tabular}
}

\vspace{10pt}
{\bf Question 2}
\begin{enumerate}[(A)]
\item Yes, $\forall i \in A,\;P(i,i)$ is true (we say that the 2-argument predicate
is {\em reflexive}): all the squares $P(i,i)$ on the diagonal of the values are shaded.
\item Yes, $\forall i \in A,\,\forall j \in A,\;P(i,j) \rightarrow P(j,i)$ is true (we say that the 2-argument predicate
is {\em symmetric}): the squares in the table of $P(i,j)$ are symmetric against
the diagonal of the values: $P(i,j)$ is shaded iff $P(j,i)$ is shaded.
\item Yes, $\forall i,j,k \in A,\;P(i,j) \wedge P(j,k) \rightarrow P(i,k)$ is true
(we say that the 2-argument predicate
is {\em transitive}). Notice that $P(i,j)$ can be true iff $i=j$ or $(i,j)$ is
one of the pairs of neighbors: $(\mathtt{a},\mathtt{b})$, or $(\mathtt{c},\mathtt{d})$, or $(\mathtt{e},\mathtt{f})$.
$P(j,k)$ is also true; so $j,k$ also belong to the same pair. Therefore $i$ and $k$ also belong to the same pair.
\item No, $\forall i,j \in A,\;P(i,j) \vee P(j,i)$ is false. For example, neither $P(\mathtt{a},\mathtt{c})$
nor $P(\mathtt{c},\mathtt{a})$ are true.
\item Yes, $\forall i \in A,\, \exists j \in A,\;P(i,j)$ is true: On each row $i$ there is at least
one shaded square.
\end{enumerate}

{\footnotesize
\begin{tabular}{|l|l|} \hline
{\bf (A)-(E)} Correct answer plus valid explanation & 2 points (for each) \\ \hline
{\bf (A)-(E)} Correct truth value & 1 point (for each) \\ \hline
\end{tabular}
}

\vspace{10pt}
{\bf Question 3}
{\bf (A)} $S(n)$ is in $O(n^3)$ (and therefore it is also in $O(n^4)$).
On the other hand, $S(n)$ is not in $O(n^2)$ or $O(n^1)$.\\
Let us show that $S(n)$ is not in $O(n^2)$. Assume from the contrary
that there are numbers $k$ and $C$ such that for each $n > k$:
$$|S(n)| = |1^2 + 2^2 + \ldots + n^2| \leq C \cdot |n^2|.$$
Since all numbers there are positive, we drop the absolute values.
We pick any even number $n > k$ which also satisfies $n > 8C$. In this case:
\begin{align}
 & S(n) = 1^2 + 2^2 + \ldots + n^2 > \nonumber \\
> & \left( \frac{n}{2} + 1 \right)^2 + \left( \frac{n}{2} + 2 \right)^2 + \ldots + n^2 > \nonumber \\
> & \frac{n}{2} \cdot \left( \frac{n}{2} \right)^2 \geq \frac{n}{8} \cdot n^2 \geq C n^2. \nonumber
\end{align}
{\em (In these inequalities, we first drop the first half of the sum;
then replace each term with $(n/2)^2$ and we still can prove that the sum is more than $Cn^2$
for the given constant $C$.)}\\
Such $n$ can always be found (no matter what $k$ and $C$ are used).
Therefore $S(n)$ can never satisfy $|S(n)| \leq C \cdot n^2$.\\
Since $n$ is even smaller than $n^2$, $S(n)$ is not in $O(n)$ either.

{\bf (B)} Let us prove that $S(n)$ is in $O(n^3)$.
Let us pick the {\em witness} to check the definition of the Big-O Notation:
$k = 1$, $C = 1$.
\begin{align}
     & 1^2 + 2^2 + \ldots + n^2 \leq \nonumber \\
\leq\;\; & n^2 + n^2 + \ldots + n^2=  n \cdot n^2 = 1 \cdot n^3. \nonumber
\end{align}

{\footnotesize
\begin{tabular}{|l|l|} \hline
{\bf (A)} Answer $O(n^3)$ with explanation & 6 points \\ \hline
{\bf (A)} Answer $O(n^3)$ without explanation & 3 points \\ \hline
{\bf (A)} Suboptimal, but correct answer $O(n^4)$ & 2 points \\ \hline
{\bf (B)} Correct witness to show inequality & 4 points \\ \hline
\end{tabular}
}

\vspace{10pt}
{\bf Question 4}
{\bf (A)} Yes, the system will always have solution (even if you replace the numbers $1,2,3$ by
any other integers). Chinese Remainder theorem only needs that the modules
($5,7,9$ in our case) are mutually prime.

{\bf (B)} The numbers satisfying $x \equiv 1\;(\text{mod}\,5)$ make this arithmetic progression:
$$1,\,6,\,11,\,16,\,21,\,26,\,31,\,\ldots$$
Number $16$ in this progression is also congruent to $2$ (modulo $7$). The next number would be
$16 + 35 = 51$ (because adding $5 \cdot 7 = 35$ does not change the remainders, when
we divide by $5$ or by $7$.\\
We conclude that the following arithmetic progression satisfies the top two congruences
(remainder $1$ modulo $5$ and remainder $2$ modulo $7$):
$$16,\,51,\,86,\,121,\,156,\,191,\,\ldots$$
Answer: $A = 16$, $B = 35$.

{\bf (C)} Observe that the first member of this arithmetic sequence ($A = 16$) gives the
remainder $7$ when divided by $9$. The difference ($B = 35$) is congruent to $8$ and also to $-1$
modulo $9$.\\
We conclude that by adding four differences, the result is
$$16 + 4 \cdot 35 \equiv 16 + 4 \cdot (-1) \equiv 12 \equiv 3\;(\text{mod}\,9).$$
The first number from the progression (item (B)) that is also congruent to $3$ modulo $9$ is $156$.
We can add $5 \cdot 7 \cdot 9 = 315$ to this number, and all the remainders will stay the same:
$$156,\,471,\,786,\,1101,\,1416,\,1731,\,2046,\,\ldots$$
Answer: $C = 156$, $D = 315$.

{\footnotesize
\begin{tabular}{|l|l|} \hline
{\bf (A)} Reasoning that $5,7,9$ are pairwise mutual primes & 2 points \\ \hline
{\bf (B)} $16+35k$ & 4 points \\ \hline
{\bf (B)} $16$, but wrong difference & 2 points \\ \hline
{\bf (C)} $156+315k$ & 4 points \\ \hline
{\bf (C)} $156$, but wrong difference & 2 points \\ \hline
\end{tabular}
}

\vspace{10pt}
{\bf Question 5}
{\bf (A)} $\beta = 0.011110_2 = 2^{-2} + 2^{-3} + 2^{-4} + 2^{-5}$.\\
{\bf (B)} $\beta = \frac{8 + 4 + 2 + 1}{2^{5}} = \frac{15}{32}$.\\
{\bf (C)} $64\alpha = 1000000_2 \cdot \alpha =$\\
$= 011110.011110011110011110\ldots_2$ (to multiply
by $2^6 = 64$ we shift the point six positions to the right).
{\bf (D)} Subtract $\alpha$ from $64\alpha$: We get $011110_2$ (because all the
digits after the point are the same \textendash{} they cancel out). We get the equation:
$$64\alpha - \alpha = 63\alpha = 011110_2 = 30_{10}$$
Therefore $\alpha = \frac{30}{63} = \frac{10}{21}$. We could also get the
same answer by finding the sum of an infinite geometric progression:
$$30 \cdot \left( \frac{1}{64} + \frac{1}{64^2} + \frac{1}{64^3} + \ldots{} \right).$$

\vspace{10pt}
{\bf Question 7}
{\bf (A)} In order to get the estimate of time complexity of the algorithm,
consider just the first three lines, where all the computations take place:

\vspace{3pt}
{\bf (1)} \hspace{0.0in} For each assignment of $n$ truth values to $a_1,\ldots,a_n$:\\
{\bf (2)} \hspace{0.2in} For each of the $2n$ Boolean operators in $E$:\\
{\bf (3)} \hspace{0.4in} Compute the value of that Boolean operator

\vspace{3pt}
The outer loop on Line 1 repeats $2^n$ times as there are exactly $2^n$ ways to assign true/false to
$n$ variables. The inner loop on Line 2 repeats $2n$ times (once for every operation you have to evaluate
in the expression). Line 3 takes just $1$ unit of time (this was given in the exercise).
The time complexity $T(n)$ is the product $(2n) \cdot 2^n$; this (worst case) happens
whenever the expression is a tautology. If it is not a tautology, this algorithm will terminate earlier
(and it will take less time).

{\bf (B)}
This $T(n) = 2n\cdot{}2^n$ is in $O(2n\cdot{}2^n)$, since any function
is in the Big-O of itself (we can take $k = 1$ and $C = 1$). If we want, we can drop the multiplier $2$
to simplify it slightly: $T(n)$ is in $O(n\cdot{}2^n)$ (in this case $k = 1$ and $C = 2$).\\
Answer: $T(n)$ is in $O(g(n))$ where $g(n) = n\cdot{}2^n$.

\vspace{10pt}
{\bf Question 8}
{\bf (A)} True: ``If $p + q$ is rational, then either both $p,q$ are rational, or both are irrational.''\\
From the contrary, if $p$ is rational and $q$ is irrational, then $(p + q) - p = q$ should be rational,
which is a contradiction. Same thing happens, if $p$ is irrational and $q$ is rational.

{\bf (B)} False: ``If $pq$ is rational, then either both $p,q$ are rational, or both are irrational.''\\
We can take $p = 0$ and $q = \sqrt{2}$; then $pq = 0$ is rational, also $p$ is rational, but $q$ is irrational.

{\bf (C)} True: ``If $p^2$ and $q^2$ are both rational, then the product $(p+q)(p-q)$ is rational.''\\
Denote the two rational numbers by $\alpha = p^2$ and $\beta = q^2$. Then their difference is
also a rational number: $\alpha - \beta = p^2 - q^2 = (p+q)(p-q)$.

{\bf (D)} True: ``If $p^3$ and $p^5$ are both rational, then $p$ is rational.''\\
If $p = 0$ then all $p$, $p^3$ and $p^5$ are rational.\\
If $p \neq 0$, then denote the non-zero rational numbers $\alpha = p^3$ and $\beta = p^5$.
Then their ratio $\frac{\beta}{\alpha} = \frac{p^5}{p^3} = p^2$ is also rational.
Finally, if we divide $\alpha = p^3$ by the rational $p^2$, we get that also $p$ is rational
(as a fraction of two rational numbers). Shortly: $p = p^1 = p^{2 \cdot 3 - 5} = \frac{ \alpha \cdot \alpha}{\beta}$.

{\bf (E)} False: ``If $pq$ and $p+q$ are both rational, then $p$ and $q$ are both rational.''\\
Consider two irrational numbers $p = 1 + \sqrt{2}$ and $q = 1 - \sqrt{2}$. Then $p + q = 2$ and
$pq = (1 + \sqrt{2})(1 - \sqrt{2}) = 1 -2 = -1$, i.e.\ the sum and the product are both rational numbers.

\end{document}