Contents
Deep Breakdown
What it tells me (Situation Awareness): Also called the "stars and bars" formula, this counts the number of ways to choose items from distinct categories, allowing repetition, when order does not matter. Used in counting multisets, distributing identical items across buckets, and some Bayesian priors (Dirichlet-multinomial). Less common in daily DA/DS but appears in combinatorial optimization and drug trial design.
Flowchart
Real-world use
Where it is used
Distributing identical items (tokens, resources, impressions) across distinct buckets (channels, categories, segments) where each bucket can receive any non-negative integer amount.
Problem it solves
How many ways can you distribute identical items across distinct categories (each category can receive 0 or more items)?
How it solves the problem
Maps the problem to choosing positions for "stars" among positions (stars for items, bars for dividers between categories). This bijection gives the formula.
Example in a business
Distributing a $\$100$10p=10n=4\binom{13}{10} = 286$.
The formula, piece by piece
- : number of distinct categories (types).
- : total items to distribute (repetitions allowed).
- : number of dividers needed in the stars-and-bars picture.
- : total symbols (stars + bars).
The formula in words
"The number of ways to place identical items into distinct bins equals ." For : ways: (2,0,0), (0,2,0), (0,0,2), (1,1,0), (1,0,1), (0,1,1).
Critical Concepts
- Contrast with (no repeats): here repetition is allowed.
- Alternative notation: (sometimes called "multichoose").
- Stars and bars bijection: stars in positions, bars in remaining positions.
- Grows as for fixed large — can be very large.
Step-by-step calculation (tiny example)
Step 1: n=3 channels, p=3 ad slots to assign Each slot independently goes to one channel; channels can receive 0, 1, 2, or 3 slots.
Step 2: Apply formula
Step 3: Verify The 10 allocations are: (3,0,0),(0,3,0),(0,0,3),(2,1,0),(2,0,1),(0,2,1),(1,2,0),(1,0,2),(0,1,2),(1,1,1).
Code examples
Beginner example (Python)
import math # Distribute 5 identical ad impressions across 3 channels
n_channels = 3
p_impressions = 5
allocations = math.comb(n_channels + p_impressions - 1, p_impressions)
print(f"Budget allocations: {allocations}") # 21Beginner example (R)
n_channels <- 3
p_impressions <- 5
allocations <- choose(n_channels + p_impressions - 1, p_impressions)
cat("Budget allocations:", allocations, "\n") # 21Professional example (Python)
import math
import itertools
def multiset_combinations(num_types: int, selection_size: int) -> int:
"""Count unordered selections of selection_size from num_types with repetition. Uses stars-and-bars: C(num_types + selection_size - 1, selection_size). Args: num_types: n, number of distinct types/categories. selection_size: p, total items to select (with repeats allowed). Returns: Number of distinct multisets. """
return math.comb(num_types + selection_size - 1, selection_size)
def enumerate_budget_allocations(n_channels: int, p_units: int) -> list[tuple[int, ...]]:
"""Enumerate all budget allocations of p_units across n_channels."""
return [ combo for combo in itertools.combinations_with_replacement( range(n_channels), p_units ) ] # Budget allocation analysis
print("Budget distribution feasibility:")
print(f"{'Channels':>10} {'Units':>8} {'Allocations':>15}")
for n_ch in [3, 4, 5]:
for p_un in [5, 10, 20]:
alloc_count = multiset_combinations(n_ch, p_un)
print(f"{n_ch:>10} {p_un:>8} {alloc_count:>15,}")Source: domain knowledge