# Continuous uniform distribution

In probability theory and statistics, the continuous uniform distributions or rectangular distributions are a family of symmetric probability distributions. Such a distribution describes an experiment where there is an arbitrary outcome that lies between certain bounds. The bounds are defined by the parameters, $a$ and $b,$ which are the minimum and maximum values. The interval can either be closed (i.e. $[a,b]$) or open (i.e. $(a,b)$). Therefore, the distribution is often abbreviated $U(a,b),$ where $U$ stands for uniform distribution. The difference between the bounds defines the interval length; all intervals of the same length on the distribution's support are equally probable. It is the maximum entropy probability distribution for a random variable $X$ under no constraint other than that it is contained in the distribution's support.

## Definitions
### Probability density function

The probability density function of the continuous uniform distribution is
    $f(x)={\begin{cases}{\frac {1}{b-a}}&{\text{for }}a\leq x\leq b,\\[8pt]0&{\text{for }}x<a\ {\text{ or }}\ x>b.\end{cases}}$
The values of $f(x)$ at the two boundaries $a$ and $b$ are usually unimportant, because they do not alter the value of ∫$_c$$^d$f(x)dx{\textstyle \int _{c}^{d}f(x)dx} over any interval $[c,d],$ nor of ∫$_a$$^b$xf(x)dx, {\textstyle \int _{a}^{b}xf(x)dx, } nor of any higher moment. Sometimes they are chosen to be zero, and sometimes chosen to be ${\tfrac {1}{b-a}}.$ The latter is appropriate in the context of estimation by the method of maximum likelihood. In the context of Fourier analysis, one may take the value of $f(a)$ or $f(b)$ to be ${\tfrac {1}{2(b-a)}},$ because then the inverse transform of many integral transforms of this uniform function will yield back the function itself, rather than a function which is equal "almost everywhere", i.e. except on a set of points with zero measure. Also, it is consistent with the sign function, which has no such ambiguity.
Any probability density function integrates to $1,$ so the probability density function of the continuous uniform distribution is graphically portrayed as a rectangle where ⁠$b-a$⁠ is the base length and ⁠${\tfrac {1}{b-a}}$⁠ is the height. As the base length increases, the height (the density at any particular value within the distribution boundaries) decreases.
In terms of mean $\mu$ and variance $\sigma ^{2},$ the probability density function of the continuous uniform distribution is
    $f(x)={\begin{cases}{\frac {1}{2\sigma {\sqrt {3}}}}&{\text{for }}-\sigma {\sqrt {3}}\leq x-\mu \leq \sigma {\sqrt {3}},\\0&{\text{otherwise}}.\end{cases}}$

### Cumulative distribution function

The cumulative distribution function of the continuous uniform distribution is:
    $F(x)={\begin{cases}0&{\text{for }}x<a,\\[8pt]{\frac {x-a}{b-a}}&{\text{for }}a\leq x\leq b,\\[8pt]1&{\text{for }}x>b.\end{cases}}$
Its inverse is:
    $F^{-1}(p)=a+p(b-a)\quad {\text{ for }}0<p<1.$
In terms of mean $\mu$ and variance $\sigma ^{2},$ the cumulative distribution function of the continuous uniform distribution is:
    $F(x)={\begin{cases}0&{\text{for }}x-\mu <-\sigma {\sqrt {3}},\\{\frac {1}{2}}\left({\frac {x-\mu }{\sigma {\sqrt {3}}}}+1\right)&{\text{for }}-\sigma {\sqrt {3}}\leq x-\mu <\sigma {\sqrt {3}},\\1&{\text{for }}x-\mu \geq \sigma {\sqrt {3}};\end{cases}}$
its inverse is:
    $F^{-1}(p)=\sigma {\sqrt {3}}(2p-1)+\mu \quad {\text{ for }}0\leq p\leq 1.$

#### Example 1. Using the continuous uniform distribution function

For a random variable $X\sim U(0,23),$ find $P(2<X<18):$
    $P(2<X<18)=(18-2)\cdot {\frac {1}{23-0}}={\frac {16}{23}}.$
In a graphical representation of the continuous uniform distribution function $[f(x){\text{ vs }}x],$ the area under the curve within the specified bounds, displaying the probability, is a rectangle. For the specific example above, the base would be ⁠$16,$⁠ and the height would be ⁠${\tfrac {1}{23}}.$⁠

#### Example 2. Using the continuous uniform distribution function (conditional)

For a random variable $X\sim U(0,23),$ find $P(X>12\ |\ X>8):$
    $P(X>12\ |\ X>8)=(23-12)\cdot {\frac {1}{23-8}}={\frac {11}{15}}.$
