\documentclass[]{statsoc}
\usepackage{enumitem}
\usepackage{natbib}
\usepackage{setspace}
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   % ... or a4paper or a5paper or ...
%\geometry{landscape}                % Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{epstopdf}
\usepackage[linesnumbered,vlined,algoruled]{algorithm2e}
\usepackage{url}
\usepackage{mathtools}
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator{\argmin}{arg\,min}
\setlength{\parskip}{0.5 em}

\title[Accurate Quantiles Using $t$-Digests]{Computing Extremely Accurate Quantiles Using $t$-Digests}

\author[Dunning \it{et al.}]{Ted Dunning\thanks{Corresponding author}}
\address{MapR Technologies, Inc \\ Santa Clara, CA}
\email{ted.dunning@gmail.com}
\author[Dunning and Ertl]{Otmar Ertl}
\address{Dynatrace\\Linz, Austria}

\email{otmar.ertl@gmail.com}
\date{}                                           % Activate to display a given date or no date
\begin{document}
\begin{abstract}
We present on-line algorithms for computing approximations of rank-based statistics that give high accuracy, particularly near the tails of a distribution, with very small sketches.  Notably, the method allows a quantile $q$ to be computed with an accuracy relative to $\max(q, 1-q)$ rather than absolute accuracy as with most other methods.  This new algorithm is robust with respect to skewed distributions or ordered datasets and allows separately computed summaries to be combined with no loss in accuracy.

An open-source Java implementation of this algorithm is available from the author. Independent implementations in Go and Python are also available.

% Please include a maximum of seven keywords
\keywords{quantiles, median, rank statistics, t-digest}
\end{abstract}
\maketitle
\section{Introduction}
Given a set of numbers, it is often desirable to compute rank-based statistics such as the median, 95-th percentile or trimmed means in an on-line fashion without having to retain all of the samples or to wait until all data has been processed. In many cases, there is an additional requirement that only a small data structure needs to be kept in memory as data is processed in a streaming fashion.  Traditionally, such statistics have been computed by sorting the data and then either finding the quantile of interest by interpolation or by re-processing all samples within particular quantile ranges.  This sorting approach can be infeasible for very large datasets or when quantiles of many subsets must be calculated. This infeasibility has led to interest in on-line approximate algorithms. Previous algorithms can compute approximate values of quantiles using constant or only weakly increasing memory footprint, but these previous algorithms cannot provide constant relative accuracy.  The new algorithm described here, the $t$-digest, provides constant memory bounds and constant relative accuracy while operating in a strictly on-line fashion.

\subsection{Previous work}

One early algorithm for computing on-line quantiles is described by \citet{Chen2000}.  In that work specific quantiles were computed by incrementing or decrementing an estimate by a value proportional to the simultaneously estimated probability density at the desired quantile.  This method is plagued by a circularity in that estimating density is only possible by estimating yet more quantiles.  Moreover, this work did not allow the computation of hybrid quantities such as trimmed means.

\citet{munro1980} provided an alternative algorithm to get a precise estimate of the median.  This is done by keeping $s$ samples from the $N$ samples seen so far where $s << N$ by the time the entire data set has been seen.  If the data are presented in random order and if $s = \theta(N^{1/2} \log N)$, then Munro and Paterson's algorithm has a high probability of being able to retain a set of samples that contains the median.  This algorithm can be adapted to find a number of pre-specified quantiles at the same time at proportional cost in memory.  The memory consumption of Munro-Paterson algorithm is, however, excessive if precise results are desired.  Approximate results can be had with less memory, however.

A more subtle problem is that the implementation of Munro and Paterson's algorithm in Sawzall\citep{sawzall} and the Datafu library\citep{datafu} uses a number of buckets computed from the GCD of the desired quantiles.  This means that if you want to compute the $99$-th, $99.9$-th and $99.99$-th percentiles, ten thousand buckets are required, each of which requires the retention of many samples. We will refer to this implementation of Munro and Paterson's algorithm as MP01 in results presented here.

One of the most important results of the work by Munro and Paterson was a proof that computing any particular quantile exactly in $p$ passes through the data requires $\Omega(N^{1/p})$ memory. For the on-line case, $p=1$, which implies that on-line algorithms cannot guarantee to produce the precise value of any particular quantile. This result together with the importance of the on-line case drove subsequent work to focus on algorithms to produce approximate values of quantiles.

\citet{Greenwald-space-efficient-online-quantiles} provided just such an approximation algorithm that is able to provide estimates of quantiles with controllable accuracy. This algorithm (which we shall refer to as GK01 subsequently in this paper) requires less memory than Munro and Paterson's algorithm and provides approximate values for pre-specified quantiles.

An alternative approach is described by  \citet{qdigest}.  In this work, incoming values are assumed to be integers of fixed size. Such integers can trivially be arranged in a perfectly balanced binary tree where the leaves correspond to the integers and the interior nodes correspond to bit-wise prefixes. This tree forms the basis of the data structure known as a Q-digest.  The idea behind a Q-digest is that in the uncompressed case, counts for various values are assigned to leaves of the tree.  To compress this tree, sub-trees are collapsed and counts from the leaves are aggregated into a single node representing the sub-tree such that the maximum count for any collapsed sub-tree is less than a threshold that is a small fraction of the total number of integers seen so far.  Any quantile can be computed by traversing the tree in left prefix order, adding up counts until the desired fraction of the total is reached.  At that point, the count for the last sub-tree traversed can be used to interpolate to the desired quantile within a small and controllable error.  The error is bounded because the count for each collapsed sub-tree is bounded.

The salient virtues of the Q-digest are
\begin{itemize}[nosep, topsep=-10pt]
\item the space required is bounded proportional to a compression factor $k$
\item the maximum error of any quantile estimate is proportional to $1/k$ and
\item the desired quantiles do not have to be specified in advance.
\vspace{10pt}
\end{itemize}

On the other hand, two problems with the Q-digest are that it depends on the set of possible values being known in advance and produces quantile estimates with constant error in $q$. In practice, this limits application of the Q-digest to samples which can be identified with the integers. Adapting the Q-digest to use a balanced tree over arbitrary elements of an ordered set is difficult.  This difficulty arises because rebalancing the tree involves sub-tree rotations and these rotations may require reapportionment of previously collapsed counts in complex ways.  This reapportionment could have substantial effects on the accuracy of the algorithm and in any case make the implementation much more complex because the concerns of counting cannot be separated from the concerns of maintaining a balanced tree.
\subsection{New contributions}
The work described here introduces a new data structure known as the $t$-digest which is formed by clustering real-valued samples. The $t$-digest differs from more well-known forms of clustering such as $k$-means in that the samples are from $\mathbb R^1$ rather than taken from any space with a metric and because the clusters are limited by size in a special way. The $t$-digest differs from previous structures designed for computing approximate quantiles in several important respects. First, although data is clustered and summarized in the $t$-digest, the range of data included in different clusters may overlap. Second, the bins are summarized by a centroid value and an accumulated weight representing the number of samples contributing to a bin rather than by the upper and lower bounds of the bin. Third, the samples are accumulated in such a way that only a few samples contribute to bins corresponding to extreme quantiles so that relative error is bounded instead of maintaining constant absolute error as with previous methods.

With the $t$-digest, accuracy for estimating the $q$ quantile is nearly constant relative to $q(1-q)$.  This is in contrast to earlier algorithms which had errors independent of $q$.  The relative error bound of the $t$-digest is convenient when computing quantiles for $q$ near $0$ or $1$ as is commonly required.  As with the Q-digest algorithm, the accuracy/size trade-off for the $t$-digest can be controlled by setting a single compression parameter $\delta$ with the amount of memory required proportional only to $\Theta(\delta)$.

\section{The $t$-digest}
A $t$-digest is generated by clustering real-valued samples and retaining the mean and number of samples for each cluster. This clustering can then be used to estimate quantile-related statistics with particularly high accuracy near the tails of a distribution. Algorithmically, there are two important ways to form a $t$-digest from a set of numbers. One version keeps a buffer of incoming samples. When the buffer fills, the contents are sorted and merged with the centroids computed from previous samples. This merging form of the $t$-digest algorithm has the virtue of allowing all memory structures to be allocated statically. On an amortized basis, this buffer-and-merge algorithm can be very fast especially if the input buffer is large. The other major $t$-digest algorithm is more akin to traditional clustering algorithms where new samples are added one at a time to whichever cluster is nearest.  Both algorithms are described here and implementations for reference implementations of both are available as open source software.

\subsection{The basic concept}
Take a $n$ samples $\lbrace x_1 \ldots x_n \rbrace = X \subset \mathbb R^1$. Define a digest  as a partition of $X$ consisting of disjoint  sets of samples $\pi_i \subset X$. The subsets $\pi_i$  in this partition are referred to as clusters. The number of samples in a cluster $\mathcal C$ is written as $|\mathcal C|$ and is typically referred to as the weight of the cluster.

Each cluster $\mathcal C$ in a digest has $|C|>0$ samples and an associated mean $\overline {\mathcal C} = \sum_{x\in \mathcal C} x / |\mathcal C|$. The clusters in the digest can be partially ordered according to their means. Clusters with identical means are assigned an arbitrary fixed ordering so that we can index the clusters consistently. Given this complete ordering, we define a left and right weight for each cluster $\mathcal C_i$ as the sum of the weights of clusters to the left and right of $\mathcal C_i$. That is
\begin{align*}
\mathcal W_{\mathrm{left}}(\mathcal C_i) = \sum_{j < i} |C_j| \\
\mathcal W_{\mathrm{right}}(\mathcal C_i) = \sum_{j > i} |C_j|
\end{align*}

Note that so far we have no constraint about the ordering of the elements in different point sets. We refer to a digest as strongly ordered if
\begin{equation}\label{eq:weak-ordering}
 i > j \implies x \ge y \mathrm{\,for\,} x \in \pi_i \mathrm{\,and\,} y \in \pi_j
 \end{equation}
. We refer to a digest as weakly ordered if
\begin{equation}\label{eq:strong-ordering}
 i > j+\Delta \implies x \ge y \mathrm{\,for\,} x \in \pi_i \mathrm{\,and\,} y \in \pi_j
 \end{equation}
for some positive offset $\Delta\ge 1$.

Such a digest is a $t$-digest if every cluster has unit weight, or has weight bounded using a scale function as defined below. A $t$-digest is called fully merged if no two consecutive clusters can be combined without violating the weight bound.

\subsection{A simple start}

Suppose that all of the samples $X=x_1 \ldots x_n$ are presented in ascending order with ties broken arbitrarily. Since the samples are ordered, we can use the index of each sample to determine the value of the empirical quantile for any new value.

If we form a trivial digest where each cluster has a single point, this digest will be a strongly ordered $t$-digest, but will likely not be fully merged.

We can group consecutive samples greedily from left to right into sub-sequences of consecutive samples, $X = \lbrace \pi_1 | \pi_2 | \ldots | \pi_m \rbrace$ where $\pi_i = \lbrace x_{\mathtt {left}(i)} \ldots x_{\mathtt{right}(i)} \rbrace$ such that each $\pi_i$ has as many samples as possible subject to the size bound. The result is a $t$-digest that is also strongly ordered and fully merged.

