liveBook · Manning

Total weight

As explained in Fabulous Adventures in Data Structures and Algorithms

Total weight is the sum of the weights assigned to all outcomes in a weighted distribution.

Total weight is the sum of the weights assigned to every outcome in a weighted distribution. It acts as the integer distribution’s scale before probabilities are normalized. section 12.9

For categorical sampling, let the total weight be W. Sampling a uniform integer from 0 through W−1 turns each outcome’s weight into a proportional interval. section 12.9

Total weight supports several operations on discrete distributions:

  • Build cumulative rung heights for weighted categorical sampling; the final height equals W and cannot itself be sampled. section 12.9
  • Scale prior and conditional weights when constructing a joint distribution, using an LCM of conditional total weights to preserve integers. section 13.2
  • Exclude zero-total distributions from the LCM calculation so empty distributions remain empty under composition. section 14.2

Uneven categorical weights

Weights 101, 99, 51, and 49 have total weight 300. Their cumulative heights are 101, 200, 251, and 300, so uniform samples from 0 through 299 fall into intervals proportional to those weights. section 12.9

Listing 13.2 Computing the LCM from the GCD
public static class Extensions
{
    [...]
    public static int LCM(this int x, int y) =>
        x * y / x.GCD(y);

    public static int LCM(this IEnumerable<int> numbers) =>
        numbers.Aggregate(1, LCM);

    public static int TotalWeight<T>(
        this IDiscreteDistribution<T> distribution) =>
        distribution.Support().Select(s => distribution.Weight(s)).Sum();
}

The C# helper computes total weight by summing the weights of all supported outcomes. section 13.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains summation, sampling intervals, LCM scaling, and zero-weight handling, but stops short of continuous distributions.
Examples
Uses weights 101, 99, 51, and 49, cumulative ladder heights, and Cold–Sneezed joint weights.
What it assumes
Assumes weighted outcomes and discrete distributions while demonstrating the concept through categorical sampling and joint distributions.
What you can do after
Apply it via C# helpers to calculate distribution scale, sample categorical outcomes, and construct integer joint weights.
How it connects
Connects weighted distributions with categorical distributions, Bernoulli distributions, LCM, GCD, likelihood functions, and joint distributions.

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.