\chapter{Boundaries reconstruction based on the triangulation refinement}\label{chap:triangulation}
The purpose of this chapter is to provide an alternative approach to the $\alpha$-shapes methods for determining the boundaries $\partial \mbox{\set{R}{}{}}(\Pi)$ of the positive luminance regions in target PS. The idea of the method is based on the triangulation refinement of the source PS explained in Chapter \ref{chap:PS}. The boundaries $\partial \mbox{\set{R}{}{}}(\Pi)$ are approximated by connecting those vertices of the triangles that follow the path same $\Pi$. Numerical results are provided for three optical systems: the two-faceted cup, the TIR-collimator and a parabolic reflector.\\ \indent The PS method is compared to both MC and QMC ray tracing. Discussion and results are provided in the last section of this chapter.
\section{Reconstruction of the boundaries}
In Chapter \ref{chap:PS} we have seen that, using the triangulation refinement, more rays close to the boundaries are traced selecting increasingly smaller values for the parameters $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and $\varepsilon_{\variabile{p}_1}^{\textrm{min}}$. Once the algorithm stops, only the triangles that are expected to be crossed by at least a boundary $\partial \mbox{\set{R}{}{}}(\Pi)$ are taken into account for the construction of the boundary. From now on, we call these triangles the \textit{boundary triangles}. Two triangles are neighbors if they have one side in common. For each boundary triangle its neighbor is found so that an ordered sequence of triangles is constructed. Given a path $\Pi$, the corresponding boundary $\partial\mbox{\set{R}{$1$}{}}(\Pi)$ on \set{S}{}{} is approximated connecting the vertices of the boundary triangles which correspond to rays following path $\Pi$. The edge-ray principle is employed in order to define the corresponding boundaries $\partial \mbox{\set{R}{}{}}(\Pi)$ at the target.
Thus, $\partial \mbox{\set{R}{}{}}(\Pi)$ at the target are given by
\begin{equation}\map{M}{}{}(\partial\mbox{\set{R}{$1$}{}}(\Pi)):\partial\mbox{\set{R}{$1$}{}}(\Pi)\rightarrow\partial\mbox{{R}{}{}}(\Pi),\end{equation}
where $\map{M}{}{}$ is defined in (\ref{eq:map1}) and $\map{M}{}{}(\partial\mbox{\set{R}{$1$}{}}(\Pi))$ is the restriction of $\map{M}{}{}$ to $\partial\mbox{\set{R}{$1$}{}}(\Pi)$ for every path
$\Pi$. \\\indent In this chapter, we develop a criterion to establish the values of the parameters $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$, $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$, $\varepsilon_{\variabile{p}_1}^{\textrm{min}}$ and $\varepsilon_{\variabile{p}_1}^{\textrm{max}}$ that give a good approximation of $\partial \mbox{\set{R}{}{}}(\Pi)$.
 Similar to the selection of $\alpha$ in the $\alpha$-shapes procedure, the triangulation parameters are chosen using \'{e}tendue conservation, i.e., conservation of area in PS. The core of our approach is the following.\\
\indent The \'{e}tendue $U_1$ at the source PS restricted to the rays that arrive at the target is calculated. If all the rays emitted by the source are received by the target, $U_1$ can be easily determined by (\ref{eq:etenduesource1}).
In case some rays do not arrive at the target reaching another detector, we use (\ref{eq:etendueintegralsource}) and (\ref{eq:etenduesumsource}).
\\ \indent The \'{e}tendue $U_{\textrm{t}}$ at the target PS is calculated using (\ref{eq:etendueintegraltarget}) and (\ref{eq:etenduesumtarget}).
To do so, the triangulation refinement method is applied to the regions $\mbox{\set{R}{}{}}(\Pi)$ for a range of values of $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$ and for a fixed value of $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$. The parameters along the $\variabile{p}$-axis are scaled by
\begin{equation}\label{eq:scaled_parameters}
\begin{aligned}
& \variabile{w} = \frac{\variabile{q}_1^{\textrm{max}}-\variabile{q}_1^{\textrm{min}}}{\variabile{p}_1^{\textrm{max}}-\variabile{p}_1^{\textrm{min}}},\\
&\varepsilon_{\variabile{p}_1}^{\textrm{min}}= \frac{\varepsilon_{\variabile{q}_1}^{\textrm{min}}}{\variabile{w}}, \\
&\varepsilon_{\variabile{p}_1}^{\textrm{max}}= \frac{\varepsilon_{\variabile{q}_1}^{\textrm{max}}}{\variabile{w}},
\end{aligned}
\end{equation}
 where
$\variabile{p}_1^{\textrm{min}}$ and $\variabile{p}_1^{\textrm{max}}$ are the minimum and the maximum $\variabile{p}$-coordinate in \set{S}{}{} and,
$\variabile{q}_1^{\textrm{min}}$ and $\variabile{q}_1^{\textrm{max}}$ are the minimum and the maximum $\variabile{q}$-coordinate in \set{S}{}{}. Every set of parameters gives a certain triangulation, for each of them an approximation of the boundaries $\partial\mbox{\set{R}{}{}}(\Pi)$ is obtained.
Next, the intersection points $(\variabile{q}^{\variabile{i}}( \Pi, \variabile{p}),\variabile{p})_{\variabile{i} = 1, \cdots, \variabile{r}}$ between $\partial\mbox{\set{R}{}{}}(\Pi)$
and the horizontal line $\variabile{p}~=~ \const{constant}$ are calculated for every path $\Pi$, and for $\variabile{p}~\in~[-1,1]$. Ordering their $\variabile{q}$-coordinates corresponding to each direction $\variabile{p}$ in ascending order, the integral in Equation (\ref{eq:etenduetarg1}) is computed.
Changing the values of the parameters, different approximations of $\partial\mbox{\set{R}{}{}}(\Pi)$ are found and, consequently, different values of $U_{\textrm{t}}$. By construction, $U_{\textrm{t}}$ is always underestimated ($U_{\textrm{t}}<U_1$) because the approximated boundaries are found joining the vertices of the \textit{boundary triangles} which are \textit{inside} the regions \set{R}{}{}$(\Pi)$.
\\ \indent To use the parameters that give a good accuracy of the target photometric variables, the difference $\Delta U = U_1-U_{\textrm{t}}$ is calculated for every value of $U_{\textrm{t}}$ found. The values of the parameters that give a small $\Delta U$ provide a triangulation refinement, from which a good approximation of the target photometric variables can be computed.
\\ \indent A similar method as described here is presented by Moore \cite{moore2013methods}. In Moore's method each ray leaves a point source at the same position while the angle coordinate changes. It starts considering three sampling rays and their corresponding paths are taken into account. In case the paths are equal, the rays traced are representative of all the rays inside the polygon that they describe at the target, otherwise an interpolation is required to compute the illumination pattern. This interpolation can affect the efficiency of the method. Our method employs the distribution of the rays at the target PS and avoids using any interpolation. Moreover, a criterion to stop our algorithm is provided in such a way that no more rays than necessary are traced. This makes ray tracing in PS more accurate compared to Moore's procedure. Furthermore, Moore method is not suitable for systems in which the size and the spatial distribution of the source is important as it consider only point sources.\\ \indent
The triangulation refinement method is tested for several optical systems. The results are presented next.
\section{The two-faceted cup}
In this section we apply the triangulation refinement in PS to the two-faceted cup described in Chapter \ref{chap:raytracing} and depicted in Figure \ref{fig:cup}.
We start tracing rays inside the system using PS ray tracing as explained in Chapter \ref{chap:PS}. To avoid rays parallel to the source and rays emitted from the endpoints, we consider their initial position $\variabile{q}_1$ and initial direction $\variabile{p}_1$ such that
\begin{equation*}
\begin{aligned}
\variabile{p}_1&\in[-1+10^{-6},1-10^{-6}] = [\variabile{p}_1^{\textrm{min}}, \variabile{p}_1^{\textrm{max}}], \\
\variabile{q}_1&\in[-2+10^{-12}, 2-10^{-12}] = [\variabile{q}_1^{\textrm{min}}, \variabile{q}_1^{\textrm{max}}].
\end{aligned}
\end{equation*}
A stopping criterion for the triangulation is defined using \'{e}tendue conservation. Since the two-faceted cup is formed by only reflective lines and its target is adjacent to the left and the right reflector (it is located exactly at the top of the system),  all the rays emitted by the source arrive at the target. Thus, from (\ref{eq:etenduesource1}) with $\n_1\sin(\myangle_1^{\textrm{max}})=\variabile{p}_1^\textrm{max}$ and $\variabile{a}=\variabile{q}_1^{\textrm{max}}$ follows
\begin{equation}U_1 = U \approx 8. \end{equation}
%To establish the number of rays needed to achieve a good accuracy of the target intensity, we compare the approximated $U_{\textrm{t}}$, obtained from a given number of rays, to the exact \'{e}tendue $U=U_1$.
Ray tracing in PS is implemented by varying the parameter $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and fixing  $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$ (we choose $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=1$), while the other two parameters are given by (\ref{eq:scaled_parameters}).
Every set of parameters gives a different triangulation at the source PS. The approximated boundaries are computed for several triangulations joining the vertices of the triangles crossed by a boundary.
\begin{figure}[t]
\centering
\begin{subfigure}{.45\textwidth}
  \includegraphics[width=\textwidth]{boundaries_source_triangles2}
 \caption{The black lines are the boundaries at \set{S}{}{}. $1500$ rays are traced using the triangulation refinement with $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.1$. }
  \label{fig:boundaries_s2}
\end{subfigure}%
\hfill
\begin{subfigure}{.45\textwidth}
  \includegraphics[width=\textwidth]{boundaries_target_triangles2}
  \caption{The black lines are the boundaries at \set{T}{}{}. $1500$ rays are traced using the triangulation refinement with $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.1$.} %
  \label{fig:boundaries_t2}
