Think Bayesian Dice!
This comes from the second edition of this book: THINK BAYES 2e! by author and educator Allen Downey (with an accompanying website and Python code, all intended to be beginner-friendly!).
Here is the question (originally from another book, Bernoulli’s Fallacy by Aubrey Clayton):
Suppose in one round of the game we had this sequence of outcomes, with G representing a greater roll, L a lesser roll, and E an equal roll:
G , G, L, E, L, L, L, E, G, L
Based on this data, what is the posterior distribution of T? What is your best guess for the value of T?
Allen Downey fully works this example with Python and makes his solution available on Collab. My aim here is to rework this example, but with a Bayesian Network (I use Netica).
One way to start is with the target node T and just a single observation node, and then build from there. Let’s call the mystery roll T1, and the clue is that T1 > T (hence G). A Boolean node, R1, encodes the first clue that T1 >T.
Compiling the equation gives this table:
We can now activate this very small network to see how the distributions are changed:
But we have 9 more clues to worry about. Surely we’re not going to add all 10 nodes …?
Yes, this is, of course, brute force, but we had to start somewhere. We can refine the model and make this much more compact, but let’s first activate this network and see what happens to the distribution for target node T:
We can significantly reduce and simplify the network structure, but at the cost of making the equations slightly more complex. We still have the discrete central node T representing the first dice roll. In a sense, T represents 6 hypotheses, which are initially equally probable. The ten evidence nodes (T1, …, T10) will tell us how each roll is related to T (greater than, less than, or equal to T):
Since we don’t need to know the distribution of each of the ten dice rolls, our evidence node needs only to track if each roll was greater than (G), less than (L), or equal (E) to the target dice T. This is the CPT for each evidence node:
A useful exercise is to fill this table manually with unnormalized count data (Netica automatically normalizes afterwards) :
This means, for example, that if T = 1, there are 5 values of T1 greater than T: only 1 is equal to T, and of course none can be less than T.
Get Pascal Bercker’s stories in your inbox
Join Medium for free to get updates from this writer.
More simply (but equivalently), we can also compile this equation:
After we compile the equations, we get :
We do the same with all 10 nodes, and once compiled, we have this updated network:
Let’s watch how that updating works. In particular, watch how the distribution for the target node T changes with each added observation (but notice what happens with node R4 and R8):
Along the way, we noticed that T4 and T8 (both E, for equal) made no difference, meaning that they gave no useful evidence either way and could have been omitted.
But can we still do better? Yes, we can, with the Multinomial Distribution, a generalization of the Binomial Distribution. We use this since there are 3 possible outcomes (G, E, L).
We have m = 3 possible outcomes (G, E, L), and we track with count nodes count _G, Count_E, and Count_L.
Notice that the equation is very nearly identical to what we used earlier, except that this one node will do the business of all ten nodes we needed earlier!
Let’s do a sanity check to be sure that this works as expected. We will activate the network and then step through the target node T, 1 through 6. We are watching to see that the count nodes work as expected. For example when T = 1, then count_L should be zero, since none can be less than 1.
This passes our sanity check and works as expected.
So now lets enter our count where Count_G = 3, Count_E = 2 and Count_L = 5 (for a total of 10).
So our final answer for the distribution of T and our best guess for its value:
SOURCES & REFERENCES
This was the prompt to Gemini Pro, which I asked ChatGPT to generate:








