# Multinomial distribution

In probability theory, the multinomial distribution is a generalization of the binomial distribution. For example, it models the probability of counts for each side of a k-sided die rolled n times. For n independent trials each of which leads to a success for exactly one of k categories, with each category having a given fixed success probability, the multinomial distribution gives the probability of any particular combination of numbers of successes for the various categories.
When k is 2 and n is 1, the multinomial distribution is the Bernoulli distribution. When k is 2 and n is bigger than 1, it is the binomial distribution. When k is bigger than 2 and n is 1, it is the categorical distribution. The term "multinoulli" is sometimes used for the categorical distribution to emphasize this four-way relationship (so n determines the suffix, and k the prefix).
The Bernoulli distribution models the outcome of a single Bernoulli trial. In other words, it models whether flipping a (possibly biased) coin one time will result in either a success (obtaining a head) or failure (obtaining a tail). The binomial distribution generalizes this to the number of heads from performing n independent flips (Bernoulli trials) of the same coin. The multinomial distribution models the outcome of n experiments, where the outcome of each trial has a categorical distribution, such as rolling a k-sided die n times.
Let k be a fixed finite number. Mathematically, we have k possible mutually exclusive outcomes, with corresponding probabilities p$_1$, ..., p$_k$, and n independent trials. Since the k outcomes are mutually exclusive and one must occur we have p$_i$ ≥ 0 for i = 1, ..., k and $\sum _{i=1}^{k}p_{i}=1$. Then if the random variables X$_i$ indicate the number of times outcome number i is observed over the n trials, the vector X = (X$_1$, ..., X$_k$) follows a multinomial distribution with parameters n and p, where p = (p$_1$, ..., p$_k$). While the trials are independent, their outcomes X$_i$ are dependent because they must sum to n.

## Definitions
### Probability mass function

Suppose one does an experiment of extracting n balls of k different colors from a bag, replacing the extracted balls after each draw. Balls of the same color are equivalent. Denote the variable which is the number of extracted balls of color i (i = 1, ..., k) as X$_i$, and denote as p$_i$ the probability that a given extraction will be in color i. The probability mass function of this multinomial distribution is:
    ${\begin{aligned}f(x_{1},\ldots ,x_{k};n,p_{1},\ldots ,p_{k})&=\Pr(X_{1}=x_{1}{\text{ and }}\dots {\text{ and }}X_{k}=x_{k})\\&={\begin{cases}{\displaystyle {n! \over x_{1}!\cdots x_{k}!}p_{1}^{x_{1}}\times \cdots \times p_{k}^{x_{k}}},\quad &{\text{when }}\sum _{i=1}^{k}x_{i}=n\\\\0&{\text{otherwise,}}\end{cases}}\end{aligned}}$
for non-negative integers x$_1$, ..., x$_k$.
The probability mass function can be expressed using the gamma function as:
    $f(x_{1},\dots ,x_{k};p_{1},\ldots ,p_{k})={\frac {\Gamma (\sum _{i}x_{i}+1)}{\prod _{i}\Gamma (x_{i}+1)}}\prod _{i=1}^{k}p_{i}^{x_{i}}.$
This form shows its resemblance to the Dirichlet distribution, which is its conjugate prior.

### Example

Suppose that in a three-way election for a large country, candidate A received 20% of the votes, candidate B received 30% of the votes, and candidate C received 50% of the votes. If six voters are selected randomly, what is the probability that there will be exactly one supporter for candidate A, two supporters for candidate B and three supporters for candidate C in the sample?
Note: Since we’re assuming that the voting population is large, it is reasonable and permissible to think of the probabilities as unchanging once a voter is selected for the sample. Technically speaking this is sampling without replacement, so the correct distribution is the multivariate hypergeometric distribution, but the distributions converge as the population grows large in comparison to a fixed sample size.
    $\Pr(A=1,B=2,C=3)={\frac {6!}{1!2!3!}}(0.2^{1})(0.3^{2})(0.5^{3})=0.135$

## Properties
### Normalization

The multinomial distribution is normalized according to:
    $\sum _{\sum _{j=1}^{k}x_{j}=n}f(x_{1},...,x_{k};n,p_{1},...,p_{k})=1$
where the sum is over all permutations of $x_{j}$such that $\sum _{j=1}^{k}x_{j}=n$.

### Expected value and variance

The expected number of times the outcome i was observed over n trials is
    $\operatorname {E} (X_{i})=np_{i}.\,$
