Probability Inequalities

Competitive programming sometimes uses randomized algorithms. Their correctness and time and space complexity usually depend on the premise that “the probability of certain random events is very small.” For example, the complexity of quicksort depends on the event that “the chosen pivot is almost the minimum or maximum element” being rare.

This article briefly introduces several tools for analyzing randomized algorithms and gives a few simple applications.

Union Bound

Let A1,⋯ ,AmA_1, \cdots, A_m be random events. Then

P{⋃i=1mAi}≤∑i=1mP{Ai}P\left\{ \bigcup_{i=1}^m A_i \right\} \leq \sum_{i=1}^m P\{A_i\}

That is, the probability that at least one event in a collection occurs is no greater than the sum of the probabilities of the individual events.

In fact, this conclusion can be strengthened slightly:

  • The probability that at least one event in a collection occurs is at least the sum of the probabilities of the individual events, minus the sum of the probabilities that each pair occurs simultaneously.
  • The probability that at least one event in a collection occurs is at most the sum of the probabilities of the individual events, minus the sum of the probabilities that each pair occurs simultaneously, plus the sum of the probabilities that each triple occurs simultaneously.
  • …

As the number of levels increases, the alternating upper and lower bounds become tighter. This series of conclusions is formally similar to the inclusion–exclusion principle, and its proof is similar as well; we omit it here.

Markov Inequality

Let XX be a nonnegative random variable. Then for any positive real number aa,

P{X≥a}≤EXaP\{ X \geq a \} \leq \frac{EX}{a}

In fact, because Markov's inequality itself uses no distributional information about the random variable beyond its expectation, the bound obtained by applying it directly is usually quite loose.

Proof

Let II be the indicator function of the event X≥aX \geq a. Then

I≤XaI \leq \frac{X}{a}

Consequently,

P{X≥a}=EI≤E[Xa]=EXaP\{ X \geq a \} = EI \leq E \left[ \frac{X}{a} \right] = \frac{EX}{a}

Chebyshev Inequality

Let XX be a random variable. Then for any a>0a > 0,

P{∣X−EX∣≥a}≤DXa2P \{ |X - EX| \geq a \} \leq \frac{DX}{a^2}

In particular, when aa is set to kσk\sigma,

P{∣X−EX∣≥kσ}≤1k2P \{ |X - EX| \geq k\sigma \} \leq \frac{1}{k^2}

where σ\sigma is the standard deviation of XX.

Proof

By the given condition,

P{∣X−EX∣≥a}=P{(X−EX)2≥a2}P \{ |X - EX| \geq a \} = P \{ (X - EX)^2 \geq a^2 \}

Since (X−EX)2(X - EX)^2 is nonnegative, Markov's inequality gives

P{(X−EX)2≥a2}≤E(X−EX)2a2=DXa2P \{ (X - EX)^2 \geq a^2 \} \leq \frac{E(X - EX)^2}{a^2} = \frac{DX}{a^2}

Chernoff Inequality

The general Chernoff inequality can be obtained by applying Markov's inequality directly to the random variable etX\mathrm{e}^{tX}:

Let XX be a random variable. Then for any t>0t > 0,

P{X≥a}=P{etX>eta}≤EetXetaP\{ X \geq a \} = P\{ \mathrm{e}^{tX} > \mathrm{e}^{ta} \} \leq \frac{E \mathrm{e}^{tX}}{\mathrm{e}^{ta}}

Similarly, when t<0t < 0,

P{X≤a}=P{etX>eta}≤EetXetaP\{ X \leq a \} = P\{ \mathrm{e}^{tX} > \mathrm{e}^{ta} \} \leq \frac{E \mathrm{e}^{tX}}{\mathrm{e}^{ta}}

Chernoff Inequality for a Sum of Poisson Trials

The random variables involved in competitive programming are usually not so “general”; we can describe them using Poisson trials from probability theory.

A Poisson trial is a random experiment with only two possible outcomes.

The outcome of one Poisson trial can be represented by a random variable taking the value 00 or 11, denoted XX, with probability distribution

