# Monthly Archives: March 2012

## Emergence of Various Probability Distributions: Some Incite via Simulations

There a a large number of probability distributions that arise in statistical modeling and inference procedures. These distributions can take many complex analytic forms, and can have, what appear to be, arbitrary parameterizations. Though the definitions of many of these distributions are mathematically esoteric, it is often very helpful to see how they can emerge from simple simulation procedures. In this post we perform a number of simple manipulations to standard Normal/Gaussian-distributed variables that give rise to some probability distributions that predominate statistics:the , the F, and the Student’s t-distributions

# The Standard Normal Distribution

Before we begin the simulations, we need to first describe an important building block, the standard Normal distribution. The Normal distribution is a smooth continuous function that is symmetric about an axis and is often described as having a “bell” shape. The distribution lies at the heart of describing a vast number of physical processes. Also, many statistical quantities and distributions emerge from simply transforming values drawn from the standard Normal distribution (I’ll describe what I mean by “standard” shortly).

Any Normal distribution is characterized by two parameters: a mean parameter , which characterizes the location of the distribution center, and a variance parameter , which characterizes the width of the distribution. Formally, the Normal distribution defines the probability of some value occurring as:

The *standard* Normal distribution has zero mean and and unit variance (i.e. equal to 1), and therefore takes the simple form:

As I will show, a number of common probability distributions emerge from performing simple manipulations of values that follow the standard Normal distribution. In order to simulate these distributions we need to be able to draw values that follow the standard Normal distribution. Turns out that we can generate pseudo-random samples from a standard Normal distribution using a clever concept known as the Box-Muller transformation. Though I won’t go into the details, the Box-Muller transformation relates measurements on the unit circle (i.e. in polar coordinates) to norms of random vectors in Cartesian space (i.e. x-y coordinates). If none of that makes any sense, don’t worry it’s not important. What *is* important is that if we know how to independently draw two random numbers and that take on values 0 to 1, then it is possible to draw a sample from the standard Normal via the following relationship

or also

since sin(x) and cos(x) are orthogonal to one another. Figure 1 compares the empirical probability distribution of values drawn from a standard Normal distribution using the MATLAB function to the distribution of values drawn from the uniform distribution using the function and transforming those values using the Box-Muller transformation.

Note that the Box-Muller transformation is not the only (or even the best/most efficient) method for generating random variables from the standard Normal distribution (for instance, see the Polar Method). The reason for demonstrating the Box-Muller method is its simplicity. For a MATLAB example of drawing samples using the Box-Muller method, see lines (16-18) in the source code below.

Ok, so now that we have a way generating samples from the standard Normal distribution, we can now begin transforming these samples in order to develop other distributions.

# Simulating the Distribution

The distribution is a common probability distribution used in statistical inference (i.e. the test) and assessing the goodness of fit of linear models to data. Specifically, a random variable drawn from the with degrees of freedom is obtained by drawing independent variables from the standard normal distribution, squaring each value drawn and taking the sum of those squared values. Formally, this process is described as:

For a MATLAB example of simulating -distributed variables, see lines (37-54) in the source code below. The results of this simulation are shown in right of Figure 2, and are compared to the theoretical distribution, which takes the mathematical form:

For values of The is what is known as the Gamma function, and can be thought of as an extension to the factorial (i.e. !) operator to continuous values (i.e. ! only works on integers).

Perhaps it is just me, but I feel that it is far more natural to interpret the distribution as a sum of squares of standard Normal variables, than this fairly complicated expression. The interpretation also lends itself well to testing the goodness of fit of a linear model to data using the sum of squares loss function. This brings us to a second probability distribution, which is often used in the analysis of variance of linear model fits.

# Simulating the F-Distribution

In order to test the goodness of fit of a linear model to some data, it is often helpful to look at the ratio of the mean squared error of the model prediction to the mean squared deviation of the data from its mean (i.e. the variance of the data). This ratio can be thought of as the proportion of the variance in the data that is explained by the model. Because this ratio is composed of two values that result from squaring and summing deviations or errors, we are equivalently taking the ratio of values that follow two distributions. If the numerator and denominator of this ratio are scaled by their respective degrees of freedom (i.e. the number of model parameters minus one for the numerator value and the number of datapoints minus one for the denominator), then the value that results is what is known as an F-statistic. These F-statistics can be used to quantify how well (i.e. the statistical significance) of the model fit by comparing it to the distribution of F-statistics values that follow the form:

