An Exercise In Conditional Probability — From 1000 Exercises In Probability
This is not particularly difficult, but it’s important to pay attention to the sequence of events to get the model right. Let’s first get the structure of the network and then worry about equations for the probabilities.
Here is one possible structure to capture the general causal flow of things and answer the five parts of the problem. The first coin is randomly picked and will be flipped twice (flip1 and flip2) and then discarded. A new coin will be randomly picked from the remaining four and flipped once.
We are given that two are double-headed, one is double-tailed, and two are normal. We can initialize the hypothesis node manually as unnormalized count data or with an equation (and let Netica normalize automatically):
Or with an equation:
P (first_coin | ) =
first_coin == HH ? 2 : // two double headed
first_coin == TT ? 1 : // 1 double tailed
first_coin == HT ? 2 : 0 // 2 normal coins
// select0 (first_coin, 2, 1, 2) Next is the Conditional Probability table (CPT) for the two flips (of the same but unknown coin). Again,n we can do this manually or with an equation:
The equations are slightly more involved ( The select0 is an alternate form, using the state index) :
P (flip1 | first_coin) =
first_coin == HH ? (flip1 == H ? 1 : 0) :
first_coin == TT ? (flip1 == T ? 1 : 0) :
first_coin == HT ? (flip1 == H ? 1/2 : 1/2) : 0
// flip1 == H ?
// select0 (first_coin, 1, 0 , 1/2) :
// select0 (first_coin, 0, 1, 1/2)
P (flip2 | first_coin) =
first_coin == HH ? (flip2 == H ? 1 : 0) :
first_coin == TT ? (flip2 == T ? 1 : 0) :
first_coin == HT ? (flip2 == H ? 1/2 : 1/2) : 0 We can manually enter the new count for the new coin since only 4 coins remain, given that the first coin is discarded. What remains is a function of what the first discarded coin was. Once again, we can enter the unnormalized coin count and let Netica automatically normalize (dividing by 4).
If not manually entered, then we do so with a clever equation:
P (new_coin | first_coin) =
( select0 (new_coin, 2, 1, 2) // original counts: HH, TT, HT
- (new_coin == first_coin ? 1 : 0) ) // subtract discarded coinClaude AI gave me this clever equation ( which turns out to be a manifestation of the Kronecker delta), which was more elegant than my original clunker (this works fine, but it’s brute force)!
Once the equations are compiled, we have this updated network. We can answer the first question immediately by looking at flip1:
Now we are ready to enter further observations to see how the network is further updated. Watch carefully and see the sequence of updates (the green numbers correspond to the question being answered):
Notice that once we enter the observation for flip1, we get an updated posterior for HH, which in turn informs the new expectation that the second toss (flip2) will be heads. And when observed to be heads again, the probability for HH for a new coin correspondingly goes down, as does the probability that a newly selected coin will turn up heads (at 52.5%, down from 60%).
Get Pascal Bercker’s stories in your inbox
Join Medium for free to get updates from this writer.
Each number corresponds to the five questions and their answers below:
BOOK SOLUTION