The covariance matrix is as follows. Each diagonal entry is the variance of a binomially distributed random variable, and is therefore
    $\operatorname {Var} (X_{i})=np_{i}(1-p_{i}).\,$
The off-diagonal entries are the covariances:
    $\operatorname {Cov} (X_{i},X_{j})=-np_{i}p_{j}\,$
for i, j distinct.
All covariances are negative because for fixed n, an increase in one component of a multinomial vector requires a decrease in another component.
When these expressions are combined into a matrix with i, j element $\operatorname {cov} (X_{i},X_{j}),$ the result is a k × k positive-semidefinite covariance matrix of rank k − 1. In the special case where k = n and where the p$_i$ are all equal, the covariance matrix is the centering matrix.
The entries of the corresponding correlation matrix are
    $\rho (X_{i},X_{i})=1.$
    $\rho (X_{i},X_{j})={\frac {\operatorname {Cov} (X_{i},X_{j})}{\sqrt {\operatorname {Var} (X_{i})\operatorname {Var} (X_{j})}}}={\frac {-p_{i}p_{j}}{\sqrt {p_{i}(1-p_{i})p_{j}(1-p_{j})}}}=-{\sqrt {\frac {p_{i}p_{j}}{(1-p_{i})(1-p_{j})}}}.$
Note that the number of trials n drops out of this expression.
Each of the k components separately has a binomial distribution with parameters n and p$_i$, for the appropriate value of the subscript i.
The support of the multinomial distribution is the set
    $\{(n_{1},\dots ,n_{k})\in \mathbb {N} ^{k}\mid n_{1}+\cdots +n_{k}=n\}.\,$
Its number of elements is
    ${n+k-1 \choose k-1}.$

### Matrix notation

In matrix notation,
    $\operatorname {E} (\mathbf {X} )=n\mathbf {p} ,\,$
and
    $\operatorname {Var} (\mathbf {X} )=n\lbrace \operatorname {diag} (\mathbf {p} )-\mathbf {p} \mathbf {p} ^{\rm {T}}\rbrace ,\,$
with p$^T$ = the row vector transpose of the column vector p.

### Visualization
#### As slices of generalized Pascal's triangle

Just like one can interpret the binomial distribution as (normalized) one-dimensional (1D) slices of Pascal's triangle, so too can one interpret the multinomial distribution as 2D (triangular) slices of Pascal's pyramid, or 3D/4D/+ (pyramid-shaped) slices of higher-dimensional analogs of Pascal's triangle. This reveals an interpretation of the range of the distribution: discretized equilateral "pyramids" in arbitrary dimension—i.e. a simplex with a grid.

#### As polynomial coefficients

Similarly, just like one can interpret the binomial distribution as the polynomial coefficients of $(p+q)^{n}$ when expanded, one can interpret the multinomial distribution as the coefficients of $(p_{1}+p_{2}+p_{3}+\cdots +p_{k})^{n}$ when expanded, noting that just the coefficients must sum up to 1.

### Large deviation theory
#### Asymptotics

By Stirling's formula, at the limit of $n,x_{1},...,x_{k}\to \infty$, we have$\ln {\binom {n}{x_{1},\cdots ,x_{k}}}+\sum _{i=1}^{k}x_{i}\ln p_{i}=-nD_{KL}({\hat {p}}\|p)-{\frac {k-1}{2}}\ln(2\pi n)-{\frac {1}{2}}\sum _{i=1}^{k}\ln({\hat {p}}_{i})+o(1)$where relative frequencies ${\hat {p}}_{i}=x_{i}/n$ in the data can be interpreted as probabilities from the empirical distribution ${\hat {p}}$, and $D_{KL}$ is the Kullback–Leibler divergence.
This formula can be interpreted as follows.
Consider $\Delta _{k}$, the space of all possible distributions over the categories $\{1,2,...,k\}$. It is a simplex. After $n$ independent samples from the categorical distribution $p$ (which is how we construct the multinomial distribution), we obtain an empirical distribution ${\hat {p}}$.
By the asymptotic formula, the probability that empirical distribution ${\hat {p}}$ deviates from the actual distribution $p$ decays exponentially, at a rate $nD_{KL}({\hat {p}}\|p)$. The more experiments and the more different ${\hat {p}}$ is from $p$, the less likely it is to see such an empirical distribution.
If $A$ is a closed subset of $\Delta _{k}$, then by dividing up $A$ into pieces, and reasoning about the growth rate of $Pr({\hat {p}}\in A_{\epsilon })$ on each piece $A_{\epsilon }$, we obtain Sanov's theorem, which states that$\lim _{n\to \infty }{\frac {1}{n}}\ln Pr({\hat {p}}\in A)=-\inf _{{\hat {p}}\in A}D_{KL}({\hat {p}}\|p)$

