UNIFORM DISTRIBUTION

 

 A random variable X is said to follow Uniform Distribution if

Px(x)=1/(b-a), a<x<b

Mean of X=(b+a)/2  ,

Variance of X=(b-a)2/12

where,

X is the random variable.

x represents the values that X can take.

Px(x) represents the Probability Density Function of X.

Examples:

1.     Phase Angle of a random sinusoidal wave.

Applications:

          In simulation it is used to generate other probability density functions.

 

ALGORITHM :

 

          The algorithm used to generate Uniform random variables is Linear Congruential Method (LCM) which is also known as Residue Method.

 

LINEAR CONGRUENTIAL or RESIDUE METHOD

         

          According to this method a sequence of integers Xi’shave a recursive relationship given by

                   Xi+1=(aXi+c)mod m ,   i=1,2,3,4…

where,

          X0 is the initial value.

          a is a constant multiplier.

          c is called the increment.

          m is the modulus.

          The selection of the above mentioned parameters X0 ,a, c, m drastically affect the statistical properties and the cycle length..

 

 

 

 

%Function to generate Uniform RV

function[c]=mrand(seed,c,a,m)

x=[];

%4 divides m so 4 divides a-1

%c and m are relatively prime

i=1;

x(i)=seed;

for i=2:m

   x(i)=mod((a*x(i-1)+c),m);

end

x=x/m;

c=x;

%Example of usage--> k=mrand(5,11,29,2^11);

 

Execution and Graph:

k=mrand(11,17,21,1000);

hist(k,20);

 

 

 

 

GAUSSIAN DISTRIBUTION

 

The p.d.f of guassian distributed R.V is

 

Gaussian RV’s drom Central Limit Theorem:

1.      For a single Gaussisan RV, the question is the number of Uniform RV’ s required. It has been shown that 12 Uniform RV’ s are convenient .

2.      Later, the application of the relation,

                     Y=(Sum (Xi) – k/2)/sqrt(k/12)   (k=12)

       gives a  Gaussian distributed RV.

3.       The histogram of the obtained array of Gaussian Distributed RV’ s are then plotted.

 

Gaussian RV’ s from Box – Mueller Algorithm:

1.      Generate two sets of Uniform RV’ s namely U1 and U2.

2.      The corresponding Gaussian RV’ s are given by:

X1=sqrt(- 2 ln U1) cos (2* p* U2)

X2=sqrt(-  2 ln  U1)sin(2* p* U2)

 

 

 

 

 

 

 

 

 

 

% Program to generate normal distributions using Box-Muller algorithm and Central Limit 
% theorem and compare the two distributions

numberofval=1000;
numberoftrials=10;

%Box - Muller Algorithm

for k=1:numberoftrials

u1 = rand(1,numberofval);
u2 = rand(1,numberofval);
for i=1:numberofval
X1(i) = sqrt((-2)*log(u1(i)))*cos(2*pi*u2(i));
X2(i) = sqrt((-2)*log(u1(i)))*sin(2*pi*u2(i));
end
varbox(k)=var(X1);
end
hist(X1);
title('PDF of Gaussian variate X1 genearted using Box-Muller algorithm');
figure(2);
hist(X2);
title('PDF of Gaussian variate X2 genearted using Box-Muller algorithm'); 

%Central Limit Theorem Method

iidnumber=12;
for k=1:numberoftrials
u=zeros(iidnumber,numberofval);
for i=1:iidnumber
u(i,:)=rand(1,numberofval);
end
norm = zeros(1,numberofval);
for j=1:numberofval
for i=1:iidnumber
norm (j) = norm(j) + u(i,j);
end
norm(j)=(norm(j) - iidnumber/2)/sqrt(iidnumber/12);
end
variid(k)=var(norm);
end
figure(3);
hist(norm);
title('PDF of Gaussian variate X generated using Central Limit Theorem');

% Comparison of the two normal distributions by comparing their variances with the 
% theoretical variance of 1


errbox=varbox-1;
erriid=variid-1;
for i=1:numberoftrials
errideal(i)=0;
end
figure(4);
plot(errbox,'r');
hold on;
plot(erriid,'b');
plot(errideal,'g');
title('Comparison of the errors in variance of the two distributions');
legend('Box-Miller','Central Limit Theorem','Ideal');
xlabel('Trial number');
ylabel('Error in variance');

  % Program to generate a Gaussian distribution from a uniform distribution

mean=5;
variance=3;
numberofval=1000;
u1=rand(1,numberofval);
expo = -2*log(u1); % Generation of exponential distribution with mean = 2
rayleigh = sqrt(expo); % Generation of Rayleigh distribution with sigma = 1
u2=rand(1,numberofval);
for i=1:numberofval
norm1(i) = rayleigh(i) * cos(2*pi*u2(i));
norm2(i) = rayleigh(i) * sin(2*pi*u2(i));
end
hist(norm1);
title('Normal distribution');
norm = norm1*sqrt(variance) + mean;
figure(2);
hist(norm);
title('Gaussian distribution with mean =5 and variance =3');

 

 

