I´m working on implementing the composition method algorithm that states the following:
Composition Approach Another method for generating rando variables is the composition approach. Suppose that X has CDF (Cumulative Distribution Function) Fx and we wish to simulate a value of X.
We can write
Where the Fj´s are also CDF´s and pj>0 for all j and Sum(Pj)=1
Composition Algorithm:
1. Generate I distributed on the non-negative integers so that:
P(I=j) = Pj
2. If I = j, then simulate Yj from Fj
3. Set X= Yj
Here´s my implementation in python, the part I´m not sure about is when I set
Yj = F[j](random.random())
Because I think I should be using the inverse of F[j]. I would like to get some clarification on whether the above line is correct or not.
Here´s the rest of the algorithm:
def composition_method(F,p):
X =[None]*len(p) #Inicializando list of size p
cont=True
while(cont):
for j in range (0,len(p)):
#1.Generate I, non negative integer sych that P(I=j)= Pj
I = inverse_transform(p)
#2. if I=j, simulate Yj from Fj
if(I=j):
Yj = F[j](random.random())
#3. Sets X= Yj
X[j]=Yj
cont = False
#4. Verififying x does not have None elements
for(elm in X):
if(elm==None):
cont =True
Any help would be highly appreciated :)