A PDF version of Transformation of Probability Densities is available. 271 kb, 14 pages (info)
This Wikibook shows how to transform the probability density of a continuous random variable in both the one-dimensional and multidimensional case.
In other words, it shows how to calculate the distribution of a function of continuous random variables.
The first section formulates the general problem and provides its solution.
However, this general solution is often quite hard to evaluate and simplifications are possible in special cases, e.g., if the random vector is one-dimensional or if the components of the random vector are independent.
Formulas for these special cases are derived in the subsequent sections.
This Wikibook also aims to provide an overview of different equations used in this field and show their connection.
A Wikibook showing how to transform the probability density of a continuous random variable in both the one-dimensional and multidimensional case. In other words, it shows how to calculate the distribution of a function of continuous random variables.
Let ${\vec {X}}=(X_{1},\ldots ,X_{n})$ be a random vector with the probability density function, pdf, $\varrho _{\vec {X}}(x_{1},\ldots ,x_{n})$ and let $f:\mathbb {R} ^{n}\to \mathbb {R} ^{m}$ be a (Borel-measurable) function.
We are looking for the probability density function $\varrho _{\vec {Y}}$ of ${\vec {Y}}:={\vec {f}}({\vec {X}})$.
First, we need to remember the definition of the cumulative distribution function, cdf, $F_{\vec {Y}}({\vec {y}})$ of a random vector: It measures the probability that each component of Y takes a value smaller than the corresponding component of y.
We will use a short-hand notation and say that two vectors are "less or equal" (≤) if all of their components are.
If n=1 and m=1, X is a continuously distributed random variable with the density $\varrho _{X}$ and $f:\mathbb {R} \rightarrow \mathbb {R}$ is a Borel-measurable function.
Then also Y := f(X) is continuously distributed and we are looking for the density $\varrho _{Y}(y)$.
In the following, f should always be at least differentiable.
Let us first note that there may be values that are never reached by f, e.g. y<0 if f(x) = x^{2}.
For all those y, necessarily $\varrho _{Y}(y)=0$.
We will now rearrange this expression in various ways.
Derivation using cumulated distribution function $F_{X}$Edit
At first, we limit ourselves to f whose derivative is never 0 (thus, f is a diffeomorphism).
Then, the inverse map $f^{-1}$ exists and f is either monotonically increasing or monotonically decreasing.
If f is monotonically increasing, then $x\leq f^{-1}(y)\Leftrightarrow f(x)\leq f(f^{-1}(y))=y$ and $f^{\prime }>0$. Therefore:
If now the derivative $f^{\prime }(x_{i})=0$ does vanish at some positions $x_{i}$, $i=1,\ldots ,N$, then we split the definition space of f using those position into $N+1$ disjoint intervals $I_{j}$.
Equation 5 holds for the functions $f_{I_{j}}$ limited in their definition space to those intervals $I_{j}$. We have
With the convention that the sum over 0 addends is 0 and using the inverse function theorem, it is possible to write this in a more compact form (read as: sum over all x, where f(x)=y):
Now we substitute u = f(x) in the integral on the right hand site, i.e. $x=f^{-1}(u)$ and ${\frac {du}{dx}}=f^{\prime }(x)$.
The integral limits are then from -∞ to y and in the rule “$dx={\frac {dx}{du}}\,du$” we have ${\frac {dx}{du}}={\frac {d\,f^{-1}(u)}{du}}$ due to the inverse function theorem.
Consequentially:
Following the same argument as in the last section, we can again derive equation 6.
This rule often misleads physics books to present the following viewpoint, which might be easier to remember, but is not mathematically sound:
If you multiply the probability density $\varrho _{X}(x)$ with the “infinitesimal length” $dx$, then you will get the probability $\varrho _{X}(x)\,dx$ for X to lie in the interval [x, x+dx].
Changing to new coordinates y, you will get by substitution:
The intuitive interpretation of the last expression is: one integrates over all possible x-values and uses the delta “function” to pick all positions where y = f(x).
This formula is often found in physics books, possibly written as expectation value, $\langle \ldots \rangle$:
Let us consider the following specific example: let $\varrho _{X}(x)={\frac {\exp[-0.5x^{2}]}{\sqrt {2\pi }}}$ and $f(x)=x^{2}$. We choose to use equation 6 (equations 5 and 7 lead to the same result). We calculate the derivative $f^{\prime }(x)=2x$ and find all x for which f(x)=y, which are $-{\sqrt {y}}$ and $+{\sqrt {y}}$ if y>0 and none otherwise. For y>0 we have:
This example is illustrated in the following graphics:
Random numbers are generated according to the standard normal distribution X ~ N(0,1). They are shown on the x-axis of figure (a). Many of them are around 0. Each of those numbers is mapped according to y = x², which is shown with grey arrows for two example points. For many generated realisations of X, a histogram on the y-axis will converge towards the wanted probability density function ρ_{Y} shown in (c). In order to analytically derive this function, we start by observing that in order for Y to be between any v and v+Δv, X must have been between either $-{\sqrt {v+\Delta v}}$ and $-{\sqrt {v}}$, or, between ${\sqrt {v}}$ and ${\sqrt {v+\Delta v}}$. Those intervals are marked in figures (b) and (c). Because the probability is equal to the area under the probability density function, we can determine ρ_{Y} from the condition that the grey shaded area in (c) must be equal to the sum of the areas in (b). The areas are calculated using integrals and it is useful to take the limit $\Delta v\to 0$ in order to get the formula noted in (c).
Another example is the inverse transformation method. Suppose a computer generates random numbers X with a uniform distribution on [0, 1], i.e.
If we want to obtain random numbers according to a distribution with the pdf $\varrho _{Z}$, we choose f as the inverse function of the cdf of Z, i.e. $Y=f(X)=F_{Z}^{-1}(X)$. We can now show that Y will have the same distribution as the wanted Z, $\varrho _{Y}=\varrho _{Z}$ by using equation 5 and the fact that $f^{-1}=F_{Z}$:
Random numbers y_{i} are generated from a uniform distribution between 0 and 1, i.e. Y ~ U(0, 1). They are sketched as colored points on the y-axis. Each of the points is mapped according to x=F^{-1}(y), which is shown with gray arrows for two example points. In this example, we have used an exponential distribution. Hence, for x ≥ 0, the probability density is $\varrho _{X}(x)=\lambda e^{-\lambda \,x}$ and the cumulated distribution function is $F(x)=1-e^{-\lambda \,x}$. Therefore, $x=F^{-1}(y)=-{\frac {\ln(1-y)}{\lambda }}$. We can see that using this method, many points end up close to 0 and only few points end up having high x-values - just as it is expected for an exponential distribution.
Mapping of a Random Vector to a Random Variable (n>1, m=1)Edit
We will now investigate the case when a random vector X with known density $\varrho _{\vec {X}}$ is mapped to (scalar) random variable Y and calculate the new density $\varrho _{Y}(y)$.
The direct evaluation of this equation is sometimes the easiest way, e.g., if there is a known formula for the area or volume presented by the integral.
Otherwise one needs to solve a parameter-depended multiple integral.
If the components of the random vector ${\vec {X}}$ are independent, then the probability density factorizes:
In this case the delta function may provide a fast tool for the evaluation. Replacing the integration bound with a step function inside the integral, $H(y-f({\vec {x}}))$ and using the fact that the derivative of a step function is the delta:
If one wants to avoid calculations with the delta function, it is of course possible to evaluate the innermost integral $\int dx_{1}$, provided that the components are independent:
If one uses the sum formula instead, the sum runs over all x_{1}, for which $f({\vec {x}})=x_{1}+x_{2}=y$, i.e. where x_{1} = y - x_{2}.
The derivative is ${\frac {\partial (x_{1}+x_{2})}{\partial x_{1}}}=1$, so that one also obtains the equation $\varrho _{Y}(y)=\int _{\mathbb {R} }\varrho _{X_{2}}(x_{2})\,\varrho _{X_{1}}(y-x_{2})\,dx_{2}$.
Integrating over x_{2} first leads to the following, equivalent expression:
If $Y=X_{1}-X_{2}$ with independent X_{1} and X_{2}, then $\varrho _{Y}(y)=\int _{\mathbb {R} }\varrho _{X_{1}}(x_{1})\,\varrho _{X_{2}}(x_{1}-y)\,dx_{1}$.
If $Y=X_{1}\cdot X_{2}$ with independent X_{1} and X_{2}, then $\varrho _{Y}(y)=\int _{\mathbb {R} }\varrho _{X_{1}}(x_{1})\,\varrho _{X_{2}}\left({\frac {y}{x_{1}}}\right){\frac {1}{|x_{1}|}}\,dx_{1}$.
If $Y={\frac {X_{1}}{X_{2}}}$ with independent X_{1} and X_{2}, then $\varrho _{Y}(y)=\int _{\mathbb {R} }\varrho _{X_{1}}(x_{2}\cdot y)\,\varrho _{X_{2}}(x_{2})\,|x_{2}|\,dx_{2}$.
Given the independent random variables X_{1} and X_{2} with the density
This example is illustrated in the following graphics:
Figure (a) shows a sketch of random sample points from a uniform distribution in a circle of radius 1, subdivided into rings. In figure (b), we count how many of those points fell into each of the rings with the same width Δv. Since the area of the rings increases linearly with the radius, one can expect more points for larger radii. For Δv → 0, the normalized histogram in (b) will converge to the wanted probability density function ρ_{Y}. In order to calculate ρ_{Y} analytically, we first derive the cumulated distribution function F_{Y}, plotted in figure (d). F_{Y}(y) is the probability to find a point inside the circle of radius v (shown in grey in figure (c)). For v between 0 and 1, we find $F_{Y}(v)={\frac {A_{v}}{A_{tot}}}={\frac {\pi v^{2}}{\pi 1^{2}}}=v^{2}$. The slope of F_{Y} is the wanted probability density function $\rho _{Y}(y)={\frac {dF_{Y}(y)}{dy}}=2y$, in agreement with figure (b).
Invertible Transformation of a Random Vector (n=m)Edit
Let ${\vec {X}}=(X_{1},\ldots ,X_{n})$ be a a random vector with the density $\varrho _{\vec {X}}(x_{1},\ldots ,x_{n})$ and let $f:\mathbb {R} ^{n}\to \mathbb {R} ^{n}$ be a diffeomorphism.
For the density $\varrho _{\vec {Y}}$ of ${\vec {Y}}:={\vec {f}}({\vec {X}})$ we have:
where $\Phi _{f^{-1}}=\left|{\frac {\partial (x_{1},\ldots ,x_{n})}{\partial (y_{1},\ldots ,y_{n})}}\right|$ is the Jacobian determinant of $f^{-1}$.
Note that $\Phi _{f^{-1}}=\left(\Phi _{f}\right)^{-1}$.
In the one-dimensional case (n=1), equation 10 coincides with equation 5.
Given the random vector ${\vec {X}}$ and the invertible matrix A and a vector ${\vec {b}}$, let ${\vec {Y}}=A{\vec {X}}^{T}+{\vec {b}}$. Then $\varrho _{\vec {Y}}({\vec {y}})=\varrho _{\vec {X}}\left(A^{-1}\,({\vec {y}}-{\vec {b}})\right)\;\left|\det A^{-1}\right|$. Also, $\det A^{-1}=1/\det A$.
Given the independent random variables $X_{1}$ and $X_{2}$, we introduce polar coordinates$Y_{1}={\sqrt {X_{1}^{2}+X_{2}^{2}}}$ and $Y_{2}=\operatorname {atan2} (X_{2},X_{1})$. The inverse map is $X_{1}=Y_{1}\,\cos Y_{2}$ and $X_{2}=Y_{1}\,\sin Y_{2}$. Due to Jacobian determinant $y_{1}$, the wanted density is $\varrho _{\vec {Y}}(y_{1},y_{2})=y_{1}\,\varrho _{\vec {X}}(y_{1}\,\cos y_{2},\;y_{1}\,\sin y_{2})$.
Possible simplifications for multidimensional mappings (n>1, m>1)Edit
Even if none of the above special cases apply, simplifications can still be possible.
Some of them are listed below:
Sometimes it is useful to split the integral region in equation 3 into parts that can be evaluate separately.
This can be made explicit by rewriting 3 with delta functions:
The parametrisation of the region where at the same time x_{1}^{2} + x_{2}^{2} + x_{3} ≤ y and x_{1}^{2} + x_{2}^{2} ≤ 1 and x_{3} ≥ 0 may not be obvious, so we use the two above formulas:
Now, we have split the integral such that the expression in brackets can be evaluated separately, because the region depends on x_{1} and x_{2} only and may contain x_{3} only as parameter.
If f is injective, then it can be easier to introduce additional helper coordinates Y_{m+1} to Y_{n}, then do the $\mathbb {R} ^{n}\to \mathbb {R} ^{n}$ transformation from section Invertible Transformation of a Random Vector and finally integrate out all helper coordinates of the so-obtained density.
Remark: If the joint pdf $\varrho _{\vec {Y}}(y_{1},y_{2})$, i.e. the conditional distribution, is not of interest and one is only interested in the marginal distribution with $\varrho _{Y_{1}}(y_{1})=\int _{\mathbb {R} }\varrho _{\vec {Y}}(y_{1},y_{2})\,dy_{2}$, then it is possible to calculate the density as described in section Mapping of a Random Vector to a Random Variable for the mapping Y_{1} = 1 X_{1} + 2 X_{2} + 3 X_{3} (and likewise for Y_{2} = 4 X_{1} + 5 X_{2} + 6 X_{3}).
In order to show some possible applications, we present the following questions, which can be answered using the techniques outlines in this Wikibook. In principle, the answers could also be approximated using a numerical random number simulation: generate several realizations of ${\vec {X}}$, calculate ${\vec {Y}}=f({\vec {X}})$ and make a histogram of the results. However, many such random numbers are needed for reasonable results, especially for higher-dimensional random vectors. Gladly, we can always calculate the resulting distribution analytically using the above formulas.
Suppose atoms in a laser are moving with normally distributed velocities V_{x}, $\varrho _{V_{x}}(v_{x})={\frac {\exp[-v_{x}^{2}/2\sigma ^{2}]}{\sqrt {2\pi \sigma ^{2}}}}$, σ^{2} = k_{B}T/m. Due to the Doppler effect, light emitted with frequency f_{0} by an atom moving with v_{x} will be detected as f ≈ f_{0} ( 1 + v_{x} / c ). Hence, f is a function of V_{x}. What does the detected spectrum, $\varrho _{f}$, look like? (Answer: Gaussian around f_{0}.)
Suppose the velocity components of an ideal gas (V_{x}, V_{y}, V_{z}) are identically, independently normally distributed as in the last example. What is the probability density $\varrho _{V}$ of $V={\sqrt {V_{x}^{2}+V_{y}^{2}+V_{z}^{2}}}$? (The answer is known as Maxwell-Boltzmann distribution.)
Suppose we do not know the exact value of X and Y, but we can assign a probability distribution to each of them. What is the distrubution of the derived property Z = X^{2} / Y and what is the mean value and standard deviation of Z? (To tackle such problems, linearisation around the mean values are sometimes used and both X and Y are assumed to be normally distributed. However, we are not limited to such restrictions.)
Suppose we consider the value of one gram gold, silver and platinum in one year from now as independent random variables G, S and P, respectively. Box A contains 1 gram gold, 2 gram silver and 3 gram platinum. Box B contains 4, 5 and 6 gram, respectively. Thus, ${\begin{pmatrix}A\\B\end{pmatrix}}={\begin{pmatrix}1&2&3\\4&5&6\end{pmatrix}}{\begin{pmatrix}G\\S\\P\end{pmatrix}}$. What is the value of the contents in box A (or box B) in one year from now? (The answer is given in an example above.) Note that A and B are correlated.
Note that the above examples assume the distribution of ${\vec {X}}$ to be known. If it is unknown, or if the calculation is based on only a few data points, methods from mathematical statistics are a better choice to quantify uncertainty.
Correlated random numbers can be obtained by first generating a vector of uncorrelated random numbers and then applying a function on them.
In order to obtain random numbers with covariance matrix C_{Y}, we can use the following know procedure: Calculate the Cholesky decomposition C_{Y} = A A^{T}. Generate a vector ${\vec {x}}$ of uncorrelated random numbers with all var(X_{i}) = 1. Apply the matrix A: ${\vec {Y}}=A{\vec {X}}$. This will result in correlated random variables with covariance matrix C_{Y} = A A^{T}.
With the formulas outlined in this Wikibook, we can additionally study the shape of the resulting distribution and the effect of non-linear transformations. Consider, e.g., that X is uniform distributed in [0, 2π], Y_{1} = sin(X) and Y_{2} = cos(X). In this case, a 2D plot of random numbers from (Y_{1}, Y_{2}) will show a uniform distribution on a circle. Although Y_{1} and Y_{2} are stochastically dependent, they are uncorrelated. It is therefore important to know the resulting distribution, because $\varrho _{\vec {Y}}(y_{1},y_{1})$ has more information than the covariance matrix C_{Y}.