\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{physics}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\relpenalty=10000
\binoppenalty=10000

\begin{document}
\section*{Combinatorial geometry}
	\begin{enumerate}
		\item % PKV 10 2017
		There are $5$ points on the plane, no three of them lie on the same line. Prove that there exists $4$ of them which form a convex quadrilateral
		\item % LVS V 2017
		A finite number of points is chosen on the plane, no three of them lie on the same line. It is known that there exists a non-convex polygons with its vertices at given points. Prove that there exists a non-convex quadrilateral with its vertices at given points.
		\item % BT 2017
		Let $n \geq 3$ be an integer. Find the largest number of angles which can be greater than $180^\circ$ in an $n$-gon whose sides are all equal.
		\item % LPV 11 2016
		Every point on the sides of an equilateral triangle is coloured either red or blue. Is it always possible to find a right angle triangle with all its vertices having the same colour.
		\item % PKV 11 2016
		Prove that there are more than $30000$ points with integral coordinates which lie within a circle of radius $100$.
		\item % VV 2013
		There are $n$ points on the plane. Starting from one of those points, in each step we move to the second closest point. After $n$ steps we have visited all the points and returned to the original point. Find all possible values for $n$.
		\item % http://www.math.olympiaadid.ut.ee/arhiiv/valik/vv2018/tvv2018.pdf ül5
		Let $k$ be a positive integer. Find all positive integers $n$ for which it is possible to choose $n$ points on the sides of a triangle (different from its vertices) and connect some of them with a line such that
		\begin{enumerate}
			\item There is at least $1$ point on each side
			\item For each pair of points $X$ and $Y$ which are on different sides of the triangle, there exists exactly $k$ points on the third side which are all connected to both $X$ and $Y$, and exactly $k$ points which are all connected to neither of $X$ or $Y$.
		\end{enumerate}

  \end{enumerate}
\end{document}


\subsection{Attention}
\label{text:approach/runtime/filtering}
For a trajectory optimization to be feasible for online applications, it must be efficient to solve. As empirically shown by experiments in Section \ref{text:experiments/unit}, the computational bottleneck of solving Problem \ref{problem:general} is evaluating and, particularly, differentiating the components, which depend on the interaction between robot and pedestrians. By design, the computational cost of the safety constraint has already been reduced. However, the interactive cost presented in Section \ref{text:approach/objective/interactive} cannot be further runtime optimized or occluded, being one of the core parts of the underlying algorithm. Therefore there are mostly two ways to increase overall runtime efficiency of the trajectory optimization: Firstly, reducing the number of solver iterations for convergence and secondly lowering the runtime cost of evaluating the optimization components itself.
\newline
While the number of solver iterations has already largely been reduced by warm-starting the optimization, the attention focusses on merely taking into account essential factors into the evaluation, with improves runtime without having a significant impact on the resulting behavior of the algorithm. Accurately, the attention filter decides which pedestrians are taken into account for a specific (interactive objective) function evaluation. Importantly to note here, that attention merely effects the evaluation of the interactive objective function (including its gradient computation). Especially, it is not applied to the safety-relevant constraints, to guarantee safety regarding all pedestrians at every time.
\newline
Using the distance between the robot as each pedestrian is a natural choice for filtering out the agents to consider for the trajectory optimization; the larger the distance is, the smaller the impact of the robot probably is. In fact, the Trajectron model uses a similar distance-based metric for building and evaluating edges in their graph network \cite{Salzmann2020}.

\begin{equation}
\attention(\x, \xped[k]) = \left( ||\x - \xped[k]||_2 > D_{Attention} \right)
\end{equation}

Although euclidean distance-based filtering is a quite naive way of distributing attention to pedestrians in the scene, it shows to be quite effective in reducing the runtime of the interactive objective and gradient evaluations, and therefore of the whole algorithm. A comparison to more sophisticated approaches such as using (forward) reachability to conservatively estimate the pedestrians that eventually could impinge on the robot trajectory, or focussing on only a specific pedestrian instead of a set of pedestrians, as used by game-theoretic crowd navigation works (as presented in Chapter \ref{text:related/crowd_navigation}, e.g.\,  \cite{Bouzat2014}\cite{Nikolaidis2017}), is given in Section \ref{text:experiments/unit}.


\documentclass[12pt]{amsart}
\usepackage[margin=0.5in]{geometry}
  % see geometry.pdf on how to lay out the page. There's lots.
\usepackage{bsymb}
\usepackage{calculation}
\usepackage{ulem}
\usepackage{hyperref}
\usepackage{unitb}

\newcommand{\REQ}{\text{REQ}}
\newcommand{\OBJ}{\mathcal{O}bj}
\newcommand{\Node}{\mathcal{N}ode}
\newcommand{\Addr}{\mathcal{P}tr}
\newcommand{\link}{\textit{LINK}}
\newcommand{\trash}{\textit{TRASH}}
\newcommand{\shared}{\textit{SHARED}}
\newcommand{\State}{\mathcal{S}tate}
\newcommand{\cEmpty}{\text{``empty''}}
\newcommand{\cNonEmpty}{\text{``non-empty''}}
\newcommand{\cInit}{\text{``init''}}
\newcommand{\cPopped}{\text{``popped''}}
\newcommand{\cBot}{\text{``idle''}}

\begin{document}

\tableofcontents

