A random variable (also called random quantity, aleatory variable, or stochastic variable) is a mathematical formalization of a quantity or object which depends on random events. The term 'random variable' in its mathematical definition refers to neither randomness nor variability but instead is a mathematical function in which

  • the domain is the set of possible outcomes in a sample space (e.g. the set <math>\{H,T\}</math> (which are the possible upper sides of a flipped coin heads <math>H</math> or tails <math>T</math> as the result from tossing a coin); and
  • the range is a measurable space (e.g. corresponding to the domain above, the range might be the set <math>\{-1, 1\}</math> if say heads <math>H</math> mapped to −1 and <math>T</math> mapped to 1). Typically, the range of a random variable is a subset of the real numbers.

thumb|This graph shows how a random variable is a function from all possible outcomes to real values. It also shows how a random variable is used for defining probability mass functions.

Informally, randomness typically represents some fundamental element of chance, such as in the roll of a die; it may also represent uncertainty, such as measurement error. However, the interpretation of probability is philosophically complicated, and even in specific cases is not always straightforward. The purely mathematical analysis of random variables is independent of such interpretational difficulties, and can be based upon a rigorous axiomatic setup.

In the formal mathematical language of measure theory, a random variable is defined as a measurable function from a probability measure space (called the sample space) to a measurable space. This allows consideration of the pushforward measure, which is called the distribution of the random variable; the distribution is thus a probability measure on the set of all possible values of the random variable. It is possible for two random variables to have identical distributions but to differ in significant ways; for instance, they may be independent.

It is common to consider the special cases of discrete random variables and absolutely continuous random variables, corresponding to whether a random variable is valued in a countable subset or in an interval of real numbers. There are other important possibilities, especially in the theory of stochastic processes, wherein it is natural to consider random sequences or random functions. Sometimes a random variable is taken to be automatically valued in the real numbers, with more general random quantities instead being called random elements. A random variate is a particular outcome or realization of a random variable.

According to George Mackey, Pafnuty Chebyshev was the first person "to think systematically in terms of random variables".

Definition

A random variable <math>X</math> is a measurable function <math>X \colon \Omega \to E</math> from a sample space <math> \Omega </math> as a set of possible outcomes to a measurable space <math> E</math>. For the measurability of <math>X</math> to be meaningful, the sample space <math>\Omega</math> needs to belong to a probability triple <math>(\Omega, \mathcal{F}, \operatorname{P})</math> (see the measure-theoretic definition). A random variable is often denoted by capital Roman letters such as <math>X, Y, Z, T</math>.

The probability that <math>X</math> takes on a value in a measurable set <math>S\subseteq E</math> is written as

: <math>\operatorname{P}(X \in S) = \operatorname{P}(\{ \omega \in \Omega \mid X(\omega) \in S \})</math>.

Standard case

In many cases, <math>X</math> is real-valued, i.e. <math>E = \mathbb{R}</math>. In some contexts, the term random element (see extensions) is used to denote a random variable not of this form.

When the image (or range) of <math>X</math> is finite or countably infinite, the random variable is called a discrete random variable and its distribution is a discrete probability distribution, i.e. can be described by a probability mass function that assigns a probability to each value in the image of <math>X</math>. If the image is uncountably infinite (usually an interval) then <math>X</math> is called a continuous random variable. In the special case that it is absolutely continuous, its distribution can be described by a probability density function, which assigns probabilities to intervals; in particular, each individual point must necessarily have probability zero for an absolutely continuous random variable. Not all continuous random variables are absolutely continuous.

Any random variable can be described by its cumulative distribution function, which describes the probability that the random variable will be less than or equal to a certain value.

Extensions

The term "random variable" in statistics is traditionally limited to the real-valued case (). In this case, the structure of the real numbers makes it possible to define quantities such as the expected value and variance of a random variable, its cumulative distribution function, and the moments of its distribution.

However, the definition above is valid for any measurable space <math>E</math> of values. Thus one can consider random elements of other sets <math>E</math>, such as random Boolean values, categorical values, complex numbers, vectors, matrices, sequences, trees, sets, shapes, manifolds, and functions. One may then specifically refer to a random variable of type <math>E</math>, or an <math>E</math>-valued random variable.

This more general concept of a random element is particularly useful in disciplines such as graph theory, machine learning, natural language processing, and other fields in discrete mathematics and computer science, where one is often interested in modeling the random variation of non-numerical data structures. In some cases, it is nonetheless convenient to represent each element of <math>E</math>, using one or more real numbers. In this case, a random element may optionally be represented as a vector of real-valued random variables (all defined on the same underlying probability space <math>\Omega</math>, which allows the different random variables to covary). For example:

  • A random word may be represented as a random integer that serves as an index into the vocabulary of possible words. Alternatively, it can be represented as a random indicator vector, whose length equals the size of the vocabulary, where the only values of positive probability are <math>(1 \ 0 \ 0 \ 0 \ \cdots)</math>, <math>(0 \ 1 \ 0 \ 0 \ \cdots)</math>, <math>(0 \ 0 \ 1 \ 0 \ \cdots)</math> and the position of the 1 indicates the word.
  • A random sentence of given length <math>N</math> may be represented as a vector of <math>N</math> random words.
  • A random graph on <math>N</math> given vertices may be represented as a <math>N \times N</math> matrix of random variables, whose values specify the adjacency matrix of the random graph.
  • A random function <math>F</math> may be represented as a collection of random variables <math>F(x)</math>, giving the function's values at the various points <math>x</math> in the function's domain. The <math>F(x)</math> are ordinary real-valued random variables provided that the function is real-valued. For example, a stochastic process is a random function of time, a random vector is a random function of some index set such as <math>1,2,\ldots, n</math>, and a random field is a random function on any set (typically time, space, or a discrete set).

Distribution functions

If a random variable <math>X\colon \Omega \to \mathbb{R}</math> defined on the probability space <math>(\Omega, \mathcal{F}, \operatorname{P})</math> is given, we can ask questions like "How likely is it that the value of <math>X</math> is equal to 2?". This is the same as the probability of the event <math>\{ \omega : X(\omega) = 2 \}\,\! </math> which is often written as <math>P(X = 2)\,\!</math> or <math>p_X(2)</math> for short.

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

: <math>F_X(x) = \operatorname{P}(X \le x)</math>

and sometimes also using a probability density function, <math>f_X</math>. In measure-theoretic terms, we use the random variable <math>X</math> to "push-forward" the measure <math>P</math> on <math>\Omega</math> to a measure <math>p_X</math> on <math>\mathbb{R}</math>. The measure <math>p_X</math> is called the "(probability) distribution of <math>X</math>" or the "law of <math>X</math>".

The density <math>f_X = dp_X/d\mu</math>, the Radon–Nikodym derivative of <math>p_X</math> with respect to some reference measure <math>\mu</math> on <math>\mathbb{R}</math> (often, this reference measure is the Lebesgue measure in the case of continuous random variables, or the counting measure in the case of discrete random variables).

The underlying probability space <math>\Omega</math> is a technical device used to guarantee the existence of random variables, sometimes to construct them, and to define notions such as correlation and dependence or independence based on a joint distribution of two or more random variables on the same probability space. In practice, one often disposes of the space <math>\Omega</math> altogether and just puts a measure on <math>\mathbb{R}</math> that assigns measure 1 to the whole real line, i.e., one works with probability distributions instead of random variables. See the article on quantile functions for fuller development.

Examples

Discrete random variable

Consider an experiment where a person is chosen at random. An example of a random variable may be the person's height. Mathematically, the random variable is interpreted as a function which maps the person to their height. Associated with the random variable is a probability distribution that allows the computation of the probability that the height is in any subset of possible values, such as the probability that the height is between 180 and 190&nbsp;cm, or the probability that the height is either less than 150 or more than 200&nbsp;cm.

Another random variable may be the person's number of children; this is a discrete random variable with non-negative integer values. It allows the computation of probabilities for individual integer values – the probability mass function (PMF) – or for sets of values, including infinite sets. For example, the event of interest may be "an even number of children". For both finite and infinite event sets, their probabilities can be found by adding up the PMFs of the elements; that is, the probability of an even number of children is the infinite sum .

In examples such as these, the sample space is often suppressed, since it is mathematically hard to describe, and the possible values of the random variables are then treated as a sample space. But when two random variables are measured on the same sample space of outcomes, such as the height and number of children being computed on the same random persons, it is easier to track their relationship if it is acknowledged that both height and number of children come from the same random person, for example so that questions of whether such random variables are correlated or not can be posed.

If <math display = "inline">\{a_n\}, \{b_n\}</math> are countable sets of real numbers, <math display="inline">b_n >0</math> and , then <math display="inline"> F=\sum_n b_n \delta_{a_n}(x)</math> is a discrete distribution function. Here <math> \delta_t(x) = 0</math> for , for . Taking for instance an enumeration of all rational numbers as , one gets a discrete function that is not necessarily a step function (piecewise constant).

Coin toss

The possible outcomes for one coin toss can be described by the sample space <math>\Omega = \{\text{heads}, \text{tails}\}</math>. We can introduce a real-valued random variable <math>Y</math> that models a $1 payoff for a successful bet on heads as follows:

<math display="block">Y(\omega) = \begin{cases}

1, & \text{if } \omega = \text{heads}, \\[6pt]

0, & \text{if } \omega = \text{tails}.

\end{cases}</math>

If the coin is a fair coin, has a probability mass function <math>f_Y</math> given by:

<math display="block">f_Y(y) = \begin{cases}

\tfrac 12,& \text{if }y=1,\\[6pt]

\tfrac 12,& \text{if }y=0,

\end{cases}</math>

Dice roll

right | thumb | If the sample space is the set of possible numbers rolled on two dice, and the random variable of interest is the sum S of the numbers on the two dice, then S is a discrete random variable whose distribution is described by the [[probability mass function plotted as the height of picture columns here.]]

A random variable can also be used to describe the process of rolling dice and the possible outcomes. The most obvious representation for the two-dice case is to take the set of pairs of numbers n<sub>1</sub> and n<sub>2</sub> from {1, 2, 3, 4, 5, 6} (representing the numbers on the two dice) as the sample space. The total number rolled (the sum of the numbers in each pair) is then a random variable X given by the function that maps the pair to the sum:

<math display="block">X((n_1, n_2)) = n_1 + n_2</math>

and (if the dice are fair) has a probability mass function f<sub>X</sub> given by:

<math display="block">f_X(S) = \frac{\min(S-1, 13-S)}{36}, \text{ for } S \in \{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\}</math>

Continuous random variable

Formally, a continuous random variable is a random variable whose cumulative distribution function is continuous everywhere. There are no "gaps", which would correspond to numbers which have a finite probability of occurring. Instead, continuous random variables almost never take an exact prescribed value c (formally, <math display="inline">\forall c \in \mathbb{R}:\; \Pr(X = c) = 0</math>) but there is a positive probability that its value will lie in particular intervals which can be arbitrarily small. Continuous random variables usually admit probability density functions (PDF), which characterize their CDF and probability measures; such distributions are also called absolutely continuous; but some continuous distributions are singular, or mixes of an absolutely continuous part and a singular part.

An example of a continuous random variable would be one based on a spinner that can choose a horizontal direction. Then the values taken by the random variable are directions. We could represent these directions by North, West, East, South, Southeast, etc. However, it is commonly more convenient to map the sample space to a random variable which takes values which are real numbers. This can be done, for example, by mapping a direction to a bearing in degrees clockwise from North. The random variable then takes values which are real numbers from the interval [0, 360), with all parts of the range being "equally likely". In this case, X = the angle 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, 180] is . Instead of speaking of a probability mass function, we say that the probability density of X is 1/360. The probability of a subset of [0,&nbsp;360) can be calculated by multiplying the measure of the set by 1/360. In general, the probability of a set for a given continuous random variable can be calculated by integrating the density over the given set.

