# Randomized Algorithms (Spring 2010)/Tail inequalities

When applying probabilistic analysis, we often want a bound in form of ${\displaystyle \Pr[X\geq t]<\epsilon }$ for some random variable ${\displaystyle X}$ (think that ${\displaystyle X}$ is a cost such as running time of a randomized algorithm). We call this a tail bound, or a tail inequality.

In principle, we can bound ${\displaystyle \Pr[X\geq t]}$ by directly estimating the probability of the event that ${\displaystyle X\geq t}$. Besides this ad hoc way, we want to have some general tools which estimate tail probabilities based on certain information regarding the random variables.

## The Moment Methods

### Markov's inequality

One of the most natural information about a random variable is its expectation, which is the first moment of the random variable. Markov's inequality draws a tail bound for a random variable from its expectation.

 Theorem (Markov's Inequality) Let ${\displaystyle X}$ be a random variable assuming only nonnegative values. Then, for all ${\displaystyle t>0}$, {\displaystyle {\begin{aligned}\Pr[X\geq t]\leq {\frac {\mathbf {E} [X]}{t}}.\end{aligned}}}
Proof.
 Let ${\displaystyle Y}$ be the indicator such that {\displaystyle {\begin{aligned}Y&={\begin{cases}1&{\mbox{if }}X\geq t,\\0&{\mbox{otherwise.}}\end{cases}}\end{aligned}}} It holds that ${\displaystyle Y\leq {\frac {X}{t}}}$. Since ${\displaystyle Y}$ is 0-1 valued, ${\displaystyle \mathbf {E} [Y]=\Pr[Y=1]=\Pr[X\geq t]}$. Therefore, ${\displaystyle \Pr[X\geq t]=\mathbf {E} [Y]\leq \mathbf {E} \left[{\frac {X}{t}}\right]={\frac {\mathbf {E} [X]}{t}}.}$
${\displaystyle \square }$
Example (from Las Vegas to Monte Carlo)

Let ${\displaystyle A}$ be a Las Vegas randomized algorithm for a decision problem ${\displaystyle f}$, whose expected running time is within ${\displaystyle T(n)}$ on any input of size ${\displaystyle n}$. We transform ${\displaystyle A}$ to a Monte Carlo randomized algorithm ${\displaystyle B}$ with bounded one-sided error as follows:

${\displaystyle B(x)}$:
• Run ${\displaystyle A(x)}$ for ${\displaystyle 2T(n)}$ long where ${\displaystyle n}$ is the size of ${\displaystyle x}$.
• If ${\displaystyle A(x)}$ returned within ${\displaystyle 2T(n)}$ time, then return what ${\displaystyle A(x)}$ just returned, else return 1.

Since ${\displaystyle A}$ is Las Vegas, its output is always correct, thus ${\displaystyle B(x)}$ only errs when it returns 1, thus the error is one-sided. The error probability is bounded by the probability that ${\displaystyle A(x)}$ runs longer than ${\displaystyle 2T(n)}$. Since the expected running time of ${\displaystyle A(x)}$ is at most ${\displaystyle T(n)}$, due to Markov's inequality,

${\displaystyle \Pr[{\mbox{the running time of }}A(x)\geq 2T(n)]\leq {\frac {\mathbf {E} [{\mbox{running time of }}A(x)]}{2T(n)}}\leq {\frac {1}{2}},}$

thus the error probability is bounded.

This easy reduction implies that ZPP${\displaystyle \subseteq }$RP.

#### Generalization

For any random variable ${\displaystyle X}$, for an arbitrary non-negative real function ${\displaystyle h}$, the ${\displaystyle h(X)}$ is a non-negative random variable. Applying Markov's inequality, we directly have that

${\displaystyle \Pr[h(X)\geq t]\leq {\frac {\mathbf {E} [h(X)]}{t}}.}$

This trivial application of Markov's inequality gives us a powerful tool for proving tail inequalities. With the function ${\displaystyle h}$ which extracts more information about the random variable, we can prove sharper tail inequalities.

### Variance

 Definition (variance) The variance of a random variable ${\displaystyle X}$ is defined as {\displaystyle {\begin{aligned}\mathbf {Var} [X]=\mathbf {E} \left[(X-\mathbf {E} [X])^{2}\right]=\mathbf {E} \left[X^{2}\right]-(\mathbf {E} [X])^{2}.\end{aligned}}} The standard deviation of random variable ${\displaystyle X}$ is ${\displaystyle \delta [X]={\sqrt {\mathbf {Var} [X]}}.}$

We have seen that due to the linearity of expectations, the expectation of the sum of variable is the sum of the expectations of the variables. It is natural to ask whether this is true for variances. We find that the variance of sum has an extra term called covariance.

 Definition (covariance) The covariance of two random variables ${\displaystyle X}$ and ${\displaystyle Y}$ is {\displaystyle {\begin{aligned}\mathbf {Cov} (X,Y)=\mathbf {E} \left[(X-\mathbf {E} [X])(Y-\mathbf {E} [Y])\right].\end{aligned}}}

We have the following theorem for the variance of sum.

 Theorem For any two random variables ${\displaystyle X}$ and ${\displaystyle Y}$, {\displaystyle {\begin{aligned}\mathbf {Var} [X+Y]=\mathbf {Var} [X]+\mathbf {Var} [Y]+2\mathbf {Cov} (X,Y).\end{aligned}}} Generally, for any random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$, {\displaystyle {\begin{aligned}\mathbf {Var} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {Var} [X_{i}]+\sum _{i\neq j}\mathbf {Cov} (X_{i},X_{j}).\end{aligned}}}
Proof.
 The equation for two variables is directly due to the definition of variance and covariance. The equation for ${\displaystyle n}$ variables can be deduced from the equation for two variables.
${\displaystyle \square }$

We will see that when random variables are independent, the variance of sum is equal to the sum of variances. To prove this, we first establish a very useful result regarding the expectation of multiplicity.

 Theorem For any two independent random variables ${\displaystyle X}$ and ${\displaystyle Y}$, {\displaystyle {\begin{aligned}\mathbf {E} [X\cdot Y]=\mathbf {E} [X]\cdot \mathbf {E} [Y].\end{aligned}}}
Proof.
 {\displaystyle {\begin{aligned}\mathbf {E} [X\cdot Y]&=\sum _{x,y}xy\Pr[X=x\wedge Y=y]\\&=\sum _{x,y}xy\Pr[X=x]\Pr[Y=y]\\&=\sum _{x}x\Pr[X=x]\sum _{y}y\Pr[Y=y]\\&=\mathbf {E} [X]\cdot \mathbf {E} [Y].\end{aligned}}}
${\displaystyle \square }$

With the above theorem, we can show that the covariance of two independent variables is always zero.

 Theorem For any two independent random variables ${\displaystyle X}$ and ${\displaystyle Y}$, {\displaystyle {\begin{aligned}\mathbf {Cov} (X,Y)=0.\end{aligned}}}
Proof.
 {\displaystyle {\begin{aligned}\mathbf {Cov} (X,Y)&=\mathbf {E} \left[(X-\mathbf {E} [X])(Y-\mathbf {E} [Y])\right]\\&=\mathbf {E} \left[X-\mathbf {E} [X]\right]\mathbf {E} \left[Y-\mathbf {E} [Y]\right]&\qquad ({\mbox{Independence}})\\&=0.\end{aligned}}}
${\displaystyle \square }$

We then have the following theorem for the variance of the sum of pairwise independent random variables.

 Theorem For pairwise independent random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$, {\displaystyle {\begin{aligned}\mathbf {Var} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {Var} [X_{i}].\end{aligned}}}
Remark
The theorem holds for pairwise independent random variables, a much weaker independence requirement than the mutual independence. This makes the variance-based probability tools work even for weakly random cases. We will see what it exactly means in the future lectures.

#### Variance of binomial distribution

For a Bernoulli trial with parameter ${\displaystyle p}$.

${\displaystyle X={\begin{cases}1&{\mbox{with probability }}p\\0&{\mbox{with probability }}1-p\end{cases}}}$

The variance is

${\displaystyle \mathbf {Var} [X]=\mathbf {E} [X^{2}]-(\mathbf {E} [X])^{2}=\mathbf {E} [X]-(\mathbf {E} [X])^{2}=p-p^{2}=p(1-p).}$

Let ${\displaystyle Y}$ be a binomial random variable with parameter ${\displaystyle n}$ and ${\displaystyle p}$, i.e. ${\displaystyle Y=\sum _{i=1}^{n}Y_{i}}$, where ${\displaystyle Y_{i}}$'s are i.i.d. Bernoulli trials with parameter ${\displaystyle p}$. The variance is

{\displaystyle {\begin{aligned}\mathbf {Var} [Y]&=\mathbf {Var} \left[\sum _{i=1}^{n}Y_{i}\right]\\&=\sum _{i=1}^{n}\mathbf {Var} \left[Y_{i}\right]&\qquad ({\mbox{Independence}})\\&=\sum _{i=1}^{n}p(1-p)&\qquad ({\mbox{Bernoulli}})\\&=p(1-p)n.\end{aligned}}}

### Chebyshev's inequality

With the information of the expectation and variance of a random variable, one can derive a stronger tail bound known as Chebyshev's Inequality.

 Theorem (Chebyshev's Inequality) For any ${\displaystyle t>0}$, {\displaystyle {\begin{aligned}\Pr \left[|X-\mathbf {E} [X]|\geq t\right]\leq {\frac {\mathbf {Var} [X]}{t^{2}}}.\end{aligned}}}
Proof.
 Observe that ${\displaystyle \Pr[|X-\mathbf {E} [X]|\geq t]=\Pr[(X-\mathbf {E} [X])^{2}\geq t^{2}].}$ Since ${\displaystyle (X-\mathbf {E} [X])^{2}}$ is a nonnegative random variable, we can apply Markov's inequality, such that ${\displaystyle \Pr[(X-\mathbf {E} [X])^{2}\geq t^{2}]\leq {\frac {\mathbf {E} [(X-\mathbf {E} [X])^{2}]}{t^{2}}}={\frac {\mathbf {Var} [X]}{t^{2}}}.}$
${\displaystyle \square }$

### Higher moments

The above two inequalities can be put into a general framework regarding the moments of random variables.

 Definition (moments) The ${\displaystyle k}$th moment of a random variable ${\displaystyle X}$ is ${\displaystyle \mathbf {E} [X^{k}]}$.

The more we know about the moments, the more information we have about the distribution, hence in principle, we can get tighter tail bounds. This technique is called the ${\displaystyle k}$th moment method.

We know that the ${\displaystyle k}$th moment is ${\displaystyle \mathbf {E} [X^{k}]}$. More generally, the ${\displaystyle k}$th moment about ${\displaystyle c}$ is ${\displaystyle \mathbf {E} [(X-c)^{k}]}$. The central moment of ${\displaystyle X}$, denoted ${\displaystyle \mu _{k}[X]}$, is defined as ${\displaystyle \mu _{k}[X]=\mathbf {E} [(X-\mathbf {E} [X])^{k}]}$. So the variance is just the second central moment ${\displaystyle \mu _{2}[X]}$.

The ${\displaystyle k}$th moment method is stated by the following theorem.

 Theorem (the ${\displaystyle k}$th moment method) For even ${\displaystyle k>0}$, and any ${\displaystyle t>0}$, {\displaystyle {\begin{aligned}\Pr \left[|X-\mathbf {E} [X]|\geq t\right]\leq {\frac {\mu _{k}[X]}{t^{k}}}.\end{aligned}}}
Proof.
 Apply Markov's inequality to ${\displaystyle (X-\mathbf {E} [X])^{k}}$.
${\displaystyle \square }$

How about the odd ${\displaystyle k}$? For odd ${\displaystyle k}$, we should apply Markov's inequality to ${\displaystyle |X-\mathbf {E} [X]|^{k}}$, but estimating expectations of absolute values can be hard.

## Select the Median

The selection problem is the problem of finding the ${\displaystyle k}$th smallest element in a set ${\displaystyle S}$. A typical case of selection problem is finding the median.

 Definition The median of a set ${\displaystyle S}$ is the ${\displaystyle (\lceil n/2\rceil )}$th element in the sorted order of ${\displaystyle S}$.

The median can be found in ${\displaystyle O(n\log n)}$ time by sorting. There is a linear-time deterministic algorithm, "median of medians" algorithm, which is quite sophisticated. Here we introduce a much simpler randomized algorithm which also runs in linear time.

### Randomized median selection algorithm

We introduce a randomized median selection algorithm called LazySelect, which is a variant on a randomized algorithm due to Floyd and Rivest

The idea of this algorithm is random sampling. For a set ${\displaystyle S}$, let ${\displaystyle m\in S}$ denote the median. We observe that if we can find two elements ${\displaystyle d,u\in S}$ satisfying the following properties:

1. The median is between ${\displaystyle d}$ and ${\displaystyle u}$ in the sorted order, i.e. ${\displaystyle d\leq m\leq u}$;
2. The total number of elements between ${\displaystyle d}$ and ${\displaystyle u}$ is small, specially for ${\displaystyle C=\{x\in S\mid d\leq x\leq u\}}$, ${\displaystyle |C|=o(n/\log n)}$.

Provided ${\displaystyle d}$ and ${\displaystyle u}$ with these two properties, within linear time, we can compute the ranks of ${\displaystyle d}$ in ${\displaystyle S}$, construct ${\displaystyle C}$, and sort ${\displaystyle C}$. Therefore, the median ${\displaystyle m}$ of ${\displaystyle S}$ can be picked from ${\displaystyle C}$ in linear time.

So how can we select such elements ${\displaystyle d}$ and ${\displaystyle u}$ from ${\displaystyle S}$? Certainly sorting ${\displaystyle S}$ would give us the elements, but isn't that exactly what we want to avoid in the first place?

Observe that ${\displaystyle d}$ and ${\displaystyle u}$ are only asked to roughly satisfy some constraints. This hints us maybe we can construct a sketch of ${\displaystyle S}$ which is small enough to sort cheaply and roughly represents ${\displaystyle S}$, and then pick ${\displaystyle d}$ and ${\displaystyle u}$ from this sketch. We construct the sketch by randomly sampling a relatively small number of elements from ${\displaystyle S}$. Then the strategy of algorithm is outlined by:

• Sample a set ${\displaystyle R}$ of elements from ${\displaystyle S}$.
• Sort ${\displaystyle R}$ and choose ${\displaystyle d}$ and ${\displaystyle u}$ somewhere around the median of ${\displaystyle R}$.
• If ${\displaystyle d}$ and ${\displaystyle u}$ have the desirable properties, we can compute the median in linear time, or otherwise the algorithm fails.

The parameters to be fixed are: the size of ${\displaystyle R}$ (small enough to sort in linear time and large enough to contain sufficient information of ${\displaystyle S}$); and the order of ${\displaystyle d}$ and ${\displaystyle u}$ in ${\displaystyle R}$ (not too close to have ${\displaystyle m}$ between them, and not too far away to have ${\displaystyle C}$ sortable in linear time).

We choose the size of ${\displaystyle R}$ as ${\displaystyle n^{3/4}}$, and ${\displaystyle d}$ and ${\displaystyle u}$ are within ${\displaystyle {\sqrt {n}}}$ range around the median of ${\displaystyle R}$.

 LazySelect Input: a set ${\displaystyle S}$ of ${\displaystyle n}$ elements over totally ordered domain. Pick a multi-set ${\displaystyle R}$ of ${\displaystyle \left\lceil n^{3/4}\right\rceil }$ elements in ${\displaystyle S}$, chosen independently and uniformly at random with replacement, and sort ${\displaystyle R}$. Let ${\displaystyle d}$ be the ${\displaystyle \left\lfloor {\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right\rfloor }$-th smallest element in ${\displaystyle R}$, and let ${\displaystyle u}$ be the ${\displaystyle \left\lceil {\frac {1}{2}}n^{3/4}+{\sqrt {n}}\right\rceil }$-th smallest element in ${\displaystyle R}$. Construct ${\displaystyle C=\{x\in S\mid d\leq x\leq u\}}$ and compute the ranks ${\displaystyle r_{d}=|\{x\in S\mid x and ${\displaystyle r_{u}=|\{x\in S\mid x. If ${\displaystyle r_{d}>{\frac {n}{2}}}$ or ${\displaystyle r_{u}<{\frac {n}{2}}}$ or ${\displaystyle |C|>4n^{3/4}}$ then return FAIL. Sort ${\displaystyle C}$ and return the ${\displaystyle \left(\left\lfloor {\frac {n}{2}}\right\rfloor -r_{d}+1\right)}$th element in the sorted order of ${\displaystyle C}$.

"Sample with replacement" (有放回采样) means that after sampling an element, we put the element back to the set. In this way, each sampled element is independently and identically distributed (i.i.d) (独立同分布). In the above algorithm, this is for our convenience of analysis.

### Analysis

The algorithm always terminates in linear time because each line of the algorithm costs at most linear time. The last three line guarantees that the algorithm returns the correct median if it does not fail.

We then only need to bound the probability that the algorithm returns a FAIL. Let ${\displaystyle m\in S}$ be the median of ${\displaystyle S}$. By Line 4, we know that the algorithm returns a FAIL if and only if at least one of the following events occurs:

• ${\displaystyle {\mathcal {E}}_{1}:Y=|\{x\in R\mid x\leq m\}|<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$;
• ${\displaystyle {\mathcal {E}}_{2}:Z=|\{x\in R\mid x\geq m\}|<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$;
• ${\displaystyle {\mathcal {E}}_{3}:|C|>4n^{3/4}}$.

${\displaystyle {\mathcal {E}}_{3}}$ directly follows the third condition in Line 4. ${\displaystyle {\mathcal {E}}_{1}}$ and ${\displaystyle {\mathcal {E}}_{2}}$ are a bit tricky. The first condition in Line 4 is that ${\displaystyle r_{d}>{\frac {n}{2}}}$, which looks not exactly the same as ${\displaystyle {\mathcal {E}}_{1}}$, but both ${\displaystyle {\mathcal {E}}_{1}}$ and that ${\displaystyle r_{d}>{\frac {n}{2}}}$ are equivalent to the same event: the ${\displaystyle \left\lfloor {\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right\rfloor }$-th smallest element in ${\displaystyle R}$ is greater than ${\displaystyle m}$, thus they are actually equivalent. Similarly, ${\displaystyle {\mathcal {E}}_{2}}$ is equivalent to the second condition of Line 4.

We now bound the probabilities of these events one by one.

 Lemma 1 ${\displaystyle \Pr[{\mathcal {E}}_{1}]\leq {\frac {1}{4}}n^{-1/4}}$.
Proof.
 Let ${\displaystyle X_{i}}$ be the ${\displaystyle i}$th sampled element in Line 1 of the algorithm. Let ${\displaystyle Y_{i}}$ be a indicator random variable such that ${\displaystyle Y_{i}={\begin{cases}1&{\mbox{if }}X_{i}\leq m,\\0&{\mbox{otherwise.}}\end{cases}}}$ It is obvious that ${\displaystyle Y=\sum _{i=1}^{n^{3/4}}Y_{i}}$, where ${\displaystyle Y}$ is as defined in ${\displaystyle {\mathcal {E}}_{1}}$. For every ${\displaystyle X_{i}}$, there are ${\displaystyle \left\lceil {\frac {n}{2}}\right\rceil }$ elements in ${\displaystyle S}$ that are less than or equal to the median. The probability that ${\displaystyle Y_{i}=1}$ is ${\displaystyle p=\Pr[Y_{i}=1]=\Pr[X_{i}\leq m]={\frac {1}{n}}\left\lceil {\frac {n}{2}}\right\rceil ,}$ which is within the range of ${\displaystyle \left[{\frac {1}{2}},{\frac {1}{2}}+{\frac {1}{2n}}\right]}$. Thus ${\displaystyle \mathbf {E} [Y]=n^{3/4}p\geq {\frac {1}{2}}n^{3/4}.}$ The event ${\displaystyle {\mathcal {E}}_{1}}$ is defined as that ${\displaystyle Y<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$. Note that ${\displaystyle Y_{i}}$'s are Bernoulli trials, and ${\displaystyle Y}$ is the sum of ${\displaystyle n^{3/4}}$ Bernoulli trials, which follows binomial distribution with parameters ${\displaystyle n^{3/4}}$ and ${\displaystyle p}$. Thus, the variance is ${\displaystyle \mathbf {Var} [Y]=n^{3/4}p(1-p)\leq {\frac {1}{4}}n^{3/4}.}$ Applying Chebyshev's inequality, {\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}_{1}]&=\Pr \left[Y<{\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right]\\&\leq \Pr \left[|Y-\mathbf {E} [Y]|>{\sqrt {n}}\right]\\&\leq {\frac {\mathbf {Var} [Y]}{n}}\\&\leq {\frac {1}{4}}n^{-1/4}.\end{aligned}}}
${\displaystyle \square }$

By a similar analysis, we can obtain the following bound for the event ${\displaystyle {\mathcal {E}}_{2}}$.

 Lemma 2 ${\displaystyle \Pr[{\mathcal {E}}_{2}]\leq {\frac {1}{4}}n^{-1/4}}$.

We now bound the probability of the event ${\displaystyle {\mathcal {E}}_{3}}$.

 Lemma 3 ${\displaystyle \Pr[{\mathcal {E}}_{3}]\leq {\frac {1}{2}}n^{-1/4}}$.
Proof.
 The event ${\displaystyle {\mathcal {E}}_{3}}$ is defined as that ${\displaystyle |C|>4n^{3/4}}$, which by the Pigeonhole Principle, implies that at leas one of the following must be true: ${\displaystyle {\mathcal {E}}_{3}'}$: at least ${\displaystyle 2n^{3/4}}$ elements of ${\displaystyle C}$ is greater than ${\displaystyle m}$; ${\displaystyle {\mathcal {E}}_{3}''}$: at least ${\displaystyle 2n^{3/4}}$ elements of ${\displaystyle C}$ is smaller than ${\displaystyle m}$. We bound the probability that ${\displaystyle {\mathcal {E}}_{3}'}$ occurs; the second will have the same bound by symmetry. Recall that ${\displaystyle C}$ is the region in ${\displaystyle S}$ between ${\displaystyle d}$ and ${\displaystyle u}$. If there are at least ${\displaystyle 2n^{3/4}}$ elements of ${\displaystyle C}$ greater than the median ${\displaystyle m}$ of ${\displaystyle S}$, then the rank of ${\displaystyle u}$ in the sorted order of ${\displaystyle S}$ must be at least ${\displaystyle {\frac {1}{2}}n+2n^{3/4}}$ and thus ${\displaystyle R}$ has at least ${\displaystyle {\frac {1}{2}}n^{3/4}-{\sqrt {n}}}$ samples among the ${\displaystyle {\frac {1}{2}}n-2n^{3/4}}$ largest elements in ${\displaystyle S}$. Let ${\displaystyle X_{i}\in \{0,1\}}$ indicate whether the ${\displaystyle i}$th sample is among the ${\displaystyle {\frac {1}{2}}n-2n^{3/4}}$ largest elements in ${\displaystyle S}$. Let ${\displaystyle X=\sum _{i=1}^{n^{3/4}}X_{i}}$ be the number of samples in ${\displaystyle R}$ among the ${\displaystyle {\frac {1}{2}}n-2n^{3/4}}$ largest elements in ${\displaystyle S}$. It holds that ${\displaystyle p=\Pr[X_{i}=1]={\frac {{\frac {1}{2}}n-2n^{3/4}}{n}}={\frac {1}{2}}-2n^{-1/4}}$. ${\displaystyle X}$ is a binomial random variable with ${\displaystyle \mathbf {E} [X]=n^{3/4}p={\frac {1}{2}}n^{3/4}-2{\sqrt {n}},}$ and ${\displaystyle \mathbf {Var} [X]=n^{3/4}p(1-p)={\frac {1}{4}}n^{3/4}-4n^{1/4}<{\frac {1}{4}}n^{3/4}.}$ Applying Chebyshev's inequality, {\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}_{3}']&=\Pr \left[X\geq {\frac {1}{2}}n^{3/4}-{\sqrt {n}}\right]\\&\leq \Pr \left[|X-\mathbf {E} [X]|\geq {\sqrt {n}}\right]\\&\leq {\frac {\mathbf {Var} [X]}{n}}\\&\leq {\frac {1}{4}}n^{-1/4}.\end{aligned}}} Symmetrically, we have that ${\displaystyle \Pr[{\mathcal {E}}_{3}'']\leq {\frac {1}{4}}n^{-1/4}}$. Applying the union bound ${\displaystyle \Pr[{\mathcal {E}}_{3}]\leq \Pr[{\mathcal {E}}_{3}']+\Pr[{\mathcal {E}}_{3}'']\leq {\frac {1}{2}}n^{-1/4}.}$
${\displaystyle \square }$

Combining the three bounds. Applying the union bound to them, the probability that the algorithm returns a FAIL is at most

${\displaystyle \Pr[{\mathcal {E}}_{1}]+\Pr[{\mathcal {E}}_{2}]+\Pr[{\mathcal {E}}_{3}]\leq n^{-1/4}.}$

Therefore the algorithm always terminates in linear time and returns the correct median with high probability.

## Chernoff Bound

Suppose that we have a fair coin. If we toss it once, then the outcome is completely unpredictable. But if we toss it, say for 1000 times, then the number of HEADs is very likely to be around 500. This striking phenomenon, illustrated in the right figure, is called the concentration. The Chernoff bound captures the concentration of independent trials.

The Chernoff bound is also a tail bound for the sum of independent random variables which may give us exponentially sharp bounds.

Before proving the Chernoff bound, we should talk about the moment generating functions.

### Moment generating functions

The more we know about the moments of a random variable ${\displaystyle X}$, the more information we would have about ${\displaystyle X}$. There is a so-called moment generating function, which "packs" all the information about the moments of ${\displaystyle X}$ into one function.

 Definition The moment generating function of a random variable ${\displaystyle X}$ is defined as ${\displaystyle \mathbf {E} \left[\mathrm {e} ^{\lambda X}\right]}$ where ${\displaystyle \lambda }$ is the parameter of the function.

By Taylor's expansion and the linearity of expectations,

{\displaystyle {\begin{aligned}\mathbf {E} \left[\mathrm {e} ^{\lambda X}\right]&=\mathbf {E} \left[\sum _{k=0}^{\infty }{\frac {\lambda ^{k}}{k!}}X^{k}\right]\\&=\sum _{k=0}^{\infty }{\frac {\lambda ^{k}}{k!}}\mathbf {E} \left[X^{k}\right]\end{aligned}}}

The moment generating function ${\displaystyle \mathbf {E} \left[\mathrm {e} ^{\lambda X}\right]}$ is a function of ${\displaystyle \lambda }$.

### The Chernoff bound

The Chernoff bounds are exponentially sharp tail inequalities for the sum of independent trials. The bounds are obtained by applying Markov's inequality to the moment generating function of the sum of independent trials, with some appropriate choice of the parameter ${\displaystyle \lambda }$.

 Chernoff bound (the upper tail) Let ${\displaystyle X=\sum _{i=1}^{n}X_{i}}$, where ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ are independent Poisson trials. Let ${\displaystyle \mu =\mathbf {E} [X]}$. Then for any ${\displaystyle \delta >0}$, ${\displaystyle \Pr[X\geq (1+\delta )\mu ]\leq \left({\frac {e^{\delta }}{(1+\delta )^{(1+\delta )}}}\right)^{\mu }.}$
Proof.
 For any ${\displaystyle \lambda >0}$, ${\displaystyle X\geq (1+\delta )\mu }$ is equivalent to that ${\displaystyle e^{\lambda X}\geq e^{\lambda (1+\delta )\mu }}$, thus {\displaystyle {\begin{aligned}\Pr[X\geq (1+\delta )\mu ]&=\Pr \left[e^{\lambda X}\geq e^{\lambda (1+\delta )\mu }\right]\\&\leq {\frac {\mathbf {E} \left[e^{\lambda X}\right]}{e^{\lambda (1+\delta )\mu }}},\end{aligned}}} where the last step follows by Markov's inequality. Computing the moment generating function ${\displaystyle \mathbf {E} [e^{\lambda X}]}$: {\displaystyle {\begin{aligned}\mathbf {E} \left[e^{\lambda X}\right]&=\mathbf {E} \left[e^{\lambda \sum _{i=1}^{n}X_{i}}\right]\\&=\mathbf {E} \left[\prod _{i=1}^{n}e^{\lambda X_{i}}\right]\\&=\prod _{i=1}^{n}\mathbf {E} \left[e^{\lambda X_{i}}\right].&({\mbox{for independent random variables}})\end{aligned}}} Let ${\displaystyle p_{i}=\Pr[X_{i}=1]}$ for ${\displaystyle i=1,2,\ldots ,n}$. Then, ${\displaystyle \mu =\mathbf {E} [X]=\mathbf {E} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {E} [X_{i}]=\sum _{i=1}^{n}p_{i}}$. We bound the moment generating function for each individual ${\displaystyle X_{i}}$ as follows. {\displaystyle {\begin{aligned}\mathbf {E} \left[e^{\lambda X_{i}}\right]&=p_{i}\cdot e^{\lambda \cdot 1}+(1-p_{i})\cdot e^{\lambda \cdot 0}\\&=1+p_{i}(e^{\lambda }-1)\\&\leq e^{p_{i}(e^{\lambda }-1)},\end{aligned}}} where in the last step we apply the Taylor's expansion so that ${\displaystyle e^{y}\geq 1+y}$ where ${\displaystyle y=p_{i}(e^{\lambda }-1)\geq 0}$. (By doing this, we can transform the product to the sum of ${\displaystyle p_{i}}$, which is ${\displaystyle \mu }$.) Therefore, {\displaystyle {\begin{aligned}\mathbf {E} \left[e^{\lambda X}\right]&=\prod _{i=1}^{n}\mathbf {E} \left[e^{\lambda X_{i}}\right]\\&\leq \prod _{i=1}^{n}e^{p_{i}(e^{\lambda }-1)}\\&=\exp \left(\sum _{i=1}^{n}p_{i}(e^{\lambda }-1)\right)\\&=e^{(e^{\lambda }-1)\mu }.\end{aligned}}} Thus, we have shown that for any ${\displaystyle \lambda >0}$, {\displaystyle {\begin{aligned}\Pr[X\geq (1+\delta )\mu ]&\leq {\frac {\mathbf {E} \left[e^{\lambda X}\right]}{e^{\lambda (1+\delta )\mu }}}\\&\leq {\frac {e^{(e^{\lambda }-1)\mu }}{e^{\lambda (1+\delta )\mu }}}\\&=\left({\frac {e^{(e^{\lambda }-1)}}{e^{\lambda (1+\delta )}}}\right)^{\mu }\end{aligned}}}. For any ${\displaystyle \delta >0}$, we can let ${\displaystyle \lambda =\ln(1+\delta )>0}$ to get ${\displaystyle \Pr[X\geq (1+\delta )\mu ]\leq \left({\frac {e^{\delta }}{(1+\delta )^{(1+\delta )}}}\right)^{\mu }.}$
${\displaystyle \square }$

The idea of the proof is actually quite clear: we apply Markov's inequality to ${\displaystyle e^{\lambda X}}$ and for the rest, we just estimate the moment generating function ${\displaystyle \mathbf {E} [e^{\lambda X}]}$. To make the bound as tight as possible, we minimized the ${\displaystyle {\frac {e^{(e^{\lambda }-1)}}{e^{\lambda (1+\delta )}}}}$ by setting ${\displaystyle \lambda =\ln(1+\delta )}$, which can be justified by taking derivatives of ${\displaystyle {\frac {e^{(e^{\lambda }-1)}}{e^{\lambda (1+\delta )}}}}$.

We then proceed to the lower tail, the probability that the random variable deviates below the mean value:

 Chernoff bound (the lower tail) Let ${\displaystyle X=\sum _{i=1}^{n}X_{i}}$, where ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ are independent Poisson trials. Let ${\displaystyle \mu =\mathbf {E} [X]}$. Then for any ${\displaystyle 0<\delta <1}$, ${\displaystyle \Pr[X\leq (1-\delta )\mu ]\leq \left({\frac {e^{-\delta }}{(1-\delta )^{(1-\delta )}}}\right)^{\mu }.}$
Proof.
 For any ${\displaystyle \lambda <0}$, by the same analysis as in the upper tail version, {\displaystyle {\begin{aligned}\Pr[X\leq (1-\delta )\mu ]&=\Pr \left[e^{\lambda X}\geq e^{\lambda (1-\delta )\mu }\right]\\&\leq {\frac {\mathbf {E} \left[e^{\lambda X}\right]}{e^{\lambda (1-\delta )\mu }}}\\&\leq \left({\frac {e^{(e^{\lambda }-1)}}{e^{\lambda (1-\delta )}}}\right)^{\mu }.\end{aligned}}} For any ${\displaystyle 0<\delta <1}$, we can let ${\displaystyle \lambda =\ln(1-\delta )<0}$ to get ${\displaystyle \Pr[X\geq (1-\delta )\mu ]\leq \left({\frac {e^{-\delta }}{(1-\delta )^{(1-\delta )}}}\right)^{\mu }.}$
${\displaystyle \square }$

Some useful special forms of the bounds can be derived directly from the above general forms of the bounds. We now know better why we say that the bounds are exponentially sharp.

 Useful forms of the Chernoff bound Let ${\displaystyle X=\sum _{i=1}^{n}X_{i}}$, where ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ are independent Poisson trials. Let ${\displaystyle \mu =\mathbf {E} [X]}$. Then 1. for ${\displaystyle 0<\delta \leq 1}$, ${\displaystyle \Pr[X\geq (1+\delta )\mu ]<\exp \left(-{\frac {\mu \delta ^{2}}{3}}\right);}$ ${\displaystyle \Pr[X\leq (1-\delta )\mu ]<\exp \left(-{\frac {\mu \delta ^{2}}{2}}\right);}$ 2. for ${\displaystyle t\geq 2e\mu }$, ${\displaystyle \Pr[X\geq t]\leq 2^{-t}.}$
Proof.
 To obtain the bounds in (1), we need to show that for ${\displaystyle 0<\delta <1}$, ${\displaystyle {\frac {e^{\delta }}{(1+\delta )^{(1+\delta )}}}\leq e^{-\delta ^{2}/3}}$ and ${\displaystyle {\frac {e^{-\delta }}{(1-\delta )^{(1-\delta )}}}\leq e^{-\delta ^{2}/2}}$. We can verify both inequalities by standard analysis techniques. To obtain the bound in (2), let ${\displaystyle t=(1+\delta )\mu }$. Then ${\displaystyle \delta =t/\mu -1\geq 2e-1}$. Hence, {\displaystyle {\begin{aligned}\Pr[X\geq (1+\delta )\mu ]&\leq \left({\frac {e^{\delta }}{(1+\delta )^{(1+\delta )}}}\right)^{\mu }\\&\leq \left({\frac {e}{1+\delta }}\right)^{(1+\delta )\mu }\\&\leq \left({\frac {e}{2e}}\right)^{t}\\&\leq 2^{-t}\end{aligned}}}
${\displaystyle \square }$

### Balls into bins, revisited

Throwing ${\displaystyle m}$ balls uniformly and independently to ${\displaystyle n}$ bins, what is the maximum load of all bins with high probability? In the last class, we gave an analysis of this problem by using a counting argument.

Now we give a more "advanced" analysis by using Chernoff bounds.

For any ${\displaystyle i\in [n]}$ and ${\displaystyle j\in [m]}$, let ${\displaystyle X_{ij}}$ be the indicator variable for the event that ball ${\displaystyle j}$ is thrown to bin ${\displaystyle i}$. Obviously

${\displaystyle \mathbf {E} [X_{ij}]=\Pr[{\mbox{ball }}j{\mbox{ is thrown to bin }}i]={\frac {1}{n}}}$

Let ${\displaystyle Y_{i}=\sum _{j\in [m]}X_{ij}}$ be the load of bin ${\displaystyle i}$.

Then the expected load of bin ${\displaystyle i}$ is

${\displaystyle (*)\qquad \mu =\mathbf {E} [Y_{i}]=\mathbf {E} \left[\sum _{j\in [m]}X_{ij}\right]=\sum _{j\in [m]}\mathbf {E} [X_{ij}]=m/n.}$

For the case ${\displaystyle m=n}$, it holds that ${\displaystyle \mu =1}$

Note that ${\displaystyle Y_{i}}$ is a sum of ${\displaystyle m}$ mutually independent indicator variable. Applying Chernoff bound, for any particular bin ${\displaystyle i\in [n]}$,

${\displaystyle \Pr[Y_{i}>(1+\delta )\mu ]\leq \left({\frac {e^{\delta }}{(1+\delta )^{1+\delta }}}\right)^{\mu }.}$

#### When ${\displaystyle m=n}$

When ${\displaystyle m=n}$, ${\displaystyle \mu =1}$. Write ${\displaystyle c=1+\delta }$. The above bound can be written as

${\displaystyle \Pr[Y_{i}>c]\leq {\frac {e^{c-1}}{c^{c}}}.}$

Let ${\displaystyle c={\frac {e\ln n}{\ln \ln n}}}$, we evaluate ${\displaystyle {\frac {e^{c-1}}{c^{c}}}}$ by taking logarithm to its reciprocal.

{\displaystyle {\begin{aligned}\ln \left({\frac {c^{c}}{e^{c-1}}}\right)&=c\ln c-c+1\\&=c(\ln c-1)+1\\&={\frac {e\ln n}{\ln \ln n}}\left(\ln \ln n-\ln \ln \ln n\right)+1\\&\geq {\frac {e\ln n}{\ln \ln n}}\cdot {\frac {2}{e}}\ln \ln n+1\\&\geq 2\ln n.\end{aligned}}}

Thus,

${\displaystyle \Pr \left[Y_{i}>{\frac {e\ln n}{\ln \ln n}}\right]\leq {\frac {1}{n^{2}}}.}$

Applying the union bound, the probability that there exists a bin with load ${\displaystyle >12\ln n}$ is

${\displaystyle n\cdot \Pr \left[Y_{1}>{\frac {e\ln n}{\ln \ln n}}\right]\leq {\frac {1}{n}}}$.

Therefore, for ${\displaystyle m=n}$, with high probability, the maximum load is ${\displaystyle O\left({\frac {e\ln n}{\ln \ln n}}\right)}$.

#### For larger ${\displaystyle m}$

When ${\displaystyle m\geq n\ln n}$, then according to ${\displaystyle (*)}$, ${\displaystyle \mu ={\frac {m}{n}}\geq \ln n}$

We can apply an easier form of the Chernoff bounds,

${\displaystyle \Pr[Y_{i}\geq 2e\mu ]\leq 2^{-2e\mu }\leq 2^{-2e\ln n}<{\frac {1}{n^{2}}}.}$

By the union bound, the probability that there exists a bin with load ${\displaystyle \geq 2e{\frac {m}{n}}}$ is,

${\displaystyle n\cdot \Pr \left[Y_{1}>2e{\frac {m}{n}}\right]=n\cdot \Pr \left[Y_{1}>2e\mu \right]\leq {\frac {1}{n}}}$.

Therefore, for ${\displaystyle m\geq n\ln n}$, with high probability, the maximum load is ${\displaystyle O\left({\frac {m}{n}}\right)}$.