#### Concentration at large n

Due to the exponential decay, at large $n$, almost all the probability mass is concentrated in a small neighborhood of $p$. In this small neighborhood, we can take the first nonzero term in the Taylor expansion of $D_{KL}$, to obtain$\ln {\binom {n}{x_{1},\cdots ,x_{k}}}p_{1}^{x_{1}}\cdots p_{k}^{x_{k}}\approx -{\frac {n}{2}}\sum _{i=1}^{k}{\frac {({\hat {p}}_{i}-p_{i})^{2}}{p_{i}}}=-{\frac {1}{2}}\sum _{i=1}^{k}{\frac {(x_{i}-np_{i})^{2}}{np_{i}}}$This resembles the gaussian distribution, which suggests the following theorem:
Theorem. At the $n\to \infty$ limit, $n\sum _{i=1}^{k}{\frac {({\hat {p}}_{i}-p_{i})^{2}}{p_{i}}}=\sum _{i=1}^{k}{\frac {(x_{i}-np_{i})^{2}}{np_{i}}}$ converges in distribution to the chi-squared distribution $\chi ^{2}(k-1)$.
The space of all distributions over categories $\{1,2,\ldots ,k\}$ is a simplex: $\Delta _{k}=\left\{(y_{1},\ldots ,y_{k})\colon y_{1},\ldots ,y_{k}\geq 0,\sum _{i}y_{i}=1\right\}$, and the set of all possible empirical distributions after $n$ experiments is a subset of the simplex: $\Delta _{k,n}=\left\{(x_{1}/n,\ldots ,x_{k}/n)\colon x_{1},\ldots ,x_{k}\in \mathbb {N} ,\sum _{i}x_{i}=n\right\}$. That is, it is the intersection between $\Delta _{k}$ and the lattice $(\mathbb {Z} ^{k})/n$.
As $n$ increases, most of the probability mass is concentrated in a subset of $\Delta _{k,n}$ near $p$, and the probability distribution near $p$ becomes well-approximated by ${\binom {n}{x_{1},\cdots ,x_{k}}}p_{1}^{x_{1}}\cdots p_{k}^{x_{k}}\approx e^{-{\frac {n}{2}}\sum _{i}{\frac {({\hat {p}}_{i}-p_{i})^{2}}{p_{i}}}}$From this, we see that the subset upon which the mass is concentrated has radius on the order of $1/{\sqrt {n}}$, but the points in the subset are separated by distance on the order of $1/n$, so at large $n$, the points merge into a continuum. To convert this from a discrete probability distribution to a continuous probability density, we need to multiply by the volume occupied by each point of $\Delta _{k,n}$ in $\Delta _{k}$. However, by symmetry, every point occupies exactly the same volume (except a negligible set on the boundary), so we obtain a probability density $\rho ({\hat {p}})=Ce^{-{\frac {n}{2}}\sum _{i}{\frac {({\hat {p}}_{i}-p_{i})^{2}}{p_{i}}}}$, where $C$ is a constant.
Finally, since the simplex $\Delta _{k}$ is not all of $\mathbb {R} ^{k}$, but only within a $(k-1)$-dimensional plane, we obtain the desired result.

#### Conditional concentration at large n