RAYLEIGH DISTRIBUTION

 

Generation of Rayleigh distribution from exponential :

 

This can be accomplished using the transformation

 

                        Y = Ö X

where X is an exponentially distributed  random variable .

 

Generation of Rayleigh distribution from normal distributions :

 

                        The formula used for this transformation is

 

                                                Y = Ö ( X12 + X22 )

            where X1 and X2 are normal distributions .

 

 % Program to generate Rayleigh ditribution using two methods 
numberofval=1000;
%First Method - rayleigh distribution from uniform distribution

u = rand(1,numberofval);
for i=1:numberofval
expo(i) = -2*log(u(i)); % Generation of exponential distribution
end
for j=1:numberofval
ray1(j) = sqrt(expo(j)); %Generation of Rayleigh distribution from exponential
end
hist(ray1);
title('PDF of Rayleigh distribution generated from uniform distribution');

% Second Method - rayleigh distribution from normal distributions
iidnumber=12;
for i=1:iidnumber
u(i,:)=rand(1,numberofval);
end
norm1 = zeros(1,numberofval);
for j=1:numberofval
for i=1:iidnumber
norm1 (j) = norm1(j) + u(i,j);
end
norm1(j)=(norm1(j) - iidnumber/2)/sqrt(iidnumber/12);
end

for i=1:iidnumber
v(i,:)=rand(1,numberofval);
end
norm2 = zeros(1,numberofval);
for j=1:numberofval
for i=1:iidnumber
norm2 (j) = norm2(j) + v(i,j);
end
norm2(j)=(norm2(j) - iidnumber/2)/sqrt(iidnumber/12);
end

for i=1:numberofval
ray2(i) = sqrt( norm1(i)^2 + norm2(i)^2 );
end
figure(2);
hist(ray2);
title('PDF of Rayleigh distribution generated from normal distributions');

 

 

 

The figure below shows the effect of Rayleigh fading channel in the place


 of AWGN channel.

   

  EXPONENTIAL DISTRIBUTION

 

 

    The exponential random variable with arrival rate λ > 0 is considered here.

This is a simple technique and has all the advantages of the inverse transform method. It is also reasonably fast.

           f(x) = λexp(-λx)        ;      F(x) = 1- exp(-λx)

          U = 1 – exp(-λz)       ;      z = (-1/λ)ln(1-U)

 If U is uniform in (0,1) then 1-U is also uniform in (0,1). Hence z = (-1/λ)lnU .

Algorithm uses the Transform Domain Approach.

  1. Let 1/l be the mean of the Random Process
  2. We use the transform domain approach
  3. After performing the calculations, we get,

z=-ln(U)/ l

            where U is the ensemble of Uniform Random Numbers.

 

Program:

%Funtion to generate Exponentially distributed RV

function[x]=mexp2(m,lamb);

m1=rand(1,m);

for i=1:length(m1)

   if m1(i)==0

      m1(i)=0.001;

   end

   x(i)=-1/lamb*log(m1(i));

end

 hist(x,25);

 

Execution and Graph:

x= mexp2(1000,3);

 

 

POISSON DISTRIBUTION

 

 

Algorithm:

1) Set A=1, k=0;

2) Generate U(0,1) i.e. uniformly dist RV

3) Set A=U*A

4) If A<exp(-l), deliver X=k and go to step 1

                      else, set k=k+1 and return to step 3

 

PROGRAM :

function poisson(N,mean)

n=1;

while(n<=N)

   a=exp(-mean);

   b=1;

   i=0;

   ui1=rand(1);  

   b=b*ui1;

   while(b>a)

      i=i+1;

      ui1=rand(1);

      b=b*ui1;

   end

   po(n)=i;

   n=n+1;

end

m=1;

max(po)

for t=0:1:max(po)

   p(m)=((mean^t)/fact(t))*exp(-mean);

   m=m+1;

end

c=[0:1:max(po)];

[d,q]=hist(po,c);

q

e=d/N;

h=[0:max(po)/[length(c)-1]:max(po)];

bar(h,e,'r');

title('Histogram of Poisson distributed random variable');

hold on;

t=0:1:m-2;

plot(t,p(t+1));

for w=1:length(q)

         pi(w)=((mean^q(w))/fact(q(w)))*exp(-mean);

  

   er(w)=e(w)-pi(w);

end

xlabel('-------> x');

ylabel('p(x)');

 

Here mean = 9

 

  

 

                     GAMMA DISTRIBUTION

 

The gamma distribution has two parameters namely α and β. The algorithm for generating gamma distributed random variables proposed by Cheng in 1977 is given below.

 

