'Is there a way to calculate probability with probability tree instead of simulation

I want to implement a function in python that gives me the exact probability (if possible in fraction form) of my following problem:

You have a list of 8 elements let's say l=[1,2,3,4,5,6,7,8], then you take succesively k number in that list, if the number is 1,2,3 or 4 then you take it off that list, otherwise you let that list as it is. The probability of having every number is equivalent.

I want to calculate the probability, within these n tries, to have at least the numer 1 and the probability to have 1 and 2.

For n=1 the probability to have 1 is 1/8

For n=2 the probability to have 1 is 1/8 + 4/8 * 1/8 +3/8 * 1/7= 27/112

to have 1 and 2 is 2 * 1/8 * 1/7 = 1/28

etc..

However as I can't formalize that probability into a formula. I tried to calculated it with an algorithm but it isn't simple.

I was able to simulate it in order to have an approximation but I'm not really satisfied with it.

def amalsimu2(adapt,n=int(1e6)):
    prob_to_find_both=0
    prob_to_find_one=0
    for _ in range(n):
        l=[i for i in range(1,9)]
        rec=[]
        for _ in range(adapt):
            draw=l[rd.randint(0,len(l)-1)]
            rec.append(draw)
            if draw<5:
                l.remove(draw)
        if 1 in rec:
            prob_to_find_one+=1
            if 2 in rec:
                prob_to_find_both+=1
    return [round(prob_to_find_one/n*100,3), round(n/prob_to_find_one,3)],[round(prob_to_find_both/n*100,3), round(n/prob_to_find_both,3)]

I looked a little bit into tree in python but I don't know if it is a good way to process.

If you have any idea on how to formulize the probability I want to compute or if you have a good idea on how to process to make it with a python algorithm, I would really appreciate that



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source