In mathematics, random variables are used in the study of probability. They were developed to assist in the analysis of games of chance, stochastic events, and the results of scientific experiments by capturing only the mathematical properties necessary to answer probabilistic questions. Further formalizations have firmly grounded the entity in the theoretical domains of mathematics by making use of measure theory.

The language and structure of random variables can be grasped at various levels of mathematical fluency. Set theory and calculus are fundamental.

There are two types of random variables — discrete and continuous. A discrete random variable takes values from a countable set of specific values, each with some probability greater than zero. A continuous random variable takes values from an uncountable set, and the probability of any one value is zero, but a set of values can have positive probability. Random variables can also be "mixed", having attributes of both discrete and continuous random variables.

A random variable has an associated probability distribution and frequently also a probability density function. Probability density functions are commonly used for continuous variables.

## Intuitive description

A random variable can be thought of as an unknown value that may change every time it is inspected. Thus, a random variable can be thought of as a function mapping the sample space of a random process to the real numbers. A few examples will highlight this.

### Examples

For a coin toss, the possible events are heads or tails. The number of heads appearing in one fair coin toss can be described using the following random variable:

${\displaystyle X={\begin{cases}1,&{\text{if heads}},\\0,&{\text{if tails}}.\end{cases}}}$

with probability mass function given by:

${\displaystyle \rho _{X}(x)={\begin{cases}{\frac {1}{2}},&{\text{if }}x=0,\\{\frac {1}{2}},&{\text{if }}x=1,\\0,&{\text{otherwise}}.\end{cases}}}$

A random variable can also be used to describe the process of rolling a fair die and the possible outcomes. The most obvious representation is to take the set {1, 2, 3, 4, 5, 6} as the sample space, defining the random variable X as the number rolled. In this case ,

${\displaystyle X={\begin{cases}1,&{\text{if a 1 is rolled}},\\2,&{\text{if a 2 is rolled}},\\3,&{\text{if a 3 is rolled}},\\4,&{\text{if a 4 is rolled}},\\5,&{\text{if a 5 is rolled}},\\6,&{\text{if a 6 is rolled}}.\end{cases}}}$

${\displaystyle \rho _{X}(x)={\begin{cases}{\frac {1}{6}},&{\text{if }}x=1,2,3,4,5,6,\\0,&{\text{otherwise}}.\end{cases}}}$

An example of a continuous random variable would be one based on a spinner that can choose a real number from the interval [0, 2π), with all values being "equally likely". In this case, X = the number spun. Any real number has probability zero of being selected. But a positive probability can be assigned to any range of values. For example, the probability of choosing a number in [0, π] is ½. Instead of speaking of a probability mass function, we say that the probability density of X is 1/2π. The probability of a subset of [0, 2π) can be calculated by multiplying the measure of the set by 1/2π. In general, the probability of a set for a given continuous random variable can be calculated by integrating the density over the given set.

An example of a random variable of mixed type would be based on an experiment where a coin is flipped and the spinner is spun only if the result of the coin toss is heads. If the result is tails, X = −1; otherwise X = the value of the spinner as in the preceding example. There is a probability of ½ that this random variable will have the value −1. Other ranges of values would have half the probability of the last example.

## Formal definition

Let ${\displaystyle (\Omega ,{\mathcal {F}},P)}$ be a probability space and ${\displaystyle ({\mathcal {}}Y,\Sigma )}$ be a measurable space, called the observation space. Then a random variable X is formally defined as a measurable function ${\displaystyle X:\Omega \rightarrow Y}$. An interpretation of this is that the preimages of the "well-behaved" subsets of Y (the elements of Σ) are events (elements of ${\displaystyle {\mathcal {F}}}$), and hence are assigned a probability by P.

## Real-valued random variables

Typically, the measurable space is the measurable space over the real numbers. In this case, let ${\displaystyle (\Omega ,{\mathcal {F}},P)}$ be a probability space. Then, the function ${\displaystyle X:\Omega \rightarrow \mathbb {R} }$ is a real-valued random variable if