\end{subfigure} %
\hfill
\begin{subfigure}{.45\textwidth}
  \includegraphics[width = \textwidth]{boundaries_source_triangles1}
  \caption{The black lines are the boundaries at \set{S}{}{}. $7500$ rays are traced using the triangulation refinement with $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.025$.}
  \label{fig:boundaries_s1}
\end{subfigure}%
\hfill
\begin{subfigure}{.45\textwidth}
  \includegraphics[width=\textwidth]{boundaries_target_triangles1}
 \caption{The black lines are the boundaries at \set{T}{}{}. $7500$ rays are traced using the triangulation refinement with $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.025$.} %
  \label{fig:boundaries_t1}
\end{subfigure}
\caption{\textbf{Boundaries at \set{S}{}{} and \set{T}{}{} of the two-faceted cup.} The approximated boundaries are computed using the triangulation refinement with two different values of $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$.}
\label{fig:boundaries_cup}
\end{figure}
\\ \indent For example, if we consider $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.1$, $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=1$ and the corresponding parameters for the $\variabile{p}$-axis given by (\ref{eq:scaled_parameters}), a triangulation with around $1500$ rays (vertices of the triangles) is found. The boundaries $\partial$\set{R}{$1$}{}$(\Pi)$ and $\partial$\set{R}{}{}$(\Pi)$ are calculated using this triangulation refinement and are depicted in black in Figures \ref{fig:boundaries_s2} and \ref{fig:boundaries_t2}, respectively. For this set of rays we found $\Delta U \approx 0.53 $. Next, we can decrease $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ to obtain a more precise approximation of $U_{\textrm{t}}$. Choosing $\varepsilon_{\variabile{q}_1}^{\textrm{min}} = 0.025$ and $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=1$, a triangulation formed by around $7500$ rays is obtained. The approximated boundaries $\partial$\set{R}{$1$}{}$(\Pi)$ and $\partial$\set{R}{}{}$(\Pi)$ are depicted with black lines in Figures \ref{fig:boundaries_s1} and \ref{fig:boundaries_t1}, respectively. The approximation of the target \'{e}tendue gives $\Delta U \approx 0.13 $.
 Obviously, the boundaries computation obtained using $\varepsilon_{\variabile{q}_1}^{\textrm{min}} = 0.025$ is more accurate.