The above concentration phenomenon can be easily generalized to the case where we condition upon linear constraints. This is the theoretical justification for Pearson's chi-squared test.
Theorem. Given frequencies $x_{i}\in \mathbb {N}$ observed in a dataset with $n$ points, we impose $\ell +1$ independent linear constraints ${\begin{cases}\sum _{i}{\hat {p}}_{i}=1,\\\sum _{i}a_{1i}{\hat {p}}_{i}=b_{1},\\\sum _{i}a_{2i}{\hat {p}}_{i}=b_{2},\\\cdots ,\\\sum _{i}a_{\ell i}{\hat {p}}_{i}=b_{\ell }\end{cases}}$(notice that the first constraint is simply the requirement that the empirical distributions sum to one), such that empirical ${\hat {p}}_{i}=x_{i}/n$ satisfy all these constraints simultaneously. Let $q$ denote the $I$-projection of prior distribution $p$ on the sub-region of the simplex allowed by the linear constraints. At the $n\to \infty$ limit, sampled counts $n{\hat {p}}_{i}$ from the multinomial distribution conditional on the linear constraints are governed by $2nD_{KL}({\hat {p}}\vert \vert q)\approx n\sum _{i}{\frac {({\hat {p}}_{i}-q_{i})^{2}}{q_{i}}}$ which converges in distribution to the chi-squared distribution $\chi ^{2}(k-1-\ell )$.
An analogous proof applies in this Diophantine problem of coupled linear equations in count variables $n{\hat {p}}_{i}$, but this time $\Delta _{k,n}$ is the intersection of $(\mathbb {Z} ^{k})/n$ with $\Delta _{k}$ and $\ell$ hyperplanes, all linearly independent, so the probability density $\rho ({\hat {p}})$ is restricted to a $(k-\ell -1)$-dimensional plane. In particular, expanding the KL divergence $D_{KL}({\hat {p}}\vert \vert p)$ around its minimum $q$ (the $I$-projection of $p$ on $\Delta _{k,n}$) in the constrained problem ensures by the Pythagorean theorem for $I$-divergence that any constant and linear term in the counts $n{\hat {p}}_{i}$ vanishes from the conditional probability to multinationally sample those counts.
Notice that
by definition, every one of ${\hat {p}}_{1},{\hat {p}}_{2},...,{\hat {p}}_{k}$ must be a rational number, whereas $p_{1},p_{2},...,p_{k}$ may be chosen from any real number in $[0,1]$ and need not satisfy the Diophantine system of equations. Only asymptotically as $n\rightarrow \infty$, the ${\hat {p}}_{i}$'s can be regarded as probabilities over $[0,1]$.
Away from empirically observed constraints $b_{1},\ldots ,b_{\ell }$ (such as moments or prevalences) the theorem can be generalized:
Theorem.
* Given functions $f_{1},...,f_{\ell }$, such that they are continuously differentiable in a neighborhood of $p$, and the vectors $(1,1,...,1),\nabla f_{1}(p),...,\nabla f_{\ell }(p)$ are linearly independent;
* given sequences $\epsilon _{1}(n),...,\epsilon _{\ell }(n)$, such that asymptotically ${\frac {1}{n}}\ll \epsilon _{i}(n)\ll {\frac {1}{\sqrt {n}}}$ for each $i\in \{1,...,\ell \}$;
* then for the multinomial distribution conditional on constraints $f_{1}({\hat {p}})\in [f_{1}(p)-\epsilon _{1}(n),f_{1}(p)+\epsilon _{1}(n)],...,f_{\ell }({\hat {p}})\in [f_{\ell }(p)-\epsilon _{\ell }(n),f_{\ell }(p)+\epsilon _{\ell }(n)]$, we have the quantity $n\sum _{i}{\frac {({\hat {p}}_{i}-p_{i})^{2}}{p_{i}}}=\sum _{i}{\frac {(x_{i}-np_{i})^{2}}{np_{i}}}$ converging in distribution to $\chi ^{2}(k-1-\ell )$ at the $n\to \infty$ limit.
In the case that all ${\hat {p}}_{i}$ are equal, the Theorem reduces to the concentration of entropies around the Maximum Entropy.

## Related distributions

In some fields such as natural language processing, categorical and multinomial distributions are synonymous and it is common to speak of a multinomial distribution when a categorical distribution is actually meant. This stems from the fact that it is sometimes convenient to express the outcome of a categorical distribution as a "1-of-k" vector (a vector with one element containing a 1 and all other elements containing a 0) rather than as an integer in the range $1\dots k$; in this form, a categorical distribution is equivalent to a multinomial distribution over a single trial.
* When k = 2, the multinomial distribution is the binomial distribution.
* Categorical distribution, the distribution of each trial; for k = 2, this is the Bernoulli distribution.
* The Dirichlet distribution is the conjugate prior of the multinomial in Bayesian statistics.
* Dirichlet-multinomial distribution.
* Beta-binomial distribution.
* Negative multinomial distribution
* Hardy–Weinberg principle ( a trinomial distribution with probabilities $(\theta ^{2},2\theta (1-\theta ),(1-\theta )^{2})$)

