\documentclass[]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{amssymb}

%title material
\title{Astronomy 400B Homework \#2}
\author{Please Show Your Work for Full Credit}
\date{Due Feb 26, 2015 by 9:35am}

%include latex definitions
\input{astro400B_definitions.tex}

%begin the document
\begin{document}

%make the title, goes after document begins
\maketitle

\section{Sparke \& Gallagher Problem 2.20}

Consider the spherical density distribution $\rho_H(r)$ with
\begin{equation}
4\pi G\rho_H(r) = \frac{V_H^2}{r^2 +a_H^2},
\end{equation}
where $V_H$ and $a_H$ are constants; what is the mass $M(<r)$ contained within
a radius $r$? Use the equation
\begin{equation}
M(<R) = RV^2/G
\end{equation}
to show that the speed $V(r)$ of a circular orbit at radius $r$
is given by
\begin{equation}
V^2(r) = V_H^2[1-(a_H/r)\arctan(r/a_H)],
\end{equation}
\noindent
and sketch $V(r)$ as a function of radius. This density law is sometimes
used to represent the mass of a galaxy's dark halo -- why?

\section{Sparke \& Gallagher Problem 2.24}

We can estimate the size of an HII region around a massive star that radiates $S_\star$ photons with energy above 13.6eV each second. Assume that the
gas within radius $r_\star$ absorbs all these photons, becoming almost
completely ionized so that $n_e \approx n_H$, the density of H nuclei. In a
steady state atoms recombine as fast as they are ionized, so the star ionizes
a mass of gas $M_g$, where
\begin{equation}
S_\star = (4r_\star^3/3)n_H^2\alpha(T_e) = (M_g/m_p)n_H\alpha(T_e).
\end{equation}
Use the equation
\begin{equation}
-\frac{dn_e}{dt} = n_e^2 \alpha(T_e)~~\mathrm{with}~~\alpha(T_e) \approx 2\times 10^{-13}\left(\frac{T_e}{10^4~\K}\right)^{-3/4}~\cm^3~\s^{-1}
\end{equation}
\noindent
to show that a mid-O star radiating $S_\star=10^{49}~\s^{-1}$ into gas of
density $10^{3}~\cm^{-3}$ creates an HII region of radius $0.67~\pc$,
containing $\sim30\Msun$ of gas (assume that $T_e=10^4\K$). What
is $r_\star$ if the density is ten times larger? Show that only a tenth
as much gas is ionized. How large is the HII region around a B1
star with $n_H=10^3 \cm^{-3}$ but only $S_\star = 3\times10^{47}~\s^{-1}$?

\pagebreak

\section{Sparke \& Gallagher Problem 3.7}

The {\it Navarro-Frenk-White} (NFW) model describes the
halos of cold dark matter that form in cosmological simulations.
Show that the potential corresponding to the density
\begin{equation}
\rho_{NFW}(r) = \frac{\rho_N}{(r/a_N)(1+r/a_N)^2}~\mathrm{is}~\Phi_{NFW}(r) = -\sigma_N^2\frac{\ln(1+r/a_N)}{(r/a_N)},
\end{equation}
\noindent
where $\sigma_N^2 = 4\pi G \rho_N a_N^2$. The density rises steeply
at the center, but less so than in the singular isothermal sphere;
at large radii $\rho(r)\propto r^{-3}$. Show that the speed
$V$ of a circular orbit at radius $r$ is given
by
\begin{equation}
V^2(r) = \sigma_N^2\left[\frac{\ln(1+r/a_N)}{(r/a_N)} - \frac{1}{(1+r/a_N)}\right].
\end{equation}

\section{Sparke \& Gallagher Problem 3.12}

Show that for the Plummer sphere model with density profile
\begin{equation}
\rho_P(r) = \frac{3a_P^2}{4\pi}\frac{M}{(r^2 + a_P^2)^{5/2}}
\end{equation}
\noindent
and potential
\begin{equation}
\Phi_P(r) = -\frac{GM}{\sqrt{r^2 +a_P^2}}
\end{equation}
\noindent
the potential energy is
\begin{equation}
PE = - \frac{3\pi}{32} \frac{GM^2}{a_P}.
\end{equation}

\end{document}


\chapter{Cost-sensitive logistic regression}\label{ch:7}

\begin{remark}{Outline}
In this chapter, we propose a cost-sensitive logistic regression algorithm. The model consists in
a new logistic regression cost function, one that takes into account the real costs due to
misclassification and correct classification. First, in Section \ref{sec:7:logistic}, we give the
background behind logistic regression. Then, in Section \ref{sec:7:cslr}, we describe the
cost-sensitive logistic regression. For this, we carry a deep analysis of the
logistic regression implicit misclassification costs in Section \ref{sec:7:log_cost_analysis}. Then
in Section \ref{sec:7:cscostfunction}, we give a new version of the logistic regression cost
function. Finally, in Section \ref{sec:7:results}, we compare the results of the proposed
algorithm against state-of-the-art methods, using the five real-world cost-sensitive databases
presented in \partname{~\textsc{\ref{part:2}}}.
\end{remark}

\section{Logistic regression}
\label{sec:7:logistic}

Logistic regression is a classification model that, in the specific context of binary
classification, estimates the posterior probability of the positive class, as the logistic sigmoid
of a linear function of the feature vector \citep{Bishop2006}. The estimated probability  is
evaluated as
\begin{equation}
  \hat p_i = P(y=1 \vert \mathbf{x}_i) = h_{\theta}(\mathbf{x}_i) =
  g\bigg(\sum_{j=1}^{k}{\theta^jx_i^j}\bigg),
\end{equation}
where $h_\theta(\mathbf{x}_i)$ refers to the hypothesis of $i$ given the parameters $\theta$, the
feature vector $\mathbf{x}_i$ for example $i$ and  $g(\cdot)$ is the logistic sigmoid function
defined as
\begin{equation}
  g(z)=\frac{1}{(1+e^{-z})} .
\end{equation}
In \figurename{~\ref{fig:ch7:1}}, the logistic sigmoid function is shown.
\begin{figure}[htbp]
  \centering
  \includegraphics{ch7_fig1}
  \caption{Sigmoid function}
  \label{fig:ch7:1}
\end{figure}

The problem then becomes on finding the right parameters that minimize a given cost function.
Usually, in the case of logistic regression, the cost function $J(\theta)$ refers to the negative
logarithm of the likelihood, such that
\begin{equation}
  J(\theta)=\frac{1}{N}\sum_{i=1}^{N} J_i(\theta),
\end{equation}
where
\begin{align}\label{eq:7:lrcost}
  J_i(\theta) =  -y_i\log(h_\theta(\mathbf{x}_i)) -(1-y_i)\log(1-h_\theta(\mathbf{x}_i)).
\end{align}
Therefore, the parameters are estimated by minimizing (\ref{eq:7:lrcost})
$
  \hat \theta = \argmin_\theta J(\theta).
$

There are several methods used to estimate the logistic regression, in particular, maximum
likelihood \citep{Hastie2009}, Newton, coordinate descent \citep{Murphy2012} and dual coordinate
descent \citep{Yu2011}. Nevertheless, all these methods rely on the assumption of convexity of the
logistic regression cost function $J(\theta)$.

\section{Cost-sensitive logistic regression}
\label{sec:7:cslr}

In this section we present our cost-sensitive logistic regression algorithm.
First, we motivate the need to modify the logistic regression, as we
analyze the implicit costs that the logistic regression assigned to each misclassification error
during the estimation of the parameters $\theta$. Then, we show our proposed algorithm.

\subsection{Implicit costs of the logistic regression}
\label{sec:7:log_cost_analysis}

The logistic regression cost function, as described in (\ref{eq:7:lrcost}), implicitly assume that
false positives and false negatives have the same costs, i.e. $C_{{FP}_i} = C_{{FN}_i}$ $\forall
i \in \{1,\cdots,N\}$. This can be easily shown by analyzing the logistic cost function for both
values of $y_i$ and the algorithm prediction $h_\theta(\mathbf{x}_i))$:

\begin{itemize}
\item If $y_i=0$ and $h_\theta(\mathbf{x}_i) \approx 0$, then
\begin{align*}
 J_i(\theta) &= -y_i\log(h_\theta(\mathbf{x}_i)) -(1-y_i)\log(1-h_\theta(\mathbf{x}_i)) \nonumber \\
 &\approx -(0)\log((0)) -(1-(0))\log(1-(0)) \nonumber \\
 &\approx 0.
\end{align*}

\item If $y_i=0$ and $h_\theta(\mathbf{x}_i) \approx 1$, then
\begin{align*}
 J_i(\theta) &\approx -(0)\log((1)) -(1-(0))\log(1-(1)) \nonumber \\
 &\approx \infty.
\end{align*}

\item If $y_i=1$ and $h_\theta(\mathbf{x}_i) \approx 0$, then
\begin{align*}
 J_i(\theta) &\approx -(1)\log((0)) -(1-(1))\log(1-(0)) \nonumber \\
 &\approx \infty.
\end{align*}

\item If $y_i=1$ and $h_\theta(\mathbf{x}_i) \approx 1$, then
\begin{align*}
 J_i(\theta) &\approx -(1)\log((1)) -(1-(1))\log(1-(1)) \nonumber \\
 &\approx 0.
\end{align*}
\end{itemize}

\noindent Then, we collect the previous results in a cost matrix:
  \begin{table}[htbp]
    \centering
    \footnotesize
    \begin{tabular}{c|c|c}
      \multicolumn{3}{c}{}\\
      \multicolumn{1}{c|}{}  & Actual Positive& Actual Negative \\
      \multicolumn{1}{c|}{} & $y_i=1$& $y_i=0$ \\
      \hline
      Predicted Positive    & \multirow{ 2}{*}{$C_{{TP}_i}\approx 0$} &
      \multirow{2}{*}{$C_{{FP}_i}\approx \infty$} \\
      $c_i=1$ & &\\
      \hline
      Predicted Negative    & \multirow{ 2}{*}{$C_{{FN}_i}\approx \infty$} & \multirow{
      2}{*}{$C_{{TN}_i}\approx 0$} \\
      $c_i=0$ & &\\
    \end{tabular}
    \caption{Logistic regression cost matrix}
    \label{tab:7:1}
  \end{table}

This confirms that the logistic regression cost function implicitly assume that
false positives and false negatives have the same costs. However, as discussed in
Chapter~\ref{ch:4} and Chapter~\ref{ch:5}, this is not the case in several real-world
applications.

\subsection{Cost-sensitive logistic regression cost function}
\label{sec:7:cscostfunction}

In order to incorporate the different real costs, as showed in
\tablename{~\ref{tab:3:cost_matrix}}, into the logistic regression, we start by analyzing the
expected costs that a modified logistic regression cost function should make for each
misclassification and correct classification case.

\begin{equation*}
  J^c_i(\theta) =
  \begin{cases}
    C_{TP_i}    & \text{if} \phantom{-}  y_i = 1 \text{ and } h_\theta(\mathbf{x}_i) \approx 1  \\
    C_{TN_i}    & \text{if} \phantom{-}  y_i = 0 \text{ and } h_\theta(\mathbf{x}_i) \approx 0  \\
    C_{FP_i}    & \text{if} \phantom{-}  y_i = 0 \text{ and } h_\theta(\mathbf{x}_i) \approx 1  \\
    C_{FN_i}    & \text{if} \phantom{-}  y_i = 1 \text{ and } h_\theta(\mathbf{x}_i) \approx 0 .
  \end{cases}
\end{equation*}

Then, as we already have the real costs, we create a new cost-sensitive logistic regression cost
function, by including the different costs into the logistic function,

\begin{align}\label{eq:CSLR}
  J^c(\theta)=\frac{1}{N} \sum_{i=1}^{N} \bigg( y_i(h_\theta(\mathbf{x}_i) C_{TP_i} +
  (1-h_\theta(\mathbf{x}_i))C_{FN_i})  \nonumber\\
  +(1-y_i)(h_\theta(\mathbf{x}_i) C_{FP_i} + (1-h_\theta(\mathbf{x}_i))C_{TN_i}) \bigg).
\end{align}

Since this  new cost function is not convex, we estimate the parameters $\theta$ with genetic
algorithms, as this optimization heuristic does not require the underlying function to be
differentiable or convex \citep{Haupt2004}.

\section{Experiments}
\label{sec:7:results}

For the experiments we use five datasets from four different real world example-dependent
cost-sensitive problems: Credit card fraud detection (see Section~\ref{sec:4:fraud}), credit
scoring (see Section~\ref{sec:4:creditscoring}), churn modeling (see Section~\ref{sec:5:churn}) and
direct marketing (see Section~\ref{sec:5:directmarketing}). The different datasets are summarized
in \tablename{~\ref{tab:4:databases}} and \tablename{~\ref{tab:5:databases}}.

In particular, we are interested in comparing the results of the different logistic regression
models. First we train a logisitc regression ($LR$) using the training ($t$), under-sampled
($u$), cost-proportionate rejection-sampling  ($r$) \citep{Zadrozny2003}  and  cost-proportionate
over-sampling ($o$) \citep{Elkan2001} datasets. Afterwards,  we evaluate the results of  the
algorithms using $BMR$, see Chapter~\ref{ch:6}. Lastly, we calculate the cost-sensitive
logistic  regression ($CSLR$). We use the logistic regression implementation of
\textit{Scikit-learn} \citep{Pedregosa2011}, and the \textit{CostCla} library, see
Appendix~\ref{ch:A}, for the cost-sensitive algorithms. Unless otherwise stated, the random
selection of the training set was repeated 50 times, and in each time the models were trained and
results collected, this allows us to measure the stability of the results.