Note that, decreasing $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$, the number of rays increases.
\\ \indent In Figure \ref{fig:etendue_cup} we show with the blue line how the target \'{e}tendue varies as a function of the parameter $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$. The exact \'{e}tendue $U=8$ is depicted with the red line. By decreasing $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ an increase of $U_{\textrm{t}}$ is observed. %Furthermore, by construction, $U_{\textrm{t}}$ is always underestimated because the approximated boundaries are found joining the vertices of the \textit{boundaries triangles} which are \textit{inside} the regions \set{R}{}{}$(\Pi)$.
 \begin{figure}[t]
  \center
  \includegraphics[width= 0.7\textwidth]{etendue_cup_epsilon}
  \caption{\textbf{\'{E}tendue for the two-faceted cup.} The total \'{e}tendue as an area in PS is depicted with the red line. The approximated \'{e}tendue for a range of values of
$\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ is shown with the blue line.}
  \label{fig:etendue_cup}
\end{figure}
In Figure \ref{fig:etendue_cup} we show the approximation of $U_{\textrm{t}}$ for at most around $1.2 \cdot 10^5$ rays traced using PS ray tracing with parameters $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.8\cdot 10^{-4}$, $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=1$, $\varepsilon_{\variabile{p}_1}^{\textrm{min}}=\varepsilon_{\variabile{q}_1}^{\textrm{min}}/2$ and $\varepsilon_{\variabile{p}_1}^{\textrm{max}}= \varepsilon_{\variabile{q}_1}^{\textrm{max}}/2$. We expect that further decreasing $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$, the value of $U_{\textrm{t}}$ becomes more precise.
\\ \indent The PS averaged an normalized intensity $\hat{I}_{\textrm{PS}}$ with $1.2 \cdot 10^5$ rays is calculated from (\ref{eq:normalized_PS_intensity}) where ${I}_{\textrm{PS}}$ is given by (\ref{eta2}). The intensity profile is shown in Figure \ref{fig:intensity_cup_triangulation} with the red line. In the same graph we show the reference intensity $\hat{I}_{\textrm{ref}}$ with the dotted blue line. For the two-faceted cup the reference intensity is actually the exact intensity ($\hat{I}_{\textrm{ref}}= \hat{I}_{\textrm{exact}}$) computed as explained in Appendix \ref{app:boundariescup}.
 \begin{figure}[t]
  \center
  \includegraphics[width= 0.8\textwidth]{intensity_cup_triangulation}
  \caption{\textbf{Intensity profile at the target of the two-faceted cup.} The reference intensity is the exact intensity. The PS intensity is computed using the triangulation refinement with $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=0.8\cdot 10^{-4}$, $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=1$, $\varepsilon_{\variabile{p}_1}^{\textrm{min}}=\varepsilon_{\variabile{q}_1}^{\textrm{min}}/2$ and $\varepsilon_{\variabile{p}_1}^{\textrm{max}}= \varepsilon_{\variabile{q}_1}^{\textrm{max}}/2$. Around $1.2 \cdot 10^5$ rays are traced.}
  \label{fig:intensity_cup_triangulation}
\end{figure}
\\ \indent
Finally, we compare PS ray tracing with both MC and QMC ray tracing by computing the error between the approximated intensities $\hat{I}_{\textrm{A}}  (\textrm{A}= \textrm{MC}, \textrm{QMC}, \textrm{PS})$ and the exact intensity $\hat{I}_{\textrm{ref}}$ using (\ref{eq:error}) with $\nbin = 100$. The results are shown in Figure \ref{fig:error_cup_triangulation} where the MC, QMC and PS intensity are depicted with the green, blue and red line, respectively.
 \begin{figure}[t]
  \center
  \includegraphics[width= 0.8\textwidth]{error_cup_triangulation}
  \caption{\textbf{Error plot for the two-faceted cup.} The errors between the approximated intensities $\hat{I}_{\textrm{A}} (\textrm{A}= \textrm{MC}, \textrm{QMC}, \textrm{PS})$ and the exact intensity $\hat{I}_{\textrm{exact}}$.}
  \label{fig:error_cup_triangulation}
\end{figure}
The graph shows that using PS ray tracing in combination with the triangulation refinement admits tracing far less rays compared to MC ray tracing.
A comparison between QMC ray tracing and PS ray tracing shows that more rays are needed in PS. Indeed, although the shapes of all the regions \set{R}{}{}$(\Pi)$ are very smooth, their boundaries at the edge of the target phase space \set{T}{}{} are difficult to approximate by triangles. With the triangulation refinement, the vertical straight lines at the edge of \set{T}{}{} are always approximated by a broken line. However, the two-faceted cup is a very simple system and QMC ray tracing does not requires a large number of rays to obtain the desired accuracy. \\ \indent Nevertheless, PS ray tracing has a big advantage compared to QMC ray tracing. Indeed, as we have seen in Chapter \ref{chap:raytracing}, MC and QMC ray tracing are binning procedures. Therefore, the MC and QMC intensities are given by the average over every bin and the error also depends on the number of bins. % according to Equations \ref{} \ref{}
PS ray tracing gives a point-wise intensity along all possible directions. In the simulations shown in this thesis we always compute the average PS intensity. This is needed to give a fair comparison of PS ray tracing versus MC and QMC ray tracing. It is very important to observe that no error related to the number of bins is involved in the PS procedure. \\ \indent
To investigate in more detail the performance of PS ray tracing, we test the method for more complicated systems. In the next paragraph we present the results for a TIR-collimator.
\section{A TIR-collimator}
In this section we provide the results of PS ray tracing for a TIR-collimator, using the triangulation refinement to compute the boundaries $\partial$\set{R}{}{}$(\Pi)$ in target PS. In particular, we consider the TIR-collimator depicted in Figure \ref{fig:analyticlens}. Since this system is located in two different media (air and glass), also the refraction law plays a role in the ray tracing procedure. We run PS ray tracing for the TIR-collimator several times gradually increasing the number of rays, i.e., gradually decreasing the values of the parameters $\varepsilon_{\variabile{q}_1}^{\textrm{min}}, \varepsilon_{\variabile{q}_1}^{\textrm{max}}, \varepsilon_{\variabile{p}_1}^{\textrm{min}}$ and $\varepsilon_{\variabile{p}_1}^{\textrm{max}}$ in the triangulation. In order to trace more rays close to the boundaries, we decide to vary only the values of $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and $ \varepsilon_{\variabile{p}_1}^{\textrm{min}}$ while fixing the values of $ \varepsilon_{\variabile{q}_1}^{\textrm{max}}$ and $ \varepsilon_{\variabile{p}_1}^{\textrm{max}}$ as the last two are responsible of the number of rays inside the regions \set{R}{}{}$(\Pi)$. Every ray traced has initial position coordinate $\variabile{q}_1\in[-\variabile{a}, \variabile{a}]$ with $\variabile{a}=2$ and the initial direction coordinate $\variabile{p}_1\in[-1,1]$. Therefore, the source PS of the TIR-collimator is the rectangular domain \set{S}{}{}$= [-2, 2] \times [-1, 1]$. The parameters $\varepsilon_{\variabile{p}_1}^{\textrm{min}}$ and $\varepsilon_{\variabile{p}_1}^{\textrm{max}}$ are scaled as in Equation (\ref{eq:scaled_parameters}).
\\ \indent To determine the triangulation refinement that gives a good approximation of the target intensity, we compare $U_1$ (source \'{e}tendue) to $U_{\textrm{t}}$ (target \'{e}tendue) and use \'{e}tendue conservation. In this case, not all light emitted by the source of the TIR-collimator arrives at the target. Indeed, using PS ray tracing, $\npath=7$ different paths $(\Pi_\lineai)_{\lineai=1, \cdots, \npath}$ are found but only five of them are paths from the source (line $1$) to the target (line $12$) (see also Section \ref{sec:results-Tir-alpha}). Thus, we need to remove from the total area of \set{S}{}{} those parts occupied by the rays that arrive at some others detectors and not at the target. % white areas in figure...
Indicating with $A_T$ the area of each of these parts (see Section \ref{sec:results-Tir-alpha}), the source PS is given by:
\begin{equation}
U_1 = 8-2A_T\approx 7.77.
\end{equation}
The target \'{e}tendue $U_{\textrm{t}}$ is obtained from (\ref{eq:etenduetarg}) for a range of values of $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and for $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=1$ fixed. The boundaries $\partial$\set{R}{}{}$(\Pi)$ are found for every value of $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and, $U_{\textrm{t}}$ is calculated for each of these boundaries.
The results shown in Figure \ref{fig:etendue_tir_triangulation} give the \'{e}tendue plot as a function of $\varepsilon_{\variabile{q}_{1}}^{\textrm{min}}$.
 \begin{figure}[t]
  \center
  \includegraphics[width = 0.7\textwidth]{etendue_tir_triangulation}
  \caption{\textbf{\'{E}tendue of the TIR-collimator.} A comparison between $U_1$ and $U_{\textrm{t}}$ shows that by decreasing the value of $\varepsilon_{\variabile{q}_{1}}^{\textrm{min}}$, $\Delta U= U_1-U_{\textrm{t}}$ decreases.}
  \label{fig:etendue_tir_triangulation}
\end{figure}
\\ \indent
The best approximation of $U_{\textrm{t}}$ shown in the previous graph is obtained using $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=1.6\cdot 10^{-3}$, tracing around $1.62 \cdot 10^5$ rays. The boundaries $\big(\partial\mbox{\set{R}{$1$}{}}(\Pi_{\lineai})\big)_{\lineai = 1, \cdots, 5}$ and $\big(\partial\mbox{\set{R}{}{}}(\Pi_{\lineai})\big)_{\lineai = 1, \cdots, 5}$ of the regions formed by these rays are shown with red lines in Figure \ref{fig:boundaries_TIR_triangulation} at the left and the right, respectively (see also Figure \ref{fig:Tir2} for a comparison of the target PS).
\begin{figure}[h]
 \begin{subfigure}[t]{0.5\textwidth}
\centering
    \includegraphics[width=\textwidth]{boundaries_triang_source_tir}
    \caption{Boundaries at the source PS.}
    \label{fig:boundaries_triang_source_tir}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.5\textwidth}
\centering
    \includegraphics[width = \textwidth]{boundaries_triang_targ_tir}
    \caption{Boundaries at the target PS.}
    \label{fig:boundaries_triang_target_tir}
\end{subfigure}
\caption{\textbf{Boundaries at \set{S}{}{} and \set{T}{}{} of the TIR-collimator.} The red lines show the boundaries found using $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=1.6\cdot 10^{-3}, \varepsilon_{\variabile{q}_1}^{\textrm{max}}=1, \varepsilon_{\variabile{p}_1}^{\textrm{min}}=\varepsilon_{\variabile{q}_1}^{\textrm{min}}/2$ and $\varepsilon_{\variabile{p}_1}^{\textrm{max}} = \varepsilon_{\variabile{q}_1}^{\textrm{max}}/2$.}
 \label{fig:boundaries_TIR_triangulation}
\end{figure}
 \\ \indent
The target PS intensity $\hat{I}_{\textrm{PS}}$ is computed and, it is compared with a reference intensity $\hat{I}_{\textrm{ref}}$ which is given by QMC ray tracing with $10^7$ rays (as the exact intensity for the TIR-collimator is unknown). The profile of the two intensities is given in Figure \ref{fig:intensity_tir_triangulation}.
 \begin{figure}[ht]
  \center
  \includegraphics[width= 0.8\textwidth]{intensity_tir_triangulation}
  \caption{\textbf{Target intensity for the TIR-collimator.} The PS intensity $\hat{I}_{\textrm{PS}}$ is computed using PS ray tracing with around $1.62\cdot 10^5$ rays. The reference intensity $\hat{I}_{\textrm{ref}}$ is obtained by QMC ray tracing with $10^7$ rays.}
  \label{fig:intensity_tir_triangulation}
\end{figure}
\\ \indent
To validate our method, PS ray tracing is compared to both MC and QMC ray tracing. The error between the approximated intensities $\hat{I}_{\textrm{A}} (\textrm{A}=\textrm{QMC}, \textrm{MC}, \textrm{PS})$ and the reference intensity $\hat{I}_{\textrm{ref}}$ as a function of the number of rays traced is calculated. The error plot is shown in a logarithmic scale in Figure \ref{fig:error_tir_triangulation} where the MC, QMC and PS convergences are shown with the green, the blue and the red line, respectively. The black dotted line is a line with slope $-\frac{1}{2}$, the blue dotted line has slope $-1$.The graph shows that MC ray tracing converges, for $\nrays \rightarrow \infty$, with an order of $\mathcal{O}\big(\frac{1}{\sqrt{\nrays}}\big)$, while both PS and QMC ray tracing have a speed of convergence of the order $\mathcal{O}\big(\frac{1}{\nrays}\big)$.
Note that, to obtain an error of $10^{-4}$, PS ray tracing allows tracing $10^2$ times less rays compared to MC ray tracing and almost $10$ times less rays compared to QMC ray tracing.
 \begin{figure}[t]
  \center
  \includegraphics[width= 0.8\textwidth]{error_nr_triangulation_tir}
  \caption{\textbf{Error as a function of the number of rays for the TIR-collimator.} The reference intensity $\hat{I}_{\textrm{ref}}$ is obtained by QMC ray tracing with $10^7$ rays.}
  \label{fig:error_tir_triangulation}
\end{figure}
\begin{figure}[t]
  \center
  \includegraphics[width= 0.75\textwidth]{error_tir_triangulation_time}
  \caption{\textbf{Error as a function of the CPU-time for the TIR-collimator.} The reference intensity $\hat{I}_{\textrm{ref}}$ is obtained by QMC ray tracing with $10^7$ rays.}
  \label{fig:error_tir_triangulation_time}
\end{figure}
\\ \indent Finally, in order to show the advantages of PS ray tracing in terms of the computational time, we provide an error convergence as a function of the CPU-time for all the three methods (MC, QMC and PS raytracing) shown in Figure \ref{fig:error_tir_triangulation_time}. The choice of the colors is consistent with Figure \ref{fig:error_tir_triangulation}. We remark that the averaged normalized intensity in PS $\hat{I}_{\textrm{PS}}$ is computed over every bin using the trapezoidal rule and discretizing each bin into $10$ more sub-intervals of the same length. Because of this, the CPU-time for computing the PS intensity is divided by $10$ to provide the real CPU-time of the PS method when it is not compared to binning procedure, e.g., MC and QMC ray tracing. This is done for \textit{all} the results presented in this thesis. From Figure \ref{fig:error_tir_triangulation_time}, we observe that PS ray tracing outperforms both MC and QMC ray tracing. PS ray tracing is approximately $10^2$ times faster than MC ray tracing and $10$ times faster than QMC ray tracing. \\ \indent The results shown in Figures \ref{fig:error_tir_triangulation} and \ref{fig:error_tir_triangulation_time} are reported in Tables \ref{tab:ps_error_triangulation} and \ref{tab:mc_error_triangulation}.
\begin{table}[t]
\centering
\caption{\bf Errors of the PS intensity for the TIR-collimator}
\begin{tabular}{lllll}
 \hline   $\varepsilon_{\variabile{q}}^{\textrm{max}} $  & $\nrays$ & \'{E}tendue & PS error & PS CPU-time (sec.) \\
  \hline
 $0.05$ & $3\,547$   & $7.50$   &  $1.75\cdot10^{-4}$ & $1.98$\\
$0.025$  & $8\,055$    & $7.61$    & $1.49\cdot 10^{-4}$ & $4.69$ \\
$0.125$  & $17\,300$    & $7.69$  & $8.68\cdot 10^{-5}$ & $10.61$\\
 $6.3 \cdot 10^{-3}$  & $38\,300$  & $7.73$   & $4.43\cdot 10^{-5}$ & $26.56$\\
 $3.1 \cdot 10^{-3}$ & $79\,600$  & $7.75$    & $2.27\cdot 10^{-5}$ & $83,21$\\
$1.6 \cdot 10^{-3}$ & $162\,300$  & $7.76$    & $1.20\cdot 10^{-5}$ & $240.53$\\
 \hline
 \end{tabular}
\label{tab:ps_error_triangulation}
 \end{table}
\begin{table}[t] \label{tab:table_tir_triangulation}
\centering
\caption{\bf Errors of the MC and QMC intensities for the TIR-collimator}
\begin{tabular}{lllll}
 \hline   $\nrays$ & MC error & MC CPU-time (sec.) & QMC error  & QMC CPU-time (sec.)\\
  \hline
 $10^3$     & $2.09\cdot 10^{-3}$ & $2.73$ & $1.43\cdot10^{-3}$    & $2.63$\\
 $10^4$     & $6.42\cdot 10^{-4}$ & $25.98$ & $3.03\cdot 10^{-4}$   & $25.84$\\
 $10^5$     & $1.92\cdot 10^{-4}$ & $259.92$ & $5.82\cdot 10^{-5}$   & $258.28$\\
 $10^6$     & $7.45\cdot 10^{-5}$ & $2585.83$   & $1.28\cdot 10^{-5}$   & $2482.67$\\
 \hline
 \end{tabular}
 \label{tab:mc_error_triangulation}
 \end{table}
%\begin{table}[t]
%\centering
%\caption{\bf Errors of the QMC intensity for the TIR-collimator}
%\begin{tabular}{lllll}
% \hline  $\nrays$\;  & QMC error  & CPU-time (sec.)\\
%  \hline
% $10^3$   & $1.43\cdot10^{-3}$    & $2.63$  \\
%$10^4$    & $3.03\cdot 10^{-4}$   & $25.84$   \\
%$10^5$    & $5.82\cdot 10^{-5}$   & $258.28$  \\
% $10^6$   & $1.28\cdot 10^{-5}$   & $2482.67$  \\
% \hline
% \end{tabular}
% \label{tab:qmc_error_triangulation}
% \end{table}
%Using PS ray tracing an error equal to $10^{-4}$ is achieved tracing almost $10$ time less compared to QMC ray tracing and $100$ times less rays compared to QMC ray tracing.
Next, we show the results for a system in which more than $5$ paths are possible.
In particular, we present the results for an optical system for which multiple reflections between rays and the mirrors occur.
\section{A Parabolic reflector}
In this section we show an example of a parabolic reflector which is depicted in Figure \ref{fig:PR}.
 It consists of a source \point{S} (line $1$), a target \point{T} (line $4$) parallel to \point{S} and two reflectors (lines $2$ and $3$) which are arcs of the same parabola.
  The minimum of the parabola is located at the point with $\variabile{x}$-coordinate equal to $0$. $\point{S}=[\variabile{-a}, \variabile{a}]$ (with $\variabile{a}=2$) and $\point{T}=[-\variabile{b},\variabile{b}]$ (with $\variabile{b}=17$) are lines perpendicular to the optical axis (\variabile{z}-axis) and are located at $\variabile{z}=0$ and $\variabile{z}=40$, respectively.
All the optical lines are located in air, therefore the index of refraction ${n}=1$ for every line.
The optical axis of the system in Figure \ref{fig:PR} corresponds to the \variabile{z}-axis.
\begin{figure}[h!]
\centering
\includegraphics[width=0.75\textwidth]{parabolic_reflector}
\caption{\textbf{A parabolic reflector.}  Each line of the system is labeled with a number.
   The source \point{S}$= [2,2]$ (line $1$) is located on the $\variabile{x}$-axis.
   The target \point{T}$= [-17, 17]$ (line $4$) is parallel to the source and is located at a height $ \variabile{z}= 40$.
   The left and right reflectors (lines $2$ and $3$) are arcs of the same parabola.}
\label{fig:PR}
\end{figure}
We trace rays in PS with source direction coordinates $\variabile{p}_1 \in [-1,1]$ and source position coordinates $\variabile{q}_1\in[-\variabile{a}+\varepsilon,\variabile{a}-\varepsilon]$ where $\varepsilon>0$ is a small number. In particular we take $\varepsilon = 10^{-12}$.\\ \indent As an example we show the triangulation refinement obtained for the parameters $$\varepsilon_{\variabile{q}_1}^{\textrm{min}} = 0.025, \; \varepsilon_{\variabile{q}_1}^{\textrm{max}} = 0.5, \; \varepsilon_{\variabile{p}_1}^{\textrm{min}} = \varepsilon_{\variabile{q}_1}^{\textrm{min}}/2, \mbox{ and }  \varepsilon_{\variabile{p}_1}^{\textrm{max}} = \varepsilon_{\variabile{q}_1}^{\textrm{max}}/2, $$ for which around $8300$ rays are traced in PS. Their distribution at \set{S}{}{} and \set{T}{}{} is shown in Figures \ref{fig:source_triang_pr} and \ref{fig:target_triang_pr}, respectively.
\begin{figure}[h]
 \begin{subfigure}[h]{0.47\textwidth}
\centering
    \includegraphics[width=\textwidth]{pr_source}
    \caption{Source PS \set{S}{}{} $= [-\variabile{a}+\varepsilon,\variabile{a}-\varepsilon]\times[-1,1]$}
    \label{fig:source_triang_pr}
\end{subfigure}
\hfill
\begin{subfigure}[h]{0.47\textwidth}
\centering
    \includegraphics[width = \textwidth]{pr_target}
    \caption{Target PS \set{T}{}{} $= [-\variabile{b},\variabile{b}]\times[-1,1]$}
    \label{fig:target_triang_pr}
\end{subfigure}
\caption{\textbf{Rays distribution at \set{S}{}{} and \set{T}{}{} of the parabolic reflector.} Around $8300$ rays are traced using PS ray tracing with parameters $\varepsilon_{\variabile{q}_1}^{\textrm{min}} = 0.025, \; \varepsilon_{\variabile{q}_1}^{\textrm{max}} = 0.5, \; \varepsilon_{\variabile{p}_1}^{\textrm{max}} = \varepsilon_{\variabile{q}_1}^{\textrm{min}}/2, \mbox{ and }  \varepsilon_{\variabile{p}_1}^{\textrm{min}} = \varepsilon_{\variabile{q}_1}^{\textrm{max}}/2$ . $17$ different paths are found, each of them correspond to a certain number of reflections.}
 \label{fig:phase_space_pr}
\end{figure} The distribution of the rays in PS gives information about the paths they follow. We note that for the parabolic reflector many paths are found. Every path corresponds to a given number of reflections. Rays can have multiple reflections at lines $2$ and $3$ before arriving at the target. The parameters used in the triangulation refinement not only establish the number of rays traced but also determine the number of detected paths. For instance, for the values of the parameters defined above, the triangulation refinement is able to detect $17$ different paths. This means that up to $8$ multiple reflections occur between the rays and the two mirrors. Counting the number of rays that follow a given path $\Pi$, we can calculate the fraction of rays for every path. For example, tracing around $8300$ rays, the percentage of the rays that have $8$ multiple reflections along one of the two reflectors is around $0.13\%$. Rays that reflect many times before reaching the target do not give a significant contribution to the target intensity. Decreasing the value of the parameter $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$, more paths can be found. The more reflections occur the smaller the corresponding area in PS is. In order to find as many path as possible, also the parameter $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$ needs to be decreased. Increasing the number of reflections considered, the corresponding regions in PS become smaller and smaller, see Figure \ref{fig:phase_space_pr}. \\ \indent Like for the optical systems considered in the previous sections, a stopping criterion of the triangulation refinement is determined for the parabolic reflector. \'{E}tendue conservation is used in order to find the values of the parameters that give a good approximation of the boundaries of the positive luminance regions in PS. For the parabolic reflector in Figure \ref{fig:PR} all the rays that leave the source arrive at the target. Indeed, line $2$ and $3$ can only reflect rays (refraction law is not involved) and the target \'{e}tendue is equal to the source \'{e}tendue. From Equation (\ref{eq:etenduesource1}) we obtain:
\begin{equation}
U = U_1 = 4(\variabile{a}-\varepsilon)\approx 8.
\end{equation}
A range of values of $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$ is considered (the triangulation parameters for the $\variabile{p}$-axis depend on the $\variabile{q}$-axis parameters according to (\ref{eq:scaled_parameters})). For each couple of values, an approximation of the boundaries $\partial$\set{R}{}{}$(\Pi)$ is found for every path $\Pi$ as explained. $U_{\textrm{t}}$ is calculated for the approximated boundaries using Equation (\ref{eq:etenduetarg}). In Table \ref{tab:etendue_pr} we show how the number of rays traced, the paths found and the value for the target \'{e}tendue depend on the triangulation parameters.
We observe that, decreasing $\varepsilon_{\variabile{q}_1}^{\textrm{min}}$ and $\varepsilon_{\variabile{q}_1}^{\textrm{max}}$ the number of both the rays traced and the paths found increases. A maximum of $17$ different paths are detected. Furthermore, the value of $U_{\textrm{t}}$ gets closer and closer to the exact \'{e}tendue $U$.
\begin{table}[t]
\centering
\caption{\bf Results of the triangulation refinement.}
\begin{tabular}{lllll}
 \hline   $\varepsilon_{\variabile{q}}^{\textrm{min}} $  & $\varepsilon_{\variabile{q}}^{\textrm{max}}$ & $\nrays$ & $\npath$ & \'{E}tendue \\
  \hline
 $0.2$ & $1$   & $643$   &  $11$ & $5.71$\\
$0.1$ & $1$   & $1\,573$   &  $15$ & $7.23$\\
$0.025$  & $0.5$    & $8\,357$  & $17$ & $7.65$\\
 $0.025/2$  & $0.5/2$  & $18\,613$   & $17$ & $7.82$\\
$0.025/4$  & $0.5/4$  & $40\,465$   & $17$ & $7.82$\\
 $0.025/8$ & $0.5/8$  & $86\,529$    & $17$ & $7.96$\\
$0.025/16$ & $0.5/16$  & $185\,581$    & $17$ & $7.98$\\
 \hline
 \end{tabular}
 \label{tab:etendue_pr}
 \end{table}
\\ \indent Using the triangulation refinement that gives the best \'{e}tendue approximation, we calculate the target averaged and normalized PS intensity $\hat{I}_{\textrm{PS}}$ from (\ref{eq:normalized_PS_intensity}).
The intensity profile is shown with the red line in Figure \ref{fig:intensity_pr}. The PS intensity is compared to a reference intensity $\hat{I}_{\textrm{ref}}$, computed using QMC ray tracing with $10^7$ rays. $\hat{I}_{\textrm{ref}}$ is depicted Figure \ref{fig:intensity_pr} with the dotted blue line. The graph shows that the two intensities coincide.
 \begin{figure}[t]
  \center
  \includegraphics[width = 0.7\textwidth]{intensity_pr}
  \caption{\textbf{Target intensity of the parabolic reflector.} For the PS intensity the parameters $\varepsilon_{\variabile{q}_1}^{\textrm{min}}=1.56\cdot 10^{-3}$, $\varepsilon_{\variabile{q}_1}^{\textrm{max}}=3.13\cdot 10^{-2}$, $\varepsilon_{\variabile{p}_1}^{\textrm{min}}=\varepsilon_{\variabile{q}_1}^{\textrm{min}}/2$, and $\varepsilon_{\variabile{p}_1}^{\textrm{max}}=\varepsilon_{\variabile{q}_1}^{\textrm{max}}/2$ are used. Around $8.15 \cdot 10^{4}$ rays are traced in PS. For the reference intensity QMC ray tracing with $10^7$ rays is implemented.}
  \label{fig:intensity_pr}
\end{figure}
\begin{table}[t] \label{tab:table_pr_triangulation}
\centering
\caption{\bf Errors of the PS intensity for the parabolic reflector}
\begin{tabular}{lll}
 \hline   $\nrays$ & PS error & CPU-time (sec.) \\
  \hline
 $643$        & $5.18\cdot10^{-4}$ & $0.24$\\
 $1\,573$       & $8.99\cdot 10^{-4}$ & $0.48$\\
 $8\,357$     & $2.87\cdot 10^{-4}$ & $2.40$ \\
 $18\,613$     & $1.38\cdot 10^{-4}$ & $5.48$\\
 $40\,465$   & $5.80\cdot 10^{-5}$ & $16.14$\\
 $86\,529$    & $2.90\cdot 10^{-5}$ & $55.04$\\
 $185\,581$   & $1.66\cdot 10^{-5}$ & $245.97$\\
 \hline
 \end{tabular}
\label{tab:table_pr_triangulation}
 \end{table}
\\ \indent
Now, our method is compared to both MC and QMC ray tracing.
The error between the approximate intensities $\hat{I}_{\textrm{A}}(\textrm{A}=\textrm{PS}, \textrm{MC}, \textrm{QMC})$ and the reference intensity is calculated. In Figure \ref{fig:error_rays_pr} the error as a function of the number of rays traced is shown for the three methods.
The green line represents the MC error, the blue line pictures the QMC error and the red line depicts the PS error. The errors are shown in a logarithmic scale, the dotted black line has slope $-1/2$, the dotted blue line has slope $-1$. Like for the other systems, MC ray tracing converges proportionally to $1/\sqrt{\nrays}$. QMC convergence is proportional to $1/\nrays$ for $\nrays \rightarrow \infty$. Less rays compared to MC ray tracing are needed in PS. In particular, to achieve an error of $10^{-4}$, around $10$ times less rays in PS are needed compared to MC and few rays less than QMC.
\begin{figure}[t]
  \center
  \includegraphics[width = 0.7\textwidth]{error_pr_100bin_rays}
  \caption{\textbf{Error as a function of the number of rays traced.} Less rays are needed using PS ray tracing compared to both MC and QMC ray tracing.}
  \label{fig:error_rays_pr}
\end{figure}
\\ \indent
Finally, the error as a function of the CPU-time is shown in Figure \ref{fig:error_time_pr}. The MC, QMC and PS errors are depicted with the green, blue and red line, respectively. To obtain an error of the order of $10^{-5}$, PS ray tracing is $10$ times faster than MC ray tracing while becomes twice slower than QMC ray tracing. The detailed results of the numerical simulations are reported in Tables \ref{tab:mc_error_pr_triangulation} and \ref{tab:QMC_error_pr_triangulation}.
\begin{figure}[t]
  \center
  \includegraphics[width = 0.7\textwidth]{error_pr_100bin_time}
  \caption{\textbf{Error as a function of the CPU-time.} PS ray tracing has significant advantages in terms of the CPU-time compared to MC ray tracing. For the parabolic reflector the computational time is comparable with QMC ray tracing.}
  \label{fig:error_time_pr}
\end{figure}
\begin{table}[t]
\centering
\caption{\bf Errors of the MC intensity for the parabolic reflector}
\begin{tabular}{lll}
 \hline   $\nrays$ & MC error & CPU-time (sec.) \\
  \hline
 $10^3$     & $1.18\cdot10^{-3}$ & $0.39$\\
 $10^4$     & $5.74\cdot 10^{-4}$ & $3.43$ \\
 $10^5$     & $1.73\cdot 10^{-4}$ & $33.13$\\
 $10^6$     & $5.79\cdot 10^{-5}$ & $328.96$\\
 $10^7$     & $1.68\cdot 10^{-5}$ & $3325.39$\\
 \hline
 \end{tabular}
 \label{tab:mc_error_pr_triangulation}
 \end{table}
\begin{table}[t]
\centering
\caption{\bf Errors of the QMC intensity for the parabolic reflector}
\begin{tabular}{lll}
 \hline   $\nrays$ & QMC error & CPU-time (sec.) \\
  \hline
 $10^3$     & $1.36\cdot10^{-3}$ & $0.53$\\
 $10^4$     & $2.05\cdot 10^{-4}$ & $3.44$ \\
 $10^5$     & $2.89\cdot 10^{-5}$ & $31.22$\\
 $10^6$     & $6.96\cdot 10^{-6}$ & $314.59$\\
 \hline
 \end{tabular}
 \label{tab:QMC_error_pr_triangulation}
 \end{table}
\\ \indent As explained above, MC and QMC errors also depend on the number of bins $\nbin$. In the simulations shown in this chapter we have always considered $\nbin = 100$. On the contrary, PS ray tracing calculates the intensity point-wise, nevertheless we considered $\nbin = 100$ bins at the target and we calculate the averaged normalized PS intensity over every bin in order to have a precise comparison with the binning procedures. Because of this, we expect that increasing the number of bins, the average PS intensity becomes more accurate. To verify this conjecture, we implemented MC, QMC and PS ray tracing considering a partitioning of the interval $[-1,1]$ into $\nbin = 150$ bins. The number of rays considered for the reference intensity have to be increased $(1.5)^5$ times. The averaged normalized intensities $\hat{I}_{\textrm{A}} (\textrm{A}=\textrm{MC}, \textrm{QMC}, \textrm{PS})$ found considering $\nbin=150$ bins are compared with the reference intensity $\hat{I}_{\textrm{ref}}$ (averaged and normalized) which is computed using QMC ray tracing with $7.6\cdot10^7$ rays. The error as a function of the number of rays is shown in Figure \ref{fig:error_pr_150_bin}. The results show that, using PS ray tracing an error of the order of $10^{-5}$ is obtained tracing around $10^2$ times less rays than MC ray tracing and twice less rays than QMC ray tracing. We conclude that, increasing the number of bins, PS error outperforms both MC and QMC ray tracing (see also Figure \ref{fig:error_rays_pr}).
\begin{figure}[h]
  \center
  \includegraphics[width= 0.7\textwidth]{error_pr_150bins_ray}
  \caption{\textbf{Error plot for the parabolic reflector considering $\nbin=150$ bins.} Increasing the number of bins, the PS error decreases resulting in a better convergence compared to MC and QMC ray tracing.}
  \label{fig:error_pr_150_bin}
\end{figure}
\section{Discussion and conclusions}
In this chapter, we presented a method to calculate the boundaries of the positive luminance regions with in PS. This method does not depend on the parameter $\alpha$ needed for the $\alpha$-shapes method presented in Chapter \ref{chap:boundaries_alpha}. Indeed, given a triangulation at the source PS, the boundaries are computed connecting the vertices of the boundary triangles, i.e., triangles crossed by a boundary, that follow the same path. Employing \'{e}tendue conservation, a stopping criterion for the triangulation refinement was developed. We applied the method to three different optical systems: the two-faceted cup, a TIR-collimator, and a parabolic reflector. Numerical results show that PS ray tracing is faster and more accurate than MC ray tracing. Compared to QMC ray tracing we observed accuracy and speed advantages of an order of magnitude with our method for the TIR-collimator.
For the two-faceted cup, PS ray tracing has a slower convergence compared to QMC ray tracing. For the parabolic reflector PS and QMC ray tracing display similar convergence. As an example, for this system we showed that increasing the number of bins the errors decrease.
To conclude we state that QMC ray tracing performs better than PS ray tracing for very simple optical systems, but the PS approach is more suitable for complicated optical systems.
\\ \indent In order to further improve PS ray tracing we develop a new method which employs the PS of \textit{all} the optical lines. This method is explained in the next chapter.
%We claim that PS ray tracing is also more accurate than the ray tracing procedure proposed by Moore (2013), \cite{moore2013methods}.
%The novelty of our approach compared to the method used by Moore, is briefly explained below. First, to compute the output intensity, we employ the phase space of the target. This avoids the use of any interpolation to compute the photometric variables and therefore, more accurate results are obtained.
%Second, in  all rays that leave the source start at the same position and only a sampling angular range is given. In our approach a rectangular source is considered thus, both the angular and spatial coordinates of each ray change. This extra variable can produce very irregular shapes of the regions at target phase space. To overcome this issue, we employ the edge-ray principle and we consider the regions at source phase space where the distribution of the rays is much more regular and the corresponding boundaries are easily computed.
%As a consequence, our procedure is suitable to compute the output intensity as function of both the angular or the spatial coordinates.
%Third, using the conservation of \'{e}tendue, we provided a criterion to stop the triangulation refinement. In this way we can estimate the number of rays required to obtain the desired accuracy and thus, we avoid tracing more rays than necessary.

%\section{The Compound Parabolic Concentrator (CPC)}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Hypothesis Testing}
\label{chap:hypo}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Hypothesis Test Selection}
\label{hypo:test_selection}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{\texorpdfstring{$Z$}{Z}-Test}
\label{hypo:Z_test}

When we sample from a distribution known to be Gaussian,
or can assume the sample means are approximately normally distributed via the central limit theorem (CLT) of \cref{stats:CLT},
and know the population standard deviation $\sigma$,
we can perform hypothesis testing on the sample mean with a \Ztest.
For a sample of size $n$ with sample mean $\expval{x}$ and population standard deviation $\sigma$
we compute the \Zscore,

\begin{equation}\label{eq:hypo:Z}
Z = \frac{\expval{x} - \mu_{0}}{\sigma_{\expval{x}}} = \frac{\expval{x} - \mu_{0}}{\sigma/\sqrt{n}},
\end{equation}

\noindent and test the null hypothesis, $H_{0}$, that the mean is $\mu_{0}$
by comparing $Z$ to the standard normal distribution.
Depending on if we wish to perform a one-sided or two-sided
test\footnote{Note we can also perform two-sample and paired {\Ztest}s, see the analogous \ttest sections with $\sigma$ replacing $s$.} we
find the probability of observing $P\left(x < Z\right)$, $P\left(Z < x\right)$, or $P\left(Z < \abs{x}\right)$
from the cumulative distribution function (CDF).
This \pvalue then determines if we accept, $\pvalue < \alpha$, or reject, $\alpha \leq \pvalue$,
$H_{0}$ at a given significance level $\alpha$, typically \num{0.05} or lower.
We can
\href{https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.zscore.html}{compute $Z$} with
\texttt{scipy.stats.zscore},
and \href{https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html}{find the \pvalue} with
\texttt{scipy.stats.norm.cdf} or \texttt{scipy.stats.norm.sf},
being careful to include the correct side(s).
For an illustration see \cref{fig:two_sided_t_test}, with \Zscore replacing the \tstat.

If we do not know the population standard deviation $\sigma$, as is often the case,
or if $n \lesssim 30$, we should use a \ttest instead.
However, if $50 \lesssim n$ we may be able to approximate $\sigma \approx s$
and use a \Ztest anyway.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Student's \texorpdfstring{$t$}{t}-Test}
\label{hypo:t_test}

Student's \ttest can be used to
statistically compare the means of two samples via the \tdist
given a \tstat and degrees of freedom $\nu$.
The \ttest is appropriate when each sample is small\footnote{For
larger sample sizes the \tdist approaches the normal distribution which should be used instead.}, $n \lesssim 30$,
and drawn from a larger normally distributed population with an unknown standard deviation.
The \pvalue returned by the test estimates the probability of obtaining the sample means
assuming $H_{0}$, typically that the samples share the same mean, is true.
We can compute two-sided, \ie the means are not equal, or one-sided, \ie mean 1 is $>$ or $<$ mean 2,
forms of the \ttest in three basic variations.
See \cref{fig:two_sided_t_test} for an illustration of a two-sided test's $H_{0}$ rejection regions.
The \texttt{scipy.stats.ttest\_*}
\href{https://docs.scipy.org/doc/scipy/reference/stats.html#statistical-tests}{family of functions}
make it easily to compute {\tstat}s and {\pvalue}s in practice.

\begin{figure}
\centering
\includegraphics[width=0.7\textwidth]{figures/hypo/one_side_t_test_rejection_regions.png}
\caption{
Illustration of a two-sided {\ttest}'s null hypothesis, $H_{0}$, rejection regions,
by \href{https://www.machinelearningplus.com/statistics/t-test-students-understanding-the-math-and-how-it-works/}{Selva Prabhakaran}.
In a one-sided \ttest we would only consider the side on a single side.
$\alpha$ is the significance level, typically \num{0.05} or lower.
}
\label{fig:two_sided_t_test}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{One-Sample}
\label{hypo:t_test:one}

In a one-sample \ttest we have one sample of size $n$ with mean $\expval{x}$ and standard deviation $s$,
and test $H_{0}$ that it belongs to a parent population with mean $\mu_{0}$.
In this case the parent population does not need to be normally distributed, but the distribution of possible $\expval{x}$ is assumed to be normal.

The \tstat \cref{eq:hypo:t:one} can be used with $\nu = n-1$ to find a \pvalue.

\begin{equation}\label{eq:hypo:t:one}
t = \frac{\expval{x} - \mu_{0}}{s / \sqrt{n}}
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Two-Sample: Unpaired}
\label{hypo:t_test:two:unpaired}

In a two-sample unpaired \ttest we have two independent samples, each of size $n$,
but with their own sample means $\expval{x_{i}}$ and standard deviations $s_{i}$.
Provided that we can assume that the two parent distributions of $x_{1}$ and $x_{2}$ have the same variance,
we can test $H_{0}$ that the two parent distribution means are equal.

The \tstat \cref{eq:hypo:t:two:unpaired} can be used with $\nu = 2n-2$ to find a \pvalue.

\begin{subequations}\label{eq:hypo:t:two:unpaired}
\begin{align}
t &= \frac{\expval{x_{1}} - \expval{x_{2}}}{s_{p} \sqrt{2/n}} \label{eq:hypo:t:two:unpaired:t} \\
s_{p} &= \sqrt{\left(s^{2}_{x_{1}} + s^{2}_{x_{2}}\right)/2} \label{eq:hypo:t:two:unpaired:s_p}
\end{align}
\end{subequations}

\subsubsection{Different Sample Sizes}
\label{hypo:t_test:two:unpaired:diff_n}

If we relax the sample size condition and let $n_{1} \neq n_{2}$
we can still compute a \tstat,
provided the parent distribution's variances are equal\footnote{A useful guideline is $1/2 < s_{x_{1}} / s_{x_{2}} < 2$.}.

The \tstat \cref{eq:hypo:t:two:unpaired:diff_n} can be used with $\nu = n_{1} + n_{2} - 2$ to find a \pvalue.

\begin{subequations}\label{eq:hypo:t:two:unpaired:diff_n}
\begin{align}
t &= \frac{\expval{x_{1}} - \expval{x_{2}}}{s_{p} \sqrt{\frac{1}{n_{1}} + \frac{1}{n_{2}}}} \label{eq:hypo:t:two:unpaired:diff_n:t} \\
s_{p} &= \sqrt{\left(\left(n_{1} - 1\right)s^{2}_{x_{1}} + \left(n_{2} - 1\right)s^{2}_{x_{2}}\right)/\left(n_{1} + n_{2} -2\right)} \label{eq:hypo:t:two:unpaired:diff_n:s_p}
\end{align}
\end{subequations}

\subsubsection{Different Sample Sizes and Variances (Welch's \texorpdfstring{$t$}{t}-Test)}
\label{hypo:t_test:two:unpaired:diff_n_diff_var}

If we further relax the assumptions and also let the population variances differ
we arrive at Welch's \ttest which approximates\footnote{The
true distribution of $t$ depends somewhat on the unknown population variances,
see the \href{https://en.wikipedia.org/wiki/Behrens\%E2\%80\%93Fisher_problem}{Behrens--Fisher problem}
for more.} the \tdist.

The \tstat \cref{eq:hypo:t:two:unpaired:diff_n_diff_var:t} can be used with $\nu$ \cref{eq:hypo:t:two:unpaired:diff_n_diff_var:dof} to find a \pvalue.

\begin{subequations}\label{eq:hypo:t:two:unpaired:diff_n_diff_var}
\begin{align}
t &= \frac{\expval{x_{1}} - \expval{x_{2}}}{\sqrt{\frac{s^{2}_{1}}{n_{1}} + \frac{s^{2}_{2}}{n_{2}}}} \label{eq:hypo:t:two:unpaired:diff_n_diff_var:t} \\
\nu &= \frac{\left(s^{2}_{1}/n_{1} + s^{2}_{2}/n_{2}\right)^{2}}{\frac{\left(s^{2}_{1}/n_{1}\right)^{2}}{n_{1}-1} + \frac{\left(s^{2}_{2}/n_{2}\right)^{2}}{n_{2}-1}} \label{eq:hypo:t:two:unpaired:diff_n_diff_var:dof}
\end{align}
\end{subequations}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Two-Sample: Paired}
\label{hypo:t_test:two:paired}

In a two-sample paired \ttest we have two dependent samples,
such as two sets of measurements from the same $n$ individuals taken at different times.
In this case, we are testing $H_{0}$ that the difference in means of the two dependent samples is $\mu_{0}$.
Note, we can set $\mu_{0} = 0$ if we simply want to test for a statistically significant difference, and not an \apriori degree of difference.
Defining the difference between paired observations as $x_{\Delta}$, we compute the mean difference $\expval{x_{\Delta}}$ and $s_{\Delta}$ standard deviation on the sample.

The \tstat \cref{eq:hypo:t:two:paired} can be used with $\nu = n-1$ to find a \pvalue.

\begin{equation}\label{eq:hypo:t:two:paired}
t = \frac{\expval{x_{\Delta}} - \mu_{0}}{s_{\Delta} / \sqrt{n}}
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{\texorpdfstring{$\chi^{2}$-Test}{Chi-Squared Test}}
\label{hypo:chi2_test}

Pearson's \chiSqtest can be used to
statistically compare a set of observations in
$n$ variables, $x_{i}$, to prior expectations via the \chiSqdist.
The \pvalue returned by the test estimates the probability of obtaining the observations
assuming $H_{0}$, \ie the expectations, is true.
The \chiSqstat, $X^{2}$, is created with the assumption that
the data are normally distributed and independent,
which often is the case due to the CLT.
It is constructed by squaring the difference\footnote{Yates's
correction for continuity $\left(x^{\text{obs}}_{j} - x^{\text{exp}}_{j}\right)^{2} \to \left(\abs{x^{\text{obs}}_{j} - x^{\text{exp}}_{j}}-0.5\right)^{2}$ may
also be applied in some low statistics cases.} between
an expected value, $x^{\text{exp}}_{i}$, and its corresponding observation, $x^{\text{obs}}_{i}$,
and dividing by the expectation:

\begin{equation}\label{eq:hypo:chi2_statistic}
X^{2} = \sum_{i=1}^{n} \frac{\left(x^{\text{obs}}_{i} - x^{\text{exp}}_{i}\right)^{2}}{x^{\text{exp}}_{i}}\,.
\end{equation}

In the limit that each $x^{\text{obs}}_{i}$ is normally distributed and $n$ is large, $X^{2} \to \chi^{2}$.
We can then use the \chiSqdist with $\nu = n-1$ degrees of freedom to find the \pvalue as the area to the right of $X^{2}$.
An easy way to compute $X^{2}$ and the \pvalue is to use the \texttt{scipy.stats.chisquare(f\_obs, f\_exp)}
\href{https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chisquare.html}{function}.

The \chiSqtest can also be used to test of the data's independence, or homogeneity,
for $m$ samples of $n$ variables with $\nu = \left(n-1\right)\left(m-1\right)$ and\footnote{Note this is really the same as \cref{eq:hypo:chi2_statistic} if we reindex, just with a different $\nu$.}

\begin{equation}\label{eq:hypo:chi2_statistic_ind}
X^{2} = \sum_{i=1}^{n} \sum_{j=1}^{m} \frac{\left(x^{\text{obs}}_{i,j} - x^{\text{exp}}_{i,j}\right)^{2}}{x^{\text{exp}}_{i,j}}\,.
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Analysis of Variance (ANOVA)}
\label{hypo:ANOVA}

When we are tasked with testing for differences between multiple groups
we should not use multiple pairwise tests, like two-sample unpaired {\ttest}s,
as the family-wise error rate \cref{eq:hypo:alpha_fw}
will compound and become unreasonable, see \cref{hypo:bonferroni_correction} for more.
Instead when dealing with three\footnote{When there are only two groups a one-factor ANOVA is equivalent to a two-sample unpaired \ttest with $F=t^{2}$.} or more groups
the appropriate Analysis of Variance (ANOVA) method should be used.
Like pairwise tests, there are multiple versions of ANOVA available for different situations, including
factorial ANOVA for multiple independent variables with interaction terms,
repeated measures ANOVA where the same subjects tested in different situations form the different groups,
Multivariate Analysis of Variance (MANOVA) for multiple dependent variables,
and further extensions such as Analysis of Covariance (ANCOVA) which incorporate correlations with additional covariate variables.
All share similar principles and assumptions,
but we will only focus on one-factor ANOVA here.

In general, ANOVA methods work by ``partitioning the sum of squares'',
\ie dividing the variance present in the data into
``explained variance'', \ie ``between-group variability'',
and
``unexplained variance'', \ie ``within-group variability'',
components.
The ratio of these components then form a \Fstat
which can be compared against the \Fdist
with the appropriate degrees of freedom to produce a \pvalue.
The null hypothesis $H_{0}$ is typically that all of the groups come from populations which share the same mean.
Increased between-group variability, or decreased within-group variability,
will increase the \Fstat and make rejecting $H_{0}$ more probable.
One downside to ANOVA testing is that if we reject $H_{0}$
we will not know which group(s) differ from the others.
In this case additional \posthoc analysis is required,
such as Bonferroni corrected {\ttest}s or Tukey's honest significance test.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Assumptions}
\label{hypo:ANOVA:assumptions}

Mathematically ANOVA is a special case of linear regression
and shares many of the same assumptions:

\begin{enumerate}[noitemsep]
  \item The variances of the different groups are equal (homoscedasticity).
  \item The observations $y_{ij}$ within group $i$ are independent and identically distributed (\iid) normal random variables.
  \item The residuals are normally distributed (normality\footnote{The normality assumption can be bent without severe consequences, but as always review the literature for specifics first.}).
  \item The dependent variable is continuous.
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{One-Factor}
\label{hypo:ANOVA:one}

A one-factor ANOVA should be used when comparing $K$ groups,
divided by a single independent variable $x$,
across a single dependent variable $y$.
We partition the sum of squares into
mean square $MS_{\text{between-group}}$ and $MS_{\text{within-group}}$
\cref{eq:hypo:ANOVA:one:MS_between,eq:hypo:ANOVA:one:MS_within}, where
$n_{i}$ represents the number of observations in the $i$th group,
$N = \sum_{i} n_{i}$ is the total number of observations,
$y_{ij}$ is the $j$th observation for group $i$,
$\expval{Y_{i}} = \sum_{j} y_{ij} / n_{i}$ is the mean for group $i$,
and $\expval{Y} = \sum_{i}\sum_{j} y_{ij} / N$ is the overall mean across all groups.

\begin{subequations}\label{eq:hypo:ANOVA:one}
\begin{align}
MS_{\text{between-group}} &= \frac{1}{K-1} \sum_{i=1}^{K} n_{i}\left(\expval{Y_{i}} - \expval{Y}\right)^{2}, \label{eq:hypo:ANOVA:one:MS_between} \\
MS_{\text{within-group}} &= \frac{1}{N-K} \sum_{i=1}^{K} \sum_{j=1}^{n_{i}} \left( y_{ij} - \expval{Y_{i}} \right)^{2}, \label{eq:hypo:ANOVA:one:MS_within} \\
F &= \frac{MS_{\text{between-group}}}{MS_{\text{within-group}}}, \label{eq:hypo:ANOVA:one:F} \\
d_{1} &= K-1,\quad d_{2} = N-K. \label{eq:hypo:ANOVA:one:dof}
\end{align}
\end{subequations}

Note that $MS_{\text{between-group}}$ is comparing the group means to the overall mean,
while $MS_{\text{within-group}}$ is comparing all of the data points to their respective group means.
The \Fstat is then the ratio of the mean squares \cref{eq:hypo:ANOVA:one:F},
which can be used with the appropriate degrees of freedom \cref{eq:hypo:ANOVA:one:dof}
and the \Fdist to produce a \pvalue.
Depending on the \pvalue and selected $\alpha$
we can accept or reject $H_{0}$,
\ie all of the groups share one mean.
The \texttt{scipy.stats.f\_oneway} \href{https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.f_oneway.html#scipy.stats.f_oneway}{function}
performs\footnote{See
\href{https://www.analyticsvidhya.com/blog/2020/06/introduction-anova-statistics-data-science-covid-python/}{here} and
\href{https://www.reneshbedre.com/blog/anova.html}{here}
for example implementations.} the one-factor ANOVA test.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{\texorpdfstring{$F$}{F}-Test of Equality of Variances}
\label{hypo:F_test_var}

We can test $H_{0}$ that the variances of two normally distributed samples are equal with a \Ftest.
Given two samples of sizes $n_{1}$ and $n_{2}$ with sample variances $s_{2}^{2} \leq s_{1}^{2}$
we construct the \Fstat as:

\begin{equation}\label{eq:hypo:F_test_var}
F = \frac{s_{1}^{2}}{s_{2}^{2}}\,.
\end{equation}

The \Fdist with degrees of freedom $d_{1} = n_{1}-1$ and $d_{2} = n_{2}-1$
then gives an appropriate \pvalue.
Note that this \Ftest is particularly sensitive to the assumption that
the underlying samples are truly normally distributed;
not only approximately normal via the CLT.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{\texorpdfstring{$F$}{F}-Test of Lack-of-Fit Sum of Squares}
\label{hypo:F_test_fit}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Binomial Proportion Test}
\label{hypo:binomial_test}

When dealing with samples of $n$ binary events we can perform hypothesis testing
on the number of observed positive events $k$
using test statistics built on the binomial distribution.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Exact Binomial Test}
\label{hypo:binomial_test:exact}

For small $n$ it is possible to compute the \pvalue
explicitly from the binomial distribution \cref{eq:stats:binomial}.
We test $H_{0}$ that the probability of success is $\pi_{0}$
having actually observed $k$ successes, $k = n \pi$.

The \pvalue is then the sum

\begin{equation}\label{eq:hypo:binomial_test:exact}
\pvalue = \sum_{i \in \, \mathcal{I}} \binom{n}{i} \pi_{0}^{i} \left(1-\pi_{0}\right)^{n-i},
\end{equation}

\noindent where $\mathcal{I}$ depends on the type of test:

\begin{table}[H]
\centering
\begin{tabular}{l|l}
$\pi < \pi_{0}$ & $\mathcal{I} = \left\{0, 1, \ldots, k\right\}$, \\
$\pi > \pi_{0}$ & $\mathcal{I} = \left\{k, k+1, \ldots, n\right\}$, \\
$\pi \neq \pi_{0}$ & $\mathcal{I} = \left\{\forall \, i: P\left(x=i\right) \leq P\left(x = k\right)\right\}$, with binomial $P\left(x\right)$ \cref{eq:stats:binomial}.
\end{tabular}
\end{table}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{One-Sample}
\label{hypo:binomial_test:one}

For large sample sizes the binomial distribution is approximated by the normal distribution
and we can use a form of the \Ztest to produce {\pvalue}s.
We require the observations to be independent,
\ie we may only sample $< \SI{10}{\percent}$ of the parent population,
the sampling distribution of $\pi$ to be approximately normal,
and that there are $\geq 10$ successes and $\geq 10$ failures,
$n \pi_{0} \geq 10$ and $n \left(1-\pi_{0}\right) \geq 10$, \ie the success-failure condition.

The \Zscore is then:

\begin{equation}\label{eq:hypo:binomial_test:one}
Z = \frac{\pi - \pi_{0}}{\sqrt{\pi_{0}\left(1-\pi_{0}\right)/n}} = \frac{k - n \pi_{0}}{\sqrt{n\pi_{0}\left(1-\pi_{0}\right)}}.
\end{equation}

Note that the one-sample test is provided by the
\texttt{scipy.stats.binomtest} \href{https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binomtest.html}{function}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Two-Sample}
\label{hypo:binomial_test:two}

In the case of two samples, we can test $H_{0}$
that the difference\footnote{Again, we set $\pi_{\Delta} = 0$ if we want to test for any difference.} in the sample's probabilities is $\pi_{\Delta}$.
We require that the $\pi$ from the two samples are uncorrelated,
have approximately normal sampling distributions,
and that their difference $\pi_{1} - \pi_{2}$ is an unbiased estimator.

The \Zscore is then:

\begin{subequations}\label{eq:hypo:binomial_test:two}
\begin{align}
Z &= \frac{\pi_{1} - \pi_{2} - \pi_{\Delta}}{\pi_{p} \left(1-\pi_{p}\right)\left(1/n_{1} + 1/n_{2}\right)} \label{eq:hypo:binomial_test:two:Z} \\
\pi_{p} &= \frac{k_{1} + k_{2}}{n_{1} + n_{2}} \label{eq:hypo:binomial_test:two:pi_p}
\end{align}
\end{subequations}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Mann-Whitney \texorpdfstring{$U$}{U} Test}
\label{hypo:mann_whitney_U_test}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Kruskal-Wallis \texorpdfstring{$H$}{H} Test}
\label{hypo:kruskal_wallis_H_test}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Kolmogorov-Smirnov Test}
\label{hypo:KS_test}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Hypothesis Test Error Types and Power Analysis}
\label{hypo:power}