One key idea of the $t$-digest is that the size of each cluster is chosen so that it is small enough to get accurate quantile estimates by interpolation, but large enough so that we don't wind up with too many clusters. Importantly, we force clusters near both ends to be small while allowing sub-sequences in middle to be larger in order to get fairly constant relative accuracy. The other key idea is that there are practical algorithms that allow us to build $t$-digests incrementally, and although the result may not quite be strongly ordered, it will be close enough to be very useful.

\subsection{The size bound for clusters}
The size bound for $t$-digests is imposed using a scale function that forces clusters near the beginning or end of the digest to be small, possibly containing only a single sample. The scale function is chosen to provide an appropriate trade-off between very accurate quantile estimate in the tails of a distribution, reasonable accuracy near the median while keeping the number of clusters as small as possible.

To limit cluster size in this way, we define the scale function as a non-decreasing function from quantile $q$ to a notional index $k$ with compression parameter $\delta$. One commonly used  scale function for the $t$-digest is
\begin{equation}
k_1(q) = \frac \delta {2\pi}  { {\sin^{-1} (2q-1)} }
\end{equation}
As with any scale function, $k_1$ is non-decreasing. It has minimum value  $k(0)=-\delta/4$ and maximum value  $k(1)=\delta/4$.  Figure \ref{fig:k-q-plot} shows the relationship between $k$ and $q$ for $\delta=10$.
\begin{figure}[htbp] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=2.5in]{figures/k-q-plot.pdf}
   \caption{The scale function translates the quantile $q$ to the scale factor $k$ in order to give variable size steps in $q$. Limiting cluster sizes allows better accuracy near $q=0$ or $q=1$. }
   \label{fig:k-q-plot}
\end{figure}
In this figure, the horizontal lines are spaced uniformly at integer values of $k$. Vertical lines are drawn from the intersection of the scale function with these evenly spaced horizontals. Note how much closer together these vertical lines are near $q=0$ and $q=1$.

The scale function provides the necessary mechanism to define the size bound of a $t$-digest. Every cluster $\mathcal C$ with more than one sample should have the $k$-size (written as $|\mathcal C|_k$) at most $1$,
\begin{equation}
\label{eq:bounded-size}
|\mathcal C|_k = k \left (q_{\mathrm {right}} \right) - k \left ( q_{\mathrm {left}} \right) \le 1
\end{equation}
where
\begin{align*}
q_{\mathrm {left}} &= {\mathcal W}_{\mathrm{left}}(\mathcal C)/n \\
q_{\mathrm {right}} &= q_{\mathrm {left}} + {| \mathcal C | / n}
\end{align*}
In a fully merged $t$-digest, adjacent clusters cannot be merged because the result would be too big
\begin{equation}
\label{eq:fully-merged}
|\mathcal C_i \cup \mathcal C_{i+1} |_k = |\mathcal C_i|_k + |\mathcal C_{i+1}|_k > 1
\end{equation}
Together, the conditions expressed in equations \ref{eq:bounded-size} and \ref{eq:fully-merged} imply that for a fully merged $t$-digest using $k_1$ with $n \gg \delta$ samples, the number of clusters $m$ will be at most $ \left \lceil\delta \,\right \rceil $.  The lower bound  on the number of clusters is near $ \lfloor\delta/2\rfloor $.

\subsection{The effect of a scale function}

The only really necessary characteristic of a scale function up to now is that be non-decreasing. As such,  a linear scale function $k_0(q) = \delta q/2$ can be used instead of  $k_1$. Using a linear scale function results in nearly uniform cluster sizes and constant absolute error in $q$  which is similar to the behavior of most previously reported quantile approximation algorithms.

Figure \ref{fig:linear-interpolation} shows how  estimating $q$ near tails is improved for a strongly ordered, fully merged $t$-digest constructed with a scale function that keeps clusters small near extreme values of $q$.
\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[height=2.in, clip]{figures/linear-interpolation.pdf}
   \caption{The left panel shows linear interpolation of the cumulative distribution function near $q=0$ with $100$ equal sized bins applied to $10,000$ data points sampled from an exponential distribution. The right panel shows the same interpolation with variable size bins as given by a strongly ordered $t$-digest with $\delta=100$. The numbers above the data points represent the number of points in each bin. }
   \label{fig:linear-interpolation}
\end{figure}
This figure shows roughly the first percentile of 10,000 data points sampled using $x \sim \log u$ where $u \sim \mathrm{Uniform}(0,1)$. In the left panel, the data points have been divided into 100 clusters, each with 100 data points, of which only the left-most bin is visible. The use of equal sized bins means that the interpolated value of $q$ in the left panel of the figure has a substantial and unavoidable error. The right panel, on the other hand, shows a $t$-digest with roughly the same number of bins ($102$ instead of $100$), but with many fewer points in bins near  $q=0$.

Of course, since every sample must be in some bin, filling some bins with fewer than $100$ samples requires that other bins must have more than $100$ samples or we have to have more bins. Note, however, that having fewer samples in the bins near $q=0$ or $q=1$ improves accuracy substantially, while increasing the bin sizes near $q=1/2$ degrades accuracy only modestly. In this particular example, the first bin has only 2 samples and thus zero error and the second bin has only 10 samples giving much smaller error than a cluster of $100$ samples would give. The clusters near $q=1/2$, on the other hand, have about $1.6$ times more samples than the uniform case, increasing errors by only a modest factor relative to equal sized bins. The overall effect is that quantile estimation accuracy is dramatically improved at the extremes but only modestly impaired near the median. Exactly how much accuracy at the tails is improved depends crucially on how close the $t$-digest is to being strictly ordered.

\subsection{Merging independent $t$-digests}
In the previous section, the algorithm for forming a $t$-digest took a set of samples as inputs. Nothing, however, would prevent the algorithm from being applied to a set of weighted samples. As long as individual weights are smaller than the size limit imposed by the scale function, the result can still be a well-formed $t$-digest.

If we form independent $t$-digests $t_X$ and $t_Y$ from separate sequences $X$ and $Y$, these $t$-digests can clearly be used to estimate quantiles of $X \cup Y$ by separately computing quantiles for $X$ and $Y$ and combining the results. It is much more useful, however, if we choose the scale factor so that it guarantees that the ordered union of two $t$-digests will still be a valid $t$-digest. The scale factors $k_0$ and $k_1$ both have this property as do the $k_2$ and $k_3$ scale functions described later, but there is no guarantee that a candidate scale function will have this property.

We can force a  digest formed by merging other digests to be fully merged by combining  centroids wherever consecutive clusters taken together meet the size bound. The resulting $t$-digest will not necessarily be the same as if we had computed a $t$-digest $t_{X \cup Y}$ from all of the original data at once even though it will meet the same size constraint. In particular, even if $t_X$ and $t_Y$ are individually strongly ordered, their union $t_X \cup t_Y$ will, in general, only be weakly ordered. This can happen, for instance, when there are centroids in very nearly the same position in the two digests being merged. This loss of strictly ordering makes it difficult to compute rigorous error bounds.

In practice, $t$-digests formed by merging other digests still do produce accurate quantile estimates, even with highly structured data sets such as ordered data or data with large numbers of repeated values, but we do not yet have a good understanding how weak the ordering is in merged digests. The section on Empirical Results examines how these issues play.

The ability to merge $t$-digests makes parallel processing of large data-sets relatively simple since independent $t$-digests can be formed from disjoint partitions of the input data and then combined to get a $t$-digest representing the complete data-set.

A similar divide and conquer strategy can be used to allow $t$-digests to be used in OLAP systems. The idea is that queries involving quantiles of subsets of data can be approximated quickly by breaking the input data set into subsets corresponding to each unique combination of selection factors. A single $t$-digest is then pre-computed for each unique combination of selection factors. To the extent that all interesting queries can be satisfied by disjoint unions of such primitive subsets, the corresponding $t$-digests can be combined to compute the desired result.

\subsection{Progressive merging algorithm}
The observation that merging $t$-digests gives good accuracy suggests a practical algorithm for constructing a $t$-digest from a large amount of data. The basic idea is to collect data in a buffer. When the buffer fills, or when a final result is required, sort the buffer contents together with any previously created centroids, and make a single pass to merge points or centroids  whenever the size limits are satisfied by the combination. With an arbitrarily large buffer, this algorithm reduces to the original approach for constructing a $t$-digest from sorted data since the overall effect is simply a single merging pass through all the data. For smaller buffer sizes, however, many merge passes are required to process a large amount of data.

