liveBook · Manning

Binomial distribution

As explained in Fabulous Adventures in Data Structures and Algorithms

A binomial distribution gives the probabilities of obtaining each possible number of successes in a fixed number of independent trials with the same success probability.

A binomial distribution models the number of successes in a fixed number of trials when each trial has the same success probability. For successes in `trials` attempts, it multiplies the probability of that success/failure pattern by the number of possible permutations. section 16.5

Ten coin flips

A coin flipped 10 times produces a distribution over possible head counts. Observing 9 heads updates a prior belief about the coin’s fairness: fairness near 50% becomes much less plausible, while higher fairness becomes more plausible. section 16.5

Listing 16.9 The binomial distribution
public sealed class Binomial : IWeightedDistribution<int>
{
    private int trials;
    private double prob;
    private IDistribution<bool> flip;

    public static Binomial Distribution(int trials, double prob) =>
        new(trials, prob);

    private Binomial(int trials, double prob)
    {
        this.trials = trials;
        this.prob = prob;
        this.flip = new Flip<bool>(true, false, prob); #1
    }

    public double Weight(int successes) =>
        Pow(prob, successes) * 
        Pow(1.0 - prob, trials - successes) *
        (double)trials.Choose(successes); #2

    public int Sample() => flip.Samples().Take(trials).Count(x => x); #3
}

The C# implementation weights each possible success count with powers for successes and failures, multiplies by the permutation count, and samples by counting successful flips. section 16.5

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines the probability calculation and implements sampling, while using a linear algorithm appropriate for small trial counts.
Examples
Uses ten coin flips, nine observed heads, fairness priors, posterior histograms, and a C# distribution implementation.
What it assumes
Uses probability of success and failure, permutations, prior beliefs, and posterior distributions without developing those ideas from zero.
What you can do after
Apply it via C# to weight success counts, sample binomial outcomes, and update fairness beliefs from observed evidence.
How it connects
Connects binomial likelihoods with prior distributions, posterior distributions, Metropolis sampling, and normal distributions.

Also explained in

Compare how 6 books explain binomial distribution →

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.