And now for our programming challenge, this is completely optional and is fairly challenging
and requires a decent knowledge of programming but if you're up for a challenge
and want to learn to do something fun, let's dive in.
We have a bag filled with coins. Each of these coins maybe a bit different.
This coin may have a probability of landing on heads of 0.5
and this coin might have a probability of 0.1
Now, I'm going to draw one coin from this bag and I'm going to flip it repeatedly and each time I flip it,
I'd like you to tell me what your best guess is for the probability of it being heads on the next flip.
To do this, we're going to write a program in Python.
What we're going to ask you to write is a Python class called flip predictor
that's going to take the set of coins in the bag.
In this case, coins with probabilities of landing on heads of 0.5, 0.4, and 0.3
then we're going to flip the coins several times.
In this case, the results were heads, heads, tails, and heads.
After every flip, we'll tell you what the result was and then we'll ask you to tell us
what the probability of heads will be the next time we flip the coin.
So to solve this, remember that Bayes' Rule tells us that the probability of the coin selected
being a given coin is equal to the probability of the flip being what it was that it's head or tails
given coin i times the probability of coin i being the coin divided by the probability
of the flip either heads or tails.
So now let's look at the code you're going to have to write.
So here we are back in the our editor
with an outline of the program you're going to have to write.
First, we just make division work the way you'd expect it to work
that is 1/2=0.5 so you don't have to think about it.
Then we defined this thing called a class.
If you aren't familiar of what a class is, don't worry, you're not going
to actually have to do anything with the class.
We set up the class right here for you.
A class is just a way of grouping functions and data those functions can use.
So here we just set up our data and that is we're given a set of coins in our bag and we just call that
self.coins and so in these functions down here you're going to be able to
access the coins in your bag as self.coins and that's going to be a Python list.
And then we create this new list called self.probs and that's just going to be a list
with the same number of items as in coins giving the probability that
each point in time of a coin being the coin selected.
At first, since the coin was selected at random that probability is simply 1/n
so if there are three coins it's a third, if there are 10 coins it's a 10th.
So what I'd like you to do is fill in these two functions.
P heads is a function that just returns the probability of heads right now.
So given your best guess of the current probabilities,
what's the probability of coming up with heads?
And you can compute this using self.probs and self.coins.
Then for the slightly trickier part, I want you to write an update function and that's going to take
a result either in H or a T, and based on that result, you're going to update self.probs
so that p heads will return with the correct value in the future.
Good luck! This is a very challenging problem. I hope you enjoy it.