${\displaystyle \{\omega :X(\omega )\leq r\}\in {\mathcal {F}}\qquad \forall r\in \mathbb {R} }$

This definition is a special case of the above because ${\displaystyle \{(-\infty ,r]:r\in \mathbb {R} \}}$ generates the Borel sigma-algebra on the real numbers, and it is enough to check measurability on a generating set. (Here we are using the fact that ${\displaystyle \{\omega :X(\omega )\leq r\}=X^{-1}((-\infty ,r])}$.)

### Distribution functions of random variables

Associating a cumulative distribution function (CDF) with a random variable is a generalization of assigning a value to a variable. If the CDF is a (right continuous) Heaviside step function then the variable takes on the value at the jump with probability 1. In general, the CDF specifies the probability that the variable takes on particular values.

If a random variable ${\displaystyle X:\Omega \to \mathbb {R} }$ defined on the probability space ${\displaystyle (\Omega ,{\mathcal {F}},P)}$ is given, we can ask questions like "How likely is it that the value of ${\displaystyle X}$ is bigger than 2?". This is the same as the probability of the event ${\displaystyle \{\omega :X(\omega )>2\}\,\!}$ which is often written as ${\displaystyle P(X>2)\,\!}$ for short, and easily obtained since ${\displaystyle P(X>2)=1-P(X\leq 2)}$

Recording all these probabilities of output ranges of a real-valued random variable X yields the probability distribution of X. The probability distribution "forgets" about the particular probability space used to define X and only records the probabilities of various values of X. Such a probability distribution can always be captured by its cumulative distribution function

${\displaystyle F_{X}(x)=\operatorname {P} (X\leq x)}$

and sometimes also using a probability density function. In measure-theoretic terms, we use the random variable X to "push-forward" the measure P on Ω to a measure dF on R. The underlying probability space Ω is a technical device used to guarantee the existence of random variables, and sometimes to construct them. In practice, one often disposes of the space Ω altogether and just puts a measure on R that assigns measure 1 to the whole real line, i.e., one works with probability distributions instead of random variables.

### Moments

The probability distribution of a random variable is often characterised by a small number of parameters, which also have a practical interpretation. For example, it is often enough to know what its "average value" is. This is captured by the mathematical concept of expected value of a random variable, denoted E[X], and also called the first moment. In general, E[f(X)] is not equal to f(E[X]). Once the "average value" is known, one could then ask how far from this average value the values of X typically are, a question that is answered by the variance and standard deviation of a random variable.

Mathematically, this is known as the (generalised) problem of moments: for a given class of random variables X, find a collection {fi} of functions such that the expectation values E[fi(X)] fully characterise the distribution of the random variable X.

## Functions of random variables

If we have a random variable X on Ω and a Borel measurable function f: RR, then Y = f(X) will also be a random variable on Ω, since the composition of measurable functions is also measurable. (Warning: this is not true if f is Lebesgue measurable.) The same procedure that allowed one to go from a probability space (Ω, P) to (R, dFX) can be used to obtain the distribution of Y. The cumulative distribution function of Y is

${\displaystyle F_{Y}(y)=\operatorname {P} (f(X)\leq y).}$

If function f is invertible, i.e. exists f^{-1}, then the previous relation can be extended to obtain

${\displaystyle F_{Y}(y)=\operatorname {P} (f(X)\leq y)=\operatorname {P} (X\leq f^{-1}(y))=F_{X}(f^{-1}(y))}$

and, again with the same hypotheses of invertibility of f, we can find the relation between the probability density functions by differentiating both sides with respect to y, in order to obtain

${\displaystyle f_{Y}(y)=f_{X}(f^{-1}(y)){\frac {df^{-1}(y)}{dy}}}$

### Example 1

Let X be a real-valued, continuous random variable and let Y = X2.

