Random Variables
The concept of a random variable is a fundamental idea in probability theory. Although we will not delve into the precise definition and intricacies of this concept today, we will explore what happens when we apply a function to a random variable. As a brief, intuitive summary, it is worth noting that a random variable is essentially a number that depends on the outcome of a random experiment, such as the number shown when rolling a die: we know the possible values and how likely each of them is, but not which one will actually come up.
Consider, for instance, measuring a distance with a laser. The measured value will not be exactly the same each time, even if the laser is pointed at the same spot. This variability arises from environmental factors and imperfections in the laser measurement device. As a result, each measurement attempt yields slightly different values. The measured values are therefore better interpreted as random variables, and typically they follow a normal distribution around the true distance, which only an omniscient observer could know.
In practice, such measurements often serve as inputs for further computations and calculations. Naturally, the outcomes of these computations depend on the distribution of the input measurements. In particular, the computed result is itself better regarded as a random variable that follows a certain distribution, since any variability in the inputs will lead to some variability in the final result. Therefore, it is advantageous to determine the distribution of the resulting values based on the input distribution. For instance, with this information, one can easily assess the precision of the computed values, which needless to say are of significant importance. Without an understanding of the variance or standard deviation, such computed values are often worth little to nothing.
Linear Function of a Normally Distributed Random Variable
So let us proceed with a concrete derivation, using the familiar normal distribution and the simplest function we can think of: a linear function. Consider a random variable \(X\) that follows a normal distribution. We denote this as follows
\begin{equation} X \sim \mathcal{N}. \end{equation}Now, we apply a linear function to obtain a new random variable \(Y\)
\begin{equation} Y = aX + b, \quad Y \sim \text{ }?. \end{equation}Our goal now is to somehow find a way to determine the distribution of \(Y\).
First observation we can make, if we add a constant \(b\) that will obviously just shift the mean. Therefore, if \(aX\) follows a distribution \(\mathcal{D}\) then \(aX+b\) will follow the same distribution, just with an increased or decreased mean corresponding to the value of \(b\). Thus, the question essentially reduces to understanding the effect of multiplying a normally distributed random variable \(X\) by a constant.
A useful technique for solving such problems is to use cumulative probabilities. In our example, we can consider
\begin{equation} P(Y \leq y) \end{equation}whereby \(Y = aX\). For simplicity, let us assume \(a > 0\); the case \(a < 0\) works analogously, except that the inequality sign flips when dividing by \(a\). Reformulating above probabilities leads to
\begin{equation} P(Y \leq y) = P(aX \leq y) = P(X \leq \frac{y}{a}) \end{equation}and since \(X \sim \mathcal{N}\) it follows that this cumulative distribution function (CDF) is given by
\begin{equation} Y_{CDF}(y) = \int_{-\infty}^{\frac{y}{a}} \frac{1}{\sqrt{2\pi\sigma^2}}e^{-(x-\mu)^2 / 2\sigma^2} dx \end{equation}There we have it, an expression for the cumulative distribution function of our random variable \(Y\). It's a rather complex integral, and it is difficult to deduce anything from it. So, let us try to find the probability density function as well. As you might know, the density function is just the derivative of the cumulative distribution function (if it is differentiable). Since this prerequisite definitely holds for our expression above, we can easily obtain the density by
\begin{equation} Y_{PDF} = Y_{CDF}' = \frac{1}{a}\frac{1}{\sqrt{2\pi\sigma^2}}e^{-(y/a-\mu)^2 / 2\sigma^2} \end{equation}This expression should look familiar; it is actually another normal distribution, just with a different variance (scaled by \(a^2\)) and different mean (scaled by \(a\)). If you do not see this immediately, try to factor out the \(a\) of the numerator in the exponent's fraction. This will lead to \(-(y-a\mu)^2 / 2\sigma^2a^2\). Then, after moving the division by \(a\) at the very beginning under the square root, you can see that
\begin{equation} \frac{1}{\sqrt{2\pi\sigma^2a^2}}e^{-(y-a\mu)^2 / 2\sigma^2a^2} = \mathcal{N}(a\mu,a^2\sigma^2). \end{equation}For \(a < 0\) the calculation is the same, except that the factor in front becomes \(\frac{1}{|a|}\), which leads to exactly the same normal distribution. To sum things up, we have shown that a random variable \(Y=aX + b\) with \(a \neq 0\), where \(X\) follows a normal distribution, is again normally distributed. In particular if \(X \sim \mathcal{N}(\mu, \sigma^2)\) then \(Y\) is normally distributed with \(\mu_Y = a\mu + b\) and \(\sigma_Y^2 = a^2\sigma^2\) (taking also the already established fact that adding a constant \(b\) will just shift the mean of whichever distribution the original random variable had into account).
Usefulness of This Result
Admittedly, on closer inspection this result is not particularly surprising. A linear function is essentially nothing more than a change of units. It will therefore never change the shape of the distribution itself.
Nevertheless, it turns out to be a very useful result. This is because a linear function can also be a good local approximation of a function, in the sense of a Taylor series.
So if we have a measured value whose distribution we know, and we want to use it for further calculations, we can simply compute the linear approximation of the function at that value. Locally, we are then only ever dealing with a linear function, and the distribution of the final result can easily be determined, at least approximately.
Moreover, in practical cases we will very often deal exclusively with normal distributions (due to the central limit theorem, whose first special case we have already examined in De Moivre's Approximation of the Binomial Distribution; how Gauss derived the normal distribution as the law of measurement errors is shown in Gauss' Original Derivation of the Normal Distribution). For propagating the distribution through the linear approximation, we can therefore almost always restrict ourselves to the normal distribution.
So once we have determined the effect of a linear function on a normal distribution, as we did in the previous section with \(\mathcal{N}(a\mu+b,a^2\sigma^2)\), we can easily set up a system for approximately determining the distribution of quantities computed from values riddled with errors, as shown in the following figure.
This system, or procedure, is commonly known as propagation of error — and it is exactly this approximate propagation of a normally distributed random variable.
Example of Propagation of Error
So let us work through a concrete example of using the method of propagation of error. Let us assume we have measured some angle \(\theta\) and we use this angle to determine the height of a building, as you can see in the following figure.
We have positioned ourselves a distance \(d\) of 100 meters away from the skyscraper for the measurement. Using the tangent function, we will be able to compute the exact height of it. However, since it is of course impossible to measure the angle completely accurate to the 100th digit after the comma, there will be some uncertainty involved. Some environmental factors, like shimmering air, could introduce even more variability.
Considering all those effects that influence our measurement, let us assume that we are able to determine the angle with a standard deviation of 0.1 degree. That means, we assume that our measurement we take will roughly be normally distributed with a standard deviation of 0.1 degrees (i.e. a variance of \(\sigma^2 = 0.01\)).
If we now calculate the height of the skyscraper, the question arises, how accurate is this result actually? So, what we are interested in, is the distribution of our resulting value if the input is distributed normally with \(\mathcal{N}(\mu_{old}, \sigma_{old}^2)\).
If we wanted to answer this question rigorously, we would have to determine the distribution of the result h, given by
\begin{equation} h = \tan(\theta) \cdot d, \quad \theta \sim \mathcal{N}, \end{equation}which needless to say is a rather cumbersome task to do. It could easily happen that this distribution is not expressible in terms of standard functions but we will only be able to provide a converging series. Luckily, however, we can use propagation of error, which will make this whole thing a walk in the park.
First, we have to find the best linear approximation for our function \(h = \tan(\theta) \cdot d\) at the point \(\theta_m\), which shall denote the actual measured value of \(\theta\). This is given by the Taylor polynomial of first degree, which leads to
\begin{equation} h_{linear}(\theta) = \tan(\theta_m) \cdot d + \frac{d}{\cos^2(\theta_m)} \cdot (\theta - \theta_m). \end{equation}Now, we know that if we approximate our function with this linear function the resulting variable will be again normally distributed, whereby the mean is given by the original mean scaled by the slope of the linear function plus the constant from the linear function. The variance is given by the slope squared times the original variance. Applying this to our example results in
\begin{equation} \mu = \tan(\theta_m) \cdot d + \frac{d}{\cos^2(\theta_m)} \cdot (\mu_{old} - \theta_m) \end{equation} \begin{equation} \sigma^2 = \frac{d^2}{\cos^4(\theta_m)} \cdot \sigma_{old}^2. \end{equation}Note that the variance depends very strongly on the angle \(\theta\). In particular, as \(\cos(\theta)\) approaches zero, the variance goes to infinity. This is a very interesting result and tells us quite a bit about the geometry of our setup. The following figure shows the tangent function as well as the factor by which the new \(\sigma\) is scaled.
We see that the tangent goes to infinity at 90 degrees. In our measurement, this would correspond to standing very close to the building relative to its height and looking almost straight up.
In this case it makes sense that even a small change in the angle already makes a huge difference in the result, the height of the building. This is illustrated by the red areas in the figure above. A small range of input values is stretched onto a huge range of results. This is the intuitive reason why the factor scaling the variance explodes as well.
Finally, let us quickly work through our concrete example. Suppose we measure an angle of \(\theta_m = 41.5^\circ\). The height then evaluates to \(h = \tan(41.5^\circ) \cdot 100\,\text{m} \approx 88.47\,\text{m}\). For the variance, we have to keep in mind that the derivative \(\frac{1}{\cos^2(\theta)}\) of the tangent only holds for angles measured in radians. The standard deviation of 0.1 degrees therefore corresponds to \(\sigma_{old} = 0.1 \cdot \frac{\pi}{180} \approx 0.001745\) in radians. Plugging this into the formula for the variance yields
\begin{equation} \sigma^2 = \frac{d^2}{\cos^4(\theta_m)} \cdot \sigma_{old}^2 = \frac{100^2}{\cos^4(41.5^\circ)} \cdot \left(0.1 \cdot \frac{\pi}{180}\right)^2 \approx 0.097\,\text{m}^2. \end{equation}The computed height thus has a standard deviation of \(\sigma \approx 0.31\,\text{m}\).
Raising a Normally Distributed Variable to the n-th Power
After our little excursion into propagation of error, let us now return to the exact derivation of the distributions that arise from applying a function. Let us continue by examining what happens to a normally distributed random variable when we raise it to the n-th power (\(x^2, x^3, x^4, \cdots\)). In practice, this could correspond, for example, to calculating an area or a volume.
We will again use cumulative probabilities and arrive at the density function by differentiation. Defining our new random variable by
\begin{equation} Y = X^n, \quad X \sim \mathcal{N}(\mu, \sigma^2) \end{equation}then we can state the following
\begin{equation} P(Y \leq y) = P(X^n \leq y) = \end{equation} \begin{equation} P(-\sqrt[n]{y} \leq X \leq \sqrt[n]{y}), \quad \text{if } n \text{ is even} \end{equation} \begin{equation} P(X \leq \sqrt[n]{y}), \quad \text{if } n \text{ is odd}. \end{equation}So we end up with two different cases, arising from the fact that even powers are guaranteed to cancel the sign of the input random variable, whereas odd powers do not. For even \(n\), \(Y\) can therefore only take on positive values, while for odd \(n\), \(\sqrt[n]{y}\) denotes the real root, which is negative for negative \(y\). Computing the cumulative distribution function by integrating the density function of the random variable \(X\) for both cases leads to
\begin{equation} \int_{-\sqrt[n]{y}}^{\sqrt[n]{y}} \frac{1}{\sqrt{2\pi\sigma^2}}e^{-(x-\mu)^2/2\sigma^2} dx, \text{ } n \text{ even} \end{equation} \begin{equation} \int_{-\infty}^{\sqrt[n]{y}} \frac{1}{\sqrt{2\pi\sigma^2}}e^{-(x-\mu)^2/2\sigma^2} dx, \text{ } n \text{ odd}. \end{equation}We now need to differentiate both expressions with respect to \(y\). The first one is a bit tricky because both integral boundaries involve the variable \(y\). However, by the fundamental theorem of calculus together with the chain rule, each boundary simply contributes one term: the density of \(X\) evaluated at that boundary, multiplied by the derivative of the boundary. The derivative of the root is \(\frac{1}{n}|y|^{\frac{1-n}{n}}\), and the lower boundary enters with a negative sign twice, once as the lower limit and once through its derivative, so both terms add up. This way, we obtain the following two density functions
\begin{equation} Y_{PDF} = \frac{y^{\frac{1-n}{n}}}{n\sqrt{2\pi\sigma^2}} \left(e^{-\frac{(\sqrt[n]{y}-\mu)^2}{2\sigma^2}} + e^{-\frac{(\sqrt[n]{y}+\mu)^2}{2\sigma^2}}\right), \text{ } n \text{ even}, \text{ } y > 0 \end{equation} \begin{equation} Y_{PDF} = \frac{|y|^{\frac{1-n}{n}}}{n\sqrt{2\pi\sigma^2}} e^{-\frac{(\sqrt[n]{y}-\mu)^2}{2\sigma^2}}, \text{ } n \text{ odd}, \text{ } y \neq 0. \end{equation}In the special case \(\mu = 0\), the two exponential terms for even \(n\) are identical, and the two probability densities become almost the same, differing only by a factor of 2 (and the one for even powers being zero for negative values). Let's examine a graphical plot of these two functions for \(\mu = 0\) to better understand the difference.
In the image above, the two densities are shown for \(n=2\) and \(n=3\). As already mentioned, the density for even \(n\) is zero for negative values. In general, we get a curve that grows beyond all bounds at 0 and drops off very sharply as we move away from 0. This was to be expected intuitively, too. When we raise a number to a power with an exponent greater than 1, two things happen. If the absolute value of the number lies between 0 and 1, it becomes smaller in absolute value and moves closer to the \(y\)-axis. If its absolute value is greater than 1, on the other hand, it becomes larger in absolute value and moves away from the \(y\)-axis. The number 1 acts as a fixed point: no matter which exponent \(n\) we choose, it is always mapped onto 1 unchanged.
What does this mean for the density? What matters is how strongly a small interval gets squeezed or stretched by the power function, and this is exactly what the derivative \(nx^{n-1}\) tells us. Close to 0, it is less than 1 and even approaches 0 (for \(n = 2\), for instance, this holds for all \(x < \frac{1}{2}\)). An interval in this region is therefore mapped onto a much smaller interval lying even closer to 0. The same probability gets squeezed into a narrower range, so the density rises sharply there.
Strictly speaking, the stretching even begins before 1: the derivative is greater than 1 from \(x = n^{-\frac{1}{n-1}}\) on, i.e. from \(\frac{1}{2}\) for \(n = 2\) and from about 0.58 for \(n = 3\). Between 0 and 1, squeezing and stretching exactly balance out, since 0 is mapped onto 0 and 1 onto 1. For values whose absolute value is greater than 1, there is only stretching left: there, the derivative is greater than \(n\), so every interval is stretched onto a wider interval lying even further out. The same probability is spread over a larger range, and the density decreases accordingly (for negative values, the same holds mirrored). Both effects are illustrated with red and blue areas, repsecctively, in the following figure.
These considerations make particular sense if we start from a uniform distribution. If the distribution of the random variable had a very low density around 0, this might offset the effect. However, in our case, the normal distribution already has a higher density near zero, so this effect is even amplified, resulting in an extremely sharp drop in the density as the values on the \(y\)-axis increase. Obviously, the higher the power to which we raise our random variable, the sharper this drop-off becomes (because the derivative becomes even smaller close to 0 and substantially higher for values greater than 1 if the power is increased, the effect of condensing or spreading out gets even more pronounced than it is for lower powers).
Different Techniques for Determining the Distribution
We have now gone through two examples: firstly, a linear function and secondly, power functions, both with the random variable constrained to follow a normal distribution. The method of distribution functions, in which we rearrange the cumulative probabilities, worked well for these examples. However, are there cases where this approach fails? And what about distributions other than the normal distribution?
Generally speaking, the approach via cumulative probabilities always works. It can only get a bit tricky to rewrite \(P(f(X) \leq y)\) as an expression in \(X\) alone, such as \(P(X \leq \cdots)\). Rearranging for \(X\) was easy for a linear function and also quite quick for the powers. For even powers, however, we already ran into first difficulties, since we obtained a chain of inequalities and thus two variable boundaries in the resulting integral, which is because even powers are non-monotonic functions.
Let us delve deeper into this by generalizing the approach through cumulative probabilities. Consider a random variable \(X\) following a distribution \(\mathcal{D}\), whose density we denote by \(\mathcal{D}(x)\) as well. Now, let's apply a function \(f\) to it, yielding a new random variable
\begin{equation} Y = f(X), \quad X \sim \mathcal{D}. \end{equation}Then let us consider the cumulative probability \(P(Y \leq y)\), which can be rearranged to
\begin{equation} P(Y \leq y) = P(f(X) \leq y). \end{equation}In the next step, we always isolated \(X\), since this is the variable we started with and we know its probability density function. This allowed us to easily integrate up to the expression on the right side of the inequality, giving us a formula for the cumulative distribution function of \(Y\). To isolate \(X\), we simply need to determine the inverse function of \(f\). If \(f\) is strictly increasing, the inequality keeps its direction, which leads to
\begin{equation} P(Y \leq y) = P(f(X) \leq y) = \end{equation} \begin{equation} P(X \leq f^{-1}(y)) = \int_{-\infty}^{f^{-1}(y)} \mathcal{D}(x) dx. \end{equation}If \(f\) is strictly decreasing instead, the inequality flips, just as it did for \(a < 0\) in the linear case. However, what if this inverse function \(f^{-1}\) does not exist at all? In that case, you need to split the function \(f\) into sections for which an inverse does exist and compute those cases separately (if two sections map to the same range of \(Y\) you have to take the sum). This is what we did, for instance, with the even powers in the example of the power function applied to a normally distributed random variable. We handled the set of negative and positive numbers separately, and since both map to the range of all positive numbers on the \(y\)-axis, we summed the two results. For \(\mu = 0\), this summation effectively evaluated to just multiplying by two.
For now, to not complicate things and arrive at a general solution for the cumulative probabilities approach, let us continue with the assumption that \(f\) is strictly monotonic, so that a globally valid inverse function exists.
Assuming that, we can move on to determining the density of \(Y\) by differentiating the above integral expression with respect to \(y\). The derivative of the integral will be given by
\begin{equation} PDF_Y(y) = \mathcal{D}(f^{-1}(y)) \left| {f^{-1}}'(y) \right|. \end{equation}The absolute value takes care of both cases at once: for a strictly increasing \(f\), the derivative of \(f^{-1}\) is positive anyway, and for a strictly decreasing \(f\), the flipped inequality yields an additional minus sign, which exactly cancels the negative derivative.
This final expression is usually referred to as the change-of-variables technique (or transformation method), and it is the standard tool to determine distributions when applying functions to random variables. Essentially, it is our approach via cumulative probabilities, carried out once in general for strictly monotonic functions and condensed into a ready-to-use formula. Its name stems from the fact that the same formula also follows directly from the substitution rule for integrals, i.e. from a change of variables \(y = f(x)\). However, the technique has some prerequisites: \(X\) must have a density (i.e. be continuously distributed; for discrete random variables, one simply adds up the probabilities anyway), and \(f\) must be differentiable with a derivative that is not zero (everywhere except on a finite set of points, to be precise). If \(f\) is not monotonic, the formula can still be applied piecewise, by summing over all points \(x_i\) with \(f(x_i) = y\), i.e. \(PDF_Y(y) = \sum_i \frac{\mathcal{D}(x_i)}{|f'(x_i)|}\), just as we did for the even powers. In several dimensions, the derivative is replaced by the Jacobian determinant, which is where this technique really shows its strength. The approach via cumulative probabilities, on the other hand, remains the more general one, as it even works if \(Y\) is discrete or \(f\) is not differentiable.
Beyond these two general approaches, there are also creative tricks for certain problems, which usually turn out to be clever ways of evaluating the cumulative probabilities. Let us see what something like this can look like. I call the following approach the creative geometric technique. Consider \(n\) independent, standard normally distributed random variables \([X_1, \cdots, X_n]\). Let us define a new random variable by
\begin{equation} Y = X_1^2 + \cdots + X_n^2. \end{equation}As you can deduce from the equation above, we are dealing with a multidimensional problem, where we have \(n\) random variables as inputs to our function, which maps them to the new random variable \(Y\). We will not delve into the specifics of why this particular function is of interest here, but it is, nevertheless, a function applied to random variables. Therefore, this is a problem that one might encounter.
Tackling it with our approach via cumulative probabilities leads to
\begin{equation} P(Y \leq y) = P(X_1^2 + \cdots + X_n^2 \leq y). \end{equation}The next step would be to isolate all the \(X_i\), which is not really possible. However, there is an easy shortcut if we put things into a geometric picture. Let us imagine a coordinate axis for each random variable \(X_i\) representing all the possible values this variable can take on. All \(n\) random variables together define an \(n\)-dimensional coordinate system. What does \(P(Y \leq y)\) ask for in this geometric setting?
Since \(Y = X_1^2 + \cdots + X_n^2\), asking for the values of \(X_i\) such that \(Y\) is less than or equal to some given number \(y\) essentially defines an \(n\)-dimensional sphere with radius \(\sqrt{y}\) in this coordinate system. All the points \([x_1,\cdots,x_n]\) that lie within this sphere will evaluate to a number \(\leq y\). This is a pretty good insight, right? Because now finding the cumulative probability \(P(Y \leq y)\) reduces to integrating the joint density of all \(X_i\) over this sphere. Finally, we then need to just differentiate the resulting integral expression and we have already obtained the PDF of \(Y\).
The detailed derivation of this PDF, however, requires finding a formula for the surface area of an \(n\)-dimensional sphere, which eventually leads to the Gamma function. So there is still quite a lot of work to do. We will hence omit the details here, but if you are interested in this derivation, which leads to the chi-squared distribution, you can check out Derivation of the Chi-Squared Distribution.
As a final point, we will investigate an approach that is quite drastically different from the ones we have discussed before. This technique, known as the technique of moment generating functions, approaches the problem from a completely different perspective.
We will not delve into the details of moment generating functions here. Assuming familiarity with the topic, if we have, for example
\begin{equation} Y = aX + b, \quad X \sim \mathcal{D} \end{equation}then we can obtain the density function of \(Y\) by firstly determining the moment generating function of \(\mathcal{D}\), which we will call \(M_X\). Then we determine how the moment generating function changes under the above defined function. For a linear function, the result is well known, and the moment generating function of \(Y\) is given by
\begin{equation} M_Y(t) = e^{bt}M_X(at). \end{equation}Finally, all that is left is to retrieve the density function from the just derived moment generating function. Depending on the original distribution \(\mathcal{D}\), this task can be quite straightforward, allowing for a relatively easy solution for the distribution of \(Y\) using this technique. However, whether this technique is applicable in the first place depends of course on the specific details of the problem at hand, not least because the moment generating function does not exist for every distribution (in that case, one can resort to the characteristic function, which always exists and works in a very similar way). The technique is especially handy for sums of independent random variables, since the moment generating function of such a sum is simply the product of the individual ones.
Conclusion and Outlook
On this exploration of applying functions to random variables, we came across many central concepts of this field. We started with the simple case of a linear function applied to a normally distributed random variable, where we found that the result again follows a normal distribution. Admittedly, this was not particularly surprising, but combined with the fact that any differentiable function can be approximated well locally by its first degree Taylor polynomial, it yields a powerful tool, especially since in practice one can often restrict oneself to the normal distribution. We thus saw how this forms the foundation of propagation of error and makes it easy to find an approximate solution and to determine the variance of a computed quantity. Afterwards, using the example of integer power functions, we looked at how the squeezing and stretching of intervals determines the resulting density.
Finally, we looked at the general techniques behind all of this: the method of distribution functions, which in principle always works, the change-of-variables technique as its ready-to-use formula for strictly monotonic, differentiable functions, the method of moment generating functions, and the creative geometric technique for the sum of squared random variables. Regarding other distributions and functions, however, we have not even really scratched the surface, and there are still countless things left to discover. Particularly exciting are those problems where the standard approaches do not work and one has to get creative (or at least greatly benefits from doing so), as we saw in the example of the sum of squares. To sum up, investigating how functions change the distribution of random variables leads to many interesting discoveries and invites many exciting explorations — and that is exactly what we will do in upcoming Stories. So stay tuned, and in the meantime, Derivation of the Chi-Squared Distribution and Distribution of the Sample Mean of a Normally Distributed Sample offer worthwhile continuations of this topic.