Exponential and Poission Distribution, Poisson Process


Basically,  inverse CDF is the basic method to generate a non-uniform random varible.
The procedure is like this:
Suppose that we wish to generate a continuous random variable X that has cdf F(X), which is
continuous and strictly increasing. Denote F-1(X).X to be the inverse function of FX. An algorithm for generating X is
The corresonding C program to generate a exponential distribution is:
/*
compile with
gcc -lm exp.c
test with
a.out 1000 2 > test.dat
then run R and examine with
test_scan("test.dat")
hist(test)
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float urand()
{
return( (float) rand()/RAND_MAX );
}

float genexp(float lambda)
{
float u,x;
u=urand();
x=(-1/lambda)*log(u);
return(x);
}

void main(int argc, char *argv[])
{
float e,lambda;
int i,n;
n=atoi(argv[1]);
lambda=atof(argv[2]);
for (i=0;i<n;i++)
{
e=genexp(lambda);
printf(" %2.4f \n",e);
}
}



As we have  exponential distribution, it is easy to get Poission Process. A matlab code to obtain it:
% poisson.m simulates a Poisson process

lambda=1; % arrival rate
Tmax=10; % maximum time

clear T;
T(1)=random('Exponential',1/lambda);
i=1;

while T(i) < Tmax,
T(i+1)=T(i)+random('Exponential',1/lambda);
i=i+1;
end

T(i)=Tmax;

stairs(T(1:i), 0:(i-1));

It will draw a figure of Sample function of Poission Process. Note that the last arrival is fixed as Tmax . It is not random.