Tossing a coin
In the video, you have seen our custom get_heads_prob()
function that estimates the probability of success of a binomial distribution. In this exercise, you will use it yourself and verify whether it does its job well in a coin-flipping experiment.
Watch out for the confusion: there are two different probability distributions involved! One is the binomial, which we use to model the coin-flipping. It's a discrete distribution with two possible values (heads or tails) parametrized with the probability of success (tossing heads). The Bayesian estimate of this parameter is another, continuous probability distribution. We don't know what kind of distribution it is, but we can estimate it with get_heads_prob()
and visualize it.
numpy
and seaborn
have been imported for you as np
and sns
, respectively.
This exercise is part of the course
Bayesian Data Analysis in Python
Exercise instructions
- Generate a list of 1000 coin tosses (0s and 1s) with 50% chance of tossing heads, and assign to the variable
tosses
. - Use the
tosses
and theget_heads_prob()
function to estimate the heads probability, and assign the result toheads_prob
. - Draw a density plot of the distribution of the heads probability you have just estimated.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Generate 1000 coin tosses
tosses = ____(____, ____, ____)
# Estimate the heads probability
heads_prob = ____
# Plot the distribution of heads probability
____(____, shade=True, label="heads probabilty")
plt.show()