## Statistical inference
### Equivalence tests for multinomial distributions

The goal of equivalence testing is to establish the agreement between a theoretical multinomial distribution and observed counting frequencies. The theoretical distribution may be a fully specified multinomial distribution or a parametric family of multinomial distributions.
Let $q$ denote a theoretical multinomial distribution and let $p$ be a true underlying distribution. The distributions $p$ and $q$ are considered equivalent if $d(p,q)<\varepsilon$ for a distance $d$ and a tolerance parameter $\varepsilon >0$. The equivalence test problem is $H_{0}=\{d(p,q)\geq \varepsilon \}$ versus $H_{1}=\{d(p,q)<\varepsilon \}$. The true underlying distribution $p$ is unknown. Instead, the counting frequencies $p_{n}$ are observed, where $n$ is a sample size. An equivalence test uses $p_{n}$ to reject $H_{0}$. If $H_{0}$ can be rejected then the equivalence between $p$ and $q$ is shown at a given significance level. The equivalence test for Euclidean distance can be found in text book of Wellek (2010). The equivalence test for the total variation distance is developed in Ostrovski (2017). The exact equivalence test for the specific cumulative distance is proposed in Frey (2009).
The distance between the true underlying distribution $p$ and a family of the multinomial distributions ${\mathcal {M}}$ is defined by $d(p,{\mathcal {M}})=\min _{h\in {\mathcal {M}}}d(p,h)$. Then the equivalence test problem is given by $H_{0}=\{d(p,{\mathcal {M}})\geq \varepsilon \}$ and $H_{1}=\{d(p,{\mathcal {M}})<\varepsilon \}$. The distance $d(p,{\mathcal {M}})$ is usually computed using numerical optimization. The tests for this case are developed recently in Ostrovski (2018).

### Confidence intervals for the difference of two proportions