In \tablename{~\ref{tab:7:results_savings}}, we show the results of each algorithm in the different
databases measured by savings. Firstly, the proposed $CSLR$ has the highest savings in the
fraud, churn and credit 1 databases, moreover, in the credit 2 and marketing databases, it
is the second best model. It is interesting to see how different the results from a standard
logistic regression and the cost-sensitive logistic
regression are. Moreover, we also calculate the results of the $F_1Score$ for each model, as
shown in \tablename{~\ref{tab:7:results_fscore}}. It is observed that the $CSLR$ algorithm is not
the one that gives the best results measured by $F_1Score$. In fact, there is not a clear relation
between the results measured by savings or  $F_1Score$.

Finally, we compute the perBest statistic  for the savings and the $F_1Score$. This statistic is
calculated as the average result of each algorithm compared with the best in each set. The results
are shown in \figurename{~\ref{fig:7:comparison_per_best}}. On average, the $CSLR$ is the best
model, as it yield to 95.5\% of the best model in the different databases. Nevertheless, when
measured by $F_1Score$, it only yield to 74.7\% of the best model. This leads to the conclusion
of the importance of using a cost-sensitive measure such as savings when evaluating real-world
example-dependent cost-sensitive problems.

\begin{figure}
  \centering
  \includegraphics{ch7_fig3}
  \caption{\textbf{Comparison of the average savings and $F_1Score$ of the algorithms versus the
    the best model.} The models that perform the best measured by $F_1Score$ are not the best
  in terms of savings.}
  \label{fig:7:comparison_per_best}
\end{figure}

Moreover, it is observed that the standard logistic regression trained using the training set is
the model that performs the worst measured by both savings and $F_1Score$. This is related not only
to the cost-sensitivity of the problems, but also to the highly unbalanced distribution of positive
and negatives presented in all the databases. This is the reason why by using an under-sampling
procedure, the results are improved by both measures.

\begin{sidewaystable}

    \centering
    \footnotesize
    \begin{tabular}{l l r@{\hskip 0in}c@{\hskip 0in}l r@{\hskip 0in}c@{\hskip 0in}l r@{\hskip
    0in}c@{\hskip 0in}l  r@{\hskip 0in}c@{\hskip 0in}l r@{\hskip 0in}c@{\hskip 0in}l} %sum 7.7
    \hline
    \bf{Family} & \bf{Algorithm} & \multicolumn{3}{c}{\bf{Fraud}} &
    \multicolumn{3}{c}{\bf{Churn}} & \multicolumn{3}{c}{\bf{Credit 1}}
    &  \multicolumn{3}{c}{\bf{Credit 2}} & \multicolumn{3}{c}{\bf{Marketing}} \\
    \hline
CI&LR-t & 0.0092 &$\pm$& 0.0002 & -0.0001 &$\pm$& 0.0002 & 0.0177 &$\pm$& 0.0126& 0.0039 &$\pm$& 0.0012 & -0.2931 &$\pm$& 0.0602\\
&LR-u & 0.1243 &$\pm$& 0.0387 & 0.0039 &$\pm$& 0.0492 & 0.4118 &$\pm$& 0.0313& 0.1850 &$\pm$& 0.0231 & 0.2200 &$\pm$& 0.0376\\
\hline
CPS&LR-r & 0.3077 &$\pm$& 0.0301 & 0.0484 &$\pm$& 0.0375 & 0.3965 &$\pm$& 0.0263& 0.2650 &$\pm$& 0.0115 & 0.4210 &$\pm$& 0.0267\\
&LR-o & 0.2793 &$\pm$& 0.0185 & 0.0316 &$\pm$& 0.0228 & 0.3301 &$\pm$& 0.0109& 0.2554 &$\pm$& 0.0090 & 0.3129 &$\pm$& 0.0277\\
\hline
BMR&LR-t-BMR & 0.4552 &$\pm$& 0.0203 & 0.1082 &$\pm$& 0.0316 & 0.2189 &$\pm$& 0.0541& \bf{0.3148} &\bf{$\pm$}& \bf{0.0094} & \bf{0.4973} &\bf{$\pm$}& \bf{0.0084}\\
\hline
CST&CSLR-t & \bf{0.6113} &\bf{$\pm$}& \bf{0.0262} & \bf{0.1118} &\bf{$\pm$}& \bf{0.0484} &
\bf{0.4554} &\bf{$\pm$}& \bf{0.1039}& 0.2748 &$\pm$& 0.0069 & 0.4484 &$\pm$& 0.0072\\
\hline
  \multicolumn{17}{c}{(Models with the highest savings are marked in bold)}
  \end{tabular}
    \caption{Results of the algorithms measured by savings}
    \label{tab:7:results_savings}

\bigskip\bigskip\bigskip  % provide some separation between the two tables

    \begin{tabular}{l l r@{\hskip 0in}c@{\hskip 0in}l r@{\hskip 0in}c@{\hskip 0in}l r@{\hskip
    0in}c@{\hskip 0in}l  r@{\hskip 0in}c@{\hskip 0in}l r@{\hskip 0in}c@{\hskip 0in}l} %sum 7.7
    \hline
    \bf{Family} & \bf{Algorithm} & \multicolumn{3}{c}{\bf{Fraud}} &
    \multicolumn{3}{c}{\bf{Churn}} & \multicolumn{3}{c}{\bf{Credit 1}}
    &  \multicolumn{3}{c}{\bf{Credit 2}} & \multicolumn{3}{c}{\bf{Marketing}} \\
    \hline
CI&LR-t & 0.1531 &$\pm$& 0.0045 & 0.0000 &$\pm$& 0.0000 & 0.0494 &$\pm$& 0.0277& 0.0155 &$\pm$& 0.0037 & 0.2702 &$\pm$& 0.0125\\
&LR-u & 0.0241 &$\pm$& 0.0163 & 0.1222 &$\pm$& 0.0098 & 0.3160 &$\pm$& 0.0314& \bf{0.3890} &\bf{$\pm$}& \bf{0.0053} & 0.3440 &$\pm$& 0.0083\\
\hline
CPS&LR-r & 0.1846 &$\pm$& 0.0123 & 0.1258 &$\pm$& 0.0111 & 0.3597 &$\pm$& 0.0156& 0.3793 &$\pm$& 0.0049 & 0.3374 &$\pm$& 0.0101\\
&LR-o & 0.1776 &$\pm$& 0.0117 & 0.1085 &$\pm$& 0.0203 & \bf{0.3769} &\bf{$\pm$}& \bf{0.0067} & 0.3804 &$\pm$& 0.0044 & \bf{0.3568} &\bf{$\pm$}& \bf{0.0102}\\
\hline
BMR&LR-t-BMR & 0.1384 &$\pm$& 0.0044 & \bf{0.1370} &\bf{$\pm$}& \bf{0.0150} & 0.1915 &$\pm$&
0.0340& 0.3572 &$\pm$& 0.0045 & 0.2954 &$\pm$& 0.0079\\
\hline
CST&CSLR-t & \bf{0.2031} &\bf{$\pm$}& \bf{0.0065} & 0.1134 &$\pm$& 0.0151 & 0.1454 &$\pm$& 0.0517& 0.3363 &$\pm$& 0.0045 & 0.2339 &$\pm$& 0.0051\\
\hline
  \multicolumn{17}{c}{(Models with the highest $F_1Score$ are marked in bold)}
  \end{tabular}
    \caption{Results of the algorithms measured by $F_1Score$}
    \label{tab:7:results_fscore}

\end{sidewaystable}


\documentclass[10pt,letter]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{stmaryrd}
\def\fatbar{\talloblong}
\onehalfspacing
\usepackage{fullpage}
\newcommand{\R}{\mathbb{R}}
\newcommand{\inner}{\langle\cdot,\cdot\rangle}
\newcommand{\inr}[2]{\langle #1, #2\rangle}
\newcommand\norm[1]{\left\lVert#1\right\rVert}

\graphicspath{ {images/} }

\begin{document}

\title{CS142 Homework Set \#5 Solutions}

\author{Timur Kuzhagaliyev}

%\date{7th November, 2017}

\maketitle

\section*{Problem 1}

\paragraph{a)} I'll use an algorithm based on a concept of a diffusing computation similar to gossip algorithm in Sivilotti 6.5, modified to pass the total number of children $+ 1$ to the parent. I'll use the same specification and definitions as in Sivilotti 6.4, with 2 changes. I will adjust the definitions of $msg(x, y)$ and $done$ to fit our problem:
\begin{align*}
msg(x, y)\quad \equiv \quad &\textrm{tuple value $(p, c)$ of the message in the channel from $x$ to $y$,}
\\
& \textrm{where $p$ is the parent of the sender (if any) and $v$ is some integer}
\\
done\quad \equiv \quad &(\; \forall\; v : v\; nbr\; I : msg(v, I) \land (msg(v, I).p = I \Rightarrow  msg(v, I).c = -1)\;\; )
\end{align*}

Note about notation: When $msg(x, y)$ is used on its own (without accessing the values in the tuple), its interpreted as "there is a message in channel from x to y".

First, we define a UNITY program for the agent $I$ that initiates the diffusing computation. Its task is to send the initial gossip messages to all of its neigbours. Once it gets a message back, it checks that it's the parent of the sender and increments the vertex count by the reported value.
\begin{align*}
\textrm{\textbf{Program}} \qquad & \textrm{Initiator}\; I
\\
\textrm{\textbf{var}} \qquad & vertex\_count\; \textrm{:\; int}
\\ & msg(a, b)\; \textrm{:\; channel from $a$ to $b$}
\\
\textrm{\textbf{initially}} \qquad & vertex\_count = 1
\\
\land\; & (\; \forall\; v : v\; nbr\; I : \neg\, msg(v, I) \land \neg\, msg(I, v)\; )
\\
\textrm{\textbf{assign}} \qquad &
(\, \fatbar\, v : v\; nbr\; I : \neg\, msg(I, v) \longrightarrow msg(I, v) := (-1, -1)\; )
\\
\fatbar\quad & (\, \fatbar\, v : v\; nbr\; I : msg(v, I) \land msg(v, I).p = I \land msg(v, I).c \neq -1 \longrightarrow
\\
& \qquad \qquad vertex\_count,\; msg(v, I).c := vertex\_count + msg(v, I).c,\; -1\; )
\end{align*}

I will refer to actions as $I1$ and $I2$. The action $I2$ of program \texttt{Initiator} checks whether $I$ is the parent of the incoming message. If it is, it adds the count passed in the message to $vertex\_count$ and resets the value in the message to $-1$. If the value in the message is already $-1$, it is ignored. By definition of $done$, $done$ will hold when $I$ has received a message from each of its neighbours and processed them using the logic I just described.

Now we define a UNITY program for all other agents which will be spreading the gossip. It's a modified version of the Gossip program in Sivilotti 6.5. The middle action executes the same logic as described in the paragraph above, but applied to \texttt{Gossip} program instead.
\begin{align*}
\textrm{\textbf{Program}} \qquad & \textrm{Gossip}\; u
\\
\textrm{\textbf{var}} \qquad & vertex\_count_u\; \textrm{:\; int}
\\ & parent_u\; \textrm{:\; process}
\\ & state_u\; \textrm{:\;} \{idle, active, complete\}
\\ & msg(a, b)\; \textrm{:\; channel from $a$ to $b$}
\\
\textrm{\textbf{initially}} \qquad & vertex\_count = 1
\\
\land\; & state_u = idle
\\
\land\; & (\; \forall\; v : v\; nbr\; u : \neg\, msg(u, v)\; )
\\
\textrm{\textbf{assign}} \qquad &
(\, \fatbar\, v : v\; nbr\; u : state_u = idle \land msg(v, u) \longrightarrow
\\
& \qquad \qquad parent_u := v\;
\\
& \qquad \qquad \lVert\;\; state_u := active\;
\\
& \qquad \qquad \lVert\;\; (\; \forall\; w : w\; nbr\; u \land w \neq v : msg(u, w) := (v, -1)\; ) )
\\ &
\fatbar (\, \fatbar\, v : v\; nbr\; u : state_u = active \land msg(v, u) \land msg(v, u).p = u \land msg(v, u).c \neq -1 \longrightarrow
\\
& \qquad \qquad vertex\_count_u,\; msg(v, u).c := vertex\_count + msg(v, u).c,\; -1\; )
\\ &
\fatbar state_u = active \land (\, \forall\, v : v\; nbr\; u \land v \neq parent_u : msg(v, u) \land (msg(v, u).p = u \Rightarrow  msg(v, u).c = -1)\;) \longrightarrow
\\
& \qquad \qquad msg(u, parent_u) := (parent_u,\, vertex\_count_u)\;
\\
& \qquad \qquad \lVert\;\; state_u := complete\;
\end{align*}

I will refer to the actions as $A1$, $A2$ and $A3$ respectively. The action $A3$ in my definition of \texttt{Gossip} is similar to that in Sivilotti 6.5, except I also check that for all incoming messages which defined $u$ as their parent the count $c$ is set to $-1$. That is, I make sure that all relevant counts were added to $vertex\_count_u$ before reporting it to the parent process.

Note that both \texttt{Initiator} and \texttt{Gossip} programs start out with $vertex\_count$ equal to $1$ because they automatically count themselves as a vertex.

\paragraph{b)} The proof for termination of diffusing computations can be found in Sivilotti 6.6. My algorithm is a modified version of the gossip algorithm Sivilotti uses.

\paragraph{c)} To prove that $I.vertex\_count$ has the correct counts at termination, we can define and prove another safety property: $\textrm{\textbf{invariant}} (done \Rightarrow I.vertex\_count = C)$ where $C$ is the true vertex count in the graph.

