\chapter{Almost Coloring of Graphs} \label{ch:graph-hard}

In this chapter, we describe our hardness results on almost
graph coloring (joint work with Dinur, Harsha \& Srinivasan \cite{DinurHSV2014}).
Recall the discussion
on graph coloring algorithms and hardness results from
\lref[Section]{sec:approx}. Given a $3$-colorable graph, best known algorithms
give a $n^{0.19996}$-coloring \cite{KawarabayashiT2014} and it is known that finding a $4$-coloring is
\NPHard~ \cite{GuruswamiK2004}. Assuming \UGC, Dinur, Mossel \& Regev~\cite{DinurMR2009}, showed that,
given an almost $3$-colorable graph, it is hard to find a $C$-coloring for any
constant $C$. Their exact result is as follows:

\begin{theorem}[Dinur, Mossel \& Regev \cite{DinurMR2009}]\label{thm:DMR}
There
is a reduction from \UG\ instances $G$ with $n$ vertices and label set $[R]$ to
graphs $\cal G$ of size $n3^{R}$ such that
\begin{itemize}
\item \YES: If $G$ is an
instance of \UG\ with $\OPT(G)\geq 1-\epsilon$ then there is a subgraph
of $\cal G$ with fractional size $1-\poly(\epsilon)$ that is $3$-colorable.
\item \NO: If $G$ is an instance of \UG\ with soundness $\OPT(G) \leq \delta$ then $\cal G$
does not have any independent sets of fractional size $O(1/\log (1/\delta))$.
\end{itemize}
\end{theorem}

For any constant $C$, taking $\delta$ to be a small
enough constant will ensure that the chromatic number is $\geq C$ in the NO
case. For getting hardness results with super-constant $C$, requires us to have
sub-constant $\delta$ which depends on $n$. In \UGC, the relation between the
the label size $R$ and soundness $\delta$ is not specified. But $R =
\Omega(1/\delta)$, since a random labeling to a \UG\ instance, satisfies
$O(1/R)$ fraction of the constraints. Assuming \UGC, with $R = \poly(1/\delta)$
and $\delta = 1/\poly(\log n)$, will ensure that (1) in the NO case, the
chromatic number is $\Omega(\log \log n)$ and (2) size of $\calG$ is $\poly(n)$.

Dinur \& Shinkar~\cite{DinurS2010} improved the analysis of the reduction, to
show that there are no independent sets of fractional size $O(1/\poly(\delta))$
in the NO case. Using the same assumption mentioned earlier, this implies
hardness for chromatic number $\Omega(\poly(\log n))$. However for these
results to hold, the alphabet size $R$ has to be $O(\log n)$.

In this chapter, we give a more efficient reduction, which ensures that the size
of $\calG$ remains small even when $R= 2^{2^{O(\sqrt{\log \log n})}}$. The reduction
of Dinur, Mossel \& Regev~\cite{DinurMR2009}, used the graph product described in
\lref[Section]{sec:graph-prod} as a gadget. This is the reason why the size of
$\calG$ is $n3^R$. We replace this gadget by the derandomized graph product
construction from \lref[Section]{sec:derand-graph-prod}. This ensures that the
size of $\calG$ is $n3^{\poly_\delta(\log R)}$. Hence the reduction remains
polynomial time, even when the alphabet size is much larger than $\log n$.

For getting the hardness result, we need to assume a conjecture similar to the
Unique Games Conjecture with specific parameters.

\begin{conjecture}[$(c(n),s(n),r(n))$-UG Conjecture]
 It is NP-Hard to
distinguish between unique label cover instances $(U,V,E,R,\Pi)$ on $n$ vertices
and $R=\F_3^{r(n)}$ from the following cases:
\begin{itemize}
 \item YES Case :
There is a labeling and a set $S\subseteq V$ of size $(1-c(n))|V|$ such that all
edges between vertices in $S$ are satisfied.
\item NO Case : For any labeling,
at most $s(n)$ fraction of edges are satisfied.
\end{itemize}
\end{conjecture}

Khot \& Regev~\cite{KhotR2008} proved that the Unique Games Conjecture implies
that for any constants $c, s \in (0,1/2)$ there is a constant $r$ such that
$(c,s,r)$-UG Conjecture is true. We also require that the constraints of the
Unique Games instance are full rank linear maps.

\begin{definition}[Linear constraint]
A constraint $\pi:R \rightarrow L$ is a
linear constraint of iff $R = L=\F_3^r$, and $\pi$ is a linear map of rank $r$.
\end{definition}

\begin{theorem}\label{thm:col-hard}
There is a reduction from $(c,s,r)$-Unique
Label Cover instances $G$ with $n$ vertices, label set $\F_3^r$ and linear
constraints to graphs $\cal G$ of size $n3^{r^{O(\log 1/\mu)}}$ where $\mu =
\poly(s)$ such that
\begin{itemize}
\item If $G$ belongs to the YES case of
$(c,s,r)$-UG then there is a subgraph of $\cal G$ with fractional size $1-c$
that is $3$-colorable.
\item If $G$ belongs to the NO case of $(c,s,r)$-UG
Conjecture then $\cal G$ does not have any independent sets of fractional size
$\mu$.
\end{itemize}
\end{theorem}

Due to the efficiency of reduction, we are
able to get hardness results even if the label cover instances had
super-polylogarithmic sized label sets of size at most $2^{2^{O(\sqrt{\log \log
n})}}$ while the reduction due to Dinur and Shinkar only worked if label set is
of size $O(\log^c n)$ for some constant $c$. However we get this improvement
only when soundness of the label cover $s(n) = 1/2^{O(\sqrt{\log \log n})}$.
 We remark that we can
improve the conclusion if \lref[Theorem]{thm:derand-ds} can be proved even when
$d = O(\log \log 1/\mu)$.

\begin{corollary}\label{cor:main}
Let $c,s,r$ be functions such that
$s^{-1}(n),r(n) = 2^{O(\sqrt {\log \log n})}$. Assuming $(c,s,r)$-UG Conjecture
on instances with linear constraints, given a graph on $N$ vertices which has an
induced subgraph of relative size $1-c$ that is $3$-colorable, no polynomial
time algorithm can find an independent set of size $\poly(s(N))$.
\end{corollary}

\section{Reduction}

In this section we prove \lref[Theorem]{thm:col-hard}. Let $G =(U,V,R,E,\Pi)$ be
a unique games cover instance with label set $R=\F_3^r$ and the constraints
$\pi$ are full rank linear transformations. We will construct a graph
$\mathcal{G=(V,E)}$ with $\mathcal V= V\times \Pe_{r,2d}$, where $d$ is a
parameter to be fixed later. Let $T_{r,d}$ be the operator in
\lref[Definition]{def:ops}. There is an edge in $\mathcal G$ between $(v,f)$ and
$(w,g)$ if there is a $u\in U$ such that $(u,v), (u,w) \in E$ and
$T_{r,d}(f\circ\pi^{-1}_{u,v},g \circ \pi^{-1}_{u,w}) >0$, where $\pi_{u,v}$ is
the full rank linear map that maps a label of $v$ to label of $u$.

\begin{lemma}[Completeness]\label{lem:completeness}
 If $G$ belongs to the YES
case of $(c,s,r)$-UG Conjecture then $\cal G$ has a induced subgraph of relative
size $1-c$ that is $3$-colorable.
\end{lemma}
\begin{proof}
Suppose the label
cover instance has a labeling $\ell: V \rightarrow \F_3^r$ and a set $S
\subseteq V, |S| = (1-c)|V|$, such that $\ell$ satisfies all the edges incident
on vertices in $S$. We will show that $A_v(f):=f(\ell(v))$ for $v\in V$, is a
$3$-coloring for the induced subgraph of $\cal G$ on the set $S \times
\Pe_{r,2d}$. For any $u \in U, v,w \in S$ having edges $(u,v),(u,w)\in E$,
consider the edge $((v,f),(w,g)) \in \mathcal E$. The colors given to the end
points are $f(\ell(v))$ and $g(\ell(w))$. Since
$T_{r,d}(f\circ\pi^{-1}_{u,v},g\circ \pi^{-1}_{u,w}) >0$, $$g\circ
\pi^{-1}_{u,w} = f\circ \pi^{-1}_{u,v} + a(p^2 +1) \text{ for some } p \in
\Pe^r_d, a \in \{1,2\}.$$ So $f(\ell(v))= f \circ \pi_{u,v}^{-1}(\ell(u)) \neq
g\circ \pi_{u,w}^{-1}(\ell(u)) = g (\ell(w))$.
\end{proof}

\begin{lemma}[Soundness]\label{lem:soundness}
If $G$ belongs to the NO case of
$(c,s,r)$-UG Conjecture, $\cal G$ has an independent set of relative size $\mu$
and $d= O(\log 1/\mu)$ then $ \mu \leq \poly(s(n)).$
\end{lemma}
\begin{proof}
Let $I_v:\Pe_{r,2d} \rightarrow \{0,1\}$ be the indicator function of $I$
restricted to vertices in $\mathcal V$ corresponding to $v\in V$. Let $J=\{v \in
V: \E_{f \in \Pe_{r,2d}} I_v(f) \geq \mu/2\}$. Then we have that $|J|/|V| \geq
\mu/2$. For $v\in J$, let $L(v) = \{x \in \F_3^{r}: \Inf_x^{\leq k}(I_v) >
\delta \}$ where $\delta = \poly(\mu), k = O(\log 1/\mu)$ are parameters from
\lref[Theorem]{thm:derand-ds}. Note that $|L(v)| \leq k/\delta$, since the sum
of all degree $k$ influences is at most $k$.
\begin{claim}\label{claim:sound}
Let $v,w \in J$ and $(u,v),(u,w) \in E$. Then there exists $a \in L(v), b \in
L(w)$ such that $\pi_{u,v}(a)= \pi_{u,w}(b)$.
\end{claim}
\begin{proof}
 Let
$A(f):= I_v(f\circ\pi^{-1}_{u,v})$, $B(g):= I_w(g\circ \pi^{-1}_{u,w})$. Since
$I$ is an independent set, if $(v,f\circ\pi^{-1}_{u,v}),
(w,g\circ\pi^{-1}_{u,w}) \in I$, then
$T_{r,d}(f\circ\pi^{-1}_{u,v},g\circ\pi^{-1}_{u,w}) =0$, which gives that
\begin{equation}
\langle A, T_{r,d} B\rangle = 0
\end{equation}
From
\lref[Theorem]{thm:derand-ds}, there is some $c \in \F_3^r$ such that
$\Inf^{\leq k}_c( A),\Inf^{\leq k}_c(B) > \delta$, which gives that
$\pi_{u,v}^{-1}(c) \in L(v)$ and $\pi_{u,w}^{-1}(c) \in L(w)$.
\end{proof}
Now
consider the randomized partial labeling $L'$ to $G$, where for $v \in J$,
$L'(v)$ is chosen randomly from $L(v)$ and for $u\in U$, choose a random
neighbor $w \in J$ (if it exists), a random label $a \in L(w)$ and set $L(u)=
\pi_{u,w}^{-1}(a)$. For any $v \in J$, any edge $(u,v)$, the probability of it
being satisfied by $L'$ is $\mu^2/k^2 = \poly(\mu)$, because of
\lref[Claim]{claim:sound}.

\end{proof}

\begin{proof}[Proof of Theorem \ref{thm:col-hard}]
The size of $\mathcal G$
denoted by $N$ is at most $ n 3^{r^{O(d)}}$. Substituting $r= 2^{O(\sqrt{\log
\log n})}, d = \log 1/\mu \leq O(\sqrt{\log \log n})$, we get that $N =
\poly(n)$ and hence the reduction is polynomial time.
\end{proof}


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}

\usepackage{a4wide}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\usepackage{hyperref}

\usepackage{graphicx}

\title{Heuristic Optimization: implementation exercise 2}
\author{Samuel Buchet: 000447808}
\date{May 2017}

\begin{document}

\maketitle

\section{Introduction}

Two stochastic local search algorithms are implemented for this second implementation exercise.
The first one is a Simulated Annealing (SA), which belongs to the class of simple stochastic algorithms.
The second one is a Greedy Randomised Adaptive Search Procedures (GRASP), which is a hybrid stochastic algorithm.

