# McDiarmid's inequality

In probability theory and theoretical computer science, McDiarmid's inequality (named after Colin McDiarmid) is a concentration inequality which bounds the deviation between the sampled value and the expected value of certain functions when they are evaluated on independent random variables. McDiarmid's inequality applies to functions that satisfy a bounded differences property, meaning that replacing a single argument to the function while leaving all other arguments unchanged cannot cause too large of a change in the value of the function.

## Statement

A function $f:{\mathcal {X}}_{1}\times {\mathcal {X}}_{2}\times \cdots \times {\mathcal {X}}_{n}\rightarrow \mathbb {R}$ satisfies the bounded differences property if substituting the value of the $i$th coordinate $x_{i}$ changes the value of $f$ by at most $c_{i}$. More formally, if there are constants $c_{1},c_{2},\dots ,c_{n}$ such that for all $i\in [n]$, and all $x_{1}\in {\mathcal {X}}_{1},\,x_{2}\in {\mathcal {X}}_{2},\,\ldots ,\,x_{n}\in {\mathcal {X}}_{n}$,
    $\sup _{x_{i}'\in {\mathcal {X}}_{i}}\left|f(x_{1},\dots ,x_{i-1},x_{i},x_{i+1},\ldots ,x_{n})-f(x_{1},\dots ,x_{i-1},x_{i}',x_{i+1},\ldots ,x_{n})\right|\leq c_{i}.$
McDiarmid's Inequality — Let $f:{\mathcal {X}}_{1}\times {\mathcal {X}}_{2}\times \cdots \times {\mathcal {X}}_{n}\rightarrow \mathbb {R}$ satisfy the bounded differences property with bounds $c_{1},c_{2},\dots ,c_{n}$.
Consider independent random variables $X_{1},X_{2},\dots ,X_{n}$ where $X_{i}\in {\mathcal {X}}_{i}$ for all $i$. Then, for any $\varepsilon >0$,
    ${\text{P}}\left(f(X_{1},X_{2},\ldots ,X_{n})-\mathbb {E} [f(X_{1},X_{2},\ldots ,X_{n})]\geq \varepsilon \right)\leq \exp \left(-{\frac {2\varepsilon ^{2}}{\sum _{i=1}^{n}c_{i}^{2}}}\right),$
    ${\text{P}}(f(X_{1},X_{2},\ldots ,X_{n})-\mathbb {E} [f(X_{1},X_{2},\ldots ,X_{n})]\leq -\varepsilon )\leq \exp \left(-{\frac {2\varepsilon ^{2}}{\sum _{i=1}^{n}c_{i}^{2}}}\right),$
and as an immediate consequence,
    ${\text{P}}(|f(X_{1},X_{2},\ldots ,X_{n})-\mathbb {E} [f(X_{1},X_{2},\ldots ,X_{n})]|\geq \varepsilon )\leq 2\exp \left(-{\frac {2\varepsilon ^{2}}{\sum _{i=1}^{n}c_{i}^{2}}}\right).$

## Extensions
### Unbalanced distributions

A stronger bound may be given when the arguments to the function are sampled from unbalanced distributions, such that resampling a single argument rarely causes a large change to the function value.
McDiarmid's Inequality (unbalanced) — Let $f:{\mathcal {X}}^{n}\rightarrow \mathbb {R}$ satisfy the bounded differences property with bounds $c_{1},c_{2},\dots ,c_{n}$.
Consider independent random variables $X_{1},X_{2},\ldots ,X_{n}\in {\mathcal {X}}$ drawn from a distribution where there is a particular value $\chi _{0}\in {\mathcal {X}}$ which occurs with probability $1-p$. Then, for any $\varepsilon >0$,
    ${\text{P}}(|f(X_{1},\ldots ,X_{n})-\mathbb {E} [f(X_{1},\ldots ,X_{n})]|\geq \varepsilon )\leq 2\exp \left({\frac {-\varepsilon ^{2}}{2p(2-p)\sum _{i=1}^{n}c_{i}^{2}+{\frac {2}{3}}\varepsilon \max _{i}c_{i}}}\right).$
This may be used to characterize, for example, the value of a function on graphs when evaluated on sparse random graphs and hypergraphs, since in a sparse random graph, it is much more likely for any particular edge to be missing than to be present.

### Differences bounded with high probability