In hypothesis testing, like binary classification, we can suffer from two types of errors;
Type I or false positives, and Type II or false negatives.
The probabilities of these errors are functions of the experimental design
and are important to understand before undertaking a study.
We label $\alpha$ as the probability of rejecting a true null hypothesis, \ie a type I error,
and $\beta$ as the probability of failing to reject a false null hypothesis, \ie a type II error.
See \cref{table:CM} for a graphical representation in the context of binary classification.

$\alpha$ is the easier parameter to understand and improve,
as it is just the \pvalue threshold we select before the study.
It is typical to use $\alpha \leq \num{0.05}$.
$\beta$ depends on many factors including
$\alpha$,
the magnitude of the underlying effect,
the measurement variance,
the model being utilized,
and the sample size $n$.
Instead of using $\beta$ directly we often talk about the statistical power of a hypothesis test, $1-\beta$,
\ie the probability of correctly rejecting a false null hypothesis.
$\num{0.8} < 1-\beta$ is a commonly used target for the power.
As experimenters, we can
try to improve our methods to reduce the measurement variance,
select a more appropriate model\footnote{Parametric
models tend to have higher powers than the equivalent non-parametric model.
% https://youtu.be/diRX_NesFkA?t=558
In particular, comparing
the Mann-Whitney U and unpaired \ttest gives $\frac{\left(1-\beta\right)_{\text{MWU}}}{\left(1-\beta\right)_{t}} = \frac{3}{\pi} = \num{0.955}$,
%while comparing the Wilcoxon signed-rank and paired \ttest gives $\frac{\left(1-\beta\right)_{\text{WSR}}}{\left(1-\beta\right)_{t}} = \frac{2}{\pi} = \num{0.6437}$, % TODo add if I cover the Wilcoxon signed-rank test in the future
for large $n$.},
or begrudgingly accept some combination of a larger $\alpha$ or larger minimal detectable difference.
However, the primary lever for improving an experiment's power is by increasing $n$,
at the cost of additional time and money to complete the study.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\texorpdfstring{$Z$}{Z}-Test Power Example}
\label{hypo:power:Z_example}

