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





