Contents
Deep Breakdown
What it tells me (Situation Awareness): counts the number of ways to choose items from 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
Real-world use
Where it is used
Feature selection: how many ways to pick features from total? Bootstrap: how many unique samples of size from a dataset of ? Binomial probability: how many ways can exactly successes occur in trials?
Problem it solves
How many unordered subsets of size can I form from distinct elements?
How it solves the problem
Starts from ordered permutations and divides by to account for the fact that the 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? . Feasible to search; is not.
The formula, piece by piece
- : pool size.
- : subset size chosen.
- : ways to order the chosen items (we want to cancel these).
- : ways to order the unchosen items (we want to cancel these too).
The formula in words
"Choose from : factorial divided by factorial times factorial."
Critical Concepts
- and (boundary cases relying on ).
- Symmetry: — choosing to include = choosing to exclude.
- Pascal's Rule: — 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
Step 3: Interpretation 15 distinct pairs of features to evaluate.
Code examples
Beginner example (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:,}") # 120Beginner example (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)) # 120Professional example (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