\section{Metaheuristics}

\subsection{Simulated annealing}

\subsubsection{Principle}

The first metaheuristic implemented is the simulated annealing algorithm.
This algorithm is made on the basis of a local search.
The neighbourhood used is the transpose one because it is the smallest one.
Thanks to this small neighbourhood, it is faster to explore most of the neighbours.
The acceptance criterion of this method is based on the metropolis condition.
If the new solution found improve the best found so far, it replaces it. Otherwise, there is a probability to accept a new solution, which is equal to $e^{ \frac{sol_{best}-sol_{actual}}{T}}$.
$sol_{best}$ is the best solution found by the algorithm so far and $sol_{current}$ is the current solution.
$T$ is a variable called temperature and it depends on three paramaters: the initial temperature ($T_0$), the length of a plateau ($N$) and the cooling rate ($\alpha$).
At the begining of the algorithm, $T$ is equal to $T_0$ and after each $N$ iterations, $T$ is updated as following: $T \leftarrow T*\alpha$.

\subsubsection{Parameter tuning}

As the acceptance criterion depends on the probability $e^{ \frac{sol_{best}-sol_{actual}}{T}}$, a new solution has more chance to be accepted if it is close to the best found so far and if the temperature is not too low. \newline

It has been found that the initial temperature should be quite high.
Otherwise, very few solutions were accepted.
As seen in class, the parameter $N$ should be proporitonal to the number of neighbours.
It has been set to twice the size of the neighbourhood ($\sim100$ for the instances of size 50, $\sim200$ for the instances of size 100).\newline

Several values have been tested for the $\alpha$ parameter.
It has been noticed that the cooling rate should be slow.
Otherwise, the temperature become low too fast and no more solutions are accepted.
The final value used is $0.998$.

\subsubsection{Addition component}

It has been noticed that even with a slow cooling rate, the temperature decreases very fast and the algorithm does not accept new solutions long before the end of the algorithm.
In addition, as the algorithm is stochastic, it follows different paths at each execution (with a different seed) and good solutions can be missed.
To improve the result, when the temperature is too low, it is re-set to the original temperature.
The limit temperature, which is equal to $100$, has been decided by testing the number of solutions accepted.

\subsection{GRASP}

\subsubsection{Principle}

Greedy Randomised Adaptive Search Procedures (GRASP) is a metaheuristic based on a constructive step and a local search step.
The algorithm builds a new solution and applies a local search to it.
This step is repeated until the termination criterion is met.\newline

The local search step is done using the algorithms from the first implementation exercise.
According to the first report, best improvement doesn't improve, so first improvement is chosen.
The insert neighbourhood is also used because it was the best one.\newline

The constructive step is based on the simplified rz heuristic.
The construction depends on a parameter $\alpha$ which represents the amount of randomness of the solution.
The first step of the algorithm remains the same: the jobs are ordered according to the weighted sum of the processing times.
After this step, the insertion position of the jobs is uniformly chosen amoung the positions whose cost is lower than or equal to $\alpha*cost_{worst} + (1-\alpha)*cost_{best}$, where $cost_{best}$ is the cost of the partial solution using the best insertion position and $cost_{worst}$ is the cost using the worst position.
If $\alpha$ is equal to $0$, the solution is equal to the heuristic solution (if no ties) and if $\alpha$ is equal to $1$, the solution is almost random.

\subsubsection{Parameter tuning}

There is only one parameter for this algorithm.
However, the tuning is complicated since the optimal $\alpha$ is not the same for all instances.
It has been observed that a low value is better.
The final value used in the algorithm is $0.4$.

\subsubsection{Addition component}

One possible improvement of GRASP is reactive GRASP.
This method consists in using multiple alphas to generate the solutions.
The alphas are selected according to a probability, which evolves in function of the solution quality obtained after the local search.
This method has been implemented but the results were worst.\newline

However, it has been noticed that like simulated annealing, the GRASP algorithm does not improve the solution long before the termination criterion is met.
In order to use the time budget more efficiently, an additional searching step has been used.
This step consists in multiple local search runs starting from the greedy randomised solution, after a perturbative step.
This method has been used to exploit the promising solutions generated by the construction step.
The process is executed if the solution selected is close enough to the best solution found so far.
The percentage of proximity to the best solution and the number of searches done have been tuned so that the algorithm generates enough greedy randomised solutions.\newline

The perturbation step has been selected from \cite{ref}.
It consists in applying random insert moves.
According to \cite{ref}, the number of moves applied should be low.
Indeed, tests have shown that the best solutions are obtained with about $5$ insert moves.

\subsection{Stoping criteria}

The stopping criteria used for the two metaheuristics are computed from results of the first implementation exercise.
The time allowed to both algorithms is equal to the mean time of the iterative improvement algorithms on instances of each size multiplied by $500$.\newline

The mean time on the instances of size $50$ is $60.17067$ and the mean time on instances of size $100$ is 281946.6.
The resulting time limits are $30085.33$ (size $50$) and $281946.6$ (size $100$).

\section{Computational results}

\subsection{Solution qualities comparison}

Each instance has been solved 5 times by each algorithm and the mean of the relative percentage deviation has been computed.
A different seed has been used for each run (see README).

\subsubsection{Means and standard deviations}

The table below contains the means and standard deviations (sd) of the relative percentage deviations (rpd) from the best known solutions for all instances of the same size.\newline

\begin{tabular} {|c|c|c|c|c|}
    \hline
    Algorithm & rpd mean (50) & rpd sd (50) & rpd mean (100) & rpd sd (100) \\
    \hline
    Simulated annealing & 0.872 & 0.362 & 1.660 & 0.536 \\
    \hline
    GRASP & 0.329 & 0.136 & 1.005 & 0.235 \\
    \hline
\end{tabular}

We can see that GRASP seems to be better than simulated annealing in terms of solution quality.
The result of this algorithm also seems to bee more stable according to the standard deviations.
We can also observe that for both algorithms, the quality is better on instances of size 50 and the results are less variable.

\subsubsection{Statistical test}

A Wilcoxon test can be executed on the relative percentage deviations on all instances of same size to confirm that there is a difference between the solution qualities reached by the algorithms.
The test performed with a significance level equal to $0.05$ returns p-values equal to $2.419448*10^{-9}$ and $1.537326*10^{-11}$.
This result shows that there is a statistical difference between the solution qualities of the two algorithms (the null hypothesis is rejected).
Then according to the means, we can conclude that the implementation of GRASP is statistically better than the one of simulated annealing.