The algorithm for merging a buffer's worth of samples into an existing set of centroids is shown as Algorithm \ref{alg:merge}. Note that the check on the size bound is optimized to only require only as many evaluations of $k$ and $k^{-1}$ during the merge as there are output values in $C'$. This is done by computing the bounding value $q_{\mathrm {limit}} = k^{-1}(k(q) + 1)$ each time a new centroid is emitted. This allows the conditional to be couched in terms of comparisons of $q$ so that if many points are merged, no additional calls to $k$ are needed. If $n \gg  \left \lceil \delta \right \rceil$, this can result in a considerable speedup because evaluating a scale function or its inverse typically involves calls to $\log$ or $\sin^{-1}$. Note also that this algorithm only uses four statically allocated arrays of primitive values, avoiding all dynamic allocation and structure boxing/unboxing.
\begin{algorithm}[ht]
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}
\SetKwFor{While}{while}{:}{fintq}
\SetKwFor{For}{for}{\string:}{}
\SetNoFillComment
\KwIn{Sequence $C = [c_1 \ldots c_m]$ a $t$-digest containing real-valued, weighted centroids with components $\mathtt{sum}$ and $\mathtt{count}$ arranged in ascending order by mean, data buffer  $X = { x_1,\ldots x_n}$ of real-valued, weighted points, and compression factor $\delta$}
\KwOut{New ordered set $C'$ of weighted centroids forming a $t$-digest}
$X \gets \mathtt{sort}(C \cup X)$\;
$ S = \sum_i x_i.\mathtt{count}$\;
$C' = \lbrack \, \rbrack, q_0 = 0$\;
$q_{limit}=k^{-1}(k(q_0, \delta)+1, \delta)$\;
$\sigma = x_1$\;
\For{$i \in 2\ldots(m+n)$} {
  $q = q_0 + (\sigma.\mathtt{count} + x_i.\mathtt{count})/S$\;
  \If {$q \le q_{limit}$} {
      $\sigma \gets \sigma + x_i$\;
  } \Else {
      $C'\mathtt{.append}(\sigma)$\;
      $q_0 \gets q_0 + \sigma.\mathtt{count}/S$\;
      $q_{limit} \gets k^{-1}(k(q_0, \delta)+1, \delta)$\;
      $\sigma \gets x_i$\;
  }
}
$C'\mathtt{.append}(\sigma)$\;
\Return $ C' $\\
\caption{Merging new data into a $t$-digest\label{alg:merge}}
\end{algorithm}

The run-time cost of the merge variant of the $t$-digest is a mixture of the frequent buffer inserts  and the rare  merges. The buffer inserts are very fast since they consist of an array write, index increment and an overflow test. The merges consist of the buffer sort and the merge itself. The merge involves a scan through both the buffer and the existing centroids plus a number of calls to the scale function roughly equal to the size of the result which is bounded by $ \lceil \delta \rceil$ for all common scale functions. If $c_1$ is the input buffer size, the dominant costs are the sort and the scale function calls so the amortized cost per input value is roughly $C_1 \log c_1 + C_2 \lceil \delta \rceil / c_1$ where $C_1$ and $C_2$ are parameters representing the sort and scale function costs respectively. This overall amortized cost has a minimum where $c_1 \approx \delta\, C_2  / C_1$. The constant of proportionality should be determined by experiment, but micro-benchmarks indicate that $C_2 / C_1$ is in the range from $5$ to $20$ for a single core of an Intel i7 processor. In these micro-benchmarks, increasing the buffer size to $10 \lceil \delta \rceil$ dramatically improves the average speed but further buffer size increases have much less effect.

Further optimization in the case of the scale function $k_1$ is possible by speeding up the evaluation of $\sin^{-1}$. It is difficult to build a very fast and still high quality approximation of $\sin^{-1}$ over the entire domain $[0,1]$, but by limiting the domain where the approximation is used to $q \in [\epsilon, 1-\epsilon]$, where $\epsilon \approx 0.01$, simple and fast approximations are available.

It is also possible to avoid evaluation of $k$ and $k^{-1}$ by estimating the maximum number of samples that can be in each candidate cluster directly from $q$. Such estimates typically under-estimate the number of samples allowed, especially near the tails, but the size of the $t$-digest is not substantially increased and accuracy can be somewhat increased.

Finally, in practice, merge-based algorithms as shown here can be subject to problems due to the fact that samples are always considered in ascending order as they are merged with existing centroids. This can cause centroids near $q=0$ to grow in an asymmetric fashion with the effect that the centroid drifts a bit as the digest is constructed. This drift means that the resulting digest is more weakly ordered than desired. This problem can be mitigated to a degree by alternating the scan order in the merge (ascending in one pass, descending the next).

Another approach is to use a stratified merge in which a temporarily larger value of the compression parameter $\delta$ is used while points are being added. When the digest is stored, a final merge pass is made to fully consolidate the digest with the final compression parameter. To some degree, this can be framed in the context of a three-way tradeoff between space, speed and accuracy. For highest speed with constant memory use, a large buffer and a smaller value of $\delta$ can be used. For highest accuracy, a large buffer and stratified merge can be used. For smallest memory, a smaller buffer and a single value of $\delta$ should be used.

To give a specific example of this trade-off, for high accuracy, a buffer size of $10\times\delta$ would be used as is commonly done but merging could be done with a compression parameter of $3\times\delta$. This would require no more memory than typically used, but would cause merging to be done about 20\% more often, and would result in 3 times as many centroids being retained between merges which would make each merge a bit more expensive. If the resulting digest is only weakly ordered, say with ordering parameter $\Delta \le 3$ due to being constructed incrementally, the final merge is likely to reduce the digest nearly to a strongly ordered state as the number of centroids and ordering parameter are both reduced by roughly a factor of 3 to 1. In practice, the reduction in ordering parameter results in dramatically smaller errors.

Stratified merging is also useful when $t$-digest is used in parallel implementations. This is described in more detail in the section on parallel computing.

\subsection{The clustering variant}
If we allow the buffer in the merging variant of the $t$-digest algorithm to contain just a single element so that merges take place every time a new point is added, the algorithm takes on a new character and becomes much more like clustering than buffering and merging.

The basic outline of the clustering algorithm for constructing a $t$-digest is quite simple.  An initially empty ordered list of centroids, $C = [ c_1 \ldots c_m ]$ is kept.  Each centroid consists of a mean and a count.  To add a new value $x_n$ with a weight $w_n$, the set of centroids is found that have minimum distance to $x_n$.  This set is reduced by retaining only centroids whose $k$-size after adding $w_n$ would meet the size bound.  If more than one centroid remains, the one with maximum weight is selected.  If an acceptable centroid is found, then the new point, $(x_n,w_n)$, is added to that centroid. If no satisfactory centroid is found then $(x_n,w_n)$ is used to form a new centroid with weight $w_n$.

This clustering variant is shown as Algorithm \ref{alg:full}.
 \begin{algorithm}[htb]
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}
\SetKwFor{While}{while}{:}{fintq}
\SetKwFor{For}{for}{\string:}{}
\SetNoFillComment
\KwIn{Ordered set of weighted centroids $C = \lbrace \rbrace$, sequence of real-valued, weighted points $X = \lbrace (x_1, w_1),\ldots (x_N, w_N)\rbrace$, accuracy tolerance $\delta$, and limit on excess growth $K$ typically in the range $3\ldots10$}
\KwOut{final set $C=[c_1 \ldots c_m]$ of weighted centroids}
\For{$(x_n, w_n) \in X$} {
  $z = \min | c_i.\mathtt{mean} - x |$\;
  $S = \lbrace c_i  :  |c_i.\mathtt{mean} - x| = z \wedge |c_i+w_1|_k < 1 \rbrace $\;
  \If {$|S| > 0$} {
       \tcp{sort by descending distance}
       $S.\mathtt{sort}( key=\lambda (c) \lbrace -c.\mathtt{sum} \rbrace)$\;
       $c \gets S.\mathtt{first()}$ \;
       $c.\mathtt{count} \gets c.\mathtt{count} + w_n$\;
       $c.\mathtt{mean} \gets c.\mathtt{mean} + (x_n- c.\mathtt{mean})/ c.\mathtt{sum}$\;
     } \Else {
      $C \gets C + (x_n, w_n)$\;
      }
      \If {$|C| > K\delta$} {
         $C \gets \mathtt{merge}( C, \lbrace\rbrace ) $\;
       }
}
$C \gets \mathtt{merge}( C, \lbrace\rbrace ) $\;
\Return $ C $\\
\caption{Construction of a $t$-Digest by clustering \label{alg:full}}
\end{algorithm}

Certain insertion orders can cause the number of centroids to increase without bound. For instance, if the values of $X$ are in ascending or descending order, $C$ will contain as many centroids as samples inserted.  This happen because each new value of $X$ is always a new maximum (or minimum) and thus will always form a new centroid.  To avoid this, the clusters can be consolidated using Algorithm \ref{alg:merge} whenever the number of clusters grows excessively.

\subsection{Alternative scale functions}
The $k_1$ and $k_0$ scale functions that we have mentioned are not the only ones possible. In fact, there are two additional important functions that provide important accuracy/size trade-offs. Altogether, these four functions serve as useful scale functions
\begin{align}
k_0(q) &= \frac \delta 2 q \\
k_1(q) &= \frac \delta {2\pi}  \sin^{-1}(2q-1)   \\
k_2(q) &= \frac \delta {4 \log n/\delta + 24} \log {\frac q {1-q}} \\
k_3(q) &= \frac \delta {4\log n/\delta + 21}\begin{cases}
\quad \log 2q & \text{if  } q \le 1/2 \\
- \log 2(1-q) & \text{if  } q > 1/2
\end{cases}
\end{align}

Each of these scale functions to build a $t$-digest gives the following two guarantees
\begin{itemize}
\item
The number of centroids in a fully merged $t$-digest is bounded by $\ceil\delta$ as the number of samples increases without bound (for $k_2$ and $k_3$ as given above, this only applies up to $10^{60}$ samples when the number of clusters starts growing roughly with $\log \log n$)
\item
The union of the centroids from multiple $t$-digests will meet the size bound and thus be a $t$-digest for a value of $\delta$ equal to the smallest $\delta$ of any of the individual digests.
\end{itemize}

It is also possible to use unnormalized versions of $k_2$ and $k_3$:
\begin{align}
k'_2(q) &=  \delta  \log {\frac q {1-q}} \\
k'_3(q) &=  \delta \begin{cases}
\quad \log 2q & \text{if  } q \le 1/2 \\
- \log 2(1-q) & \text{if  } q > 1/2
\end{cases}
\end{align}
With these denormalized scale functions the number of centroids in a $t$-digest will grow roughly with $\log n$.

The major benefit of $k_2$ and $k_3$ scale functions is that they constrain the size of clusters near $q=0$ and $q=1$ even more strenuously than $k_1$ does. Whereas, clusters are limited in size proportionally to $\sqrt {q (1-q)}$ for $k_1$, $k_2$ and $k_3$ limit cluster sizes proportional to $q(1-q)$ and $\min(q,1-q)$ respectively where $q$.

This more rigorous regulation of cluster size means that errors in interpolation due to weak ordering of the clusters in a digest or due to non-uniform distribution of samples for clusters will be compensated by having smaller clusters near extreme values of $q$. The $k_3$ scale function, in particular, will drive errors to zero near the extremes by forcing more clusters near $q=0$ or $q=1$ to have only a single sample.

\subsection{Interpolation of the cumulative distribution function}
The information in a fully-merged $t$-digest is not sufficient, in general, to precisely determine the empirical quantile $q$ of any particular value $x$ because information is lost as samples are clustered together. The only exception to this is in the case of clusters containing only a single sample where the centroid is the same as the single sample.

The approach taken in current implementations of the $t$-digest is to make some simplifying assumptions about the distribution of samples in the original data. These are:
\begin{enumerate}
\item The samples for a cluster are evenly split into samples that are to the left of the centroid and those to the right if there is more than one sample.
\item The samples between two clusters are uniformly distributed between them.
\end{enumerate}
The first assumption is obviously the best we can do with no further information about the expected distribution. The second assumption is true asymptotically for continuous distributions as the number of clusters in a $t$-digest increases if the samples associated with each cluster do not extend beyond the neighboring centroids. In practice, clusters in a $t$-digest are sufficiently localized and quantile accuracy is good even for highly skewed distributions.

Based on these assumptions, we have four important cases. These are a) interpolation between clusters each with more than one sample, b) interpolation between a multi-sample cluster and a cluster with just a single sample, c) interpolation between clusters each with just a single sample and d) interpolation for the first and last cluster.

Between two clusters where both have more than one sample, the empirical cumulative distribution function is approximated using a linear approximation between two consecutive centroids that allocates half the weight of each centroid to the interval between them. This is equivalent to saying that the the mid-point of the cluster is at the centroid. This is illustrated in Figure \ref{fig:interpolation}.
\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=3in]{figures/interpolation.pdf}
   \caption{Interpolation of the empirical cumulative distribution function between centroids of clusters with more than one sample is done by assuming half of the points for each centroid are to the left of the centroid and half are to the right. }
   \label{fig:interpolation}
\end{figure}

When clusters have a single sample, however, this interpolation can be improved by noting that this single sample is located exactly at the centroid for that cluster. We can use that to improve our interpolation as illustrated in Figure \ref{fig:singletons}.
\begin{figure}[tb] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=3in]{figures/singleton.pdf}
   \caption{Clusters with a single sample are handled specially. Adjacent to a normal cluster, as is the case between $x_1$ and $x_2$, interpolation is done assuming $w_1/2$ samples occur between $x_1$ and $x_2$, and the single sample at $x_2$. That single sample causes the cumulative distribution to step to the mid-point of the individual sample at $x_2$. Between singleton clusters $x_2$ and $x_3$, the cumulative distribution is given a constant value until it steps again at $x_3$.}
   \label{fig:singletons}
