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.
Diese Übung ist Teil des Kurses
Monte Carlo Simulations in Python
Anleitung zur Übung
- Set
pto the appropriate probability of success, where success is defined as flipping heads. - Using
pas the probability of success, sample from the geometric distributionst.geom10,000 times.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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()