# Von Mises–Fisher distribution

In directional statistics, the von Mises–Fisher distribution (named after Richard von Mises and Ronald Fisher), is a probability distribution on the $(p-1)$-sphere in $\mathbb {R} ^{p}$. If $p=2$ the distribution reduces to the von Mises distribution on the circle.

## Definition

The probability density function of the von Mises–Fisher distribution for the random p-dimensional unit vector $\mathbf {x}$ is given by:
    $f_{p}(\mathbf {x} ;{\boldsymbol {\mu }},\kappa )=C_{p}(\kappa )\exp \left({\kappa {\boldsymbol {\mu }}^{\mathsf {T}}\mathbf {x} }\right),$
where $\kappa \geq 0,\left\Vert {\boldsymbol {\mu }}\right\Vert =1$ and
the normalization constant $C_{p}(\kappa )$ is equal to
    $C_{p}(\kappa )={\frac {\kappa ^{p/2-1}}{(2\pi )^{p/2}I_{p/2-1}(\kappa )}},$
where $I_{v}$ denotes the modified Bessel function of the first kind at order $v$. If $p=3$, the normalization constant reduces to
    $C_{3}(\kappa )={\frac {\kappa }{4\pi \sinh \kappa }}={\frac {\kappa }{2\pi (e^{\kappa }-e^{-\kappa })}}.$
The parameters ${\boldsymbol {\mu }}$ and $\kappa$ are called the mean direction and concentration parameter, respectively. The greater the value of $\kappa$, the higher the concentration of the distribution around the mean direction ${\boldsymbol {\mu }}$. The distribution is unimodal for $\kappa >0$, and is uniform on the sphere for $\kappa =0$.
The von Mises–Fisher distribution for $p=3$ is also called the Fisher distribution. It was first used to model the interaction of electric dipoles in an electric field. Other applications are found in geology, bioinformatics, and text mining.

### Note on the normalization constant

In the textbook, Directional Statistics by Mardia and Jupp, the normalization constant given for the Von Mises Fisher probability density is apparently different from the one given here: $C_{p}(\kappa )$. In that book, the normalization constant is specified as:
    $C_{p}^{*}(\kappa )={\frac {({\frac {\kappa }{2}})^{p/2-1}}{\Gamma (p/2)I_{p/2-1}(\kappa )}}$
where $\Gamma$ is the gamma function. This is resolved by noting that Mardia and Jupp give the density "with respect to the uniform distribution", while the density here is specified in the usual way, with respect to Lebesgue measure. The density (w.r.t. Lebesgue measure) of the uniform distribution is the reciprocal of the surface area of the (p-1)-sphere, so that the uniform density function is given by the constant:
    $C_{p}(0)={\frac {\Gamma (p/2)}{2\pi ^{p/2}}}$
It then follows that:
    $C_{p}^{*}(\kappa )={\frac {C_{p}(\kappa )}{C_{p}(0)}}$
While the value for $C_{p}(0)$ was derived above via the surface area, the same result may be obtained by setting $\kappa =0$ in the above formula for $C_{p}(\kappa )$. This can be done by noting that the series expansion for $I_{p/2-1}(\kappa )$ divided by $\kappa ^{p/2-1}$ has but one non-zero term at $\kappa =0$. (To evaluate that term, one needs to use the definition $0^{0}=1$.)

### Support

The support of the Von Mises–Fisher distribution is the hypersphere, or more specifically, the $(p-1)$-sphere, denoted as
    $S^{p-1}=\left\{\mathbf {x} \in \mathbb {R} ^{p}:\left\|\mathbf {x} \right\|=1\right\}$
This is a $(p-1)$-dimensional manifold embedded in $p$-dimensional Euclidean space, $\mathbb {R} ^{p}$.

## Relation to normal distribution