To prove that this new safety property holds I will extend the proof given in Sivilotti 6.6. There, he defines $T_1$ and $T_2$, proving the invariant that both are trees. We also know that the diffusing computation terminates, so eventually the "gossip" reaches all of the vertices, and each vertex eventually receives counts from its children and reports its count to its parent. Similar to Sivilotti 6.6.1, action $A3$ can delete a vertex from $T_2$, and it can be shown by contradiction that the deleted vertex must be a leaf in $T_2$.

We can show that when $T_2$ shrinks back to the state where it just contains $I$, $I.vertex\_count$ will be the correct count, by considering subtrees of $T_2$, when $T_2$ has all of the vertices from the original graph (i.e. right before it begins to shrink).

We know that $done$ will only hold after $I$ has received messages from all of its neighbours and action $I2$ has been executed on every incoming message, for which $I$ is the parent of the sender. This means $I$ would have taken the counts from these messages and added them to its $vertex\_count$. Since $vertex\_count$ starts out as $1$, after $done$ holds $I$ will hold the correct counts given that each child of $I$ in $T_2$ has reported the correct counts for the subtree this child is a root of.

We can show that the same condition applies to all vertices in $T_2$ that are a root of some subtree of $T_2$, apart from leaf vertices of $T_2$. This is true because action $A3$ only sends out a message once it received a message from all of its non-parent neighbours and the messages have been processed, that is $A_2$ has been applied to all incoming messages for which the current vertex is the parent of the sender. Again, if all children have reported the correct counts, the root of the subtree will have the correct $vertex\_count_u$ and report it to its parent.

Finally, the leaf vertices in $T_2$ would have no children by definition of $T_2$ - if they would have children they would not be leaves when $T_2$ contains all vertices from the original graph. This means that action $A2$ will never run and will never increment $vertex\_count_u$, so its value will just remain $1$. If the leaf has no other neighbours but the parent, it will simply report $1$ to the parent, which is the correct count since the subtree only has 1 vertex. If the leaf node has other neighbours, we know that a diffusing computation eventually reaches all vertices, so eventually current vertex will receive a message from all of its non-parent neighbours and finally report $1$ to its parent.

From 3 points above we can conclude that $I.vertex\_count$ would have the correct counts since each of its children would report the counts of its respective subtrees, and these counts will be correct given every subtree under a particular child reports the correct counts. The base case is a leaf node, and we've shown that a leaf node would report a correct count of $1$. Building up, this shows that every subtree would report the correct count, hence $I$ would receive the correct counts from its children.

\section*{Problem 2}

\paragraph{a)} To show that the provided property is required, let's consider a modified version of Lamport's mutual exclusion algorithm where the agent just needs to receive a message from every other agent after sending the request, but the logical time on these messages need not be larger than requested access time $t_i$. We will show that this modified version does not satisfy the safety property.

Consider the execution path where processes $P_1$ and $P_2$ have already communicated before, and $P_1$ has successfully entered the critical section (CS). In the beginning of timeline below, $P_1$ is just about to leave the CS and notify $P_2$ about it. As you can see at logical time $7$ on $P_2$, its queue still contains that older request from $P_1$.

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{hw5_problem2}

\paragraph{Explaining the diagram:} At time $7$ $P_2$ sends out a message requesting access to the critical section. At time $8$, it receives a message from $P_1$ saying that it has left the critical section. In our modified version of Lamport's algorithm, even though the timestamp on the message from $P_1$ is $5$ and hence smaller than local logical time, we still treat it as an $ACK$ and enter the critical section.

Process $P_1$ sends out its own request for access at local time $6$, and waits for any messages from $P_2$. At local time $8$, it receives a request $<P2, 7>$ from $P_2$, but the time of the request is larger than $P_1$'s own request. $P_2$'s request goes to the end of the queue. Now, $P_1$ has received a message from $P_2$ and its request is in the head of the queue - hence it enters the CS, by our modified version of Lamport's algorithm.

Now we have 2 processes in the critical section at the same time, so using \textit{any} messages as ACKs instead of messages with larger logical clock value is not sufficient.

\pagebreak

\paragraph{b)} Consider the following timeline:

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{hw5_problem2_b}

At the beginning of the timeline, we assume that all processes have already communicated before. $P_2$ is in the CS, $P_1$ is waiting for $P_2$ to leave the CS and $P_3$ simply maintains a queue without requesting access to CS. Once $P_2$ exits CS, it broadcasts a release message which gets delivered to $P_1$ sooner than to $P_3$. Since $P_1$'s request is now at the head of the queue and its request timestamp is smaller than all known times, it will now enter the CS. This can happen before $P_3$ will receive the release message, so $P_1$ will be in CS without being at the head of the queue for all agents.
\\

This cannot happen when there are no messages in transit. By definition of Lamport's algorithm, if there are no messages in transit, either all agents are in the non-critical section (NC) of code or there is a single agent that is currently inside CS with zero or more agents waiting for it to exit the CS. In the first case, since all agents are in NC, there is no reason for them to communicate so there are no messages in transit. In the second case, all agents have already exchanged requests and ACKs, and now are just waiting for the agent inside CS to exit and notify all other agents so next process can enter it.

We're interested in the second case. Let's denote the process that's currently in CS as $P_{CS}$. $P_{CS}$ could've entered CS by either being the first process to request access and receive ACKs from all other agents OR by being the second process in the queue and popping the head of the queue after the previous process exits CS.

We know that there are no requests with timestamps lower than $P_{CS}$'s request because all messages were already delivered and there are no messages in transit. If $P_{CS}$ was the very first process to request access, by now it would have received ACKs from all other agents and will now be in CS. At the same time, we know that it's at the head of every other agent's queue because it has the smallest request time and all messages have been delivered.

If $P_{CS}$ was the second (earliest) process to request access and have now popped the head of the queue to become first, it must have received a release message from the previous process in CS. Since no messages are in transit, all other processes would have also received the same notification and popped the previous process of their queues. Now, $P_{CS}$ has the smallest request time and all other agents have $P_{CS}$'s request on their queue since all messages were delivered. This means $P_{CS}$ is at the head of the queue for every agent when it enters CS.

Therefore, if there are no messages in transit and $P_{CS}$ is inside the CS, $P_{CS}$'s request must be at the head of queue for all agents.

\section*{Problem 3}

Description of the problem suggests that the algorithm does not involve agents sending ACKs back when they receive a request, so I will assume that this is indeed the case. Also assuming there is only one FIFO channel (in each direction) between any 2 agents.

The question doesn't bound the value of $\tau$ above, so I will assume that $\tau$ is finite, i.e. \texttt{my-time} will be broadcast eventually. The question also suggests that no release or request messages are sent at time $n*\tau$ - I will assume that request and release messages are timestamped with the time at which they have been sent, and not at the time they have been queued. That is, if the request message was about to be sent but it was time to broadcast \texttt{my-time}, the timestamp on the request message once its sent will be larger than reported \texttt{my-time}.

\paragraph{Proving safety:} We need to show that multiple agents cannot be in CS at the same time. We can prove that this is the case by contradiction.

Assume we have 2 agents, $P_1$ and $P_2$ in CS at the same time. For this to be true, their corresponding request times $z_1$ and $z_2$ must be the smallest timestamps in their lists $L$.

Assume, wlog, that $z_1 < z_2$. For $P_2$ to be in CS, it must be true that $z_2 < \textrm{REMOTE\_TIMES}[P_1]$. Since message channels are FIFO and $\textrm{REMOTE\_TIMES}$ is updated on any received message, $P_1$'s request must be in $P_2$'s list $L$, and hence $P_2$ will know $z_1 < z_2$ and cannot enter the CS. This is a contradiction.

\paragraph{Proving progress:} For a particular agent $P_i$ in $TRY$ state, as our metric we can use the number of elements in the array $P_i.\textrm{REMOTE\_TIMES}$ that have known time values $z_j$ smaller than $P_i$'s request time $z_i$. Clearly this value is bound below by $0$, and since the clocks on every agent can only tick forward, this number is guaranteed to not increase.

To show  that the metric is guaranteed to decrease we can consider some agent $P_j$ such that\linebreak[4]\mbox{$P_i.\textrm{REMOTE\_TIMES}[P_j] < z_i$}. By the definition of our algorithm, the clock on each agent always ticks forward and every agent transmits its local time at some interval $\tau$. This means that eventually $P_i$ will receive a message from $P_j$ with timestamp greater than $z_i$, decreasing our metric by 1. This execution will be repeated until our metric reaches zero.

We can also show that eventually any process in $TRY$ state will receive a release message for all the requests in its $L$ that have timestamps $z_j$ smaller than $z_i$, hence allowing it to enter CS. If $z_i$ is not the smallest timestamp in $L$ and there are no messages in transit, there must exist some other agent $P_k$ with request timestamp $z_k$ such that $z_k < z_i$. Note that our chosen metric for $P_k$ will eventually decrease to $0$ allowing it to enter CS. It will later leave CS and broadcast a release message. This can be repeated for all agents with request timestamps smaller than $z_i$ until we reach a state where it is $P_i$'s turn to enter CS.

Remark: If there is a significant time difference between $P_i$ and $P_k$ (e.g. $P_i$'s time is $250$ seconds greater than $P_k$'s time) it is true that $P_k$ can enter and exit CS multiple times before $P_i$ gets it turn, but eventually the time on $P_k$ will exceed $z_i$ and $P_i$ will be able to enter CS.

Therefore the safety and progress properties are satisfied and the algorithm is correct.

\end{document}


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

\marginpar{Monday\\ 2020-4-27, \\ compiled \\ \today}

% We discussed the Fluctuation Dissipation Theorem last time:
% we have qualitative arguments, if by equipartition each DoF has energy \(1/2 k_B T\) and it is dissipative there must be some forcing mechanism.

Let us consider a lossy harmonic oscillator, whose equation of motion is
%
\begin{align}
m_0 \ddot{x} + m_0 \gamma_0 \dot{x} + k_0 x = F
\,,
\end{align}
%
so in Fourier space the force can be expressed as
%
\begin{align}
F(\omega ) &= m_0 \qty(-\omega^2 - i \omega \gamma_0 + \frac{k_0}{m_0 }) x(\omega )  \\
&= \underbrace{\frac{i m_0 }{\omega} \qty(-\omega^2 - i \omega \gamma_0 + \omega_0^2)}_{Z(\omega )}
\dot{x}(\omega )
\,,
\end{align}
%
which means that we can write the impedance as
%
\begin{align}
Z(\omega ) = \underbrace{m_0 \gamma_0}_{\Re{Z}} + i \frac{m_0}{\omega } \qty(\omega_0^2 - \omega^2)
\,.
\end{align}
%

So, the thermal noise PSD of the reverse-engineered signal will be
%
\begin{align}
S_x (\omega ) = \frac{4 k_B T}{\omega^2}
\frac{m_0 \gamma_0 }{m_0^2 \gamma_0^2 +
\qty( \frac{m_0}{\omega } \qty(\omega_0^2 - \omega^2))^2}
= S_F (\omega ) \abs{H_{F \to x} (\omega )}^2
\,.
\end{align}

For \textbf{structural damping} \(\delta \), we have \(\Re{Z(\omega )} = m_0 \delta \omega_0^2/ \omega \), so the force PSD is
%
\begin{align}
S_F (\omega ) =4 k_B T m_0 \delta \frac{\omega_0^2}{\omega }
\,.
\end{align}

\subsubsection{Thermal noise for the resonant bar}

What does the noise look like in the bar-transducer system?
We need to compare the generic \(Z(\omega )\) we wrote for the harmonic oscillator to the transfer function we found for the fundamental mode of the resonant bar, \(H_{h \to \xi_0  } (\omega )\): we find that they are related by
%
\begin{align}
H_{h \to \xi_0 } (\omega ) = - \frac{2L}{\pi^2} m_0 \omega i \frac{1}{Z(\omega )}
\,,
\end{align}
%
which, together with the fact that \(\Re{Z} = m_0 \gamma_0 \), means that the noise PSD for the fundamental mode will read
%
\begin{align}
S_{\xi_0 }
&= \frac{4k_BT}{\omega^2} \frac{\Re{Z(\omega )}}{\abs{Z(\omega )}^2}
= \frac{4k_BT}{\omega^2 } \frac{m_0 \gamma_0 }{\abs{ \frac{2L}{\pi^2} m_0 \omega_0  \frac{1}{H}}^2}
\\
&= \frac{4 k_B T \gamma_0 }{m_0 \omega^{4}}
\qty(\frac{\pi^2}{2L})^2 \abs{H_{h \to \xi_0 } (\omega )}^2
\,.
\end{align}

If we try to reverse-engineer the original GW input, we must divide by the absolute value of the transfer function, which cancels out: so, we find
%
\begin{align}
S_{h, \text{th}} = \frac{S_{\xi_0 }}{\abs{H_{h \to \xi_0 } (\omega )}^2}
= \frac{4 k_B T \gamma_0 }{m_0 \omega^{4}}
\qty(\frac{\pi^2}{2L})^2
= \frac{\pi}{Q_0 } \frac{k_B T}{M v_s^2} \frac{f_0^3}{f^{4}}
\,,
\end{align}
%
where we used the relations \(\gamma_0 = \omega_0 / Q_0 \), \(m_0 = M / 2\) and \(L = \pi v_s / \omega_0 \).
So, a large quality factor reduces thermal noise.