\end{figure}
When the neighboring cluster has multiple samples, we can interpolate almost as before, but when the next cluster is itself a singleton, we can avoid interpolation entirely.

The final case to be considered is that of the first or last cluster in the digest.
When such a terminal cluster has only one sample, then we know that $x_{\mathrm {max}}$ is due exactly to that single sample. Where the cluster has two samples, we know that those two samples were at $x_{\mathrm {max}}$ and $x_n-(x_{\mathrm {max}}-x)$ and thus they can be treated as two singletons. Where there are more than two samples, however, it is advantageous to treat the cluster as having half the samples before the centroid and half after. For the half of the samples that come after, one of those samples must be at $x_{\mathrm {max}}$ and thus it can be treated as a singleton with the remainder being interpolated. All of this discussion is reversed, of course, first for last and left for right when treating the first cluster. This is shown in Figure \ref{fig:endpoint} for the last cluster in a digest.
\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=3in]{figures/endpoint.pdf}
   \caption{For the last clusters, if the cluster weight is greater than $2$, we can use the fact that a singleton must occur at $x_{\mathrm {min}}$ or $x_{\mathrm {max}}$ to improve interpolation accuracy. The first cluster is handled by reversing these steps.}
   \label{fig:endpoint}
\end{figure}

For scale functions $k_2$ and $k_3$ the first and last clusters will always be singletons and thus will never need this special handling. For the $k_1$ scale function, however, the first and last clusters may well have more than one sample thus making it desirable to interpolate out beyond the centroid.

\section{Empirical Assessments}
Figure \ref{fig:by-scale} examines the accuracy of quantile estimation with a $t$-digest.  As intended, absolute error in quantile estimation is small and decreases near $q=0$ and $q=1$ (left panel) for scale functions $k_1$, $k_2$ and $k_3$. Though it does not show well in this figure because of the vertical scale, in the mid-range where $q \in [0.1, 0.9]$, $k_1$ provides somewhat better absolute error than $k_2$ and $k_3$.
\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \includegraphics[width=5.5in]{figures/relative-error.pdf}
   \caption{The left panel shows absolute error in parts per million of estimations of quantile with $\delta = 100$ for scale functions $k_1$, $k_2$ and $k_3$. Errors are computed by taking $10^6$ samples from a uniform distribution and comparing estimates from a $t$-digest against exact quantiles computed from the original samples. This was repeated 50 times to get a sense of variability. The left panel shows absolute error in parts per million and the right panel shows relative error.}
   \label{fig:by-scale}
\end{figure}

All scale functions show a pattern of decreasing absolute error for more and more extreme values of $q$, but $k_2$ and $k_3$ achieve single digit part per million errors for $q\le 0.001$ or $q\ge0.999$ which is surprisingly good since only about 50 to 60 centroids are retained in total for $\delta=100$.

In spite of the decrease in the absolute error with all three scale functions as $q$ becomes more extreme, the relative error is not as well controlled. This is shown in right panel of Figure \ref{fig:by-scale}. Scale functions $k_2$ and $k_3$ exhibit good control of relative error down to $q=0.001$ but for $q= 10^{-5}$ and $q=10^{-4}$, relative error increases a bit. For  smaller values of $q$, all relevant centroids have only a single sample and thus errors go to zero. For $k_1$, the situation is not as favorable; relative error increases dramatically starting at $q=0.001$ and is off scale by $q=10^{-5}$.
\subsection{Degree of overlap of $t$-digest clusters}
Figure \ref{fig:cluster-spread} shows how effective the stratified merging strategy  is at producing high quality clusters. This requires about 20\% more merge operations and  each merge operation is typically less than twice as expensive. The resulting clusters are not quite strictly ordered, but overlap between clusters is limited to adjacent clusters.
\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \includegraphics[width=4.5in]{figures/cluster-spread.pdf}
   \caption{The first few clusters in a typical $t$-digest show that while the digest is not strictly ordered, only adjacent clusters overlap. This plot shows roughly the first 20 clusters of the digest with even-numbered clusters below and odd-numbered ones above. Note how odd clusters overlap with adjacent even clusters, but never with adjacent odd clusters. This corresponds to weak ordering with $\Delta=1$  }
   \label{fig:cluster-spread}
\end{figure}
The digest shown here was produced by inserting
a total of $10^6$ samples using a working compression of $\delta = 316$ and alternating the direction of merges. The final digest was produced by doing a final merge step with $\delta = 100$ resulting in a roughly $3:1$ merge of the working clusters. With unidirectional merge steps and keeping $\delta=100$ during construction, the overlap between clusters commonly extends to the adjacent 3-4 clusters, substantially degrading accuracy.

\subsection{Persisting $t$-digests}
For the accuracy setting and test data used in these experiments, the $t$-digest contained $50-60$ centroids.  The results of $t$-digest can thus be stored by storing this many centroid means and weights.  If centroids are kept as double precision floating point numbers and counts kept as 4-byte integers, the $t$-digest resulting from from the accuracy tests described here would require less than 800 bytes of storage.

This size can be decreased, however.  One simple option is to store differences between centroid means and to use a variable byte encoding to store each cluster size.  The differences between successive means are are about two orders of magnitude smaller than the means themselves so using single precision floating point to store these differences can allow the $t$-digest from the tests described here to be stored in under 500 bytes while still regaining nearly 10 significant figures of accuracy in the means.  This is roughly equivalent to the maximum precision possible with a Q-digest operating on 32 bit integers, but the dynamic range of $t$-digests will be considerably higher and the accuracy is considerably better.

\subsection{Space/Accuracy Trade-off}
Not surprisingly, there is a trade-off between the size of the $t$-digest as controlled by the compression parameter $\delta$ versus the accuracy which which quantiles can be estimated.  Figure \ref{fig:accuracy-scaling} shows the trade-off for different quantiles. Of particular interest is the fact that for smaller values of $\delta$, errors appear to decrease roughly with the  square of the number of retained centroids (or equivalently, the compression parameter $\delta$) and with the square root for larger values of $\delta$. The original (now known to be incorrect, or at least incomplete) intuition underlying the $t$-digest suggested that errors would decrease with the square of size of the clusters in the digest as would be the case of a step-wise approximation of a continuous curve as step size is decreased. In fact, numerical experiments suggest the dominant sources of error in $t$-digest are the difference between the mean and median of each cluster and deviation of the actual distribution of samples in a cluster from the linearly interpolation. Both of these error sources should scale with the inverse square root of the number of samples in a cluster.

In particular, since cluster size for a particular value of $q$ should be directly proportional to $\delta$,  the observed scaling of error with $\delta$ appears to contradict the original intuition and support square root scaling of errors, at least for larger values of $\delta$. This discrepancy explains why $k_1$ does not perform as well as $k_2$ and $k_3$ in terms of relative error, but a rigorous explanation of the scaling of error with $\delta$ is still not available. In practical terms, however, the $t$-digest clearly performs well.

\begin{figure}[p] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=4in]{figures/error-vs-compression.pdf}
   \caption{The scaling of quantile estimation for various values of compression factor $\delta$ and $q$.
   The general pattern is that absolute error scales like $1/\delta^2$ for small values of $\delta$ and like $1/\sqrt{\delta}$ for values larger than some cutoff. The cutoff is higher for larger values of $q$ (roughly 500 for $q=0.01$ but only 100 for $q=10^{-5}$). The grey dashed lines provide a reference for $1/\sqrt{\delta}$ scaling, the dotted grey lines show $1/\delta^2$.   At $q=10^{-5}$ error goes to zero for values $\delta > 200$ and cannot be shown on a log scale. The same happens for $q=10^{-6}$ except error is zero for all values of $\delta$. The data shown here are for $k_2$, but the results for $k_3$ are nearly indistinguishable.}
   \label{fig:accuracy-scaling}
\end{figure}

\subsection{Computing $t$-digests in parallel}
With large scale computations, it is important to be able to compute aggregates like the $t$-digest on separate portions of the input in parallel and then combine those aggregates.

For example, in a map-reduce framework such as Hadoop, a combiner function can build a $t$-digest on the outputs of several mappers and then a single reducer can be used to combine the outputs of the combiners to compute the $t$-digest for the entire data set.

Another example can be found in certain databases such as Apache Druid which maintains tree structured indices and allows the programmer to specify that particular aggregates of the data being stored can be kept at interior nodes of the index.  The benefit of this is that aggregation can be done almost instantaneously over any contiguous sub-range of the index.  The cost is quite modest with only a $O(\log(N))$ total increase in effort over keeping a running aggregate of all data where $N$ is the depth of the tree.  In many practical cases, the tree can be no more than two or three levels and still provide fast enough response.  For instance, if it is desired to be able to compute quantiles for any period up to 30 years in 30 second increments, simply keeping $t$-digests for intervals of 30 seconds, 2.5 hours and 31 days is likely to be satisfactory because at most about 300 digests will ever need to be merged.

The most straightforward to merge $t$-digests is to use Algorithm \ref{alg:merge} which sorts all centroids from the $t$-digests being merged and then consolidates them as much as possible while maintaining the $t$-digest size criterion. As with non-parallel implementations, stratified merging is useful where a higher value of $\delta$ is used when data is inserted into the smaller digests and a smaller value of $\delta$ is used when combining the digests.

Figure \ref{fig:merge} illustrates how this turns out. This figure shows accuracy achieved when summarizing 1,000,000 samples across 20 trials. The panels, taken from left to right, show the results of merging 5, 20 and 100 sub-digests. Within each panel, the three bars represent the use of a single digest which internally uses $\delta=300,100$ stratified merging, merged digests with $\delta=200$ in the sub-digests and $\delta=100$ in the result and non-stratified merge with $\delta=100$. At all levels of parallelism, the non-stratified merge result was worse than the non-merged stratified result. Stratified parallel merging was comparable to the non-parallel merge, though a bit more variable, with 5-way parallelism, but got progressively more accurate with high levels of parallelism.

\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=5.5in]{figures/merge.pdf}
   \caption{Accuracy of a $t$-digest accumulated directly is nearly the same as when the digest is computed by combining digests from 5, 20 or 100 equal sized paritions of the data.  All panels were computed by 20 repetitions of aggregating 1,000,000 values. }
   \label{fig:merge}
\end{figure}

In terms of speed, merging $t$-digests is relatively cheap. In one experiment, 1000 digests were used to summarize 1,000,000 samples each and these digests were then merged into a single digest. It took about 150ms to fill each digest and about 150 ms to merge all thousand digests. Each digest is about 1kB in this example so a total of about 1MB of digests would need to be moved to the merge. Overall, this means that, with sufficient hardware (roughly 1000 cores), a $t$-digest can be produced from a billion data points in less than half a second.

