Generation of random variables with required probability distribution characteristic is of paramount importance in simulating a communication system. Here, I will demonstrate how we can generate a simple random variable, estimate & plot the probability density function (PDF) from the generated data and then match it with the intended theoretical PDF.

More such simple examples available in the book : Digital Modulations using Matlab : Build Simulation Models from Scratch – by Mathuranathan Viswanathan

## Step 1: Create the random variable:

A survey of commonly used fundamental methods to generate a given random variable is given in * [1]. *For this demonstration, we will consider the normal random variable with the following parameters : (\mu) – mean and (\sigma) – standard deviation. First generate a vector of randomly distributed random numbers of sufficient length (say (100000)) with some valid values for (\mu) – mean and (\sigma) . There are more than one way to generate this. Two of them are given below

- Method 1: Using the in-built ‘
function (requires statistics toolbox)**random’**

123mu=0;sigma=1;%mean=0,variance=1L=100000; %length of the random vectorR = random('Normal',mu,sigma,L,1);%method 1

- Method 2: Using
**‘randn’**function that generates normally distributed random numbers having \(\mu=0\) and \(sigma=1\).

123mu=0;sigma=1;%mean=0,variance=1L=100000; %length of the random vectorR = randn(L,1)*sigma + mu; %method 2

- Method 3: Box-Muller transformation
method using**[2]****‘rand’**function that generates uniformly distributed random numbers

12345mu=0;sigma=1;%mean=0,variance=1L=100000; %length of the random vectorU1 = rand(L,1); %uniformly distributed random numbers U(0,1)U2 = rand(L,1); %uniformly distributed random numbers U(0,1)R = sqrt(-2*log(U1)).*cos(2*pi*U2);

## Step 2: Plot the estimated histogram

Typically, if we have a vector of random numbers that is drawn from a distribution, we can estimate the PDF using the histogram tool. Matlab supports two in-built functions to compute and plot histograms:

– introduced before R2006a**hist**– introduced in R2014b**histogram**

Which one to use ? Matlab’s help page points that the* hist *function is

**not**

**recommended**for several reasons and the issue of inconsistency is one among them. The

*function is the*

**histogram****recommended**function to use.

Estimate and plot the *normalized* histogram using the **recommended*** ‘histogram’* function. And for verification, overlay the theoretical PDF for the intended distribution. When using the histogram function to plot the estimated PDF from the generated random data, use

*‘pdf’*option for

*‘Normalization’*option. Do not use the

*‘probability’*option for

*‘Normalization’*option, as it will not match the theoretical PDF curve.

1 2 3 4 5 6 7 |
histogram(R,'Normalization','pdf'); %plot estimated pdf from the generated data X = -4:0.1:4; %range of x to compute the theoretical pdf fx_theory = pdf('Normal',X,mu,sigma); %theoretical normal probability density hold on; plot(X,fx_theory,'r'); %plot computed theoretical PDF title('Probability Density Function'); xlabel('values - x'); ylabel('pdf - f(x)'); axis tight; legend('simulated','theory'); |

However, if you do not have Matlab version that was released before R2014b, use the* ‘hist’* function and get the histogram frequency counts ((f)) and the bin-centers ((x)). Using these data, normalize the frequency counts using the overall area under the histogram. Plot this normalized histogram and overlay the theoretical PDF for the chosen parameters.

1 2 3 4 5 6 7 8 9 10 11 |
%For those who don't have access to 'histogram' function %get un-normalized values from hist function with same number of bins as histogram function numBins=50; %choose appropriately [f,x]=hist(R,numBins); %use hist function and get unnormalized values figure; plot(x,f/trapz(x,f),'b-*');%plot normalized histogram from the generated data X = -4:0.1:4; %range of x to compute the theoretical pdf fx_theory = pdf('Normal',X,mu,sigma); %theoretical normal probability density hold on; plot(X,fx_theory,'r'); %plot computed theoretical PDF title('Probability Density Function'); xlabel('values - x'); ylabel('pdf - f(x)'); axis tight; legend('simulated','theory'); |

## Step 3: Theoretical PDF:

The given code snippets above, already include the command to plot the theoretical PDF by using the * ‘pdf’* function in Matlab. It you do not have access to this function, you could use the following equation for computing the theoretical PDF

$$ f_X(x) = \frac{1}{\sqrt{2 \pi \sigma^2}} exp \left[ -\frac{\left(x-\mu\right)^2}{2 \sigma^2} \right]

$$

The code snippet for that purpose is given next.

1 2 3 |
X = -4:0.1:4; %range of x to compute the theoretical pdf fx_theory = 1/sqrt(2*pi*sigma^2)*exp(-0.5*(X-mu).^2./sigma^2); plot(X,fx_theory,'k'); %plot computed theoretical PDF |

* Note: * The functions –

*and*

**‘random’***, requires statistics toolbox.*

**‘pdf’**## References:

[1] John Mount, ‘Six Fundamental Methods to Generate a Random Variable’, January 20, 2012

[3] Matlab documentation for various functions used : random, pdf, histogram, hist