\subsubsection{Thermal noise for the transducer}

To treat the thermal noise for the bar-transducer system, we assume that both of them are at equilibrium at a certain temperature \(T\), but their damping factors are independent:
%
\begin{align}
S_{F_0 } = 4 k_B T m_0 \gamma_0 \qquad \text{and} \qquad
S_{F_t} = 4 k_B T m_t \gamma_{t}
\,.
\end{align}

The displacement of the transducer due to the two stochastic forces is given by
%
\begin{align}
\xi_{t} (\omega ) = \frac{\pi^2}{2L} \frac{H_{h \to \xi_{t} } (\omega )}{\omega^2} \qty(\frac{F_0 }{m_0 } - \frac{F_t (\omega )}{m_t} \frac{\omega^2 - \omega_0^2 + i \omega \gamma_0 }{\omega_0^2})
\,.
\end{align}

Using these two relations we can compute the transducer's power spectral density:
%
\begin{align}
S_{\xi_{t}} (\omega ) = \frac{\pi^{4}}{4 L^{2}}
\frac{\abs{H_{h \to \xi_{t}}^2} (\omega )}{\omega^{4}}
4 k_B T \qty(\frac{\gamma_0}{m_0 } + \frac{\gamma_{t}}{m_t} \frac{(\omega^2- \omega_0^2)^2 + \omega \gamma_0^2}{\omega_0^2})
\,.
\end{align}

\todo[inline]{Missing square modulus of the transfer function in the notes! See \cite[eq.\ 8.134]{maggioreGravitationalWavesVolume2007}.}

In terms of the input, this will look like:
%
\begin{align}
S _{h, \text{th}} = \pi \frac{k_B T}{M v_s^2}
\frac{f_0^3}{f^{4}} \qty( \frac{1}{Q_0} + \frac{1}{\mu Q_t} \frac{\qty(f^2-f_0^2)^2 + \qty(f f_0 / Q_0 )^2}{f_0^{4}})
\,.
\end{align}

While \(Q_0 \) is quite large, both \(Q_t\) and \(\mu \) are rather small: this might seem like a big issue, since it means that the thermal noise contribution from the transducer is quite large.

Fortunately, near resonance (\(f \approx f_0 \)) the first term in the numerator goes to zero; while the second term always stays small since it is suppressed by a factor \(Q_0^{-2}\).
This means that, while far from resonance the thermal noise from the transducer dominates, near resonance it is quite small and the thermal noise from the bar dominates.
% The noise by the transducer is very small at resonance, the noise of the bar does not have features there: the PSD is

% \todo[inline]{Missing bit\dots still to understand}
A qualitative explanation for this behavior is that since the response of the system is resonant even an oscillation with a large amplitude corresponds to a small GW amplitude when we reconstruct the signal.

\subsection{Readout noise}

Without getting into the specifics of the electronic setup, we try to construct a system so that small displacements of the bar are mapped linearly to voltages which we can measure:
%
\begin{align}
V _{\text{out}} = \alpha \xi_{t}
\,,
\end{align}
%
and, since the PSD is 2-homogeneous, if we have some readout noise whose PSD is \(S_{V _{\text{out}}}\), then the effective PSD of the noise for the reconstructed displacement signal will be
%
\begin{align}
S_{\xi_{t}} = \frac{1}{\alpha^2} S_{V _{\text{out}}}
\,.
\end{align}

This means that we can interpret the readout noise as an effective displacement. If we go further and consider the effective GW signal modification, we will see
%
\begin{align}
S_{h, \text{ro}} = \frac{1}{\abs{H_{h \to \xi_{t}}}^2}
S_{\xi_{t}} = \frac{1}{\abs{H_{h \to \xi_{t}}}^2 \alpha^2} S_{V _{\text{out}}}
\,.
\end{align}

Usually, the spectral shape of the readout noise is quite uniform, but since the transfer function is featured the effective GW PSD due to readout will be featured as well.

\subsection{Effective temperature}

We want to find out how much energy the GW must carry in order to be visible above the noise.

For \textbf{thermal noise}, we have seen that the characteristic time for the decay of the decay of a fundamental mode oscillation is \(\tau_0 = 1/ \gamma_0 = Q_0 / \omega_0 \), of the order of several minutes.
This characterizes \emph{all} the mode's interactions with the thermal bath, so the time it takes for the bath to spoil the oscillation is \(\tau_0 \) as well: this means that, while we might naïvely expect the noise threshold for the detection of a GW to be \(E_{GW} \geq k_B T\), it is in fact much lower.
Specifically, if \(\Delta t\) is our sampling time, then the noise threshold is actually \(E_{GW} \geq k_B T \Delta t / \tau_0 \).

As for the \textbf{readout noise}, if our bandwidth (the inverse of the sampling time) is \(\Delta f\), then the variance of the readout noise in terms of displacement of the transducer can be recovered from the PSD by
%
\begin{align}
\expval{\xi_{t}^2} &= \int_{f_0 - \Delta f / 2}^{f_0 + \Delta f / 2}
S_{\xi_{t} , \text{ro}} \dd{f} \sim S_{\xi_{t} , \text{ro}} \Delta f
\,,
\end{align}
%
which corresponds to an energy of \(E_{\text{ro}} \sim m_t \omega_0 \expval{\xi_{t}^2} \sim m_t \omega_0  S_{\xi_{t} , \text{ro}} \Delta f\).

The interesting thing to note is that the dependence of the thermal noise threshold energy on the sampling time is \textbf{direct} (if we sample for a long time the bath has a long time to interact with the oscillator), while the corresponding quantity for the readout noise has an \textbf{inverse dependence} on \(\Delta t\) (sampling very fast is error-prone since we are exposed to more high-frequency noise):
%
\begin{align}
\Delta E _{\text{min}} \sim k_B T \frac{\Delta t}{\tau_0 }
+ \frac{m_t \omega_0 S_{\xi_{t}, \text{ro}}}{\Delta t}
\,.
\end{align}

We need to trade off these two contributions; the minimum can be found by differentiating and it comes out to be
%
\begin{align}
\Delta f _{\text{opt}} = \frac{1}{\Delta t _{\text{opt}}} \approx
\pi \frac{f_0 }{Q \sqrt{\Gamma }}
\qquad \text{where} \qquad
\Gamma = \frac{m_t \omega_0^3  S_{\xi_t, \text{ro}}}{4Q k_B T}
\,.
\end{align}
%

An important distinction to make is the one between the useful bandwidth \(\Delta f _{\text{opt}}\), which is quite large (tens of Hertz), and the width of the resonance peak \emph{of the transfer function}, which is \(\Delta f _{\text{res}} \sim f_0 / Q \sim \SI{1}{mHz}\).
The detector's actual resonance peaks are \textbf{broad}, with a bandwidth comparable to \(\Delta f _{\text{opt}}\).

At the optimal sampling rate, the two contributions to the \(\Delta E\) are equal, therefore we have
%
\begin{align}
\Delta E _{\text{opt}} \sim 2 k_B T \frac{\Delta t _{\text{opt}}}{\tau_0 }
\sim 2 k_B T \frac{\omega_0}{Q \Delta f}
= k_B \underbrace{T \frac{4 \pi f_0 }{Q \Delta f}}_{T _{\text{eff}}}
\,,
\end{align}
%
which can be interpreted as a new effective temperature at which the oscillator is immersed: substituting in we can find that it is given by
%
\begin{align}
T _{\text{eff}} \sim 4 \sqrt{\Gamma } T
\,,
\end{align}
%
which can be much lower than the real temperature of the object, since \(\Gamma \) can be made to be of the order \num{e-8} to \num{e-9}: this means that the effective temperature can be three orders of magnitude lower than the thermodynamic one.

The definitive formula for the sum of these contributions can be found in Maggiore \cite[eq.\ 8.150]{maggioreGravitationalWavesVolume2007}; it is interesting to compare the shape of the PSD at varying values of \(\Gamma \): if it is rather high (\(\sim \num{e-7}\)) then the PSD is quite high as well with two conspicuous drops at \(\omega_0 \pm \omega_{p}\). Here, the transducer thermal noise dominates almost everywhere.

If \(\Gamma \) becomes lower, of the order of \num{e-9}, then the PSD becomes uniformly low in the range \([\omega_0 - \omega_{p}, \omega_0 + \omega_{p}]\).

\section{Gravitational Wave Interferometry}

\subsection{Mach-Zender interferometer}

The setup here is the following: the laser impacts onto a first beamsplitter, is split onto two orthogonal paths which are made to converge onto a second beamsplitter through two mirrors. After this second beamsplitter, the signal is measured. Depending on the phase, the interference is either constructive or destructive at the second beamsplitter.

The laser's electric field will be given by
%
\begin{align}
\vec{E} _{\text{in}} = \vec{E}_{0} \exp(-i \qty(\omega_{l} t - k_l t))
\,,
\end{align}
%
where the subscript \(l\) means ``laser'', we include it in order to distinguish these parameters from the GW ones.

In general:
\begin{enumerate}
    \item at reflection the beam picks up a phase \(\pi \);
    \item at transmission the beam picks up no phase;
    \item between the two paths there is a phase difference due to their different lengths: \(\Delta \phi \).
\end{enumerate}

So, the fields incoming to the second beamsplitter are:
%
\begin{align}
E_T = \frac{E _{\text{in}}}{\sqrt{2}} e^{i \pi }
\qquad \text{and} \qquad
E_R = \frac{E _{\text{in}}}{\sqrt{2}} e^{i \Delta \phi }
\,.
\end{align}

At the second beamsplitter there are two outputs: the outgoing fields are
%
\begin{align}
E _{\text{out, }1} &= \frac{E_{T}}{\sqrt{2}} e^{i \pi }
+ \frac{E_{R}}{\sqrt{2}} = \frac{E_{\text{in}}}{2} \qty(1 + e^{i \Delta \phi })  \\
E _{\text{out, }2} &= \frac{E_{T}}{\sqrt{2}}
+ \frac{E_{R}}{\sqrt{2}} e^{i \pi } = \frac{E_{\text{in}}}{2} \qty(- 1 - e^{i \Delta \phi })
\,,
\end{align}
%
so the initial power is multiplied by \((1 + \cos( \Delta \phi )) / 2 \).
So, is energy not conserved? This output is the same on both ends of the beamsplitter, and these two do not sum to the initial power.

This result is due to an oversight: the phase of \(\pi \) is actually picked up only if the index of refraction increases along the path of the beam. Correcting for this, we find
%
\begin{align}
E _{\text{out, }2}= \frac{E_{\text{in}}}{2} \qty(-1 + e^{i \Delta \phi })
\,,
\end{align}
%
so
%
\begin{align}
\abs{E _{\text{out, }2}}^2 = \frac{\abs{E _{\text{in}}}^2}{2} \qty(1 - \cos(\Delta \phi))
\,,
\end{align}
%
so we recover energy conservation: \(\abs{E _{\text{out, }1}}^2 + \abs{E _{\text{out, }2}}^2 = \abs{E _{\text{in}}}^2\).

\subsection{Michelson-Morley interferometer}

Now the setup is different: the first and second beamsplitters are the same one, and the mirrors reflect the laser directly backwards.

The electric fields in the path from the beamsplitter to either mirror are denoted as \(E_x\) and \(E_y\); they are given by
%
\begin{align}
E_{x} &= \frac{E_0 }{\sqrt{2} \sqrt{2}}
\exp(i \qty(kx - \omega_{l}t + \phi_{x})) \\
E_{y} &= \frac{E_0 }{\sqrt{2} \sqrt{2}}
\exp(i \qty(ky - \omega_{l}t + \phi_{y}))
\,,
\end{align}
%
where the double \(\sqrt{2}\) factor is due to the fact that each beam goes through the beamsplitter twice; while the factors \(\phi_{x, y}\) are the phases picked up upon reflection on each side.

The output electric field (for either output of the beamsplitter --- we will distinguish between them by varying \(\phi \)) is given by
%
\begin{align}
E _{\text{out}} = E_{x} + E_{y}
= \frac{E_0}{2} \qty(\exp(i \qty(kx - \omega_{l}t + \phi_{x})) + \exp(i \qty(ky - \omega_{l}t + \phi_{y})))
\,,
\end{align}
%
so the output intensity is
%
\begin{align}
I _{\text{out}} &= \abs{E _{\text{out}}}^2
= \frac{E_0^2}{4} \qty(2 + \Re{\exp(i(k(x-y) - (\phi_{x} - \phi_{y}) ))})  \\
&= \frac{E_0^2}{2} \qty(1 + \cos(k(x-y) + (\phi_{x} - \phi_{y})))
\,.
\end{align}

Now, for output 1 (perpendicular to the original laser beam) we have \(\phi_{x1} = \pi \) and \(\phi_{y1} = 2\pi \), which can be gathered by counting the number of times the \(x\) (\(y\)) beam is reflected, and adding a \(\pi \) for each.
Similarly, for output 2 we have \(\phi_{x2} = \pi \) and \(\phi_{y2} = 3 \pi \).
So, we have \(\Delta \phi_{1} = -\pi \) and \(\Delta \phi_{2} = - 2 \pi \).