In the setting of a multinomial distribution, constructing confidence intervals for the difference between the proportions of observations from two events, $p_{i}-p_{j}$, requires the incorporation of the negative covariance between the sample estimators ${\hat {p}}_{i}={\frac {X_{i}}{n}}$ and ${\hat {p}}_{j}={\frac {X_{j}}{n}}$.
Some of the literature on the subject focused on the use-case of matched-pairs binary data, which requires careful attention when translating the formulas to the general case of $p_{i}-p_{j}$ for any multinomial distribution. Formulas in the current section will be generalized, while formulas in the next section will focus on the matched-pairs binary data use-case.
Wald's standard error (SE) of the difference of proportion can be estimated using:$^{:378}$
${\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}={\sqrt {\frac {({\hat {p}}_{i}+{\hat {p}}_{j})-({\hat {p}}_{i}-{\hat {p}}_{j})^{2}}{n}}}$
For a $100(1-\alpha )\%$ approximate confidence interval, the margin of error may incorporate the appropriate quantile from the standard normal distribution, as follows:
$({\hat {p}}_{i}-{\hat {p}}_{j})\pm z_{\alpha /2}\cdot {\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}$
As the sample size ($n$) increases, the sample proportions will approximately follow a multivariate normal distribution, thanks to the multidimensional central limit theorem (and it could also be shown using the Cramér–Wold theorem). Therefore, their difference will also be approximately normal. Also, these estimators are weakly consistent and plugging them into the SE estimator makes it also weakly consistent. Hence, thanks to Slutsky's theorem, the pivotal quantity ${\frac {({\hat {p}}_{i}-{\hat {p}}_{j})-(p_{i}-p_{j})}{\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}}$ approximately follows the standard normal distribution. And from that, the above approximate confidence interval is directly derived.
The SE can be constructed using the calculus of the variance of the difference of two random variables: ${\begin{aligned}{\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}&={\sqrt {{\frac {{\hat {p}}_{i}(1-{\hat {p}}_{i})}{n}}+{\frac {{\hat {p}}_{j}(1-{\hat {p}}_{j})}{n}}-2\left(-{\frac {{\hat {p}}_{i}{\hat {p}}_{j}}{n}}\right)}}\\&={\sqrt {{\frac {1}{n}}\left({\hat {p}}_{i}+{\hat {p}}_{j}-{\hat {p}}_{i}^{2}-{\hat {p}}_{j}^{2}+2{\hat {p}}_{i}{\hat {p}}_{j}\right)}}\\&={\sqrt {\frac {({\hat {p}}_{i}+{\hat {p}}_{j})-({\hat {p}}_{i}-{\hat {p}}_{j})^{2}}{n}}}\end{aligned}}$
A modification which includes a continuity correction adds ${\frac {1}{n}}$ to the margin of error as follows:$^{:102–3}$
$({\hat {p}}_{i}-{\hat {p}}_{j})\pm \left(z_{\alpha /2}\cdot {\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}+{\frac {1}{n}}\right)$
Another alternative is to rely on a Bayesian estimator using Jeffreys prior which leads to using a dirichlet distribution, with all parameters being equal to 0.5, as a prior. The posterior will be the calculations from above, but after adding 1/2 to each of the k elements, leading to an overall increase of the sample size by ${\frac {k}{2}}$. This was originally developed for a multinomial distribution with four events, and is known as wald+2, for analyzing matched pairs data (see the next section for more details).
This leads to the following SE:
${\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}_{wald+{\frac {k}{2}}}={\sqrt {\frac {\left({\hat {p}}_{i}+{\hat {p}}_{j}+{\frac {1}{n}}\right){\frac {n}{n+{\frac {k}{2}}}}-\left({\hat {p}}_{i}-{\hat {p}}_{j}\right)^{2}\left({\frac {n}{n+{\frac {k}{2}}}}\right)^{2}}{n+{\frac {k}{2}}}}}$
${\begin{aligned}{\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}_{wald+{\frac {k}{2}}}&={\sqrt {\frac {\left({\frac {x_{i}+1/2}{n+{\frac {k}{2}}}}+{\frac {x_{j}+1/2}{n+{\frac {k}{2}}}}\right)-\left({\frac {x_{i}+1/2}{n+{\frac {k}{2}}}}-{\frac {x_{j}+1/2}{n+{\frac {k}{2}}}}\right)^{2}}{n+{\frac {k}{2}}}}}\\&={\sqrt {\frac {\left({\frac {x_{i}}{n}}+{\frac {x_{j}}{n}}+{\frac {1}{n}}\right){\frac {n}{n+{\frac {k}{2}}}}-\left({\frac {x_{i}}{n}}-{\frac {x_{j}}{n}}\right)^{2}\left({\frac {n}{n+{\frac {k}{2}}}}\right)^{2}}{n+{\frac {k}{2}}}}}\\&={\sqrt {\frac {\left({\hat {p}}_{i}+{\hat {p}}_{j}+{\frac {1}{n}}\right){\frac {n}{n+{\frac {k}{2}}}}-\left({\hat {p}}_{i}-{\hat {p}}_{j}\right)^{2}\left({\frac {n}{n+{\frac {k}{2}}}}\right)^{2}}{n+{\frac {k}{2}}}}}\end{aligned}}$
Which can just be plugged into the original Wald formula as follows:
$(p_{i}-p_{j}){\frac {n}{n+{\frac {k}{2}}}}\pm z_{\alpha /2}\cdot {\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}_{wald+{\frac {k}{2}}}$

## Occurrence and applications
### Confidence intervals for the difference in matched-pairs binary data (using multinomial with k=4)

