Search code examples
pythonsimulationprobability

How can I estimate the probability of coin flips with python?


I have created a program that simulates a specific number of coin flips. The simulation flips the coin 8 times, it is currently running the simulation 10000 times. I want to find out specific probabilities using the simulation. For example, what is the probability of getting exactly 2 tails in the 8 flips based on the 10000 results. I have a brief idea of how to do this, however I am not sure how to isolate exactly two results. The coin also has a random bias set to it.

Code:

# Random numbers:
coin_flip_seed = random_num

bias_choices = [0.2, 0.4, 0.6, 0.8]

coin_flip_bias = bias_choices[coin_flip_seed % 4]

# Main simulation:
np.random.seed(coin_flip_seed)

results = [['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S']]

tests = 10000

for j in range(tests):
    flips = []
   
    for i in range(8):
        flip_result = np.random.binomial(1,coin_flip_bias)
     
        if flip_result == 1:
            flips = np.append(flips, 'H')
        else:
            flips = np.append(flips, 'T')
   
    results = np.append(results, [flips], axis = 0)

results = results[1:]

print(results[4])
# Probability of getting exactly 2 tails:
num_2T = 0

for k in range(tests):
    if results[k,2] == 'T':
        num_2T += 1
        

print("The probability of getting exactly 2 tails is: ", num_2T/tests)

At the moment I am only able to find out the probability of getting 1 tail on the 3rd flip. Thanks in advance.


Solution

  • If I set the random seed:

    coin_flip_seed = 111
    

    And run your code, I get:

    num_2T
    1980
    

    Your results object is a numpy array, so to get the same result above you can just sum the number of entries in the 3rd column that are 'T' :

    np.sum(results[:,2] == 'T')
    1980
    

    Likewise, if we think in terms of the matrix you have now, what you need are the number of rows that have exactly 2 'T'. This gives you for each row, the number of 'T':

    np.sum(results=='T',axis=1)
    

    And we just count how many of them are 2:

    np.sum(np.sum(results=='T',axis=1)==2)
    2933
    

    Lastly as a comment, most likely you can simulate results using np.random.choice and providing the shape of your simulation. Should give you something similar.