liveBook · Manning

Chapter 12: A better abstraction for randomness

What you will learn in chapter 12 of Fabulous Adventures in Data Structures and Algorithms

This chapter introduces a higher-level abstraction for discrete randomness, centered on representing and efficiently sampling unfair die rolls as categorical distributions in preparation for Bayesian reasoning.

After this chapter you will be able to

Section by section

Introduction

This chapter introduces a higher-level abstraction for discrete randomness, centered on representing and efficiently sampling unfair die rolls as categorical distributions in preparation for Bayesian reasoning.

Key terms: random number generating functions, random quantities, Where, SelectMany, Join, GroupBy, discrete probability distributions, categorical distribution, unfair die, sampling

12.1 What are probabilities?

This section contrasts frequentist and Bayesian meanings of probability and states how the book will use both perspectives.

Key terms: Frequentist, probability, outcome, trials, frequency, Bayesian, confidence, evidence, frequentist philosophy, Bayesian attitude

12.2 What are discrete probability distributions?

This section defines finite discrete probability distributions through probability mass functions and explains sampling outcomes according to their assigned probabilities.

Key terms: probability distribution, support, discrete, continuous, probability mass function, PMF, P, discrete uniform distribution, sampling, finite support

12.3 Generating uniform samples with Random

This section explains what C# Random.Next(max) samples, why its pseudorandom output is unsuitable for security, and why randomness needs a higher-level generic distribution abstraction.

Key terms: Random, Next(max), discrete uniform distribution, support, pseudorandom, deterministic algorithm, RandomNumberGenerator, crypto-strength pseudorandom number generator, one-use tool, probability distribution

12.4 IDistribution<T> and IDiscreteDistribution<T>

This section defines generic interfaces for sampling probability distributions and representing discrete distributions with integer weights.

Key terms: IDistribution<T>, Sample(), support, IDiscreteDistribution<T>, Support(), Weight(T), weight, non-normalized integer weights, PMF, rounding errors

12.5 Flipping an unfair coin with Bernoulli

This section implements Bernoulli coin-flip distributions by combining singleton and discrete-uniform distributions, while reducing outcome weights to lowest terms with the GCD.

Key terms: Bernoulli distribution, tails, heads, coin flip, weight, total weight, discrete uniform distribution, singleton distribution, double-headed coin, double-tailed coin

12.6 Improving the ecosystem with extension methods

This section adds extension methods that expose distributions as infinite sample sequences and quickly visualize discrete distributions.

Key terms: Samples(), IDistribution<T>, unbounded sequence of samples, extension methods, fluent interface, discrete distributions, weights, histogram, Bernoulli

12.7 Representing unfair die rolls by adding a projection

This section implements projected discrete distributions so an underlying distribution can be mapped to arbitrary categories and combined into an unfair categorical distribution.

Key terms: Projected, projection, underlying discrete distribution, Sample(), Support(), Weight(T), group, total weight, Bernoulli distribution, categorical distribution

12.8 Categorical algorithm 1: Make a big list

This section presents a quick categorical-distribution algorithm that samples from a weight-expanded list and explains why its O(W) setup cost is impractical.

Key terms: categorical distribution, support, weight, uniform discrete distribution, projection, Sample(), quick-and-dirty projection algorithm, O(1), O(W), total weight

12.9 Categorical algorithm 2: Climb a ladder

This section presents ladder climbing as a categorical-sampling algorithm, compares its costs with the prior big-list approach, and sketches a binary-search optimization.

Key terms: total weight, support, ladder, rungs, uniform distribution, cumulative weights, projection, categorical distribution, Algorithm 1, Algorithm 2

12.10 Categorical algorithm 3: Rejecting rejection sampling

This section introduces categorical rejection sampling as dart throwing on a weighted histogram, then explains why its unbounded retries make it a poor categorical sampler.

Key terms: rejection sampling, categories, weights, largest weight, dartboard, X, non-X portions, proportional, generated samples, Max()

12.11 Categorical algorithm 4: The alias algorithm

This section explains how the alias algorithm preprocesses integer-weighted categorical distributions into row distributions that sample in constant time without rejection.

Key terms: alias algorithm, categorical distribution, weights, short row, long row, just-right row, singleton, Bernoulli, projection, uniformly distributed

12.12 Filtering out a category

This section shows how to filter a categorical distribution by excluding outcomes and renormalizing the remaining sampling probabilities, setting up Bayesian reasoning.

Key terms: categorical distribution, predicate, support, filtered distribution, unfair die, green, weight, re-roll, histogram, Bayes’ theorem

12.13 Summary

This summary recaps discrete probability distributions, their programming abstractions and sampling algorithms, and filtering categories for later probabilistic reasoning.

Where this is used later

Concepts explained in this chapter

← Chapter 11: Second abstract nonsense interlude: Monads Chapter 13: Conditional probability with Bayes’ theorem →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.