For the case of matched-pairs binary data, a common task is to build the confidence interval of the difference of the proportion of the matched events. For example, we might have a test for some disease, and we may want to check the results of it for some population at two points in time (1 and 2), to check if there was a change in the proportion of the positives for the disease during that time.
Such scenarios can be represented using a two-by-two contingency table with the number of elements that had each of the combination of events. We can use small f for sampling frequencies: $f_{11},f_{10},f_{01},f_{00}$, and capital F for population frequencies: $F_{11},F_{10},F_{01},F_{00}$. These four combinations could be modeled as coming from a multinomial distribution (with four potential outcomes). The sizes of the sample and population can be n and N respectively. And in such a case, there is an interest in building a confidence interval for the difference of proportions from the marginals of the following (sampled) contingency table:
| | Test 2 positive | Test 2 negative | Row total |
| Test 1 positive | $f_{11}$ | $f_{10}$ | $f_{1*}=f_{11}+f_{10}$ |
| Test 1 negative | $f_{01}$ | $f_{00}$ | $f_{0*}=f_{01}+f_{00}$ |
| Column total | $f_{*1}=f_{11}+f_{01}$ | $f_{*0}=f_{10}+f_{00}$ | $n$ |
In this case, checking the difference in marginal proportions means we are interested in using the following definitions: $p_{1*}={\frac {F_{1*}}{N}}={\frac {F_{11}+F_{10}}{N}}$, $p_{*1}={\frac {F_{*1}}{N}}={\frac {F_{11}+F_{01}}{N}}$. And the difference we want to build confidence intervals for is:
$p_{*1}-p_{1*}={\frac {F_{11}+F_{01}}{N}}-{\frac {F_{11}+F_{10}}{N}}={\frac {F_{01}}{N}}-{\frac {F_{10}}{N}}=p_{01}-p_{10}$
Hence, a confidence intervals for the marginal positive proportions ($p_{*1}-p_{1*}$) is the same as building a confidence interval for the difference of the proportions from the secondary diagonal of the two-by-two contingency table ($p_{01}-p_{10}$).
Calculating a p-value for such a difference is known as McNemar's test. Building confidence interval around it can be constructed using methods described above for Confidence intervals for the difference of two proportions.
The Wald confidence intervals from the previous section can be applied to this setting, and appears in the literature using alternative notations. Specifically, the SE often presented is based on the contingency table frequencies instead of the sample proportions. For example, the Wald confidence intervals, provided above, can be written as:$^{:102–3}$
${\widehat {\operatorname {SE} (p_{*1}-p_{1*})}}={\widehat {\operatorname {SE} (p_{01}-p_{10})}}={\frac {\sqrt {n(f_{10}+f_{01})-(f_{10}-f_{01})^{2}}}{n{\sqrt {n}}}}$
Further research in the literature has identified several shortcomings in both the Wald and the Wald with continuity correction methods, and other methods have been proposed for practical application.
One such modification includes Agresti and Min’s Wald+2 (similar to some of their other works) in which each cell frequency had an extra ${\frac {1}{2}}$ added to it. This leads to the Wald+2 confidence intervals. In a Bayesian interpretation, this is like building the estimators taking as prior a dirichlet distribution with all parameters being equal to 0.5 (which is, in fact, the Jeffreys prior). The +2 in the name wald+2 can now be taken to mean that in the context of a two-by-two contingency table, which is a multinomial distribution with four possible events, then since we add 1/2 an observation to each of them, then this translates to an overall addition of 2 observations (due to the prior).
This leads to the following modified SE for the case of matched pairs data:
${\widehat {\operatorname {SE} (p_{*1}-p_{1*})}}={\frac {\sqrt {(n+2)(f_{10}+f_{01}+1)-(f_{10}-f_{01})^{2}}}{(n+2){\sqrt {(n+2)}}}}$
Which can just be plugged into the original Wald formula as follows:
$(p_{*1}-p_{1*}){\frac {n}{n+2}}\pm z_{\alpha /2}\cdot {\widehat {\operatorname {SE} ({\hat {p}}_{i}-{\hat {p}}_{j})}}_{wald+2}$
Other modifications include Bonett and Price’s Adjusted Wald, and Newcombe’s Score.

## Computational methods
### Random variate generation

First, reorder the parameters $p_{1},\ldots ,p_{k}$ such that they are sorted in descending order (this is only to speed up computation and not strictly necessary). Now, for each trial, draw an auxiliary variable X from a uniform (0, 1) distribution. The resulting outcome is the component
    $j=\min \left\{j'\in \{1,\dots ,k\}\colon \left(\sum _{i=1}^{j'}p_{i}\right)-X\geq 0\right\}.$
{X$_j$ = 1, X$_k$ = 0 for k ≠ j } is one observation from the multinomial distribution with $p_{1},\ldots ,p_{k}$ and n = 1. A sum of independent repetitions of this experiment is an observation from a multinomial distribution with n equal to the number of such repetitions.

### Sampling using repeated conditional binomial samples

Given the parameters $p_{1},p_{2},\ldots ,p_{k}$ and a total for the sample $n$ such that $\sum _{i=1}^{k}X_{i}=n$, it is possible to sample sequentially for the number in an arbitrary state $X_{i}$, by partitioning the state space into $i$ and not-$i$, conditioned on any prior samples already taken, repeatedly.

#### Algorithm: Sequential conditional binomial sampling

Heuristically, each application of the binomial sample reduces the available number to sample from and the conditional probabilities are likewise updated to ensure logical consistency.

## Software implementations

* The MultinomialCI R package allows the computation of simultaneous confidence intervals for the probabilities of a multinomial distribution given a set of observations.