\subsection{Comparison with Q-digest}
In terms of error size, the 4.0 preview implementation of $t$-digest dominates the implementation of the Q-digest\citep{qdigest} from the popular stream-lib package \citep{github:stream} when digests of the same size are compared.  This is shown in Figure \ref{fig:qd-comparison}.  In the left panel, the relationship between the compression parameter $q$ and size is shown for both digests for 20 trials where 100,000 uniformly distributed samples are inserted into each kind of digest.  To force the digests to be equal in size compression parameter values of $\delta_q=20$ for Q-digest  and $\delta_t = 200$ for $t$-digest were chosen. This gives an average digest size of about $874$ bytes for Q-digest and 990 bytes for $t$-digest, as shown in the left panel. The panel on the right compares errors under these conditions and shows that the $t$-digest errors are more than an order of magnitude smaller for all values of $q$ (actually more than two orders of magnitude better). For values near $q=0$ the $t$-digest produces absolute errors 3-5 orders of magnitude smaller than the Q-digest does. When compared on distributions with high amounts of skew, the results are even more striking in favor of the $t$-digest.
\begin{figure}[htb] %  figure placement: here, top, bottom, or page
   \centering
   \includegraphics[width=4.5in]{figures/qd-sizes.pdf}
   \caption{The left panel shows the size of a serialized Q-digest and $t$-digest versus compression parameter $\delta$ for 100,000 uniformly distributed samples. Trials were repeated 20 times. The left panel shows how compression parameters $\delta_q$ and $\delta_t$ are chosen to control digest size to approximately 1k byte. The right panel shows absolute error for various values of $q$.  With this vertical scale, $t$-digest errors are only barely distinguishable from zero near the median.  }
   \label{fig:qd-comparison}
\end{figure}
Note that the parameters chosen for this comparison depend on the number of samples because the size of the Q-digest increases roughly logarithmically with the number of samples while the size of the $t$-digest is strictly bounded and is nearly constant once the number of samples exceeds $2 \delta$.

\section{Conclusion}
The $t$-digest is a novel on-line algorithm that dominates the previously state-of-the-art Q-digest in terms of accuracy and size.  The $t$-digest provides accurate on-line estimates of a variety of of rank-based statistics including quantiles and trimmed mean.  The core ideas of the algorithm are simple and empirically demonstrate high accuracy.  The $t$-digest can also be used in parallel applications or in OLAP-style indexes.

The $t$-digest algorithm is available in the form of an open source, well-tested implementation maintained the author \citep{t-digest-project}.  It has already been adopted by large companies such as Netflix and Microsoft for internal monitoring and has been included in open source libraries such as Apache Mahout and stream-lib and has been incorporated into prominent open-source projects such as Apache Lucene and Elasticsearch.

%\subsection{}
\bibliographystyle{chicago}
\bibliography{refs}{}

\end{document}


\subsection{Age-specific parameters}

Age-structured parameters are presented in Table \ref{tab:age_params}.

\begin{table}[ht]
\renewcommand{\baselinestretch}{1}
    \begin{tabular}[ht]{| p{2cm} | p{2.5cm} | p{3cm} | p{3cm} | p{2.5cm}|}
    \hline
        Age group (years) & Clinical fraction\textsuperscript{a} & Relative susceptibility to infection & Infection fatality rate & Proportion of symptomatic patients hospitalised \\
        \hline
        0 to 4 & 0.533 & 0.36 & 3 $\times$ 10\textsuperscript{-5} & 0.029 \\
        \hline
        5 to 9 & 0.533 & 0.36 & 1 $\times$\textsuperscript{-5} & 0.029 \\
        \hline
        10 to 14 & 0.533 & 0.36 & 1 $\times$\textsuperscript{-5} & 0.014 \\
        \hline
        15 to 19 & 0.533 & 1 & 3 $\times$\textsuperscript{-5} & 0.014 \\
        \hline
        20 to 24 & 0.679 & 1 & 6 $\times$\textsuperscript{-5} & 0.030 \\
        \hline
        25 to 29 & 0.679 & 1 & 1.3 $\times$\textsuperscript{-4} & 0.030 \\
        \hline
        30 to 34 & 0.679 & 1 & 2.4 $\times$\textsuperscript{-4} & 0.047 \\
        \hline
        35 to 39 & 0.679 & 1 & 4.0 $\times$\textsuperscript{-4} & 0.047 \\
        \hline
        40 to 44 & 0.679 & 1 & 7.5 $\times$\textsuperscript{-4} & 0.081 \\
        \hline
        45 to 49 & 0.679 & 1 & 1.21 $\times$\textsuperscript{-3} & 0.081 \\
        \hline
        50 to 54 & 0.679 & 1 & 2.07 $\times$\textsuperscript{-3} & 0.128 \\
        \hline
        55 to 59 & 0.679 & 1 & 3.23 $\times$\textsuperscript{-3} & 0.128 \\
        \hline
        60 to 64 & 0.803 & 1 & 4.56 $\times$\textsuperscript{-3} & 0.221 \\
        \hline
        65 to 69 & 0.803 & 1.41 & 1.075 $\times$\textsuperscript{-2} & 0.221 \\
        \hline
        70 to 74 & 0.803 & 1.41 & 1.674 $\times$\textsuperscript{-2} & 0.389 \\
        \hline
        75 and above & 0.803 & 1.41 & 5.748 $\times$\textsuperscript{-2, b} & 0.471 \\
        \hline
        Source/ rationale &
        Table 1 of systematic review and meta-analysis with appropriate accounting for testing during the pre-symptomatic period \cite{RN140}. &
        Conversion of odds ratios presented in Table S15 of Zhang et al. 2020 to relative risks using data presented in Table S14 of the same study \cite{RN7}.\textsuperscript{c} &
        Estimated from pooled analysis of data from 45 countries from Table S3 of O'Driscoll et al \cite{RN6}. Values consistent with previous estimates using serosurveys performed in Spain \cite{RN21}. &
        Table 1.1 from NNDSS report on COVID-19 cases in Australia to May 2021 \\ \hline
	\end{tabular}
	\title{Age-stratified parameter values.}
	\caption{\textbf{Age-stratified parameters not varied during calibration, or varied through a common multiplier.} \textsuperscript{a}Proportion of incident cases developing symptoms. \textsuperscript{b}Weighted average of IFR estimates for 70 to 79 and 80 and above age groups. \textsuperscript{c}Note the relative magnitude of these values are similar to those estimated by the analysis we use to estimate the age-specific clinical fraction.}
	\label{tab:age_params}
\end{table}

\clearpage


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

\section*{8 October 2019}

\subsubsection{An ansatz for the LAWE}

Last lecture we started talking about linearization \& perturbation theory.

We use a complex spinning ansatz, which is by itself not physical, however its conjugate is also a solution so after we have found a solution we just need to take the real part.

Substituting \(\zeta (m, t) = \eta (m) \exp(i \sigma t)\) into the LAWE we find that the exponentials simplify:
%
\begin{align}
-r \sigma^2 \eta  =
4 \pi r^2 \eta \pdv{}{m} \qty((3 \Gamma_1 - 4)P)+
\frac{1}{r} \pdv{}{m} \qty(16 \pi^2 \Gamma_1 P \rho r^6 \pdv{\eta }{m} )
\,.
\end{align}

Since we have an explicit \(r\), it is convenient to rewrite this in the Eulerial formalism, using the continuity equation: we get
%
\begin{subequations}
\begin{align}
- r \sigma^2 \eta &=
\frac{\eta }{\rho } \pdv{}{r} \qty((3\Gamma_1 - 4) P)
+ \frac{1}{r^3 \rho } \pdv{}{r} \qty(\Gamma_1 P r^{4} \pdv{\eta }{r})  \\
\sigma^2 \eta  &= - \hlc{green}{\frac{\eta }{\rho r} \pdv{}{r} \qty(3 \Gamma_1 P - 4P )} -\hlc{cyan}{\frac{1}{r^{4} \rho } \pdv{}{r} \qty(\Gamma_1 P r^{4} \pdv{\eta }{r})}
\,, \label{eq:linear-operator-LAWE}
\end{align}
\end{subequations}
%
\todo[inline]{Slide 4.37: there is a minus sign missing, right?}

We will have analytic solutions for the adiabatic case, with the additional hypotheses of either \(\Gamma_1 = 4/3\) or \(\Gamma_1 > 4/3\) and homogeneity.

[The justification of the adiabatic approx might be asked at the exam.]

\subsubsection{Boundary conditions for the LAWE}

What are the boundary conditions we should set? The final result is:
%
\begin{enumerate}
    \item \(\delta r = 0\) at \(r = 0\);
    \item \(\pdv*{\eta}{r} = 0 \) at \(r=0\);
    \item \((4 + R^3 \sigma^2 / (GM)) \eta + \delta P / P = 0\) at \(r =R\);
    \item \(\eta = \delta r / r = 1\) at \(r = R\).
\end{enumerate}

Let us show why. The first condition has an immediate physical basis: there cannot be radial displacement at \(r=0\) since there is nowhere to go at \(r<0\), and if there was positive radial displacement \(\delta r\) it would mean there is a vacuum in the region \(r<\delta r \), which is unphysical because of the pressure there.

So, by the first condition, we need the term
%
\begin{align}
\hlc{cyan}{\frac{1}{r^4 \rho } \pdv{}{r} \qty(\Gamma_1 P r^{4} \pdv{\eta }{r})}
= \frac{\Gamma_1 P}{\rho } \pdv[2]{\eta }{r}
+ 4 \frac{\Gamma_1 P}{\rho r} \pdv{\eta}{r}
+ \frac{\Gamma_1 }{\rho } \pdv{\eta }{r} \pdv{P}{r}
+ \frac{P}{\rho } \pdv{\eta }{r} \pdv{\Gamma_1}{r}
\,
\end{align}
%
to be well behaved (nonsingular) at \(r=0\).

We know that \(\pdv*{P}{r}\) and \(\pdv*{\Gamma_1}{r}\) are zero at \(r=0\), so we can neglect the terms containing them. This is because any physical quantity must approach \(r=0\) with zero \(\dv*{}{r}\) slope, in order for it to be differentiable at the center.

% Let us show this: we write the momentum conservation equation in the Eulerian form,
% %
% \begin{align}
% \pdv{P}{r} = - \frac{Gm \rho }{r^2}
% \,,
% \end{align}
% %
% and look at the asymptotic behaviour at \(r \rightarrow 0\): \(\rho \) and \(G\) are finite, while \(m\) and \(r^2\) go to zero. We know that \(m  \sim r^3\), therefore globally the behaviour is \(r^3/r^2 = r \rightarrow 0\).

% Also, we know that \(P = \rho^{\Gamma_1 }\). So, let us write \(\pdv*{P}{r}\):
% %
% \begin{align}
% \pdv{}{r} \exp(\Gamma_1 \log \rho )
% = \exp(\Gamma_1 \log \rho) \qty(\log \rho \pdv{\Gamma_1  }{r} + \Gamma_1 \pdv{}{r} \log \rho ) = 0
% \,,
% \end{align}
% %
% \todo[inline]{prove that \(\pdv*{\Gamma_1}{r} =0\) for \(r=0\)}

In the second term we have a division by \(r\): in order for it not to diverge we must ask that \(\pdv*{\eta }{r} =0\) at \(r=0\).

