<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 A dice problem
This notebook demonstrates the use of Pmf and Suite objects, and explores a dice-rolling problem that turns out to be more complicated than it sounds
Copyright 2016 Allen Downey
MIT License
Step1:  A dice problem
Suppose I have a six-sided die that is red on 2 sides and blue on 4 sides, and another die that's the other way around, red on 4 sides and blue on 2.
I choose a die at random and roll it, and I tell you it came up red.  What is the probability that I rolled the second die (red on 4 sides)?
To answer this question, I'll create Pmf objects to represent the two dice
Step2:  And I'll make another Pmf to represent the random choice of one die or the other.
Step3:  Now I can use the Random method to choose a die and then roll it.
Step4:  Scenario A
The following generator simulates the process of repeatedly choosing a die and then rolling it.
Step5:  We can use this generator to simulate rolls
Step6:  In the long run, the proportion of red and blue is 50-50.
Step7:  We can see that by computing the weighted mixture of the two dice
Step9:  To answer the original question, I'll create a suite of hypotheses where each hypothesis is represented by a die, and the likelihood of the data under each hypothesis is the probability that the given die yields the given outcome.
Step10:  Now I can create a suite that represents the prior distribution.
Step11:  And update it with the data.
Step12:  The posterior probabilities for d1 is 1/3 and the posterior probability for d2 is 2/3.
Intuitively, the posterior probability of d2 is higher than the prior (which was 1/2) because the outcome (red) is more likely on d2 than d1.  If we had rolled blue, the probability of d1 would be higher.
Now suppose I ask you to predict the outcome of the next roll.  Remember that in this scenario, I choose a new die each time, so what you learned on the first roll doesn't help you with the second.  The predictive distribution is a weighted mixture of the dice, using the prior weights
Step13:  Scenario B
Now consider a different scenario.  Instead of choosing a new die every time, I choose a die once and roll it repeatedly.  Here's a generator that simulates this scenario
Step14:  In the long run, the proportion of red is either 1/3 or 2/3, not 1/2
Step15:  After the first roll, the posterior suite is the same as in the previous scenario
Step16:  In this scenario, we know we are going to roll the same die each time, so the information we learned from the first roll informs our prediction for the second.
Specifically, now the predictive distribution is based on the posterior, not the prior
Step17:  Having seen one red, we are more inclined to belive that I am rolling d2, so we are more inclined to predict that I will roll red again.
If I do roll red again, we can update the posterior again, using the new data, and make a new prediction
Step18:  If we continue this process, we will eventually be confident that we know which die is being rolled
Step19:  And the predictive distribution will be close to 1/3 or 2/3, depending on which die we think it is.
Step20:  Scenario C
Now let's consider another scenario
Step21:  In this scenario, obviously, the outcome is always red
Step22:  But now suppose I ask you about the last die I rolled.  What is the probability that it is the reddish die, d2?
One each roll, there are four possible results, with these probabilities
Step23:  That's the same posterior we saw in Scenarios A and B.  So even though we knew the outcome would be red, nevertheless we learn something about the die.
Of course, now the predictive distribution is always red
Step24:  Scenario D
Finally, let's consider the scenario where I choose a die once and roll it repeatedly until the out come is red.
Step25:  Again, obviously, the outcome is always red
Step26:  But now the probability of getting red is the same regardless of which die I chose.  On average, it takes longer to get to red if I chose d1, but if I only tell you the outcome and don't tell you how many times I rolled, you have no way of knowing which die I chose.
So the posterior is the same as the prior.
Step27:  If you are not sure about that argument (and after all this I don't blame you), see below for a more persuasive argument.
And the predictive distribution is, again, always red.
Step29:  Summary
In summary, each of the four scenarios yields a different pair of posterior and predictive distributions.
Scenario        Posterior probability of d2      Predictive probability of red
A               2/3                              1/2
B               2/3                              5/9
C               2/3                              1
D               1/2                              1
Scenario D, the more persuasive version
Recall that in Scenario D I choose a die and then roll it until I get red.  I claim
Step30:  The likelihood is the geometric PMF with probability p.
If you know I got red on the first try, that's evidence in favor of d2
Step31:  If you know I got it on the second try, that's equally likely with d1 or d2
Step32:  If it takes three tries or more, that's evidence for d1.
<ASSISTANT_TASK:>
Python Code:
from __future__ import print_function, division
from thinkbayes2 import Pmf, Suite
from thinkbayes2 import MakeMixture
from fractions import Fraction
Explanation: A dice problem
This notebook demonstrates the use of Pmf and Suite objects, and explores a dice-rolling problem that turns out to be more complicated than it sounds
Copyright 2016 Allen Downey
MIT License: http://opensource.org/licenses/MIT
End of explanation
d1 = Pmf({'Red':Fraction(2), 'Blue':Fraction(4)}, label='d1 (bluish) ')
d1.Print()
d2 = Pmf({'Red':Fraction(4), 'Blue':Fraction(2)}, label='d2 (reddish)')
d2.Print()
Explanation: A dice problem
Suppose I have a six-sided die that is red on 2 sides and blue on 4 sides, and another die that's the other way around, red on 4 sides and blue on 2.
I choose a die at random and roll it, and I tell you it came up red.  What is the probability that I rolled the second die (red on 4 sides)?
To answer this question, I'll create Pmf objects to represent the two dice:
End of explanation
dice = Pmf({d1:Fraction(1), d2:Fraction(1)})
dice.Print()
Explanation: And I'll make another Pmf to represent the random choice of one die or the other.
End of explanation
dice.Random().Random()
Explanation: Now I can use the Random method to choose a die and then roll it.
End of explanation
def rollA(dice):
    while True:
        die = dice.Random()
        roll = die.Random()
        yield roll
Explanation: Scenario A
The following generator simulates the process of repeatedly choosing a die and then rolling it.
End of explanation
iterA = rollA(dice)
for i in range(5):
    print(next(iterA))
Explanation: We can use this generator to simulate rolls:
End of explanation
Pmf(next(iterA) for i in range(1000))
Explanation: In the long run, the proportion of red and blue is 50-50.
End of explanation
MakeMixture(dice).Print()
Explanation: We can see that by computing the weighted mixture of the two dice:
End of explanation
class Dice(Suite):
    def Likelihood(self, data, hypo):
        
        data: 'Red' or 'Blue'
        hypo: a Die object
        
        return hypo[data]
Explanation: To answer the original question, I'll create a suite of hypotheses where each hypothesis is represented by a die, and the likelihood of the data under each hypothesis is the probability that the given die yields the given outcome.
End of explanation
prior = Dice({d1:Fraction(1), d2:Fraction(1)})
prior.Print()
Explanation: Now I can create a suite that represents the prior distribution.
End of explanation
posterior = prior.Copy()
posterior.Update('Red')
posterior.Print()
Explanation: And update it with the data.
End of explanation
predictive = MakeMixture(prior)
predictive.Print()
Explanation: The posterior probabilities for d1 is 1/3 and the posterior probability for d2 is 2/3.
Intuitively, the posterior probability of d2 is higher than the prior (which was 1/2) because the outcome (red) is more likely on d2 than d1.  If we had rolled blue, the probability of d1 would be higher.
Now suppose I ask you to predict the outcome of the next roll.  Remember that in this scenario, I choose a new die each time, so what you learned on the first roll doesn't help you with the second.  The predictive distribution is a weighted mixture of the dice, using the prior weights:
End of explanation
def rollB(dice):
    die = dice.Random()
    while True:
        roll = die.Random()
        yield roll
Explanation: Scenario B
Now consider a different scenario.  Instead of choosing a new die every time, I choose a die once and roll it repeatedly.  Here's a generator that simulates this scenario:
End of explanation
iterB = rollB(dice)
Pmf(next(iterB) for i in range(1000))
Explanation: In the long run, the proportion of red is either 1/3 or 2/3, not 1/2:
End of explanation
posterior = prior.Copy()
posterior.Update('Red')
posterior.Print()
Explanation: After the first roll, the posterior suite is the same as in the previous scenario:
End of explanation
predictive = MakeMixture(posterior)
predictive.Print()
Explanation: In this scenario, we know we are going to roll the same die each time, so the information we learned from the first roll informs our prediction for the second.
Specifically, now the predictive distribution is based on the posterior, not the prior:
End of explanation
posterior.Update('Red')
posterior.Print()
predictive = MakeMixture(posterior)
predictive.Print()
Explanation: Having seen one red, we are more inclined to belive that I am rolling d2, so we are more inclined to predict that I will roll red again.
If I do roll red again, we can update the posterior again, using the new data, and make a new prediction:
End of explanation
posterior = prior.Copy()
for i in range(10):
    posterior.Update(next(iterB))
posterior.Print()
Explanation: If we continue this process, we will eventually be confident that we know which die is being rolled:
End of explanation
predictive = MakeMixture(posterior)
predictive.Print()
Explanation: And the predictive distribution will be close to 1/3 or 2/3, depending on which die we think it is.
End of explanation
def rollC(dice):
    while True:
        die = dice.Random()
        roll = die.Random()
        if roll == 'Red':
            yield roll
Explanation: Scenario C
Now let's consider another scenario:  Suppose I choose a die and roll it.  If the outcome is red, I report the outcome.  Otherwise I choose a die again and roll again, and repear until I get red.
Here's a generator that simulates this scenario.
End of explanation
iterC = rollC(dice)
Pmf(next(iterC) for i in range(1000))
Explanation: In this scenario, obviously, the outcome is always red:
End of explanation
posterior = prior.Copy()
posterior[d1] *= 1
posterior[d2] *= 2
posterior.Normalize()
posterior.Print()
Explanation: But now suppose I ask you about the last die I rolled.  What is the probability that it is the reddish die, d2?
One each roll, there are four possible results, with these probabilities:
d1, red       1/2 * 1/3
d1, blue      1/2 * 2/3
d2, red       1/2 * 2/3
d2, blue      1/2 * 1/3
On the last roll, I tell you that the outcome is red, so we are left with two possibilities:
d1, red       1/2 * 1/3
d2, red       1/2 * 2/3
The likelihood ratio is 2 to 1, so we can use that to update the prior:
End of explanation
predictive = Pmf({'Red':1.0})
predictive.Print()
Explanation: That's the same posterior we saw in Scenarios A and B.  So even though we knew the outcome would be red, nevertheless we learn something about the die.
Of course, now the predictive distribution is always red:
End of explanation
def rollD(dice):
    die = dice.Random()
    while True:
        roll = die.Random()
        if roll == 'Red':
            yield roll
Explanation: Scenario D
Finally, let's consider the scenario where I choose a die once and roll it repeatedly until the out come is red.
End of explanation
iterD = rollD(dice)
Pmf(next(iterD) for i in range(1000))
Explanation: Again, obviously, the outcome is always red:
End of explanation
posterior = prior.Copy()
posterior.Print()
Explanation: But now the probability of getting red is the same regardless of which die I chose.  On average, it takes longer to get to red if I chose d1, but if I only tell you the outcome and don't tell you how many times I rolled, you have no way of knowing which die I chose.
So the posterior is the same as the prior.
End of explanation
predictive = Pmf({'Red':1.0})
Explanation: If you are not sure about that argument (and after all this I don't blame you), see below for a more persuasive argument.
And the predictive distribution is, again, always red.
End of explanation
class ScenarioD(Suite):
    def Likelihood(self, data, hypo):
        
        data: k, number of times I rolled to get a Red
        hypo: a Die object
        
        p = hypo['Red']
        k = data
        return (1-p)**(k-1) * p
Explanation: Summary
In summary, each of the four scenarios yields a different pair of posterior and predictive distributions.
Scenario        Posterior probability of d2      Predictive probability of red
A               2/3                              1/2
B               2/3                              5/9
C               2/3                              1
D               1/2                              1
Scenario D, the more persuasive version
Recall that in Scenario D I choose a die and then roll it until I get red.  I claim:
If you know how many times I rolled before getting red, you get information about which die it was.
If you don't know how many times I rolled, you get no information.  That is, the posterior probabilities for d1 and d2 are the same as the priors.
To demonstrate the first part, fere's a Suite that takes as data the number of times I rolled (including the last one that came up red):
End of explanation
suite = ScenarioD([d1, d2])
suite.Update(1)
suite.Print()
Explanation: The likelihood is the geometric PMF with probability p.
If you know I got red on the first try, that's evidence in favor of d2:
End of explanation
suite = ScenarioD([d1, d2])
suite.Update(2)
suite.Print()
Explanation: If you know I got it on the second try, that's equally likely with d1 or d2:
End of explanation
suite = ScenarioD([d1, d2])
suite.Update(3)
suite.Print()
Explanation: If it takes three tries or more, that's evidence for d1.
End of explanation
<END_TASK>