Plugging this in, and using the fact that \(\cos(x + \pi ) = - \cos(x)\), we find
%
\begin{align}
I _{\text{out, 1}} &= \frac{E_0^2}{2} \qty(1 - \cos(k(x-y))) = E_0^2 \sin^2 \qty(\frac{k}{2} (x-y)) \\
I _{\text{out, 2}} &= \frac{E_0^2}{2} \qty(1 + \cos(k(x-y)))
= E_0^2 \sin^2 \qty(\frac{k}{2} (x-y) + \frac{\pi}{2})
\,.
\end{align}

So, both of the outputs heavily depend on the path length difference between the two beams.

\subsection{GW interactions in the detector frame}

In the detector frame we can treat the GW as a Newtonian force acting on the mirrors, and we can compute it using the TT-gauge perturbation \(h_{xx}^{TT} = h_0 \cos(\omega_{GW}t)\) since the Riemann tensor is invariant in linearized gravity:
%
\begin{align}
F_{x} \approx \frac{m}{2} x_0 \ddot{h}_{xx}^{TT}
\,,
\end{align}
%
since the perturbation is really small, we can treat \(x_0 \) as a constant.
This means that the length of the arm changes according to the differential equation
%
\begin{align}
\ddot{x} = \frac{1}{2} x_0 \ddot{h}^{TT}_{xx}
\,.
\end{align}

This only holds in the short arm approximation: \(x \ll \lambda_{GW}\), which means \(f_{GW} \ll c/ L  \approx \SI{100}{kHz} (L / \SI{3}{km} )\) --- fortunately this is not a great constraint, since modern GW interferometers are only sensitive up to a few \SI{}{kHz} anyway.

Let us consider a plus-polarized GW travelling along the \(z\) axis, and let us assume that the beamsplitter is the origin.
Then, let us analyze the motion of the two test masses which are in free-fall: the two mirrors, one for each arm. Their positions will be, respectively, \((x_{XTM}, y_{XTM}, z_{XTM})\) and \((x_{YTM}, y_{YTM}, z_{YTM})\). Because of the polarization of the wave, the only coordinates which will evolve in time will be
%
\begin{align}
x_{XTM} (t ) &= L_x + \frac{h_0 L_x}{2} \cos(\omega_{GW} t) \\
y_{YTM} (t ) &= L_y - \frac{h_0 L_y}{2} \cos(\omega_{GW} t)
\,.
\end{align}
%

If we insert the displacement for the mirrors, assuming \(L_x \sim L_y \sim L\) we find
%
\begin{align}
I _{\text{out}}
&= E_0^2 \sin^2(k(x-y))  \\
&= E_0^2 \sin^2 \qty(k \qty[L_x + \frac{h_0 L_x}{2} \cos(\omega_{GW} t) - L_y + \frac{h_0 L_y}{2} \cos(\omega_{GW} t)]) \\
&= E_0^2 \sin^2 \qty(k \qty(L_x - L_y + h_0 L \cos(\omega_{GW} t)))
\,.
\end{align}

One might think that we should not be able to see any effect, since the GW also stretches the wavelength of the light: this is not the case, since each wavefront will still be travelling at \(c\) and so its travel time will change for a change in the spatial components of the metric.

However, this detector-frame approach is not great, since it neglects all time-of-flight effects. Let us consider the problem in the TT-gauge.

\end{document}


% !TEX root = ./bursty_transcription.tex
\section{Bursty promoter models - generating function solutions and numerics}
\label{sec:gen_fcn_appdx}

\subsection{Constitutive promoter with bursts}

\subsubsection{From master equation to generating function}

The objective of this section is to write down the steady-state mRNA
distribution for model 5 in Figure~\ref{fig2:constit_cartoons}. Our claim is
that this model is rich enough that it can capture the expression pattern of
bacterial constitutive promoters. Figure~\ref{figS1:bursty_one_state} shows two
different schematic representations of the model.
Figure~\ref{figS1:bursty_one_state}(A) shows the promoter cartoon model with
burst initiation rate $k_i$, mRNA degradation rate $\gamma$, and mean burst size
$b$. For our derivation of the chemical master equation we will focus more on
Figure~\ref{figS1:bursty_one_state}(B). This representation is intended to
highlight that bursty gene expression allows transitions between mRNA count $m$
and $m'$ even with $m - m' > 1$.