The example above is a conditional probability case for the continuous uniform distribution: given that ⁠$X>8$⁠ is true, what is the probability that ⁠$X>12?$⁠ Conditional probability changes the sample space, so a new interval length ⁠$b-a'$⁠ has to be calculated, where $b=23$ and $a'=8.$ The graphical representation would still follow Example 1, where the area under the curve within the specified bounds displays the probability; the base of the rectangle would be ⁠$11,$⁠ and the height would be ⁠${\tfrac {1}{15}}.$⁠

### Generating functions
#### Moment-generating function

The moment-generating function of the continuous uniform distribution is:
    $M_{X}=\mathrm {E} (\mathrm {e} ^{tX})=\int _{a}^{b}\mathrm {e} ^{tx}{\frac {dx}{b-a}}={\frac {\mathrm {e} ^{tb}-\mathrm {e} ^{ta}}{t(b-a)}}={\frac {B^{t}-A^{t}}{t(b-a)}},$
from which we may calculate the raw moments $m_{k}:$
    $m_{1}={\frac {a+b}{2}},$
    $m_{2}={\frac {a^{2}+ab+b^{2}}{3}},$
    $m_{k}={\frac {\sum _{i=0}^{k}a^{i}b^{k-i}}{k+1}}.$
For a random variable following the continuous uniform distribution, the expected value is $m_{1}={\tfrac {a+b}{2}},$ and the variance is $m_{2}-m_{1}^{2}={\tfrac {(b-a)^{2}}{12}}.$
For the special case $a=-b,$ the probability density function of the continuous uniform distribution is:
    $f(x)={\begin{cases}{\frac {1}{2b}}&{\text{for }}-b\leq x\leq b,\\[8pt]0&{\text{otherwise}};\end{cases}}$
the moment-generating function reduces to the simple form:
    $M_{X}={\frac {\sinh bt}{bt}}.$

#### Cumulant-generating function

For ⁠$n\geq 2,$⁠ the $n$-th cumulant of the continuous uniform distribution on the interval ⁠$[-{\tfrac {1}{2}},{\tfrac {1}{2}}]$⁠ is ${\tfrac {B_{n}}{n}},$ where $B_{n}$ is the $n$-th Bernoulli number.

### Standard uniform distribution

The continuous uniform distribution with parameters $a=0$ and $b=1,$ i.e. $U(0,1),$ is called the standard uniform distribution.
One interesting property of the standard uniform distribution is that if $u_{1}$ has a standard uniform distribution, then so does $1-u_{1}.$ This property can be used for generating antithetic variates, among other things. In other words, this property is known as the inversion method where the continuous standard uniform distribution can be used to generate random numbers for any other continuous distribution. If $u_{1}$ is a uniform random number with standard uniform distribution, i.e. with $U(0,1),$ then $x=F^{-1}(u_{1})$ generates a random number $x$ from any continuous distribution with the specified cumulative distribution function $F.$

### Relationship to other functions

As long as the same conventions are followed at the transition points, the probability density function of the continuous uniform distribution may also be expressed in terms of the Heaviside step function as:
    $f(x)={\frac {\operatorname {H} (x-a)-\operatorname {H} (x-b)}{b-a}},$
or in terms of the rectangle function as:
    $f(x)={\frac {1}{b-a}}\ \operatorname {rect} \left({\frac {x-{\frac {a+b}{2}}}{b-a}}\right).$
There is no ambiguity at the transition point of the sign function. Using the half-maximum convention at the transition points, the continuous uniform distribution may be expressed in terms of the sign function as:
    $f(x)={\frac {\operatorname {sgn} {(x-a)}-\operatorname {sgn} {(x-b)}}{2(b-a)}}.$

## Properties
### Moments

The mean (first raw moment) of the continuous uniform distribution is:
    $E(X)=\int _{a}^{b}x{\frac {dx}{b-a}}={\frac {b^{2}-a^{2}}{2(b-a)}}={\frac {b+a}{2}}.$
The second raw moment of this distribution is:
    $E(X^{2})=\int _{a}^{b}x^{2}{\frac {dx}{b-a}}={\frac {b^{3}-a^{3}}{3(b-a)}}.$
In general, the $n$-th raw moment of this distribution is:
    $E(X^{n})=\int _{a}^{b}x^{n}{\frac {dx}{b-a}}={\frac {b^{n+1}-a^{n+1}}{(n+1)(b-a)}}.$
The variance (second central moment) of this distribution is:
    $V(X)=E\left({\bigX-E(X){\big )}^{2}\right)=\int _{a}^{b}\left(x-{\frac {a+b}{2}}\right)^{2}{\frac {dx}{b-a}}={\frac {(b-a)^{2}}{12}}.$

### Order statistics

