BRAGA

02 Writing02-02 Notes02-02-343

Combinations (Unordered, no repeats)

Published 16 Apr 20263 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
nCr=(nr)=n!r!(n−r)!^nC_r = \binom{n}{r} = \frac{n!}{r!(n-r)!}
(1)

Deep Breakdown

What it tells me (Situation Awareness): (nr)\binom{n}{r} counts the number of ways to choose rr items from nn distinct items when order does NOT matter. This is the most fundamental combinatorial formula in statistics and ML: it appears in binomial distributions, hypothesis testing (exact tests), feature subset enumeration, and bootstrap sampling counts.

Flowchart

n distinct items,choose r, orderirrelevantStart with P(n,r):orderedarrangementsDivide by r!:remove orderingof chosen itemsC(n,r) = P(n,r) /r! = n! / (r!(n-r)!)Symmetric:C(n,r) = C(n, n-r)

Real-world use

Where it is used

Feature selection: how many ways to pick kk features from pp total? Bootstrap: how many unique samples of size nn from a dataset of NN? Binomial probability: how many ways can exactly kk successes occur in nn trials?

Problem it solves

How many unordered subsets of size rr can I form from nn distinct elements?

How it solves the problem

Starts from ordered permutations P(n,r)P(n,r) and divides by r!r! to account for the fact that the r!r! orderings of any chosen subset all represent the same unordered selection.

Example in a business

Feature selection exhaustive search: with 20 features, how many 5-feature subsets exist? (205)=15,504\binom{20}{5} = 15{,}504. Feasible to search; (5010)=10,272,278,170\binom{50}{10} = 10{,}272{,}278{,}170 is not.

The formula, piece by piece

(nr)=n!r!(n−r)!\binom{n}{r} = \frac{n!}{r!(n-r)!}

  • nn: pool size.
  • rr: subset size chosen.
  • r!r!: ways to order the chosen rr items (we want to cancel these).
  • (n−r)!(n-r)!: ways to order the unchosen items (we want to cancel these too).

The formula in words

"Choose rr from nn: nn factorial divided by rr factorial times (n−r)(n-r) factorial." (52)=5!/(2!⋅3!)=120/12=10\binom{5}{2} = 5!/(2! \cdot 3!) = 120/12 = 10

Critical Concepts

  • (n0)=1\binom{n}{0} = 1 and (nn)=1\binom{n}{n} = 1 (boundary cases relying on 0!=10! = 1).
  • Symmetry: (nr)=(nn−r)\binom{n}{r} = \binom{n}{n-r} — choosing rr to include = choosing n−rn-r to exclude.
  • Pascal's Rule: (nr)=(n−1r−1)+(n−1r)\binom{n}{r} = \binom{n-1}{r-1} + \binom{n-1}{r} — basis of Pascal's triangle.
  • Python: math.comb(n, r) (3.8+). R: choose(n, r).

Step-by-step calculation (tiny example)

Step 1: n=6, r=2 Choose 2 features from 6 to include in a simple model.

Step 2: Apply formula (62)=6!2!⋅4!=7202⋅24=72048=15\binom{6}{2} = \frac{6!}{2! \cdot 4!} = \frac{720}{2 \cdot 24} = \frac{720}{48} = 15

Step 3: Interpretation 15 distinct pairs of features to evaluate.

Code examples

Beginner example (Python)

python
import math   # Feature selection: how many 3-feature subsets from 10 features?
n_features = 10
subset_size = 3
num_subsets = math.comb(n_features, subset_size)
print(f"C({n_features}, {subset_size}) = {num_subsets:,}")   # 120

Beginner example (R)

r
n_features <- 10
subset_size <- 3
num_subsets <- choose(n_features, subset_size)
cat(sprintf("C(%d, %d) = %d\n", n_features, subset_size, num_subsets))  # 120

Professional example (Python)

python
import math
import numpy as np

def feature_selection_feasibility( total_features: int, max_subset_size: int, feasibility_threshold: int = 1_000_000 ) -> None:
    """Print feasibility table for exhaustive feature subset search. Args: total_features: Total number of available features (n). max_subset_size: Maximum subset size to evaluate (r). feasibility_threshold: Max C(n,r) considered feasible. """
    print(f"Feasibility for n={total_features} features:")
    print(f"{'r':>5} {'C(n,r)':>20} {'log10 C(n,r)':>15} {'Feasible?':>12}")
    for r_size in range(1, max_subset_size + 1):
        c_n_r = math.comb(total_features, r_size)
    log10_val = math.log10(c_n_r) if c_n_r > 0 else 0
    feasible = c_n_r <= feasibility_threshold
    print(f"{r_size:>5} {c_n_r:>20,} {log10_val:>15.2f} {str(feasible):>12}")
    feature_selection_feasibility(total_features=20, max_subset_size=10)

Source: AIO p. 60

#probability-statistics#combinatorics#permutations-and-combinations

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close