liveBook · Manning

Weight

As explained in Fabulous Adventures in Data Structures and Algorithms

An integer weight is a whole-number quantity representing the relative contribution of an outcome to a weighted distribution.

An integer weight expresses an outcome’s share of a discrete distribution without requiring its probability to be stored as a fraction. For an outcome in the support, divide its positive weight by the sum of all support weights to get its sampling probability. An outcome outside the support has weight zero. Weights therefore specify ratios: 1 and 3 describe the same probabilities as 2 and 6. section 12.4

In C#, IDiscreteDistribution<T> exposes Support(), Weight(T), and Sample(). A uniform distribution over integers from 0 to max − 1 gives every supported integer weight 1. This separates reasoning about a distribution from individual calls to Random.Next. section 12.4

Listing 12.1 An interface and the discrete uniform distribution
public interface IDistribution<T>
{
    T Sample();
}

public interface IDiscreteDistribution<T> : IDistribution<T>
{
    IEnumerable<T> Support();
    int Weight(T t);
}

public sealed class DiscreteUniform : IDiscreteDistribution<int>
{
    private readonly Random rnd = new();
    private readonly int max;
    public static DiscreteUniform Distribution(int max)
    {
        if (max <= 0)
            throw new InvalidOperationException();
        return new(max);
    }
    private DiscreteUniform(int max) => this.max = max;
    public int Sample() => rnd.Next(max);
    public IEnumerable<int> Support() => Enumerable.Range(0, max);
    public int Weight(int t) => 0 <= t && t < max ? 1 : 0;
}

The uniform implementation shows the key invariant directly: Weight returns 1 inside the support and 0 outside it. section 12.4

An unfair die made from a fair one

Project six equally weighted die outcomes onto colors: one green face, two red faces, and three blue faces. Grouping outcomes by color adds their weights, giving green:1, red:2, and blue:3. Dividing every weight by their greatest common divisor keeps an equivalent distribution with small integers. section 12.7

Integer weights avoid the rounding problem of probabilities stored as doubles that do not sum to exactly 1. Their trade-off appears when combining distributions: multiplying a prior weight by a conditional weight and dividing by that conditional distribution’s total can produce a fraction. To construct a joint distribution with integer weights, first scale by the least common multiple of the conditional total weights; each later division then comes out evenly. section 12.4 section 13.2

From prior weights to posterior weights

With prior weights 90 for no cold and 10 for a cold, conditional sneezing weights produce joint weights 27 for no cold and sneezing, and 86 for a cold and sneezing. Filtering to people who sneezed leaves posterior weights 27:86. These are relative strengths, not percentages. section 13.2 section 13.3

Weights also support combining distributions by merging equal outcomes and adding their weights. An empty distribution gives every value weight zero, but cannot produce a sample; joint-distribution calculations must exclude its zero total weight when computing the least common multiple. section 14.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines normalization by total weight, implements uniform and projected weights, and works through LCM scaling for integer joint weights.
Examples
Uses a uniform integer distribution, a color-projected die, and cold-and-sneezing prior, joint, and posterior weights.
What it assumes
Assumes familiarity with C# generics and collection operations; defines the distribution interface but builds LCM from an already implemented GCD.
What you can do after
Build it from scratch: implement weighted discrete distributions and preserve integer weights when projecting outcomes or constructing joint distributions.
How it connects
Connects weights to probability mass functions, support, categorical distributions, greatest common divisors, least common multiples, and Bayesian posterior distributions.

Also explained in

Compare how 4 books explain weight →

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.