Calculating $\beta$ can be challenging and is commonly done in software\footnote{The \texttt{statsmodels.stats.power}
\href{https://www.statsmodels.org/stable/stats.html\#power-and-sample-size-calculations}{module}
provides many useful functions, see \href{https://machinelearningmastery.com/statistical-power-and-power-analysis-in-python/}{here} for one example implementation.} particular to the model being utilized.
For a simple example, we can consider a \Ztest of a null hypothesis $\mu_{0}$
and compute the power of the test at a specific value of the alternative hypothesis $\mu_{a}$, with $\mu_{0} < \mu_{a}$.
For the given $\alpha$ being used we can look up the corresponding critical \Zscore, $Z_{\alpha}$.
Then assuming a standard deviation of $s_{\text{est}}$ from prior work,
and knowing $n$, we can estimate the sample mean
which would put us at the critical \Zscore, $\expval{x_{c}}$ \cref{eq:hypo:power_ex:x_c}.
We then calculate the \Zscore again, this time assuming the alternative hypothesis $\mu_{a}$ is true
and we have observed $\expval{x_{c}}$ from our sample, \ie we are right at the edge of rejecting a false null hypothesis.
This \Zscore, $Z_{a}$ \cref{eq:hypo:power_ex:Z_a}, can finally be used to find the power $1-\beta = P\left(Z_{a} < Z\right)$.

\begin{subequations}\label{eq:hypo:power_ex}
\begin{align}
Z_{\alpha} &= \frac{\expval{x_{c}} - \mu_{0}}{s_{\text{est}} / \sqrt{n}} \implies
\expval{x_{c}} = \frac{Z_{\alpha} s_{\text{est}}}{\sqrt{n}} + \mu_{0} \label{eq:hypo:power_ex:x_c} \\
Z_{a} &= \frac{\expval{x_{c}} - \mu_{a}}{s_{\text{est}} / \sqrt{n}}
= Z_{\alpha} + \sqrt{n}\,\frac{\mu_{0} - \mu_{a}}{s_{\text{est}}} \label{eq:hypo:power_ex:Z_a}
\end{align}
\end{subequations}

Note that as advertised $Z_{a}$ depends on
the choice of $\alpha$ via $Z_{\alpha}$,
the magnitude of the underlying effect $\mu_{0} - \mu_{a}$,
the measurement variance $s_{\text{est}}$,
the sample size $n$,
and is particular to this hypothesis test.
Plugging in numbers, if
$\alpha = \num{0.05} \to Z_{\alpha} = \num{1.645}$,
$\mu_{0} = \num{10}$,
$s_{\text{est}} \approx \num{2}$,
$n = \num{100}$,
and we want to find the power of the test for an alternative hypothesis of $\mu_{a} = \num{10.5}$,
we have $Z_{a} = \num{-0.8551} \to P\left(\num{-0.8551} < Z\right) = \num{0.8038}$
and thus the power is an acceptable $1-\beta = \num{0.8038} = \SI{80.38}{\percent}$.
We could use a similar line of reasoning to estimate
the $n$ necessary to obtain a desired $\alpha$ and $\beta$ before running the experiment.

% import numpy as np
% import scipy.stats
% norm = scipy.stats.norm
% Z_a = norm.ppf(1-0.05) + np.sqrt(100)*(10-10.5)/2
% print(f'Z_a = {Z_a:.4f}')
% print(f'Power = 1-beta = {1-norm.cdf(Z_a):.4f}')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Lehr's Equation for \texorpdfstring{$t$}{t}-Tests}
\label{hypo:power:lehr}

As a rough approximation for one-sided (two-sided) {\ttest}s,
Lehr argues that to have a power of $1-\beta \sim \num{0.8}$ with $\alpha = \num{0.05}$,
$n$ \cref{eq:hypo:power:lehr} should be set to 8\footnote{This
factor is $8 \approx \left(Z_{\alpha/2} + Z_{\beta}\right)^{2}$, for $1-\beta = \num{0.8}$, $\alpha = \num{0.05}$.} (16)
times the ratio of the estimated population variance, $s^{2}$,
and the desired detectable difference squared, $\Delta^{2} = \left(\mu_{1} - \mu_{2}\right)^{2}$.
Note that we can also rearrange this approximation to estimate $\Delta^{2}$ given a particular $n$.

\begin{subequations}\label{eq:hypo:power:lehr}
\begin{align}
n &\approx \hphantom{1}8 \frac{s^{2}}{\Delta^{2}}\quad \left(\text{One-Sided}\right), \label{eq:hypo:power:lehr:one} \\
n &\approx 16 \frac{s^{2}}{\Delta^{2}}\quad \left(\text{Two-Sided}\right). \label{eq:hypo:power:lehr:two}
\end{align}
\end{subequations}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Bonferroni Correction}
\label{hypo:bonferroni_correction}

When conducting multiple hypothesis tests on the same set of data
we run the risk of underreporting $\alpha$ for the whole analysis.
In particle physics this is known as the look elsewhere effect\footnote{See the discussion in Appendix C of the dissertation \cite{mepland_dissertation}.}
\cite{Demortier:2007zz,lyons2008,Gross2010,Ranucci:2012ed}.
For example, if we set $\alpha = \num{0.05}$ for any individual test
on a set of data with many features, but then run $\num{20}$
tests on it, by chance we'd expect $\approx \num{1}$ tests
to erroneously reject a true $H_{0}$.
To quantify this concept, we can construct
the family-wise $\alpha$ across all of the $N$ tests done on a dataset\footnote{There is
disagreement on the best way to treat $\alpha_{\text{FW}}$,
\eg are we even talking about the right null hypothises \cite{Perneger1236},
what if the different tests are use correlated variables -- then they are not wholly independent tests in the context of $\alpha_{\text{FW}}$.
The $\alpha_{\text{FW}}$ of \cref{eq:hypo:alpha_fw} is just one simple definition.},
$\alpha_{\text{FW}}$ \cref{eq:hypo:alpha_fw}.
In our earlier example, we would have $\alpha_{\text{FW}} = \num{0.642}$,
or a \SI{64.2}{\percent} chance of at least one test rejecting $H_{0}$ in error.

\begin{equation}\label{eq:hypo:alpha_fw}
\alpha_{\text{FW}} = 1 - \left(1 - \alpha\right)^{N}
\end{equation}

To address this issue we can apply the Bonferroni correction,
and simply divide our nominal $\alpha$ by $N$
before conducting the tests\footnote{Or equivalently multiply the observed {\pvalue}s by $N$.}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Sequential Bonferroni Correction, \texorpdfstring{\ie}{ie} Holm--Bonferroni Correction}
\label{hypo:bonferroni_correction:sequential}

We can control $\alpha_{\text{FW}}$ more efficiently in terms of the cost imposed on $\alpha$, and hence decreased power,
by using the sequential Bonferroni correction, \ie the Holm--Bonferroni correction.
As the name suggests, in the sequential correction
we iterate through the $i \in \left\{0, 1, \ldots, N-1\right\}$ tests
in order of their {\pvalue}s, from smallest to largest,
checking that $i$th test's \pvalue is $< \alpha / \left(N-i\right)$.
When we come to the first $i+1$ test with a $\pvalue \geq \alpha / \left(N-\left(i+1\right)\right)$
we stop iterating and say the previous $i$ tests reject $H_{0}$,
while the remaining $N-i$ tests fail to reject $H_{0}$.
In this way we can constrain $\alpha_{\text{FW}} \leq \alpha$,
while checking most tests against a less stringent condition
than $\alpha / N$ of the normal Bonferroni correction.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Experiment Design}
\label{hypo:experiment}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Sources of Bias}
\label{hypo:experiment:bias}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Blocking}
\label{hypo:experiment:blocking}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Interaction Effects}
\label{hypo:experiment:interaction}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{AB Testing Example}
\label{hypo:experiment:AB}
% TODO