More formally, given any interval <math display="inline">I = [a, b] = \{x \in \mathbb{R} : a \le x \le b \}</math>, a random variable <math>X_I \sim \operatorname{U}(I) = \operatorname{U}[a, b]</math> is called a "continuous uniform random variable" (CURV) if the probability that it takes a value in a subinterval depends only on the length of the subinterval. This implies that the probability of <math>X_I</math> falling in any subinterval <math>[c, d] \sube [a, b]</math> is proportional to the length of the subinterval, that is, if , one has

<math display="block">

\Pr\left( X_I \in [c,d]\right) = \frac{d - c}{b - a}

</math>

where the last equality results from the unitarity axiom of probability. The probability density function of a CURV <math>X \sim \operatorname {U}[a, b]</math> is given by the indicator function of its interval of support normalized by the interval's length: <math display="block">f_X(x) = \begin{cases}

\displaystyle{1 \over b-a}, & a \le x \le b \\

0, & \text{otherwise}.

\end{cases}</math>Of particular interest is the uniform distribution on the unit interval <math>[0, 1]</math>. Samples of any desired probability distribution <math>\operatorname{D}</math> can be generated by calculating the quantile function of <math>\operatorname{D}</math> on a randomly-generated number distributed uniformly on the unit interval. This exploits properties of cumulative distribution functions, which are a unifying framework for all random variables.