McDiarmid's inequality may be extended to the case where the function being analyzed does not strictly satisfy the bounded differences property, but large differences remain very rare.
McDiarmid's Inequality (Differences bounded with high probability) — Let $f:{\mathcal {X}}_{1}\times {\mathcal {X}}_{2}\times \cdots \times {\mathcal {X}}_{n}\rightarrow \mathbb {R}$ be a function and ${\mathcal {Y}}\subseteq {\mathcal {X}}_{1}\times {\mathcal {X}}_{2}\times \cdots \times {\mathcal {X}}_{n}$ be a subset of its domain and let $c_{1},c_{2},\dots ,c_{n}\geq 0$ be constants such that for all pairs $(x_{1},\ldots ,x_{n})\in {\mathcal {Y}}$ and $(x'_{1},\ldots ,x'_{n})\in {\mathcal {Y}}$,
    $\left|f(x_{1},\ldots ,x_{n})-f(x'_{1},\ldots ,x'_{n})\right|\leq \sum _{i:x_{i}\neq x'_{i}}c_{i}.$
Consider independent random variables $X_{1},X_{2},\dots ,X_{n}$ where $X_{i}\in {\mathcal {X}}_{i}$ for all $i$. Let $p=1-\mathrm {P} ((X_{1},\ldots ,X_{n})\in {\mathcal {Y}})$ and let $m=\mathbb {E} [f(X_{1},\ldots ,X_{n})\mid (X_{1},\ldots ,X_{n})\in {\mathcal {Y}}]$. Then, for any $\varepsilon >0$,
    ${\text{P}}\left(f(X_{1},\ldots ,X_{n})-m\geq \varepsilon \right)\leq p+\exp \left(-{\frac {2\max \left(0,\varepsilon -p\sum _{i=1}^{n}c_{i}\right)^{2}}{\sum _{i=1}^{n}c_{i}^{2}}}\right),$
and as an immediate consequence,
    ${\text{P}}(|f(X_{1},\ldots ,X_{n})-m|\geq \varepsilon )\leq 2p+2\exp \left(-{\frac {2\max \left(0,\varepsilon -p\sum _{i=1}^{n}c_{i}\right)^{2}}{\sum _{i=1}^{n}c_{i}^{2}}}\right).$
There exist stronger refinements to this analysis in some distribution-dependent scenarios, such as those that arise in learning theory.

### Sub-Gaussian and sub-exponential norms

Let the $k$th centered conditional version of a function $f$ be
    $f_{k}(X)(x):=f(x_{1},\ldots ,x_{k-1},X_{k},x_{k+1},\ldots ,x_{n})-\mathbb {E} _{X'_{k}}f(x_{1},\ldots ,x_{k-1},X'_{k},x_{k+1},\ldots ,x_{n}),$
so that $f_{k}(X)$ is a random variable depending on random values of $x_{1},\ldots ,x_{k-1},x_{k+1},\ldots ,x_{n}$.
McDiarmid's Inequality (Sub-Gaussian norm) — Let $f:{\mathcal {X}}_{1}\times {\mathcal {X}}_{2}\times \cdots \times {\mathcal {X}}_{n}\rightarrow \mathbb {R}$ be a function. Consider independent random variables $X=(X_{1},X_{2},\dots ,X_{n})$ where $X_{i}\in {\mathcal {X}}_{i}$ for all $i$.
Let $f_{k}(X)$ refer to the $k$th centered conditional version of $f$. Let $\|\cdot \|_{\psi _{2}}$ denote the sub-Gaussian norm of a random variable.
Then, for any $\varepsilon >0$,
    ${\text{P}}\left(f(X_{1},\ldots ,X_{n})-m\geq \varepsilon \right)\leq \exp \left({\frac {-\varepsilon ^{2}}{32e\left\|\sum _{k\in [n]}\|f_{k}(X)\|_{\psi _{2}}^{2}\right\|_{\infty }}}\right).$
McDiarmid's Inequality (Sub-exponential norm) — Let $f:{\mathcal {X}}_{1}\times {\mathcal {X}}_{2}\times \cdots \times {\mathcal {X}}_{n}\rightarrow \mathbb {R}$ be a function. Consider independent random variables $X=(X_{1},X_{2},\dots ,X_{n})$ where $X_{i}\in {\mathcal {X}}_{i}$ for all $i$.
Let $f_{k}(X)$ refer to the $k$th centered conditional version of $f$. Let $\|\cdot \|_{\psi _{1}}$ denote the sub-exponential norm of a random variable.
Then, for any $\varepsilon >0$,
    ${\text{P}}\left(f(X_{1},\ldots ,X_{n})-m\geq \varepsilon \right)\leq \exp \left({\frac {-\varepsilon ^{2}}{4e^{2}\left\|\sum _{k\in [n]}\|f_{k}(X)\|_{\psi _{1}}^{2}\right\|_{\infty }+2\varepsilon e\max _{k\in [n]}\left\|\|f_{k}(X)\|_{\psi _{1}}\right\|_{\infty }}}\right).$

### Bennett and Bernstein forms

Refinements to McDiarmid's inequality in the style of Bennett's inequality and Bernstein inequalities are made possible by defining a variance term for each function argument. Let
    ${\begin{aligned}B&:=\max _{k\in [n]}\sup _{x_{1},\dots ,x_{k-1},x_{k+1},\dots ,x_{n}}\left|f(x_{1},\dots ,x_{k-1},X_{k},x_{k+1},\dots ,x_{n})-\mathbb {E} _{X_{k}}f(x_{1},\dots ,x_{k-1},X_{k},x_{k+1},\dots ,x_{n})\right|,\\V_{k}&:=\sup _{x_{1},\dots ,x_{k-1},x_{k+1},\dots ,x_{n}}\mathbb {E} _{X_{k}}\left(f(x_{1},\dots ,x_{k-1},X_{k},x_{k+1},\dots ,x_{n})-\mathbb {E} _{X_{k}}f(x_{1},\dots ,x_{k-1},X_{k},x_{k+1},\dots ,x_{n})\right)^{2},\\{\tilde {\sigma }}^{2}&:=\sum _{k=1}^{n}V_{k}.\end{aligned}}$
McDiarmid's Inequality (Bennett form) — Let $f:{\mathcal {X}}^{n}\rightarrow \mathbb {R}$ satisfy the bounded differences property with bounds $c_{1},c_{2},\dots ,c_{n}$. Consider independent random variables $X_{1},X_{2},\dots ,X_{n}$ where $X_{i}\in {\mathcal {X}}_{i}$ for all $i$. Let $B$ and ${\tilde {\sigma }}^{2}$ be defined as at the beginning of this section.
Then, for any $\varepsilon >0$,
    ${\text{P}}(f(X_{1},\ldots ,X_{n})-\mathbb {E} [f(X_{1},\ldots ,X_{n})]\geq \varepsilon )\leq \exp \left(-{\frac {\varepsilon }{2B}}\log \left(1+{\frac {B\varepsilon }{{\tilde {\sigma }}^{2}}}\right)\right).$
McDiarmid's Inequality (Bernstein form) — Let $f:{\mathcal {X}}^{n}\rightarrow \mathbb {R}$ satisfy the bounded differences property with bounds $c_{1},c_{2},\dots ,c_{n}$. Let $B$ and ${\tilde {\sigma }}^{2}$ be defined as at the beginning of this section.
Then, for any $\varepsilon >0$,
    ${\text{P}}(f(X_{1},\ldots ,X_{n})-\mathbb {E} [f(X_{1},\ldots ,X_{n})]\geq \varepsilon )\leq \exp \left(-{\frac {\varepsilon ^{2}}{2\left({\tilde {\sigma }}^{2}+{\frac {B\varepsilon }{3}}\right)}}\right).$

## Proof

The following proof of McDiarmid's inequality constructs the Doob martingale tracking the conditional expected value of the function as more and more of its arguments are sampled and conditioned on, and then applies a martingale concentration inequality (Azuma's inequality). An alternate argument avoiding the use of martingales also exists, taking advantage of the independence of the function arguments to provide a Chernoff-bound-like argument.
For better readability, we will introduce a notational shorthand: $z_{i\rightharpoondown j}$ will denote $z_{i},\dots ,z_{j}$ for any $z\in {\mathcal {X}}^{n}$ and integers $1\leq i\leq j\leq n$, so that, for example,
    $f(X_{1\rightharpoondown (i-1)},y,x_{(i+1)\rightharpoondown n}):=f(X_{1},\ldots ,X_{i-1},y,x_{i+1},\ldots ,x_{n}).$
