Bayesian Smoothing for Rare Categories
A category seen only twice should not get to speak as confidently as one seen ten thousand times, and Bayesian smoothing is the simple fix that pulls small-sample averages back toward a sensible prior automatically.
Prerequisites: Target Encoding for Categorical Features, Empirical Bayes
Target encoding replaces a category — say, an exchange ticker — with the average outcome for that category, like average next-day return for stocks on that exchange. That works fine for categories with thousands of observations, but a category seen only twice, both times happening to be up days, gets encoded as "100% chance of an up day" — a wildly overconfident number produced from essentially no evidence. Bayesian smoothing fixes exactly this: it pulls small-sample averages back toward a sensible baseline, and lets large-sample averages speak almost entirely for themselves.
The analogy: a new restaurant with two five-star reviews
A restaurant with two reviews, both five stars, has a raw average of 5.0 — better, on paper, than a restaurant with 4,000 reviews averaging 4.6. Nobody actually believes the two-review restaurant is better; two reviews are not enough evidence to overturn the assumption that most restaurants land somewhere in the ordinary range. A sensible rating system blends the raw average with a prior belief about typical restaurant quality, weighted by how much evidence there is — two reviews barely move the needle from the prior, four thousand reviews move it almost entirely to the observed average. That blend, formalized, is Bayesian smoothing.
The formula
For a category with observations and observed target average , and an overall prior average (typically the global target mean), the smoothed estimate is
where is a smoothing strength — think of it as "how many pseudo-observations of the prior to blend in." In plain English: treat the prior as if it were additional, imaginary observations all equal to , mix them in with the real observations, and average everything together. When , the real data dominates and . When , the prior dominates and .
Worked example 1: two ratios shown side by side
Global average target (say, 50% of days are up days across the whole universe), smoothing strength . Category A: , (both up days). Smoothed: — barely above the prior, correctly reflecting that two observations are almost no evidence. Category B: , . Smoothed: — essentially unchanged from the raw average, because 5,000 observations overwhelm 20 pseudo-observations of prior.
Worked example 2: choosing changes how cautious the encoding is
Keep category A (, , ) but vary . At : — barely smoothed, still overconfident. At : , as above. At : — almost entirely the prior, since 2 real observations are swamped by 200 pseudo-observations. Choosing is choosing how many observations you require before you start trusting a category's own average over the population-wide baseline — a direct, interpretable knob, not an arbitrary tuning constant.
The explorer above shows a Bayesian posterior blending a prior with observed data exactly the same way — drag the data count up and watch the posterior pull away from the prior and toward the observed rate, which is the same mechanism smoothing rare categories relies on.
Bayesian smoothing blends a category's own observed average with a global prior, weighted by sample size, so categories with little data default toward the population baseline instead of reporting an extreme, low-evidence estimate as if it were reliable.
What this means in practice
Any target-encoded categorical feature in a quant pipeline — sector, exchange, counterparty, instrument type — needs this smoothing, because financial categories are almost always unevenly represented: a handful of large-cap tickers dominate observation counts while many small or newly-listed names have only a few dozen data points. Skipping smoothing routinely produces target-encoded features that look powerful in training and fall apart out of sample, because the model has effectively memorized noise for rare categories.
Smoothing does not, by itself, prevent target leakage — a smoothed encoding computed using the current row's own target value still leaks that row's answer into its own feature, just less severely. Smoothing and out-of-fold or ordered computation (see Ordered Boosting and Target Statistics) solve two different problems and are normally both required together.
Related concepts
Practice in interviews
Further reading
- Micci-Barreca, A Preprocessing Scheme for High-Cardinality Categorical Attributes (2001)