${\displaystyle F_{Y}(y)=\operatorname {P} (X^{2}\leq y).}$

If y < 0, then P(X2y) = 0, so

${\displaystyle F_{Y}(y)=0\qquad {\hbox{if}}\quad y<0.}$

If y ≥ 0, then

${\displaystyle \operatorname {P} (X^{2}\leq y)=\operatorname {P} (|X|\leq {\sqrt {y}})=\operatorname {P} (-{\sqrt {y}}\leq X\leq {\sqrt {y}}),}$

so

${\displaystyle F_{Y}(y)=F_{X}({\sqrt {y}})-F_{X}(-{\sqrt {y}})\qquad {\hbox{if}}\quad y\geq 0.}$

### Example 2

Suppose ${\displaystyle \scriptstyle X}$ is a random variable with a cumulative distribution

${\displaystyle F_{X}(x)=P(X\leq x)={\frac {1}{(1+e^{-x})^{\theta }}}}$

where ${\displaystyle \scriptstyle \theta >0}$ is a fixed parameter. Consider the random variable ${\displaystyle \scriptstyle Y=\log(1+e^{-X}).}$ Then,

${\displaystyle F_{Y}(y)=P(Y\leq y)=P(\log(1+e^{-X})\leq y)=P(X>-\log(e^{y}-1)).\,}$

The last expression can be calculated in terms of the cumulative distribution of ${\displaystyle X,}$ so

${\displaystyle F_{Y}(y)=1-F_{X}(-\log(e^{y}-1))\,}$
${\displaystyle =1-{\frac {1}{(1+e^{\log(e^{y}-1)})^{\theta }}}}$
${\displaystyle =1-{\frac {1}{(1+e^{y}-1)^{\theta }}}}$
${\displaystyle =1-e^{-y\theta }.\,}$

## Equivalence of random variables

There are several different senses in which random variables can be considered to be equivalent. Two random variables can be equal, equal almost surely, equal in mean, or equal in distribution.

In increasing order of strength, the precise definition of these notions of equivalence is given below.

### Equality in distribution

Two random variables X and Y are equal in distribution if they have the same distribution functions:

${\displaystyle \operatorname {P} (X\leq x)=\operatorname {P} (Y\leq x)\quad {\text{for all}}\quad x.}$

Two random variables having equal moment generating functions have the same distribution. This provides, for example, a useful method of checking equality of certain functions of i.i.d. random variables.

${\displaystyle d(X,Y)=\sup _{x}|\operatorname {P} (X\leq x)-\operatorname {P} (Y\leq x)|,}$

which is the basis of the Kolmogorov–Smirnov test.

### Equality in mean

Two random variables X and Y are equal in p-th mean if the pth moment of |XY| is zero, that is,

${\displaystyle \operatorname {E} (|X-Y|^{p})=0.}$

As in the previous case, there is a related distance between the random variables, namely

${\displaystyle d_{p}(X,Y)=\operatorname {E} (|X-Y|^{p}).\,}$

This is equivalent to the following:

### Almost sure equality

Two random variables X and Y are equal almost surely if, and only if, the probability that they are different is zero:

${\displaystyle \operatorname {P} (X\neq Y)=0.}$

For all practical purposes in probability theory, this notion of equivalence is as strong as actual equality. It is associated to the following distance:

${\displaystyle d_{\infty }(X,Y)=\sup _{\omega }|X(\omega )-Y(\omega )|,}$

where 'sup' in this case represents the essential supremum in the sense of measure theory.

### Equality

Finally, the two random variables X and Y are equal if they are equal as functions on their probability space, that is,

${\displaystyle X(\omega )=Y(\omega )\qquad {\hbox{for all}}\quad \omega .}$

## Convergence

Much of mathematical statistics consists in proving convergence results for certain sequences of random variables; see for instance the law of large numbers and the central limit theorem.

There are various senses in which a sequence (Xn) of random variables can converge to a random variable X. These are explained in the article on convergence of random variables.