for variables having and degrees of freedom. The funcion is known as the Beta function or integral.

We can also simulate distributions of F-statistics by generating two variables with respective degrees of freedom and (just as we did above), scaling each variable by it’s relative degrees of freedom and taking their ratio. Formally this is described by:

A MATLAB example of this simulation is shown in lines (77-104) in the code below. The results of the simulation are displayed in the bottom row of Figure 3.

Again, it insightful to interpret the F-distribution as simply the ratio of scaled distributions rather than the complicated mathematical expression for the F-distribution shown above.

# Simulating the Student’s t-distribution

During statistics analyses, it is often helpful to determine if the mean of a dataset of size is different than some specified value . In order to test the hypothesis is customary to calculate what is called a test statistic:

Here the sample mean is

and the sample variance is

The distribution of these t-values is known as the t-distribution. Like the Normal distribution, the t-distribution is symmetric and “bell” shaped, but has heavier tails than a Normal distribution and is parameterized by a single parameter that corresponds to the degrees of freedom in the sample (i.e. sample size). Formally, the t-distribution has the following form

The t-distribution is used in a number of other statistical tests as well, particularly when an available sample size is small. Also, because it was developed in order to make Guiness the fine beer that it is today, the Student’s t-distribution is likely even more important than the Normal distribution!

The t-distribution with degrees for freedom can be simulated by repeatedly sampling independent datapoints from the standard normal distributions and calculating t-values on these samples. A MATLAB simulation of various t-distributions is shown in Figure 4, along with comparisons to the theoretical function shown above (see lines (130-148) of the code below).

Also, another way to simulate a t-distribution (not shown here) is to sample values that are the ratio of a standard normal variable to the square root of a -distributed variable scaled by its degrees of freedom:

# Wrapping up

So, hopefully these simulations give you some incite on how some standard probability distributions can come about. Just another example of how “complex” ideas can be derived from simple manipulations of a general mechanism, but are often hidden in the mathematical detail and formalisms.

# MATLAB Code

(Use the toolbar in the upper right corner of the source in order to copy to your clipboard)