Starting from a normal distribution with isotropic covariance $\kappa ^{-1}\mathbf {I}$ and mean ${\boldsymbol {\mu }}$ of length $r>0$, whose density function is:
    $G_{p}(\mathbf {x} ;{\boldsymbol {\mu }},\kappa )=\left({\sqrt {\frac {\kappa }{2\pi }}}\right)^{p}\exp \left(-\kappa {\frac {(\mathbf {x} -{\boldsymbol {\mu }})^{\mathsf {T}}(\mathbf {x} -{\boldsymbol {\mu }})}{2}}\right),$
the Von Mises–Fisher distribution is obtained by conditioning on $\left\|\mathbf {x} \right\|=1$. By expanding
    $(\mathbf {x} -{\boldsymbol {\mu }})^{\mathsf {T}}(\mathbf {x} -{\boldsymbol {\mu }})=\mathbf {x} ^{\mathsf {T}}\mathbf {x} +{\boldsymbol {\mu }}^{\mathsf {T}}{\boldsymbol {\mu }}-2{\boldsymbol {\mu }}^{\mathsf {T}}\mathbf {x} ,$
and using the fact that the first two right-hand-side terms are fixed, the Von Mises-Fisher density, $f_{p}(\mathbf {x} ;r^{-1}{\boldsymbol {\mu }},r\kappa )$ is recovered by recomputing the normalization constant by integrating $\mathbf {x}$ over the unit sphere. If $r=0$, we get the uniform distribution, with density $f_{p}(\mathbf {x} ;{\boldsymbol {0}},0)$.
More succinctly, the restriction of any isotropic multivariate normal density to the unit hypersphere, gives a Von Mises-Fisher density, up to normalization.
This construction can be generalized by starting with a normal distribution with a general covariance matrix, in which case conditioning on $\left\|\mathbf {x} \right\|=1$ gives the Fisher-Bingham distribution.

## Estimation of parameters
### Mean direction

A series of N independent unit vectors $x_{i}$ are drawn from a von Mises–Fisher distribution. The maximum likelihood estimates of the mean direction $\mu$ is simply the normalized arithmetic mean, a sufficient statistic:
    $\mu ={\bar {x}}/{\bar {R}},{\text{where }}{\bar {x}}={\frac {1}{N}}\sum _{i}^{N}x_{i},{\text{and }}{\bar {R}}=\|{\bar {x}}\|,$

### Concentration parameter

Use the modified Bessel function of the first kind to define
    $A_{p}(\kappa )={\frac {I_{p/2}(\kappa )}{I_{p/2-1}(\kappa )}}.$
Then:
    $\kappa =A_{p}^{-1}({\bar {R}}).$
Thus $\kappa$ is the solution to
    $A_{p}(\kappa )={\frac {\left\|\sum _{i}^{N}x_{i}\right\|}{N}}={\bar {R}}.$
A simple approximation to $\kappa$ is (Sra, 2011)
    ${\hat {\kappa }}={\frac {{\bar {R}}(p-{\bar {R}}^{2})}{1-{\bar {R}}^{2}}},$
A more accurate inversion can be obtained by iterating the Newton method a few times
    ${\hat {\kappa }}_{1}={\hat {\kappa }}-{\frac {A_{p}({\hat {\kappa }})-{\bar {R}}}{1-A_{p}({\hat {\kappa }})^{2}-{\frac {p-1}{\hat {\kappa }}}A_{p}({\hat {\kappa }})}},$
    ${\hat {\kappa }}_{2}={\hat {\kappa }}_{1}-{\frac {A_{p}({\hat {\kappa }}_{1})-{\bar {R}}}{1-A_{p}({\hat {\kappa }}_{1})^{2}-{\frac {p-1}{{\hat {\kappa }}_{1}}}A_{p}({\hat {\kappa }}_{1})}}.$

### Standard error

For N ≥ 25, the estimated spherical standard error of the sample mean direction can be computed as:
    ${\hat {\sigma }}=\left({\frac {d}{N{\bar {R}}^{2}}}\right)^{1/2}$
where
    $d=1-{\frac {1}{N}}\sum _{i}^{N}\left(\mu ^{T}x_{i}\right)^{2}$