Mixed type

A mixed random variable is a random variable whose cumulative distribution function is neither discrete nor everywhere-continuous.

The measure-theoretic definition is as follows.

Let <math>(\Omega, \mathcal{F}, P)</math> be a probability space and <math>(E, \mathcal{E})</math> a measurable space. Then an <math>(E, \mathcal{E})</math>-valued random variable is a measurable function , which means that, for every subset , its preimage is <math>\mathcal{F}</math>-measurable; <math>X^{-1}(B)\in \mathcal{F}</math>, where <math>X^{-1}(B) = \{\omega : X(\omega)\in B\}</math>. This definition enables us to measure any subset <math>B\in \mathcal{E}</math> in the target space by looking at its preimage, which by assumption is measurable.

In more intuitive terms, a member of <math>\Omega</math> is a possible outcome, a member of <math>\mathcal{F}</math> is a measurable subset of possible outcomes, the function <math>P</math> gives the probability of each such measurable subset, <math>E</math> represents the set of values that the random variable can take (such as the set of real numbers), and a member of <math>\mathcal{E}</math> is a "well-behaved" (measurable) subset of <math>E</math> (those for which the probability may be determined). The random variable is then a function from any outcome to a quantity, such that the outcomes leading to any useful subset of quantities for the random variable have a well-defined probability.