Since \(\eta \) is \(\zeta \) times a phase, this also means that \(\pdv*{\zeta }{r} =0 \) at \(r=0\).
We can plug this into the linearized continuity equation, the Eulerian form of \eqref{eq:linearized-continuity}:
%
\begin{subequations}
\begin{align}
\frac{ \delta \rho }{\rho } &= - 3\zeta - r\pdv{\zeta }{r} \\
0=\pdv{\zeta }{r} &= - \frac{1}{r} \qty(3\zeta + \frac{ \delta \rho }{\rho })  \\
\implies 0 &= 3\zeta + \frac{ \delta \rho }{\rho }
\,,
\end{align}
\end{subequations}
%
into which we can substitute the linearized adiabatic expressions for the temperature and pressure perturbations \eqref{eq:linearized-adiabatic-energy-conservation}: so we find
%
\begin{align}
3\zeta = - \frac{1}{\Gamma_1 } \frac{ \delta P }{P}
= - \frac{1}{\Gamma_3 -1} \frac{ \delta T}{T}
\,,
\end{align}
%

We use the Eulerian form of the momentum conservation \eqref{eq:linearized-momentum-conservation} to figure out the surface boundary conditions: it reads
%
\begin{subequations}
\begin{align}
r \sigma^2 \eta &=
\frac{1}{\rho } \qty(P \pdv{}{r} \qty(\frac{ \delta P}{P})
+ \frac{ \delta P }{P } \pdv{P}{r}
+ 4 \eta  \pdv{P}{r})  \\
\pdv{}{r} \qty(\frac{ \delta P}{P}) &= \frac{\rho}{P}
\qty(r \sigma^2 \eta ) - \frac{1}{P} \qty(\frac{ \delta P}{P} \pdv{P}{r} + 4 \eta  \pdv{P}{r})  \\
&= \frac{\rho r \sigma^2 \eta }{P}
- \frac{ \delta P}{P} \pdv{\log P}{r}
- 4 \eta \pdv{\log P}{r}  \\
&= -\pdv{\log P}{r} \qty(- \frac{\rho r \sigma^2 \eta }{\pdv*{P}{r}}
+ 4 \eta
+ \frac{ \delta P}{P}
)
\,,
\end{align}
\end{subequations}
%
since we assumed that all perturbations are \emph{in phase} to the radial one, and thus wrote them all as proportional to \(\exp(i \sigma t)\), which then simplified.
We define the \emph{pressure scale height} as:

\begin{equation}
  H_P = - \qty(\pdv{\log P }{r} )^{-1}\,,
\end{equation}
%
it represents ``how far we should move in the star for the pressure to change \(e\)-fold''.

We insert this into the equation; also we use the Eulerian momentum conservation equation
%
\begin{align}
\pdv{P}{r} = -\rho \frac{Gm}{r^2}
\,,
\end{align}
%
so that we find
%
\begin{align}
\pdv{}{r} \qty(\frac{ \delta P}{P}) =
\frac{1}{H_P} \qty(
\qty(
4 + \frac{r^3 \sigma^2}{ Gm}
) \eta
+ \frac{ \delta P }{P }
)
\,,
\end{align}
%

Since outside the star the pressure is zero, we must have that \(H_P \rightarrow 0\) when \(r \rightarrow R\): the pressure must change by an infinite number of \(e\)-folds to reach zero, so the displacement needed for a single \(e\)-fold change must go to zero. Therefore, the term multiplying \(1/H_P\) must also go to zero to avoid divergences: so we ask
%
\begin{align}
\qty(4 + \frac{r^3 \sigma^2 }{Gm}) \eta + \frac{ \delta P}{P} \rightarrow 0
\,
\end{align}
%
as \(r \rightarrow R\). Since we are considering the mass variable at the edge of the star, we also must have \(m=M\).

The last condition, \(\eta = \delta r / r = 1\) at \(r=R\) comes from the fact that we want our study to give us \emph{periods}, not \emph{amplitudes}: we cannot find those out, so we normalize in a way that is convenient. The LAWE is 1-homogeneous!

\subsubsection{Solving the LAWE}

The LAWE can be written compactly with a linear operator \(\mathcal L\):

\begin{equation}
  \mathcal L (\eta) = \sigma^2 \eta\,,
\end{equation}
%
where the expression for \(\mathcal{L}\) can be read off equation \eqref{eq:linear-operator-LAWE}. The equation is in the form of a Sturm-Liouville differential equation; in general those have the form
%
\begin{align}
\pdv{}{r} \qty(p(r) \pdv{\eta }{r}) + \lambda t(r) \eta
- s(r) \eta  = 0
\,.
\end{align}

\todo[inline]{The signs are inconsistent in the slides!}

So, the eigenvalue \(\sigma^2\) is the square of the pulsation.
There are infinitely many solutions to the LAWE, only some (finitely many?) fulfill the boundary condition.
The eigenvalues are real since \(\mathcal L\) is Hermitian,  and they have a wavefunction associated: \(\eta_m (r)\) corresponding to \(\sigma_m ^2\).

If \(\sigma^2 > 0\) we have an oscillating solution, if \(\sigma^2 <0 \) we have an exponential collapse or explosion since the solution is proportional to \(\exp(i \sigma t)\).

We label solutions by \emph{radial order} \(m \in \mathbb{N}\): \(m=0\) has the lowest frequency, and then we have overtones.
We choose the labels so that \(\sigma_{m_1} < \sigma_{m_2} \iff m_1 < m_2\).

The radial order \(m\) is also the number of nodes.

The eigenfunctions are orthogonal wrt the scalar product

\begin{equation}
  \braket{\eta_m}{\eta_n} = \int _{0}   ^{R} \eta_m \eta_n \rho r^4 \dd{r} = f(n) \delta_{nm}
\end{equation}

\todo[inline]{Possibly there is a \(4 \pi\) missing in order for this to be consistent with the following definition\dots}

The functions \(\zeta_m\) are orthogonal with respect to the same product. The system is linear: we can write a general solution as a superposition.

We can define the moment of inertia:

\begin{equation}
  J_m = \int _{0}   ^{M} \abs{\zeta_m}^2 r^2 \dd{m}
\end{equation}

and then we can recover the eigenvalue by:

\begin{equation}
  \sigma_m^2 = \frac{1}{J_m} \int_0^M \zeta_m ^* \mathcal L \zeta_m r^2 \dd{m}
  = \frac{\ev{\mathcal{L}}{\zeta_{m}}}{\braket{\zeta_{m}}{\zeta_{m}}}
\end{equation}

\subsection{Simplifications}

\subsubsection{Period-mean density relation}

If \(\eta = \const\), and \(\rho \) and \(\Gamma_1\) are also constanst, we can remove the \(\pdv*{\eta }{r}\) terms: so we have
%
\begin{subequations}
\begin{align}
\sigma^2 \eta + \frac{\eta }{\rho r} \pdv{}{r} \qty(P \qty(3 \Gamma_1 - 4)) &= 0  \\
\sigma^2 &= - \frac{3 \Gamma_1 - 4}{\rho r} \pdv{P}{r}  \\
\sigma^2 &= \qty( 3\Gamma_1 -4 ) \frac{Gm}{r^3}
\,,
\end{align}
\end{subequations}
%
and by inserting the mean density formula \(\overline{\rho} = M / \qty(4 \pi R^3 / 3)\) we get the period-mean density relation: \(\sigma = 2 \pi / T\), so we have \(T^{-2} \propto \overline{\rho}\), the period-mean density relation.

Actually, this simplified model gives us \(m / r^3 = \const\): the density in this case must be constant and equal to \(\overline{\rho}\) (even if we did not use the assumption before).
The precise relation is given by
%
\begin{align}
T^2 \overline{\rho} = \frac{3 \pi }{\qty(3 \Gamma_1 -4) G}
\,,
\end{align}
%

\subsubsection{Polytropic model}

It is a gas sphere with the following constitutive equaition:

\begin{equation}
  P = K_n \rho^{1 + 1/n} = K_n \rho^{\frac{n+1}{n}}
\end{equation}
%
with varying \emph{polytropic} index \(n\).