\begin{figure}[h!]
\centering
\includegraphics{../figures/si/figS0X_bursty_states.pdf}
\caption{
\textbf{Bursty transcription for unregulated promoter.}
(A) Schematic of the one-state bursty transcription model. Rate $k_i$ is the
bursty initiation rate, $\gamma$ is the mRNA degradation rate, and $b$ is the
mean burst size. (B) Schematic depiction of the mRNA count state transitions.
The model in (A) allows for transitions of $> 1$ mRNA counts with probability
$G_{m-m'}$, where the state jumps from having $m'$ mRNA to having $m$ mRNA in a
single burst of gene expression.}
\label{figS1:bursty_one_state}
\end{figure}

To derive the master equation we begin by considering the possible state
transitions to ``enter'' state $m$. There are two possible paths to jump from an
mRNA count $m' \neq m$ to a state $m$ in a small time window $\Delta t$:
\begin{enumerate}
        \item By degradation of a single mRNA, jumping from $m+1$ to $m$.
        \item By producing $m-m'$ mRNA for $m' \in \{0, 1, \ldots, m-1\}$.
\end{enumerate}
For the ``exit'' states from $m$ into $m' \neq m$ during a small time window
$\Delta t$ we also have two possibilities:
\begin{enumerate}
        \item By degradation of a single mRNA, jumping from $m$ to $m-1$.
        \item By producing $m'-m$ mRNA for $m'-m \in \{1, 2, \ldots\}$.
\end{enumerate}
This implies that the probability of having $m$ mRNA at time $t + \Delta t$ can
be written as
\begin{equation}
\begin{split}
p(m, t + \Delta t) = &p(m, t)
+ \overbrace{\gamma \Delta t (m + 1) p(m + 1, t)}^{m + 1 \rightarrow m}
- \overbrace{\gamma \Delta t m p(m, t)}^{m \rightarrow m - 1} \\
&+ \overbrace{k_i \Delta t \sum_{m'=0}^{m-1} G_{m-m'} p(m', t)}^
{m'\in \{0, 1, \ldots m-1\} \rightarrow m}
- \overbrace{k_i \Delta t \sum_{m'=m + 1}^{\infty} G_{m'-m} p(m, t)}^
{m \rightarrow m'\in \{m+1, m+2, \ldots\}},
\end{split}
\label{eq:si_master_deltat}
\end{equation}
where we indicate $G_{m'-m}$ as the probability of having a burst of size
$m'-m$, i.e. when the number of mRNAs jump from $m$ to $m' > m$ due to a single
mRNA transcription burst. We suggestively use the letter $G$ as we will assume
that these bursts sizes are geometrically distributed with parameter $\theta$.
This is written as
\begin{equation}
G_{k} = \theta (1 - \theta)^k\; \text{for } k \in \{0, 1, 2, \ldots \}.
\end{equation}
In Section~\ref{sec:beyond_means} of the main text we derive this functional
form for the burst size distribution. An intuitive way to think about it is that
for transcription initiation events that take place instantaneously there are
two competing possibilities: Producing another mRNA with probability $(1 -
\theta)$, or ending the burst with probability $\theta$. What this implies is
that for a geometrically distributed burst size we have a mean burst size $b$ of
the form
\begin{equation}
b \equiv \left\langle m' - m \right\rangle
= \sum_{k=0}^\infty k \theta (1 - \theta)^k = {1 - \theta \over \theta}.
\end{equation}

To clean up Equation~\ref{eq:si_master_deltat} we can send the first term on the
right hand side to the left, and divide both sides by $\Delta t$. Upon taking
the limit where $\Delta t \rightarrow 0$ we can write
\begin{equation}
{d \over dt}p(m, t) = (m + 1) \gamma p(m + 1, t)
- m \gamma p(m, t)
+ k_i \sum_{m'=0}^{m-1} G_{m-m'} p(m', t)
- k_i \sum_{m'=m + 1}^{\infty} G_{m'-m} p(m, t).
\end{equation}
Furthermore, given that the timescale for this equation is set by the mRNA
degradation rate $\gamma$ we can divide both sides by this rate, obtaining
\begin{equation}
{d \over d\tau}p(m, \tau) = (m + 1) p(m + 1, \tau)
- m p(m, \tau)
+ \lambda \sum_{m'=0}^{m-1} G_{m-m'} p(m', \tau)
- \lambda \sum_{m'=m + 1}^{\infty} G_{m'-m} p(m, \tau),
\label{eq:si_master_ode}
\end{equation}
where we defined $\tau \equiv t \times \gamma$, and $\lambda \equiv k_i/\gamma$.
The last term in Eq.~\ref{eq:si_master_ode} sums all burst sizes except for a
burst of size zero. We can re-index the sum to include this term, obtaining
\begin{equation}
\lambda \sum_{m'=m + 1}^{\infty} G_{m'-m} p(m, \tau) = \lambda p(m, t) \left[
\underbrace{\sum_{m'={m}}^{\infty}G_{m'-m}}
_{\text{re-index sum to include burst size zero}} -
\underbrace{G_0}_{\text{subtract extra added term}}\right].
\end{equation}
Given the normalization constraint of the geometric distribution, adding the
probability of all possible burst sizes -- including size zero since we
re-indexed the sum -- allows us to write
\begin{equation}
\sum_{m'=m}^{\infty}G_{m'-m} - G_0 = 1 - G_0.
\end{equation}
Substituting this into Eq.~\ref{eq:si_master_ode} results in
\begin{equation}
{d \over d\tau}p(m, \tau) = (m + 1) p(m + 1, \tau)
- m p(m, \tau)
+ \lambda \sum_{m'=0}^{m-1} G_{m-m'} p(m', \tau)
- \lambda p(m, \tau) \left[ 1 - G_0 \right].
\label{eq:si_master_ode_2}
\end{equation}
To finally get at a more compact version of the equation notice that the third
term in Eq.~\ref{eq:si_master_ode_2} includes burst from size $m'-m = 1$ to size
$m' - m = m$. We can include the term $p(m, t) G_0$ in the sum which allows
bursts of size $m' - m = 0$. This results in our final form for the chemical
master equation
\begin{align}
{d \over d\tau}p(m, \tau) =
(m + 1) p(m+1, \tau)
- m p(m, \tau) -
\lambda p(m, \tau)
+ \lambda \sum_{m^\prime=0}^m G_{m-m^\prime} p(m^\prime, \tau).
\label{eq:si_master_unreg}
\end{align}

In order to solve Eq.~\ref{eq:si_master_unreg} we will use the generating
function method~\cite{vanKampen2007}. The probability generating function
is defined as
\begin{align}
F(z,t) = \sum_{m=0}^\infty z^m p(m,t),
\end{align}
where $z$ is just a dummy variable that will help us later on to obtain the
moments of the distribution. Let us now multiply both sides of
Eq.~\ref{eq:si_master_unreg} by $z^m$ and sum over all $m$
\begin{equation}
\sum_m z^m {d \over d\tau} p(m, \tau) =
\sum_m z^m \left[
- m p(m, \tau)
+ (m + 1) p(m + 1, \tau)
+ \lambda \sum_{m' = 0}^m G_{m-m'} p(m', \tau) - \lambda p(m, \tau)
\right],
\end{equation}
where we use $\sum_m \equiv \sum_{m=0}^\infty$. We can distribute the sum and
use the definition of $F(z, t)$ to obtain
\begin{equation}
{d F(z, \tau) \over d\tau} =
- \sum_m z^m m p(m, \tau)
+ \sum_m z^m (m + 1) p(m + 1, \tau)
+ \lambda \sum_m z^m \sum_{m'=0}^m G_{m-m'} p(m', \tau)
- \lambda F(z, \tau).
\label{eq:si_generating_01}
\end{equation}
We can make use of properties of the generating function to write everything in
terms of $F(z, \tau)$: the first term on the right hand side of
Eq.~\ref{eq:si_generating_01} can be rewritten as
\begin{align}
\sum_{m} z^{m} \cdot m \cdot p(m, \tau) &=
\sum_{m} z \frac{\partial z^{m}}{\partial z} p(m, \tau), \\
&=\sum_{m} z \frac{\partial}{\partial z}\left(z^{m} p(m, \tau)\right), \\
&=z \frac{\partial}{\partial z}\left(\sum_{m} z^{m} p(m, \tau)\right), \\
&= z {\partial F(z, \tau) \over \partial z}.
\end{align}
For the second term on the right hand side of Eq.~\ref{eq:si_generating_01} we
define $k \equiv m + 1$. This allows us to write
\begin{align}
\sum_{m=0}^{\infty} z^{m} \cdot(m+1) \cdot p(m+1, \tau) &=
\sum_{k=1}^{\infty} z^{k-1} \cdot k \cdot p(k, \tau), \\
&=z^{-1} \sum_{k=1}^{\infty} z^{k} \cdot k \cdot p(k, \tau), \\
&=z^{-1} \sum_{k=0}^{\infty} z^{k} \cdot k \cdot p(k, \tau), \\
&=z^{-1} \left(z \frac{\partial F(z)}{\partial z}\right), \\
&=\frac{\partial F(z)}{\partial z}.
\end{align}

\begin{figure}[h!]
\centering
\includegraphics[width=5cm]{../figures/si/figS0X_reindex_sum.pdf}
\caption{\textbf{Reindexing double sum.} Schematic for reindexing the sum
$\sum_{m=0}^\infty \sum_{m'=0}^m$. Blue circles depict the 2D grid of
nonnegative integers restricted to the lower triangular part of the $m, m'$
plane. The trick is that this double sum runs over all $(m, m')$ pairs with
$m'\le m$. Summing $m$ first instead of $m'$ requires determining the
boundary: the upper boundary of the $m'$-first double sum becomes the
lower boundary of the $m$-first double sum.}
\label{figS2:sum_reindex}
\end{figure}

The third term in Eq.~\ref{eq:si_generating_01} is the most trouble. The trick
is to reverse the default order of the sums as
\begin{equation}
\sum_{m=0}^{\infty} \sum_{m'=0}^{m} = \sum_{m'=0}^{\infty} \sum_{m=m'}^{\infty}.
\end{equation}
To see the logic of the sum we point the reader to
Figure~\ref{figS2:sum_reindex}. The key is to notice that the double sum
$\sum_{m=0}^\infty \sum_{m'=0}^m$ is adding all possible pairs $(m, m')$ in the
lower triangle, so we can add the terms vertically as the original sum
indexing suggests, i.e.
\begin{equation}
\sum_{m=0}^{\infty} \sum_{m'=0}^{m} x_{(m, m')}=
x_{(0, 0)} + x_{(1, 0)} + x_{(1, 1)} + x_{(2, 0)} + x_{(2, 1)} + x_{(2, 2)} +
\ldots,
\end{equation}
where the variable $x$ is just a placeholder to indicate the order in which the
sum is taking place. But we can also add the terms horizontally as
\begin{equation}
\sum_{m'=0}^{\infty} \sum_{m=m'}^{\infty} x_{(m, m')} =
x_{(0, 0)} + x_{(1, 0)} + x_{(2, 0)} + \ldots + x_{(1,1)} + x_{(2, 1)} + \ldots,
\end{equation}
which still adds all of the lower triangle terms. Applying this reindexing
results in
\begin{align}
\lambda \sum_m z^m \sum_{m'=0}^m G_{m-m'} p(m', \tau) =
\lambda \sum_{m'=0}^{\infty} \sum_{m=m'}^{\infty} z^m
\theta (1 - \theta)^{m-m'} p(m', \tau),
\end{align}
where we also substituted the definition of the geometric distribution $G_{k} =
\theta (1 - \theta)^k$. Redistributing the sums we can write
\begin{align}
\lambda \sum_{m'=0}^{\infty} \sum_{m=m'}^{\infty} z^m
\theta (1 - \theta)^{m-m'} p(m', \tau) =
\lambda \theta \sum_{n=0}^{\infty}(1-\theta)^{m'} P(m', \tau)
\sum_{m=m'}^{\infty} \left[z (1-\theta)\right]^{m}.
\label{eq:si_generating_02}
\end{align}

The next step requires us to look slightly ahead into what we expect to obtain.
We are working on deriving an equation for the generating function $F(z, \tau)$
that when solved will allow us to compute what we care about, i.e. the
probability function $p(m, \tau)$. Upon finding the function for $F(z, \tau)$,
we will recover this probability distribution by evaluating derivatives
of $F(z, \tau)$ at $z=0$, whereas we can evaluate derivatives of
$F(z, \tau)$ at $z=1$ to instead recover the moments of the
distribution. The point here is that when the dust settles we
will evaluate $z$ to be less than or equal to one.
Furthermore, we know that the parameter of the geometric distribution $\theta$
must be strictly between zero and one. With these two facts we can safely state that
$| z (1 - \theta) | < 1$. Defining $n \equiv m - m'$ we rewrite the last sum in
Eq.~\ref{eq:si_generating_02} as
\begin{align}
\sum_{m=m'}^{\infty} \left[z (1-\theta)\right]^{m} &=
\sum_{n=0}^{\infty} \left[z (1-\theta)\right]^{n + m'} \\
&= \left[ z (1 - \theta) \right]^{m'}
\sum_{n=0}^{\infty} \left[ z (1 - \theta) \right]^{n} \\
&= \left[ z (1 - \theta) \right]^{m'}
\left( {1 \over 1 - z (1 - \theta)} \right),
\end{align}
where we use the geometric series since, as stated before, $| z (1 - \theta) | <
1$. Putting these results together, the PDE for the generating function is
\begin{equation}
{\partial F \over \partial \tau} =
{\partial F \over\partial z}
- z {\partial F \over \partial z} - \lambda F
+ \frac{\lambda\theta F}{1-z(1-\theta)}.
\end{equation}
Changing variables to $\xi=1-\theta$ and simplifying gives
\begin{align}
{\partial F \over \partial \tau} + (z - 1) {\partial F \over \partial z} =
\frac{(z-1)\xi}{1-z\xi}\lambda F.
\label{eq:1state_unreg_015}
\end{align}

\subsubsection{Steady-state}

To get at the mRNA distribution at steady state we first must solve
Eq.~\ref{eq:1state_unreg_015} setting the time derivative to zero. At
steady-state, the PDE reduces to the ODE
\begin{align}
\deriv[F]{z} = \frac{\xi}{1-z\xi}\lambda F,
\end{align}
which we can integrate as
\begin{align}
\int \frac{dF}{F} = \int \frac{\lambda\xi dz}{1-\xi z}.
\end{align}
The initial conditions for generating functions can be subtle and confusing. The
key fact follows from the definition
$F(z,t) = \sum_m z^m p(m,t)$.
Clearly normalization of the distribution requires that
$F(z=1, t) = \sum_m p(m,t) = 1$.
A subtlety is that sometimes the generating function may be undefined
\textit{at} $z=1$, in which case the limit as $z$ approaches $1$ from
below suffices to define the normalization condition.
We also warn the reader that, while it is frequently convenient to change
variables from $z$ to a different independent variable, one must
carefully track how the normalization condition transforms.

Continuing on, we evaluate the integrals (producing a constant $c$) which gives
\begin{align}
\ln F &= -\lambda \ln(1-\xi z) + c
\\
F &= \frac{c}{(1-\xi z)^\lambda}.
\end{align}
Only one choice for $c$ can satisfy initial conditions, producing
\begin{align}
F(z) = \left(\frac{1-\xi}{1-\xi z}\right)^\lambda
        = \left(\frac{\theta}{1 - z(1-\theta)}\right)^\lambda,
\label{eq:gen_fn}
\end{align}

\subsubsection{Recovering the steady-state probability distribution}

To obtain the steady state mRNA distribution $p(m)$ we are aiming for we need to
extract it from the generating function
\begin{equation}
F(z) = \sum_m z^m p(m).
\end{equation}
Taking a derivative with respect to $z$ results in
\begin{equation}
{d F(z) \over dz} = \sum_m m z^{m - 1} p(m).
\end{equation}
Setting $z = 0$ leaves one term in the sum when $m = 1$
\begin{equation}
\left.\frac{d F(z)}{d z}\right|_{z=0} =
\left(0 \cdot 0^{-1} \cdot p(0)
+ 1 \cdot 0^0 \cdot p(1)
+ 2 \cdot 0^1 \cdot p(2)
+ \cdots \right) = p(1),
\end{equation}
since in the limit $\lim_{x \rightarrow 0^+} x^x = 1$. A second derivative of
the generating function would result in
\begin{equation}
\frac{d^{2} F(z)}{d z^{2}} = \sum_{m=0}^{\infty} m(m-1) z^{m-2} p(m).
\end{equation}
Again evaluating at $z = 0$ gives
\begin{equation}
\left.\frac{d^{2} F(z)}{d z}\right|_{z=0} = 2 p(z).
\end{equation}
In general any $p(m)$ is obtained from the generating function as
\begin{equation}
p(m) = {1 \over m!} \left. {d^m F(z) \over dz} \right\vert_{z=0}.
\label{eq:prob_from_gen}
\end{equation}

Let's now look at the general form of the derivative for our generating function
in Eq.~\ref{eq:gen_fn}. For $p(0)$ we simply evaluate $F(z=0)$ directly,
obtaining
\begin{equation}
p(0) = F(z=0) = \theta^{\lambda}.
\end{equation}
The first derivative results in
\begin{equation}
\begin{aligned}
\frac{d F(z)}{d z} &=\theta^{\lambda} \frac{d}{d z}(1-z(1-\theta))^{-\lambda} \\
&=\theta^{\lambda}\left[-\lambda(1-z(1-f))^{-\lambda-1} \cdot(\theta-1)\right]\\
&=\theta^{\lambda}\left[\lambda(1-z(1-\theta))^{-\lambda-1}(1-\theta)\right].
\end{aligned}
\end{equation}
Evaluating this at $z=0$ as required to get $p(1)$ gives
\begin{equation}
\left.\frac{d F(z)}{d z}\right|_{z=0}=\theta^{\lambda} \lambda(1-\theta)
\end{equation}
For the second derivative we find
\begin{equation}
\frac{d^{2} F(z)}{d z^{2}} = \theta^{\lambda}
\left[\lambda(\lambda+1)(1-z(1-\theta))^{-\lambda-2}(1-\theta)^{2}\right].
\end{equation}
Again evaluating $z = 0$ gives
\begin{equation}
\left.\frac{d^{2} F(z)}{d z^{2}}\right|_{z=0} =
\theta^{\lambda} \lambda(\lambda+1)(1-\theta)^{2}.
\end{equation}
Let's go for one more derivative to see the pattern. The third derivative of the
generating function gives
\begin{equation}
\frac{d^{3} F(z)}{d z^{3}} =
\theta^{\lambda}
\left[\lambda(\lambda+1)
(\lambda+2)(1-z(1-\theta))^{-\lambda-3}(1-\theta)^{3}\right],
\end{equation}
which again we evaluate at $z=0$
\begin{equation}
\left.\frac{d^{3} F(z)}{d z^{3}}\right|_{z=1} =
\theta^{\lambda}\left[\lambda(\lambda+1)(\lambda+2)(1-\theta)^{3}\right].
\end{equation}
If $\lambda$ was an integer we could write this as
\begin{equation}
\left.\frac{d^{3} F(z)}{d z^{3}}\right|_{z=0} =
\frac{(\lambda+2) !}{(\lambda-1) !} \theta^{\lambda}(1-\theta)^{3}.
\end{equation}
Since $\lambda$ might not be an integer we can write this using Gamma functions
as
\begin{equation}
\left.\frac{d^{3} F(z)}{d z^{3}}\right|_{z=0} =
\frac{\Gamma(\lambda+3)}{\Gamma(\lambda)} \theta^{\lambda}(1-\theta)^{3}.
\end{equation}
Generalizing the pattern we then have that the $m$-th derivative takes the form
\begin{equation}
\left.\frac{d^{m} F(z)}{d z^{m}}\right|_{z=0} =
\frac{\Gamma(\lambda+m)}{\Gamma(\lambda)} \theta^{\lambda}(1-\theta)^{m}.
\end{equation}
With this result we can use Eq.~\ref{eq:prob_from_gen} to obtain the desired
steady-state probability distribution function
\begin{equation}
p(m) = \frac{\Gamma(m+\lambda)}{\Gamma(m+1)\Gamma(\lambda)}
        \theta^\lambda (1-\theta)^m.
\end{equation}
Note that the ratio of gamma functions is often expressed as a binomial
coefficient, but since $\lambda$ may be non-integer, this would be ill-defined.
Re-expressing this exclusively in our variables of interest, burst rate
$\lambda$ and mean burst size $b$, we have
\begin{equation}
p(m) = \frac{\Gamma(m+\lambda)}{\Gamma(m+1)\Gamma(\lambda)}
        \left(\frac{1}{1+b}\right)^\lambda
        \left(\frac{b}{1+b}\right)^m.
\label{eq:nbinom_deriv_final}
\end{equation}

\subsection{Adding repression}
\subsubsection{Deriving the generating function for mRNA distribution}

Let us move from a one-state promoter to a two-state promoter, where one state
has repressor bound and the other produces transcriptional bursts as above.
A schematic of this model is shown as model 5 in
Figure~\ref{fig1:means_cartoons}(C). Although now we have an equation for each
promoter state, otherwise the master equation reads similarly to the one-state
case, except with additional terms corresponding to transitions between promoter
states, namely
\begin{align}
{d\over dt}p_R(m,t) =&
k_R^+ p_A(m,t) - k_R^- p_R(m,t)
        + (m+1)\gamma p_R(m+1,t) - m\gamma p_R(m,t)
\\
\begin{split}
{d\over dt}p_A(m,t) =& - k_R^+ p_A(m,t) + k_R^- p_R(m,t)
        + (m+1)\gamma p_A(m+1,t) - m\gamma p_A(m,t)
\\
&- k_i p_A(m,t) + k_i \sum_{m^\prime=0}^m \theta(1-\theta)^{m-m^\prime} p_A(m^\prime,t),
\end{split}
\end{align}
where $p_R(m,t)$ is the probability of the system having $m$ mRNA copies and
having repressor bound to the promoter at time $t$, and $p_A$ is an analogous
probability to find the promoter without repressor bound. $k_R+$ and $k_R^-$
are, respectively, the rates at which repressors bind and unbind to and from the
promoter, and $\gamma$ is the mRNA degradation rate. $k_i$ is the rate at which
bursts initiate, and as before, the geometric distribution of burst sizes has
mean $b=(1-\theta)/\theta$.

Interestingly, it turns out that this problem maps exactly onto the three-stage
promoter model considered by Shahrezaei and Swain in~\cite{Shahrezaei2008}, with
relabelings. Their approximate solution for protein distributions amounts to the
same approximation we make here in regarding the duration of mRNA synthesis
bursts as instantaneous, so their solution for protein distributions also solves
our problem of mRNA distributions. Let us examine the analogy more closely. They
consider a two-state promoter, as we do here, but they model mRNA as being
produced one at a time and degraded, with rates $v_0$ and $d_0$. Then they model
translation as occurring with rate $v_1$, and protein degradation with rate
$d_1$ as shown in Figure~\ref{fig:shahrezaei}. Now consider the limit where
$v_1, d_0\rightarrow\infty$ with their ratio $v_1/d_0$ held constant. $v_1/d_0$
resembles the average burst size of translation from a single mRNA: these are
the rates of two Poisson processes that compete over a transcript, which matches
the story of geometrically distributed burst sizes. In other words, in our
bursty promoter model we can think of the parameter $\theta$ as determining one
competing process to end the burst and $(1 - \theta)$ as a process wanting to
continue the burst. So after taking this limit, on timescales slow compared to
$v_1$ and $d_0$, it appears that transcription events fire at rate $v_0$ and
produce a geometrically distributed burst of translation of mean size $v_1/d_0$,
which intuitively matches the story we have told above for mRNA with variables
relabeled.

\begin{figure}
\centering
\includegraphics{../figures/si/figS0X_Shahrezaei_promoter.pdf}
\caption{\textbf{Schematic of three-stage promoter from~\cite{Shahrezaei2008}.}
Adapted from Shahrezaei \& Swain~\cite{Shahrezaei2008}. In their paper they
derive a closed form solution for the protein distribution. Our two-state bursty
promoter at the mRNA level can be mapped into their solution with some
relabeling.}
\label{fig:shahrezaei}
\end{figure}

To verify this intuitively conjectured mapping between our problem and the
solution in~\cite{Shahrezaei2008}, we continue with a careful solution for the
mRNA distribution using probability generating functions, following the ideas
sketched in~\cite{Shahrezaei2008}. It is natural to nondimensionalize rates in
the problem by $\gamma$, or equivalently, this amounts to measuring time in
units of $\gamma^{-1}$. We are also only interested in steady state, so we set
the time derivatives to zero, giving
\begin{align}
0 =& k_R^+ p_A(m) - k_R^- p_R(m) + (m+1) p_R(m+1) - m p_R(m)
\\
\begin{split}
0 =& - k_R^+ p_A(m) + k_R^- p_R(m) + (m+1) p_A(m+1) - m p_A(m)
\\
&- k_i p_A(m) + k_i \sum_{m^\prime=0}^m \theta(1-\theta)^{m-m^\prime} p_A(m^\prime),
\end{split}
\end{align}
where for convenience we kept the same notation for all rates, but these are
now expressed in units of mean mRNA lifetime $\gamma^{-1}$.

The probability generating function is defined as before in the constitutive
case, except now we must introduce a generating function for each promoter
state,
\begin{align}
f_A(z) = \sum_{m=0}^\infty z^m p_A(m),
\;
f_R(z) = \sum_{m=0}^\infty z^m p_R(m).
\end{align}
Our real objective is the generating function $f(z)$ that generates the mRNA
distribution $p(m)$, independent of what state the promoter is in. But since
$p(m) = p_A(m) + p_R(m)$, it follows too that $f(z) = f_A(z) + f_R(z)$.

As before we multiply both equations by $z^m$ and sum over all $m$. Each
individual term transforms exactly as did an analogous term in the constitutive
case, so the coupled ODEs for the generating functions read
\begin{align}
0 =& k_R^+ f_A(z) - k_R^- f_R(z) + \pderiv{z} f_R(z) - z \pderiv{z} f_R(z)
\\
\begin{split}
0 =&  - k_R^+ f_A(z) + k_R^- f_R(z) + \pderiv{z} f_A(z) - z \pderiv{z} f_A(z)
\\
&- k_i f_A(z) + k_i \frac{\theta}{1-z(1-\theta)} f_A(z),
\end{split}
\end{align}
and after changing variables $\xi = 1 - \theta$ as before and rearranging, we
have
\begin{align}
0 &= k_R^+ f_A(z) - k_R^- f_R(z) + (1-z) \pderiv{z} f_R(z)
\\
0 &=  - k_R^+ f_A(z) + k_R^- f_R(z) + (1 - z) \pderiv{z} f_A(z)
+ k_i \frac{(z-1)\xi}{1-z\xi} f_A(z),
\end{align}
We can transform this problem from two coupled first-order ODEs to a single
second-order ODE by solving for $f_A$ in the first and plugging into the second,
giving
\begin{align}
\begin{split}
0 = (1&-z) \pderiv[f_R]{z}
+ \frac{1-z}{k_R^+}
        \left(k_R^- \pderiv[f_R]{z} + \pderiv[f_R]{z} +(z-1) \psecderiv[f_R]{z}\right)
\\
&+ \frac{k_i}{k_R^+} \frac{(z-1)\xi}{1-z\xi}
        \left(k_R^- f_R + (z-1) \pderiv[f_R]{z}\right),
\end{split}
\end{align}
where, to reduce notational clutter, we have dropped the explicit $z$ dependence
of $f_A$ and $f_R$. Simplifying we have
\begin{align}
0 = \psecderiv[f_R]{z}
        - \left(\frac{k_i\xi}{1-z\xi}
                + \frac{1 + k_R^- + k_R^+}{1-z}
        \right)\pderiv[f_R]{z}
        + \frac{k_i k_R^- \xi}{(1-z\xi)(1-z)}f_R.
\end{align}
This can be recognized as the hypergeometric differential equation, with
singularities at $z=1$, $z=\xi^{-1}$, and $z=\infty$. The latter can be verified
by a change of variables from $z$ to $x=1/z$, being careful with the chain rule,
and noting that $z=\infty$ is a singular point if and only if $x=1/z=0$ is a
singular point.

The standard form of the hypergeometric differential equation has its
singularities at 0, 1, and $\infty$, so to take advantage of the standard form
solutions to this ODE, we first need to transform variables to put it into a
standard form. However, this is subtle. While any such transformation should
work in principle, the solutions are expressed most simply in the neighborhood
of $z=0$, but the normalization condition that we need to enforce corresponds to
$z=1$. The easiest path, therefore, is to find a change of variables that maps 1
to 0, $\infty$ to $\infty$, and $\xi^{-1}$ to 1. This is most intuitively done
in two steps.

First map the $z=1$ singularity to 0 by the change of variables $v=z-1$, giving
\begin{align}
0 = \psecderiv[f_R]{v}
        + \left(\frac{k_i\xi}{(1+v)\xi - 1}
                + \frac{1 + k_R^- + k_R^+}{v}
        \right)\pderiv[f_R]{v}
        + \frac{k_i k_R^- \xi}{((1+v)\xi - 1)v}f_R.
\end{align}
Now two singularities are at $v=0$ and $v=\infty$. The third is determined by
$(1+v)\xi -1 = 0$, or $v=\xi^{-1} - 1$. We want another variable change that
maps this third singularity to 1 (without moving 0 or infinity). Changing
variables again to $w=\frac{v}{\xi^{-1} - 1} = \frac{\xi}{1-\xi} v$ fits the
bill. In other words, the combined change of variables
\begin{align}
w = \frac{\xi}{1-\xi} (z-1)
\end{align}
maps $z = \{1, \xi^{-1}, \infty\}$ to $w =\{0, 1, \infty\}$ as desired. Plugging
in, being mindful of the chain rule and noting
$(1 + v)\xi - 1 = (1 - \xi)(w - 1)$ gives
\begin{align}
0 = \left(\frac{\xi}{1-\xi}\right)^2 \psecderiv[f_R]{w}
+ \left(
        \frac{\xi k_i}{(1-\xi)(w-1)} + \frac{\xi(1 + k_R^- + k_R^+)}{(1-\xi)w}
\right) \frac{\xi}{1-\xi} \pderiv[f_R]{w}
+ \frac{k_i k_R^- \xi^2}{(1-\xi)^2 w(w-1)}f_R.
\end{align}
This is close to the standard form of the hypergeometric differential equation,
and some cancellation and rearrangement gives
\begin{align}
0 = w(w-1)\psecderiv[f_R]{w}
+ \left(k_i w + (1 + k_R^- + k_R^+)(w-1)\right) \pderiv[f_R]{w}
+ k_i k_R^- f_R.
\end{align}
and a little more algebra produces
\begin{align}
0 = w(1-w)\psecderiv[f_R]{w}
+ \left(1 + k_R^- + k_R^+
        - (1 + k_i + k_R^- + k_R^+)w
\right) \pderiv[f_R]{w}
- k_i k_R^- f_R,
\end{align}
which is the standard form. From this we can read off the solution in terms of
hypergeometric functions ${_2F_1}$ from any standard source,
e.g.~\cite{Abramowitz1964}, and identify the conventional parameters in terms of
our model parameters. We want the general solution in the neighborhood of $w=0$
($z=1$), which for a homogeneous linear second order ODE must be a sum of two
linearly independent solutions. More precisely,
\begin{align}
f_R(w) = C^{(1)} {_2F_1}(\alpha, \beta, \delta; w)
+ C^{(2)} w^{1-\delta}{_2F_1}(1+\alpha-\delta, 1+\beta-\delta, 2-\delta; w)
\end{align}
with parameters determined by
\begin{align}
\begin{split}
\alpha\beta &= k_i k_R^-
\\
1+\alpha+\beta &= 1+k_i+k_R^-+k_R^+
\\
\delta &= 1 + k_R^- + k_R^+
\end{split}
\end{align}
and constants $C^{(1)}$ and $C^{(2)}$ to be set by boundary conditions. Solving for
$\alpha$ and $\beta$, we find
\begin{align}
\begin{split}
\alpha &= \frac{1}{2}
\left(k_i+k_R^-+k_R^+ + \sqrt{(k_i+k_R^-+k_R^+)^2 - 4k_i k_R^-}\right)
\\
\beta &= \frac{1}{2}
\left(k_i+k_R^-+k_R^+ - \sqrt{(k_i+k_R^-+k_R^+)^2 - 4k_i k_R^-}\right)
\\
\delta &= 1 + k_R^- + k_R^+.
\end{split}
\end{align}
Note that $\alpha$ and $\beta$ are interchangeable in the definition of
${_2F_1}$ and differ only in the sign preceeding the radical.
Since the normalization condition requires that $f_R$ be finite at $w=0$,
we can immediately set $C^{(2)}=0$ to discard the second solution.
This is because all the rate constants are strictly positive,
so $\delta>1$ and therefore $w^{1-\delta}$ blows up as $w\rightarrow0$.
Now that we have $f_R$, we would like to find the generating function
for the mRNA distribution, $f(z) = f_A(z) + f_R(z)$.
We can recover $f_A$ from our solution for $f_R$, namely
\begin{align}
f_A(z) = \frac{1}{k_R^+}\left(k_R^- f_R(z) + (z-1) \pderiv[f_R]{z}\right)
\end{align}
or
\begin{align}
f_A(w) = \frac{1}{k_R^+}\left(k_R^- f_R(w) + w \pderiv[f_R]{w}\right),
\end{align}
where in the second line we transformed our original relation between
$f_R$ and $f_A$ to our new, more convenient, variable $w$.
Plugging our solution for $f_R(w) = C^{(1)}{_2F_1}(\alpha, \beta, \delta; w)$
into $f_A$, we will require the differentiation rule for ${_2F_1}$,
which tells us
\begin{align}
\pderiv[f_R]{w} = C^{(1)}\frac{\alpha\beta}{\delta}
                {_2F_1}(\alpha+1, \beta+1, \delta+1; w),
\end{align}
from which it follows that
\begin{align}
f_A(w) = \frac{C^{(1)}}{k_R^+}
\left(
k_R^- {_2F_1}(\alpha, \beta, \delta; w)
+ w\frac{\alpha\beta}{\delta} {_2F_1}(\alpha+1, \beta+1, \delta+1; w)
\right)
\end{align}
and therefore
\begin{align}
f(w) = C^{(1)}\left(1 + \frac{k_R^-}{k_R^+}\right)
        {_2F_1}(\alpha, \beta, \delta; w)
+ w \frac{C^{(1)}}{k_R^+} \frac{\alpha\beta}{\delta}
        {_2F_1}(\alpha+1, \beta+1, \delta+1; w).
\end{align}
To proceed, we need one of the (many) useful identities known for
hypergeometric functions, in particular
\begin{align}
w\frac{\alpha\beta}{\delta} {_2F_1}(\alpha+1, \beta+1, \delta+1; w)
=
(\delta-1)\left(
{_2F_1}(\alpha, \beta, \delta-1; w) - {_2F_1}(\alpha, \beta, \delta; w)
\right).
\end{align}
Substituting this for the second term in $f(w)$, we find
\begin{align}
f(w) = \frac{C^{(1)}}{k_R^+}
\left[
        \left(k_R^+ + k_R^-\right)
        {_2F_1}(\alpha, \beta, \delta; w)
+ (\delta-1)\left(
        {_2F_1}(\alpha, \beta, \delta-1; w) - {_2F_1}(\alpha, \beta, \delta; w)
        \right)
\right],
\end{align}
and since $\delta-1 = k_R^+ + k_R^-$, the first and third terms cancel,
leaving only
\begin{align}
f(w) = C^{(1)}\frac{k_R^+ + k_R^-}{k_R^+} {_2F_1}(\alpha, \beta, \delta-1; w).
\end{align}
Now we enforce normalization, demanding $f(w=0) = f(z=1) = 1$.
${_2F_1}(\alpha, \beta, \delta-1; 0) = 1$, so we must have
$C^{(1)} = k_R^+ / (k_R^+ + k_R^-)$ and consequently
\begin{align}
f(w) =  {_2F_1}(\alpha, \beta, k_R^+ + k_R^-; w).
\end{align}
Recalling that the mean burst size $b = (1-\theta)/\theta = \xi/(1-\xi)$
and $w = \frac{\xi}{1-\xi} (z-1) = b (z-1)$,
we can transform back to the original variable $z$ to find the tidy result
\begin{align}
f(z) =  {_2F_1}(\alpha, \beta, k_R^+ + k_R^-; b(z-1)),
\end{align}
with $\alpha$ and $\beta$ given above by
\begin{align}
\begin{split}
\alpha &= \frac{1}{2}
\left(k_i+k_R^-+k_R^+ + \sqrt{(k_i+k_R^-+k_R^+)^2 - 4k_i k_R^-}\right)
\\
\beta &= \frac{1}{2}
\left(k_i+k_R^-+k_R^+ - \sqrt{(k_i+k_R^-+k_R^+)^2 - 4k_i k_R^-}\right).
\end{split}
\end{align}
Finally we are in sight of the original goal. We can generate the steady-state
probability distribution of interest by differentiating the generating function,
\begin{align}
p(m) = m! \left.\frac{\partial^m}{\partial z^m} f(z) \right|_{z=0},
\end{align}
which follows easily from its definition. Some contemplation reveals that
repeated application of the derivative rule used above will produce products of
the form $\alpha(\alpha+1)(\alpha+2)\cdots(\alpha+m-1)$ in the expression for
$p(m)$ and similarly for $\beta$ and $\delta$. These resemble ratios of
factorials, but since $\alpha$, $\beta$, and $\delta$ are not necessarily
integer, we should express the ratios using gamma functions instead. More
precisely, one finds
\begin{align}
p(m) = \frac{
        \Gamma(\alpha + m)\Gamma(\beta + m)\Gamma(k_R^+ + k_R^-)
        }
        {
        \Gamma(\alpha)\Gamma(\beta)\Gamma(k_R^+ + k_R^- + m)
        }
\frac{b^m}{m!}{_2F_1}(\alpha+m, \beta+m, k_R^++k_R^-+m; -b)
\label{eq:p_m_bursty+rep_appdx}
\end{align}
which is finally the probability distribution we sought to derive.

\subsection{Numerical considerations and recursion formulas}
\subsubsection{Generalities}
We would like to carry out Bayesian parameter inference on FISH data
from~\cite{Jones2014}, using~\eq{eq:p_m_bursty+rep_appdx} as our
likelihood. This requires accurate (and preferably fast)
numerical evaluation of the hypergeometric function ${_2F_1}$,
which is a notoriously hard problem~\cite{Pearson2017, Gil2007},
and our particular needs here present an especial challenge as we show below.

The hypergeometric function is defined by its Taylor series as
\begin{align}
{_2F_1}(a,b,c;z)
= \sum_{l=0}^\infty
\frac{\Gamma(a + l)\Gamma(b + l)\Gamma(c)}
        {\Gamma(a)\Gamma(b)\Gamma(c + l)}
\frac{z^l}{l!}
\end{align}
for $|z|<1$, and by analytic continuation elsewhere.
If $z\lesssim1/2$ and $\alpha$ and $\beta$ are not too large
(absolute value below 20 or 30),
then the series converges quickly and an accurate numerical representation is
easily computed by truncating the series after a reasonable number of terms.
Unfortunately, we need to evaluate ${_2F_1}$ over mRNA copy numbers fully out
to the tail of the distribution, which can easily reach 50, possibly 100.
From~\eq{eq:p_m_bursty+rep_appdx}, this means evaluating ${_2F_1}$
repeatedly for values of $a$, $b$, and $c$ spanning the full range
from $\mathcal{O}(1)$ to $\mathcal{O}(10^2)$,
even if $\alpha$, $\beta$, and $\delta$
in~\eq{eq:p_m_bursty+rep_appdx} are small,
with the situation even worse if they are not small.
A naive numerical evaluation of the series definition will be
prone to overflow and, if any of $a,b,c<0$, then some successive terms in the
series have alternating signs which can lead to catastrophic cancellations.

One solution is to evaluate ${_2F_1}$ using arbitrary precision arithmetic
instead of floating point arithmetic,
e.g., using the \texttt{mpmath} library in Python.
This is accurate but incredibly slow computationally.
To quantify how slow, we found that
evaluating the likelihood defined by~\eq{eq:p_m_bursty+rep_appdx} $\sim50$ times
(for a typical dataset of interest from~\cite{Jones2014},
with $m$ values spanning 0 to $\sim50$)
using arbitrary precision arithmetic is 100-1000 fold slower than
evaluating a negative binomial likelihood for the corresponding
constitutive promoter dataset.

To claw back $\gtrsim30$ fold of that slowdown, we can exploit
one of the many catalogued symmetries involving ${_2F_1}$.
The solution involves recursion relations originally explored by Gauss,
and studied extensively in~\cite{Pearson2017, Gil2007}.
They are sometimes known as contiguous relations and relate the values
of any set of 3 hypergeometric functions whose arguments differ by integers.
To rephrase this symbolically, consider a set of hypergeometric functions
indexed by an integer $n$,
\begin{align}
f_n = {_2F_1}(a+\epsilon_i n, b+\epsilon_j n, c+\epsilon_k n; z),
\end{align}
for a fixed choice of $\epsilon_i, \epsilon_j, \epsilon_k \in \{0,\pm 1\}$
(at least one of $\epsilon_i, \epsilon_j, \epsilon_k$ must be nonzero,
else the set of $f_n$ would contain only a single element).
Then there exist known recurrence relations of the form
\begin{align}
A_n f_{n-1} + B_n f_{n} + C_n f_{n+1} = 0,
\end{align}
where $A_n, B_n$, and $C_n$ are some functions of $a,b,c$, and $z$.
In other words, for fixed $\epsilon_i, \epsilon_j, \epsilon_k, a, b,$ and $c$,
if we can merely evaluate ${_2F_1}$ twice, say for $n^\prime$ and $n^\prime-1$,
then we can easily and rapidly generate values for arbitrary $n$.

This provides a convenient solution for our problem: we need repeated
evaluations of ${_2F_1}(a+m, b+m, c+m; z)$
for fixed $a,b$, and $c$ and many integer values of $m$.
They idea is that we can use arbitrary precision arithmetic to evaluate
${_2F_1}$ for just two particular values of $m$ and then generate
${_2F_1}$ for the other 50-100 values of $m$ using the recurrence
relation.
In fact there are even more sophisticated ways of utilizing the recurrence
relations that might have netted another factor of 2 speed-up, and
possibly as much as a factor of 10, but the method described here had
already reduced the computation time to an acceptable
$\mathcal{O}(\text{1 min})$, so these more sophisticated approaches did
not seem worth the time to pursue.

However, there are two further wrinkles.
The first is that
a naive application of the recurrence relation is numerically unstable.
Roughly, this is because the three term recurrence relations,
like second order ODEs, admit two linearly independent solutions.
In a certain eigenbasis, one of these solutions dominates the other
as $n\rightarrow\infty$, and as $n\rightarrow-\infty$,
the dominance is reversed.
If we fail to work in this eigenbasis, our solution of the recurrence relation
will be a mixture of these solutions and rapidly accumulate numerical error.
For our purposes, it suffices to know that the authors of~\cite{Gil2007}
derived the numerically stable solutions (so-called \textit{minimal solutions})
for several possible choices of $\epsilon_i, \epsilon_j, \epsilon_k$.
Running the recurrence in the proper direction using a minimal solution
is numerically robust and can be done entirely in floating point arithmetic,
so that we only need to evaluate ${_2F_1}$ with arbitrary precision arithmetic
to generate the seed values for the recursion.

The second wrinkle is a corollary to the first.
The minimal solutions are only minimal for certain ranges of the argument $z$,
and not all of the 26 possible recurrence relations
have minimal solutions for all $z$.
This can be solved by using one of the many transformation formulae for
${_2F_1}$ to convert to a different recurrence relation that has
a minimal solution over the required domain of $z$, although
this can require some trial and error to find the right transformation,
the right recurrence relation, and the right minimal solution.

\subsubsection{Particulars}
Let us now demonstrate these generalities for our problem of interest.
In order to evaluate the probability distribution of our
model,~\eq{eq:p_m_bursty+rep_appdx}, we need to evaluate hypergeometric functions
of the form ${_2F_1}(\alpha+m, \beta+m, \delta+m; -b)$
for values of $m$ ranging from $0$ to $\mathcal{O}(100)$.
The authors of~\cite{Gil2007} did not derive a recursion relation
for precisely this case. We could follow their methods and do so ourselves,
but it is much easier to convert to a case that they did consider.
The strategy is to look through the minimal solutions tabulated
in~\cite{Gil2007} and search for a transformation we could apply to
${_2F_1}(\alpha+m, \beta+m, \delta+m; -b)$ that would place the $m$'s
(the variable being incremented by the recursion)
in the same arguments of ${_2F_1}$ as the minimal solution.
After some ``guess and check,'' we found that the transformation
\begin{align}
{_2F_1}(\alpha+m, \beta+m, \delta+m; -b)
=
(1+b)^{-\alpha-m}
        {_2F_1}\left(\alpha+m, \delta-\beta, \delta+m; \frac{b}{1+b}\right),
\label{eq:rec_euler_pretransform}
\end{align}
produces a ${_2F_1}$ on the right hand side that closely resembles
the minimal solutions $y_{3,m}$ and $y_{4,m}$ in Eq.~4.3 in~\cite{Gil2007}.
Explicitly, these solutions are
\begin{align}
y_{3,m}
&\propto
{_2F_1}\left(-\alpha^\prime + \delta^\prime - m,
                -\beta^\prime + \delta^\prime,
                1-\alpha^\prime-\beta^\prime+\delta^\prime-m;
                1-z\right)
\\
y_{4,m}
&\propto
{_2F_1}\left(\alpha^\prime + m,
                \beta^\prime,
                1+\alpha^\prime+\beta^\prime-\delta^\prime+m;
                1-z\right),
\label{eq:minimal_soln_sans_prefac}
\end{align}
where we have omitted prefactors which are unimportant for now.
Which of these two we should use depends on what values $z$ takes on.
Equating $1-z=b/(1+b)$ gives $z=1/(1+b)$, and since $b$ is strictly positive,
$z$ is bounded between 0 and 1.
From Eq.~4.5 in~\cite{Gil2007}, $y_{4,m}$ is the minimal solution
for real $z$ satisfying $0<z<2$, so this is the only minimal solution we need.

Now that we have our minimal solution,
what recurrence relation does it satisfy?
Confusingly, the recurrence relation of which $y_{4,m}$ is a solution
increments different arguments of ${_2F_1}$ that does $y_{4,m}$:
it increments the first only, rather than first and third.
This recurrence relation can be looked up, e.g., Eq.~15.2.10
in~\cite{Abramowitz1964}, which is
\begin{align}
(\delta^\prime - (\alpha^\prime + m)) f_{m-1}
+
(2(\alpha^\prime+m) - \delta^\prime + (\beta^\prime - \alpha^\prime)z)f_m
+ \alpha^\prime(z-1) f_{m+1} = 0.
\label{eq:chosen_rec_rel}
\end{align}
Now we must solve for the parameters appearing in the recurrence relation
in terms of our parameters, namely by setting
\begin{align}
\begin{split}
\alpha^\prime &= \alpha
\\
\beta^\prime &= \delta - \beta
\\
1 + \alpha^\prime + \beta^\prime - \delta^\prime &= \delta
\\
1 - z &= \frac{b}{1+b}
\end{split}
\end{align}
and solving to find
\begin{align}
\begin{split}
\alpha^\prime &= \alpha
\\
\beta^\prime &= \delta - \beta
\\
\delta^\prime &= 1 + \alpha - \beta
\\
z &= \frac{1}{1+b}
.
\end{split}
\end{align}
Finally we have everything we need. The minimal solution
\begin{align}
y_{4,m}
=
\frac{\Gamma(1+\alpha^\prime-\delta^\prime+m)}
        {\Gamma(1+\alpha^\prime+\beta^\prime-\delta^\prime+m)}
\times
{_2F_1}\left(\alpha^\prime + m,
                \beta^\prime,
                1+\alpha^\prime+\beta^\prime-\delta^\prime+m;
                1-z\right),
\end{align}
where we have now included the necessary prefactors,
is a numerically stable solution of the recurrence
relation~\eq{eq:chosen_rec_rel} if the recursion is run
from large $m$ to small $m$.

Let us finally outline the complete procedure as an algorithm to be implemented:
\begin{enumerate}
\item Compute the value of ${_2F_1}$ for the two
largest $m$ values of interest using arbitrary precision arithmetic.
\item Compute the prefactors to construct
$y_{4,\text{max}(m)}$ and $y_{4,\text{max}(m)-1}$.
\item Recursively compute $y_{4,m}$ for all $m$ less than $\text{max}(m)$ down
to $m=0$.
\item Cancel off the prefactors of the resulting values of
$y_{4,m}$ for all $m$ to produce ${_2F_1}$ for all desired $m$ values.
\end{enumerate}

With ${_2F_1}$ computed, the only remaining numerical danger in computing
$p(m)$ in~\eq{eq:p_m_bursty+rep_appdx} is overflow of the gamma functions.
This is easily solved by taking the log of the entire expression
and using standard routines to compute the log of the gamma functions,
then exponentiating the entire expression at the end if $p(m)$
is needed rather than $\log p(m)$.