It is then possible to approximate a $100(1-\alpha )\%$ a spherical confidence interval (a confidence cone) about $\mu$ with semi-vertical angle:
    $q=\arcsin \left(e_{\alpha }^{1/2}{\hat {\sigma }}\right),$ where $e_{\alpha }=-\ln(\alpha ).$
For example, for a 95% confidence cone, $\alpha =0.05,e_{\alpha }=-\ln(0.05)=2.996,$ and thus $q=\arcsin(1.731{\hat {\sigma }}).$

## Expected value

The expected value of the Von Mises–Fisher distribution is not on the unit hypersphere, but instead has a length of less than one. This length is given by $A_{p}(\kappa )$ as defined above. For a Von Mises–Fisher distribution with mean direction ${\boldsymbol {\mu }}$ and concentration $\kappa >0$, the expected value is:
    $A_{p}(\kappa ){\boldsymbol {\mu }}$.
For $\kappa =0$, the expected value is at the origin. For finite $\kappa >0$, the length of the expected value is strictly between zero and one and is a monotonic rising function of $\kappa$.
The empirical mean (arithmetic average) of a collection of points on the unit hypersphere behaves in a similar manner, being close to the origin for widely spread data and close to the sphere for concentrated data. Indeed, for the Von Mises–Fisher distribution, the expected value of the maximum-likelihood estimate based on a collection of points is equal to the empirical mean of those points.

## Entropy and KL divergence

The expected value can be used to compute differential entropy and KL divergence.
The differential entropy of ${\text{VMF}}({\boldsymbol {\mu }},\kappa )$ is:
    ${\bigl \langle }-\log f_{p}(\mathbf {x} ;{\boldsymbol {\mu }},\kappa ){\bigr \rangle }_{\mathbf {x} \sim {\text{VMF}}({\boldsymbol {\mu }},\kappa )}=-\log f_{p}(A_{p}(\kappa ){\boldsymbol {\mu }};{\boldsymbol {\mu }},\kappa )=-\log C_{p}(\kappa )-\kappa A_{p}(\kappa )$
where the angle brackets denote expectation. Notice that the entropy is a function of $\kappa$ only.
The KL divergence between ${\text{VMF}}({\boldsymbol {\mu _{0}}},\kappa _{0})$ and ${\text{VMF}}({\boldsymbol {\mu _{1}}},\kappa _{1})$ is:
    ${\Bigl \langle }\log {\frac {f_{p}(\mathbf {x} ;{\boldsymbol {\mu _{0}}},\kappa _{0})}{f_{p}(\mathbf {x} ;{\boldsymbol {\mu _{1}}},\kappa _{1})}}{\Bigr \rangle }_{\mathbf {x} \sim {\text{VMF}}({\boldsymbol {\mu _{0}}},\kappa _{0})}=\log {\frac {f_{p}(A_{p}(\kappa _{0}){\boldsymbol {\mu _{0}}};{\boldsymbol {\mu _{0}}},\kappa _{0})}{f_{p}(A_{p}(\kappa _{0}){\boldsymbol {\mu _{0}}};{\boldsymbol {\mu _{1}}},\kappa _{1})}}$

## Transformation

