Aan de slagGa gratis aan de slag

Sampling from a geometric distribution

Eva has a biased coin that has a probability of turning heads only 20% of the time. Eva flips her coin and records the number of flips needed to get a result of heads.

The geometric distribution is perfectly suited to model the number of flips needed to reach a result of heads, with the success rate p defined as the probability of turning heads each time.

Your task is to use the geometric distribution to simulate Eva's coin flips to reach heads 10,000 times, recording the number of flips needed to reach heads each time. Then, you'll visualize the results!

The following have been imported for you: seaborn as sns, pandas as pd, SciPy's stats module as st, and matplotlib.pyplot as plt.

Deze oefening maakt deel uit van de cursus

Monte Carlo Simulations in Python

Cursus bekijken

Oefeninstructies

  • Set p to the appropriate probability of success, where success is defined as flipping heads.
  • Using p as the probability of success, sample from the geometric distribution st.geom 10,000 times.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Set p to the appropriate probability of success
p = ____

# Sample from the geometric distribution 10,000 times
samples = ____
samples_dict = {"nums":samples}
sns.histplot(x="nums", data=samples_dict)  
plt.show()
Code bewerken en uitvoeren