AllenDowney / ThinkBayes2

Text and code for the forthcoming second edition of Think Bayes, by Allen Downey.
http://allendowney.github.io/ThinkBayes2/
MIT License
1.8k stars 1.49k forks source link

Chapter 3 exercise on socks - question and answers not aligned #40

Closed alisterfrench closed 9 months ago

alisterfrench commented 3 years ago

in the exercise on sock drawers, a single question is currently asked: "What is the probability that the socks are white"

Oddly two answer spaces are given and neither of the given answers seem to match the question.

I would cautiously propose that there is a 30% chance that the socks are white because a white pair occupies 1/8 of the "sock pair space" and matching pairs in total occupy 2/8 + 3/18

with a common denominator those fractions become 9/72 and 18/72 +12/72 which implies there is a 9 in 30 chance or 3 in 10 chance of a matching pair being white. this bears out my code's answer

hypos = ['WW', 'XX', 'XY'] probs = [1/8,(1/8)+(3/18),1-(1/4)-(3/18)] prior = Pmf(probs,hypos) prior likelihood = [1,1,0] posterior = prior * likelihood posterior.normalize() posterior['WW']

result: 0.30

57op commented 1 year ago

I agree with your statement, the solution provided in the book doesn't make sense in my head.

In my opinion, it's clearer to state the possible outcomes we are expecting and to follow the choosing of the socks step by step:

# K is black
prior = Pmf(1 / 5, ['WW', 'KK', 'RR', 'GG', 'BB'])
likelihood = [1 / 2, 1 / 2, 1 / 3, 1 / 3, 1 / 3]

# draw the first sock
pmf = prior * likelihood
pmf.normalize()
pmf['WW'] # 0.25

# draw the second sock
pmf = pmf * likelihood
pmf.normalize()
pmf['WW'] # 0.3
AllenDowney commented 1 year ago

I believe the answer in the book is correct under the assumption stated in the problem "For simplicity, let’s assume that there are so many socks in both drawers that removing one sock makes a negligible change to the proportions."

This assumption is not super realistic, but it is intended to simplify the problem so it can be solved by applying the methods in the chapter.

The solution has two steps: first, figuring out the probability that the socks were drawn from the drawer with black and white socks, and then the probability that the socks are white if they were drawn from the black and white drawer.