Pick any $x_{1}',x_{2}',\ldots ,x_{n}'$. Then, for any $x_{1},x_{2},\ldots ,x_{n}$, by triangle inequality,
    ${\begin{aligned}&|f(x_{1\rightharpoondown n})-f(x'_{1\rightharpoondown n})|\\[6pt]\leq&|f(x_{1\rightharpoondown \,n})-f(x'_{1\rightharpoondown (n-1)},x_{n})|+c_{n}\\\leq&|f(x_{1\rightharpoondown n})-f(x'_{1\rightharpoondown (n-2)},x_{(n-1)\rightharpoondown n})|+c_{n-1}+c_{n}\\\leq&\ldots \\\leq&\sum _{i=1}^{n}c_{i},\end{aligned}}$
and thus $f$ is bounded.
Since $f$ is bounded, define the Doob martingale $\{Z_{i}\}$ (each $Z_{i}$ being a random variable depending on the random values of $X_{1},\ldots ,X_{i}$) as
    $Z_{i}:=\mathbb {E} [f(X_{1\rightharpoondown n})\mid X_{1\rightharpoondown i}]$
for all $i\geq 1$ and $Z_{0}:=\mathbb {E} [f(X_{1\rightharpoondown n})]$, so that $Z_{n}=f(X_{1\rightharpoondown n})$.
Now define the random variables for each $i$
    ${\begin{aligned}U_{i}&:=\sup _{x\in {\mathcal {X}}_{i}}\mathbb {E} [f(X_{1\rightharpoondown (i-1)},x,X_{(i+1)\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)},X_{i}=x]-\mathbb { f(X_{1\rightharpoondown (i-1)},X_{i\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}],\\L_{i}&:=\inf _{x\in {\mathcal {X}}_{i}}\mathbb {E} [f(X_{1\rightharpoondown (i-1)},x,X_{(i+1)\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)},X_{i}=x]-\mathbb { f(X_{1\rightharpoondown (i-1)},X_{i\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}].\\\end{aligned}}$
Since $X_{i},\ldots ,X_{n}$ are independent of each other, conditioning on $X_{i}=x$ does not affect the probabilities of the other variables, so these are equal to the expressions
    ${\begin{aligned}U_{i}&=\sup _{x\in {\mathcal {X}}_{i}}\mathbb {E} [f(X_{1\rightharpoondown (i-1)},x,X_{(i+1)\rightharpoondown n})-f(X_{1\rightharpoondown (i-1)},X_{i\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}],\\L_{i}&=\inf _{x\in {\mathcal {X}}_{i}}\mathbb {E} [f(X_{1\rightharpoondown (i-1)},x,X_{(i+1)\rightharpoondown n})-f(X_{1\rightharpoondown (i-1)},X_{i\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}].\\\end{aligned}}$
Note that $L_{i}\leq Z_{i}-Z_{i-1}\leq U_{i}$. In addition,
    ${\begin{aligned}U_{i}-L_{i}&=\sup _{u\in {\mathcal {X}}_{i},\ell \in {\mathcal {X}}_{i}}\mathbb {E} [f(X_{1\rightharpoondown (i-1)},u,X_{(i+1)\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}]-\mathbb {E} [f(X_{1\rightharpoondown (i-1)},\ell ,X_{(i+1)\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}]\\[6pt]&=\sup _{u\in {\mathcal {X}}_{i},\ell \in {\mathcal {X}}_{i}}\mathbb {E} [f(X_{1\rightharpoondown (i-1)},u,X_{(i+1)\rightharpoondown n})-f(X_{1\rightharpoondown (i-1)},l,X_{(i+1)\rightharpoondown n})\mid X_{1\rightharpoondown (i-1)}]\\&\leq \sup _{x_{u}\in {\mathcal {X}}_{i},x_{l}\in {\mathcal {X}}_{i}}\mathbb {E} [c_{i}\mid X_{1\rightharpoondown (i-1)}]\\[6pt]&\leq c_{i}\end{aligned}}$
Then, applying the general form of Azuma's inequality to $\left\{Z_{i}\right\}$, we have
    ${\text{P}}(f(X_{1},\ldots ,X_{n})-\mathbb {E} [f(X_{1},\ldots ,X_{n})]\geq \varepsilon )=\operatorname {P} (Z_{n}-Z_{0}\geq \varepsilon )\leq \exp \left(-{\frac {2\varepsilon ^{2}}{\sum _{i=1}^{n}c_{i}^{2}}}\right).$
The one-sided bound in the other direction is obtained by applying Azuma's inequality to $\left\{-Z_{i}\right\}$ and the two-sided bound follows from a union bound. $\square$