When <math>E</math> is a topological space, then the most common choice for the σ-algebra <math>\mathcal{E}</math> is the Borel σ-algebra , which is the σ-algebra generated by the collection of all open sets in <math>E</math>. In such case the <math>(E, \mathcal{E})</math>-valued random variable is called an <math>E</math>-valued random variable. Moreover, when the space <math>E</math> is the real line <math>\mathbb{R}</math>, then such a real-valued random variable is called simply a random variable. Note that we are not giving <math>\mathbb R</math> the usual Lebesgue <math>\sigma</math>-algebra, which is the completion of the Borel <math>\sigma</math>-algebra. This choice allows for more measurable functions <math>f:\Omega\to \mathbb R</math> and makes it easier to check that a function <math>f:\Omega\to \mathbb R</math> is measurable, as we only need to check that preimages of open sets are measurable.

Real-valued random variables

In this case the observation space is the set of real numbers. Recall, <math>(\Omega, \mathcal{F}, P)</math> is the probability space. For a real observation space, the function <math>X\colon \Omega \rightarrow \mathbb{R}</math> is a real-valued random variable if

: <math>\{ \omega : X(\omega) \le r \} \in \mathcal{F} \qquad \forall r \in \mathbb{R}.</math>

This definition is a special case of the above because the set <math>\{(-\infty, r]: r \in \R\}</math> generates the Borel σ-algebra on the set of real numbers, and it suffices to check measurability on any generating set. Here we can prove measurability on this generating set by using the fact that .

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 <math>\operatorname{E}[X]</math>, and also called the first moment. In general, <math>\operatorname{E}[f(X)]</math> is not equal to <math>f(\operatorname{E}[X])</math>. Once the "average value" is known, one could then ask how far from this average value the values of <math>X</math> typically are, a question that is answered by the variance and standard deviation of a random variable. <math>\operatorname{E}[X]</math> can be viewed intuitively as an average obtained from an infinite population, the members of which are particular evaluations of <math>X</math>.

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

Moments can only be defined for real-valued functions of random variables (or complex-valued, etc.). If the random variable is itself real-valued, then moments of the variable itself can be taken, which are equivalent to moments of the identity function <math>f(X)=X</math> of the random variable. However, even for non-real-valued random variables, moments can be taken of real-valued functions of those variables. For example, for a categorical random variable X that can take on the nominal values "red", "blue" or "green", the real-valued function <math>[X = \text{green}]</math> can be constructed; this uses the Iverson bracket, and has the value 1 if <math>X</math> has the value "green", 0 otherwise. Then, the expected value and other moments of this function can be determined.

Functions of random variables

A new random variable can be defined by applying a real Borel measurable function <math>g\colon \mathbb{R} \rightarrow \mathbb{R}</math> to the outcomes of a real-valued random variable <math>X</math>. That is, <math>Y=g(X)</math>. The cumulative distribution function of <math>Y</math> is then

: <math>F_Y(y) = \operatorname{P}(g(X) \le y).</math>

If function <math>g</math> is invertible (i.e., <math>h = g^{-1}</math> exists, where <math>h</math> is <math>g</math>'s inverse function) and is either increasing or decreasing, then the previous relation can be extended to obtain

: <math>F_Y(y) = \operatorname{P}(g(X) \le y) =

\begin{cases}

\operatorname{P}(X \le h(y)) = F_X(h(y)),

& \text{if } h = g^{-1} \text{ increasing} ,\\

\\

\operatorname{P}(X \ge h(y)) = 1 - F_X(h(y)),

& \text{if } h = g^{-1} \text{ decreasing} .

\end{cases}</math>

With the same hypotheses of invertibility of <math>g</math>, assuming also differentiability, the relation between the probability density functions can be found by differentiating both sides of the above expression with respect to <math>y</math>, in order to obtain