clear all; close all % SOME CONSTANTS nNu = 10; nSimu = 1e6; dx = 0.1; x = -6:dx:6; % PART 1: NORMAL DISTRIBUTION %----------------------------- % ONE CAN GENERATE NORMALLY-DISTRIBUTED RANDOM VARIABLES FROM % UNIFORMLY DISTRIBUTED VARIABLES USING A GEOMETRIC METHOD KNOWN % AS THE BOX-MULLER TRANSFORM fprintf('\nSimulating Normal Distribution From Uniform Random Variables.'); R = rand(nSimu,2); NSimu = sqrt(-2*log(R(:,1))).*cos(2*pi*R(:,2)); NTheory = randn(1,nSimu); % OR... % N = sqrt(-2*log(R(:,1))).*sin(2*pi*R(:,2)); % (THESE ACTUALLY COME IN PAIRS) figure('Name','Simulated Normal Distribution'); [nHistTheory,nBin] = hist(NTheory,x); nHistTheory = nHistTheory/(sum(nHistTheory)*dx); bar(nBin,nHistTheory,'k') hold on; [nHist,nBin] = hist(NSimu,x); nHist = nHist/(dx*sum(nHist)); stairs(nBin,nHist,'r','Linewidth',2) legend({'Theory','Simulated'}) title(sprintf('Simulated Mean = %1.4f\n Simulated Variance = %1.4f',mean(NSimu),var(NSimu))) xlim([-6 6]); fprintf('\nPress any key to continue.\n') pause % PART 2: CHI-SQUARED-DISTRIBUTION %---------------------------------- % SIMULATE AND CALCULATE THEORETICAL VALUES % FOR CHI-SQUARE DISTRIBUTION fprintf('\nSimulating Chi-Squared Distribution\n'); zFun0 = 'randn(1,nSimu).^2'; zFun = zFun0; dx = 0.01; x = 0:dx:15; legStr = {}; for nu = 1:nNu fprintf('\r dF = %d',nu); zTmp = eval(zFun); [zTmp,zBin] = hist(zTmp,x); zSimu(:,nu) = zTmp/(dx*sum(zTmp)); % SIMULATED zTheory(:,nu) = chi2pdf(x,nu); % THEORETICAL zFun = [zFun,'+',zFun0]; legStr{end+1} = sprintf('dF = %d',nu); end % DISPLAY CHI-SQUARE SIMULATIONS figure('Name','Simulated Chi-Squared Distribution') subplot(121);plot(zBin,zTheory,'Linewidth',2) xlim([0 15]); ylim([0,.4]); legend(legStr) xlabel('x'); ylabel('p(x)') title('Theoretical') subplot(122);plot(zBin,zSimu) xlim([0 15]); ylim([0,.4]); legend(legStr) xlabel('x'); ylabel('p(x)') title('Simulated') fprintf('\nPress any key to continue.\n') pause % PART 3: F-DISTRIBUTION %------------------------ % SIMULATE AND CALCULATE THEORETICAL VALUES % FOR F-DISTRIBUTION fprintf('\nSimulating F-Distribution\n'); nSimu = 1e5; cnt = 1; nu = [2,10,50,100]; x = 0:dx:5; zFun0 = 'randn(1,nSimu).^2'; zFun = zFun0; legStr = {}; for nuTop = nu zFunTop = zFun0; for iF = 1:nuTop-1 zFunTop = [zFunTop,'+',zFun0]; end for nuBottom = nu fprintf('\r dF1 = %d, dF2 = %d',nuTop,nuBottom); zFunBottom = zFun0; for iF = 1:nuBottom-1 zFunBottom = [zFunBottom,'+',zFun0]; end zTop = eval(zFunTop)/nuTop; % CHI-2 DISTRIBUTIONS SCALED BY dF zBottom = eval(zFunBottom)/nuBottom; Ftmp = zTop./zBottom; % F IS THE RATIO OF SCALED CHI-2s [Ftmp,zBin] = hist(Ftmp,x); FSimu(:,cnt) = Ftmp/(dx*sum(Ftmp)); FTheory(:,cnt) = fpdf(x,nuTop,nuBottom); cnt = cnt+1; end legStr{end+1} = sprintf('dF = %d',nuTop); end % DISPLAY F-DISTRIBUTION SIMULATIONS figure('Name','Simulated F Distribution') axCnt = cnt; for iP = 1:numel(nu); subplot(2,numel(nu),iP) plot(x,FTheory(:,(iP-1)*numel(nu)+1:iP*numel(nu)),'Linewidth',2); xlim([0 5]); ylim([0 2]); legend(legStr); title(sprintf('dF = %d',nu(iP))) xlabel('x'); ylabel('p(x) Theoretical'); subplot(2,numel(nu),numel(nu)+iP); plot(x,FSimu(:,(iP-1)*numel(nu)+1:iP*numel(nu))); xlim([0 5]); ylim([0 2]); legend(legStr); title(sprintf('dF = %d',nu(iP))); xlabel('x'); ylabel('p(x) Simulated'); end fprintf('\nPress any key to continue.\n') pause % PART 4 STUDENT'S T-DISTRIBUTION %--------------------------------- fprintf('\nSimulating Student''s t Distribution\n'); nSimu = 1e6; dx = 0.01; x = -6:dx:6; nu = [2 10 50 100]; legStr = {}; cnt = 1; for dF = nu fprintf('\r dF = %d',dF); x0 = randn(dF,nSimu); xBar = mean(x0); s = var(x0); mu = 0; % randn IS ZERO MEAN tTmp = (xBar - mu)./sqrt(s/dF); [tTmp,tBin] = hist(tTmp,x); tSimu(:,cnt) = tTmp/(dx*sum(tTmp)); % SIMULATED tTheory(:,cnt) = tpdf(x,dF); % THEORETICAL legStr{end+1} = sprintf('dF = %d',dF); cnt = cnt+1; end % DISPLAY T-DISTRIBUTION SIMULATIONS figure('Name','Simulated Student''s t Distribution') subplot(121);plot(tBin,tTheory,'Linewidth',2) xlim([-6 6]); ylim([0 .4]); legend(legStr) xlabel('x'); ylabel('p(x)') title('Theoretical') subplot(122);plot(tBin,tSimu,'Linewidth',2) xlim([-6 6]); ylim([0 .4]); legend(legStr) xlabel('x'); ylabel('p(x)') title('Simulated') fprintf('\nPress any key to finish demo.\n') pause clear all; close all