0

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

enter image description here

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 :)

Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74

1 Answers1

2

The Fj's are cumulative distribution functions chosen for two properties. 1) Together, with the appropriate weightings, they make up the Fx you're interested in, and 2) They should themselves be easy to generate from. The most common way would be to use inversion. Another possibility would be convolution. The details would depend on the specific set of Fj's.

If you're not familiar with either inversion or convolution, you really shouldn't be jumping into composition.

pjs
  • 18,696
  • 4
  • 27
  • 56
  • Thanks for your answer, so what I´m generating on my function is a random variable X with the cumulative distribution Fx I mentioned? Or do I still have some errors in my code? – Pablo Estrada Aug 13 '14 at 21:36
  • 1
    If you've correctly constructed your composition, you should end up with Fx. Your specific code looks wrong, though. When you generate a value `I`, that means that you should be using the `I`th CDF function to generate the next `X` value via some suitable method. It *doesn't* mean generate an `X` conditional on `I` matching the current loop index. – pjs Aug 13 '14 at 22:40
  • 1
    See page 59 of [this tutorial paper](http://www.informs-sim.org/wsc07papers/007.pdf) for a specific examples of how to generate a simple triangle distribution using a variety of alternative techniques, including composition. – pjs Aug 13 '14 at 22:45
  • Thanks pjs you really helped me here. I will remove the conditional statement and use I as the index of F to generate X. I´ve realized it is impossible to make this method work with any Fi as a lambda expression in python since I cannot use inversion(which is the only method I´m allowed to use) on all cdf´s. Thanks again – Pablo Estrada Aug 14 '14 at 02:48
  • 1
    @PabloEstrada Glad to help. You may still be able to use lambdas if you interpret the algorithm as "generate an X from the distribution with CDF I". In that case your lambda's would be any appropriate generating mechanism -- inversion, convolution, composition, acceptance/rejection, or even another composition. – pjs Aug 14 '14 at 04:12