Let $X_{1},...,X_{n}$ be an i.i.d. sample from $U(0,1),$ and let $X_{(k)}$ be the $k$-th order statistic from this sample.
$X_{(k)}$ has a beta distribution, with parameters $k$ and ⁠$n-k+1.$⁠
The expected value is:
    $\operatorname {E} \left(X_{(k)}\right)={k \over n+1}.$
This fact is useful when making Q–Q plots.
The variance is:
    $\operatorname {V} \left(X_{(k)}\right)={k(n-k+1) \over (n+1)^{2}(n+2)}.$

### Uniformity

The probability that a continuously uniformly distributed random variable falls within any interval of fixed length is independent of the location of the interval itself (but it is dependent on the interval size $(\ell )$), so long as the interval is contained in the distribution's support.
Indeed, if $X\sim U(a,b)$ and if $[x,x+\ell ]$ is a subinterval of $[a,b]$ with fixed $\ell >0,$ then:
    $P{\bigX\in [x,x+\ell ]{\big )}=\int _{x}^{x+\ell }{\frac {dy}{b-a}}={\frac {\ell }{b-a}},$
which is independent of $x.$ This fact motivates the distribution's name.

### Uniform distribution on more general sets

The uniform distribution can be generalized to sets more general than intervals.
Formally, let $S$ be a Borel set of positive, finite Lebesgue measure $\lambda (S),$ i.e. $0<\lambda (S)<+\infty .$ The uniform distribution on $S$ can be specified by defining the probability density function to be zero outside $S$ and constantly equal to ${\tfrac {1}{\lambda (S)}}$ on $S.$
An interesting special case is when the set S is a simplex. It is possible to obtain a uniform distribution on the standard n-vertex simplex in the following way.$^{:Thm.4.1}$take n independent random variables with the same exponential distribution; denote them by X$_1$,...,X$_n$; and let Y$_i$ := X$_i$ / (sum$_i$ X$_i$). Then, the vector Y$_1$,...,Y$_n$ is uniformly distributed on the simplex.

## Related distributions

* If X has a standard uniform distribution, then by the inverse transform sampling method, Y = − λ$^{−1}$ ln(X) has an exponential distribution with (rate) parameter λ.
* If X has a standard uniform distribution, then Y = X$^n$ has a beta distribution with parameters (1/n,1). As such,
* The Irwin–Hall distribution is the sum of n i.i.d. U(0,1) distributions.
* The Bates distribution is the average of n i.i.d. U(0,1) distributions.
* The standard uniform distribution is a special case of the beta distribution, with parameters (1,1).
* The sum of two independent uniform distributions U$_1$(a,b)+U$_2$(c,d) yields a trapezoidal distribution, symmetric about its mean, on the support [a+c,b+d]. The plateau has width equals to the absolute different of the width of U$_1$ and U$_2$. The width of the sloped parts corresponds to the width of the narrowest uniform distribution.
** If the uniform distributions have the same width w, the result is a triangular distribution, symmetric about its mean, on the support [a+c,a+c+2w].
** The sum of two independent, equally distributed, uniform distributions U$_1$(a,b)+U$_2$(a,b) yields a symmetric triangular distribution on the support [2a,2b].
* The distance between two i.i.d. uniform random variables |U$_1$(a,b)-U$_2$(a,b)| also has a triangular distribution, although not symmetric, on the support [0,b-a].

## Statistical inference
### Estimation of parameters
#### Estimation of maximum
##### Minimum-variance unbiased estimator

Given a uniform distribution on $[0,b]$ with unknown $b,$ the minimum-variance unbiased estimator (UMVUE) for the maximum is:
    ${\hat {b}}_{\text{UMVU}}={\frac {k+1}{k}}m=m+{\frac {m}{k}},$
where $m$ is the sample maximum and $k$ is the sample size, sampling without replacement (though this distinction almost surely makes no difference for a continuous distribution). This follows for the same reasons as estimation for the discrete distribution, and can be seen as a very simple case of maximum spacing estimation. This problem is commonly known as the German tank problem, due to application of maximum estimation to estimates of German tank production during World War II.

##### Method of moment estimator

The method of moments estimator is:
    ${\hat {b}}_{MM}=2{\bar {X}},$
where ${\bar {X}}$ is the sample mean.

##### Maximum likelihood estimator

The maximum likelihood estimator is:
    ${\hat {b}}_{ML}=m,$
where $m$ is the sample maximum, also denoted as $m=X_{(n)},$ the maximum order statistic of the sample.

#### Estimation of minimum

Given a uniform distribution on $[a,b]$ with unknown a, the maximum likelihood estimator for a is:
    ${\hat {a}}_{ML}=\min\{X_{1},\dots ,X_{n}\}$,
the sample minimum.

#### Estimation of midpoint

