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
.
This exercise is part of the course
Monte Carlo Simulations in Python
Exercise instructions
- 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 distributionst.geom
10,000 times.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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()