ALGORITHM :

 

1.     Generate U1 and U2 as IID U(0,1).

2.     Let V = a ln[ U1/(1-U1)], Y = αexp(V), Z = U1^2 * U2, and W = b + qV- Y.

3.     If W+d-θZ >= 0, return X = Y. Otherwise proceed to step 4.

4.     If W >= lnZ, return X = Y. Otherwise, go back to step 1.

 Where a = 1/sqrt(2α – 1) , b = α – ln4 , q = α + 1/a ,

θ = 4.5 and d = 1+lnθ

Here X is the gamma distributed random variate.

 

PROGRAM :

 

function gamma(N,alpha,beta)

a=1/sqrt(2*alpha-1);%INITIALIZE THE CONSTANTS

b=alpha-log(4);

q=alpha+(1/a);

theta=4.5;

d=1+log(theta);

n=1;

while(n<=N)

   u1=rand(1);%STEP 1 OF THE ALGORITHM

   u2=rand(1);

   v=a*log(u1/(1-u1));%STEP 2 OF THE ALGORITHM

   y=alpha*exp(v);

   z=(u1^2 )*u2;

   w=b+q*v-y;

   if(w+d-theta*z >=0)%STEP 3

      ga(n)=beta*y;

      a=1/sqrt(2*alpha-1);

      b=alpha-log(4);

      q=alpha+(1/a);

      theta=4.5;

      d=1+log(theta);

      n=n+1;

   else

      if(w>=log(z))%STEP 4

         ga(n)=beta*y;

         n=n+1;

         a=1/sqrt(2*alpha-1);

      b=alpha-log(4);

      q=alpha+(1/a);

      theta=4.5;

      d=1+log(theta);

   end

end

end

[l,h]=hist(ga,15*beta);

e=l/N;

s=[min(ga):(max(ga)-min(ga))/(15*beta-1):max(ga)];

bar(s,e,'r');%PLOT THE HISTOGRAM OF THE GENERATED VARIATES

hold on;

g=1;

for k=1:alpha-1

   g=g*k;

end

m=1;

for j=0:0.1:max(ga)

   p(m)=(1/(g*(beta^alpha)))*(j^(alpha-1))*exp(-j/beta);%THE ACTUAL DISTRIBUTION

   m=m+1;

end

j=0:0.1:max(ga);

c=1:m-1;

plot(j,p(c));%PLOT THE ORIGINAL CURVE

title('Histogram of Gamma distributed random variable');

for w=1:length(h)

   pi(w)=(1/(g*(beta^alpha)))*(h(w)^(alpha-1))*exp(-h(w)/beta);

   er(w)=e(w)-pi(w); %CALCULATE THE ERROR BETWEEN THE TWO

end

end

xlabel('--------------> x');

ylabel('p(x)');

 

 

 


LAPLACIAN DISTRIBUTION

 

LAPLACIAN DISTRIBUTED RV USING TRANSFORM METHOD:

    

              The transform method is used to generate Laplacian distributed random variates. An example of a Laplacian distributed random variable is human voice.

 

Method :

                                                        

                   f(x) = (1/2√2 )*exp(- x  /√2)

      

                     F(x) = 1 – exp(x/√2)/2    for x > 0

 

                             = exp(x/√2)/2          for x < 0

 

                     U =   exp(Z√2)/2         

                   

 Hence          Z = √2ln(2U)                  for x < 0

 

                     U =  1 – exp(Z√2)/2   

 

 Hence           Z = - √2ln(2U)                for x > 0

 

ALGORITHM :

 

1.     Generate U(0,1).

2.     Calculate Z1 =  √2ln(2U) and Z2 = - √2ln(2U)               

 

 

PROGRAM:

 

close all;

clear all;

 

y=0;

for i=1:1000

   b1=0;

   b2=0;

   u=rand(1);

   b1=-(sqrt(2))*log(2*u);

   b2=(sqrt(2))*log(2*u);

   y=[y b1 b2];

end

%Plotting of histogram

c=length(-5:1.1:5);

s=hist(y,c);

bar(-5:1.1:5,s/2000);

 

m=1;

for z=-5:0.05:5

 

   if z<0

   pz(m)=1/(2*sqrt(2))*(exp(z/(sqrt(2))));   %Laplacian distribution

  else

   pz(m)=1/(2*sqrt(2))*(exp(-z/(sqrt(2))));  %Laplacian distribution

end

m=m+1;

end

 

%Plotting of Laplacian distribution using pdf

hold on;

z=-5:0.05:5;

a=1:m-1;

plot(z,pz(a),'r');

grid on;

title('LAPLACIAN DISTRIBUTION');

xlabel('--------->X , Y');

ylabel('--------->p(X) , p(Y)');

legend('p(X)','p(Y)');