Von Mises-Fisher (VMF) distributions are closed under orthogonal linear transforms. Let $\mathbf {U}$ be a $p$-by-$p$ orthogonal matrix. Let $\mathbf {x} \sim {\text{VMF}}({\boldsymbol {\mu }},\kappa )$ and apply the invertible linear transform: $\mathbf {y} =\mathbf {Ux}$. The inverse transform is $\mathbf {x} =\mathbf {U'y}$, because the inverse of an orthogonal matrix is its transpose: $\mathbf {U} ^{-1}=\mathbf {U} '$. The Jacobian of the transform is $\mathbf {U}$, for which the absolute value of its determinant is 1, also because of the orthogonality. Using these facts and the form of the VMF density, it follows that:
    $\mathbf {y} \sim {\text{VMF}}(\mathbf {U} {\boldsymbol {\mu }},\kappa ).$
One may verify that since ${\boldsymbol {\mu }}$ and $\mathbf {x}$ are unit vectors, then by the orthogonality, so are $\mathbf {U} {\boldsymbol {\mu }}$ and $\mathbf {y}$.

## Pseudo-random number generation
### General case

An algorithm for drawing pseudo-random samples from the Von Mises Fisher (VMF) distribution was given by Ulrich and later corrected by Wood. An implementation in R is given by Hornik and Grün; and a fast Python implementation is described by Pinzón and Jung.
To simulate from a VMF distribution on the $(p-1)$-dimensional unitsphere, $S^{p-1}$, with mean direction ${\boldsymbol {\mu }}\in S^{p-1}$, these algorithms use the following radial-tangential decomposition for a point $\mathbf {x} \in S^{p-1}\subset \mathbb {R} ^{p}$ :
    $\mathbf {x} =t{\boldsymbol {\mu }}+{\sqrt {1-t^{2}}}\mathbf {v}$
where $\mathbf {v} \in \mathbb {R} ^{p}$ lives in the tangential $(p-2)$-dimensional unit-subsphere that is centered at and perpendicular to ${\boldsymbol {\mu }}$; while $t\in [-1,1]$. To draw a sample $\mathbf {x}$ from a VMF with parameters ${\boldsymbol {\mu }}$ and $\kappa$, $\mathbf {v}$ must be drawn from the uniform distribution on the tangential subsphere; and the radial component, $t$, must be drawn independently from the distribution with density:
    $f_{\text{radial}}(t;\kappa ,p)={\frac {(\kappa /2)^{\nu }}{\Gamma ({\frac {1}{2}})\Gamma (\nu +{\frac {1}{2}})I_{\nu }(\kappa )}}e^{t\kappa }(1-t^{2})^{\nu -{\frac {1}{2}}}$
where $\nu ={\frac {p}{2}}-1$. The normalization constant for this density may be verified by using:
    $I_{\nu }(\kappa )={\frac {(\kappa /2)^{\nu }}{\Gamma ({\frac {1}{2}})\Gamma (\nu +{\frac {1}{2}})}}\int _{-1}^{1}e^{t\kappa }(1-t^{2})^{\nu -{\frac {1}{2}}}\,dt$
as given in Appendix 1 (A.3) in Directional Statistics. Drawing the $t$ samples from this density by using a rejection sampling algorithm is explained in the above references. To draw the uniform $\mathbf {v}$ samples perpendicular to ${\boldsymbol {\mu }}$, see the algorithm in, or otherwise a Householder transform can be used as explained in Algorithm 1 in.

### 3-D sphere

To generate a Von Mises–Fisher distributed pseudo-random spherical 3-D unit vector X$_s${\textstyle \mathbf {X} _{s}} on the S$^2${\textstyle S^{2}}sphere for a given μ{\textstyle \mu } and κ{\textstyle \kappa }, define
$\mathbf {X} _{s}=[r,\theta ,\phi ]$
where θ{\textstyle \theta } is the polar angle, ϕ{\textstyle \phi } the azimuthal angle, and r=1{\textstyle r=1} the distance to the center of the sphere
for μ=[0, (.), 1]{\textstyle \mathbf {\mu } =[0, (.), 1]} the pseudo-random triplet is then given by
$\mathbf {X} _{s}=[1,\arccos W,V]$
where V{\textstyle V} is sampled from the continuous uniform distribution U(a, b){\textstyle U(a, b)} with lower bound a{\textstyle a} and upper bound b{\textstyle b}
$V\sim U(0,2\pi )$
and
$W=\cos \theta =1+{\frac {1}{\kappa }}(\ln \xi +\ln(1-{\frac {\xi -1}{\xi }}e^{-2\kappa }))$
where ξ{\textstyle \xi } is sampled from the standard continuous uniform distribution U(0, 1){\textstyle U(0, 1)}
$\xi \sim U(0,1)$
here, W{\textstyle W}should be set to W=1{\textstyle W=1} when ξ=0{\textstyle \mathbf {\xi } =0} and X$_s${\textstyle \mathbf {X} _{s}} rotated to match any other desired μ{\textstyle \mu }.

## Distribution of polar angle

For $p=3$, the angle θ between $\mathbf {x}$ and ${\boldsymbol {\mu }}$ satisfies $\cos \theta ={\boldsymbol {\mu }}^{\mathsf {T}}\mathbf {x}$. It has the distribution
    $p(\theta )=\int d^{2}xf(x;{\boldsymbol {\mu }},\kappa )\,\delta \left(\theta -{\text{arc cos}}({\boldsymbol {\mu }}^{\mathsf {T}}\mathbf {x} )\right)$,
which can be easily evaluated as
    $p(\theta )=2\pi C_{3}(\kappa )\,\sin \theta \,e^{\kappa \cos \theta }$.
For the general case, $p\geq 2$, the distribution for the cosine of this angle:
    $\cos \theta =t={\boldsymbol {\mu }}^{\mathsf {T}}\mathbf {x}$
is given by $f_{\text{radial}}(t;\kappa ,p)$, as explained above.

## The uniform hypersphere distribution

When $\kappa =0$, the Von Mises–Fisher distribution, ${\text{VMF}}({\boldsymbol {\mu }},\kappa )$ on $S^{p-1}$ simplifies to the uniform distribution on $S^{p-1}\subset \mathbb {R} ^{p}$. The density is constant with value $C_{p}(0)$. Pseudo-random samples can be generated by generating samples in $\mathbb {R} ^{p}$ from the standard multivariate normal distribution, followed by normalization to unit norm.

### Component marginal of uniform distribution

For $1\leq i\leq p$, let $x_{i}$ be any component of $\mathbf {x} \in S^{p-1}$. The marginal distribution for $x_{i}$ has the density:
    $f_{i}(x_{i};p)=f_{\text{radial}}(x_{i};\kappa =0,p)={\frac {(1-x_{i}^{2})^{{\frac {p-1}{2}}-1}}{B{\bigl{\frac {1}{2}},{\frac {p-1}{2}}{\bigr )}}}$
where $B(\alpha ,\beta )$ is the beta function. This distribution may be better understood by highlighting its relation to the beta distribution:
    ${\begin{aligned}x_{i}^{2}&\sim {\text{Beta}}{\bigl{\frac {1}{2}},{\frac {p-1}{2}}{\bigr )}&&{\text{and}}&{\frac {x_{i}+1}{2}}&\sim {\text{Beta}}{\bigl{\frac {p-1}{2}},{\frac {p-1}{2}}{\bigr )}\end{aligned}}$
where the Legendre duplication formula is useful to understand the relationships between the normalization constants of the various densities above.
Note that the components of $\mathbf {x} \in S^{p-1}$ are not independent, so that the uniform density is not the product of the marginal densities; and $\mathbf {x}$ cannot be assembled by independent sampling of the components.

### Distribution of dot-products

In machine learning, especially in image classification, to-be-classified inputs (e.g. images) are often compared using cosine similarity, which is the dot product between intermediate representations in the form of unitvectors (termed embeddings). The dimensionality is typically high, with $p$ at least several hundreds. The deep neural networks that extract embeddings for classification should learn to spread the classes as far apart as possible and ideally this should give classes that are uniformly distributed on $S^{p-1}$. For a better statistical understanding of across-class cosine similarity, the distribution of dot-products between unitvectors independently sampled from the uniform distribution may be helpful.
Let $\mathbf {x} ,\mathbf {y} \in S^{p-1}$ be unitvectors in $\mathbb {R} ^{p}$, independently sampled from the uniform distribution. Define:
    ${\begin{aligned}t&=\mathbf {x} '\mathbf {y} \in [-1,1],&r&={\frac {t+1}{2}}\in [0,1],&s&={\text{logit}}(r)=\log {\frac {1+t}{1-t}}\in \mathbb {R} \end{aligned}}$
where $t$ is the dot-product and $r,s$ are transformed versions of it. Then the distribution for $t$ is the same as the marginal component distribution given above; the distribution for $r$ is symmetric beta and the distribution for $s$ is symmetric logistic-beta:
    ${\begin{aligned}r&\sim {\text{Beta}}{\bigl{\frac {p-1}{2}},{\frac {p-1}{2}}{\bigr )},&s&\sim B_{\sigma }{\bigl{\frac {p-1}{2}},{\frac {p-1}{2}}{\bigr )}\end{aligned}}$
The means and variances are:
    ${\begin{aligned}E[t]&=0,&E[r]&={\frac {1}{2}},&E[s]&=0,\end{aligned}}$
and
    ${\begin{aligned}{\text{var}}[t]&={\frac {1}{p}},&{\text{var}}[r]&={\frac {1}{4p}},&{\text{var}}[s]&=2\psi '{\bigl{\frac {p-1}{2}}{\bigr )}\approx {\frac {4}{p-1}}\end{aligned}}$
where $\psi '=\psi ^{(1)}$ is the first polygamma function. The variances decrease, the distributions of all three variables become more Gaussian, and the final approximation gets better as the dimensionality, $p$, is increased.

## Generalizations
### Matrix Von Mises-Fisher

The matrix von Mises-Fisher distribution (also known as matrix Langevin distribution) has the density
    $f_{n,p}(\mathbf {X} ;\mathbf {F} )\propto \exp(\operatorname {tr} (\mathbf {F} ^{\mathsf {T}}\mathbf {X} ))$
supported on the Stiefel manifold of $n\times p$ orthonormal p-frames $\mathbf {X}$, where $\mathbf {F}$ is an arbitrary $n\times p$ real matrix.

### Saw distributions

Ulrich, in designing an algorithm for sampling from the VMF distribution, makes use of a family of distributions named after and explored by John G. Saw. A Saw distribution is a distribution on the $(p-1)$-sphere, $S^{p-1}$, with modal vector ${\boldsymbol {\mu }}\in S^{p-1}$ and concentration $\kappa \geq 0$, and of which the density function has the form:
    $f_{\text{Saw}}(\mathbf {x} ;{\boldsymbol {\mu }},\kappa )={\frac {g(\kappa \mathbf {x} '{\boldsymbol {\mu }})}{K_{p}(\kappa )}}$
where $g$ is a non-negative, increasing function; and where $K_{P}(\kappa )$ is the normalization constant. The above-mentioned radial-tangential decomposition generalizes to the Saw family and the radial compoment, $t=\mathbf {x} '{\boldsymbol {\mu }}$ has the density:
    $f_{\text{Saw-radial}}(t;\kappa )={\frac {2\pi ^{p/2}}{\Gamma (p/2)}}{\frac {g(\kappa t)(1-t^{2})^{(p-3)/2}}{B{\bigl{\frac {1}{2}},{\frac {p-1}{2}}{\bigr )}K_{p}(\kappa )}}.$
where $B$ is the beta function. Also notice that the left-hand factor of the radial density is the surface area of $S^{p-1}$.
By setting $g(\kappa \mathbf {x} '{\boldsymbol {\mu }})=e^{\kappa \mathbf {x} '{\boldsymbol {\mu }}}$, one recovers the VMF distribution.

### Weighted Rademacher Distribution

The definition of the Von Mises-Fisher distribution can be extended to include also the case where $p=1$, so that the support is the 0-dimensional hypersphere, which when embedded into 1-dimensional Euclidean space is the discrete set, $\{-1,1\}$. The mean direction is $\mu \in \{-1,1\}$ and the concentration is $\kappa \geq 0$. The probability mass function, for $x\in \{-1,1\}$ is:
    $f_{1}(x\mid \mu ,\kappa )={\frac {e^{\kappa \mu x}}{e^{-\kappa }+e^{\kappa }}}=\sigma (2\kappa \mu x)$
where $\sigma (z)=1/(1+e^{-z})$ is the logistic sigmoid. The expected value is $\mu \,{\text{tanh}}(\kappa )$. In the uniform case, at $\kappa =0$, this distribution degenerates to the Rademacher distribution.