The midpoint of the distribution, ${\tfrac {a+b}{2}},$ is both the mean and the median of the uniform distribution. Although both the sample mean and the sample median are unbiased estimators of the midpoint, neither is as efficient as the sample mid-range, i.e. the arithmetic mean of the sample maximum and the sample minimum, which is the UMVU estimator of the midpoint (and also the maximum likelihood estimate).

### Confidence interval
#### For the maximum

Let $X_{1},X_{2},X_{3},...,X_{n}$ be a sample from $U_{[0,L]},$ where $L$ is the maximum value in the population. Then $X_{(n)}=\max(X_{1},X_{2},X_{3},...,X_{n})$ has the Lebesgue-Borel-density $f={\frac {d\Pr _{X_{(n)}}}{d\lambda }}:$
    $f(t)=n{\frac {1}{L}}\left({\frac {t}{L}}\right)^{n-1}\!=n{\frac {t^{n-1}}{L^{n}}}1\!\!1_{[0,L]}(t),$ where $1\!\!1_{[0,L]}$ is the indicator function of $[0,L].$
The confidence interval given before is mathematically incorrect, as
    $\Pr {\big[{\hat {\theta }},{\hat {\theta }}+\varepsilon ]\ni \theta {\big )}\geq 1-\alpha$
cannot be solved for $\varepsilon$ without knowledge of $\theta$. However, one can solve
    $\Pr {\big[{\hat {\theta }},{\hat {\theta }}(1+\varepsilon )]\ni \theta {\big )}\geq 1-\alpha$ for $\varepsilon \geq (1-\alpha )^{-1/n}-1$ for any unknown but valid $\theta ;$
one then chooses the smallest $\varepsilon$ possible satisfying the condition above. Note that the interval length depends upon the random variable ${\hat {\theta }}.$

## Occurrence and applications

The probabilities for uniform distribution function are simple to calculate due to the simplicity of the function form. Therefore, there are various applications that this distribution can be used for as shown below: hypothesis testing situations, random sampling cases, finance, etc. Furthermore, generally, experiments of physical origin follow a uniform distribution (e.g. emission of radioactive particles). However, it is important to note that in any application, there is the unchanging assumption that the probability of falling in an interval of fixed length is constant.

### Economics example for uniform distribution

In the field of economics, usually demand and replenishment may not follow the expected normal distribution. As a result, other distribution models are used to better predict probabilities and trends such as Bernoulli process. But according to Wanke (2008), in the particular case of investigating lead-time for inventory management at the beginning of the life cycle when a completely new product is being analyzed, the uniform distribution proves to be more useful. In this situation, other distribution may not be viable since there is no existing data on the new product or that the demand history is unavailable so there isn't really an appropriate or known distribution. The uniform distribution would be ideal in this situation since the random variable of lead-time (related to demand) is unknown for the new product but the results are likely to range between a plausible range of two values. The lead-time would thus represent the random variable. From the uniform distribution model, other factors related to lead-time were able to be calculated such as cycle service level and shortage per cycle. It was also noted that the uniform distribution was also used due to the simplicity of the calculations.

### Sampling from an arbitrary distribution

The uniform distribution is useful for sampling from arbitrary distributions. A general method is the inverse transform sampling method, which uses the cumulative distribution function (CDF) of the target random variable. This method is very useful in theoretical work. Since simulations using this method require inverting the CDF of the target variable, alternative methods have been devised for the cases where the CDF is not known in closed form. One such method is rejection sampling.
The normal distribution is an important example where the inverse transform method is not efficient. However, there is an exact method, the Box–Muller transformation, which uses the inverse transform to convert two independent uniform random variables into two independent normally distributed random variables.

### Quantization error

In analog-to-digital conversion, a quantization error occurs. This error is either due to rounding or truncation. When the original signal is much larger than one least significant bit (LSB), the quantization error is not significantly correlated with the signal, and has an approximately uniform distribution. The RMS error therefore follows from the variance of this distribution.

## Random variate generation

There are many applications in which it is useful to run simulation experiments. Many programming languages come with implementations to generate pseudo-random numbers which are effectively distributed according to the standard uniform distribution.
On the other hand, the uniformly distributed numbers are often used as the basis for non-uniform random variate generation.
If $u$ is a value sampled from the standard uniform distribution, then the value $a+(b-a)u$ follows the uniform distribution parameterized by $a$ and $b,$ as described above.

## History

While the historical origins in the conception of uniform distribution are inconclusive, it is speculated that the term "uniform" arose from the concept of equiprobability in dice games (note that the dice games would have discrete and not continuous uniform sample space). Equiprobability was mentioned in Gerolamo Cardano's Liber de Ludo Aleae, a manual written in 16th century and detailed on advanced probability calculus in relation to dice.