P{X=i}={pi,i=11−p1,i=0P\{ X = i \} = \begin{cases} p_i, & i = 1 \\ 1 - p_1, & i = 0 \end{cases}

For Poisson trials, we have the following result:

For nn independent Poisson trials X1,X2,⋯ ,XnX_1, X_2, \cdots, X_n, let X=∑i=1nXiX = \sum_{i=1}^{n} X_i and μ=EX\mu = EX. Then for any 0<ϵ<10 < \epsilon < 1,

P{∣X−μ∣≥ϵμ}≤2exp⁡(−13μϵ2)P\left\{ |X - \mu| \geq \epsilon \mu \right\} \leq 2 \exp\left( - \frac{1}{3} \mu \epsilon^2 \right)

Hoeffding Inequality

If X1,⋯ ,XnX_1, \cdots, X_n are mutually independent real random variables with Xi∈[ai,bi]X_i\in [a_i,b_i], and X=∑i=1nXiX=\sum\limits_{i=1}^n X_i, then

P{∣X−EX∣≥ϵ}≤2exp⁡(−2ϵ2∑i=1n(bi−ai)2)P\{ |X - EX| \geq \epsilon \} \leq 2\exp \left( \frac {-2\epsilon^2}{\sum\limits_{i=1}^n (b_i-a_i)^2} \right)

Both the Chernoff and Hoeffding inequalities bound how far a random variable can deviate from its expected value. Their proofs are rather lengthy; interested readers can consult the relevant chapters of Probability and Computing.

Empirically, if EXEX is not too close to a1+⋯+ana_1+\cdots+a_n, the bound given by this inequality is often relatively tight. If it is very close—for example, in UOJ #72 New Solution—the bound is often quite loose, and Chernoff's inequality is a better choice.

Example Applications

Example: Estimating Pi by Random Point Sampling

Consider the following algorithm for estimating the exact value of π\pi:

In the square region [−1,1]2[-1, 1]^2, generate nn points uniformly at random. Let the number of points that fall inside the unit disk x2+y2≤1x^2 + y^2 \leq 1 be mm. Then 4mn\dfrac{4m}{n} can be used as an approximation to π\pi.

Question: to guarantee that, with probability at least (1−δ)(1 - \delta), the algorithm returns a result with relative error at most ϵ\epsilon, how should nn be chosen?

Solution

Let XiX_i indicate the event that the ii-th randomly generated point lies inside the unit disk. Then the total number of points inside the disk is X=∑i=1nXiX = \sum_{i=1}^{n} X_i. We need to find a suitable nn such that

P{∣4Xn−π∣≥ϵπ}≤δP\left\{ \left| \frac{4X}{n} - \pi \right| \geq \epsilon \pi \right\} \leq \delta

The above is equivalent to

P{∣X−π4n∣≥ϵ⋅π4n}≤δP\left\{ \left| X - \frac{\pi}{4}n \right| \geq \epsilon \cdot \frac{\pi}{4}n \right\} \leq \delta

By the Chernoff inequality, it suffices to require

2exp⁡(−13ϵ2⋅π4n)≤δ2 \exp\left( - \frac{1}{3} \epsilon^2 \cdot \frac{\pi}{4}n \right) \leq \delta

From this we obtain

n≥12πϵ−2ln⁡2δn \geq \frac{12}{\pi} \epsilon^{-2} \ln \frac{2}{\delta}

Thus the desired accuracy can be achieved when n=Ω(ϵ−2ln⁡1δ)n = \Omega(\epsilon^{-2} \ln \frac{1}{\delta}).

Example: Lottery Problem

A box contains nn balls, exactly kk of which correspond to prizes. You repeatedly draw a ball independently and uniformly at random, returning it to the box after each draw. How many draws guarantee, with probability at least (1−ϵ)(1 - \epsilon), that every prize ball is drawn at least once?

Solution

If there is only one prize ball, then M=nlog⁡ϵ−1M=n\log\epsilon^{-1} draws suffice, because the probability that all MM draws miss it is

(1−1n)nlog⁡ϵ−1≤elog⁡ϵ=ϵ\Big(1-\dfrac 1n\Big)^{n\log\epsilon^{-1}}\leq e^{\log\epsilon}=\epsilon

Now suppose there are k>1k>1 prize balls. By the Union Bound, it suffices to ensure that the probability of missing each prize ball is at most ϵk\dfrac \epsilon k. Therefore the answer is nlog⁡kϵn \log \dfrac{k}{\epsilon}.

Example: Randomly Selecting Half of the Elements

Give an algorithm that selects from nn elements, uniformly at random, a subset of size n2\dfrac{n}{2}, assuming nn is even. The only random source available is a fair coin; try to minimize the number of coin flips (the minimum is not required).

Solution

First consider the following algorithm:

  • By flipping nn coins, choose one subset uniformly at random from all subsets.
  • Repeat this process until the selected subset has size exactly n2\dfrac n2.
    • Notice that subsets of size n2\dfrac n2 account for at least 1n\dfrac 1n of all subsets, so the expected number of repetitions is ≤n\leq n.

This algorithm requires an expected n2n^2 coin flips.

Another algorithm is as follows:

  • We can implement a uniformly random choice of one element using an expected 2⌈log⁡2n⌉2\lceil\log_2 n\rceil coin flips from nn elements.
    • Specifically, generate a binary number with ⌈log⁡2n⌉\lceil\log_2 n\rceil bits uniformly at random. If it is greater than or equal to nn, generate again; otherwise, select the element with that number (numbered from 0) and finish.
  • Then choose one element from all elements, then one from the remaining elements, and so on, until n2\dfrac n2 elements have been selected.

This algorithm requires an expected n⌈log⁡2n⌉n\lceil\log_2 n\rceil coin flips.

Combine the two algorithms:

  • First use the first algorithm to obtain a random subset.
  • If the subset has fewer than n2\dfrac n2 elements, repeatedly add elements using the second algorithm until its size reaches n2\dfrac n2.
  • If the subset has more than n2\dfrac n2 elements, repeatedly remove elements using the second algorithm until its size decreases to n2\dfrac n2.

Try to analyze the number of operations required by the second and third steps, that is, the number of additions or removals:

  • Let the 0–1 random variable XiX_i indicate whether ii is selected into the initial subset, and let X:=X1+⋯+XnX:=X_1+\cdots+X_n be the subset size. Then the number of operations required by the second and third steps is ∣X−E[X]∣\big|X-\mathrm{E}[X]\big|. Taking t=c⋅nt=c\cdot\sqrt n in Hoeffding's inequality, where cc is any constant, gives Pr[∣X−E[X]∣≥t]≤2e−c2\mathrm{Pr}\Big[\big|X-\mathrm{E}[X]\big|\geq t\Big]\leq 2\mathrm{e}^{-c^2}. In other words, by allowing a deviation on the order of Θ(n)\Theta(\sqrt n), we can obtain an arbitrarily small constant failure probability.

We have now shown that, with high probability, the algorithm uses at most n+Θ(nlog⁡n)n+\Theta(\sqrt n\log n) coin flips.

  • Here nn comes from the coin flips used to obtain the initial subset; Θ(nlog⁡n)\Theta(\sqrt n\log n) is the total cost of the Θ(n)\Theta(\sqrt n) additions or removals.

Expected Complexity

Let us analyze the expected number of coin flips from another perspective.

Use Hoeffding's inequality to bound the expected number of operations in the second and third steps:

E∣X−EX∣=∫0∞P{∣X−E[X]∣≥t}dt≤2∫0∞exp⁡(−t2n)dt=πnE|X - EX| = \int_0^\infty P\{ |X - E[X]| \geq t \} \mathrm{d}t \leq 2 \int_0^\infty \exp \left(-\frac {t^2}{n}\right) \mathrm{d}t=\sqrt{\pi n}

Therefore, the expected number of coin flips needed by the second and third steps is πn⋅2⌈log⁡2n⌉\sqrt{\pi n}\cdot2\lceil\log_2 n\rceil.

In total, this algorithm requires an expected n+2πn⌈log⁡2n⌉n+2\sqrt{\pi n}\lceil\log_2 n\rceil coin flips.

Exercise: Balls and Bins

Throw nn balls independently and uniformly at random into nn boxes. Prove that, with probability at least 1−1n1 - \dfrac{1}{n}, the number of balls in the fullest box is at least Ω(log⁡nlog⁡log⁡n)\Omega \left( \dfrac{\log n}{\log \log n} \right).

This content comes from OI Wiki: Probability Inequalities. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.