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