\section{m0}
\input{pop-left/machine_m0}
\begin{machine}{m0}
    \with{functions}
    \with{sets}
    \[ \newset{\Addr_0} \]
    \[ \constant{ dummy : \Addr_0} \]
    \[ \constant{ \bot : \OBJ_0} \]
    \[ \definition
        {\Addr}{ \Addr_0 \setminus \{ dummy \} } \]
    \[ \definition
        {\OBJ}{ \OBJ_0 \setminus \{ \bot \} } \]
    \[ \definition
        {\Node}{ [ 'item : \OBJ_0 \setminus \{ \bot \} ] } \]
    \[\newset{\OBJ_0}\]

shared:
    \[ \variable{ LH, RH : \Addr_0 } \]
    \[ \variable{ free : \set [\Addr_0] } \]
    \[ \variable{ ver : \Int } \]
    \[ \variable{ result : \OBJ_0 } \]
    \[ \variable{ \link, \trash : \Addr_0 \pfun
        \{ 'item : \OBJ_0
         , 'left : \Addr_0
         , 'right : \Addr_0  \} } \]

private:
    \[ \variable{ popL,remL : \Bool } \]
    \[ \variable{ resL : \OBJ_0 } \]

\begin{align*}
  \initialization{m0:init0}{ LH = dummy } \\
  \initialization{m0:init1}{ RH = dummy } \\
  \initialization{m0:init2}{ free = \Addr } \\
  \initialization{m0:init3}{ ver = 0 } \\
  \initialization{m0:init4}{ result = \bot } \\
  \initialization{m0:init5}{ \link = \emptyfun } \\
  \initialization{m0:init6}{ \trash = \emptyfun } \\
  \initialization{m0:init7}{ popL = \false } \\
  \initialization{m0:init8}{ remL = \false } \\
  \initialization{m0:init9}{ resL = \bot } \\
\end{align*}

internal:
\newevent{add:popL}{req\_popL}
\newevent{hdl:popL:empty}{hdl\_popL\_empty}
\newevent{hdl:popL:one}{hdl\_popL\_one}
\newevent{hdl:popL:more}{hdl\_popL\_more}
\newevent{returnL}{returnL}

\begin{align*}
    \evguard{add:popL}{m0:grd0}{\neg popL} \\
    \evbcmeq{add:popL}{m0:act0}{popL}{\true} \\
    \cschedule{returnL}{m0:sch0}{remL} \\
    \evbcmeq{returnL}{m0:act0}{result}{resL} \\
    \evbcmeq{returnL}{m0:act1}{remL}{\false} \\
\end{align*}

\begin{align*}
  \theorem{m0:thm0:ASM}{ LH \in \dom.\link \bunion \{ dummy \} } \\
  \theorem{m0:thm1:ASM}{ RH \in \dom.\link \bunion \{ dummy \} } \\
  \theorem{m0:thm2:ASM}{ \neg LH \in \dom.\trash  } \\
  \theorem{m0:thm3:ASM}{ \neg RH \in \dom.\trash  } \\
  \invariant{m0:inv0}{ free \subseteq \Addr }
\end{align*}

\[ \indices{hdl:popL:empty}{ v : \Int } \]
\[ \indices{hdl:popL:one}{ v : \Int } \]
\[ \indices{hdl:popL:more}{ v : \Int } \]

\begin{align*}
  \cschedule{hdl:popL:empty}{m0:sch0}{v = ver} \\
  \cschedule{hdl:popL:one}{m0:sch0}{v = ver} \\
  \cschedule{hdl:popL:more}{m0:sch0}{v = ver} \\
  \cschedule{hdl:popL:empty}{m0:sch1}{popL} \\
  \cschedule{hdl:popL:one}{m0:sch1}{popL} \\
  \cschedule{hdl:popL:more}{m0:sch1}{popL} \\
  \cschedule{hdl:popL:empty}{m0:sch2}{LH = dummy} \\
  \cschedule{hdl:popL:one}{m0:sch2}{\neg LH = dummy} \\
  \cschedule{hdl:popL:more}{m0:sch2}{\neg LH = dummy} \\
  \cschedule{hdl:popL:one}{m0:sch3}{LH = RH} \\
  \cschedule{hdl:popL:more}{m0:sch3}{\neg LH = RH} \\
  \evbcmeq{hdl:popL:empty}{m0:act0}
    {ver}{ver + 1} \\
  \evbcmeq{hdl:popL:one}{m0:act0}
    {ver}{ver + 1} \\
  \evbcmeq{hdl:popL:more}{m0:act0}
    {ver}{ver + 1} \\
  \evbcmeq{hdl:popL:empty}{m0:act1}
    {popL}{\false} \\
  \evbcmeq{hdl:popL:one}{m0:act1}
    {popL}{\false} \\
  \evbcmeq{hdl:popL:more}{m0:act1}
    {popL}{\false} \\
  \evbcmeq{hdl:popL:empty}{m0:act2}
    {remL}{\true} \\
  \evbcmeq{hdl:popL:one}{m0:act2}
    {remL}{\true} \\
  \evbcmeq{hdl:popL:more}{m0:act2}
    {remL}{\true} \\
  \evbcmeq{hdl:popL:empty}{m0:act3}
    {resL}{\bot} \\
  \evbcmeq{hdl:popL:one}{m0:act3}
    {resL}{\link.LH.'item} \\
  \evbcmeq{hdl:popL:more}{m0:act3}
    {resL}{\link.LH.'item} \\
  \evbcmeq{hdl:popL:one}{m0:act4}
    {\trash}{\trash \1| LH \fun \link.LH} \\
  \evbcmeq{hdl:popL:more}{m0:act4}
    {\trash}{\trash \1| LH \fun \link.LH} \\
  \evbcmeq{hdl:popL:one}{m0:act5}
    {\link}{ \{LH\} \domsub \link } \\
  \evbcmeq{hdl:popL:more}{m0:act5}
    {\link}{ \{LH\} \domsub \link } \\
  \evbcmeq{hdl:popL:one}{m0:act6}
    {LH}{ dummy } \\
  \evbcmeq{hdl:popL:one}{m0:act7}
    {RH}{ dummy } \\
  \evbcmeq{hdl:popL:more}{m0:act6}
    {LH}{ \link.LH.'right } \\
\end{align*}

external:
\newevent{ext:pushL:empty}{EXT\_pushL\_empty}
\newevent{ext:pushL:non:empty}{EXT\_pushL\_non\_empty}
\newevent{ext:pushR:empty}{EXT\_pushR\_empty}
\newevent{ext:pushR:non:empty}{EXT\_pushR\_non\_empty}
\newevent{ext:popL:empty}{EXT\_popL\_empty}
\newevent{ext:popL:one}{EXT\_popL\_one}
\newevent{ext:popL:more}{EXT\_popL\_more}
\newevent{ext:popR:empty}{EXT\_popR\_empty}
\newevent{ext:popR:one}{EXT\_popR\_one}
\newevent{ext:popR:more}{EXT\_popR\_more}
\newevent{ext:return}{EXT\_return}

\paragraph{pushL --- empty}

\begin{align*}
  \param{ext:pushL:empty}{x : \OBJ_0 } \\
  \param{ext:pushL:empty}{n : \Addr_0 } \\
\end{align*}

\begin{align*}
  \evguard{ext:pushL:empty}{m0:grd0}{LH = dummy } \\
  \evguard{ext:pushL:empty}{m0:grd1}{x \in \OBJ } \\
  \evguard{ext:pushL:empty}{m0:grd2}{n \in free } \\
  \evbcmeq{ext:pushL:empty}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:pushL:empty}{m0:act1}
      {free}{free \setminus \{ n \} } \\
  \evbcmeq{ext:pushL:empty}{m0:act2}
      {\link}{\link \1| n \fun
          \left[ \begin{array}{l}
            'item := x, \\
            'left := dummy, \\
            'right := dummy
          \end{array} \right] } \\
  \evbcmeq{ext:pushL:empty}{m0:act3}
      {LH}{n} \\
  \evbcmeq{ext:pushL:empty}{m0:act4}
      {RH}{n} \\
\end{align*}

\paragraph{pushL --- non-empty}

\begin{align*}
  \param{ext:pushL:non:empty}{x : \OBJ_0 } \\
  \param{ext:pushL:non:empty}{n : \Addr_0 } \\
\end{align*}

\begin{align*}
  \evguard{ext:pushL:non:empty}
    {m0:grd0}{ \neg LH = dummy } \\
  \evguard{ext:pushL:non:empty}
    {m0:grd1}{x \in \OBJ } \\
  \evguard{ext:pushL:non:empty}
    {m0:grd2}{n \in free } \\
  \evbcmeq{ext:pushL:non:empty}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:pushL:non:empty}{m0:act1}
      {free}{free \setminus \{ n \} } \\
  \evbcmeq{ext:pushL:non:empty}{m0:act2}
      {\link}{\link \1| n \fun
          \left[ \begin{array}{l}
            'item := x, \\
            'left := dummy, \\
            'right := LH
          \end{array} \right] } \\
  \evbcmeq{ext:pushL:non:empty}{m0:act3}
      {LH}{n} \\
  \evbcmeq{ext:pushL:non:empty}{m0:act4}
      {\link}{ \link \1| LH \fun
        (\link.LH) [ 'left := n ] } \\
\end{align*}

\paragraph{pushR --- empty}

\begin{align*}
  \param{ext:pushR:empty}{x : \OBJ_0 } \\
  \param{ext:pushR:empty}{n : \Addr_0 } \\
\end{align*}

\begin{align*}
  \evguard{ext:pushR:empty}
      {m0:grd0}{RH = dummy } \\
  \evguard{ext:pushR:empty}
      {m0:grd1}{x \in \OBJ } \\
  \evguard{ext:pushR:empty}
      {m0:grd2}{n \in free } \\
  \evbcmeq{ext:pushR:empty}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:pushR:empty}{m0:act1}
      {free}{free \setminus \{ n \} } \\
  \evbcmeq{ext:pushR:empty}{m0:act2}
      {\link}{\link \1| n \fun
          \left[ \begin{array}{l}
            'item := x, \\
            'left := dummy,
            'right := dummy
          \end{array} \right] } \\
  \evbcmeq{ext:pushR:empty}{m0:act3}
      {LH}{n} \\
  \evbcmeq{ext:pushR:empty}{m0:act4}
      {RH}{n} \\
\end{align*}

\paragraph{pushR --- non-empty}

\begin{align*}
  \param{ext:pushR:non:empty}{x : \OBJ_0 } \\
  \param{ext:pushR:non:empty}{n : \Addr_0 } \\
\end{align*}

\begin{align*}
  \evguard{ext:pushR:non:empty}
    {m0:grd0}{ \neg RH = dummy } \\
  \evguard{ext:pushR:non:empty}
    {m0:grd1}{x \in \OBJ } \\
  \evguard{ext:pushR:non:empty}
    {m0:grd2}{n \in free } \\
  \evbcmeq{ext:pushR:non:empty}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:pushR:non:empty}{m0:act1}
      {free}{free \setminus \{ n \} } \\
  \evbcmeq{ext:pushR:non:empty}{m0:act2}
      {\link}{\link \1| n \fun
          \left[ \begin{array}{l}
            'item := x, \\
            'left := RH,
            'right := dummy
          \end{array} \right] } \\
  \evbcmeq{ext:pushR:non:empty}{m0:act3}
      {RH}{n} \\
  \evbcmeq{ext:pushR:non:empty}{m0:act4}
      {\link}{ \link \1| RH \fun
        (\link.RH) [ 'right := n ] } \\
\end{align*}

\paragraph{popL --- empty}

\begin{align*}
  \evguard{ext:popL:empty}
      {m0:grd0}{LH = dummy} \\
  \evbcmeq{ext:popL:empty}{m0:act0}
      {ver}{ver + 1}
\end{align*}

\paragraph{popL --- one}

\begin{align*}
  \evguard{ext:popL:one}
      {m0:grd0}{\neg LH = dummy} \\
  \evguard{ext:popL:one}
      {m0:grd1}{LH = RH} \\
  \evbcmeq{ext:popL:one}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:popL:one}{m0:act1}
      {\link}{ \{LH\} \domsub \link } \\
  \evbcmeq{ext:popL:one}{m0:act2}
      {LH}{ dummy } \\
  \evbcmeq{ext:popL:one}{m0:act3}
      {RH}{ dummy } \\
  \evbcmeq{ext:popL:one}{m0:act4}
      {\trash}{ \trash \1| LH \fun \link.LH } \\
\end{align*}

\paragraph{popL --- more}

\begin{align*}
  \evguard{ext:popL:more}
      {m0:grd0}{\neg LH = dummy} \\
  \evguard{ext:popL:more}
      {m0:grd1}{\neg LH = RH} \\
  \evbcmeq{ext:popL:more}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:popL:more}{m0:act1}
      {\link}{ \{LH\} \domsub \link } \\
  \evbcmeq{ext:popL:more}{m0:act2}
      {LH}{ \link.LH.'right } \\
  \evbcmeq{ext:popL:more}{m0:act3}
      {\trash}{ \trash \1| LH \fun \link.LH } \\
\end{align*}

\paragraph{popR --- empty}

\begin{align*}
  \evguard{ext:popR:empty}
      {m0:grd0}{RH = dummy} \\
  \evbcmeq{ext:popR:empty}{m0:act0}
      {ver}{ver + 1}
\end{align*}

\paragraph{popR --- one}

\begin{align*}
  \evguard{ext:popR:one}
      {m0:grd0}{\neg RH = dummy} \\
  \evguard{ext:popR:one}
      {m0:grd1}{LH = RH} \\
  \evbcmeq{ext:popR:one}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:popR:one}{m0:act1}
      {\link}{ \{RH\} \domsub \link } \\
  \evbcmeq{ext:popR:one}{m0:act2}
      {LH}{ dummy } \\
  \evbcmeq{ext:popR:one}{m0:act3}
      {RH}{ dummy } \\
  \evbcmeq{ext:popR:one}{m0:act4}
      {\trash}{ \trash \1| RH \fun \link.RH } \\
\end{align*}

\paragraph{popR --- more}

\begin{align*}
  \evguard{ext:popR:more}
      {m0:grd0}{\neg RH = dummy} \\
  \evguard{ext:popR:more}
      {m0:grd1}{\neg LH = RH} \\
  \evbcmeq{ext:popR:more}{m0:act0}
      {ver}{ver + 1} \\
  \evbcmeq{ext:popR:more}{m0:act1}
      {\link}{ \{RH\} \domsub \link } \\
  \evbcmeq{ext:popR:more}{m0:act2}
      {RH}{ \link.RH.'left } \\
  \evbcmeq{ext:popR:more}{m0:act3}
      {\trash}{ \trash \1| RH \fun \link.RH } \\
\end{align*}

\paragraph{return}

\end{machine}
\section{m1}

\input{pop-left/machine_m1}

\begin{machine}{m1}
  \refines{m0}
  \[ \newset{\State} \]
  \[ \constant{\cInit : \State} \]
  \[ \constant{\cBot : \State} \]
  \[ \constant{\cPopped : \State} \]
  \[ \constant{\cEmpty : \State} \]
  \[ \constant{\cNonEmpty : \State} \]
  \[ \assumption{m0:asm0}{ \neg \cInit = \cBot } \]
  \[ \assumption{m0:asm1}{ \neg \cInit = \cEmpty } \]
  \[ \assumption{m0:asm2}{ \neg \cInit = \cNonEmpty } \]
  \[ \assumption{m0:asm3}{ \neg \cInit = \cPopped } \]
  \[ \assumption{m0:asm4}{ \neg \cBot = \cEmpty } \]
  \[ \assumption{m0:asm5}{ \neg \cBot = \cNonEmpty } \]
  \[ \assumption{m0:asm6}{ \neg \cBot = \cPopped } \]
  \[ \assumption{m0:asm7}{ \neg \cEmpty = \cNonEmpty } \]
  \[ \assumption{m0:asm8}{ \neg \cEmpty = \cPopped } \]
  \[ \assumption{m0:asm9}{ \neg \cNonEmpty = \cPopped } \]
  \[ \variable{lh : \Addr_0} \]
  \[ \variable{state : \State} \]
  \begin{align*}
    \initialization{m1:init0}
      { lh = dummy } \\
    \initialization{m1:init2}
      { state = \cBot } \\
  \end{align*}

\newevent{read:LH}{read\_LH}

\begin{align*}
  \cschedule{read:LH}{m1:sch0}
    {state \in \{ \cInit, \cNonEmpty \} } \\
  \cschedule{read:LH}{m1:sch1}
    { \neg LH = dummy } \\
  \evbcmeq{read:LH}{m1:act0}
    {lh}{LH} \\
  \evbcmeq{read:LH}{m1:act1}
    {state}{ \ifelse{LH = dummy}{\cEmpty}{\cNonEmpty} } \\
  \evbcmeq{read:LH}{m1:act2}
    {ver}{ \ifelse{LH = dummy}{ver + 1}{ver} } \\
\end{align*}

\subsection{Invariants}

\begin{align*}
  \invariant{m1:inv0}
    {state = \cBot & \3\equiv \neg popL \land \neg remL} \\
  % \invariant{m1:inv1}
  %   {state = \cInit & \3\implies popL} \\
  % \invariant{m1:inv2}
  %   {state = \cNonEmpty & \3\implies popL \land \neg lh = dummy} \\
  \invariant{m1:inv3}
    {popL & \3\equiv state \in \{\cInit,\cNonEmpty\}} \\
  \invariant{m1:inv4}
    { state = \cPopped & \3\implies \left(
      \begin{array}{ll}
         & remL \\
         \land & lh \in \dom.\trash \\
         \land & \trash.lh.'item = resL
       \end{array} \right) } \\
  \invariant{m1:inv5}
    { remL &\3\equiv state \in \{ \cPopped, \cEmpty \} } \\
  \invariant{m1:inv7}
    { \qforall{p}{p \in \dom.\link}{\link.p.'item \in \OBJ} } \\
  \invariant{m1:inv6}
    { state = \cEmpty & \3\equiv remL \land resL = \bot } \\
\end{align*}

\subsection{State}

\removevar{popL,remL,resL}

\removeguard{add:popL}{m0:grd0}
\removeact{add:popL}{m0:act0}
\begin{align*}
  \evguard{add:popL}{m1:grd0}
    { state = \cBot } \\
  \evbcmeq{add:popL}{m1:act0}
    {state}{ \cInit } \\
\end{align*}

\removeact{hdl:popL:empty}{m0:act0}
\removeact{hdl:popL:empty}{m0:act1}
\removeact{hdl:popL:empty}{m0:act2}
\removeact{hdl:popL:empty}{m0:act3}
\removecoarse{hdl:popL:empty}{m0:sch1}
\begin{align*}
  &\cschedule{hdl:popL:empty}{m1:sch0}
    { state \in \{ \cInit, \cNonEmpty \} } \\
  &\evbcmeq{hdl:popL:empty}{m1:act0}
    {lh}{LH} \\
  &\evbcmeq{hdl:popL:empty}{m1:act1}
    { state }{ \ifelse{ LH = dummy }{\cEmpty}{\cNonEmpty} } \\
  & \evbcmeq{hdl:popL:empty}{m1:act2}
    {ver}{ \ifelse{LH = dummy}{ver + 1}{ver} } \\
\end{align*}

\removeact{hdl:popL:more}{m0:act1}
\removeact{hdl:popL:more}{m0:act2}
\removeact{hdl:popL:more}{m0:act3}
% \removecoarse{hdl:popL:more}{m0:sch0}{popL}
\removecoarse{hdl:popL:more}{m0:sch1}{popL}
\removecoarse{hdl:popL:more}{m0:sch2}{popL}
\removecoarse{hdl:popL:more}{m0:sch3}{popL}
% \removeind{hdl:popL:more}{v}
\begin{align*}
  % &\witness{hdl:popL:more}{v}{v = ver} \\
  &\cschedule{hdl:popL:more}{m1:sch0}
    { state = \cNonEmpty } \\
  &\cschedule{hdl:popL:more}{m1:sch1}
    { lh = LH } \\
  &\cschedule{hdl:popL:more}{m1:sch2}
    { \neg lh = RH } \\
  &\cschedule{hdl:popL:more}{m1:sch3}
    { \neg lh = dummy } \\
  &\evbcmeq{hdl:popL:more}{m1:act0}
    { state }{ \cPopped } \\
  % &\evbcmeq{hdl:popL:more}{m1:act0}
  %   { remL }{ \true } \\
\end{align*}

\replace{hdl:popL:more}{m1:sch0,m1:sch1}{m1:prog0}
\replace{hdl:popL:one}{m1:sch0,m1:sch1}{m1:prog0}
\begin{align*}
  \progress{m1:prog0}
    {v = ver \1\land \neg LH = dummy
        \1\land state \in \{ \cInit, \cNonEmpty \} }
    { (LH = lh \1\land \neg LH = dummy \1\land state = \cNonEmpty)
      \1\lor \neg v = ver}
  \refine{m1:prog0}{ensure}{read:LH}{}
  % \progress{m1:prog0}
  %   {v = ver \1\land \neg LH = dummy
  %       \1\land state \in \{ \cInit, \cNonEmpty \} }
  %   { (LH = lh \1\land \neg LH = dummy \1\land state = \cNonEmpty)
  %     \1\lor \neg v = ver}
  % \refine{m1:prog1}{ensure}{read:LH}{}
\end{align*}
\[ \dummy{v : \Int} \]

\removeact{hdl:popL:one}{m0:act1}
\removeact{hdl:popL:one}{m0:act2}
\removeact{hdl:popL:one}{m0:act3}
% \removecoarse{hdl:popL:one}{m0:sch0}
\removecoarse{hdl:popL:one}{m0:sch1}
\removecoarse{hdl:popL:one}{m0:sch2}
\removecoarse{hdl:popL:one}{m0:sch3}
% \removeind{hdl:popL:one}{v}
\begin{align*}
  % &\witness{hdl:popL:one}{v}{v = ver} \\
  &\cschedule{hdl:popL:one}{m1:sch0}
    { state = \cNonEmpty } \\
  &\cschedule{hdl:popL:one}{m1:sch1}
    { lh = LH } \\
  &\cschedule{hdl:popL:one}{m1:sch3}
    { \neg lh = dummy } \\
  &\evbcmeq{hdl:popL:one}{m1:act0}
    { state }{ \cPopped } \\
\end{align*}

\[ \splitevent{returnL}{returnL:empty,returnL:non:empty} \]
% \begin{align*}
    \refiningevent{returnL}{returnL:empty}{returnL\_empty} \\
    \refiningevent{returnL}{returnL:non:empty}{returnL\_non\_empty}  \\
% \end{align*}
\hide{
\removeact{returnL:empty}{m0:act0}
\removeact{returnL:empty}{m0:act1}
\removecoarse{returnL:empty}{m0:sch0}
\removeact{returnL:non:empty}{m0:act0}
\removeact{returnL:non:empty}{m0:act1}
\removecoarse{returnL:non:empty}{m0:sch0}}
% \replace{returnL}{m0:sch0}{m1:sch}{}{}
\begin{align*}
  \cschedule{returnL:empty}{m1:sch0}{state = \cEmpty} \\
  \evbcmeq{returnL:empty}{m1:act0}{state}{\cBot} \\
  \evbcmeq{returnL:empty}{m1:act1}{result}{\bot} \\
  \cschedule{returnL:non:empty}{m1:sch0}{state = \cPopped} \\
  \evbcmeq{returnL:non:empty}{m1:act0}{state}{\cBot} \\
  \evbcmeq{returnL:non:empty}{m1:act1}{result}{\trash.lh.'item} \\
\end{align*}

\removeinit{m0:init7}
\removeinit{m0:init8}
\removeinit{m0:init9}
\begin{align*}
  \initwitness{resL}{resL = \bot} \\
  \initwitness{remL}{remL = \false}\\
  \initwitness{popL}{popL = \false}
\end{align*}

\end{machine}

\section{m2}
\input{pop-left/machine_m2}

\begin{machine}{m2}
  \refines{m1}
    \removevar{ver}
    \removeact{ext:popL:empty}{m0:act0}
    \removeact{ext:popL:more}{m0:act0}
    \removeact{ext:popL:one}{m0:act0}
    \removeact{ext:popR:empty}{m0:act0}
    \removeact{ext:popR:more}{m0:act0}
    \removeact{ext:popR:one}{m0:act0}
    \removeact{ext:pushL:empty}{m0:act0}
    \removeact{ext:pushL:non:empty}{m0:act0}
    \removeact{ext:pushR:empty}{m0:act0}
    \removeact{ext:pushR:non:empty}{m0:act0}

    % \removeact{read:LH}{m0:act0}
    \removeact{hdl:popL:more}{m0:act0}
    \removeact{hdl:popL:one}{m0:act0}
    \removeact{read:LH}{m1:act2}
    \removecoarse{read:LH}{m1:sch1}
    \removecoarse{read:LH}{m0:sch2}
    \removeinit{m0:init3}
    \hide{
        \mergeevents{read:LH,hdl:popL:empty}{read:LH}}
    \begin{align*}
      \initwitness{ver}{ ver = 0 }
    \end{align*}
    % \removeact{hdl:popL:more}{m1:act2}
    % \removeact{hdl:popL:one}{m1:act2}
    \removecoarse{read:LH}{m0:sch0}

  \removecoarse{hdl:popL:one}{m0:sch0}{popL}
  \removecoarse{hdl:popL:more}{m0:sch0}{popL}
  \removeind{read:LH}{v}
  \removeind{hdl:popL:one}{v}
  \removeind{hdl:popL:more}{v}
  \begin{align*}
  &\witness{read:LH}{v}{v = ver} \\
  &\witness{hdl:popL:one}{v}{v = ver} \\
  &\witness{hdl:popL:more}{v}{v = ver} \\
  \end{align*}

\subsection{read RH}

\newevent{read:RH}{read\_RH}
  \[ \variable{rh : \Addr_0} \]

\begin{align*}
  \initialization{m1:init1}
    { rh = dummy } \\
\end{align*}

\replace{hdl:popL:one}{m2:sch0}{m2:prog0}
\refine{m1:prog2}{ensure}{read:RH}{}
\begin{align*}
  \cschedule{read:RH}{m1:sch0}{ state = \cNonEmpty } \\
  \evbcmeq{read:RH}{m1:act0}{rh}{RH} \\
  \cschedule{hdl:popL:one}{m2:sch0}
    { rh = RH } \\
  \cschedule{hdl:popL:one}{m2:sch1}
    { lh = rh } \\
\end{align*}

\begin{align*}
  \progress{m2:prog0}
    {v = ver \land state \in \{ \cNonEmpty, \cInit \} }
    {(rh = RH \land state = \cNonEmpty) \lor \neg v = ver}
\end{align*}

    % \removecoarse{hdl:popL:more}{m0:sch0}
    % \removecoarse{hdl:popL:one}{m0:sch0}
  % \mergeevents{foo}{bar}
\end{machine}

\end{document}


\section{Scalable Inference}\label{sec:inf}

%n the single user case, the goal is to infer the posterior distribution over the utilities of test items, $\bs f^*$,
%given a set of pairwise training labels, $\bs y$. In the multi-user case, w
Given a set of pairwise training labels, $\bs y$,
we aim to find the posterior over the matrix
$\bs F^*=\bs V^{*T} \bs W^*$ of utilities for test items and test users,
and the posterior over consensus utilities for test items, $\bs t^*$.
The non-Gaussian likelihood (Equation \ref{eq:plphi})
makes exact inference intractable, hence previous work uses
 the Laplace approximation for GPPL~\citep{chu2005preference}
or combines expectation propagation (EP) with variational Bayes for a
multi-user model~\citep{houlsby2012collaborative}.
The Laplace approximation is a maximum a-posteriori solution that
takes the most probable values of parameters rather than integrating over their distributions,
and has been shown to perform poorly for classification compared to EP~\citep{nickisch2008approximations}.
However,
%for a latent factor model, the
%EP and VB approximate the true posterior with a simpler, factorised distribution.
%%that can be learned using an iterative algorithm.
%For crowdGPPL, the true posterior is multi-modal,
%In a latent factor model, the latent factors can be re-ordered arbitrarily without
%affecting $\bs F$, causing a \emph{non-identifiability problem}.
%Since using EP would average these modes and produce uninformative predictions over $\bs F$, so
%\citet{houlsby2012collaborative} incorporate a VB step that approximates a single mode.
a drawback of EP is that convergence is not guaranteed
%, even when distributions are conjugate
~\citep{minka2001expectation}.
%do they also linearise in the same way? -- both linearise. But EP uses a joint over y and f as its approximation to p(y|f), then optimises the parameters iteratively. It's not guaranteed to converge. Variational EGP instead approximates
% p(y|f) directly with the best fit Gaussian. It's not clear whether this could be updated iteratively but it doesn't
% seem to work if done simultaneously with the other variables we need to learn (the linearisation),
% perhaps because the algorithm for learning the weights breaks if the variance of q(y|f), Q, keeps changing.
% Possibly because Q does not change incrementally. So it's
% possible that an outer loop could be used.
%TODO: remove redundancy with the related work section. Consider whether this should actually be in a background section. NM^2 is limiting, not just the other costs. The other costs NP etc come into play in this pairwise model only.
More importantly, inference for a GP using either method
has computational complexity $\mathcal{O}(N^3)$
and memory complexity $\mathcal{O}(N^2)$, where $N$ is the number of data points.

The cost of inference can be reduced using a \emph{sparse} approximation based on a set of
\emph{inducing points}, which act as substitutes for the points in the training dataset.
By choosing a fixed number of inducing points, $M \ll N$, the computational cost is cut to $\mathcal{O}(NM^2)$,
and the memory complexity to $\mathcal{O}(NM)$.
Inducing points must be selected %to give a good approximation
using either heuristics or by optimising their positions to maximise an estimate of the
marginal likelihood.
One such sparse approximation is the \emph{generalized fully independent training conditional} (GFITC)~\citep{NIPS2007_3351,snelson2006sparse},
used by \citet{houlsby2012collaborative} for collabGP.
%which generalizes the fully independent training conditional
%(FITC)~\citep{snelson2006sparse} method to non-Gaussian likelihoods.
%GFITC assumes that each training and test point is independent of all other points
%given the function values at the inducing points.
%This may be inappropriate for the pairwise likelihood (Equation \ref{eq:plphi})
%because it ignores the covariance between the utilities of the items.
However, time and memory costs that grow linearly with $\mathcal{O}(N)$
start to become a problem with thousands of data points,
as all data must be processed in every iterative update,
before any other parameters such as $s$ are updated,
making GFITC unsuitable for very large datasets~\citep{hensman2015scalable}.
%and distributed computation cannot be applied to GFITC to tackle the growing
%computational costs as the objective function does not contain a sum over observations~

We derive a more scalable approach for GPPL and crowdGPPL using
stochastic variational inference (SVI)~\citep{hoffman2013stochastic}.
%an iterative scheme that
%limits the computational and memory costs at
%each iteration.
%and allows training data to be split into mini-batches for parallel processing.
%As we explain below,
%this allows us to reduce
For GPPL, this reduces the time complexity of each iteration %of the algorithm
%from $\mathcal{O}(NM^2)$
to $\mathcal{O}(P_i M^2 + P_i^2 M + M^3)$,
and memory complexity %from $\mathcal{O}(NM)$
to $\mathcal{O}(P_i M + M^2  + P_i^2)$,
where $P_i$ is a mini-batch size that we choose in advance.
Neither $P_i$ nor $M$ are dependent on the size of the dataset, meaning that SVI
can be run with arbitrarily large datasets,
and other model parameters such as $s$ can be updated before processing all data
to encourage faster convergence.
First, we define a suitable likelihood approximation to enable the use of SVI.

\subsection{Approximating the Posterior with a Pairwise Likelihood}

The preference likelihood in Equation \ref{eq:plphi}
is not conjugate with the Gaussian process, which means there is no analytic expression for
the exact posterior.
For single-user GPPL, we therefore
approximate the preference likelihood with a Gaussian:
%This avoids the need for quadrature methods, as in \cite{hensman2015scalable} or ...
\begin{flalign}
p(\bs f | \bs y, s) & \propto \prod_{p=1}^P p\left(y_p | z_p\right) p\left(\bs f | \bs K, s\right)
= \prod_{p=1}^P \Phi\left(z_p\right) \mathcal{N}\left(\bs f; \bs 0, \bs K/s\right)
%= \mathbb{E}\left[\prod_{p=1}^P \Phi(z_p)\right] = \prod_{p=1}^P \Phi(\hat{z}_p)
%\approx \mathcal{N}(\bs y; \Phi(\hat{\bs z}), \bs Q),
& \\
& \approx \prod_{p=1}^P \mathcal{N}\left(y_p; \Phi(z_p), Q_{p,p}\right)
\mathcal{N}\left(\bs f; \bs 0, \bs K/s\right)
 = \mathcal{N}\left(\bs y; \Phi(\bs z), \bs Q\right) \mathcal{N}\left(\bs f; \bs 0, \bs K/s\right), &\nonumber
\end{flalign}
where $\bs Q$ is a diagonal noise covariance matrix
and we omit the kernel hyperparameters, $\theta$, to simplify notation.
For crowdGPPL, we use the same approximation to the likelihood, but
replace $\bs f$ with $\bs F$.
% and the priors over $\bs v$, $\bs t$ and $\bs w$
%throughout this section.
%There are two problems with this approximation so far:
%firstly, $\Phi(\bs z)$ is a nonlinear function of $\bs f$,
%which makes the posterior intractable:
%\begin{flalign}
%p(\bs f | \bs y) %\propto p(\bs y | \bs f)\mathcal{N}(\bs f; \bs 0, \bs K/s)
%\approx \frac{ \mathcal{N}(\bs y; \Phi(\bs z), \bs Q)\mathcal{N}(\bs f; \bs 0, \bs K/s) }{
%\int \mathcal{N}(\bs y; \Phi(\bs z), \bs Q)\mathcal{N}(\bs f'; \bs 0, \bs K/s) df'}
%\end{flalign}
%Secondly, we need to estimate the diagonal variance terms in $\bs Q$.
We estimate the diagonals of $\bs Q$
by moment matching our approximate likelihood with $\Phi(z_p)$,
which defines a Bernoulli distribution with variance $Q_{p,p} = \Phi(z_p)(1 - \Phi(z_p))$.
However, this means that $\bs Q$ % the variance of the approximate likelihood
depends on $\bs z$ and therefore on $\bs f$,
so the approximate posterior over $\bs f$ cannot be computed in closed form.
To resolve this, we approximate $Q_{p,p}$
using an estimated posterior over $\Phi(z_p)$ computed
independently for each pairwise label, $p$.
%thereby replacing intractable expectations with respect to $p(\bs f|\bs y)$
%with simple
We obtain this estimate
 by updating the parameters of the conjugate prior for the Bernoulli likelihood,
 which is
a beta distribution with parameters $\gamma$ and $\lambda$.
We find $\gamma$ and $\lambda$ by
matching the moments of the beta prior to the prior mean and variance of $\Phi(z_p)$,
estimated using numerical integration.
The prior over $\Phi(z_p)$ is defined by a GP for single-user GPPL, $p(\Phi(z_p) | \bs K, \alpha_0, \beta_0)$,
and a non-standard distribution for crowdGPPL.
%Assuming a beta prior over $\Phi(z_p)$ means that $y_p$ has a beta-Bernoulli
%distribution.
Given the observed label $y_p$, we estimate the diagonals in $\bs Q$
as the variance of the posterior beta-Bernoulli:
\begin{flalign}
Q_{p,p} & \approx \frac{ (\gamma + y_p)(\lambda + 1 - y_p) }{(\gamma + \lambda + 1)^2}. &
\end{flalign}
The covariance $\bs Q$ therefore approximates the expected noise in the observations,
hence captures variance due to $\sigma$ in Equation \ref{eq:plphi}.
This approximation performs well empirically
for Gaussian process classification~\citep{reece2011determining,simpson2017bayesian} and
classification using extended Kalman filters~\citep{lee2010sequential,lowne2010sequential}.

Unfortunately, the nonlinear term $\Phi(\bs z)$ means that the posterior is still intractable,
so we replace $\Phi(\bs z)$ with a linear function of $\bs f$ by taking
the first-order Taylor series expansion of $\Phi(\bs z)$
about the expectation $\mathbb{E}[\bs f] = \hat{\bs f}$:
\begin{flalign}
\Phi(\bs z) &\approx \tilde{\Phi}(\bs z) = \bs G \left(\bs f-\hat{\bs f}\right)
+ \Phi(\hat{\bs z}), & \\
G_{p,i} &= \frac{\partial \Phi(\hat{z}_p)} {\partial f_i}
= \Phi(\hat{z}_p)\left(1 - \Phi(\hat{z}_p)\right) \left(2y_p - 1\right)\left( [i = a_p] - [i = b_p]\right), &
\end{flalign}
%where $\bs G$ is a matrix whose elements
%$G_{p,i}= \Phi(\hat{z}_p)(1 - \Phi(\hat{z}_p)) (2y_p - 1)( [i = a_p] - [i = b_p])$
%are the partial derivatives of $\Phi(\hat{z_p})$ %the pairwise likelihood
%with respect to $f_i$.
%of the latent function values, $\bs f$.
where $\hat{\bs z}$ is the expectation of $\bs z$ computed using Equation \ref{eq:predict_z},
and $[i=a]=1$ if $i=a$ and is $0$ otherwise.
There is a circular dependency between $\hat{\bs f}$,
which is needed to compute $\hat{\bs z}$, and $\bs G$. %the linearization terms in the likelihood,
We estimate these terms using a variational inference procedure
that iterates between updating $\bs f$ and $\bs G$~\citep{steinberg2014extended}
as part of Algorithm \ref{al:singleuser}.
%and is described in more detail below.
The complete approximate posterior for GPPL is now as follows:
\begin{flalign}
p(\bs f | \bs y, s)
\approx %\frac{1}{Z}
\mathcal{N}(\bs y; \bs G (\bs f-\mathbb{E}[\bs f]) + \Phi(\hat{\bs z}), \bs Q) \mathcal{N}(\bs f; \bs 0, \bs K/s) / Z = \mathcal{N}(\bs f; \hat{\bs f}, \bs C), &&
\label{eq:likelihood_approx}
\end{flalign}
where $Z$ is a normalisation constant.
Linearisation means that our approximate likelihood is conjugate to the prior,
so the approximate posterior is also Gaussian.
%TODO any other variant in Nickish that uses linearization? Can we cite nickisch to say that linearization is good, m'kay?
%TODO show the posterior without further SVI approximations?
Gaussian approximations to the posterior have shown strong empirical results for
classification~\citep{nickisch2008approximations} and
preference learning~\citep{houlsby2012collaborative},
%including the
%expectation propagation method~\citep{rasmussen_gaussian_2006}.
and linearisation using a Taylor expansion has been widely tested
in the extended Kalman filter~\citep{haykin2001kalman}
as well as Gaussian processes~\citep{steinberg2014extended,bonilla2016extended}.
%\todo{generate a synthetic plot showing the
%difference between a true posterior over f and our
%approximation on synthetic data. Do what Nickisch did?}
%Given our approximate posterior, we now derive an efficient inference scheme using SVI.

\subsection{SVI for Single User GPPL}

%TODO what's going on here? First, we need to learn s. Second, we need a more efficient way to learn
% f without inverting K and without using all observations at once.
Using the linear approximation in the previous section,
posterior inference requires inverting
$\bs K$ with computational cost $\mathcal{O}(N^3)$
and taking an expectation with respect to $s$, which remains intractable.
We address these problems using stochastic variational inference (SVI)
with a sparse approximation to the GP that limits
the size of the covariance matrices we need to invert.
We introduce $M \ll N$ inducing items with inputs
$\bs X_m$,
utilities $\bs f_m$, and covariance $\bs K_{mm}$. The
covariance between the observed and inducing items is $\bs K_{nm}$.
% The inducing points act as proxies for the observed points during inference,
% and thereby reduce the number of data points we have to perform costly operations % over.
%We modify the variational approximation in Equation \ref{eq:vb_approx} to introduce the inducing points
For clarity, we omit $\theta$ from this point on.
We assume a \emph{mean-field} approximation to the joint posterior over
inducing and training items
that factorises between different sets of latent variables:
\begin{flalign}
p\left(\bs f, \bs f_m, s | \bs y, \bs X, \bs X_m, k_{\theta}, \alpha_0, \beta_0 \right)
&\approx q\left(\bs f, \bs f_m, s\right) = q(s)q\left(\bs f\right)q\left(\bs f_m\right), \label{eq:svi_approx} &&
\end{flalign}
where $q(.)$ are \emph{variational factors} defined below.
Each factor corresponds to a subset of latent variables, $\bs \zeta_i$, and
takes the form $\ln q(\bs \zeta_i) = \mathbb{E}_{j \neq i}[\ln p(\bs \zeta_i, \bs x, \bs y)]$.
That is, the expectation with respect
to all other latent variables, $\bs\zeta_j,\forall j \neq i$, of the log joint distribution
of the observations and latent variables, $\bs \zeta_i$.
To obtain the factor for $\bs f_m$, we marginalise $\bs f$ and take expectations with respect to $q(s)$:
\begin{flalign}
\ln q\left(\bs f_m\right) &= \ln \mathcal{N}\!\left(\bs y; \tilde{\Phi}(\bs z), \bs Q\right)
+ \ln\mathcal{N}\left(\bs f_m; \bs 0, \frac{\bs K_{mm}}{\mathbb{E}\left[s\right]}\right) \!  + \textrm{const} %& \nonumber \\
 = \ln \mathcal{N}\left(\bs f_m; \hat{\bs f}_m, \bs S \right), &
 \label{eq:fhat_m}
\end{flalign}
where the variational parameters $\hat{\bs f}_m$ and $\bs S$ are computed using
an iterative SVI procedure described below.
We choose an approximation of $q(\bs f)$ that depends only on the inducing point utilities, $\bs f_m$, and is independent of the observations:
 \begin{flalign}
\ln q\left(\bs f\right) & = \ln \mathcal{N}\left(\bs f; \bs A \hat{\bs f}_m,
\bs K + \bs A (\bs S - \bs K_{mm}/\mathbb{E}[s]) \bs A^T \right), &
\end{flalign}
where $\bs A=\bs K_{nm} \bs K^{-1}_{mm}$.
Therefore, we no longer need to invert an $N \times N$ covariance matrix to compute $q(\bs f)$.
The factor $q(s)$ also depends only the inducing points:
\begin{flalign}
& \ln q(s) = \mathbb{E}_{q(\bs f_m)}[\ln\mathcal{N}(\bs f_m| \bs 0, \bs K_{mm}/s)] + \ln \mathcal{G}(s; \alpha_0, \beta_0) + \mathrm{const}
= \ln \mathcal{G}(s; \alpha, \beta), & \label{eq:qs}
\end{flalign}
where $\alpha= \alpha_0 + \frac{M}{2}$ and $\beta = \beta_0 + \frac{1}{2}
\textrm{tr}\left(\bs K^{-1}_{mm}\left(S + \hat{\bs f}_m \hat{\bs f}_m^T\right)\right)$.
The expected value is
$\mathbb{E}[s] = \frac{\alpha}{\beta}$.

We apply variational inference to iteratively reduce the KL-divergence between our approximate posterior
%$q(s)q(\bs f)q(\bs f_m)$
and the true posterior (Equation \ref{eq:svi_approx}) %, $p(s, \bs f, \bs f_m | \bs K, \alpha_0, \beta_0, \bs y)$,
by maximising a lower bound, $\mathcal{L}$, on the log marginal likelihood (detailed equations in Appendix \ref{sec:vb_eqns}), which is given by:
%(see also Equation \ref{eq:full_L_singleuser} in the Appendix):%, $\ln p(\bs y | \bs K, \alpha_0, \beta_0)$ :
\begin{flalign}
&\ln p(\bs y | \bs K, \alpha_0, \beta_0) = \textrm{KL}\left(q\left(\bs f, \bs f_m, s\right)  || p\left(\bs f, \bs f_m, s | \bs y, \bs K, \alpha_0, \beta_0\right)\right)
+ \mathcal{L} & \label{eq:lowerbound}
\\
%\end{flalign}
%Taking expectations with respect to the variational $q$ distributions, $\mathcal{L}$ is:
%\begin{flalign}
&\mathcal{L} = \mathbb{E}_{q(\bs f)}\left[\ln p(\bs y | \bs f)\right]
+ \mathbb{E}_{q\left(\bs f_m, s\right)}\left[\ln p\left(\bs f_m, s | \bs K,
\alpha_0, \beta_0 \right) -\ln q\left(\bs f_m\right) - \ln q(s)\right]. & \nonumber
\end{flalign}
%         invK_mm_expecFF = self.invK_mm.dot(self.uS + self.um_minus_mu0.dot(self.um_minus_mu0.T))
%         self.rate_s = self.rate_s0 + 0.5 * np.trace(invK_mm_expecFF)
To optimise $\mathcal{L}$,
we initialise the $q$ factors randomly, then
update each one in turn, taking expectations with respect to the other factors.

The only term in $\mathcal{L}$ that refers to the observations, $\bs y$,
is a sum of $P$ terms, each of which refers to one observation only.
This means that $\mathcal{L}$ can be maximised by considering a random subset of
observations at each iteration~\citep{hensman2013gaussian}.
%Therefore, the SVI solution replaces Equations \ref{eq:fhat_m} and \ref{eq:S} for computing
%$\hat{\bs f}_m$ and $\bs S$ over all observations with a sequence of stochastic updates.
For the $i$th update of $q(\bs f_m)$, we randomly select $P_i$
observations $\bs y_i = \{ y_p \forall p \in \bs P_i \}$,
where $\bs P_i$ is a random subset of indexes of observations,
and $P_i$ is a mini-batch size.
The items referred to by the pairs in the subset are
$\bs N_i = \{a_p \forall p \in \bs P_i \} \cup \{ b_p \forall p \in \bs P_i\}$.
We  perform updates using $\bs Q_i$ (rows and columns of $\bs Q$ for pairs in $\bs P_i$),
$\bs K_{im}$ and $\bs A_i$ (rows of $\bs K_{nm}$ and $\bs A$ in $\bs N_i$),
$\bs G_i$ (rows of $\bs G$ in $\bs P_i$ and columns in $\bs N_i$), and
$\hat{\bs z}_i = \left\{ \hat{\bs z}_p \forall p \in P_i \right\}$.
%All matrices with subscript $_i$ contain only the subset of elements relating to
%observations in $\bs P_i$.
% The linearization matrix $\bs G_i$ is the subset of elements in $\bs G$ relating to observations in $\bs P_i$,
%  is the corresponding subset of elements in $\bs Q$,
%  is the covariance between the items referred to by pairs in $\bs P_i$
% and the inducing points,
% and  contains the corresponding rows of $\bs A$.
The updates optimise the natural parameters of the Gaussian distribution by following the
natural gradient~\citep{hensman2015scalable}:
\begin{flalign}
\bs S^{-1}_i  & = (1 - \rho_i) \bs S^{-1}_{i-1} + \rho_i\left( \mathbb{E}[s]\bs K_{mm}^{-1} + \pi_i\bs A_i^T \bs G^T_{i} \bs Q^{-1}_i \bs G_{i} \bs A_{i} \right)&
\label{eq:S_stochastic} \\
\hat{\bs f}_{m,i}  & = \bs S_i \left( \! (1 - \rho_i) \bs S^{-1}_{i-1} \hat{\bs f}_{m,i-1}  +
%\right. \nonumber \\
%& \left.\hspace{1.5cm}
\rho_i \pi_i
\bs A_{i}^{T} \bs G_{i}^T \bs Q_i^{-1}\! \left( \bs y_i  - \Phi(\hat{\bs z}_i) + \bs G_{i} \bs A_i \hat{\bs f}_{m,i-1} \! \right) \! \right) &
\label{eq:fhat_stochastic}
\end{flalign}
where
$\rho_i=(i + \epsilon)^{-r}$ is a mixing coefficient that controls the update rate,
$\pi_i = \frac{P}{P_i}$ weights each update according to sample size,
 $\epsilon$ is a delay hyperparameter and $r$ is a forgetting rate~\citep{hoffman2013stochastic}.

By performing updates in terms of mini-batches,
the time complexity of Equations \ref{eq:S_stochastic} and
\ref{eq:fhat_stochastic} is
%has order $\mathcal{O}(M^3)$, and the second term has order
$\mathcal{O}(P_i M^2 + P_i^2 M + M^3)$ and
%The $P_i^2$ term arises due to $\bs G_i$, which is an $N_i \times P_i$ matrix, where $N_i \leq 2P_i$ is the number of
%items referred to by the pairwise labels in the mini-batch.
memory complexity is  $\mathcal{O}(M^2 + P_i^2 + M P_i)$.
%, where each complexity term is due
%to the sizes of $K_{mm}$, $G_i$ and $K_{im}$.
The only parameters that must be stored between iterations relate to the
inducing points, hence the memory consumption does not grow with the dataset size
as in the GFITC approximation used by \citet{houlsby2012collaborative}.
A further advantage of stochastic updating is that the $s$ parameter (and any other global
parameters not immediately depending on the data) can be learned
before the entire dataset has been processed,
which means that poor initial estimates of $s$ are rapidly improved
and the algorithm can converge faster.

\begin{algorithm}
 \KwIn{ Pairwise labels, $\bs y$, training item features, $\bs x$,
 test item features $\bs x^*$}
 \nl Select inducing point locations $\bs x_{mm}$ and compute kernel matrices $\bs K$, $\bs K_{mm}$ and $\bs K_{nm}$ given $\bs x$ \;
 \nl Initialise $\mathbb{E}[s]$ and $\hat{\bs f}_m$ to prior means
 and $\bs S$ to prior covariance $\bs K_{mm}$\;
 \While{$\mathcal{L}$ not converged}
 {
 \nl Select random sample, $\bs P_i$, of $P$ observations\;
 \While{$\bs G_i$ not converged}
  {
  \nl Compute $\mathbb{E}[\bs f_i]$ \;
  \nl Compute $\bs G_i$ given $\mathbb{E}[\bs f_i]$ \;
  \nl Compute $\hat{\bs f}_{m,i}$ and $\bs S_{i}$ \;
  }
 \nl Update $q(s)$ and compute $\mathbb{E}[s]$ and $\mathbb{E}[\ln s]$\;
 }
\nl Compute kernel matrices for test items, $\bs K_{**}$ and $\bs K_{*m}$, given $\bs x^*$ \;
\nl Use converged values of $\mathbb{E}[\bs f]$and $\hat{\bs f}_m$ to estimate
posterior over $\bs f^*$ at test points \;
\KwOut{ Posterior mean of the test values, $\mathbb{E}[\bs f^*]$ and covariance, $\bs C^*$ }
\vspace{0.2cm}
\caption{The SVI algorithm for GPPL: preference learning with a single user.}
\label{al:singleuser}
\end{algorithm}
The complete SVI algorithm is summarised in Algorithm \ref{al:singleuser}.
It uses a nested loop to learn $\bs G_i$, which avoids storing the complete matrix,
$\bs G$.
It is possible to distribute computation in lines 3-6 by selecting multiple random samples
to process in parallel. A global estimate of $\hat{\bs f}_m$ and $\bs S$
is passed to each compute node, which runs the loop over lines 4 to 6.
The resulting updated $\hat{\bs f}_m$ and $\bs S$ values are then passed back to a
central node that combines them by taking a mean weighted by $\pi_i$ to account for
the size of each batch.
%This does not require modifying Equations \ref{eq:S_stochastic} and \ref{eq:fhat_stochastic}, since they already contain a sum weighted by $\pi_i$.

Inducing point locations can be learned
as part of the variational inference procedure, which
%or by optimising a bound on the log marginal likelihood.
breaks convergence guarantees, or by an expensive optimisation process~\citep{hensman2015scalable}.
We obtain good performance by choosing inducing points up-front
using K-means++~\citep{arthur2007k} with $M$ clusters to cluster
the feature vectors,
then taking the cluster centres as inducing points that represent the distribution of observations.

The inferred distribution over the inducing points can be used
to estimate the posteriors of test items, $f(\bs x^*)$, according to:
\begin{flalign}
\bs f^* \! \! &= \bs K_{*m} \bs K_{mm}^{-1} \hat{\bs f}_m, &
\bs C^* \! \! = \bs K_{**} + \bs K_{*m} \bs K_{mm}^{-1} (\bs S - \bs K_{mm} / \mathbb{E}[s] ) \bs K_{mm}^{-1}\bs K_{*m}^T ,
\end{flalign}
where $\bs C^*$ is the posterior covariance of the test items, $\bs K_{**}$ is their prior covariance, and
$\bs K_{*m}$ is the covariance between test and inducing items.
%It is possible to recover the lower bound proposed by
%\citet{hensman2015scalable} for classification by generalizing the
%likelihood to arbitrary nonlinear functions, and omitting terms relating to $p(s|\alpha_0,\beta_0)$ and $q(s)$.
% However, our approach avoids expensive quadrature methods by linearizing the likelihood to enable analytical updates. We also infer $s$ in a Bayesian manner,
% rather than treating as a hyper-parameter, which is important for preference learning where $s$ controls the noise level of the observations relative to  $f$.

\subsection{SVI for CrowdGPPL}

We now provide the variational posterior for the crowdGPPL model defined in Equation \ref{eq:joint_crowd}:
\begin{flalign}
& p\left( \bs V, \bs V_m, \bs W, \bs W_m, \bs t, \bs t_m, s^{(v)}_1, .., s^{(v)}_C,
s^{(w)}_1, .., s^{(w)}_C, s^{(t)} | \bs y, \bs X, \bs X_m, \bs U, \bs U_m, k, \alpha_0, \beta_0 \right)
& \nonumber \\
& \approx q(\bs t) q(\bs t_m)q\left(s^{(t)}\right)\prod_{c=1}^{C} q(\bs v_{c})q(\bs w_c)q(\bs v_{c,m})q(\bs w_{c,m})
q\left(s^{(v)}_c\right)q\left(s^{(w)}_c\right), & %\nonumber \\
%& = q(\bs F) q(s^{(t)}) \prod_{c=1}^C q(s^{(v)}_c), &
\end{flalign}
where $\bs U_m$ are the feature vectors of inducing users and the variational $q$ factors are defined below.
We use SVI to optimise the lower bound on the log marginal likelihood
(detailed in Appendix \ref{sec:crowdL}), which is given by:
\begin{flalign}
& \mathcal{L}_{cr} =
\mathbb{E}_{q(\bs F)}%(\bs t, \bs t_m, \bs V, \bs V_m, \bs W, \bs W_m, s_1,...,s^{(v)}_c,s^{(t)})
[\ln p(\bs y | \bs F)]
+ \mathbb{E}_{q\left(\bs t_m, s^{(t)}\right)} \left[\ln p\left(\bs t_m, s^{(t)} | \bs K_{mm}, \alpha_0^{(t)}, \beta_0^{(t)}\right)
- \ln q(\bs t_m)  - \ln q\left(s^{(t)}\right) \right]  & \nonumber \\
&
+ \sum_{c=1}^C \!\! \bigg\{  \mathbb{E}_{q\left(\bs v_{m,c},s^{(v)}_c\right)}\left[\ln p\left(\bs v_{m,c}, s^{(v)}_c | \bs K_{mm}, \alpha_0^{(v)}, \beta_0^{(v)}\right) - \ln q(\bs v_{m,c}) - \ln q\left(s_c^{(v)}\right) \right]
&  \nonumber \\
&
+  \mathbb{E}_{q\left(\bs w_{m,c}, s_c^{(w)}\right)}\left[\ln p\left(\bs w_{m,c},s^{(w)}_c | \bs L_{mm}, \alpha_0^{(w)}, \beta_0^{(w)} \right)
  - \ln q(\bs w_{m,c} )  - \ln q\left(s_c^{(w)} \right) \right] \bigg\} . &
  \label{eq:lowerbound_crowd}
\end{flalign}
The SVI algorithm
follows the same pattern as Algorithm \ref{al:singleuser},
updating each $q$ factor in turn by computing means and covariances
for  $\bs V_m$, $\bs W_m$ and $\bs t_m$ instead of $\bs f_m$ (see Algorithm \ref{al:crowdgppl}).
The time and memory complexity of each update are
%the same as for single-user GPPL,
%except that we now have $C$ updates, and the number of inducing points for items and users
%may be different:
$\mathcal{O}(CM_{\mathrm{items}}^3 + CM_{\mathrm{items}}^2 P_i + CM_{\mathrm{items}} P_i^2$
$ + CM_{\mathrm{users}}^3 + CM_{\mathrm{users}}^2 P_i + CM_{\mathrm{users}} P_i^2 )$
%Memory complexity for crowdGPPL is
and
$\mathcal{O}(CM_{\mathrm{items}}^2 + P_i^2 + M_{\mathrm{items}} P_i + CM_{\mathrm{users}}^2 + M_{\mathrm{users}} P_i)$, respectively.
%
The variational factor for the $c$th inducing item component is:
\begin{flalign}
\ln q(\bs v_{m,c})  & =
\mathbb{E}_{q(\bs t, \bs w_{m,c'}\forall c', \bs v_{m,c'}\forall c'\backslash c) }\left[
\ln \mathcal{N}\left( \bs y; \tilde{\Phi}(\bs z), Q \right) \right]
+ \ln\mathcal{N}\left(\bs v_{\!m,c}; \bs 0, \frac{\bs K_{mm}}{\mathbb{E}[s^{(v)}_c]}\right)
 +  \textrm{const} & \nonumber \\
% are the dimensions collapsed to a single MVN?
& = \ln \mathcal{N}\left(\bs v_{m,c}; \hat{\bs v}_{m,c}, \bs S_c^{(v)} \right), &
\end{flalign}
where posterior mean $\hat{\bs v}_{m,c}$ and covariance $\bs S_c^{(v)}$ are computed using
equations of the same form as % those of the single user GPPL in
Equations \ref{eq:S_stochastic} and \ref{eq:fhat_stochastic}, except $\bs Q^{-1}$
 is scaled by expectations over $\bs w_{m,c}$,
and $\hat{\bs f}_{m,i}$ is replaced by $\hat{\bs v}_{m,c,i}$.
The factor for the inducing points of $\bs t$ follows a similar pattern to $\bs v_{m,c}$:
\begin{flalign}
\ln q(\bs t_m) & =
\mathbb{E}_{q(\bs w_{m,c'}\forall c', \bs v_{m,c'}\forall c')}\left[
\ln \mathcal{N}\left( \bs y; \tilde{\Phi}(\bs z), Q \right)
\right]
+ \ln\mathcal{N}\left( \bs t_m; \bs 0, \frac{\bs K_{mm}}{\mathbb{E}[s^{(t)}]} \right)
+ \textrm{const} & \nonumber \\
& = \ln \mathcal{N}\left( \bs t_m; \hat{\bs t}_{m}, \bs S^{(t)} \right), &
\end{flalign}
where the equations for $\hat{\bs t}$ and $\bs S^{(t)}$
are the same as Equations \ref{eq:S_stochastic} and \ref{eq:fhat_stochastic},
except $\hat{\bs f}_{m,i}$ is replaced by $\hat{\bs t}_{m,i}$.
%(see also Equations \ref{eq:St} and \ref{eq:hatt}).
Finally, %require a different linearization matrix, $\bs J \in P \times U$, containing partial derivatives
%of the pairwise likelihood with respect to $\hat{w}_c$. Its elements are given by:
%\begin{flalign}
%J_{p,j} = \Phi(\mathbb{E}[z_p])(1 - \Phi(\mathbb{E}[z_p]) (2y_p - 1) [u_p = j] % needs to be added or subtracted depending on a or b
%\end{flalign}
%now multiply by V. What about covariances between v?
the variational distribution for each inducing user's component is:% then as follows:
\begin{flalign}
\ln q(\bs w_{\! m,c} )  = &
\mathbb{E}_{q(\bs t,\bs w_{m,c'}\forall c'\backslash c, \bs v_{m,c'}\forall c')}\left[
\ln \mathcal{N}\! \left( \bs y; \tilde{\Phi}(\bs z), Q \right) \right]
+ \ln\mathcal{N}\!\left(\bs w_{\! m,c}; \bs 0, \frac{\bs L_{mm}}{\mathbb{E}[s^{(w)}_c]} \right)
+ \textrm{const} & \nonumber \\
& = \ln \mathcal{N}\left( \bs w_{m,c}; \hat{\bs w}_{\!m,c}, \bs \Sigma_c \right), &
\end{flalign}
where $\hat{\bs w}_c$ and $\bs \Sigma_{c}$ also follow the pattern of
Equations \ref{eq:S_stochastic} and \ref{eq:fhat_stochastic},
with $\bs Q^{-1}$ scaled by expectations of
$\bs w_{c,m}$,
 and $\hat{\bs f}_{m,i}$ replaced by $\hat{\bs w}_{m,c,i}$.
%(see also Appendix \ref{sec:post_params}, Equations \ref{eq:Sigma} and \ref{eq:what}).
We provide the complete equations for the variational means
and covariances for $\bs v_{m,c}$, $\bs t_m$ and $\bs w_{m,c}$ in
Appendix \ref{sec:post_params}.
The expectations for inverse scales, $s^{(v)}_1,..,s^{(v)}_c$, $s^{(w)}_1,..,s^{(w)}_c$
 and $s^{(t)}$ can be computed using Equation \ref{eq:qs} by
substituting the corresponding terms for $\bs v_c$, $\bs w_c$ or $\bs t$ instead of $\bs f$.

% The equations for the means and covariances
% can be adapted for stochastic updating by applying weighted sums over
% the stochastic update and the previous values in the
% same way as  Equation \ref{eq:S_stochastic} and \ref{eq:fhat_stochastic}.
% The stochastic updates for the inducing points of the latent factors depend
% on expectations with respect to the observed points.
% As with the single user case, the variational factors at the observed items are independent of the observations given the variational factors of the inducing points
% (likewise for the observed users):
% \begin{flalign}
% \ln q(\bs V) & = \sum_{c=1}^C \ln \mathcal{N}\left( \bs v_c; \bs A_v\hat{\bs v}_{m,c},
% \frac{\bs K_{v}}{\mathbb{E}[s^{(v)}_c]} + \bs A_v (\bs S_{m,c} - \frac{\bs K_{mm}}{\mathbb{E}[s^{(v)}_c]})\bs A_v \right) & \label{eq:qv} \\
% \ln q(\bs t) & = \ln \mathcal{N}\left( \bs t; \bs A_t \hat{\bs t}_m,
% \frac{\bs K_{t}}{\mathbb{E}[s^{(t)}]} + \bs A_t (\bs s^{(t)} - \frac{\bs K_{mm}}{\mathbb{E}[s^{(t)}]})\bs A_t \right)  & \label{eq:qt}\\
% \ln q(\bs W) & = \sum_{c=1}^C \ln \mathcal{N}\left( \bs w_c; \bs A_w \hat{\bs w}_{m,c}, \bs L_{} + \bs A_w (\bs\Sigma - \bs L_{mm}/s^{(w)}_c) \bs A_w \right). &
% \label{eq:qw}
% \end{flalign}
%As with GPPL, the stochastic updates are amenable to parallel computation within one iteration
%of the variational inference algorithm,
% by performing computations for mini-batches of training data in parallel.

Predictions for crowdGPPL can be made by computing the posterior mean utilities, $\bs F^*$,
and the covariance $\bs \Lambda_u^*$ for each user, $u$, in the test set:
\begin{flalign} \label{eq:predict_crowd}
&\bs F^* = \hat{\bs t}^* + \sum_{c=1}^C \hat{\bs v}_{c}^{*T} \hat{\bs w}_{c}^*, \hspace{1cm} \bs \Lambda_u^* = \bs C_{t}^* + \sum_{c=1}^C \omega_{c,u}^* \bs C_{v,c}^* + \hat{w}_{c,u}^2  \bs C_{v,c}^*  +\omega_{c,u}^* \hat{\bs v}_{c}\hat{\bs v}_{c}^T, &
\end{flalign}
where $\hat{\bs t}^*$, $\hat{\bs v}_{c}^*$ and $\hat{\bs w}_{c}^*$ are posterior test means,
$\bs C_{t}^*$ and $\bs C_{v,c}^*$ are posterior covariances of the test items,
and $\omega_{c,u}^*$ is the posterior variance of the user components for $u$.
(see Appendix \ref{sec:predictions}, Equations \ref{eq:tstar} to \ref{eq:omegastar}).
The mean $\bs F^*$ and covariances $\Lambda^*_u$ can be inserted into Equation \ref{eq:plphi} to predict pairwise labels.
In practice, the full covariance terms are needed only for Equation \ref{eq:plphi}, so need only be computed
between items for which we wish to predict pairwise labels.


\chapter{Half Wave Rectification}
	\section{Aim}
		\begin{itemize}
			\tightlist
			\item Explain Rectification
			\item Explain Half Wave Rectification
			\item Explain Half Wave Rectification:For Positive Half Cycle
			\item Explain Half Wave Rectification:For Negative Half Cycle
		\end{itemize}

	\section{Apparatus}
		\begin{itemize}
			\tightlist
			\item Silicon/Germanium Diode
			\item Resistor
			\item AC Power Source
			\item Oscilloscope
		\end{itemize}

	\section{Theory}
		\subsection{Rectification}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.9\linewidth]{img/exp6/1}
				\caption{Function of a Rectifier}
				\label{fig:rffxn}
			\end{figure}
			A rectifier is a device that converts alternating current (AC) to direct current (DC), a process known as rectification. Rectifiers are essentially of two types – a half wave rectifier and a full wave rectifier.

		\subsection{Half Wave Rectification}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.9\linewidth]{img/exp6/2}
				\caption{Half Wave Rectification}
				\label{fig:rfhw}
			\end{figure}
			On the positive cycle the diode is forward biased and on the negative cycle the diode is reverse biased. By using a diode we have converted an AC source into a pulsating DC source. In summary we have ‘rectified’ the AC signal.
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.3\linewidth]{img/exp6/3}
				\caption{Half Wave Rectification Circuit}
				\label{fig:rfhwc}
			\end{figure}
			The simplest kind of rectifier circuit is the half-wave rectifier.The half-wave rectifier is a circuit that allows only part of an input signal to pass. The circuit is simply the combination of a single diode in series with a resistor, where the resistor is acting as a load.

		\subsection{Half Wave Rectifiers – Waveforms}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.7\linewidth]{img/exp6/6}
				\includegraphics[width=0.7\linewidth]{img/exp6/4}
				\caption{Half Wave Rectification Wave Form}
				\label{fig:rfhwwf}
			\end{figure}
			The output DC voltage of a half wave rectifier can be calculated with the following two ideal equations.
			$$V_{peak}=V_{rms} \times \sqrt{2}$$
			$$V_{dc}=\frac{V_{peak}}{\pi}$$

		\subsection{Half Wave Rectification: For Positive Half Cycle}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.9\linewidth]{img/exp6/5}
				\caption{Half Wave Rectification - Positive Half Cycle}
				\label{fig:rfhwphc}
			\end{figure}
			Diode is forward biased, acts as a short circuit, passes the waveform through.

			For positive half cycle: $$V_I - V_b - I \times r_d - I \times R=0$$ where,
			$V_I$ is the input voltage,\\
			$V_b$ is barrier potential,\\
			$r_d$ is diode resistance,\\
			$I$ is total current,\\
			$R$ is resistance\\
			$$I=\frac{V_I - V_b}{r_d + R}$$
			$$V_O = I \times R$$
			$$V_O =\frac{V_I - V_b}{r_d + R} \times R$$
			For $r_d << R$,
			$$V_O = V_I- V_b$$
			$V_b$ is 0.3 for Germanium ,
			$V_b$ is 0.7 for Silicon

			For $V_I<V_b$,

			The diode will remain OFF.The Output voltage will be,
			$$V_O =0$$
			For $V_I>V_b$,

			The diode will be ON.The Output voltage will be,
			$$V_O = V_I- V_b$$

		\subsection{Half Wave Rectification: For Negative Half Cycle}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.9\linewidth]{img/exp6/7}
				\caption{Half Wave Rectification - Negative Half Cycle}
				\label{fig:rfhwnhc}
			\end{figure}
			Diode is reverse biased, acts as a open circuit, does not pass the waveform through.

			For negative half cycle:
			$$V_O=0 \quad Since, \quad I =0$$

		\subsection{Half wave Rectification: For an Ideal Diode}
			For Ideal Diode,
			$$V_b = 0$$
			For positive half cycle,
			$$V_O = V_I$$
			For negative half cycle,
			$$V_O = 0$$

		\subsection{Average output voltage}
			\begin{align*}
				V_O &= \begin{cases}
					V_m \sin wt, \quad\quad 0 \leq wt \leq \pi\\
					0, \quad\quad\quad\quad\quad \pi \leq wt \leq 2 \pi
				\end{cases}\\
				V_{av} &= \frac{V_m}{\pi} =0.318V_m
			\end{align*}

		\subsection{RMS load voltage}
			$$V_{rms}=I_{rms} \times R = \frac {V_m}{2}$$

		\subsection{Average load current}
			\begin{align*}
				I_{av} &= \frac{V_{av}}{R} =\frac{\frac{V_m}{\pi}}{R}\\
				I_{av} &= \frac{V_{m}}{\pi \times R}=\frac{I_m}{\pi}
			\end{align*}

		\subsection{RMS load current}
			$$I_{rms}=\frac {I_m}{2}$$

		\subsection{Form factor}
			It is defined as the ratio of rms load voltage and average load voltage.
			\begin{align*}
				F.F &= \frac{V_{rms}}{V_{av}}\\
				&= \frac{\frac{V_{m}}{2}}{\frac{V_{av}}{2}}=\frac{\pi}{2}=1.57
			\end{align*}
			$$F.F \geq 1$$
			$$rms \geq av$$

		\subsection{Ripple Factor}
			\begin{align*}
				\gamma &= \sqrt{{F.F}^2-1} \times 100\%\\
				&= \sqrt{{1.57}^2-1} \times 100\%\\
				&= 1.21\%
			\end{align*}
		\subsection{Efficiency}
			It is defined as ratio of dc power available at the load to the input ac power.
			\begin{align*}
				n\% &= \frac{P_{load}}{P_{in}} \times 100\%\\
				&= \frac {{I_{dc}^2} \times R}{{I_{rms}^2} \times R}\times 100\%\\
				&= \frac{\frac {I_{m}^2}{\pi^2}}{\frac{I_{m}^2}{4}}\times 100\%\\
				&= \frac{4}{\pi^2}\times 100\%\\
				&= 40.56 \%
			\end{align*}

		\subsection{Peak Inverse Volatge}
			For rectifier applications, peak inverse voltage (PIV) or peak reverse voltage (PRV) is the maximum value of reverse voltage which occurs at the peak of the input cycle when the diode is reverse-biased.The portion of the sinusoidal waveform which repeats or duplicates itself is known as the cycle. The part of the cycle above the horizontal axis is called the positive half-cycle, the part of the cycle below the horizontal axis is called the negative half cycle. With reference to the amplitude of the cycle, the peak inverse voltage is specified as the maximum negative value of the sine-wave within a cycle's negative half cycle.

			$$ V_{PIV}=V$$
			$$ -V_m +V=0 \Rightarrow V=V_m$$
			$$ V_{PIV} \geq V_m$$

	\section{Procedure}
		\begin{enumerate}
			\tightlist
			\item Set the resistor $R_L$.
			\item Click on 'ON' button to start the experiment.
			\item Click on 'Sine Wave' button to generate input waveform
			\item Click on 'Oscilloscope' button to get the rectified output.
			\item Vary the Amplitude, Frequency, volt/div using the controllers.
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.7\linewidth]{img/exp6/8}
				\caption{}
				\label{fig:rfhwp}
			\end{figure}
			\item Click on "Dual" button to observe both the waveform.
			\item Channel 1 shows the input sine waveform, Channel 2 shows the output rectified waveform.
			\item Calculate the Ripple Factor.Theoretical Ripple Factor= 1.21.
		\end{enumerate}

	\section{Calculations}
		Measure the $V_m$

		$$V_{rms}= \frac{V_m}{2}$$
		$$V_{dc}= \frac{V_m} {\pi}$$
		Ripple Factor($RP$)$$RP = \frac{V_{ac}}{V_{dc}}$$
		Since, $$V_{ac}=\sqrt{(V^2_{rms}-V^2_{dc})}$$
		Peak Current: $0.74999999 mA$

	\section{Observations}
		\begin{figure}[h]
			\centering
			\begin{tikzpicture}
				\begin{axis}[
					domain=0:3*360,
					samples=4*360,
					xtick=\empty,
					width=15cm, height=6cm,
					ymin=-1,
					enlarge x limits=false
					]
					\addplot [red, very thick] {sin(x)};
				\end{axis}
			\end{tikzpicture}
			\caption{Un-Rectified AC Waveform}
		\end{figure}
		\begin{figure}[h]
			\centering
			\begin{tikzpicture}
				\begin{axis}[
					domain=0:3*360,
					samples=4*360,
					xtick=\empty,
					width=15cm, height=6cm,
					ymin=0,
					enlarge x limits=false
					]
					\addplot [blue, very thick] {sin(x)};
				\end{axis}
			\end{tikzpicture}
			\caption{Half Wave Rectified Waveform}
		\end{figure}

	\section{Result}
		A rectifier is a device that converts alternating current (AC) to direct current (DC). It is done by using a diode or a group of diodes. Half wave rectifiers use one diode, while a full wave rectifier uses multiple diodes.

		The working of a half wave rectifier takes advantage of the fact that diodes only allow current to flow in one direction.


% Created 2020-10-06 mar 18:24
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=169, usenames, dvipsnames]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{amssymb}
\usepgfplotslibrary{groupplots}
\usepackage{pgfplotstable}
\newcommand*{\shift}{\operatorname{q}}
\definecolor{ppc}{rgb}{0.1,0.1,0.6}
\definecolor{iic}{rgb}{0.6,0.1,0.1}
\definecolor{ddc}{rgb}{0.1,0.6,0.1}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{2020-10-06}
\title{Process Automation Laboratory  - Anti-windup assignment}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Process Automation Laboratory  - Anti-windup assignment},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Context}
\label{sec:org7348bef}
\begin{frame}[label={sec:orgccface9}]{The two-tank system}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/two-tanks-shutoff-valve.png}
\end{center}
\end{frame}

\section{Anti-windup}
\label{sec:orge5ed4e3}

\begin{frame}[label={sec:org755088d}]{Anti-windup using back-calculation - Spot the mistake}
\begin{center}
\includegraphics[height=0.38\textheight]{../../figures/astrom-back-tracking.png}
\includegraphics[height=0.42\textheight]{../../figures/back-tracking-review.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgede0024}]{Anti-windup using back-calculation}
\begin{center}
\includegraphics[width=.82\linewidth]{../../figures/antiwindup-review.png}
\end{center}
\end{frame}
\end{document}


% !Mode:: "TeX:UTF-8"
% !TEX program  = xelatex
\title{What Angle Should We Throw a Football for Maximum Range}
\author[Iydon]{Iydon at DataHub}
\date{\today}
\institute[SUSTech]{
    Department of Mathematics \\
    Southern University of Science and Technology
}

\begin{frame}
	\maketitle
\end{frame}

\section{Introduction}
\begin{frame}[t]{Projectile Motion}
    \begin{block}{Projectile motion}
        Projectile motion is a form of motion experienced by an object or particle (a projectile) that is thrown near the Earth’s surface and moves along a curved path under the action of gravity only\footnote{In particular, the effects of air resistance are assumed to be negligible.}.
    \end{block}
\end{frame}

\begin{frame}{Projectile Motion}
    \begin{figure}
        \centering
        \includegraphics[width=.7\textwidth]{../figures/quadratic_equation-1.png}
        \caption{Parabolic trajectories at different angles}
    \end{figure}
\end{frame}

\section{Quadratic Equation}
\begin{frame}[t]{Definition}
    \begin{block}{Quadratic equation}
        In algebra, a quadratic equation (from the Latin \emph{quadratus} for ``square'') is any equation having the form
        \begin{equation}\label{E:1}
            c_2 x^2 + c_1 x + c_0 = 0,
        \end{equation}
        where $x$ represents an unknown, and $c_n$ ($n=0,1,2$) represent known numbers, with $a\neq 0$. If $a=0$, then the equation is linear, not quadratic, as there is no $c_2 x^2$ term. Then we can divide both sides of the equation by $c_2$, and replace $c_1/c_2$ with $b$, $c_0/c_2$ with $c$.
        \begin{equation}\label{E:quadratic-equation}
            x^2 + bx + c = 0.
        \end{equation}
        That is, every quadratic equation can be transformed into the form of Equation~\eqref{E:quadratic-equation}.
    \end{block}
\end{frame}

\begin{frame}[t]{Quadratic Formula and Its Derivation}
    \begin{equation}\label{E:quadratic-formula-1}
        \begin{aligned}
            x^2 + bx + c &= 0 \\
            x^2 + 2\frac{b}{2}x &= -c \\
            \left(x+\frac{b}{2}\right)^2 &= \frac{b^2-4c}{4}.
        \end{aligned}
    \end{equation}

    The left side of the Equation~\eqref{E:quadratic-formula-1} is a squared form, which means that it may have multiple solutions. That is, if $b^2-4c\geq 0$,
    \begin{equation}\label{E:quadratic-formula-2}
        \begin{aligned}
            x + \frac{b}{2} &= \pm \frac{\sqrt{b^2-4c}}{2} \\
            x &= \frac{-b\pm\sqrt{b^2-4c}}{2}.
        \end{aligned}
    \end{equation}
\end{frame}

\section{Applications}
\begin{frame}[t]{Solution to Article Title}
    \begin{figure}
        \centering
        \includegraphics[width=.5\textwidth]{../figures/quadratic_equation-3.png}
        \caption{Parabolic trajectories at different angles}
    \end{figure}
\end{frame}

\begin{frame}[t]{Solution to Article Title}
    we have both horizontal and vertical distance at time $t$,
    \begin{equation}\label{E:solution-2}
        \begin{cases}
            S_x &= V\cos(\theta)t \\
            S_y &= V\sin(\theta)t - \frac{1}{2} gt^2
        \end{cases}
    \end{equation}

    We can find all of the coordinate $(S_x, S_y)$ with angle $\theta$. From Equation~\eqref{E:solution-2}, we have $t=\tfrac{2V\sin(\theta)}{g}$, then we substitute the expression of $t$ back to the $S_y$, we have the quadratic equation
    \begin{equation}\label{E:solution-3}
        S_y = \tan(\theta)S_x - \frac{g}{2V^2\cos^2(\theta)}S_x^2.
    \end{equation}

    From quadratic formula, we can solve this quadratic equation, the solutions are $0$ and $\tfrac{2V^2}{g}\sin(\theta)$. Obviously, $0$ is the initial position, then the football will land at $\tfrac{2V^2}{g}\sin(\theta)$, that is, $\tfrac{V^2}{g}\sin(2\theta)$.
\end{frame}

\begin{frame}[t]{Simulation of Projectile Motion}
    \begin{block}{}
        You can use quadratic equation to simulate projectile motion, which can solve free fall problems. If you are familiar with Python, you can use Python to draw the pictures of trajectories, and you will find it easy to use Python to solve quadratic equations, especially in symbolic calculations. And the code is in appendix of article.
    \end{block}
\end{frame}

\section{Conclusions}
\begin{frame}[t]{Conclusions}
    \begin{block}{}
        From this article, we introduce projectile motion and quadratic equation through a real world problem --- \emph{what angle should we throw a football for maximum range}, then we give a proper and accurate definition of quadratic equation, from which we derive quadratic formula for solving quadratic equations. Furthermore, we list some useful applications of the quadratic equation and implement them in \texttt{Python}.
    \end{block}
\end{frame}

\section*{Acknowledgments}
\begin{frame}[t]{Acknowledgments}
    \begin{itemize}
        \item The author would like to thank the Associate Editor and one referee for their valuable comments which have greatly improved the paper.
        \item The author’s research is fully supported by the DataHub Organization of China (No. 1000001).
    \end{itemize}
\end{frame}

\begin{frame}{Acknowledgments}
    \centering\Huge Thank you!
\end{frame}

\section*{References}
\begin{frame}[t]{References}
	\begin{thebibliography}{9}\large
        \bibitem{C:1} Wikipedia contributors. Projectile motion — Wikipedia, the free encyclopedia[EB/OL]. 2019. \url{https://en.wikipedia.org/w/index.php?title=Projectile_motion&oldid=928426750}.
        \bibitem{C:2} Wikipedia contributors. Trajectory — Wikipedia, the free encyclopedia[EB/OL]. 2019. \url{https://en.wikipedia.org/w/index.php?title=Trajectory&oldid=929280103}.
        \bibitem{C:3} Wikipedia contributors. Quadratic equation — Wikipedia, the free encyclopedia [EB/OL]. 2019. \url{https://en.wikipedia.org/w/index.php?title=Quadratic_equation&oldid=929716375}.
    \end{thebibliography}
\end{frame}