\begin{bluebox}
This can be derived as a solution for the Lane-Emden equation, which comes from Poisson's equation for the gravitational potential: so we do the following manipulation
%
\begin{subequations}
\begin{align}
\nabla^2 \Phi  &= 4 \pi G \rho  \\
\frac{1}{r^2} \dv{}{r} \qty(r^2 \dv{\Phi }{r}) &= 4 \pi G \rho  \marginnote{Laplacian in spherical coordinates under spherical symmetry}[-1cm]\\
-\frac{1}{r^2}\qty(\frac{r^2}{\rho } \dv{P}{r}) &= 4 \pi G \rho   \marginnote{Used the hydrostatic equilibrium equation}\\
\dv{}{r} \qty(\frac{r^2}{\rho } \dv{P}{r}) &= - 4 \pi G \rho r^2
\,,
\end{align}
\end{subequations}
%
so if we assume \(\rho = \rho_{c} \theta^{n}\) the polytropic equation of state will read
%
\begin{align}
P = K_n \rho^{1 + 1/n} = K_n \rho_{c}^{1 + 1/n} \theta^{n (1 + 1/n)}
= K_n \rho_{c}^{1 + 1/n} \theta^{n+1}
\,,
\end{align}
%
where \(\rho_{c}\) is the central density, so we have \(\theta (r=0) = 1\) and \(\theta' (r=0) =0\). Inserting this into Poisson's equation gives us
%
\begin{subequations}
\begin{align}
\dv{}{r} \qty(\frac{r^2}{\rho_{c} \theta^{n}}
\dv{}{r} \qty(K_{n} \rho_{c}^{1 + 1/n} \theta^{n+1})) &=-4 \pi G \rho r^2  \\
\dv{}{r} \qty(r^2 (n+1) K_n \rho_{c}^{1/n} \dv{\theta }{r}) & = - 4 \pi G \rho r^2
\,,
\end{align}
\end{subequations}
%
so we can adimensionalize the radial coordinate by putting inside of it all the numbers: \(r = \alpha \xi \), with \(\alpha \) chosen so that the equation turns into
%
\begin{align}
\frac{1}{\xi^2} \dv{}{\xi } \qty(\xi^2 \dv{\theta }{\xi })
+ \theta^{n} = 0
\,.
\end{align}

This is always solvable, and it is solvable analytically for certain values of \(n\).

\end{bluebox}

It models spheres with different mass distributions:
\(n=0\) is constant density, \(n=5\) is infinite central density, \(n=3\) is the Eddington standard model, which is reasonable for the Sun and stars on the main sequence.

With these assumptions we can explicitly solve for the wavefunctions, and make predictions of the fractional modulus of the oscillations at a certain radius wrt the modulus at the surface (which can be found only experimentally).

The overtones die out toward the center even faster than the fundamental: these oscillations are very much a \emph{surface phenomenon}.

Beyond the \(\eta\)s we can also plot the pressure perturbations: these will not be normalized.

\begin{bluebox}
Slide 4.108: What is \(\omega \)? Should we not have \(\sigma = \omega \) of the mode? It seems like \(\mathcal{P}\) is the period, and the numbers are consistent with \(\mathcal{P} = 2 \pi / \sigma \)

Answer, from the first equation in Schwarzschild 1941\footnote{\url{https://ui.adsabs.harvard.edu/abs/1941ApJ....94..245S/abstract}}: \(\omega \) and \(\sigma \) are related by
%
\begin{align}
  \omega = \frac{\sigma}{\sqrt{\pi G \Gamma_1 \rho_{0c}}}
  \,,
\end{align}
%
so \(\omega \) is adimensional: it is, in a certain sense, an adimensionalized frequency. From the data in slide 4.108 we can derive the value of \(\Gamma_1 \rho_{0c} = (\sigma / \omega )^2 / (\pi G) \approx \SI{5.184e14}{kg/m^3}\).
\end{bluebox}

\subsubsection{Numerical RR Lyrae models}

We integrate the stellar structure equations numerically, so instead of a simple polytropic equilibrium model our equilibrium model is numerical.
This is done looking at an RR Lyrae variable stars.
% \todo[inline]{What are their characteristics?}

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{figures/RR_Lyr_frequency_differences.pdf}
\caption{RR Lyrae frequency differences. The frequencies are measured in \SI{}{rad / d}.}
\label{fig:RR_Lyr_frequency_differences}
\end{figure}

We find that the ratio between the period of the fundamental and of the first overtone is around \(P_1 / P_0 \approx \num{.743} \divisionsymbol \num{.745}\) in observed RR Lyrae, while the LAWE model predicts \(\num{.731}\).
The observed period is around \(P_0 \approx \SI{.5668}{d}\), while the model predicts \(P_0 \sim \SI{.6454}{d}\).

We can see that \(\sigma_m - \sigma_{m-1} \approx \const\) when \(m\) gets large, see figure \ref{fig:RR_Lyr_frequency_differences}.

The wavefunctions die out faster than the polytropic model when \(r \rightarrow 0\).

There are ``bumps'' in the pressure perturbation plot: these are the partial ionization regions of \ce{H} and \ce{He}.

These appear because we start from a solution of the stellar structure equations, where all the properties of stellar matter were used, to start off with the LAWE.

\section{Non-adiabatic oscillations}

\subsection{Driving and damping layers}

How can we tell, theoretically, how stable and how wide the various modes are?
We expect to see the stable modes, and not to see the unstable ones.

Let us start from the Lagrangian momentum conservation:

\begin{equation}
    \pdv[2]{r}{t} = - 4 \pi r^2 \pdv{P}{m} - \frac{Gm}{r^2}
\end{equation}

and apply to it the identity: \(\frac[i]{1}{2} \pdv{}{t} v^2 = \pdv{r}{t} \pdv[2]{r}{t}\), by multiplying everything by \(\pdv*{r}{t} \), and then we integrate everything with respect to \(m\): we find
%
\begin{align}
\int_{M} \frac{1}{2} \pdv{}{t} \qty(v^2) \dd{m} =
\hlc{olive}{- \int_{M} 4 \pi r^2 \pdv{P}{m} \pdv{r}{t} \dd{m} }
\hlc{teal}{- \int_{M} \frac{Gm}{r^2} \pdv{r}{t} \dd{m}}
\,.
\end{align}
%

Now we apply some manipulations: first of all, the following:
%
\begin{subequations}
\begin{align}
\hlc{teal}{\int_{M} \qty(- \frac{Gm}{r^2} \pdv{r}{t}) \dd{m}} &=
\int_{M} \pdv{}{t} \qty(\frac{Gm}{r}) \dd{m}  \\
&= \dv{}{t} \int_{M} \frac{Gm}{r} \dd{m}  \\
&= \hlc{teal}{- \dv{\Omega }{t}}
\,
\end{align}
\end{subequations}
%
where \(\Omega \) is the total gravitational potential energy, while
%
\begin{subequations}
\begin{align}
\hlc{olive}{- \int_{M} 4 \pi r^2 \pdv{P}{m} \pdv{r}{t} \dd{m}} &=
- \int_{M} \pdv{}{m} \qty(4 \pi r^2 P \pdv{r}{t}) \dd{m}
+ \int_{M} P \pdv{}{m} \qty(4 \pi r^2 \pdv{r}{t}) \dd{m}  \\
&= - \underbrace{\eval{4 \pi r^2 P \pdv{r}{t}}_{m=0}^{m=M}}_{=0}
+ \int_{M} \frac{4 \pi }{3} P \pdv{}{m} \pdv{}{t} \qty(r^3) \dd{m}  \\
&= \int_{M} P \pdv{}{t} \underbrace{\qty(4 \pi r^2 \pdv{r}{m})}_{1 / \rho } \dd{m}  \marginnote{Commuted \(m\) and \(r\) derivatives}\\
&= \hlc{olive}{\int_{M} P \pdv{}{t} \qty(\rho^{-1}) \dd{m}}
\,,
\end{align}
\end{subequations}
%
so in the end we get
%
\begin{equation}
\pdv{}{t} \int_M \frac{v^2}{2} \dd{m} =
\hlc{teal}{- \dv{\Omega}{t} } +
\hlc{olive}{\int_M P \pdv{}{t} \frac{1}{\rho} \dd{m}}
\end{equation}

We integrate in time over a pulsation period, which cancels out the gravitational potential term which is conservative:
%
\begin{align}
-\int_{\Pi } \dv{\Omega }{t} \dd{t} = 0
\,.
\end{align}

If we also divide by the period, we are computing an average over a period \(\Pi \). The term
%
\begin{align}
\expval{\pdv{}{t} \int_{M} \frac{v^2}{2} \dd{m}}_{\Pi }
= \frac{1}{\Pi } \int_{\Pi } \pdv{}{t} \int_{M} \frac{v^2}{2} \dd{m} \dd{t} = \frac{ \Delta E _{\text{kin}}}{\Pi }
\,
\end{align}
%
is the average power converted to kinetic energy; the variation in kinetic energy is also the work done on the system: \(\Delta E _{\text{kin}} = W\). So, taking the average on both sides of the equation we get
%
\begin{equation} \label{eq:driving-damping-layers-1}
\expval{\dv{W}{t}}_\Pi = \frac{1}{\Pi} \int_\Pi \int_M   P \pdv{}{t} \qty(\frac{1}{\rho}) \dd{m} \dd{t}.
\end{equation}

Some layers will provide energy to the oscillation motion (\emph{drive} it), some others will \emph{damp} it.
These are characterized by the sign of their contribution the RHS of this equation.

If it is positive, we have instability since more and more work is being done one the star every period; if it is negative the pulsations will tend to die out, giving stability, since they lose kinetic energy every pulse.

The average time scale of change of the perturbations is

\begin{equation}
  \kappa \defeq \frac{1}{\tau} = -\frac{1}{2} \frac{\expval{\dv*{W}{t}}_\Pi}{\expval{ \delta \psi}_\Pi }
\,,
\end{equation}
%
where \(\expval{ \delta \psi }\) is the ``average pulsational energy per pulsation cycle'':
\todo[inline]{
so, the integral of the absolute value of \(\dv*{W}{t}\)?
This \(\psi \) was not defined...

Also, this way \(\tau \) can be negative: maybe the proper definition is \(\tau = 1/ \abs{\kappa }\)?
}

So, if \(\kappa < 0\) we are looking at a driving layer, since then \(\expval{ \dv*{W}{t}} >0\), while \(\kappa > 0\) means we are in a damping layer.

The term \(\expval{\dv*{W}{t}}_\Pi \) can also be interpreted as the net heat gain fed into mechanical work during a pulsation cycle: this can be derived from the first law of thermodynamics (written in its per-unit-mass form, and with the differentials being applied to time derivatives):
%
\begin{align}
\dv{Q}{t} = \dv{E}{t} + P \dv{(\rho^{-1})}{t}
\,,
\end{align}
%
so if this is averaged over a period the internal energy change is approximately zero (thermal (i.\ e.\ internal) energy changes on the thermal timescale, which is much longer than the dynamical timescale), so we have
%
\begin{align}
\expval{\dv{Q}{t}}_{\Pi } = \expval{P \dv{}{t} (\rho^{-1})}
\,,
\end{align}
%
and therefore we can rewrite equation \eqref{eq:driving-damping-layers-1} as
%
\begin{align}
\expval{\dv{W}{t}}_{\Pi } = \frac{1}{\Pi } \int_{\Pi } \int_{M} \dv{Q}{t} \dd{m} \dd{t}
\,,
\end{align}
%
where \(Q\) is a heat per unit mass, so its integral in \(\dd{m}\) is the total heat change across all stellar layers.

In the adiabatic case, we had
%
\begin{align}
\pdv{}{t}\qty( \frac{\delta P}{P} - \Gamma_1 \frac{\delta \rho}{\rho}) =0
\,,
\end{align}
%
(see equation \eqref{eq:perturbed-linearized-energy-log-pressure}, in which we neglected the heat variation terms in the adiabatic case) so the perturbations were in phase.

Now we add a term to the time derivatives: the pressure and density perturbations will stop being in phase. The sign of the heat variation term gives us the difference between \emph{driving} heat transfer and \emph{damping} heat transfer. Equation \eqref{eq:perturbed-linearized-energy-log-pressure} will now look like
%
\begin{align}
\pdv{}{t} \frac{ \delta P}{P} = \Gamma_1 \pdv{}{t} \frac{ \delta \rho }{\rho } + \frac{P}{\rho } (\Gamma_3 -1) \delta \dv{Q}{t}
\,,
\end{align}
%
so the instants of minimum pressure and minimum density will not be synchronized anymore, since there is that extra \(\dv*{Q}{t}\) term.

In a PV diagram, we can see that these correspond to right and left oriented loops (as opposed to the loops with zero total signed area we had in the adiabatic case).

The total work performed on the star is given by the area of the loops: the area is positive for a loop going clockwise, and negative for a loop going counter-clockwise.

If the term \(\delta \qty(\dv*{Q}{t})>0\) we are looking at a \emph{driving layer}, since at the time of maximum compression (\(\pdv*{}{t} (\delta \rho  / \rho ) = 0\)) we will have \(\pdv*{}{t} \qty(\delta P / P) > 0\): the maximum pressure perturbation will happen as the layer is expanding, so the expansion will be amplified.
The symmetric reasoning gives us the fact that if \(\delta \qty(\dv*{Q}{t}) < 0\) we have a \emph{damping layer}.

There tend to be more driving layers in the outer parts of the star, especially peaking in power generation in the partial Helium and Hydrogen ionization regions.

\todo[inline]{What is going on in these regions? Why do they act this way?}

\begin{bluebox}
The star is effectively a themal engine converting heat into work; this will result in an increased overall entropy of the star, and a smoothing of its temperature gradient, however:
%
\begin{enumerate}
\item the time scales on which this process occurs are much larger than the time scales on which oscillating motions are created and destroyed (specifically, the entropy changes on the thermal time scale);
\item then energies of the oscillations are much smaller than the global thermal energy of the star.
\end{enumerate}
%
therefore this process is typically not relevant.
\end{bluebox}

\end{document}


\documentclass[sigconf, nonacm]{acmart}

\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08emT\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\copyrightyear{2019}
\acmYear{2019}

\usepackage[]{algorithm2e}

\begin{document}

\title{Fair ranking in academic search}
\subtitle{Notebook for the TREC 2019 Fair Ranking Track}

\author{Malte Bonart}
\email{malte.bonart@th-koeln.de}
\orcid{0000-0001-9548-9918}
\affiliation{%
  \institution{Technische Hochschule Köln}
}

\begin{abstract}
This notebook summarizes our participation in the first Fair Ranking Track at TREC 2019 \cite{asia_biega_trec_2019}. We shortly introduce the problem setting, give an overview of the software framework, and discuss the task and the results of our two submissions.
\end{abstract}

\keywords{ranking, algorithmic fairness, learning-to-rank}

\maketitle

\section{Introduction}

The issue of algorithmic fairness has recently gained popularity in the IR community \cite{ekstrand_fairness_2019, castillo_fairness_2019}. In this year's Fair Ranking Track, the goal was to re-rank rankings of scholarly articles from a productive academic search engine such that groups of authors are 'fairly' exposed in the sequence of rankings. The data for this challenge was taken from the \emph{SemanticScholar} open research corpus that contains around 47 million articles \cite{ammar_18}.

It was assumed, that a fair exposure for a group of authors was satisfied if the group's $g$ share of aggregated exposure $\mathcal{E}_g$ equals a group's share of aggregated relevance $\mathcal{R}_g$, such that

\begin{equation}
    \mathcal{E}_g = \frac{\sum_{a \in \mathcal{A}_g}\sum_{\pi \in \Pi}e_a^\pi}{\sum_{a \in \mathcal{A}} \sum_{\pi \in \Pi}e_a^\pi} =
    \frac{\sum_{a \in \mathcal{A}_g}\sum_{\pi \in \Pi}r_a^\pi}{\sum_{a \in \mathcal{A}} \sum_{\pi \in \Pi}r_a^\pi} = \mathcal{R}_g,
\label{eq:fairness}
\end{equation}
with $\mathcal{A}$ the set of all authors, $\mathcal{A}_g$ the set of authors in the group $g$, $\Pi$ the set of all rankings, $e^\pi_a$ the single exposure and $r^\pi_a$ the individual relevance of author $a$ in ranking $\pi$.

Individual exposure and relevance for a single ranking $\pi$ and an author $a$ was defined in the spirit of the Expected Reciprocal Rank metric \cite{chapelle_expected_2009} as
\begin{equation}
    \begin{split}
        e_a^\pi &= \sum_{i=1}^{|\pi|} \Big[\gamma^{i-1} \prod_{j=1}^{i-1}(1-f(r_{\pi_j}))\Big]\mathbf{I}(\pi_i \in \mathcal{D}_a) \\
        r_a^\pi &= \sum_{i=1}^{|\pi|}\Big[f(r_{\pi_i})\Big]\mathbf{I}(\pi_i \in \mathcal{D}_a),
    \end{split}
\end{equation}
where $0 < \gamma < 1$ a discounting factor, $f(r_{\pi_i})$ a monotonic transformation of the relevance $r_{\pi_i}$ of document $\pi_i$ into a probability value and $\mathcal{D}_a$ the set of documents from author $a$.

Note first, that an author's \emph{relevance} metric is independent of the ranking. Second, an author's \emph{exposure} metric at position $i$ is independent of the relevance of the document at that specific position, but it depends on the relevance of the previous documents in the ranking. Exposure is high if the author's documents are at the beginning of the ranking or if the previously seen documents have a low relevance value.

Individual relevance and exposure metrics are summed up and normalized for each group. Unfairness for group $g$ is then measured as the deviation from the ideal fairness condition given by \eqref{eq:fairness}. This approach resembles existing group fairness perspectives in the literature \cite{singh_fairness_2018, zehlike_reducing_2018, biega_equity_2018}. However, this setting is different from the usual fairness problems in IR, as we do not assume a particular 'protected' group but allow for various group definitions and sizes. Also, exposure and relevance are not measured for the ranked items themselves but the underlying authors of the items. Hence, ranked items can belong to several groups, and the effect of re-ranking a single item can have multiple opposing effects on the overall fairness of the system.

The system's overall unfairness $\Delta$ was measured as the Euclidean norm over each group's unfairness,
\begin{equation}
    \Delta = \sqrt{\sum_{g \in \mathcal{G}} (\mathcal{E}_g - \mathcal{R}_g)^2},
\end{equation}
and the system's overall relevance $U$ for the users was measured as the average Reciprocal Rank metric for all rankings,
\begin{equation}
    U = \frac{1}{|\Pi|}\sum_{\pi \in \Pi} \sum_{i=1}^{|\pi|} \Big[\gamma^{i-1} \prod_{j=1}^{i-1}(1-f(r_{\pi_j}))\Big]f(r_{\pi_i}).
\end{equation}
The system has two objectives: Minimizing group unfairness $\Delta$ for the authors of the ranked items while maximizing the users' relevance $U$. This can be formalized as a weighted sum optimization problem:
\begin{equation}
\begin{aligned}
& \underset{\pi_i}{\text{min}}
& & \alpha \Delta - (1-\alpha)U
\end{aligned}
\label{eq:optimization}
\end{equation}

As a further challenge, the authors of the Fair Ranking Track assumed that no information about author group relationships, $\mathcal{A}_g$ for $g \in \mathcal{G}$, was available during training time, e.g., before a ranking was submitted. The intention was that this restriction leads to systems that are robust against various changing group definitions. Therefore, direct optimization of \eqref{eq:optimization} is not feasible without further knowledge of how authors are partitioned into groups.

In an initial attempt to study this problem and to provide a reference for more elaborated models, we asked: How well do already existing learning-to-rank retrieval systems and simple heuristic baselines perform in terms of fairness and relevance?

\section{Framework and Submissions}

In the following, we describe the data pipeline and software framework that is publicly available and open-source \cite{malte_bonart_2019_3514668}. Its modularity allows for the additional integration and evaluation of models without further effort. For this year's track, we created to submissions: First, a random shuffling of the documents in each ranking without considering further information and second, a ranking model based on the \emph{LambdaMart} \cite{burges_ranknet_2010, ma_jma127/pyltr_2019} algorithm and several features that we constructed from the corpus.

The \emph{SemanticScholar} corpus has been downloaded from \cite{ammar_18} and written into a local \emph{Elasticsearch} database with integrated learning to rank plugin \cite{noauthor_o19s/elasticsearch-learning--rank_2019}. We wrote several modules that are outlined in figure \ref{fig:model}. The \emph{Corpus} class serves as an interface to the database. The \emph{FeatureEngineer} class calculates a feature matrix given a query and a corresponding set of documents $\pi$. Table \ref{tab:features} lists the $10$ features that are currently implemented.

\begin{figure}[t]
    \centering
    \includegraphics[width=\columnwidth]{model.pdf}
    \caption{Class diagram that describes the software modules and their dependencies.}
    \label{fig:model}
\end{figure}

\begin{table}[t]
    \centering
    \begin{tabular}{ll}
    \toprule
    level & feature \\
    \midrule
    query-document (BM25) &  title\\
    & abstract \\
    & entities \\
    & venue \\
    & journal \\
    & authors' names \\
    \midrule
    document &  year\\
    & out-citations \\
    & in-citations \\
    \midrule
    query & query length \\
    \bottomrule
    \end{tabular}
    \caption{Features used for the system}
    \label{tab:features}
\end{table}

The \emph{Model} class serves as an interface to different model implementations, and it allows for the integration of further approaches. Given a group definition, the \emph{Evaluator} class can be used to calculate the performance of a trained system in terms of searcher's relevance $U$ and author's unfairness $\Delta$. Finally, the \emph{InputOutputHandler} class is used to handle and parse the various input files and to prepare a valid output file for submission. The system accepts a list of query sequences, a list of rankings, and a partition of authors into groups.

Some of the assessed documents were not included in the corpus and have been removed. Further, as proposed by the organizers \cite{asia_biega_trec_2019}, rankings with fewer than five entries were also removed. The removal resulted in a drop of $15\%$ of the training queries and led to roughly $3800$ documents with a relevance judgement.

\section{Results}

The test files consisted of $5$ sequences of query-ranking pairs. Each sequence had $25000$ entries, that consist of a query and a list of corresponding unlabelled documents. The query sequences were created by sampling queries with replacement from an original test set of $635$ unique queries with around $4300$ corresponding documents. The specific sampling process is not known.

The submissions' performance was assessed by calculating the aggregated relevance $U$ and the aggregated unfairness $\Delta$ based on two different author partitions: First, groups were calculated according to their productivity and impact, approximated by the h-index. And second, according to the economic level of their country of origin. Note that the group definitions were not known beforehand and were only revealed after the submission phase.

\begin{table}[h]
    \centering
    \begin{tabular}{lccc}
    \toprule
    & \multicolumn{2}{c}{unfairness} & relevance \\
    \cmidrule(r){2-3} \cmidrule(l){4-4}
     & $\Delta_{hindex}$ & $\Delta_{level}$ & $U$  \\
    min  & 0.0405 & 0.0059 & 0.5480  \\
    mean & 0.0753 & 0.0435 & 0.6100  \\
    max  & 0.1110 & 0.0832 & 0.6740  \\
    \midrule
    random & 0.0405 & 0.0326 & 0.5480 \\
    lambdamart & 0.0855 &0.0741 & 0.6600  \\
    \bottomrule
    \end{tabular}
    \caption{Aggregated test results, averaged over the five query sequences.}
    \label{tab:results}
\end{table}

Table \ref{tab:results} summarises the aggregated performance of all submissions. For better readability, results from the five query-sequences have been averaged as we did not find any large differences between the individual sequences.

Interestingly, our randomized submission has the lowest unfairness value for the h-index partition, and a below-average unfairness for the economic level group. It also performs worst for relevance. The second submission, the \emph{LambdaMart} algorithm, optimized users' relevance. As expected, it performed above average and achieved a relevance score close to the maximum. In terms of unfairness, this method performs above average as well but does not come close to the maximum unfairness values observed. Therefore, as this method utilizes a known learning-to-rank retrieval framework, it provides a useful baseline for both unfairness and relevance. In addition, it shows the magnitude of unfairness that current systems, which solely optimize for relevance, produce.

\section{Discussion}

For the first Fair Ranking Track at TREC 2019, we utilized a basic learning-to-rank framework for solving this task. The main challenge was that multiple objectives (searchers' relevance and authors' group fairness) had to be optimized under the constraint of an unknown author-group-partition. This constraint seems not practicable, as one can presumably always find an arbitrary group definition that \emph{maximizes unfairness} for a given list of rankings. Existing fairness frameworks in the literature require some group definitions during training time or, for post-processing methods, the group definitions are required for re-ranking a system's output \emph{before} the final submission \cite{zehlike_fairsearch:_2019}. Further research should concentrate on first, exploring the solution space of the optimization problem and analytically study the trade-offs between high fairness and high relevance. Second, robust ranking models should be implemented. Ideally, they can be trained without requiring a specific group definition, but should later handle various group definitions when making actual ranking decisions.

\begin{acks}
This research was supported by the Digital Society research program funded by the Ministry of Culture and Science of the German State of North Rhine-Westphalia.
\end{acks}

\bibliographystyle{ACM-Reference-Format}
\bibliography{literature}
\end{document}