The Box-Muller Algorithm
There are many problems in computational science that can be efficiently solved using stochastic methods; examples include the evaluation of multidimensional integrals, analyzing “what-if” scenarios in control systems, and predicting future events in physics and finance. It is fairly straightforward to generate a list of pseudo-random numbers on a computer. However, these numbers are uniformly distributed. What if we want to generate a string of numbers with a different probability distribution, like a Gaussian distribution (i.e. a bell curve)?
The Box-Muller algorithm can be used to convert two sets of random numbers with uniform distributions into two sets of random numbers with Gaussian distributions. This algorithm does not use any approximation methods. Instead, it makes use of the famous Gaussian integral.
The Gaussian Integral
The Gaussian integral is given by:
If we try to integrate the Gaussian function using traditional methods, we quickly find that the integral does not have a neat solution. However, we can use a little trick to make short work of the integral.
Suppose I have two bell curves, one a function of and the other a function of . We can multiply these two integrals together to get:
Since we are analyzing a function in the xy-plane, let’s switch over to polar coordinates. Then, and . The limits of integration also change to and rather than and . Now we integrate over all values and all values.
Now we can use u-substitution to solve this integral. Let and . Then:
Here’s the cool part. Remember, and are just variable names. It does not matter if we call each variable , , , or cupcake. All that matters is the integral is equal to . Let’s just rename variable to . Then:
There we go! We have solved for the total area under a Gaussian curve. Admittedly, I skipped over some of the nuances of why this procedure works, so if you want to know all of the nitty gritty details, read up on Fubini’s theorem.
The Box-Muller Algorithm
Now that we have solved the Gaussian integral, the Box-Muller algorithm is pretty straightforward. Suppose I want to create two sets of random numbers ( and ), each with a probability density function given by a Gaussian integral ( and ). We can use the result above to get expressions for and :
We can call the total probability density function . Since and are independent sets of numbers:
This looks really familiar. Letting gives:
And now the interesting part. Suppose I can generate two uniformly distributed sets of numbers between zero and one, not including zero. Call them and . First, I can transform and into two new sets, and based on the expression above:
The first expression for represents a uniform distribution of numbers over all values from zero to (i.e. over all angles). The second expression for represents a Gaussian distribution. We can now convert these polar coordinates to cartesian coordinates using , and :
As weird as it sounds, and both have Gaussian distributions because and had Gaussian distributions!
We can see the transform in action below. I first genrate 500 pairs of random numbers that are uniformly distributed between zero and one. These 500 points look like a little box in the center of the plot. Then, when you click on the graph, the points change according to the Box-Muller transformation.
We can also verify that the transformations produce Gaussian distributions. Below I have plotted four graphs. The one in the upper left shows pairs of uniformly distributed random numbers. The graph in the upper right corner shows the Box-Muller transformation of these points. The two graphs on the bottom show the probability density functions of the original dataset and the transformed dataset. For simplicity, I only show the probability density function along the horizontal axis (), but the graphs look similar along the vertical axis. You can see that the graph in the upper left hand corner follows a uniform distribution, while the graph in the upper right hand corner follows a Gaussian distribution.
Click the graphs to redraw the points.
There we have it! We can use the Box-Muller transformation to generate random points with a Gaussian distribution. In the next couple of weeks, we will solve the diffusion equation and visualize it with the Box-Muller algorithm.
See you then!