liveBook · Manning

Probability distribution monad

As explained in Fabulous Adventures in Data Structures and Algorithms

A probability distribution monad is a monadic structure whose context represents uncertainty over outcomes and whose chaining operation combines distributions according to conditional probabilities.

A probability distribution monad turns ordinary value workflows into workflows over random values. A discrete distribution generates T values according to a probability mass function; its unit wraps one value as a distribution. Chaining treats the first distribution as a prior and the next function as a likelihood, then combines their joint distribution and marginalizes the intermediate value. section 14.0 section 14.1

The monadic operations expose several useful interpretations:

  • Map is ordinary projection over a distribution, while Join flattens a distribution whose outcomes are themselves distributions. section 14.1
  • As an additive monad, distributions can merge supports, use an empty distribution as MZero, and filter values with Where. section 14.2

From a cold prior to a sneeze distribution

Start with a 90%/10% distribution for no cold versus cold. Bind it to a conditional sneeze distribution: without a cold, sneezing has weights 3/97; with a cold, 86/14. Marginalization produces weights of 113 for Yes and 887 for No, combining uncertainty about the cause with uncertainty about the symptom.

Flattening a choice of dice

A fair coin can choose between a six-sided die and a ten-sided die. The resulting nested distribution can be flattened with Join, yielding weights 8 for outcomes 1–6 and weights 3 for outcomes 7–10.

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Builds Bind, Map, Join, additive identity, and filtering, including joint-distribution and marginalization mechanics.
Examples
Demonstrates cold-and-sneeze inference and flattening a coin-selected six-sided or ten-sided die.
What it assumes
Assumes familiarity with generic types and monads while defining probability distributions, unit, Bind, Map, and Join.
What you can do after
Build it from scratch in C# using distribution constructors, Bind, Map, Join, and predicate filtering.
How it connects
Connects probability mass functions, priors, likelihoods, joint distributions, marginalization, monads, and additive monads.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.