\documentclass[11pt,a4paper,fleqn]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks=true, citecolor=blue, linkcolor=blue, filecolor=blue,urlcolor=blue]{hyperref}
\hypersetup{
     colorlinks   = true,
     citecolor    = gray
}
\usepackage{wrapfig}

\usepackage{caption}
\captionsetup{format=plain, indent=5pt, font=footnotesize, labelfont=bf}

\setkomafont{disposition}{\scshape\bfseries}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
%\usepackage{bbm}
% \usepackage{mathtools}
% \usepackage{epsfig}
% \usepackage{grffile}
%\usepackage{times}
%\usepackage{babel}
\usepackage{tikz}
\usepackage{paralist}
\usepackage{color}
\usepackage[top=3cm, bottom=2.5cm, left=2.5cm, right=3cm]{geometry}
%\setlength{\mathindent}{1ex}

% PGF
\usepackage{pgfplots}
\usepackage{pgf}
\usepackage{siunitx}
\usepackage{xfrac}
\usepackage{calculator}
\usepackage{calculus}
\usepackage{eurosym}
\usepackage{booktabs}
%\sisetup{per-mode=fraction,%
%	fraction-function=\sfrac}

%\newcommand{\eur}[1]{\EUR{#1}\si{\per\kilo\meter}}
\pgfplotsset{
  compat=newest,
  every axis/.append style={small, minor tick num=3}
}

%\usepackage[backend=biber,style=alphabetic,url=false,doi=false]{biblatex}
%\addbibresource{sheet01_biber.bib}
% \addbibresource{/home/coroa/papers/refs.bib}

\newcommand{\id}{\mathbbm{1}}
\newcommand{\NN}{{\mathbbm{N}}}
\newcommand{\ZZ}{{\mathbbm{Z}}}
\newcommand{\RR}{{\mathbbm{R}}}
\newcommand{\CC}{{\mathbbm{C}}}
\renewcommand{\vec}[1]{{\boldsymbol{#1}}}

\renewcommand{\i}{\mathrm{i}}

\newcommand{\expect}[1]{\langle\,#1\,\rangle}
\newcommand{\e}[1]{\ensuremath{\,\mathrm{#1}}}

\renewcommand{\O}{\mc{O}}
\newcommand{\veps}{\varepsilon}
\newcommand{\ud}[1]{\textup{d}#1\,}

\newcommand{\unclear}[1]{\color{green}#1}
\newcommand{\problem}[1]{\color{red}#1}
\newcommand{\rd}[1]{\num[round-mode=places,round-precision=1]{#1}}

%\DeclareSIUnit{\euro}{\EUR}
\DeclareSIUnit{\dollar}{\$}
\newcommand{\eur}{\text{\EUR{}}}

\usepackage{palatino}
\usepackage{mathpazo}
\setlength\parindent{0pt}
\usepackage{xcolor}
\usepackage{framed}
\definecolor{shadecolor}{rgb}{.9,.9,.9}

\def\cap{\text{Cap}}
\def\floor{\text{Floor}}
\def\l{\lambda}
\def\m{\mu}
\def\d{\partial}
\def\cL{\mathcal{L}}
\def\co2{CO${}_2$}

\def\mw{\text{ MW}}
\def\mwh{\text{ MWh}}
\def\gw{\text{ GW}}
\def\gwh{\text{ GWh}}
\def\emwh{\text{ \euro/MWh}}
\def\bemwh{\text{ [\euro/MWh]}}
\newcommand{\ubar}[1]{\text{\b{$#1$}}}

%=====================================================================
%=====================================================================
\begin{document}

\begin{flushright}
  \textbf{Energy System Modelling }\\
  {\small Karlsruhe Institute of Technology}\\
  {\small Institute for Automation and Applied Informatics}\\
  {\small Summer Term 2020}\\
 \end{flushright}

  \vspace{-0.5em}
  \hrulefill
  \vspace{0.3em}

 \begin{center}
  \textbf{\textsc{\Large Solutions IV: Electricity Markets}}\\
  \small Will be worked on in the exercise session on Friday, 25 June 2020.\\[1.5em]
 \end{center}

 \vspace{-0.5em}
 \hrulefill
 \vspace{0.8em}

%=============== ======================================================
\paragraph{Solution IV.1 \normalsize (Shadow prices of limits on consumption).}~\\
%=====================================================================

Suppose that the utility for the electricity consumption of an industrial company is given by
\[
U(d) = 70d - 3d^2 [\textrm{\euro}/\si{\hour}] \quad , \quad d_{min}=2\leq d \leq d_{max}=10,
\]
where $d$ is the demand in MW and $d_{min}, d_{max}$ are the minimum and maximum demand. \\
[1em]
Assume that the company is maximising its net surplus for a given electricity price $\pi$, i.e. it maximises $\max_{d} \left[U(d) -
\pi d\right]$.
\begin{enumerate}[(a)]
 \begin{shaded} \item If the price is $\pi = 5$~\euro/MWh, what is the optimal
  demand $d^*$?  What is the value of the KKT multiplier $\mu_{max}$
  for the constraint $d \leq d_{max}=10$ at this optimal solution?
  What is the value of $\mu_{min}$ for $d \geq d_{min} = 2$?\end{shaded}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 We convert the exercise to an optimisation problem with objective
 \begin{align}
  &f(d) = U(d) - \pi d = (70-\pi) d - 3d^2 \\
  &\max\limits_{d \in \mathbb{R}} \ f(d)
 \end{align}

 with constraints
 \begin{align}
  d  & \leq d_{max} = 10 \hspace{1cm}\leftrightarrow\hspace{1cm} \m_{max}  \\
  -d & \leq -d_{min} = -2 \hspace{1cm}\leftrightarrow\hspace{1cm} \m_{min}
 \end{align}
 \rule{\textwidth}{0.4pt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 From stationarity for the optimal point we get:
 \begin{align}
  0 & =   \frac{\d \mathcal{L}}{\d d} = \frac{\d f}{\d d} - \sum_{i} \lambda_i^* \frac{\d g_i}{\d d} -  \sum_{j} \mu_j^* \frac{\d h_j}{\d d}\\
  0 & =   \frac{\d}{\d d} \left((70-\pi) d - 3d^2 \right) - \m_{max} + \m_{min} \\
    & =  (70-\pi) - 6d - \m_{max} + \m_{min} \label{eq:2stat}
 \end{align}

 Note that it does not matter whether you pull in the constant of the right-hand side of the respective constraint.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[h]
	\centering
	\includegraphics{graph_u_problem1.pdf}
	\caption{utility for the electricity consumption with demand d}
	\label{fig:solution04tutorial}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 The marginal utility curve is $U'(d) = 70 - 6d$ [\euro/MWh]. At
 $\pi = 5$ and if the demand were unconstrained, the demand would be determined by $5=70-6d$, i.e. $d =
  65/6 = 10.8333$, which is above the consumption limit
 $d_{max} = 10$. Therefore the optimal demand is \\
  $d^* = 10$,\\
   the upper limit is binding such that $\mu_{max} \geq 0$ and the lower limit is non-binding such that\\
    $\mu_{min} = 0$. \\
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 To determine the value of $\mu_{max}$ we use \eqref{eq:2stat} to get
 \begin{equation*}
	\m_{max} =  (70-\pi) - 6d^* - \mu_{min} =  70 - 5 -60 = 5
 \end{equation*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 \begin{shaded}
  \item Suppose now the electricity price is $\pi = 60$~\euro/MWh. What are
  the optimal demand $d^*$, $\mu_{max}$ and $\mu_{min}$ now?
 \end{shaded}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 At $\pi = 60$, the demand would be determined by $60=70-6d$, i.e. $d = 10/6 = 1.667$, which is below the consumption limit $d_{min} = 2$. Therefore the optimal demand is \\
  $d^* = 2$,\\
   the upper limit is non-binding such that\\
    $\mu_{max}= 0$\\
 and the lower limit is binding such that $\mu_{min} \geq 0$.

 To determine the value of $\mu_{min}$ we use \eqref{eq:2stat} to get
 \begin{equation*}
   \m_{min} = -(70-\pi) + 6d^* + \mu_{max} = -(70 - 60) + 12 + 0 = 2.
 \end{equation*}

 \newpage
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%=============== ======================================================
\paragraph{Solution IV.2 \normalsize (Economic dispatch in a single bidding zone).}~\\
%=====================================================================

Consider an electricity market with two generator types, one with the cost function $C_1(g_1)=c_1g_1$ with variable cost $c_1 = 20\emwh$, capacity $G_1 = 300\mw$ and a dispatch rate of $g_1$~[MW], and another with the cost function $C_2(g_2)=c_2g_2$ with variable cost $c_2=50\emwh$, capacity $G_2=400\mw$ and a dispatch rate of $g_2$~[MW]. The demand has utility function $U(d) = 8000d - 5d^2$~[\euro/h] for a consumption rate of $d$~[MW].
\begin{enumerate}[(a)]
 \begin{shaded}\item What are the objective function and constraints required for an optimisation problem to maximise short-run social welfare in this market?\end{shaded}

 The optimisation problem has the objective function:
 \begin{align*}
 &f(d, g_1, g_2) =  U(d) - C_1(g_1) - C_2(g_2) = 8000d-5d^2 - c_1g_1 - c_2g_2 \\
 & \max_{d,g_1,g_2}f(d, g_1, g_2)
 \end{align*}
 with constraints:
 \begin{align*}
  d - g_1 - g_2 & = 0 \leftrightarrow \l              \\
  g_1           & \leq G_1 \leftrightarrow \bar{\m}_1 \\
  g_2           & \leq G_2 \leftrightarrow \bar{\m}_2 \\
  -g_1          & \leq 0 \leftrightarrow \ubar{\m}_1  \\
  -g_2          & \leq 0 \leftrightarrow \ubar{\m}_2
 \end{align*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{shaded}\item Write down the Karush-Kuhn-Tucker (KKT) conditions for this problem.\end{shaded}

\textbf{ Stationarity} gives for $d$:
 \begin{align*}
   0 & =   \frac{\d \mathcal{L}}{\d d} = \frac{\d f}{\d d} - \sum_{i} \lambda_i^* \frac{\d g_i}{\d d} -  \sum_{j} \mu_j^* \frac{\d h_j}{\d d}\\
   &= \frac{\d U}{\d d} - \l \\
   & = 8000 - 10d - \l
 \end{align*}
 Stationarity for $g_1$ gives:
 \begin{equation*}
  -\frac{\d C_1}{\d g_1} + \l - \bar{\m}_1 + \ubar{\m_1}  =  -c_1+ \l - \bar{\m}_1 + \ubar{\m_1} = 0
 \end{equation*}
 Stationarity for $g_2$ gives:
 \begin{equation*}
  -\frac{\d C_2}{\d g_2} + \l - \bar{\m}_2 + \ubar{\m_2}  =  -c_2+ \l - \bar{\m}_2 + \ubar{\m_2} = 0
 \end{equation*}
\textbf{ Primal feasibility} is just the generator limits above in (a). \\
 \textbf{Dual feasibility} is $\bar{\m}_i^*,\ubar{\m}_i^* \geq 0$ and \\
  \textbf{complementary slackness} is $\bar{\m}_i^*(g_i^*-G_i) = 0$ and $\ubar{\m}_i^* g_i^* = 0$ for $i=1,2$.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{shaded}\item Determine the optimal rate of production of the generators and the value of all KKT multipliers. What is the interpretation of the respective KKT multipliers?\end{shaded}
 The marginal utility at the full output of the generators,
 \begin{align*}
 &G_1 + G_2 = 300\ \text{MW} + 400\ \text{MW} = 700\ \text{MW} \\
 &U'(700) = 8000 - 10\cdot700 = 1000\ \text{\euro/MWh}
 \end{align*}
 which is higher than the costs $c_i$, so we'll find optimal rates
 \begin{align*}
 g_1^* &= G_1 = 300 \ \text{MW} \\
 g_2^* &= G_2 = 400 \ \text{MW} \\
 d^* &= G_1+G_2 = 700\ \text{MW}
 \end{align*}
This means (from stationarity)
\begin{equation*}
\l^*= \frac{\d U}{\d d} \bigg|_{d=d^*} = 8000 - 10d^* = 1000 \text{\euro/MWh},
\end{equation*}
which is the market price. Because the lower constraints on the generator output are not binding, from complementary slackness we have $\ubar{\m}_i = 0$. The upper constraints are binding, so $\bar{\m}_i \geq 0$. \\
 From stationarity $\bar{\m}_i =
  \l - c_i + \ubar{\m}_i$, which is the increase in social welfare if Generator $i$
 could increase its capacity by a marginal amount.

 $$\bar{\m}_1=1000-20=980 \text{\euro/MWh}$$
 $$\bar{\m}_2=1000-50=950 \text{\euro/MWh}$$

\end{enumerate}
\newpage
%=============== ======================================================
\paragraph{Solution IV.3 \normalsize (efficient dispatch in a two-bus power system).}~\\
%=====================================================================

\begin{figure}[h]
 \centering
 \includegraphics[width=14cm]{two-bus}
 \caption{A simple two-bus power system.}
 \label{twobus}
\end{figure}

Consider the two-bus power system shown in Figure \ref{twobus}, where the two nodes represent two markets, each with different total demand $D_i$, and one generator at each node producing $G_i$. At node A the demand is $D_A = 2000 \si{\mega\watt}$, whereas at node B the demand is $D_B = 1000 \si{\mega\watt}$. Furthermore, there is a transmission line with a capacity denoted by $F_{AB}$. The marginal cost of production of the generators connected to buses A and B are given respectively by the following expressions:
\begin{align*}
 MC_A & = 20 + 0.03 P_A \hspace{1cm}\eur/\si{\mega\watt\hour}  \\
 MC_B & = 15 + 0.02 P_B \hspace{1cm} \eur/\si{\mega\watt\hour}
\end{align*}

Assume that the demands $D_A$ and $D_B$ are constant and insensitive to price, that energy is sold at its marginal cost of production and that there are no limits on the output of the generators.

\begin{enumerate}[(a)]
 \begin{shaded}\item Calculate the price of electricity at each bus, the production
  of each generator, and the flow on the line for the following cases. You may also calculate the values of any KKT multiplier as a bonus.\end{shaded}

The price of electricity is the value of the dual variable at the nodal balance equation.

 Use the following nomenclature: price $\lambda_{A/B}$, generation $G_{A/B}$, flow $F_{AB}$.
 \begin{enumerate}[(i)]
  \begin{shaded}\item The line between buses A and B is disconnected.\end{shaded}

  Where to start: $P_A=D_A$ and $P_B=D_B$ and substitute into $MC_i$.

  $\l_A= 80\emwh$, $\l_B=35\emwh$,

  $G_{A}=2000$ MW, $P_B=1000$ MW, $F_{AB}=0$

  \begin{shaded}\item The line between buses A and B is in service and has an unlimited capacity.\end{shaded}

  Where to start:
  No restriction in transmission, so prices must be the same for the two nodes: $\l_A = \l_B$, therefore, $MC_A = MC_B$.
  Also: $P_A+P_B = D_A+D_B$.

    $\l_A= 53\emwh$, $\l_B=53\emwh$,

  $G_{A}=1100\mw$, $P_B=1900$ MW, $F_{AB}=-900\mw$

  \begin{shaded}\item The line between buses A and B is in service and has an unlimited capacity, but the maximum output of Generator B is 1500~MW.\end{shaded}

  Where to start:
  $P_B=1500$ MW since it is now constrained but would have been higher in the unconstrained case (ii).
  Also, since there are no transmission constraints: $\l_A = \l_B$.
  Also: $P_A+P_B = D_A+D_B$.

    $\l_A= 65\emwh$, $\l_B=65\emwh$,

  $G_{A}=1500\mw$, $P_B=1500$ MW, $F_{AB}=-500\mw$

  \begin{shaded}\item The   line between buses A and B is in service and has an unlimited capacity, but the maximum output of Generator A is 900~MW. The output of Generator B is unlimited.\end{shaded}

  Where to start:
  $P_A=900$ MW since it is now constrained but would have been higher in the unconstrained case (ii).
  Also, since there are no transmission constraints: $\l_A = \l_B$.
  Also: $P_A+P_B = D_A+D_B$.

    $\l_A= 57\emwh$, $\l_B=57\emwh$,

  $G_{A}=900\mw$, $P_B=2100$ MW, $F_{AB}=-1100\mw$

  \begin{shaded}\item The line between buses A and B is in service but its capacity is limited to 600~MW. The output of the generators is unlimited.\end{shaded}

    Where to start:
    $F_{AB} = - 600$ MW since we would want even more transmission, if it were not constrained.
    Also, $P_A+F_{AB}=2000$ MW.

    $\l_A= 62\emwh$, $\l_B=47\emwh$,

  $G_{A}=1400\mw$, $P_B=1600$ MW, $F_{AB}=-600\mw$
 \end{enumerate}
 \begin{shaded}\item Calculate the generator revenues, generator profits, consumer payments and consumer net surplus for all the cases considered in the above problem. Who benefits from the line connecting these two buses?\end{shaded}
 Generator revenues $R_{i}$, generator costs $C_{i}$, generator profits $P_{i}$, consumer payments $E_{i}$. Find the generator profits by subtracting the costs from the revenue. Costs are given by integrating the marginal cost, i.e. $C_A = 20P_A + 0.015P_A^2$ and $C_B = 15P_B + 0.01P_B^2$. The generator at $B$ and the consumers at $A$ benefit from the line (price increases at $B$, decreases at $B$).
 \begin{table}[!h]
  \centering
  \begin{tabular}{lrrrrr}
   \toprule
   Case          & (i)      & (ii)     & (iii)    & (iv)     & (v)      \\
   \midrule
   $E_A$ (\euro) & 160,000 & 106,000 & 130,000 & 114,000 & 124,000 \\
   $R_A$ (\euro) & 160,000 & 58,300  & 97,500  & 51,300  & 86,800  \\
   $C_A$ (\euro) & 100,000 & 40,150  & 63,750  & 30,150  & 57,400  \\
   $P_A$ (\euro) & 60,000  & 18,150  & 33,750  & 21,150  & 29,400  \\
   \midrule
   $E_B$ (\euro) & 35,000  & 53,000  & 65,000  & 57,000  & 47,000  \\
   $R_B$ (\euro) & 35,000  & 100,700 & 97,500  & 119,700 & 75,200  \\
   $C_B$ (\euro) & 25,000  & 64,600  & 45,000  & 75,600  & 49,600  \\
   $P_B$ (\euro) & 10,000  & 36,100  & 52,500  & 44,100  & 25,600  \\
   \bottomrule
  \end{tabular}
 \end{table}

 \begin{shaded}\item Calculate the congestion surplus for case (v). For what values of the flow on the line between buses A and B is the congestion surplus equal to zero?\end{shaded}
 Congestion surplus is 9000 \euro:
 \begin{equation*}
  \left(E_A + E_B\right) - (R_A + R_B) = |F_{AB}|\times (\l_A - \l_B)
 \end{equation*}
 Congestion surplus is equal to zero when the flow $F_{AB}=0$, or when it is equal to the unconstrained value $F_{AB}=-900\mw$ (then $\l_A = \l_B$).
\end{enumerate}

\end{document}


\documentclass[12pt,a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[charter]{mathdesign}
\usepackage{amsmath,amsthm,enumitem,graphicx,titlesec,xcolor}
\usepackage{microtype}
\usepackage[a4paper,margin=25mm]{geometry}
\usepackage[unicode]{hyperref}

\hypersetup{
    hidelinks,
    pdftitle={Distributed Algorithms},
    pdfauthor={Juho Hirvonen and Jukka Suomela},
}

\definecolor{titlecolor}{HTML}{0088cc}
\definecolor{hlcolor}{HTML}{f26924}

\newcommand{\q}[2]{\paragraph{\mbox{Question #1: }#2.}}
\newcommand{\sep}{{\centering \raisebox{-3mm}[0mm][0mm]{$*\quad*\quad*$}\par}}
\newcommand{\hl}[1]{\textbf{\emph{#1}}}
\newcommand{\cemph}[1]{\textcolor{hlcolor}{\emph{#1}}}
\DeclareMathOperator{\re}{re}

\setitemize{noitemsep,leftmargin=3ex}

\titleformat{\paragraph}[runin] {\normalfont\normalsize\bfseries\color{titlecolor}}{\theparagraph}{1em}{}

\begin{document}

\noindent
\emph{CS-E4510 Distributed Algorithms / Juho Hirvonen, Jukka Suomela\\
exam, 26 February 2020}

\paragraph{Instructions.}

There is only one question, which is an open-ended small research project. Your answer has to demonstrate \hl{at least} that you understand both the PN and LOCAL models of distributed computing, you can design efficient distributed algorithms, and you can also prove negative results; you do not need to do more than what is reasonable to expect to finish in \hl{3 hours}.

You are free to look at any source material (this includes lecture notes, textbooks, and anything you can find with Google), but you are not allowed to collaborate with anyone else or ask for anyone's help (this includes collaboration with other students and asking for help in online forums). You are free to use any results from the lecture notes directly (including the results from the regular exercises). You are free to use computers and computer programs to find solutions.

\sep

\paragraph{Definition.}

Let $G = (V,E)$ be a graph, and let $a$ and $b$ be real numbers. We say that a function $f\colon V \to [0,1]$ is an $(a,b)$-labeling if the following holds: for all edges $\{u,v\} \in E$ we have got $a \le \bigl|f(u) - f(v)\bigr| \le b$.

\sep

\paragraph{Question.}

Your task is to study distributed algorithms for finding $(a,b)$-labelings, for different values of parameters $a$ and $b$. You can first consider the case that graph $G$ is a \hl{cycle}. What can you say about the problem in that case?
\begin{itemize}
    \item For what values of $a$ and $b$ a solution always exists in any cycle?
    \item For what values the problem cannot be solved at all in the deterministic PN model?
    \item For what values the problem can be solved in $O(1)$ rounds in the deterministic LOCAL model?
    \item For what values the problem can be solved in $o(n)$ rounds in the deterministic LOCAL model? [Note, it is small-$o$, not big-$O$.]
    \item For what values the problem requires $\Omega(n)$ rounds in the deterministic LOCAL model?
\end{itemize}
You do not need to give a full characterization that covers all possible $(a,b)$ pairs, but you should be able to say at least something positive and something negative. If you have time or you run out of questions you can solve, you can also consider similar questions in other graph families, e.g.\ \hl{paths} or \hl{trees}, and/or other models of computing, e.g., randomized PN or deterministic CONGEST.

\sep

\paragraph{Hints.}

To get started, you can consider e.g.\ the following questions:
\begin{itemize}
    \item What does a $(0,0)$-labeling look like in a cycle? Does it always exist, is it easy to find?
    \item What does a $(0,1)$-labeling look like in a cycle? Does it always exist, is it easy to find?
    \item What does a $(1,1)$-labeling look like in a cycle? Does it always exist, is it easy to find?
    \item What does a $(0.5,1)$-labeling look like in a cycle? Does it always exist, is it easy to find?
    \item What does a $(0.1,0.1)$-labeling look like in a cycle? Does it always exist, is it easy to find?
    \item What does a $(0.1,0.2)$-labeling look like in a cycle? Does it always exist, is it easy to find?
\end{itemize}

\end{document}


\section{Variable selection}\label{ssec:varselection}

\subsection{Synthetic controls variable weighting algorithm}

In this section we give further details about the variable weighting procedure of the synthetic controls algorithm, which we allude to in the primary paper. We also details about the types of tuning procedures that have been proposed using pre-treatment data, and again argue that these procedures may be sub-optimal when predicting the ETC. This is not meant to be a comprehensive treatment of these issues, but rather to more formally illustrate the problem of using pre-treatment data to conduct variable selection or variable weighting when estimating the ETC. For further details on the specifics of these ideas, we refer to \cite{abadie2010synthetic}, \cite{abadie2015comparative}, \cite{kaul2015synthetic}.

The synthetic controls algorithm chooses the weights $\gamma$ that minimizes the weighted L2-squared distance of the covariates using a diagonal weighting matrix $V$. $V$ is then chosen to minimize the mean-square error of the weighted difference in pre-treatment outcomes. Letting $Z_a$ be the matrix of pre-treatment outcomes for treatment group $A = a$, the synthetic controls algorithm solves the following optimization problem for a fixed $V$:

\begin{align}
\gamma(V) = \arg\min_{\tilde{\gamma}(V^\star)} = (\bar{X}_1 - X_0^T\tilde{\gamma})'V(\bar{X}_1 - X_0^T\tilde{\gamma})
\end{align}

This is the ``inner'' optimization. $V^\star$ is then determined in an ``outer'' optimization to minimize the imbalances in the pre-treatment outcomes $Z$:

\begin{align}
    V^\star = \arg\min_V (\bar{Z}_1 - Z_0^T\gamma(V))'(\bar{Z}_1 - Z_0^T\gamma(V))
\end{align}

In applications the covariate matrix $X_a$ may contain some elements of $Z_a$. In cases where $X_a$ contains all pre-treatment outcomes, \cite{kaul2015synthetic} has shown that the predictor weights $V^\star$ will give no weight to auxillary covariates (covariates that are not the pre-treatment outcomes), rendering these irrelevant to the model.

While in practice $V$ is often learned on the same data as the weights, \cite{abadie2010synthetic} also propose to use cross-validation to choose $V$. For this procedure, we assume we have enough data that we can divide our pre-treatment data into a training data from periods $T = 1, ..., T - l - 1$, a validation period from periods $T - l, ..., T - 1$, and a post-treatment period at time $T$. To make this discussion more general, assume that we are evaluating a set of candidate models $\mathcal{M}$ on the validation data (where for the synthetic controls algorithm we can think of this as the set of all possible weighting matrices $V$). Let $\bar{Y}^a_{a', t}$ be the mean potential outcome under treatment $A = a$ for treatment group $A = a'$ at time $t$ (where $t$ occurs during the validation period). Let $\hat{\bar{Y}}^a_{a'', t}(m)$ be an estimator of that potential outcome at time $t$ using model $m$, trained during the training period using data from treatment group $A = a''$. Finally, let $\bar{Y}_{a'}^{a, T}$ be the post-treatment target estimand, where $\hat{Y}^a_{a'', T}(m)$ is the estimator using model $m$ learned during the training period and fit to the validation period data. This model selection procedure implicitly assumes that:

\begin{align*}
m^\star = \min_{m \in \mathcal{M}}\sum_{T - l}^{T-1}\|\hat{Y}^0_{0, t}(m) - \bar{Y}^0_{1, t}\| = \min_{m \in \mathcal{M}}\mathbb{E}\{\|\hat{Y}^0_{0, T}(m) - \bar{Y}^0_{1, T}\|\}
\end{align*}

In other words, this procedure selects the model (or weighting matrix $V$) using the empirical loss in the validation period as a proxy for the expected loss in the post-treatment time-period.\footnote{It is possible that multiple models in $\mathcal{M}$ either perfectly predict the pre-treatment outcomes, or predict them equally well. In this case we would require an additional criteria to choose the optimal model (see, e.g, \cite{becker2017cross}}. This has some intuitive appeal in the typical synthetic controls setting where the estimand is the ETT since we observe $Y^0_{sct}$ for $t < T$.

However, when synthetic controls are used to estimate the ETC, this idea is more troubling because we never observe $Y^1_{sct}$ (or a mean-unbiased proxy) prior to treatment for any unit. If we wish to use pre-treatment outcomes to optimally select variables or determine relative covariate importance, we would need strong assumptions, for example:

\begin{align*}\label{assumption:second}
m^\star = \min_{m \in \mathcal{M}}\sum_{T - l}^{T-1}\|\hat{Y}^0_{1, t}(m) - \hat{Y}^0_{0, t}\| = \min_{m \in \mathcal{M}}\mathbb{E}\{\|\hat{Y}^1_{1, T}(m) - \bar{Y}^1_{0, T}\|\}
\end{align*}

We call this assumption ``counterfactual risk invariance.'' In other words, we assume that the model that minimizes the validation-period risk also minimizes the post-treatment risk for the opposite counterfactual. This is quite a strong assumption, and may not be well-justified depending on the nature of the problem.

We briefly note that we conflate variable selection with the synthetic controls' variable weighting procedure for the purposes of this exposition. Regardless, from a finite-sample perspective the synthetic controls estimator will be biased should the weights fail to balance a relevant confounder regardless of whether the imbalance were due to a sub-optimal weighting matrix $V$. We also note that synthetic controls are frequently motivated by a linear factor model for the outcome, while we have assumed no unmeasured confounding and a linear model throughout our discussions. However, this general point still holds when considering unobserved factors that might be weak confounders of $Y^0$ but strong confound $Y^1$.

\subsection{Illustration}

We now illustrate this problem using our application, and consider the potential confounding role of Republican governance for our counterfactual estimate. Republican governance is a strong predictor of a state's decision to expand Medicaid \cite{courtemanche2017early}. Moreover, existing evidence prior to Medicaid expansion showed that Medicaid take-up rates were lower in more conservative states \cite{sommers2012understanding}. However, when generating their synthetic control weights to estimate the ETT, \cite{courtemanche2017early} and \cite{kaestner2017effects} do not control for these factors. \footnote{\cite{courtemanche2017early} does control for Republican governor in their regression model and they find that it is a statistically significant predictor of 2013 uninsurance rates. One reason they may not control for this in the synthetic control model is practical: it is challenging to balance this covariate using control data without extrapolating from the data.} However, it is clear that if take-up rates depend on governance, we may expect this to be a strong confounder of $Y^1$  even if arguably it is not a confounder of $Y^0$.

We illustrate this by conducting a variable importance analysis to examine the confounding role of Republican governance on our estimate of $\bar{Y}_0^1$. Specifically, we remove the balance constraints from the Republican governance indicators and examine how our estimates of $\hat{Y}_0^1$ change. Letting $\hat{Y}^1_{0, s}$ be the estimate when removing the Republican governance indicators (or more generally, the covariate matrix $S$ where $X = (Z, S)$). We subtract our original point estimate $\hat{Y}^1_0$ from $\hat{Y}^1_{0, s}$ to generate the difference $\hat{\Delta}^1$. This difference tells us about the direction of the bias our estimate of $\hat{Y}^1_0$ would incur when we do attempt to constrain the imbalance in covariate $S$. Our hypothesis implies that we should expect $\hat{\Delta}_s^1 < 0$: that is, keeping all other covariates (roughly) fixed, we expect the predicted uninsurance rate will decrease when as the level of Republican governance decreases. In addition to the Republican governance indicators, we also examine four other covariate groups: pre-treatment uninsurance rates and pre-treatment unemployment rates, and three sets of different demographic indicators. We then calculate these differences when removing each state and provide the minimum and maximum differences in parentheses. \footnote{This quantity does not represent a fixed target of inference and so we therefore do not estimate confidence intervals for them.}. We caution that our results do not imply that Republican governance is not an important confounder of $Y^0_{1, T}$ since we do not analyze this directly.

For the H-SBW estimator we calculate $\hat{\Delta}^1$ equal to -0.69 (min = -0.83, max = -0.42) and equal to -0.79 (min = -0.90, max = -0.66) on our unadjusted dataset. In other words, our primary estimated treatment effect moved 0.78 percentage points further away from zero when we excluded the Republican governance indicators. This reflects a 33 percent decrease in our point estimate, a not-unsubstantial difference. Moreover, all of these estimates were less than zero, regardless of whether we conditioned on the covariate adjustment or not, regardless of whether we remove the early expansion states or not, and when removing each state. Additional distributional results across all leave-one-state-out estimates for the primary dataset are available in Table~\ref{tab:rdiffc1} below. Additional results are available on request.

We also consider four other covariate sets. We find that our estimates are most sensitive to controlling for pre-treatment outcomes and unemployment rates. This is not unexpected: all else equal, the expansion region had much lower pre-treatment uninsurance rates. If we do not control for these covariates, the comparable region will likely have lower pre-treament uninsurance rates, causing the estimated counterfactual to be closer to zero. The effect estimates were less sensitive to the removal of other covariate groups, and all point estimates are available in Table~\ref{tab:ptests}. Overall these results highlight the importance of Republican governance in our counterfactual outcome model of $Y^1$.

\subsection{Effect heterogeneity}

If the models specified by \cite{kaestner2017effects} and \cite{courtemanche2017early} are correct (that is, they correctly omit Republican governance from their balancing weights for estimating $\bar{Y}^0_{1, T}$), our results imply treatment effect heterogeneity with respect to Republican governance. Moreover, because the expansion-state region is much more Democratic than the non-expansion region, this heterogeneity could potentially drive differences between the ETC and the ETT. Because this is a policy question of some interest, we directly investigate this by estimating the outcome model on the full data with treatment assignment interacted with each covariate \footnote{For this analysis we calculate separate covariate adjustments on the untreated data.} We then examine how the estimated treatment effect would change if we decreased the interaction between treatment assignment and each Republican governance indicator -- Republican governor, Republican lower legislature control, and Republican total control -- by 50 percentage points (the original variables are either 0 or 100 and are measured at the state level). This linear combination of coefficients estimates how the treatment effect would change for any given collection of states against a set that is identical except for being 50 percentage points lower, on average, across the Republican governance indicators. We find that the linear combination is positive (0.21 percentage points) and statistically significant at the 5 percent level on the unadjusted dataset. In contrast to our previous results, this would indicate that the estimated treatment effect may be larger among Republican governed areas. However, this finding is not robust to any other specification that we run. We interpret these results as providing no evidence of treatment effect heterogeneity with respect to Republican governance. The full results are available in Table~\ref{tab:hte}.

\subsection{Variable importance result tables}

Table~\ref{tab:ptests} presents all point estimates from estimators that we calculated. The ``Var subset`` column indicates which variables were excluded from the estimation: 0 excludes no variables; 1 removes Republican governance indicators; 2 pre-treatment uninsurance and unemployment rates; 3 urban, age, education, citizenship, marital status, student, disability, or female; 4 race, ethnicity, income, foreign born; 5 children, population growth, and household to person ratio. We see that the largest changes generally occur when excluding the pre-treatment uninsurance and unemployment rates. This is not surprising: controlling for the other covariates, the pre-treatment uninsurance rate was substantially lower in the treated region compared to the control region. Given that pre-treatment uninsurance rates are highly correlated with post-treatment rates, we find that this comparison leads to a larger absolute magnitude point estimate, highlighting the need to control for these covariates. The results are qualitatively similar when excluding the early expansion states and are available on request.

%Wed Jan 13 15:24:43 2021
\begin{table}[h!]
\centering
\caption{Point estimates for all specifications}
\label{tab:ptests}
\begin{tabular}{llrrrr}
  \hline
Variable subset & Adjustment & H-SBW & BC-HSBW & SBW & BC-SBW \\
  \hline
0 & Homogeneous & -2.33 & -2.05 & -2.35 & -2.07 \\
  0 & Heterogeneous & -2.24 & -1.98 & -2.28 & -2.00 \\
  0 & None & -2.34 & -2.22 & -2.39 & -2.19 \\
  1 & Homogeneous & -3.02 & -2.98 & -3.03 & -2.76 \\
  1 & Heterogeneous & -3.02 & -2.98 & -3.01 & -2.76 \\
  1 & None & -3.13 & -3.17 & -3.14 & -2.93 \\
  2 & Homogeneous & -5.40 & -4.73 & -5.12 & -4.48 \\
  2 & Heterogeneous & -5.40 & -4.72 & -5.12 & -4.48 \\
  2 & None & -5.40 & -4.81 & -5.12 & -4.55 \\
  3 & Homogeneous & -2.21 & -1.94 & -2.24 & -1.91 \\
  3 & Heterogeneous & -2.14 & -1.86 & -2.18 & -1.85 \\
  3 & None & -2.33 & -2.03 & -2.38 & -2.02 \\
  4 & Homogeneous & -2.34 & -2.18 & -2.29 & -2.15 \\
  4 & Heterogeneous & -2.30 & -2.17 & -2.25 & -2.10 \\
  4 & None & -2.39 & -2.37 & -2.45 & -2.35 \\
  5 & Homogeneous & -2.33 & -2.12 & -2.37 & -2.15 \\
  5 & Heterogeneous & -2.24 & -2.05 & -2.30 & -2.09 \\
  5 & None & -2.34 & -2.25 & -2.38 & -2.26 \\
   \hline
\end{tabular}
\end{table}

Table~\ref{tab:rdiffc1} displays the quantiles of the distribution $\hat{\Delta}_v^1$ estimates when leaving out each state for the primary dataset and removing the early expansion states. The ``resample'' column indicates whether the entire adjustment procedure was recalculated (``Procedure'') or whether we left out each state conditional on the adjustment (``States''). These differences tend to be slightly larger in absolute magnitude when removing early expansion states and are available on request.

\begin{table}[h!]
\centering
\caption{$\hat{\Delta}^1_v$ leave-one-state-out estimates, primary dataset}
\label{tab:rdiffc1}
\begin{tabular}{lllrrrrrr}
  \hline
Inference & Adjustment & Weight type & Original & 0\% & 25\% & 50\% & 75\% & 100\% \\
  \hline
States & Heterogeneous & H-SBW & -0.78 & -0.88 & -0.80 & -0.78 & -0.73 & -0.59 \\
  Procedure & Heterogeneous & H-SBW & -0.78 & -0.89 & -0.82 & -0.74 & -0.69 & -0.46 \\
  States & Heterogeneous & BC-HSBW & -1.00 & -1.13 & -1.02 & -0.99 & -0.94 & -0.71 \\
  Procedure & Heterogeneous & BC-HSBW & -1.00 & -1.11 & -1.03 & -0.98 & -0.93 & -0.66 \\
  States & Heterogeneous & SBW & -0.74 & -0.93 & -0.77 & -0.73 & -0.70 & -0.52 \\
  Procedure & Heterogeneous & SBW & -0.74 & -0.96 & -0.78 & -0.73 & -0.69 & -0.53 \\
  States & Heterogeneous & BC-SBW & -0.77 & -0.97 & -0.79 & -0.75 & -0.72 & -0.56 \\
  Procedure & Heterogeneous & BC-SBW & -0.77 & -0.96 & -0.80 & -0.76 & -0.69 & -0.50 \\
  States & Homogeneous & H-SBW & -0.69 & -0.83 & -0.74 & -0.70 & -0.64 & -0.42 \\
  Procedure & Homogeneous & H-SBW & -0.69 & -0.86 & -0.76 & -0.71 & -0.65 & -0.36 \\
  States & Homogeneous & BC-HSBW & -0.93 & -1.07 & -0.95 & -0.94 & -0.88 & -0.64 \\
  Procedure & Homogeneous & BC-HSBW & -0.93 & -1.05 & -0.97 & -0.93 & -0.87 & -0.59 \\
  States & Homogeneous & SBW & -0.67 & -0.86 & -0.72 & -0.67 & -0.65 & -0.40 \\
  Procedure & Homogeneous & SBW & -0.67 & -0.90 & -0.73 & -0.69 & -0.65 & -0.47 \\
  States & Homogeneous & BC-SBW & -0.70 & -0.89 & -0.75 & -0.70 & -0.66 & -0.50 \\
  Procedure & Homogeneous & BC-SBW & -0.70 & -0.91 & -0.76 & -0.73 & -0.66 & -0.44 \\
  States & None & H-SBW & -0.79 & -0.90 & -0.81 & -0.80 & -0.76 & -0.66 \\
  Procedure & None & H-SBW & -0.79 & -0.90 & -0.81 & -0.80 & -0.76 & -0.66 \\
  States & None & BC-HSBW & -0.95 & -1.10 & -0.98 & -0.94 & -0.90 & -0.84 \\
  Procedure & None & BC-HSBW & -0.95 & -1.10 & -0.98 & -0.94 & -0.90 & -0.84 \\
  States & None & SBW & -0.75 & -0.89 & -0.77 & -0.75 & -0.73 & -0.58 \\
  Procedure & None & SBW & -0.75 & -0.89 & -0.77 & -0.75 & -0.73 & -0.58 \\
  States & None & BC-SBW & -0.74 & -0.88 & -0.75 & -0.72 & -0.70 & -0.57 \\
  Procedure & None & BC-SBW & -0.74 & -0.88 & -0.75 & -0.72 & -0.70 & -0.57 \\
   \hline
\end{tabular}
\end{table}

Table~\ref{tab:hte} displays the estimated linear combination of model coefficients on the Republican governance indicators with 95 percent confidence intervals (standard errors calculated using leave-one-state-out jackknife, repeating the entire covariate adjustment procedure each time).

\begin{table}[h!]
\caption{Effect heterogeneity with respect to Republican governance}
\label{tab:hte}
\centering
\begin{tabular}{rlll}
  \hline
Estimate & Adjustment & CI (95\%) & Dataset \\
  \hline
-1.67 & Heterogeneous & (-5.27, 1.94) & Primary \\
  0.78 & Homogeneous & (-5.62, 7.18) & Primary \\
  0.21 & None & (0.02, 0.40) & Primary \\
  -1.80 & Heterogeneous & (-5.46, 1.85) & Early expansion excluded \\
  0.59 & Homogeneous & (-6.13, 7.31) & Early expansion excluded \\
  0.15 & None & (-0.12, 0.42) & Early expansion excluded \\
   \hline
\end{tabular}
\end{table}

\clearpage


\chapter{Introduction}

\textit{Rendering} is generally the process of generating a two-dimensional image, called \textit{render}, from the mathematical description of a three-dimensional scene.
%To oversimplify it, a scene is made of light sources, geometries and a camera, all defined in mathematical, hence rather abstract, terms.
Several rendering algorithms have been developed over the years and many of them have been designed to produce images as \textit{photorealistic} and lifelike as possible. The most recent ones of these kind are all based upon simulating how light interacts with the scene, closely imitating its natural behavior. A particularly successful rendering algorithm following this philosophy is \textit{Path Tracing} \cite{kajiya1986rendering} since its derivations and itself are widespread across animation, visual effects and video games. In simple words, path tracing is based upon the idea of shooting rays out of an artificial camera towards the scene and make each bounce around the scene until it reaches a light source; then, the luminous energy carried by the ray and its bounces, called \textit{path}, can be computed --- or \textit{traced} back.  Without diving further into its details --- which are left for section \ref{background} ---, it is clear how complex path tracing can get. This makes it difficult to grasp by people approaching it and difficult to debug.

We are presenting a tool capable of showing the user an overview of the inner workings of a path tracer by providing interactive visualizations of the very core of a path tracer: paths and how they interact with the scene.
We strived to create something that does not tell but --- literally --- shows the swarm of paths shot by a path tracer to help understand what is actually going on during the rendering process.

The idea of providing interactive renderings of the data generated by a path tracer --- or a ray tracer --- is not original. Let us mention a few that inspired our work:
\begin{itemize}
	\item The \textit{Ray tracing visualization toolkit} (\textit{rtVTK}) by C. Gribble et Al. \cite{gribble2012ray} is a nice starting point but it focuses on ray tracers \cite{whitted1979improved, cook1984distributed} more than on path tracers.
	Furthermore, it is designed to render only a single ray tree at once, making it difficult to picture a global overview on the data that concurred in the generation of the final render; after all, a good mantra of presenting visual data is, quoting Ben Shneiderman, “Overview first, zoom and filter, then details-on-demand” \cite{shneiderman2003eyes}. rtVTK practically presents only the details.
	\item The work presented in \textit{A Framework for Visual Dynamic Analysis of Ray Tracing Algorithms} by H. Lesev and A. Penev \cite{lesev2014framework} has extensive filtering and data gathering features which inspired us, but it works only on ray tracers, as much as rtVTK.
	\item Another great inspiration for us was the framework presented in \textit{Applying Visual Analytics to Physically Based Rendering} by G. Simons et Al. \cite{simons2019applying}, especially the visualization parts. We however drift off it because, while they reduce the data they gather from a path tracer by selecting only about 1/6000th of the total paths, our goal is to keep the whole dataset and let the user choose what to visualize from all the samples.
	\item Another tightly related work is \textit{EMCA}, which stands for \textit{Explorer of Monte-Carlo based Algorithms}, by C. Kreisl \cite{EMCA@2019}. It focuses on path tracing and visualizations but its client-server architecture with per-pixel path analysis lacks of the global view we are trying to achieve.
\end{itemize}

\section{Background}
\label{background}

%To fully comprehend the topics and challenges presented in this work a background is needed.

We will now provide a quick overview on path tracing to establish a common ground of terminology between us and the reader. This is not meant to be in any way an exhaustive explanation, please refer to the cited literature to have a better insight.

\subsection{The rendering equation}
\label{secrendeq}
As we introduced before, \textit{photorealistic rendering} is the process that given a mathematical description of a three-dimensional scene outputs an as life-like as possible image, know as \textit{render}. The process simulates how light travels from light sources to an abstract camera sensor while interacting with the scene. An important equation that describes how light behaves is the \textit{Rendering Equation} introduced by J. Kajiya in 1986 \cite{kajiya1986rendering} here presented with different notations:
\begin{equation}
	L_o(\vec{x},\vec{\omega}_o) =
		L_e(\vec{x},\vec{\omega}_o) +
		L_r(\vec{x},\vec{\omega}_o)
	\label{rendeqeasy}
\end{equation}
It says that, considering a surface, the amount of light that leaves the surface point $\vec{x}$ in direction $\vec{\omega}_o$, called \textit{outgoing radiance} ($L_o$), is determined by the emitted radiance ($L_e$), which is the one emanated by the surface itself, plus the reflected radiance ($L_r$) which is given by:
\begin{equation}
	L_r(\vec{x},\vec{\omega}_o) =
		\int_{\mathcal{H}^2}
			f(\vec{\omega}_o, \vec{x}, \vec{\omega}_i)
			L_i(\vec{x},\vec{\omega}_i)
			d\vec{\omega}_i
	\label{reflectioneq}
\end{equation}
This means it consists in the sum of the radiance incoming ($L_i$) from all possible directions ($\vec{\omega}_i \in \mathcal{H}^2$) hitting point $\vec{x}$, weighted by the reflective and refractive properties of the material ($f(\cdots)$).

Now, image sensors, as well as all animal visual organs, are made of several small photo sensitive units that measure the incoming radiance hitting their surfaces over a short period of time. By putting together the values read by the units an image is produced. To compute the incoming radiance on each of these photo sensitive units called \textit{pixels}, the incoming radiance must be integrated over the pixel surface and scaled by its area to make the result independent of the sensor's size:
\begin{equation}
	L_{pixel} = \frac{1}{A_{pixel}}
		\int_{\vec{q}\in pixel} L_i(\vec{q}, \vec{\omega}_r)dA_q
	\label{pixelradiance}
\end{equation}
Where $\vec{\omega}_r$ depends on the camera mathematical model, that can range from the simplest pinhole camera to a complex simulation of real camera lenses groups.

At this point is clear that rendering is all about computing the radiance hitting a sensor but the rendering equation presented above (eq. \ref{rendeqeasy}) describes only outgoing radiance. Fortunately, thanks to the fact that radiance is constant along a ray\footnote{Radiance is constant along a ray only in perfect vacuum. All non-volumetric path tracers --- which we exclusively focus on --- assume vacuum between surfaces.}, it is possible to write:
\begin{equation}
	L_i(\vec{x},\vec{\omega}_i) = L_o(\vec{y},\vec{\omega}_o)
\end{equation}
Where $\vec{y}$ is the first point on a surface hit by the ray shot from $\vec{x}$ in direction $\vec{\omega}_i$ and $\vec{\omega}_o$ is the direction pointing from $\vec{y}$ to $\vec{x}$. Introducing the Raytracing $RT$ operator:
\begin{equation}
	\vec{y} = RT(\vec{x},\vec{\omega}_i)
\end{equation}
And rewriting $\vec{\omega}_o$ in function of $\vec{\omega}_i$:
\begin{equation}
	\vec{\omega}_o = -\vec{\omega}_i
\end{equation}
We have that:
\begin{equation}
	L_i(\vec{x},\vec{\omega}_i) = L_o(RT(\vec{x},\vec{\omega}_i),-\vec{\omega}_i)
	\label{lilo}
\end{equation}
The first thing that can be taken from this is that to compute the incoming radiance on a sensor it is sufficient to shoot a ray and compute the radiance on the scene point hit by the ray outgoing in the inverse ray direction. Secondly, plugging everything that has been said until now produces a more complete rendering equation:
\begin{equation}
	L_o(\vec{x},\vec{\omega}_o) =
		L_e(\vec{x},\vec{\omega}_o) +
		\int_{\mathcal{H}^2}
			f(\vec{\omega}_o, \vec{x}, \vec{\omega}_i)
			L_o(RT(\vec{x},\vec{\omega}_i),-\vec{\omega}_i)
			d\vec{\omega}_i
	\label{renderingeq}
\end{equation}

%Where:
%\begin{description}
%	\item[$\vec{x}$] A point in space.
%	\item[$\vec{\omega}_o$] An outgoing direction from $\vec{x}$.
%	\item[$\vec{\omega}_i$] An incoming direction towards $\vec{x}$
%	\item[$L(\vec{x},\vec{\omega}_o)$] Radiance outgoing in direction $\vec{\omega}_o$ from $\vec{x}$
%	\item[$L_e(\vec{x},\vec{\omega}_o)$] Radiance emitted in direction $\vec{\omega}_o$ by $\vec{x}$.
%	\item[$\mathcal{H}^2$] All the possible directions.
%	\item[$L(\vec{x},\vec{\omega}_i)$] Incoming radiance from direction $\vec{\omega}_i$ on $\vec{x}$.
%	\item[$f(\vec{\omega}_o, \vec{x}, \vec{\omega}_i)$] The Bidirectional Scattering Distribution Function (BSDF) computed on $\vec{x}$ on directions $\vec{\omega}_o$ and $\vec{\omega}_i$.
%	\item[$\hat{n}_{\vec{x}}$] The surface normal on $\vec{x}$.
%\end{description}

\subsection{Monte Carlo integration}

Given all the equations above, all it is needed to render a convincing image is solving them. The first problem that surfaces is that they contain integrals. Integrals are infinitesimal sums and as such they do not conform too well with the discrete nature of electronic calculators. In other words, integrals cannot be numerically solved on computers. Their solution, though, can be approximated. Without lingering on the reasons, a particularly suitable approximation method for our case is the Monte Carlo integration \cite{kalos2009monte}.

It randomly picks $N$ samples ($x_i, i \in [0,N)$) from the integration interval ($\Omega$), evaluates the integrand ($f(x)$) with each sample and does an average of the evaluated values weighting them by the probability density function ($pdf(x)$) of picking the corresponding sample from the interval. This gives:
\begin{equation}
	\int_{\Omega}f(x)dx \approx \frac{1}{N} \sum_{i=0}^{N} \frac{f(x_i)}{pdf(x_i)}
	\label{montecarlo}
\end{equation}

The value computed by Monte Carlo gets closer to the correct result increasing the number of random samples $N$. It has been demonstrated that if the samples are picked from the interval using a uniform distribution, the relative error is directly proportional to $\frac{1}{\sqrt{N}}$. From this it can be deducted that Monte Carlo will never output the correct solution but it will asymptotically tend to it as samples increase; that is why literature usually talk about \textit{convergence} and sometimes about \textit{convergence speed}, which is an informal value expressing how many samples are needed to approximate an acceptable result, where what is considered acceptable varies case by case.

The $\frac{1}{\sqrt{N}}$ factor can be improved by a technique called \textit{importance sampling} that is based upon the idea of drawing samples from a distribution as close as possible to the integrand in order to reduce the variance. The ideal case would be using the integrand itself as distribution but often, due to its complexity, is impossible to draw samples directly from it. Alternative distributions that roughly approximate the integrand are used instead.
This can even be brought a step forward by stochastically combining two approximate distributions together using the so-called \textit{multiple importance sampling} (MIS) \cite{veach1995optimally}.

\subsection{Path tracing}

Now that we have a tool to numerically solve integrals, we can apply it to the integrals presented in section \ref{secrendeq}. The total incoming radiance on a pixel (eq. \ref{pixelradiance}) can then be calculated by randomly picking sample points on its surface and then applying Monte Carlo (eq. \ref{montecarlo}):
\begin{equation}
	L_{pixel} \approx \frac{1}{N A_{pixel}}
		\sum^{N}_{i = 0} \frac{L_i(\vec{q}_i, \vec{\omega}_{ri})}{p(\vec{q}_i)}
\end{equation}
The number of samples picked here $N$ is usually constant for each pixel. Due to its direct relation between final render quality --- more samples means a solution closer to the real one --- and render time --- each additional sample adds computational burden --- it is a rather important parameter in all path tracers and it is called \textit{samples per pixel} or \textit{spp} for short.

Now, it is a matter of computing $L_i$ for each pixel sample. Recalling the logical results given by the conservation of radiance along rays presented in equation \ref{lilo} the problem shifts on how to compute the outgoing radiance of the point of the scene hit by the ray shot from the sample position ($\vec{q}_i$) in its associated direction ($\vec{\omega}_{ri}$). This value, as presented by the complete version of the rendering equation (eq. \ref{renderingeq}), depends on another integral having all possible directions as its integration interval. Furthermore, inside the integral there is again an outgoing radiance, but this time computed with other parameters ($L_o(RT(\vec{x},\vec{\omega}_i),-\vec{\omega}_i)$). By substitution, it begins to become clear that evaluating one outgoing radiance theoretically leads to solving a recursively infinite amount of integrals. This, through the power of Monte Carlo integration, becomes manageable and a path tracer computes outgoing radiances by sampling all the possible directions just once. The rendering equation can then be written as:
\begin{equation}
	L_o(\vec{x},\vec{\omega}_o) \approx
	L_e(\vec{x},\vec{\omega}_o) +
	\frac{1}{pdf(\vec{\omega}_i)}
	f(\vec{\omega}_o, \vec{x}, \vec{\omega}_i)
	L_o(RT(\vec{x},\vec{\omega}_i),-\vec{\omega}_i)
\end{equation}
With this approximation the calculations consist in shooting a ray, find an intersection with the scene, compute the properties of the material on the intersection, pick a random new direction, shoot a ray in that direction from the intersection point and then keep doing the same recursively, forming a path. In practice, to not make a path bounce indefinitely, a maximum number of bounces, called \textit{maximum path length}, is arbitrarily decided before rendering, as much as the number of samples per pixel is.

Path tracers that behave as described until now are informally called na\"ive path tracers. Other more complex tracers employ techniques to speed up the Monte Carlo convergence. As already mentioned, a common technique is importance sampling, as it can be applied to path tracing by sampling new bounces' directions from probability distributions that naturally carry more radiance. This can happen in two ways: either paths are forced to follow the properties of the material they are bouncing on, or they are directed towards a light source. A usually even more efficient approach is to combine these two sampling strategies through multiple importance sampling.

% Using this approximation, estimating radiance for each pixel sample can be written in pseudo code as shown in listing \ref{ptalgo}.
% It is clear how

% \begin{Listing}
% 	\begin{lstlisting}
% function estimate_radiance(ray):
% 	intersection := intersect scene with ray
% 	if no intersection:
% 		return 0

% 	x := intersection point
% 	omega_o := ray direction
% 	omega_i := random direction picked from distribution d
% 	pdf_omega_i := pdf of the distribution d evaluated for omega_i
% 	new_ray := ray originating from x and direction omega_i
% 	incoming_radiance := estimate_radiance(new_ray)
% 	material := material properties computed with omega_o, x, and omega_i
% 	return (1 / pdf_omega_i) * material * incoming_radiance

% primary_ray := ray shot from the camera
% estimate_radiance(primary_ray)
% 	\end{lstlisting}
% 	\caption{Pixel sample radiance estimation.}
% 	\label{ptalgo}
% \end{Listing}

%\subsection{OpenGL}

%It means that the algorithms that generated these datasets have different ways of choosing the direction of each path bounce in order to maximize the radiance carried by each path, speeding up the convergence of the Monte Carlo integration and hence trying to lower the amount of noise given the same amount of pixel samples. This technique, used in every application of Monte Carlo methods, is called \textit{importance sampling} \cite{kalos2009monte}. Multiple importance sampling \cite{veach1995optimally}.

\section{Motivation}
\label{motivation}
%As introduced above, path tracing is an algorithm that is not easy to picture. For someone who got to understand it, the rendering equation is more than enough to tell how a path tracer work. At least to get started, everything a person needs to know is in that equation: once combined with Monte Carlo integration, everyone is theoretically ready to implement a path tracer.

As introduced above, path tracing is not easy to picture. As many research groups before us thought, having a tool showing what a tracer is doing by visualizing its interactions with the 3D scene would help the professional developers lost in the debugging cycle as well as the students trying to get a hang of path tracing's inner workings. The challenge this vision has to face is the sheer amount of these interactions; talking about path tracing, to have a decent render\footnote{We considered as a decent render a $512 \times 512$ pixels and 256 spp image which is a good halfway point between production quality and being way too far from convergence.}, more than two hundreds paths have to be shot for each pixel: to produce an image, more than fifty million paths have to be shot. Postponing data size considerations for later (sec. \ref{datatogather}), leaves us with potential datasets that without proper data reduction or filtering are of no use to anyone. Related works usually perform substantial reductions of the datasets \cite{simons2019applying,EMCA@2019}, but we believe that having all the paths available for visualization would improve the experience.

With these premises, a more precise idea for a visualization tool came to us.
The user should have the power to visually and interactively explore all the paths previously generated by a path tracer during rendering. The paths have to be immersed in an interactive rendering of the scene: their most important property is their geometry or, in other words, where they bounce inside the scene. In the beginning, out of curiosity and naivety, we tried visualizing all the paths of some datasets and even if those were rather small, we achieved compelling yet cluttered results (fig. \ref{visual_clutter}); this made visualizing the millions of paths of a proper dataset out of question from the earliest stages. To navigate this ocean of paths, the user should be provided with a global summary overview of the dataset in its entirety, and they should also able to filter them using spatial queries. For the global overview we envisioned a heatmap applied as a texture to the 3D view of the scene that shows areas where there is more path activity, while for the spatial queries we thought the user might want to select path bouncing on certain noteworthy surfaces, such as lights or mirrors.

\begin{figure}
	\centering
	\centering
	\begin{subfigure}[t]{0.49\linewidth}
		\includegraphics[width=\textwidth]{chapters/chapter_intro/clutter_early1}
		\caption{$64 \times 64$, 1 spp}
	\end{subfigure}
	\begin{subfigure}[t]{0.49\linewidth}
		\includegraphics[width=\textwidth]{chapters/chapter_intro/clutter_early2}
		\caption{$64 \times 64$, 64 spp}
	\end{subfigure}

	\caption{Visual clutter in an early version of the tool where the scene rendering has not been implemented yet. Both datasets have been generated on the \textit{Cornell Box} scene.}
	\label{visual_clutter}
\end{figure}

Later in the development, it became clear that the possibility of comparing different datasets would come in handy to users. It could be useful in many cases such as comparing two progressive versions of the same in-development path tracer to understand what changed or, more in an educational context, such as showing a ground truth next to a purposely wrong dataset to understand why one is wrong on a deeper level.

Of course, before even talking about visualizing data, data has to be gathered from a path tracer. We envisioned a software, parallel to the visualization client, able to plug to any path tracer and gather with little effort the required data during the rendering process. Then the idea of a gathering library with a very simple interface came about; a library that can be plugged into any path tracer the user is currently working on by just calling the few required functions.

To summarize, our vision consisted in a tool divided into a visualization client and a data gathering library which can be used both for debugging and teaching purposes. The library should have an easy to interact with interface, while the client would let the user explore different datasets simultaneously and in their entirety using the filtering and visualization options provided to them.

%select a portion of a surface of the 3D scene the tracer has been run upon and see the paths that bounce there with a bunch of useful data. To be able to do that the whole set of paths shoot by a tracer are needed: by the very stochastic nature of a path tracer, it is impossible to determine which paths will end up bouncing where without resolving them all first. That is why it has been decided it was essential to store data about each path during the rendering process. To make the tool usable in most possible use cases, it had to be able to plug into an existing path tracer and this lead to the conception of the tool as a two software pieces suite: a \textit{data gatherer library} called \texttt{gatherer} and a \textit{visualization client} called \texttt{gathererclient}.

%Now this “useful data” was not extremely well-defined during those early stages, so most of the efforts have been directed to the very essential: rendering the requested paths keeping interactivity.


\section{The \replacecopy algorithm}
\Label{sec:replacecopy}

The \replacecopy algorithm of the \cxx Standard Library \cite[\S 28.6.5]{cxx-17-draft} substitutes
specific elements from general sequences.
%
Here, the general implementation
has been altered to process \valuetype ranges.
The new signature reads:

\begin{lstlisting}[style=acsl-block]

  size_type replace_copy(const value_type* a, size_type n, value_type* b,
                         value_type v, value_type w);
\end{lstlisting}

The \replacecopy algorithm copies the elements from the range \inl{a[0..n]}
to range {\inl{b[0..n]}}, substituting every occurrence of \inl{v} by \inl{w}.
The return value is the length of the range.
As the length of the range is already a parameter of
the function this return value does not contain new
information.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.50\textwidth]{Figures/replace.pdf}
\caption{\Label{fig:replace} Effects of \replace}
\end{figure}

Figure~\ref{fig:replace} shows the behavior of \replacecopy at hand of an example
where all occurrences of the value~3 in~\inl{a[0..n-1]} are replaced with the
value~2 in~\inl{b[0..n-1]}.

\subsection{The predicate \Replace}

We start with defining in the following listing the predicate \logicref{Replace}
that describes the intended relationship between the input array \inl{a[0..n-1]}
and the output array \inl{b[0..n-1]}.
Note the introduction of \emph{local bindings} \inl{\\let ai = ...}
and \inl{\\let bi = ...} in the definition of \Replace (see \cite[\S 2.2]{ACSLSpec}).

\input{Listings/Replace.acsl.tex}

This listing also contains a second, overloaded version of \Replace
which we will use for the specification of the related in-place
algorithm \specref{replace}.

%\clearpage

\subsection{Formal specification of \replacecopy}

Using predicate \Replace the specification of \specref{replacecopy}
is as simple as shown in the following listing.
Note that we also require that the input range \inl{a[0..n-1]} and
output range \inl{b[0..n-1]} do not overlap.

\input{Listings/replace_copy.h.tex}

\subsection{Implementation of \replacecopy}

The implementation (including loop annotations) of \implref{replacecopy}
is shown in the following listing.
Note how the structure of the loop annotations resembles
the specification of \specref{replacecopy}.

\input{Listings/replace_copy.c.tex}

\clearpage


% use option [preprint] to remove info line at bottom
% journal options: aop,aap,aos,aoas,ssy
% natbib option: authoryear
\documentclass[12pt,aoas]{imsart} %% to review
%%\documentclass[aoas]{imsart}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Package
\usepackage{amssymb}
\usepackage{verbatim}
%\usepackage[francais,english]{babel}
\usepackage{graphicx}
\usepackage{lineno}
\RequirePackage{natbib}
\usepackage{hyperref}
\usepackage{algorithm2e}
\RequirePackage{amsthm,amsmath}
\usepackage{color}
%\usepackage[latin1]{inputenc}
%\usepackage[table]{xcolor}

% provide arXiv number if available:
%\arxiv{arXiv:0000.0000}

% put your definitions there:
\startlocaldefs
\numberwithin{equation}{section}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\theoremstyle{definition}
\newtheorem{algo}{Algorithm}[section]
\endlocaldefs
\DeclareMathOperator*{\argmin}{arg\,min}

\begin{document}

%% To review
\baselineskip 0.8cm
\linenumbers
%%

\begin{frontmatter}

% "Title of the paper"
\title{Fast inference of individual admixture coefficients using geographic data}
\runtitle{Fast inference of individual admixture coefficients}

\begin{aug}
\author{\fnms{Kevin} \snm{Caye}\thanksref{t1}\ead[label=e1]{kevin.caye@imag.fr}},
\author{\fnms{Flora} \snm{Jay}\thanksref{t2}\ead[label=e2]{flora.jay@lri.fr}},
\author{\fnms{Olivier} \snm{Michel}\thanksref{t1}\ead[label=e3]{olivier.michel@gipsa-lab.grenoble-inp.fr}},
\and
\author{\fnms{Olivier} \snm{Fran\c cois}\thanksref{t1}
\ead[label=e4]{olivier.francois@imag.fr}}

%% \thankstext{m1}{This work has been partially supported by the LabEx PERSYVAL-Lab (ANR-11-LABX-0025-01) funded by the French program Investissement d\rq{}Avenir}
%% \thankstext{m2}{Olivier Fran\c cois acknowledges support from Grenoble INP and from the Agence Nationale de la Recherche, project AFRICROP ANR-13-BSV7-0017}
\runauthor{K. Caye et al.}

\affiliation{Universit\'e Grenoble-Alpes\thanksmark{t1} and Universit\'e Paris-Sud\thanksmark{t2}}

\address{Kevin Caye and Olivier Fran\c cois\\
Universit\'e Grenoble-Alpes\\
Centre National de la Recherche Scientifique\\
TIMC-IMAG UMR 5525\\
Grenoble, 38042, France\\
\printead{e1}\\
\phantom{E-mail:\ }\printead*{e4}}

\address{Flora Jay\\
Université Paris-Sud\\
Université Paris-Saclay\\
Laboratoire de Recherche en Informatique UMR 7206\\
CNRS UMR 8623\\
Orsay, 91400, France\\
\printead{e2}}

\address{Olivier Michel\\
Universit\'e Grenoble-Alpes\\
Centre National de la Recherche Scientifique\\
GIPSA-lab UMR 5216\\
Grenoble, 38042, France\\
\printead{e3}}

\end{aug}

\begin{abstract}
Accurately evaluating the distribution of genetic ancestry across geographic space is one of the main questions addressed by evolutionary biologists. This question has been commonly addressed through the application of Bayesian estimation programs allowing their users to estimate individual admixture proportions and allele frequencies among putative ancestral populations. Following the explosion of high-throughput sequencing technologies, several algorithms have been proposed to cope with computational burden generated by the massive data in those studies. In this context, incorporating geographic proximity in ancestry estimation algorithms is an open statistical and computational challenge. In this study, we introduce new algorithms that use geographic information to estimate ancestry proportions and ancestral genotype frequencies from population genetic data. Our algorithms combine matrix factorization methods and spatial statistics to provide estimates of ancestry matrices based on least-squares approximation.  We demonstrate the benefit of using spatial algorithms through extensive computer simulations, and we provide an example of application of our new algorithms to a set of spatially referenced samples for the plant species {\it Arabidopsis thaliana}.  Without loss of statistical accuracy, the new algorithms exhibit runtimes that are much shorter than those observed for previously developed spatial methods. Our algorithms are implemented in the {\tt R} package, {\tt tess3r}, which is available from \url{https://github.com/BioShock38/TESS3_encho_sen}.
\end{abstract}

\begin{keyword}
\kwd{Ancestry Estimation Algorithms}
\kwd{Genotypic Data}
\kwd{Geographic Data}
\kwd{Fast Algorithms}
\end{keyword}

\end{frontmatter}

\input{intro.tex}
\input{method.tex}
\input{results.tex}
\input{discussion.tex}

\appendix

\input{appendix.tex}

\newpage

\section*{Acknowledgements}
This work has been partially supported by the LabEx PERSYVAL-Lab (ANR-11-LABX-0025-01) funded by the French program Investissement d\rq{}Avenir. Olivier Fran\c cois acknowledges support from Grenoble INP and from the Agence Nationale de la Recherche, project AFRICROP ANR-13-BSV7-0017.

% AOS,AOAS: If there are supplements please fill:
%\begin{supplement}[id=suppA]
%  \sname{Supplement A}
%  \stitle{Title}
%  \slink[doi]{10.1214/00-AOASXXXXSUPP}
%  \sdatatype{.pdf}"
%  \sdescription{Some text}
%\end{supplement}

\input{references.tex}
\input{tables_figures.tex}

\end{document}


\documentclass[a4paper, 12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xspace}
\usepackage{cleveref}
\usepackage{booktabs}
\usepackage[parfill]{parskip}
\usepackage{booktabs}

\newcommand{\pt}{\ensuremath{p_{\mathrm{T}}}\xspace}

\begin{document}

\section{Introduction}
Herein lines some documentation on the HitFinder module in Delphes, and how I made it.

This HitFinder module repeats the algorithm found within the ParticlePropagator module, except N times, where N is the number of ``surfaces''.
The surfaces correspond to barrel layers, or end-cap layers (in this module, they simply correspond to two-dimensional surfaces, there are no modules or services).
One can apply a pT cut to the input particles, as, for example, low momentum particle would not reach certain regions of the detector.

Within Delphes, I added a Hit class that contains the 4-position of each particle hit, and the reference to the Delphes particle.

\section{From hits to tracks}
The distribution of vertices from a sample of 100 events (overlaid with a mean pileup of 200) is shown in \ref{fig:vertices}.
The width of the distribution is $\sigma = 53$\,mm
\begin{figure}
  \centering
  \includegraphics[width=0.5\linewidth]{images/vertexDistribution}
  \caption{Distribution of vertices in 100 ttbar events (with mean pileup 200) and 100 TeV.}
  \label{fig:vertices}
\end{figure}

\subsection{The basic tracking}
\subsubsection{From outside to in}
For the basic tracking algorithm, a coincidence of three tracks is required, starting from a hit in the outer layer, and falling within a cone of $3\sigma$ around the detector coordinate origin.

\begin{figure}
  \centering
  \includegraphics[width=0.5\linewidth]{images/geometry1}
  \caption{}
  \label{fig:circles1}
\end{figure}

Given a point $p= (a, b)$ on a circle of radius $2r$, the midpoint of the line from the origin to $p$ has the coordinates $(\alpha, \beta) = (a/2, b/2)$.
Therefore, the equation of the smaller circle shown in \cref{fig:circles1} is
\begin{equation}
  \Big( x - \frac{a}{2} \Big)^2 + \Big( y - \frac{b}{2} \Big)^2 = r^2.
\end{equation}
We are actually interested in the intersection of the small circle in \cref{fig:circles1} with another circle, centered on the origin as displayed in \cref{fig:circles2}.
Using the notation of the \cref{fig:circles2} we must combine
\begin{equation}
  x^2 + y^2 = r_2^2 \quad \mathrm{and} \quad (x - \alpha)^2 + (y - \beta)^2 = r^2,
  \label{eq:toSolve1}
\end{equation}
where for notational simplicity we have used $r = r_1 / 2$.
Eliminating $y$ \cref{eq:toSolve1} gives
\begin{align}
  \Big( x - \alpha \Big)^2 + \Big( \sqrt{r_2^2 - x^2} - \beta \Big)^2 = & ~ r^2 \\
  x^2 + \alpha^2 - 2x\alpha + r_2^2 - x^2 + \beta^2 -2\beta \sqrt{r_2^2 - x^2} = & ~ r^2 \\
  (\alpha ^2 + \beta^2) -2 x \alpha +r_2^2 -2\beta\sqrt{r_2^2 - x^2} = & ~ r^2.
\end{align}
Using $\alpha^2 + \beta^2 = r^2$,
\begin{align}
  r_2 ^2 & = 2x\alpha + 2\beta \sqrt{r_2^2 - x^2} \\
  (r_2^2 - 2\alpha x )^2 & = 4\beta^2 (r_2^2 - x^2) \\
  r_2^4  + 4\alpha^2 x^2 - 4r_2^2 \alpha x & = 4\beta^2 (r_2^2 - x^2) \\
  4x^2 (\alpha^2 + \beta^2) - 4r_2^2 \alpha x + r_2^4 -4\beta^2 r_2^2 & = 0.
\end{align}
Again using $\alpha^2 + \beta^2 = r^2$ leaves us with the quadratic
\begin{equation}
  4r^2 x^2 -4 r_2^2 \alpha x +r_2^2(r_2^2 -4 \beta^2) = 0
\end{equation}
and therefore,
\begin{equation}
  x  = \frac{r_2^2 \alpha \pm \sqrt{r_2^4\alpha^2 - r^2 r_2^2(r_2^2 - 4\beta^2)}}{2r^2}.
\end{equation}
In the case that $\beta = 0$, then $r=\alpha$ and the above simplifies to $x = \frac{r_2^2}{2r}$.
Likewise for $y$ we find
\begin{equation}
  y  = \frac{r_2^2 \beta \pm \sqrt{r_2^4\beta^2 - r^2 r_2^2(r_2^2 - 4\alpha^2)}}{2r^2},
\end{equation}
which in the case $\alpha = 0$ simplifies to $y = \frac{r_2^2}{2r}$ (with $r=\beta$).

\begin{figure}
  \centering
  \includegraphics[width=0.5\linewidth]{images/geometry2.eps}
  \caption{}
  \label{fig:circles2}
\end{figure}

\subsubsection{From inside to out}
This second algorithm is used to create tracks starting from a hit in the innermost layer,
and then finding matching hits in the outer layers.
In the $r$--$z$ plane, two lines are drawn from the hit location to the edges of the luminous region along the beamline, these lines are extended outwards, and any hit within the two lines are matched
to the inner hit.

\begin{figure}
  \centering
  \includegraphics[width=0.5\linewidth]{images/geometry3.eps}
  \caption{}
  \label{fig:circles3}
\end{figure}

In the $r$--$\phi$ plane it is considered that the hit can traverse to the outermost layer in a circle.
Given that the charge of the particle is not known, the trajectory can curve in one of two directions.
We wish to find limits in $\phi$ to which the particle hits can be matched.
With respect to \cref{fig:circles3} the algorithm proceeds as follows:
\begin{itemize}
  \item A hit is found in the innermost layer (which has radius $r_2$) at point $(a, b)$.
  \item In the extreme case, the particle would bend in a circle in one direction or another, and so would end up at either point $p_1 = (c, d)$ or point $p_2 = (e, f)$.
  \item A hit is matched if any hit in the outer layers lie within $\phi_1$ and $\phi_2$.
  \item The angle between any $p_1$ and $p_2$ will always been the same, and so this only needs to be calculated once per geometry.
\end{itemize}
Setting the problem up, we want to find the equations of the circles intersecting the origin and point $(a, b)$, i.e. solve
\begin{equation}
  (x - \alpha)^2 + (y - \beta)^2 = (r_1/2)^2, \quad a \in {x}, b \in {y}
  \label{eq:circInToOut1}
\end{equation}
This amounts to finding the centre of these circles, the coordinates $(\alpha, \beta)$.
Since the circle intersects the origin, we also have  $\alpha ^2 + \beta^2 = (r_1 / 2 )^2$.
Given that we can substitute $a$ and $b$ into \cref{eq:circInToOut1} we can re-write it as
$(a - \alpha)^2 + (b - \beta)^2 = (r_1/2)^2 $. Given we only need to find $\alpha$ and $\beta$ we can re-write these as $x$ and $y$ respectively, and find that we have to solve
\begin{equation}
  (a - x)^2 + (b - y)^2 = R^2 \quad \mathrm{and} \quad x^2 + y^2 = R^2
\end{equation}
where $R=r_1 / 2$. This problem is very similar to that of \cref{eq:toSolve1}, the solutions are
\begin{align}
  x \rightarrow \alpha & = \frac{a(a^2 + b^2) \pm \sqrt{b^2 (4R^2 -a^2 -b^2)   (a^2 + b^2) }}{2 (a^2 + b^2)} \\
  y \rightarrow \beta & = \frac{b(a^2 + b^2) \pm \sqrt{a^2 (4R^2 -a^2 - b^2) (a^2 + b^2) }}{2 (a^2 + b^2)}
\end{align}
where $\alpha$ and $\beta$ are the coordinates of the centre of the circles. Note that $R^2 \leq a^2 + b^2$.
However, this gives us four possible coordinates, we much check which combinations give the circles that intersect with $(a, b)$.
Analytically we can show that these combinations are $(\alpha_+ , \beta_-)$ and $(\alpha_-, \beta_+)$.
First, we'll simplify the above expressions
\begin{align}
  \alpha_\pm &  = \frac{a}{2} \pm \frac{b}{2} q \\
  \beta_\pm & = \frac{b}{2} \pm \frac{a}{2} q
\end{align}
where $q = \frac{\sqrt{(4R^2 - a^2 - b^2)(a^2 + b^2)}}{a^2 + b^2}$.
We check the coordinate $(\alpha_+ , \beta_-)$ first by evaluating $(a - \alpha_-)^2 + (b - \beta_+)^2$:
\begin{align}
  &  (\frac{a}{2} + \frac{b}{2}q)^2 + (\frac{b}{2} - \frac{a}{2}q)^2 \\
  & = \frac{1}{4} \left( a^2 + b^2q^2 + 2abq +b^2 + a^2 -2abq \right) \\
  & = \frac{1}{4} \left( a^2 + b^2 +(4R^2 - a^2 - b^2) \right) \\
  & = R^2.
\end{align}
Therefore, $(\alpha_+ , \beta_-)$ describes the coordinates of one of the circles.
By inspection we can see that $(\alpha_- , \beta_+)$ are also the coordinates of the other circle.
This rules out the other two combinations of coordinates as possible centres.
For a detector geometry defined by three layers at radii 0.532\,m, 0.582\,m  and 0.632\,m the phi window for these layers is:
\begin{table}
  \centering
  \begin{tabular}{cc}
    \toprule
    Layer change & $\Delta \phi$ (rad) \\
    \midrule
    $1 \rightarrow 2$ & 0.418 \\
    $1 \rightarrow 3$ & 0.570 \\
    $2 \rightarrow 3$ & 0.400 \\
    \bottomrule
  \end{tabular}
  \caption{Phi windows when traversing from one layer to another.}
\end{table}

\subsubsection{A more sensible phi window}
Given the innermost layer is at a radial distance of at least 0.5\,m from the interaction point,
and the magnetic field is 4.0\,T, the minimum particle \pt required for a particle to even reach the barrel layer is 0.6\,GeV.
However, from the trigger perspective we are not interested in low momentum particles, and in fact particles with a momentum below 2\,GeV may not even be considered.
We may therefore define a smaller $\phi$ window in which to match particles by considering the bending of particles with a larger momentum.
To be conservative, we will assume that the minimum particle \pt will be 1\,GeV.
We are interested in a phi window, so to simplify the calculation we will consider two barrel layers at radii $r_1$ and $r_2$ (with $r_2 > r_1$) described by circles centred at the origin, and a particle trajectory
described by $(x-R)^2 + y^2 = R^2$ where $R$ is the bending radius of the particle.
We require that $R > r_2$.
For notational simplicity, consider a general barrel layer with radius $r$.
We then find the intersection of the track with the barrel layer by solving
\begin{equation}
  (x-R)^2 + y^2 = R^2 \quad \mathrm{and} \quad x^2 + y^2 = r^2.
\end{equation}
This gives
\begin{align}
  x^2 + R^2 - 2xR + r^2 - x^2 = R^2 \\
  x = \frac{r^2}{2R}.
\end{align}
The $y$ coordinate is then given by
\begin{equation}
  y = +r\sqrt{1-\frac{1}{2R}}
\end{equation}
where we take the positive solution as we are only interested in the difference in phi angles.
Returning to the specific barrel layers now, the $\phi_i$ position of the track intercept with the barrel at radius $r_i$ is given by
\begin{equation}
  \phi_i = \arccos \left( \frac{r_i}{2R} \right).
\end{equation}
We want to calculate a phi window around a hit in the innermost layer as a loose cut to locate particles in the outer layers.
This $\phi$ window will therefore be
\begin{align}
\Delta \phi & = | \phi_1 - \phi_2 | \\
  & =  \left| \cos^{-1} \left( \frac{r_1}{2R} \right) - \cos^{-1} \left( \frac{r_2}{2R} \right) \right|
\end{align}
The bending radius $R$ as a function of \pt is given by $\pt \{ \mathrm{GeV}/c \} = 1.199 R \{ \mathrm{m} \} $ (for a 4\,T magnetic field).
A plot of the $\Delta \phi$ as a function of \pt is showing in \cref{fig:phiDeviation} for three cases.
\begin{figure}
  \centering
  \includegraphics[width=0.7\linewidth]{images/phiDeviation.pdf}
  \caption{Phi deviation as a function of \pt of a particle track traversing from one barrel layer positioned at radius $r_1$ and a second positioned at radius $r_2$ for three cases.
  Each case has $r_1 = 0.532$\,m however for the blue line, the $r_2$ is 100\,mm further out, for the orange line 50\,mm further out and for the green line 10\,mm further out.}
  \label{fig:phiDeviation}
\end{figure}

\begin{table}
  \centering
  \begin{tabular}{ccc}
    \toprule
    $r_1$ & $r_2$ & $\Delta \phi$ \\
    \midrule
    532 & 632 & 0.064 \\
    542 & 622 & 0.051 \\
    552 & 612 & 0.038 \\
    562 & 602 & 0.026 \\
    572 & 592 & 0.013 \\
    \bottomrule
  \end{tabular}
  \caption{Some numbers for the $\phi$ deviation for a particle with $\pt = 1$\,GeV.
  Distances are in mm, angles in radians.}
\end{table}

\begin{figure}
  \centering
  \includegraphics[width=0.5\linewidth,angle=-90]{images/pileup_hits}
  \caption{The number of hits in the outermost tracking layer (spacing 20mm) as a function of pileup.
  No transverse momentum cut is applied, other than that the hit must have reached the outermost layers.}
\end{figure}

\end{document}


\subsection{L* Execution}
\label{sec:l-exec}
\begin{enumerate}
  \item \begin{minipage}{0.3\textwidth}
          \begin{tabular}{c||c}
            $T_1$ & $\E$ \\
            \hline\hline
            $\E$  & 1    \\
            \hline\hline
            b     & 1    \\
            a     & 0    \\
          \end{tabular}
        \end{minipage} \quad
        \begin{minipage}{0.6\textwidth}
          The table is not closed, because $row(a) = 0$ is not present in S. \\
          $b$ will be promoted.
        \end{minipage}
  \item \begin{minipage}{0.3\textwidth}
          \begin{tabular}{c||c}
            $T_2$ & $\E$ \\
            \hline\hline
            $\E$  & 1    \\
            a     & 0    \\
            \hline\hline
            b     & 1    \\
            ab    & 0    \\
            aa    & 1    \\
          \end{tabular}
        \end{minipage}\quad
        \begin{minipage}{0.6\textwidth}
          $ab$ and $aa$ have been added in $SA$ to respect the condition where: $\forall x \in S, \forall y \in \Sigma, \exists z \in S_{ext} \text{ such that } x \cdot y = z $.\\
          The table is closed ($\forall x \in SA, \exists s \in S \mid row(x) = row(s)$) and consistent (there are no two similar rows in $S$), the \textit{Learner} can send its conjecture.
        \end{minipage}

  \item \begin{minipage}{0.3\textwidth}
          \input{sections/automata/L_aut1.tex}
        \end{minipage}\quad
        \begin{minipage}{0.6\textwidth}
          This is the automaton sent.
          The states are $0$ and $1$ since there are two distinct rows in the upper part of the table, $1$ is the initial state since $row(\E) = 0$ and it is also accepting since $T(\E) = 1$.\\
          Transitions are made as follow: \\
          \begin{itemize}
            \item $\delta(1, a) = \delta(row(\E), a) = row(\E \cdot a) = 0$.
            \item $\delta(1, b) = \delta(row(\E), b) = row(\E \cdot b) =  1$.
            \item $\delta(0, a) = \delta(row(a), a) = row(a \cdot a) =  1$.
            \item $\delta(0, b) = \delta(row(a), b) = row(a \cdot b) = 0$.
          \end{itemize}
          However, the automaton is not valid, since the word \textit{aba} is accepted by the conjecture but not by the \textit{Teacher}.
        \end{minipage}

  \item \begin{minipage}{0.3\textwidth}
          \begin{tabular}{c||c}
            $T_3$ & $\E$ \\
            \hline\hline
            $\E$  & 1    \\
            a     & 0    \\
            aba   & 0    \\
            ab    & 0    \\
            \hline\hline
            b     & 1    \\
            aa    & 1    \\
            abab  & 0    \\
            abaa  & 0    \\
            abb   & 1    \\
          \end{tabular}
        \end{minipage} \quad
        \begin{minipage}{0.5\textwidth}
          $aba$ as been added in $S$ as well as each of its prefixes. $abab, abaa \text{ and } abb$ have been added in $SA$ to keep the \OT complete. \\
          This table is not consistent: $row(a) = row(aba)$ but taking $row(a \cdot a) \neq row(aba \cdot abaa)$. Column $a$ is going to be added because $T(a \cdot a \cdot \E) \neq row(aba \cdot a \cdot \E)$
        \end{minipage}

  \item \begin{minipage}{0.3\textwidth}
          \begin{tabular}{c||c|c}
            $T_4$ & $\E$ & a \\
            \hline\hline
            $\E$  & 1    & 0 \\
            a     & 0    & 1 \\
            aba   & 0    & 0 \\
            ab    & 0    & 0 \\
            \hline\hline
            b     & 1    & 1 \\
            aa    & 1    & 1 \\
            abab  & 0    & 0 \\
            abaa  & 0    & 0 \\
            abb   & 1    & 1 \\
          \end{tabular}
        \end{minipage} \quad
        \begin{minipage}{0.5\textwidth}
          $T_43$ is not closed: $row(b)$ is not present in $S$, so it will be promoted.
        \end{minipage}

  \item \begin{minipage}{0.3\textwidth}
          \begin{tabular}{c||c|c}
            $T_5$ & $\E$ & a \\
            \hline\hline
            $\E$  & 1    & 0 \\
            a     & 0    & 1 \\
            aba   & 0    & 0 \\
            ab    & 0    & 0 \\
            b     & 1    & 1 \\
            \hline\hline
            aa    & 1    & 1 \\
            abab  & 0    & 0 \\
            abaa  & 0    & 0 \\
            abb   & 1    & 1 \\
            bb    & 1    & 1 \\
            ba    & 1    & 1 \\
          \end{tabular}
        \end{minipage} \quad
        \begin{minipage}{0.5\textwidth}
          $T_5$ is not consistent $row(aba) = row(ab)$ but $row(abab) \neq row(abb)$
          Column $b$ is going to be added because $T(aba \cdot b \cdot \E) \neq row(ab \cdot b \cdot \E)$
        \end{minipage}

  \item \begin{minipage}{0.3\textwidth}
          \begin{tabular}{c||c|c|c}
            $T_6$ & $\E$ & a & b \\
            \hline\hline
            $\E$  & 1    & 0 & 1 \\
            a     & 0    & 1 & 0 \\
            aba   & 0    & 0 & 0 \\
            ab    & 0    & 0 & 1 \\
            b     & 1    & 1 & 1 \\
            \hline\hline
            aa    & 1    & 1 & 1 \\
            abab  & 0    & 0 & 0 \\
            abaa  & 0    & 0 & 0 \\
            abb   & 1    & 1 & 1 \\
            bb    & 1    & 1 & 1 \\
            ba    & 1    & 1 & 1 \\
          \end{tabular}
        \end{minipage} \quad
        \begin{minipage}{0.5\textwidth}
          $T_6$ is closed and consistent.
        \end{minipage}

  \item \begin{minipage}{0.3\textwidth}
          \input{sections/automata/L_aut2.tex}
        \end{minipage}\quad\\
        \begin{minipage}{0.6\textwidth}
          This automaton recognized perfectly the language proposed by the Teacher, so the algorithm can stop.
        \end{minipage}
\end{enumerate}