Professional Documents
Culture Documents
,from table
specified significance
Level and the given sample size N.
Inverse Transform Technique
Aim: -
Implement Inverse Transform technique to find the random
variants:
Perform the following task to show the concept.
a) Define Inverse Transform
b) Write Algorithm for continuous distributions.
Write C code for implementing Inverse Transform
technique.
i) Exponential distribution:
a. Accept parameters | & u from the user.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X).
c. Display calculated terms.
ii) Weibull distribution:
a. Accept parameters of shape |>0, scale o>0 &
scale v from the user.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X).
c. Display calculated terms.
iii) Gamma distribution:
a. Accept parameters lamda from the user.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X).
c. Display calculated terms.
iv) Triangular distribution:
a. Accept parameters a, b, & c from the user,
where asbsc.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X), where X is distributed uniformly
between a, b.
c. Display calculated terms
Concept:-
The inverse transform technique can be used to sample
from the exponential, the uniform, the Weibull, and the
Triangular distributions and the empirical distributions
Additionally, it is the underlying principle for sampling
from a wide variety of discrete distributions. The technique
will be explained in detail for the exponential distribution
and then applied to other distributions. It is most straight
forward, but not always more efficient, technique
computationally.
(1) Exponential distribution:
Exponential distribution, has probability density
function (pdf) given by
f(x)= e
- x
, x>= 0
0, x< 0
And cumulative distribution function (cdf) given by
F (x)= }
-
x
f (t)dt = 1- e
- x
, x>= 0
0, x< 0
The parameter can be interpreted as the mean number of
occurrences per time unit. For e.g. if inter arrival times
X
1
, X
2
, X
3
had an exponential distribution with rate , then
could be interpreted as the mean number of arrivals per
time unit, or the arrival rate. Notice that for any i
E (X
i
) = 1/
So that 1/ is the mean inter arrival time. The goal is
to develop a procedure for generating values X1, X2, X3 that
have an Exponential distribution. The inverse transform
technique can be utilized, at least in principle, for any
distribution, but it is most useful when the cdf, F(x), is of
such simple form that its inverse, F
-1
, can be easily
computed. A step-by-step procedure for the
Inverse transform technique, illustrated by the exponential
distributions, is as follows:
Step1: Compute the cdf of the desired random variable X. For
the exponential distribution, the cdf is F(x)= 1-e
- x
,x>=0.
Step2: Set F(x)=R on the range of X. For the exponential
distribution, it becomes 1-e
- x
= R on the range x>=0.
Since X is a random variable, it follows that 1-e
- x
is also
a random variable, here called R. As will be showed later, R
has a uniform distribution over the interval (0,1).
Step3: Solve the equation F (x)=R on the range of X. For the
exponential distribution, the solution proceeds as follows:
1-e
- x
= R
-e
- x
=1- R
- x= In (1-R)
X = -1/ In (1-R) (I)
Equation I is called a random variate generator for the
exponential distribution. In general, equation I is written
as X = F
-1
(R). Generating a sequence of values is accomplished
through step 4.
Step4: Generate (as needed) uniform random numbers R1, R2,
R3, and compute the desired random variates by:
Xi=F
-1
(R
i
)
For the exponential case, F
-1
(R) = (-1/)In (1-R) by
equation(I), so that
Xi = -1/1 In (1-R) (II)
For I = 1, 2, 3, one simplification that is usually employed
in equation(II), is to be replace 1-Ri by R
i
to yield
Xi = -1/1 In (1-R) (III)
which is justified since both R
i
and 1- R
i
are uniformly
distributed on (0,1)
(2)Weibull distribution: -
Step 1 : The cdf is given by
|
F (x)= 1-e
-(x/o)
x>=0
|
Step 2 : F (x)= 1-e
-(x/o)
= R
Step 3 : Solving for X in terms of yields
X=o[-In(1-R)]
1-|
(3)Gamma distribution
If X is following exponention with parameter
n
then
X is gamma(n, ). To generate one observation or
i=1
One random variable from gamma, we have to first
generate n observations from exponential say X
1
, X
2
,...,
X
n
then Y=
X is random observation from gamma.
n
Therefore Y=-1/*
log(1-R)
i=1
(4)Triangular distribution:
Consider a random variable X that has pdf.
This distribution is called Triangular distribution with
endpoints (0,2) & mode at 1. Its cdf is given by:
f(x) = 2(x-a) a s x s b
(b-a)(c-a)
= 2(c-x) b s x s c
(c-b)(c-a)
= 0 otherwise (I)
F(x) = (x-a)
2
a s x s b
(b-a)(c-a)
= (c-x)
2
b s x s c
(c-b)(c-a)
= 0 x>c (II)
By equation (I), 0<=X<=1 implies that 0<=R<=1/2, in which
case X=2R.
By equation (II), 1<=X<=2 implies that 1/2<=R<=1, in which
case X=2-2(1-R).
Thus X is generated by , F(x) = a+(b-a)R.
Source Codes:
(1)Exponential Distribution:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<math.h>
int n,i,m=100;
float x[50];
float a,X[100];
float R[100];
void main()
{
clrscr();
printf("\nRandom Variate Generation for Exponential
Distribution \n");
printf("\n-----------------------------------------\n");
printf("\nPdf of Exponential Distribution is lamda*e^(-
lamda*x)\n");
printf("\nCdf of Uniform Distribution is 1-e^(-
lamda*x)\n");
printf("\nEnter the value of lamda(mean): ");
scanf("%f",&a);
printf("\nEnter the number of random numbers you are
going to provide : ");
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.2f\t",R[i]);
}
printf("\n\nX[i]=(-1/lamda)*log(1-R[i])");
printf("\n\n----------------------------------------\n");
printf("| i | R[i] |X[i]=-1/a*log(1-
R[i]|\n");
printf("--------------------------------------------\n");
for(i=0;i<n;i++)
{
x[i]=(-1/a)*log(1-((double)R[i]));
printf("| %d\t | %lf\t | %f\t
|\n",i+1,R[i],x[i]);
}
printf("-------------------------------------\n");
getch();
}
Output
Random Variate Generation for Exponential Distribution
------------------------------------------------------
Pdf of Exponential Distribution is lamda*e^(-lamda*x)
Cdf of Uniform Distribution is 1-e^(-lamda*x)
Enter the value of lamda(mean): 2
Enter the number of random numbers you are going to provide :
5
Random Numbers Generated are:
0.98 0.89 0.06 0.91 0.77
-----------------------------------------------
| i | R[i] |X[i]=-1/a*log(1-R[i]|
-----------------------------------------------
| 1 | 0.980000 | 1.956012 |
| 2 | 0.890000 | 1.103637 |
| 3 | 0.060000 | 0.030938 |
| 4 | 0.910000 | 1.203973 |
| 5 | 0.770000 | 0.734838 |
----------------------------------------------
(2) Weibull Distribution
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
int n,i;
double x[50];
float a,b,p;
float R[10],r1;
void main()
{
clrscr();
printf("\nRandom Variate Generation for Weibull Distribution
\n");
printf("\n--------------------------------------------------\n");
printf("\nPdf of Weibull Distribution is
(beta/(alpha)^beta)*(x^beta-1)(e^(-x/alpha)^beta)\n");
printf("\nCdf of Weibull Distribution is 1-e^(-x/alpha)^beta\n");
printf("\nEnter the value of alpha(float): ");
scanf("%f",&a);
printf("\nEnter the value of beta(float): ");
scanf("%f",&b);
p=a/b;
printf("\nEnter the number of random numbers you are going to
provide : ");
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.4f\t",R[i]);
}
printf("\n\n------------------------------------------\n");
printf("| i | R[i] | X[i]=(-
alpha/beta)*log(1-R[i])|\n");
printf("------------------------------------------\n");
for(i=0;i<n;i++)
{
r1=1-R[i];
x[i]=-p*log((double)r1);
printf("| %d\t | %f\t | %lf\t |\n",
i+1,R[i],x[i]);
}
printf("------------------------------------------\n");
getch();
}
Output
Random Variate Generation for Weibull Distribution
--------------------------------------------------
Pdf of Weibull Distribution is (beta/(alpha)^beta)*(x^beta-
1)(e^(-x/alpha)^beta)
Cdf of Weibull Distribution is 1-e^(-x/alpha)^beta
Enter the value of alpha(float): 2.5
Enter the value of beta(float): 3.4
Enter the number of random numbers you are going to provide : 10
Random Numbers Generated are:
0.0400 0.6300 0.4200 0.5700 0.2400 0.4200 0.1600 0.5900
0.0500 0.6100
----------------------------------------------------------
| i | R[i] | X[i]=(-alpha/beta)*log(1-R[i])|
----------------------------------------------------------
| 1 | 0.040000 | 0.030016 |
| 2 | 0.630000 | 0.731068 |
| 3 | 0.420000 | 0.400535 |
| 4 | 0.570000 | 0.620566 |
| 5 | 0.240000 | 0.201792 |
| 6 | 0.420000 | 0.400535 |
| 7 | 0.160000 | 0.128201 |
| 8 | 0.590000 | 0.655587 |
| 9 | 0.050000 | 0.037716 |
| 10 | 0.610000 | 0.692359 |
----------------------------------------------------------
(3) Gamma Distribution
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int n,i,j,n1;
float r[100],a,r1,r2,y,R[10];
clrscr();
printf("\nRandom variate generation using Gamma
Distribution\n");
printf("\n--------------------------------------------\n");
printf("\n\tConvolution Method");
printf("\n\t------------------");
printf("\nHow many gamma variates should be generated: ");
scanf("%d",&n1);
for(j=0;j<n1;j++)
{
printf("\nEnter the number of random numbers required to
generate %d gamma variate:",j+1);
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.4f\t",R[i]);
}
printf("\n\nEnter the value of lamda: ");
scanf("%f",&a);
r1=1.0;
r2=1.0;
for(i=0;i<n;i++)
{
r1= r2 *(1-R[i]);
r2=r1;
}
y=(-1/a) * log((double)r1);
printf("\n\nGamma variate is : %lf",y);
}
getch();
}
Output
Random variate generation using Gamma Distribution
--------------------------------------------------
Convolution Method
------------------
How many gamma variates should be generated: 2
Enter the number of random numbers required to generate 1
gamma variate:5
Random Numbers Generated are:
0.3800 0.9200 0.4500 0.4600 0.2600
Enter the value of lamda: 0.5
Gamma variate is : 9.037786
Enter the number of random numbers required to generate 2
gamma variate:4
Random Numbers Generated are:
0.5300 0.8300 0.0800 0.2300
Enter the value of lamda: 0.3
Gamma variate is : 9.572418
(4)Triangular Distribution
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<math.h>
int a,b,n,i;
float x[50],r1;
double R[100];
void main()
{
clrscr();
printf("\nRandom Variate Generation for Triangular
Distribution \n");
printf("\n---------------------------------------------------
--\n");
printf("\nEnter the number of random numbers you are going to
provide : ");
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.2f\t",R[i]);
}
printf("\nX[i]=(2*R[i])^1/2 0<=R[i]<=0.5");
printf("\n =2-((2*(1-R[i]))^1/2) 0.5<R[i]<=1");
printf("\n\n------------------------------------------\n");
printf("| i | R[i] | X[i] |\n");
printf("------------------------------------------\n");
for(i=0;i<n;i++)
{
r1=1-R[i];
if(R[i]>0 & R[i]<=0.5)
x[i]= sqrt(2*R[i]);
else if(R[i]>0.5 & R[i]<=1)
x[i]=2-(sqrt(2*r1));
printf("| %d\t | %lf\t | %f\t |\n",i+1,R[i],x[i]);
}
printf("------------------------------------------\n");
getch();
}
Output
Random Variate Generation for Triangular Distribution
Enter the value of lamda(mean): 1
Enter the number of random numbers you are going to provide :
20
Random Numbers Generated are:
0.92 0.71 0.62 0.09 0.32 0.76 0.72 0.77
0.63 0.07
0.70 0.61 0.98 0.12 0.64 0.35 0.63 0.36
0.33 0.02
-----------------------------------------------
| i | R[i] | X[i]=-1/alog(1-R[i]|
-----------------------------------------------
| 0 | 0.920000 | 2.525729 |
| 1 | 0.710000 | 1.237874 |
| 2 | 0.620000 | 0.967584 |
| 3 | 0.090000 | 0.094311 |
| 4 | 0.320000 | 0.385662 |
| 5 | 0.760000 | 1.427116 |
| 6 | 0.720000 | 1.272966 |
| 7 | 0.770000 | 1.469676 |
| 8 | 0.630000 | 0.994252 |
| 9 | 0.070000 | 0.072571 |
| 10 | 0.700000 | 1.203973 |
| 11 | 0.610000 | 0.941609 |
| 12 | 0.980000 | 3.912023 |
| 13 | 0.120000 | 0.127833 |
| 14 | 0.640000 | 1.021651 |
| 15 | 0.350000 | 0.430783 |
| 16 | 0.630000 | 0.994252 |
| 17 | 0.360000 | 0.446287 |
| 18 | 0.330000 | 0.400478 |
| 19 | 0.020000 | 0.020203 |
---------------------------------------------
Practical No:09
Acceptance-Rejection Technique:
Aim: -
Implement methods for generation of random variates, X for a
given Distribution.
Perform the following tasks to show the concept:
a. Define Acceptance Rejection Technique.
b. Write algorithm for Acceptance Rejection Technique.
c. Write C/C++/excel code for implementing:
1) Poisson distribution
a) Accept value of mean o, arrivals n and
probability of random variate p.
b) Display the calculated Poisson variates.
Concept:-
Method for generating random variates, X, uniformly
distributed between and one way to proceed would be to
follow these steps:
Step1 : Generate a random numbers R.
Step2 : a) If R>=1/4, accept X=R then goto step 3.
b) If R<=1/4, reject R, and then return to step 1.
Step3 : If another uniform random variate on [1/4, 1] is
needed repeat the procedure
beginning at step1, if not stop.
Poisson Distribution : -
A Poisson random variable, N with mean o>0 has pmf
p(n)= P(N=n)=e
-o
o
n
/n!, n=0,1,2,3,.
N can be interpreted as the number of arrivals from
Poisson arrival process in one
unit of time.
Step1 : Set n=0, P=1
Step2 :.Generate the random numbers R
n+1
and replace P by P*R
n+1
.
Step3 : If P<e-o, then accept N=n. Otherwise, reject the
current n, increase n by one, &
return to step2.
To generate one poisson variate, if N=n, then n+1 random
numbers are required, so the average number is given by:
E(N+1)= o+1, which is quite large if the mean, o, of the
poisson distribution is large.
`