\subsubsection{Correlation between the arpd's}

\begin{figure}
    \centering
        \includegraphics[scale=0.5]{images/correlation}
    \caption{Correlation plot between the arpd's}
    \label{fig:corr}
\end{figure}

The figure \ref{fig:corr} shows the correlation between the average relative percentage deviations of the solutions of all instances returned by the two algorithms.
The blue crosses correspond to the instances of size $50$ and the red crosses correspond to the instances of size $100$.

We can see that there is a difference between the instances of size $50$ and the instances of size $100$.
It seems that there is a correlation for instances of size $50$.
However, for instances of size $100$, no correlation is visible.\newline

A statistical test can be used to confirm the results again.
A spearman test with a significance level equal to $0.05$ gives a p-value equal to $0.003$ for instances of size $50$ and a p-value equal to 0$.21$ for instances of size $100$.
This results confirm the previous discussion, as the null hypothesis is rejected for instances of size $50$.\newline

These results show that the SA algorithm may be improved to obtain better qualities on the easiest instances (according to the arpd's of GRASP).
The warming step introduced may be inefficient and a bigger neighbourhood like the insert one could be better for this algorithm.

\subsection{Run-time distributions}

The figures \ref{fig:rt_sa} and \ref{fig:rt_grasp} show the Qualified run time distributions of the algorithms.
This plots are obtained with 25 runs of the algorithms on the first 5 instances of size 50 with a time limit equal to ten times the previous one (for size 50).
As the algorithms do not necessarily reach the best known solutions, the run time distribution has been computed for $0.1$, $0.2$ and $1.2$ percents of the best known solution.
The tests have been performed on Ubuntu 16.04 with a Intel Core i3-3110M CPU 2.40GHz. \newline

We can observe on the plots of the two algorithms that the probabilty of reaching 1.2\% of the best solution is quite high from the beginning of the algorithm.
However, the probability ton reach 0.1\% remains low at the begining of the runs.

On the both plots, the black curve corresponds to an exponential distribution.
On the GRASP plot, the distribution is $1-2^{-\frac{x}{17}}$ and on the SA plot, the distribution is $1-2^{-\frac{x}{100}}$.
On the grasp plot, wa can see that the exponential distribution does not fit completely with the qualified run time distribution, which means that the algorithm can be slightly improved with restarts.\newline

The figure \ref{fig:rt_comp} shows the comparison between the qualified run time distribution for both algorithms with a goal equal to $0.5\%$ of the best known solution.
We can observe that the GRASP run time distribution dominates the SA one.
Indeed, all the time, the probability to reach the goal is higher with GRASP.
It confirms the hypothesis that this implementation of GRASP is better than the one of Simulated Annealing.

\begin{figure}
    \centering
        \includegraphics[scale=0.5]{images/rt_sa}
    \caption{Qualified run time distribution of simulated annealing}
    \label{fig:rt_sa}
\end{figure}

\begin{figure}
    \centering
        \includegraphics[scale=0.5]{images/rt_grasp}
    \caption{Qualified run time distribution of GRASP}
    \label{fig:rt_grasp}
\end{figure}

\begin{figure}
    \centering
        \includegraphics[scale=0.5]{images/rt_comp}
    \caption{Comparison of qualified run time distributions}
    \label{fig:rt_comp}
\end{figure}

\section{Conclusion}

To conclude, the results show that the GRASP implementation is better than the SA one.
The means, the statistical tests and the comparison of the qualified run time distributions give strong evidences.\newline

However, the behaviour of the Simulated annealing is odd.
The correlation plot shows that all the instances of size 100 seem to have the same difficulty with this algorithm.
It is possible that bad choices were made during the implementation.
A possible explanation is the use of the transpose neighbourhood (it was done to keep a small neighbourhood, in order to keep a low number of iterations for each temperature value).
To improve the result, it should be interesting to test with the insert neighbourhood.
The warming step could also be inefficient in this case.\newline

To finish, it seems that GRASP can still be improved, as shown by the qualified run time distribution.

% \section{better solution found}
% 50\_20\_03 \newline
% 1 28 24 14 48 15 34 45 19 5 18 37 11 29 31 9 39 10 25 33 30 44 2 43 7 41 20 40 49 47 38 4 42 17 16 22 6 26 8 50 13 21 32 23 46 36 35 3 12 27 \newline
% 591806

\begin{thebibliography}{12}

\bibitem{ref} Q.-K. Pan, R. Ruiz.
2012.
Local search methods for the flowshop scheduling problem with flowtime minimization.
European Journal of Operational Research 222(1), 31-43.

\end{thebibliography}

\end{document}


\chapter{Number Theory}


\chapter{Full Flattening}
\label{chapter:fullflattening}
This chapter will introduce the classic, fully-flattened implementation of the model. It has been written in Futhark, a functional, high-level data-parallel language, with the main reason for this being simplicity.

The purpose of this implementation is to further underline the importance of the locality of reference vs. thread divergence trade-off, particularly when working with large data sets. In this case, thread divergence optimization is at its peak, while locality of reference is at its worst. The implementation exploits both inner and outer parallelism, as it processes all input options at the same time.

\section{Sequential Version to Futhark-basic}
As full-flattening is difficult to imagine and compose from scratch, we have started by creating a basic, one option per thread Futhark implementation, which we refer to as \textit{Futhark-basic}. Due to the functional language semantics, some of the optimizations done in \textit{CUDA-option} were not possible here. This involves particularly the memory allocations, which are handled automatically by the Futhark compiler, allowing only global memory padding. Nevertheless, sorting on either width or height can still turn to be useful in order to reduce thread-divergence overhead.

As it will be shown in chapter \ref{chapter:experimentalresults}, albeit its drawbacks, the performance of \textit{Futhark-basic} has the full potential of competing with both CUDA implementations. Despite that, its main purpose is to serve as a template for deriving a fully-flattened version, which we refer to as \textit{Futhark-flat}.

Using a functional language to represent a data-processing algorithm is rather straight-forward. Once the options are read in the sequential version, they are iterated over by using a \textit{for-loop}. In Futhark, this is done by a \textit{map} operation. Since this is the first \textit{map} encountered in the Futhark program, and it is not nested, the function being mapped will be executed in parallel over all options. Temporary arrays such as \textit{Qs}, \textit{QsCopy} and \textit{alphas} are declared with Futhark's \textit{let}-binding and every variable gets allocated in global memory. While C programs usually first allocate an array, then enter a loop to provide its initial values, in Futhark this will be done by a composition of \textit{replicate}, \textit{iota} or \textit{map}.

The rest of the code translation followed the sequential code inside the loop, by replacing C\texttt{++} code with its functional Futhark equivalent. Two optimizations have been done in order to simplify and reuse code, which included (i) moving the backward and forward helpers into reusable functions, together with the computation of \textit{jvalues} and (ii) replacing the scatter in the forward helper by a gather operation in order to optimize writes. Other more complex transformations include summing up elements in an array, done by a loop in C\texttt{++} and by a reduce operation in Futhark. However, none of these transformation can be described as non-trivial, as they are typical to functional languages. The complete code base for Futhark-basic can be found in file \textit{futhark/futhark-basic.fut} and is outlined in algorithm~\ref{alg:futhark-basic}.

\newpage
\begin{algorithm}[H]
\DontPrintSemicolon
\caption{Futhark-basic\label{alg:futhark-basic}}
\SetKwFor{For}{loop}{do}{end}
\SetKwFor{ForAll}{map}{with function:}{end}
\SetKw{KwFor}{for}
\SetKw{Map}{map}
\SetKw{Iota}{iota}
\SetKw{Reduce}{reducePlus}
\SetKw{Replicate}{replicate}
\SetKw{Returnmap}{return@map}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

\underline{function trinomialBasic}\;
\Input{options : [ \{ StrikePrice, Maturity, Length, TermUnit, TermStepCount, ReversionRate, Volatility, Type \} ],\\yields : [ \{ Price, Timestep \} ]}
\Output{Price approximations for all options}
results = \ForAll{options}{
    constants = compute constants from option\;
    Qs = \Replicate{width 0}\tcc*{Init Qs to 0}
    Qs[jmax] = 1\tcc*{Initial Q value}
    alphas = \Replicate{height 0}\tcc*{Init alphas to 0}
    alphas[0] = compute yield at constants.dt\tcc*{Initial alpha value}
    \tcc{Forward propagation}
    \For{(Qs,alphas) \KwFor i < height}{
        \tcc{$f_1, f_2, f_3$ check for out-of-bounds on j-index and set 0s}
        Qs = \Map{$f_1$ Qs}\tcc*{Pre-compute Qs}
        Qs = \Map{$f_2$ (\Iota{width})}\tcc*{Get next step with fwdHelper}
        tmpQs = \Map{$f_3$ Qs}\tcc*{Compute tmpQs for reduce}
        alpha = \Reduce{tmpQs}\tcc*{Compute next alpha}
        alphas[$\text{i}+1$] = alpha\tcc*{Set next alpha}
    }
    \tcc{Backward propagation}
    Prices = \Replicate{width 100}\tcc*{Init Prices to 100\textdollar}
    \For{(Prices) \KwFor i=(max\_height-1) $\geq$ 0}{
        \tcc{$f_4$ check for out-of-bounds on j-index and sets 0s}
        Prices = \Map{$f_4$ (\Iota{width})}\tcc*{Prev step with bkwdHelper}
    }
    \Returnmap Prices[jmax] \tcc*{Result for one option}
}

\Return results
\end{algorithm}
\newpage

\section{Futhark-basic to Futhark-flat}
Once Futhark-basic was validated against the C\texttt{++} sequential implementation and the book example, \textit{Futhark-flat} could be derived. In Futhark, all arrays are stored in (slow) global memory, which increases the time each thread needs for reading the data. The two implementations work with different levels of parallelism. \textit{Futhark-basic} uses nested parallelism, where the pricing of one option happens sequentially, but multiple options are priced simultaneously with the use of a parallel map. In \textit{Futhark-flat} many nested computations are performed in parallel, operating on enormous arrays, each containing data for all options. Helper \textit{index} and \textit{flag} arrays are used to indicate the start and end memory addresses of each option in such arrays, in order to allow multiple threads to work on the same array in parallel. The way threads are allocated to work on each array happens behind the scenes, automated by the \textit{Futhark} compiler. The transformations performed in \textit{CUDA-multi} (see chapter \ref{chapter:multoptionsperthreadblock}) and \textit{Futhark-flat} should in theory be the same, as the difference between the two only lies in the way memory is allocated, the use of in-place updates in CUDA, and the amount of options processed in parallel.

The program expects to read the dataset from input as a structure of arrays. The \textit{trinomialFlat} function is then invoked with an array of all options as input, where multiple arrays are computed from it, representing a series of \textit{OptionConstants} mentioned in algorithm~\ref{alg:sequential} in chapter~\ref{chapter:sequential}. It can be seen that memory usage increases proportionally with the number of options. Furthermore, every array created inside \textit{trinomialFlat}, such as \textit{Qs} and \textit{alphas} is also stored in global memory. In this implementation, the size of the width-dependent arrays (e.g. \textit{Qs} and \textit{QsCopy}) is the sum of all widths times the data type size (i.e. whether a float or double precision is used). Height-dependent arrays (\textit{alphas}) on the other hand are computed as the number of options times the maximum height times the data type size (Note that an optimization can be made here, to avoid the padding of height-dependent arrays). While this implementation is expected to perform fast enough on small data sets, it is also expected that the performance will significantly degrade with the increased number of options, because of the excessive amount of memory it requires.

Since \textit{trinomialFlat} works with arrays of individual properties for all options, differently from the function in \textit{Futhark-basic}, which works with individual properties for one option at a time, several flattening principles were used in order to apply the same permutations to all elements in the arrays at the same time. After inspecting \textit{Futhark-basic} code, we have extracted functions that have to be flattened in order to apply them on flat arrays.

\subsection*{Flattening transformations}
It can be seen immediately on Algorithm~\ref{alg:futhark-basic} that there is a repetitive use of operations, i.e., there are two \textit{replicates} on widths and 4 \textit{maps} on widths. This allows efficient reuse of many of the temporary arrays, used in the process of flattening, such as \textit{inds} and \textit{flags}. Furthermore, it is possible to reuse arrays between the different transformations, as e.g. \textit{replicate} and \textit{map} are both done on the widths array. The complete pseudo-code for \textit{Futhark-flat} can be found under algorithms~\ref{alg:futhark-flat}-\ref{alg:futhark-flat2} further in this section. Before the algorithm itself, we introduce the specific flattening operation we have used in order to implement it.
\\\\
The code transformation starts from a \textit{replicate} on the widths (Alg.~\ref{alg:futhark-basic} line 4). This is used to initialize the array of \textit{Qs}. As shown in the example in section \ref{chapter:section:flattening:reduce}, first step is obtaining \textit{ns} and \textit{ms}. The \textit{ns} in this case are the widths, which we can get from the option constants. We can then obtain the \textit{inds} and the \textit{size}. The \textit{inds} are computed by performing an exclusive scan. Since we needed the \textit{scanned\_lens} (an inclusive scan on widths) array further in the code, we omit the exclusive scan and instead perform a map, adding the neutral element 0 in the beginning and excluding the last element of \textit{scanned\_lens} (Alg.~\ref{alg:futhark-flat} line 7):
\begin{align}
\centered
\nonumber
\mathit{len\_inds} = &\;\mathit{map}\;(i\;\rightarrow\;\mathit{if}\;(i==0)\; \mathit{then}\;0\;\mathit{else}\;\mathit{scanned\_lens}[i-1])\\\nonumber
&(\mathit{iota}\;\mathit{numAllOptions})
\end{align}
Furthermore, we can obtain the \textit{size} from \textit{last} \textit{scanned\_lens}. The next step is obtaining a \textit{flags} array as
$\mathit{flags} = \mathit{scatter}\;(\mathit{replicate}\;w\;0)\;\mathit{len\_inds}\;\mathit{widths}$ (Alg.~\ref{alg:futhark-flat} line 8).
The array of \textit{ms} on the other hand is obtained by performing a $\mathit{replicate}\;w\;0$. Computing the \textit{vals} array is the next step of the transformation. It can be seen in \textit{Futhark-basic}, however, that the \textit{Qs} array is not only initialized, but also the $\mathit{jmax}^{\mathit{th}}$ element of it is set to 1 (Alg.~\ref{alg:futhark-basic} lines 4-5). We have decided to simplify the process by combining these two operations. We start by creating a \textit{sgm\_inds} array by (Alg.~\ref{alg:futhark-flat} line 9-10):
\begin{align}
\centered
\nonumber
\mathit{scatter}\;(\mathit{replicate}\;w\;0)\;\mathit{len\_inds}\;(\mathit{iota}\;\mathit{numAllOptions})
\end{align}
 where \textit{numAllOptions} is obtained through $\mathit{length}\;\mathit{options}$. We then perform
 \\$\mathit{sgmScanPlus}\;\mathit{flags}\;\mathit{sgm\_inds}$ which results in an array containing indexes of all options, spread across the \textit{size} of \textit{Qs}. In the next step, we create the array (Alg.~\ref{alg:futhark-flat} line 13)
\begin{align}
\centered
\nonumber
\mathit{q\_lens} = \mathit{map}(x\rightarrow x-1)\;(\mathit{sgmScanPlus}\;\mathit{flags}\;(\mathit{replicate}\;w\;1))
\end{align}
which contains segmented enumerators for each of the option widths. This array is going to be useful for getting $j-indexes$ from the \textit{Qs} arrays later on. Finally, we apply (Alg.~\ref{alg:futhark-flat} line 14)
\begin{align}
\centered
\nonumber
\mathit{Qs}=\mathit{map}\;((i,k) \rightarrow \mathit{if}\;(i==\mathit{jmaxs}[\mathit{sgm\_inds}[k]])\;\mathit{then}\;1\;\mathit{else}\;0)\;\mathit{q\_lens}\;(\mathit{iota}\;w)
\end{align}
This concludes the first replicate on widths.
\\\\
The next step is a \textit{replicate} on \textit{alphas} (Alg.~\ref{alg:futhark-basic} line 6) along the height of all trees. We have approached this transformation by determining the max height from all options, which could then be used to create an enormous array of size $\mathit{total\_len}=\mathit{numAllOptions}*\mathit{max\_height}$ (Alg.~\ref{alg:futhark-flat} line 16), where $\mathit{max\_height}$ is obtained with a custom \textit{reduce} on the heights, which finds the highest element. Once again we want to combine the initialization of \textit{alphas} to $0$ and the computation of $\mathit{alphas}[0]$ with the use of the \textit{yield curve}, however, for all options. This means that every $0^{\mathit{th}}$ element of each segment of \textit{alphas} has to be assigned with a value from the yield curve. We can do this in 1 step, but for simplicity, the pseudo-code of \textit{Futhark-flat} divides these first computations of alphas in two steps. We first $\mathit{replicate}\;\mathit{total\_len}\;0$ to initialize the array. Finally, we obtain (Alg.~\ref{alg:futhark-flat} line 21)\\
$\mathit{alphas} = \mathit{scatter}\;\mathit{alphas}\;(\mathit{map}\;(i\;\rightarrow\;i*\mathit{seq\_len})\;(\mathit{iota}\;\mathit{numAllOptions}))\;\mathit{yields}$.
\\\\
The next three operations are two maps on \textit{Qs} and one on $\mathit{iota}\;\mathit{width}$ (Alg.~\ref{alg:futhark-basic} line 11-13). As mentioned in \ref{chapter:section:flattening:map}, a nested \textit{map} is simply the same function applied to the flat array. Since we already have the flat \textit{Qs}, we can safely apply the maps (Alg.~\ref{alg:futhark-flat2} lines 25-27). Similarly, we have \textit{q\_lens}, which contains segmented enumerators of all \textit{widths}. Note that, as before, the mapping functions consist of a safe mechanism, checking if the j-value is going out of bounds. This can happen in the beginning of the tree construction, as $j_{min}$ and $j_{max}$ have not been reached yet, hence some nodes are missing, e.g. there are no nodes above node B, or below node D on fig. \ref{fig:treeconststage1}.
\\\\
A \text{reduce} on \textit{tmpQs} is done next (Alg.~\ref{alg:futhark-basic} line 14), which needs to be flattened in order to obtain the new alphas for the next steps of all options. Luckily, \textit{tmpQs} are already flat, as it is a result of a \textit{map} operation performed on \textit{Qs}. As mentioned in chapter \ref{chapter:section:flattening:reduce}, we can obtain the \textit{vals} in a \textit{sgmReduce} by using a \textit{segmented scan} (Alg.~\ref{alg:futhark-flat2} line 28):
\begin{align}
\centered
\nonumber
\mathit{alphaVals} = \mathit{sgmscanPlus}\;\mathit{flags}\;\mathit{tmpQs}
\end{align}
We remove the redundant step of computing the actual \text{reduce} result and instead write the next step of \textit{alphas} directly, with the use of the \textit{alpha\_indsp1} helper array (Alg.~\ref{alg:futhark-flat2} lines 31-34).
\begin{align}
\centered
\nonumber \mathit{alpha\_indsp1}\;&=\;\mathit{map}\;f_6\;(\mathit{iota}\;\mathit{numAllOptions})\\
\nonumber \mathit{alphas}\;&=\;\mathit{scatter}\;\mathit{alphas}\;\mathit{alpha\_indsp1}\;\mathit{alpha\_vals}
\end{align}
The backward propagation begins with the initialization of the \textit{Prices} array, which is a \textit{map} over \textit{iota w}, which is already flat, hence the map function is simply applied to all flat elements (Alg.~\ref{alg:futhark-flat2} line 38). Furthermore, we observe that Prices inside the backward propagation step are also computed with the use of a \textit{map} over \textit{iota w}, hence the same rule applies (Alg.~\ref{alg:futhark-flat2} line 41).
\\
Finally, \textit{Prices} at all \textit{jmax} must be returned. For this step, we can easily obtain \textit{inds} of all root elements in \textit{Prices} and their respective \textit{vals}. The algorithm is finalized by returning $\mathit{res} = \mathit{scatter}\;(\mathit{replicate}\;\mathit{numAllOptions}\;0)\;\mathit{inds}\;\mathit{vals}$ (Alg.~\ref{alg:futhark-flat2} line 45-49), which consists of the final price estimates for all options.

% The techniques we have used for flattening nested \textit{replicate}, \textit{map}, \textit{scan}, \textit{reduce} and \textit{write} can be seen in sections \ref{chapter:section:flattening:replicate},  \ref{chapter:section:flattening:map}, \ref{chapter:section:flattening:scan} and \ref{chapter:section:flattening:reduce} respectively. Note that since we are working on either the widths or the heights for all nested operations,

\newpage
\begin{algorithm}[H]
\DontPrintSemicolon
\caption{Futhark-flat\label{alg:futhark-flat}}
\SetKw{Map}{map}
\SetKw{ReduceNormal}{reduce}
\SetKw{Reduce}{reducePlus}
\SetKw{Replicate}{replicate}
\SetKw{Scatter}{scatter}
\SetKw{Scan}{scanPlus}
\SetKw{Sgmscan}{sgmscanPlus}
\SetKw{Last}{last}
\SetKw{Length}{length}
\SetKw{Iota}{iota}
\SetKw{KwFor}{for}
\SetKw{Unzip}{unzip}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

\underline{function trinomialFlat}\;
\Input{options : [ \{ StrikePrice, Maturity, Length, TermUnit, TermStepCount, ReversionRate, Volatility, Type \} ],\\yields : [ \{ Price, Timestep \} ]}
\Output{Price approximations for all options}
\tcc*{Get option constants}
(widths, heights, constants) = \Unzip{(\Map{$f_1$ options})}\;
numAllOptions = \Length{options}\tcc*{Get length of options}
scanned\_lens = \Scan{widths}\tcc*{Scan widths}
w = \Last{scanned\_lens}\tcc*{Total size of all width arrays}
len\_inds = \Map{(i $\rightarrow$ if (i == 0) then 0 else scanned\_lens[$i-\mathit{1}$]) (\Iota{ numAllOptions})}\tcc*{Get width indexes}
flags = \Scatter{(\Replicate{w 0}) len\_inds widths}\tcc*{Get width flags}
sgm\_inds = \Scatter{(\Replicate{w 0}) len\_inds (\Iota{ numAllOptions})}\;
sgm\_inds = \Sgmscan{flags sgm\_inds}\tcc*{Get segm. width inds}
\;
\tcc{Get $j_{\mathit{min}}$ to $j_{\mathit{max}}$ range values for all options, represented in the range from $0$ to $2*j_{\mathit{max}}+1$}
q\_lens = \Map{($text{x} \rightarrow x-1$) (\Sgmscan{flags (\Replicate{w 1})})}\;
Qs = \Map{((i,k) $\rightarrow$ if (i == jmaxs[sgm\_inds[k]])\text{ then one else zero}) q\_lens (\Iota{w})}\tcc*{Initialize Qs}
\tcc{Get max height}
max\_height = \ReduceNormal{((x,y) $\rightarrow$ if (x > y) then x else y)) 0 heights}\;
seq\_len = max\_height + 1\tcc*{Compute length of max tree height}
total\_len = numAllOptions * seq\_len\tcc*{Copmute alphas length}
alphas = \Replicate{total\_len 0}\tcc*{Init alphas array with 0s}
\tcc{Init alphas array with initial alpha values on starting indexes for all options}
alphas = \Scatter{alphas (\Map{($i \rightarrow i*\mathit{seq\_len}$) (\Iota{numAllOptions})}) yields}
\end{algorithm}

\newpage
\begin{algorithm}[H]
\DontPrintSemicolon
\caption{Futhark-flat - cont. 2\label{alg:futhark-flat2}}
\SetKw{Map}{map}
\SetKw{ReduceNormal}{reduce}
\SetKw{Reduce}{reducePlus}
\SetKw{Replicate}{replicate}
\SetKw{Scatter}{scatter}
\SetKw{Scan}{scanPlus}
\SetKw{Sgmscan}{sgmscanPlus}
\SetKw{Last}{last}
\SetKw{Unzip}{unzip}
\SetKw{Length}{length}
\SetKw{Iota}{iota}
\SetKw{KwFor}{for}
\setcounter{AlgoLine}{21}

\tcc{Forward propagation}
\For{(Qs, alphas) \KwFor i < max\_height}{
    \tcc{$f_2,f_3,f_4$ check for out-of-bounds on j-index and set 0s}
    Qs = \Map{$f_2$ (Qs) (q\_lens) (\Iota{w})}\tcc*{Precompute Qexp on Qs array (uses sgm\_inds, seq\_len, alphas, other constants)}
    Qs = \Map{$f_3$ q\_lens (\Iota{w})}\tcc*{Compute Qs in the next step}
    tmpQs = \Map{$f_4$ q\_lens (\Iota{w})}\tcc*{Compute tmpQs for reduce}
    alpha\_vals = \Sgmscan{flags tmpQs}\;
    \;
    \tcc{$f_5,f_6$ check for out-of-bounds on i-index and set 0s}
    alpha\_vals = \Map{$f_5$ (\Iota{numAllOptions})}\tcc*{Compute alphas}
    alpha\_indsp1 = \Map{$f_6$ (\Iota{numAllOptions})}\;
    \tcc{Update alphas at next step}
    alphas = \Scatter{alphas alpha\_indsp1 alpha\_vals}\;
}\;
\tcc{Backward propagation}
Prices = \Map{$f_7$ (\Iota{w})}\tcc*{Init Prices to 100\textdollar}
\For{(Prices) \KwFor $i=(\mathit{max\_height}-1)$ $\geq 0$}{
    \tcc{$f_8$ check for out-of-bounds on j-index and sets 0s}
    Prices = \Map{$f_8$ q\_lens (\Iota{w})}\tcc*{Compute Price at prev step}
}\;
\tcc{Get root inds and Prices}
(inds, vals) = \Unzip (\Map{$f_9$ (iota numAllOptions)})\;
\tcc{Scatter prices for all options}
Prices = \Scatter{(\Replicate{numAllOptions 0}) inds vals}\;
\;
\Return{Prices} \tcc*{Return results for all options}
\end{algorithm}

\section{Validation}
% validation on futhark-bench on the book example
To validate the correctness of \textit{Futhark-flat}, we have used \textit{futhark-bench}, a built-in tool, which is the recommended way to benchmark Futhark programs. The code is compiled using the specified compiler and ran a specified number of times for each test case. The output is validated against the output files in the \textit{out} folder, previously created by running the Sequential C\texttt{++} implementation, described in chapter \ref{chapter:sequential}. The average runtime is also printed to the standard output. \textit{Futhark-flat} has been successfully validated on all input data sets.

\section{Comparison with CUDA-multi}
\label{chapter:section:multiflatdifference}
The core differences between \textit{CUDA-multi} and \textit{Futhark-flat} are (i) the number of options that can be priced in parallel and (ii) the arrangement of memory. While CUDA provides the concept of \textit{thread blocks}, where all threads in a single block are run on the same multiprocessor (hence allowing the use of \textit{shared thread memory} and \textit{register memory} for faster data access), Futhark operates on a larger granularity, thus is only able to operate with the much slower global memory. This difference makes it possible to derive a multiple options per thread block in CUDA, where a chunk of options can be priced in parallel, but not in Futhark. Despite that, both implementations operate on a flat list of options. Whether this list comprises of the number of options whose widths can fit in a CUDA block (1024), or of all options that were inputted, the flattening transformations of both versions remain semantically the same.

When comparing the kernel function from \textit{CUDA-multi} with the \textit{trinomialFlat} function in \textit{Futhark-flat}, the first noticeable difference is the computation of option constants. While Futhark-flat computes constants for all options in one map operation and stores them in separate arrays in global memory, \textit{CUDA-multi} computes constants only for options in the current thread block (as intended) and stores them in fast thread registers.

In CUDA's case, option constants could also be stored in shared memory to ease the register pressure in order to fit more blocks on SMs, but that would mean each memory access is always slower. On the other hand, we can also enforce a limit on the number of registers, but it might result in register spilling to slow global memory. However, spilled registers can still get cached in L1 cache which has the same speed as shared memory. We tried to experiment with putting the constants into shared memory and it indeed eased register pressure, but it also noticeably hurt the performance. Possible optimization in \textit{CUDA-multi} could be some combination of shared memory for rarely accessed constants and registers for frequently accessed ones.

Another similar difference is in forward propagation, where \textit{CUDA-multi} stores temporary values \textit{tmpQs} in thread registers which results in much faster access.

\section*{Summary}
This chapter has provided an overview of our fully flattened parallel implementation using Futhark. It has started by introducing \textit{Futhark-basic}, a one-option per thread implementation in Futhark, created and used as a template to derive \textit{Futhark-flat}, together with the flattening transformations applied and the method we have used to validate its correctness. At the end, it compared flattening implementation in \textit{Futhark-flat} with the one in \textit{CUDA-multi}. This concludes the last two algorithm implementations and leads to the methodology and experiments performed in order to determine the pros and cons of each version and more importantly their performance.


\section{Importance Functions for Variance Reduction}
\label{sec:Importance}

The effective use of variance reduction techniques can lead to a faster time to
a desired solution and a reduced variance in the specified tally. However,
specifying variance reduction parameters is not always a straightforward
procedure. In simple geometries, a user might intuitively understand which
regions of a problem may contribute more to a desired solution, but for more
complex geometries, this may not be so obvious.
In the following subsections, the theory in determining which regions of a
problem are important to eliciting a tally response will be described.
The first topic discussed will be
the concept of importance and obtaining a measure of importance with
Monte Carlo sampling.
Second, the adjoint equation and its relation to importance will be introduced.
Last, the contributon solution and how its relation to tally responses is
reviewed.

\subsection{The Concept of Importance}
\label{sec:Importance}

The concept of importance is, in essence, a means of defining which regions of
a problem that are likely to contribute to a
response and which are less likely to contribute to a response. The regions
that are more likely to generate a response will have a
higher importance than those that do not.
If an importance
function for a system can be obtained computationally, that importance
function can be
strategically used in variance reduction techniques to speed up the Monte Carlo
calculations.

As described in Section \ref{subsec:AutomatedMCVR}, Booth
\cite{booth_automatic_1982} proposed
a method to quantify a cell's importance within a Monte Carlo simulation
(Eq. \eqref{eq:BoothImp}). In
this method, Booth suggested estimating the cell's importance using Monte Carlo
transport as:
\begin{equation*}
  Importance  = \frac{\text{score of particles leaving the cell}}
                     {\text{weight leaving the cell}}.
\end{equation*}
This particular calculation of importance
follows from the intuitive explanation for importance in the preceding
paragraph. Recall from Section \ref{subsec:MCVR} that in variance reduction
methods, the population of particles is increased in important regions such that
the number of samples or particles contributing to a tally increases, but the
total problem weight is conserved. More important regions should have many
lower-weight particles to reduce the tally variance. Using Booth's bookkeeping
method for estimating regional importance,
if a cell has a greater weight leaving the cell than the number
of particles, that means that the relative contribution of that cell to the
tally is likely to be lower than other regions. If, instead, the number of
particles leaving the cell is greater than the weight leaving the cell, then
that region is more important to the tally response, because that particle
population is higher than other cells.

While this
estimation of the importance requires only a Monte Carlo forward calculation of
the problem, it was referred to as the forward-adjoint importance generator
\cite{booth_automatic_1982, booth_deep_1982, booth_importance_1984} because the
bookeeping tracked by Eq. \eqref{eq:BoothImp} was a forward-approximation of the
adjoint. Adjoint theory and how it relates to importance will be discussed in
Section
\ref{sec:AdjointImportance}.
Booth's estimation of importance was used to generate
weight window target values inversely proportional to the importance.
In this case, the track weight times the expected score is approximately
constant in the problem. Choosing this inverse relationship between the weight
window and importance is common practice in variance reduction, and is often a
good choice because it is nearly optimal over a broad range of a problem
phase-space \cite{booth_common_2012}.

It should be noted that Booth's method is reliant on the statistical
precision of the cells sampled to generate their importances. For
deep-penetration problems, obtaining a ``good'' estimate of the cell importances
many mean free paths from the forward source takes several iterations. With
large fluctuations between iterations, this has the potential to
be a very slow and
computationally inefficient way to calculate importance in a problem. Using a
solution of the adjoint that is equally valid across all of the problem space
is more ideal for deep-penetration problems.

\subsection{The Adjoint Solution for Importance}
\label{sec:AdjointImportance}

Using the solution of the adjoint formulation of the neutron transport
equation is one of the most widely recognized methods for generating
an importance function. This subsection will begin with a brief summary of
adjoint theory. A discussion on how
the adjoint solution differs physically from the forward solution for a
particular problem follows. Last, some early investigations on how the
adjoint
and importance are related are summarized.

\subsubsection{Theory}

In previous sections we have reviewed the statistical precision of Monte
Carlo-based methods, and how sampled results in Monte Carlo can be obtained
in less time
with variance reduction methods. We have also briefly addressed the forward and
the adjoint solutions for a particular problem. In neutron transport, the
integral form of the forward, steady-state, particle transport equation can be
defined as:
\begin{multline}
\hat\Omega \cdot \nabla \psi
        (\vec {r} ,E,\:\hat\Omega)+\Sigma _{ t }
        (\vec{r},E)\psi (\vec { r } ,E,\:\hat\Omega) = \\
        \int _{ 4\pi  } \int _{ 0 }^{ \infty  } \Sigma _{ s }(E'\rightarrow E,
        \hat\Omega'\rightarrow\hat\Omega)\psi (\vec { r } ,E',\: \hat\Omega')dE'
        \:d\hat\Omega' + q_{e}(\vec { r } ,E, \:\hat\Omega),
\label{eq:F-NTE}
\end{multline}
where $\vec { r }$, $E$, and $\hat\Omega$, are direction, energy, and angle,
respectively, giving six dimensions of phase-space in total.
$\psi$ is the neutron flux, $\Sigma$ is the neutron
interaction (scattering, absorption, total) cross section, and $q_{e}$ is the
external fixed source. Alternatively, this can be written in operator form,
\begin{equation}
  H \psi = q_{e} \:,
\label{eq:F-NTE2}
\end{equation}
where $H$ represents the streaming, scattering, and absorptive terms from Eq.
\eqref{eq:F-NTE}, $\psi$ is the angular flux as it is in Eq. \eqref{eq:F-NTE}, and
$q_{e}$ is the source term.

The forward transport equation tells us where particles are moving
throughout the system. Of note: the
particles move in the scattering term from $E'$ into $E$, and from $\hat\Omega'$
into $\hat\Omega$. Therefore, for a particular problem with a given $q_{e}$,
particles start at $q_e$ and move throughout the system,
either downscattering in energy, streaming out of the problem,
absorbed by the problem materials, or
induce a response at the tally location.

The adjoint equation of the same form, in contrast, can be expressed as:
\begin{multline}
-\hat\Omega \cdot \nabla \psi^{\dagger}
        (\vec {r} ,E,\:\hat\Omega)+\Sigma _{ t }
        (\vec{r},E)\psi^{\dagger}  (\vec { r } ,E,\:\hat\Omega)
       = \\
        \int _{ 4\pi  } \int _{ 0 }^{ \infty  } \Sigma _{ s }(E\rightarrow E',
        \hat\Omega\rightarrow\hat\Omega')\psi^{\dagger}  (\vec { r } ,E',\:
        \hat\Omega')dE' \:d\hat\Omega' + q_{e}^\dagger(\vec { r } ,E,
        \:\hat\Omega),
\label{eq:A-NTE}
\end{multline}
or in operator form as
\begin{equation}
  H^{\dagger} \psi^{\dagger} = q_{e}^{\dagger},
\label{eq:A-NTE2}
\end{equation}
where the variables with $\dagger$ signify the adjoint-specific variables for
the problem: the adjoint flux $\psi^{\dagger}$ and the adjoint source
$q_{e}^{\dagger}$.
Note here that the particles in the adjoint equation move from $E$ into $E'$, and
from $\hat\Omega$ into $\hat\Omega'$, which indicates an upscattering in energy
and a reversal of direction when compared to the forward problem.
The external source, too, is different, changing
from $q_{e}$ to $q_{e}^\dagger$.

To solve the adjoint problem the adjoint source, $q_{e}^{\dagger}$,
can be chosen such that it has the potential to reveal information about the
forward problem. In MC variance reduction, we seek to obtain
information on the
detector or tally response for the system.
The response for the forward problem
given a defined source distribution  $q(\vec{r}, E, \hat{\Omega})$ is
\begin{subequations}
\begin{equation}
  R_{tally} = \int_{4\pi} \int_{V} \int_{E} \psi(\vec{r}, E, \hat{\Omega})
  \Sigma_{tally}(\vec{r},E, \hat\Omega) dE dV d\hat\Omega ,
\end{equation}
where $dE$ $dV$ and $d\Omega$ are the differential spaces of energy, volume, and
angle in the tally region.
This can be simplified using bracket notation, where the brackets indicate an
integration over all phase-space,
\begin{equation}
  R_{tally} = \langle \psi \Sigma_{tally} \rangle .
\end{equation}
\end{subequations}
$\psi$ is the angular flux and $\Sigma_{tally}$ is the effective tally
response function.

For a simple source-detector problem, we choose
$q_{e}^{\dagger}$ to be $\Sigma_{tally}$; or the adjoint source is the
tally/detector response function
for the system. Therefore, the adjoint particles start at low energy at the detector
location, move away from the adjoint source (the detector location), and scatter
up in energy. By making the choice that $q_{e}^{\dagger} = \Sigma_{tally}$, the
response function can be written as a product for the forward flux and the
adjoint source
\begin{equation}
  R_{tally} = \langle \psi q^{\dagger} \rangle .
  \label{eq:response1}
\end{equation}
By using the adjoint identity and the same operators $H$ from Eqs. \eqref{eq:F-NTE2}
and \eqref{eq:A-NTE2}
\begin{equation}
  \langle \psi, H^{\dagger} \psi^{\dagger} \rangle =
  \langle \psi^{\dagger}, H \psi \rangle .
\end{equation}
Eq. \eqref{eq:response1} can be written as a function of the
adjoint flux and the forward source distribution
\begin{equation}
  R = \langle \psi^{\dagger} q \rangle .
  \label{eq:response2}
\end{equation}

At this point, we know that the solution to the adjoint problem
transports particles from the adjoint source (which is the detector or tally
location) into the problem phase-space. The adjoint particles are
upscattered in energy and are transported in $-\Omega$ relative to the forward
problem. However, it may not be immediately obvious how this adjoint solution
relates to importance for the forward solution. Let us start with a simple
illustrative example: a monoenergetic, monodirectional, point source. The
forward source takes the form of a delta function:
\begin{equation*}
  q(\vec{r}, E, \hat{\Omega}) = \delta(\vec{r}-\vec{r}_0) \delta(E-E_0)
  \delta(\hat{\Omega}-\hat{\Omega}_0) .
\end{equation*}
Using this definition of the forward source and evaluating Eq.
\eqref{eq:response2}, we obtain
\begin{equation*}
  \begin{split}
    R &= \langle \psi^{\dagger} q \rangle \\
    &= \int_{V} \int_{E} \int_{\Omega} \psi^{\dagger}(\vec{r}, E, \hat{\Omega})
       q(\vec{r}, E, \hat{\Omega}) d\hat\Omega dE dV \\
       & = \psi^{\dagger}(\vec{r}_0, E_0, \hat{\Omega}_0).
\end{split}
\end{equation*}

This result shows that the solution to the adjoint equation is the detector
response for the forward problem. As a result, the adjoint flux can be used as
an indicator of a particle produced in $\vec{r}, E, \hat{\Omega}$ contributing
to a response in the system. This indicator can be thought of as the particle's
importance to achieving the tally or response objective. Consequently, it is
often said that the adjoint is the importance function for the problem.

The adjoint solution is used in nuclear engineering for a number of
applications, including reactor physics and perturbation theory
\cite{lewins_importance_1965, lewins_developments_1968,
greenspan_developments_1976, lux_monte_1991}.
However, Goertzel and Kalos' early work recognized its
application for deep-penetration radiation shielding.
Goertzel and Kalos \cite{goertzel_monte_1958}
showed analytically
that the exact adjoint solution, if used as an importance or weighting
function for the forward Monte Carlo calculation, will result in a zero variance
solution for the forward Monte Carlo problem. Further, Kalos
\cite{kalos_importance_1963} showed in a 1D infinite hydrogen slab problem that
an analytically-derived adjoint importance function significantly improved the
speed to convergence for neutron
transport in deep-penetration problems.

Goertzel and Kalos' finding that an exact adjoint can lead to a zero variance
solution means that if a single particle is transported with the adjoint
weighting function, its score will be the same as the total system response.
Only a single particle is required to get an exact solution for the forward
problem. This is prohibitive because obtaining
an exact adjoint solution is just as computationally expensive as getting
an exact forward solution. Instead, one seeks to obtain a good, but fairly
inexpensive, estimate of the
adjoint solution based on computational limitations. A good importance estimate
should help reduce the variance in a reasonable amount of time and be
relatively computationally inexpensive. A Monte Carlo solution can
provide a continuous solution over the problem phase-space. However, as
discussed in Section \ref{subsec:MCVR}, the quality of this adjoint solution
relies on the number of samples used to calculate it and that may take a
significant amount of time. A deterministic solution
has the potential to offer equal or better solution confidence across the
entire problem. However, it is discretized in space, energy, and angle.
For deep-penetration importance functions, we
opt for deterministically-obtained solutions due to the solution's equally
distributed validity.

\subsubsection{Implementation}

Coveyou, Cain, and Yost \cite{coveyou_adjoint_1967} expanded on Goertzel and
Kalos' work by interpreting in which ways the adjoint solution could be adapted
for Monte Carlo variance reduction. In particular, they investigated the choice
of biasing schemes and how effective they were at variance reduction for a
simple one-dimensional problem. They reiterated that the adjoint solution is a
good estimate for importance, but should not be calculated explicitly, and
rather estimated by a simpler model. The adjoint function is not necessarily the
most optimal importance function; however, it is likely the closest and most
obtainable estimate of importance that can be calculated
\cite{coveyou_adjoint_1967}. They
concluded that source biasing by the solution to the adjoint equation or by the
expected response is the best choice for Monte Carlo variance reduction, as it
can be used independently from any other variance reduction technique, and
provides good results.

Tang and Hoffman \cite{tang_monte_1988} built off of the parameters derived by
Coveyou et al.\ \cite{coveyou_adjoint_1967} to generate variance reduction
parameters automatically for fuel cask dose rate analyses. In their work, Tang
and Hoffman used the 1D discrete ordinates code XSDRNPM-S to
calculate the adjoint fluxes for their shielding problems. The results from this
calculation were then used to generate biasing parameters for Monte Carlo;
specifically, they aimed at generating parameters for energy biasing, source
biasing, Russian roulette and splitting, and next event estimation
probabilities. They implemented their work in the SAS4 module in SCALE
\cite{SCALE6_1}; it was
one of the earlier implementations of a fully-automated deterministic
biasing procedure for Monte Carlo.

\subsection{The Contributon Solution for Importance}
\label{sec:ContributonImportance}

Contributon theory is another useful concept that can be used as a measure of
importance
\cite{williams_generalized_1991,williams_contributorn_1992,williams_contributon_study}.
However, contributon theory quantifies importance differently than
adjoint theory. In contributon transport, a pseudo-particle, the
\textit{contributon}, is defined. The contributon carries response in the problem
system from the radiation source to a detector. The
total number of contributons in a system are conserved by the \textit{contributon
conservation principle}: all contributons that are emitted from the source
eventually arrive at the detector. Much of the work in this realm has been done
by Williams and collaborators
\cite{williams_generalized_1991,williams_contributorn_1992,williams_contributon_study}.

The contributon transport equation can be derived in a form analogous
to the forward (Eq. \eqref{eq:F-NTE}) and adjoint (Eq. \eqref{eq:A-NTE}) equations.
The derivation of Eq. \eqref{eq:Cont-NTE} and its corresponding variables is
available in a number of the sources referenced in this section, so we will
abstain from re-deriving it here. The angular contributon flux is defined
as the product of the forward and
adjoint angular fluxes:
\begin{equation}
\Psi (\vec {r} ,E,\:\hat\Omega) = \psi^{\dagger} (\vec {r} ,E,\:\hat\Omega)
        \psi(\vec {r} ,E,\:\hat\Omega) \:.
\label{eq.Cont-Flux}
\end{equation}
The contributon transport equation is:
\begin{multline}
\hat\Omega \cdot \nabla \Psi (\vec {r} ,E,\:\hat\Omega)
+\widetilde{\Sigma} _{ t }(\vec{r},E,\:\hat\Omega)\Psi (\vec { r } ,E,\:\hat\Omega)
     = \\
        \int _{ 4\pi  } \int _{ 0 }^{ \infty  }
        \widetilde{p}(\vec{r}, \hat\Omega'\rightarrow\hat\Omega, E'\rightarrow E)
        \widetilde{P}(\vec{r}, \hat\Omega',E')
        \widetilde{\Sigma} _{ t }(\vec{r}, E', \hat\Omega')
        \Psi (\vec { r } ,E',\: \hat\Omega')dE' \:d\hat\Omega'
        + \hat p(\vec { r } ,E, \:\hat\Omega) R .
\label{eq:Cont-NTE}
\end{multline}
The units of phase-space are the same as observed in the forward and adjoint
transport equations. The symbols decorated with tildes denote variables that are
unique to the contributon equation; $\widetilde{p}$ and $\widetilde{P}$ are both
probability functions related to scattering and $\widetilde{\Sigma}$ are
effective cross sections.
The effective cross sections are given by:
\begin{equation}
\begin{aligned}
\widetilde{\Sigma}_{t}(\vec{r}, E, \hat\Omega) &=
        \widetilde{\Sigma}_{s}(\vec{r}, E, \hat\Omega) +
        \widetilde{\Sigma}_{a}(\vec{r}, E, \hat\Omega)    \\
     &= \frac{\iint \Sigma_{s}(\vec{r},\hat\Omega''\cdot\hat\Omega,
         E\rightarrow E'') \psi^{\dagger}
         (\vec{r}, \Omega'', E'') d\Omega'' dE''}
         {\psi^{\dagger}(\vec{r}, E, \hat\Omega)}
        + \frac{Q^{\dagger}(\vec{r}, E, \hat\Omega)}
        {\psi^{\dagger}(\vec{r}, E, \hat\Omega)}.
\end{aligned}
\end{equation}
Note here that the effective scattering and absorption cross sections are
adjoint flux-dependent. Where the adjoint flux becomes small, the interaction
probabilities will become large. As a result, regions where the adjoint flux
is high interaction probabilities become low, causing fewer interactions and
more streaming. Conversely, regions with low adjoint
fluxes--like the problem boundary--will have a very high cross section, thus
encouraging particle transport back towards the adjoint source. This
increased probability of interaction in low flux regions encourages response
particle (contributon)
transport towards the detector or tally, thus contributing to a response.

The scattering probability of a contributon at position $\vec{r}$, $E'$, and
$\hat\Omega'$ is:
\begin{equation}
\widetilde{P}(\vec{r}, \hat\Omega',E') =
         \frac{\widetilde{\Sigma}_{ s }(\vec{r}, E', \hat\Omega')}
       {\widetilde{\Sigma}_{ t }(\vec{r}, E', \hat\Omega')} \:,
\end{equation}
and the probability that a contributon scattering at $\vec{r}$, $E'$,
and $\hat\Omega'$ will scatter into $d\hat\Omega$ $dE$ is
\begin{equation}
\widetilde{p}(\vec{r}, \hat\Omega'\rightarrow\hat\Omega, E' \rightarrow E) =
       \frac{\Sigma_{s}(\vec{r}, \hat\Omega'\cdot\hat\Omega, E'\rightarrow E)
       \psi^{\dagger} (\vec{r}, E, \hat\Omega)}
       {\iint \Sigma_{s}(\vec{r},\hat\Omega'\cdot\hat\Omega'',E'\rightarrow
       E'')\psi^{\dagger} (\vec{r}, E'', \hat\Omega'')d\hat\Omega'' dE''}.
\end{equation}
The distribution function governing the contributon source is
\begin{equation}
\hat p(\vec{r}, E, \hat\Omega) =
\frac{\psi^{\dagger}(\vec{r}, E, \hat\Omega) Q(\vec{r},E,\hat\Omega)}
     {\int \int \int \psi^{\dagger}(\vec{r'},E',\hat\Omega')
     Q(\vec{r'},E',\hat\Omega') d\hat\Omega' dE' dV'},
\end{equation}
note that the contributon source is actually defined in Eq.
\eqref{eq:Cont-NTE} by the product of $\hat{p}$
and $R$. $R$ is contributon production rate; it
is given by integral of the adjoint flux and the forward source
\begin{equation}
  \begin{split}
R &= \int \int \int \psi^{\dagger}(\vec{r},E,\hat\Omega)Q(\vec{r},E,\hat\Omega)
    d\hat\Omega dE dV \\
  & = \langle \psi^{\dagger}Q  \rangle
\end{split} \:,
\end{equation}
which is recognizable as the system response described
in Section \ref{sec:AdjointImportance}. It can also be shown by integrating Eq.
\eqref{eq:Cont-NTE} over all phase space and ensuring that the function
$\hat{p}$ is normalized, that
\begin{equation}
  R = \langle \widetilde{\Sigma}_a \Psi \rangle,
  \label{eq:contribprod}
\end{equation}
or the rate at which contributons die in the detector is the same as
the rate at which they are produced by the contributon source.
Knowing that $R$ is the contributon production rate, let us consider the
probability that a particle will be absorbed in the detector, or $P$, given by
\begin{equation}
  P  = \langle \Sigma_a \psi \rangle.
\end{equation}
Adding a factor of $\psi^{\dagger}/\psi^{\dagger}$ to the terms on the right hand
side, this becomes
\begin{equation}
  P = \Big \langle \frac{\Sigma_a}{\psi^{\dagger}} \psi \psi^{\dagger} \Big
  \rangle.
\end{equation}
By using the identities from the contributon equation, this is also
\begin{equation}
  P = \langle \widetilde{\Sigma_a} \Psi \rangle .
\end{equation}
Next, substituting the definition from Eq. \eqref{eq:contribprod} into this
equation, it follows that
\begin{equation}
  P = R.
  \label{eq:PR}
\end{equation}
This is the same
\textit{contributon conservation principle} introduced at the beginning of this
section.
Williams noted that one could
go so far as to transport contributons rather than real particles with Monte Carlo.
Because every particle transported would eventually reach the detector and give
an exact value for R (as shown by Eq. \eqref{eq:PR}), this
would lead to a zero variance solution. However, the nature of solving the
contributon equation with Monte Carlo (or any other transport mechanism)
involves knowing the exact solution to the adjoint equation, and so relies on
the same computational obstacles as solving the adjoint transport equation.

As mentioned in the previous section, the adjoint flux is an indicator of a
particle's importance to inducing a response. Conversely, the contributon
flux describes the importance of a particle to the solution.
Becker's thesis \cite{becker_hybrid_2009} aptly points out that this is
illustrated most dramatically in a source-detector problem, where the forward
source has little importance to the adjoint source, but does have importance to
the problem solution. As a result, both the contributon solution and the adjoint
solution can be considered importance functions for a problem, but the
importance that they describe differs.

Williams recognized the applications of contributons to
shield design and optimization in an extension of contribution theory called
spatial channel theory. In particular, Williams noted that variables
relevant to contributon response were useful in determining transport paths
through media \cite{williams_contributon_study, williams_SCC_shielding}. A study
of different contributon values throughout the system could enlighten users on
regions with higher response potential. This could then be used to intelligently
choose regions for detector locations or add to shielding.
The contributon values in this theory include the contributon flux, the
contributon density, the contributon current, or the contributon velocity
\cite{williams_relations_1977}.
In this way, the
user could find the particles most influential to the response of the system. A
region with high response potential is the most important to a detector tally. The
variables of response described by Williams are the response potential, the
response current, and the response vorticity \cite{williams_contributorn_1992}.

Contributon theory and spatial channel theory have been applied successfully to
shielding analyses \cite{seydaliev_contributon_2008, williams_SCC_shielding} due
to their ability to show particle flow between a source and response effectively.
Williams and Engle showed that
spatial channel theory can be used in reactor shielding analyses.
In their work, they used
contributon currents to determine preferential flow paths through the Fast Flux
Test Facility (FFTF) \cite{williams_SCC_shielding}.
Seydaliev \cite{seydaliev_contributon_2008} used angle-dependent forward and
adjoint
fluxes and currents to visualize the contributon flux for
simple source-detector problems. In this work, he showed that contributon flow
in the system behaves much like a fluid between the source and detector,
following preferential flow paths more densely.
Seydaliev also observed ray effects in the
contributon flux for high energy photons, and traditional methods like using a
first collision source, did not remedy the issue. The contributon formulation of
particle transport can show important particle flow paths between a source and a
detector, but it is still not immune to computational obstacles that exist for
standard forward- and adjoint- transport.

The past few subsections have described the different means by which importance
can be defined or quantified for a problem.
As discussed in Section \ref{sec:Importance}, generating an importance function
with Monte Carlo is limited in that the quality of the importance map is only as
good as the regions that are sampled. For deep-penetration problems, it may be
prohibitively difficult to obtain adequate importance sampling with traditional
Monte Carlo methods.

Deterministically-obtained importance functions, however,
offer the benefit of a solution that is equally valid across all of the problem
solution-space. This is because the deterministic solution's precision is
limited to convergence criteria, not sampling of individual particles. Using a
deterministic solution is often faster and much less computationally-intensive
than Monte Carlo for importance quantification.
As a result, many hybrid methods opt
to use deterministically-obtained importance functions to generate variance
reduction parameters for Monte Carlo transport.


\documentclass[main.tex]{subfiles}
\begin{document}

% \subsection{Realistic interferometers}

\marginpar{Friday\\ 2020-5-8, \\ compiled \\ \today}

% \todo[inline]{About an hour of only listening}

With a FP cavity we increase the interaction time of a photon with the GW by a factor \(\mathcal{F} / 2 \pi \), yielding
%
\begin{align}
\tau_{s} = \frac{L}{c} \frac{\mathcal{F}}{\pi }
\,.
\end{align}

The cavity, in a way, acts as a low-pass filter, removing any effects which happen on timescales shorter than \(\tau_{s}\).

Ideally, we would want to make the storage time as long as possible, but still smaller than the period of the GW:
%
\begin{align}
\tau_{s} \sim \frac{1}{2 f_{GW}}
\implies
\mathcal{F} \sim \frac{\pi c }{L 2 f_{GW}} \sim \num{e3} \qty(\frac{\SI{100}{Hz}}{f_{GW}})
\,.
\end{align}

This is an intuitive way to write it, let us make it more precise: we can model power loss at a mirror \(i\) as \(r_i^2 + t_i^2 = 1 - p_i\), where \(p_i\) is a small parameter quantifying the fraction of power lost at the mirror.

We can define an \textbf{effective power loss} \(p\) by \((1-p_1 ) r_2^2 = 1 - p\).
Then, we will have
%
\begin{align}
r_1^2 = 1 - p_1 - t_1^2 < 1 - p_1 \implies r_1^2 r_2^2 < 1 - p
\,,
\end{align}
%
\todo[inline]{This whole calculation is kind of unclear\dots}
so the finesse will satisfy the inequality
%
\begin{align}
r_1 r_2 \approx 1 - \frac{\pi}{\mathcal{F}} \leq 1- \frac{p}{2}
\,.
\end{align}

Let us define the \textbf{coupling factor} \(\sigma = p \mathcal{F} / \pi \): it will be between 0 and 2, and its value will determine whether the cavity is over- or under-coupled; for \(\sigma = 1 \) it will be impedance-matched.

This is interesting since it allows us to study the response of the phase of the reflected field
%
\begin{align}
E_r = - E _{\text{in}} \frac{r_1 - r_2 e^{-ik2L}}{1 - r_1 r_2 e^{-ik2L}}
\,,
\end{align}
%
to a small perturbation \(\epsilon = 2 k_l \delta L\): its phase \(\phi = \arg E_r\) responds as
%
\begin{align}
\phi  = \pi + \arctan (\frac{\mathcal{F} \epsilon }{\pi } \frac{1}{1 - \sigma })
+ \arctan (\frac{\mathcal{F}\epsilon }{\pi })
\,,
\end{align}
%
so for \(\sigma > 1\) we have partial cancellation of the reflected field, if \(\sigma = 1\)  there is no reflected light at all, while if \(\sigma < 1 \) gets small we get ever more light but less sensitivity.

\todo[inline]{Why are we interested in the phase of the \emph{reflected} field?}

If we suppose \(r_2 = 1 \), \(r_1 \sim 1\) and \(p_1 = 0 \) we get \(p = 1 - r_2^2 = 0\), so \(\sigma = 0\), which means that the phase sensitivity is
%
\begin{align}
\pdv{\phi }{\epsilon } = \frac{2 \mathcal{F}}{\pi  }
\,,
\end{align}
%
since we have two equal arctangent terms, both of which are linear near the origin.
This is much better than what we have with a simple Michelson interferometer, \(\pdv*{\phi }{\epsilon } = 1\).

\subsubsection{GW response of the cavity in the detector frame}

Suppose we have a Michelson interferometer with a FP cavity, which is invested by a \(h_+\) polarized GW which propagates perpendicular to the interferometer plane: then, the phase response will look like
%
\begin{align}
\Delta \phi_{FP} = \Delta \phi_{x} - \Delta \phi_{y} =
2 \times 2 k_l \frac{1}{2} L h_0 \cos(\omega_{GW} t) \frac{2 \mathcal{F}}{\pi }
\,,
\end{align}
%
where the first factor of 2 is because the light goes back and forth, while the second factor of 2 is because the effects on the two arms are opposite in sign; the phase difference is computed as laser wavevector times GW length difference (which in the detector frame is due to the mirrors moving around), and finally the factor \(2 \mathcal{F} / \pi \) is the enhancement due to the FP cavity. The amplitude of this variation is then
%
\begin{align}
\abs{\Delta \phi_{FP}} = \frac{4 \mathcal{F}}{\pi } k_l L h_0
\,.
\end{align}

\subsubsection{GW response of the cavity in the TT gauge}

We treat the problem in the sidebands picture; the carrier angular frequency is \(\omega_{l}\), and we get some power in the sidebands \(\omega_{l} \pm \omega_{gw}\).

At each round-trip of the light, some of the power is moved from the carrier to the sidebands.
We express this as a vector equation: we define a vector containing the amplitudes of the fields, \(\vec{B} = (\text{carrier}, \text{upper sideband}, \text{lower sideband})\); if \(\vec{B}\) is calculated after a round trip while \(\vec{A}\) is calculated before it then we will have
%
\begin{subequations}
\begin{align}
\left[\begin{array}{c}
B_1  \\
B_2  \\
B_3
\end{array}\right]
=
\left[\begin{array}{ccc}
X_{00}  & 0 & 0 \\
X_{10}  & X_{11}  & 0 \\
X_{20}  & 0 & X_{22}
\end{array}\right]
\left[\begin{array}{c}
A_1  \\
A_2  \\
A_3
\end{array}\right]
\,.
\end{align}
\end{subequations}
%

The diagonal terms describe the in-cavity propagation of the three modes, while the \(X_{10} \) and \(X_{20} \) terms describe the GW-caused scattering of the carrier field into the sidebands.

Going through the algebra, we find the following expression:
%
\begin{align}
\abs{\Delta \phi_{x}}
&= h_0 k_{l} L \operatorname{sinc} \qty( \frac{\omega_{gw}L}{c}) \frac{r_2 \qty(1 - r_1^2 - p )}{r_2 (1 - p) - r_1 }  \frac{1}{\abs{e^{2 i \omega_{gw} \frac{L}{c}} - r_1 r_2} } \\
&= h_0 k_{l} L \operatorname{sinc} \qty( \frac{\omega_{gw}L}{c}) \frac{r_2 \qty(1 - r_1^2 - p )}{r_2 (1 - p) - r_1 }  \frac{1}{\sqrt{1 + (r_1 r_2 )^2 - 2 r_1 r_2 \cos( 2 \omega_{GW} L / c)}}
\,.
\end{align}

We can make some approximations: a typical interferometer will work with \(p \approx 0 \), \(r_2 \approx 1\), \(r_1 \sim 1\). Applying these, we can write
%
\begin{align}
\frac{r_2 \qty(1 - r_1^2 - p )}{r_2 (1 - p) - r_1 } \approx \frac{1 - r_1^2}{1 - r_1 } =  1 + r_1 \approx 2 \qty(1 + \epsilon (r1, r2, p))
\,,
\end{align}
%
where we defined the small parameter \(\epsilon \): what we are saying is that globally the term will be close to 2.

Also, our finesse is large, of the order \(\mathcal{F} L / c \sim 1 / \omega_{GW}\), so \(\omega_{GW} L / c\) is very small: therefore the value of \(\operatorname{sinc} (\omega_{GW} L / c)\) is around 1, and
%
\begin{align}
\cos(\frac{2 \omega_{GW} L}{c})\approx 1 - \frac{1}{2} \qty(\frac{2 \omega_{GW}L }{c})^2
\,.
\end{align}

With all of these approximations, we can write the phase response of teh cavity as
%
\begin{align}
\abs{\Delta \phi_{x}} &\approx 2 h_0 k_l L \frac{1 + \epsilon (r_1, r_2, p)}{1 - r_1 r_2 } \frac{1}{\sqrt{1 + \frac{r_1 r_2 }{(1 - r_1 r_2 )^2} \qty( \frac{2 \omega_{GW} L}{c})^2}}  \\
&\approx 2 h_0 k_l L \frac{\mathcal{F}}{\pi } \frac{1}{\sqrt{1 + \qty(4 \pi f_{GW} \tau_{s})^2}}
\,,
\end{align}
%
where we have used the definition of the finesse: \(\mathcal{F} = \pi \sqrt{r_1 r_2 } / (1 - r_1 r_2 )\) and the storage time \(\tau_{s} = (L/c)(\mathcal{F} / \pi )\).
\todo[inline]{The square should not include the 1! mistake in the slides.}

This is only for an arm: we recover the factor \(2\) which appears in the detector-frame expression if we consider both.

So, we can see an additional effect which was hidden in the detector frame expression: the cavity acts as a low-pass filter, whose cutoff is called the \emph{cavity pole},
%
\begin{align}
f_p = \frac{1}{4 \pi \tau_{s}} \approx \frac{c}{4 \mathcal{F}L}
\,,
\end{align}
%
using which we can write the response of the cavity as
%
\boxalign{
\begin{align}
\abs{\Delta \phi_{FP}} = h_0 \frac{4 \mathcal{F}}{\pi } k_l L \frac{1}{\sqrt{1 + \qty( \frac{f_{gw}}{f_p})^2}}
\,.
\end{align}}

This is the \textbf{transfer function} of the cavity, from the GW signal \(h\) to the phase difference! We can also express it in terms of the laser wavelength \(\lambda_{l} = 2 \pi / k_l\) as
%
\boxalign{
\begin{align}
T_{FP} (f_{GW}) = \frac{8 \mathcal{F} L}{\lambda_{l}}
\frac{1}{\sqrt{1 + (f_{GW} / f_p)^2}}
\,.
\end{align}}

This is of the order of \num{e13} radians per unit strain for typical values of the finesse \(\mathcal{F} \sim 500\), arm length \(L \sim \SI{3}{km}\) and laser wavelength \(\lambda_{l} \sim \SI{1000}{nm}\), if \(f_{GW} < f_p\).
The cavity pole is of the order \(f_p \sim \SI{50}{Hz}\).

For GW frequencies of the order of \SI{500}{Hz} the response drops to about \num{e12}.

\subsubsection{Double-recycled Fabry-Perot interferometer}

This is the setup used by actual interferometers: we have
\begin{enumerate}
    \item an input laser with a power of \SI{100}{W} and a wavelength of \(\lambda_{l} \approx \SI{1064}{nm}\);
    \item an \textbf{input mode cleaner}, which filters out the high-order modes and stabilizes the frequency;
    \item a first \textbf{power-recycling cavity}, inside which the power is of the order \SI{5}{kW}, and which increases the effective power seen by the actual FP cavities;
    \item the beamsplitter;
    \item for each arm, the \SI{3}{km}-long \textbf{FP arm cavity}; the circulating power inside which is of the order \SI{750}{kW};
    \item the output of the beamsplitter, to which is connected a \textbf{signal extraction cavity}, which resonantly enhances the GW sidebands, and a \textbf{output mode cleaner}, which rejects any high-order modes which might have been generated.
\end{enumerate}

The FP arm cavities also act as mode filters and frequency stabilizers.
The signal extraction cavity can be tuned to change the response of the interferometer.

\end{document}


\subsection{The \acrshort{nrpa} to solve stochastic problem}%
\label{sub:the_nrpa_to_solve_stochastic_problem}

Let's consider the \gls{nrpa} as described in the section~\ref{sec:the_nrpa} and apply it to stochastic problem.
Obviously, the \gls{nrpa} is not suited for this king of problem.
But we want to highlight the main issues to explain how we try to attend to them.
In the rest of this section, we will separate each issue and open the discussion around them.
We will also present different ideas that we had to fix them.

\subsubsection{A sequence of action in stochastic context?}%
\label{ssub:a_sequence_of_action_in_stochastic_context}

The first issue we pointed in section~\ref{sub:stochastic_problem_and_how_to_solve_them} is the fact that sequence are not certain anymore.
The \gls{nrpa} aims to find the best sequence possible that start from root to any terminal state.
But if a sequence may not lead to the same outcome, or worst, happen to lead to illegal actions, it is a real problem.

The concept of sequence is therefore dangerous to use in stochastic problem as they tend to not have optimal sequence or policy.

\subsubsection{Evaluation of a sequence}%
\label{ssub:evaluation_of_a_sequence}

The \gls{nrpa} evaluates its sequences by Monte-Carlo simulation in its playout (see section~\ref{sub:nrpa_playout}).
It attaches the result of the simulation which follows a policy to the sequence it evaluates.
Storing the best sequence it finds, the \gls{nrpa} tends to be stubborn.
Once it has found a good sequence, its policy will generate similar sequences.
It might never find a better sequence because it will be stuck on exploitation of the best known sequence.
This allows to perform very well in non stochastic problem as a sequence always produce the same results.

Evaluate a sequence of a stochastic problem might result in a high score just by fluke.
If it happens, then the algorithm might stuck to this sequence which is just badly evaluate because the expected reward of the sequence may be very different.

\subsubsection{Policy over action for a given state}%
\label{ssub:policy_over_action_for_a_given_state}

The basic policy of the \gls{nrpa} gives probability distribution over actions for every state.
Of course, as seen in section~\ref{sub:nrpa_a_story_of_code} it really depends on the definition given to the \textit{code}  function.
But, a stochastic problem may present states that are not often meet.
You may very well encounter states one and only time due to the low probability in the transition between states and the size of the state space.
Therefore, coding your action based on too much information from the state is a bad idea because the algorithm might not learn anything at all.

The codes must then contains global information on the current state.
Even better, they should capture the nature of the action and its consequences on any state.

\subsubsection{Summary}%
\label{ssub:summary}

The \gls{nrpa} is a very precise algorithm based on the fact that sequence can be replayed and always give the same results.
It seems to lack a layer of abstraction necessary when dealing with stochastic problem.
Indeed, these problems place the player in new state and situation they might never have encountered before.
Therefore, their knowledge must, in some way, be useful to them in any given situation.
That is a point the \gls{nrpa} misses depending on how coded the actions are.


\documentclass{article}

\title{SRS for $h_g$ and $h_c$}

\author{Spencer Smith}

\usepackage{longtable}
\usepackage{booktabs}

\newcommand{\blt}{- } %used for bullets in a list

\newcounter{datadefnum} %Datadefinition Number
\newcommand{\ddthedatadefnum}{DD\thedatadefnum}
\newcommand{\ddref}[1]{DD\ref{#1}}

\newcommand{\colAwidth}{0.2\textwidth}
\newcommand{\colBwidth}{0.73\textwidth}

\renewcommand{\arraystretch}{1.2} %so that tables with equations do not look crowded

\begin{document}

\maketitle

\section*{Table of Units}

Throughout this document SI (Syst\`{e}me International d'Unit\'{e}s) is employed
as the unit system.  In addition to the basic units, several derived units are
employed as described below.  For each unit, the symbol is given followed by a
description of the unit with the SI name in parentheses.  ~\newline

\begin{longtable}{l p{11cm}}

m & \blt for length (metre)\\
kg & \blt for mass (kilogram)\\
s & \blt for time (second)\\
K & \blt for temperature (kelvin)\\
$^oC$ & \blt for temperature (centigrade)\\
J & \blt for energy (joule, J=$\mathrm{\frac{kg m^2}{s^2}}$)\\
cal & \blt for energy (calorie, cal $\approx$ 4.2 $\mathrm{\frac{kg m^2}{s^2}}$)\\
mol& \blt for amount of substance (mole)\\
W &\blt for power (watt, W=$\mathrm{\frac{kgm^2}{s^3}}$)\\

\end{longtable}

\section*{Table of Symbols}

The table that follows summarizes the symbols used in this document along with
their units.  The choice of symbols was made with the goal of being consistent
with the nuclear physics literature and that used in the FP manual.  The SI
units are listed in brackets following the definition of the symbol.

\begin{longtable}{l p{10.5cm}}
$h_c$ & \blt convective heat transfer coefficient between clad and coolant ($\mathrm{\frac{kW}{m^2C}}$)\\
$h_g$ & \blt effective heat transfer coefficient between clad and fuel surface ($\mathrm{\frac{kW}{m^2C}}$)\\
\end{longtable}

\section{Data Definitions}

~\newline
\noindent
\begin{minipage}{\textwidth}
\begin{tabular}{p{\colAwidth} p{\colBwidth}}
\toprule
\textbf{Number} & \textbf{DD\refstepcounter{datadefnum}\thedatadefnum} \label{hg}\\
\midrule
Label & $h_g$\\
\midrule
Units & $ML^0t^{-3}T^{-1}$\\
\midrule
SI equivalent & $\mathrm{\frac{kW}{m^{2\circ} C}}$\\
\midrule
Equation & $h_g$ =$ \frac{2k_{c}h_{p}}{2k_{c}+\tau_c h_{p}}$\\
\midrule
Description & $h_g$ is the  gap conductance\newline
$\tau_c$ is the clad thickness\newline
$h_p$ is initial gap film conductance\newline
$k_c$ is the clad conductivity\newline
NOTE: Equation taken from the code\\
\midrule
 Sources & source code\\
\bottomrule
\end{tabular}
\end{minipage}\\

~\newline
\noindent
\begin{minipage}{\textwidth}
\begin{tabular}{p{\colAwidth} p{\colBwidth}}
\toprule
\textbf{Number} & \textbf{DD\refstepcounter{datadefnum}\thedatadefnum \label{hc}}\\
\midrule
Label & $h_c$\\
\midrule
Units & $ML^0t^{-3}T^{-1}$\\
\midrule
SI equivalent & $\mathrm{\frac{kW}{m^{2o}C}}$\\
\midrule
Equation & $h_c$ =$\frac{ 2k_{c}h_{b}}{2k_{c}+\tau_ch_{b}}$\\
\midrule
Description &
$h_c$ is the  effective heat transfer coefficient between the clad and the
coolant \newline
$\tau_c$ is the clad thickness\newline
$h_b$ is initial coolant film conductance\newline
$k_c$ is the clad conductivity\newline
NOTE: Equation taken from the code\\
\midrule
 Sources & source code \\
\bottomrule
\end{tabular}
\end{minipage}\\

\end{document}


\chapter{Log-Linear Models}

\begin{ex}
\end{ex}

\begin{ex}
\end{ex}

\begin{ex}
\end{ex}

\begin{ex}
\end{ex}

\begin{ex}
\end{ex}

\begin{ex}
\end{ex}


% Golden Rule One, Exercise
%
% The purpose of the exercise is to demonstrate that even small,
% apparently simple, datasets can have surprising properties. In
% particular, we demonstrate that it can be unwise to trust only
% summary statistics, without inspecting the underlying distribution
% of data

\subsection{Golden Rule 1 Exercise}
\begin{frame}
  \frametitle{Subgroups}
  \begin{itemize}
    \item You are in group A, B, C or D - this decides your dataset: \\
             \texttt{expnA.tab}, \texttt{expnB.tab}, \texttt{expnC.tab}, \texttt{expnD.tab}
    \item You will use \texttt{R} at the command-line to analyse your data
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{The biological question}
  \begin{itemize}
    \item Your dataset \texttt{expn?.tab} describes (log) expression data for two genes: \texttt{gene1} and \texttt{gene2}
    \item Expression measured at eleven time points (including control)
    \item Q: Are \texttt{gene1} and \texttt{gene2} genes coregulated?
    \item How do we answer this question?
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Reformulating the biological question}
  \begin{itemize}
    \item<1-> Q: Are \texttt{gene1} and \texttt{gene2} genes coregulated?
    \item<1-> A: We cannot determine this from expression data alone
    \item<2-> Reformulate the question:
    \item<2-> NewQ: Is there evidence that \texttt{gene1} and \texttt{gene2} expression profiles are correlated? \\
                   (is expression \texttt{gene1} $\propto$ \texttt{gene2})
    \item<2-> How do we answer this new question?
  \end{itemize}
\end{frame}

% [fragile] frames must end with \end{frame} directly following a newline, or they break!
\begin{frame}[fragile]
  \frametitle{Starting the analysis}
  \begin{itemize}
    \item Change directory to where Exercise 1 data is located, and start R.
  \end{itemize}
\begin{lstlisting}[language=bash]
$ cd ~/introToBioinformatics/data/ex1_expression/
$ R
\end{lstlisting}
\end{frame}

% [fragile] frames must end with \end{frame} directly following a newline, or they break!
\begin{frame}[fragile]
  \frametitle{Load and inspect data in R}
\begin{lstlisting}[language=R]
> data = read.table("expnA.tab", sep="\t", header=TRUE)
> head(data)
  gene1 gene2
1    10  8.04
2     8  6.95
3    13  7.58
4     9  8.81
5    11  8.33
6    14  9.96
\end{lstlisting}
\end{frame}

% [fragile] frames must end with \end{frame} directly following a newline, or they break!
\begin{frame}[fragile]
  \frametitle{Load and inspect data in R}
\begin{lstlisting}[language=R]
> mean(data$gene1)
[1] 9
> mean(data$gene2)
[1] 7.500909
> sd(data$gene1)
[1] 3.316625
> sd(data$gene2)
[1] 2.031568
> cor(data)
          gene1     gene2
gene1 1.0000000 0.8164205
gene2 0.8164205 1.0000000
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Results}
  \begin{center}
  \begin{tabular}{r|l|l|l|l}
          measure & expnA & expnB & expnC & expnD \\
	  \hline
	  mean(gene1) & 9     &  &  & \\
	  mean(gene2) & 7.5   &  &  & \\
  	  sd(gene1)   & 3.3   &  &  & \\
  	  sd(gene2)   & 2.0   &  &  & \\
	  cor(data)   & 0.816 &  &  & \\
  \end{tabular}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Results}
  \begin{center}
  \begin{tabular}{r|l|l|l|l}
	  measure & expnA & expnB & expnC & expnD \\
	  \hline
	  mean(gene1) & 9     & 9     & 9     & 9 \\
	  mean(gene2) & 7.5   & 7.5   & 7.5   & 7.5 \\
  	  sd(gene1)   & 3.3   & 3.3   & 3.3   & 3.3 \\
  	  sd(gene2)   & 2.0   & 2.0   & 2.0   & 2.0 \\
	  cor(data)   & 0.816 & 0.816 & 0.816 & 0.816 \\
  \end{tabular}
  \end{center}
  \begin{itemize}
     \item<2-> $r=0.816 (P<0.005)$ in every experiment
     \item<2-> Can we conclude that \texttt{gene1} and \texttt{gene2} are coexpressed in each experiment?
  \end{itemize}
\end{frame}

% [fragile] frames must end with \end{frame} directly following a newline, or they break!
\begin{frame}[fragile]
  \frametitle{Plot the data in R}
\begin{lstlisting}[language=R]
> plot(data)
\end{lstlisting}
  \includegraphics[width=0.4\textwidth]{images/ex1_screenshot_d}
\end{frame}

\begin{frame}
  \frametitle{Always plot the data}
    Which gene pairs are coexpressed?
  \begin{center}
    \includegraphics[width=0.9\textwidth]{images/ex1_rplot} \\
  \end{center}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Always plot the data}
    Is the matrix of (Pearson) correlation values potentially misleading?
\begin{lstlisting}[language=R]
> data = anscombe
> cor(data)[1:4,5:8]
           y1         y2         y3         y4
x1  0.8164205  0.8162365  0.8162867 -0.3140467
x2  0.8164205  0.8162365  0.8162867 -0.3140467
x3  0.8164205  0.8162365  0.8162867 -0.3140467
x4 -0.5290927 -0.7184365 -0.3446610  0.8165214
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Sometimes real correlation doesn't mean anything}
  \begin{center}
    \includegraphics[width=0.8\textwidth]{images/spurious_correlation} \\
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{If you look for anything you'll find something}
  \begin{center}
    \includegraphics[width=0.8\textwidth]{images/twitter_pca} \\
  \end{center}
\end{frame}