
Optimal Binning for Numerical Variables using Sketch-based Algorithm
Source:R/obn_sketch.R
ob_numerical_sketch.RdImplements optimal binning using the **KLL Sketch** (Karnin, Lang, Liberty, 2016), a probabilistic data structure for quantile approximation in data streams. This is the only method in the package that uses a fundamentally different algorithmic approach (streaming algorithms) compared to batch processing methods (MOB, MDLP, etc.).
The sketch-based approach enables:
Sublinear space complexity: O(k log N) vs O(N) for batch methods
Single-pass processing: Suitable for streaming data
Provable approximation guarantees: Quantile error \(\epsilon \approx O(1/k)\)
The method combines KLL Sketch for candidate generation (every distinct value for N <= 50) with an exact Dynamic Programming selection of the cutpoints, followed by monotonicity enforcement via the Pool Adjacent Violators Algorithm (PAVA).
Usage
ob_numerical_sketch(
feature,
target,
min_bins = 3,
max_bins = 5,
bin_cutoff = 0.05,
max_n_prebins = 20,
monotonic = TRUE,
convergence_threshold = 1e-06,
max_iterations = 1000,
sketch_k = 200
)Arguments
- feature
Numeric vector of feature values. Missing values (NA/NaN) are dropped silently: those rows are counted in no bin and the counts add up to the number of non-missing rows.
-Infand+Infare kept as extreme values in the first and last bin and never become cutpoints. A feature whose values are all missing is an error. The sketch summarises the finite values; with fewer than two distinct finite values a single bin is returned.- target
Integer vector of binary target values (must contain only 0 and 1). Must have the same length as
feature. Missing values are not permitted.- min_bins
Minimum number of bins (default: 3). Must be at least 2.
- max_bins
Maximum number of bins (default: 5). Must be >=
min_bins. It is a ceiling, not a target: the number of bins is chosen by BIC in[min_bins, max_bins](see Details, Phase 3).- bin_cutoff
Minimum fraction of total observations per bin (default: 0.05). Must be in (0, 1). Bins with fewer observations will be merged with neighbors.
- max_n_prebins
Accepted for compatibility with the other binning functions (default: 20; must be in [2, 1000]). The candidate grid is a fixed set of about 40 sketch quantiles.
- monotonic
Logical flag to enforce WoE monotonicity (default: TRUE). Uses a monotone constraint inside the cutpoint optimization, then pools adjacent violators. Direction (increasing/decreasing) is the one with the larger BIC.
- convergence_threshold
Convergence threshold (default: 1e-6). Accepted for compatibility: the selected cutpoints never exceed
max_bins - 1, so no iterative bin-count reduction is needed.- max_iterations
Maximum iterations for bin optimization (default: 1000). Prevents infinite loops in the optimization process.
- sketch_k
Integer parameter controlling sketch accuracy (default: 200). Larger values improve quantile precision but increase memory usage. Approximation error: observed rank error about \(1/k\) of the sample size (200 → about 0.5%). Valid range: [10, 1000]. Typical values: 50 (fast), 200 (balanced), 500 (precise).
Value
A list of class c("OptimalBinningSketch", "OptimalBinning", "list") containing:
- id
Numeric vector of bin identifiers (1-based indexing).
- bin
Character vector of right-closed bin labels
"(lower;upper]"; the first bin is labelled from-Infand the last one up to+Inf.- bin_lower
Numeric vector of lower bin boundaries: the observed minimum (included) for the first bin, the previous cutpoint (excluded) otherwise.
- bin_upper
Numeric vector of upper bin boundaries (included); the observed maximum for the last bin.
- woe
Numeric vector of Weight of Evidence values. Monotonic if
monotonic = TRUE.- iv
Numeric vector of Information Value contributions per bin.
- count
Integer vector of total observations per bin.
- count_pos
Integer vector of positive class (target = 1) counts per bin.
- count_neg
Integer vector of negative class (target = 0) counts per bin.
- total_iv
Total Information Value (sum of bin IVs).
- cutpoints
Numeric vector of bin split points (length = number of bins - 1). These are the internal boundaries between bins.
- converged
Logical flag indicating whether optimization converged.
- monotonic
Logical. TRUE when the final WoE of the regular bins is monotone (in the reported direction when one is fixed), checked on the reported WoE and on the reference WoE
log((count_pos + 0.5) / (count_neg + 0.5)). When monotonicity was requested and this is FALSE (the merges stop atmin_bins), a warning is raised. Up to version 1.14.1 nothing signalled it.- iterations
Integer number of optimization iterations performed.
Details
Algorithm Overview
The sketch-based binning algorithm executes in four phases:
Phase 1: KLL Sketch Construction
The KLL Sketch maintains a compressed, multi-level representation of the data distribution:
$$\text{Sketch} = \{\text{Compactor}_0, \text{Compactor}_1, \ldots, \text{Compactor}_L\}$$
where each \(\text{Compactor}_\ell\) stores items with weight \(2^\ell\). When a
compactor exceeds capacity \(k\) (controlled by sketch_k), it is compacted.
Theoretical Guarantees (Karnin et al., 2016):
For a quantile \(q\) with estimated value \(\hat{q}\):
$$|\text{rank}(\hat{q}) - q \cdot N| \le \epsilon \cdot N$$
where \(\epsilon \approx O(1/k)\) and space complexity is \(O(k \log(N/k))\).
This implementation compacts deterministically (the kept element of each pair alternates between levels) instead of using KLL's random coin, so results are reproducible without a seed. Its worst-case rank error is \(O(\log(N/k)/k)\); the observed error is about \(1/k\). The quantiles only propose candidate cutpoints: every bin statistic is then computed exactly from the data.
Phase 2: Candidate Extraction
Approximately 40 quantiles are extracted from the sketch using a non-uniform grid with higher resolution in distribution tails.
Phase 3: Optimal Cutpoint Selection
An exact Dynamic Programming over the intervals between consecutive candidates
(about 40 sketch quantiles; for N <= 50, the boundaries between consecutive distinct
values) returns the partition with the largest total IV for each number of bins
\(K\) between min_bins and max_bins. Among them, the one with the
largest Schwarz (1978) BIC of the binned event-rate model,
$$\mathrm{BIC} = \sum_j \left[ n^+_j \log \hat p_j + n^-_j \log (1 - \hat p_j) \right] - \frac{K - 1}{2} \log N,$$
is kept (ties: fewer bins): a bin is added only when it raises the log-likelihood
by more than \(\log(N) / 2\), so max_bins is a ceiling, not a target.
Picking the \(K\) with the largest training IV over-split small samples and rare
events. With monotonic = TRUE the optimization is restricted to partitions
with a monotone (smoothed) WoE, in both directions, and the BIC decides between
them. The objective is
the IV with normalized Jeffreys smoothing (0.5 per bin and class), and every bin must
hold at least ceiling(bin_cutoff * N) observations inside the optimization.
(Up to version 1.14.1 the larger datasets used the max_bins - 1 candidates
with the largest single-cut IV, which clustered next to each other.) When no number
of bins in that range can give every bin that many observations, min_bins bins
as large as possible are used.
Phase 4: Bin Refinement
Bins are refined through frequency constraint enforcement, monotonicity enforcement (if requested). Bins that hold no observation are always merged.
Computational Complexity
Time: \(O(N \log k + N \times C + k^2 \times I)\)
Space: \(O(k \log N)\) for large N
When to Use Sketch-based Binning
Use: Large datasets (N > 10^6) with memory constraints or streaming data
Avoid: Small datasets (N < 1000) where approximation error may dominate
References
Karnin, Z., Lang, K., & Liberty, E. (2016). "Optimal Quantile Approximation in Streams". Proceedings of the 57th Annual IEEE Symposium on Foundations of Computer Science (FOCS), 71-78. doi:10.1109/FOCS.2016.20
Greenwald, M., & Khanna, S. (2001). "Space-efficient online computation of quantile summaries". ACM SIGMOD Record, 30(2), 58-66. doi:10.1145/376284.375670
Barlow, R. E., Bartholomew, D. J., Bremner, J. M., & Brunk, H. D. (1972). Statistical Inference Under Order Restrictions. Wiley.
Siddiqi, N. (2006). Credit Risk Scorecards: Developing and Implementing Intelligent Credit Scoring. Wiley. doi:10.1002/9781119201731
Examples
# \donttest{
# Example 1: Basic usage with simulated data
set.seed(123)
feature <- rnorm(500, mean = 100, sd = 20)
target <- rbinom(500, 1, prob = plogis((feature - 100) / 20))
result <- ob_numerical_sketch(
feature = feature,
target = target,
min_bins = 3,
max_bins = 5
)
# Display results
print(data.frame(
Bin = result$id,
Count = result$count,
WoE = round(result$woe, 4),
IV = round(result$iv, 4)
))
#> Bin Count WoE IV
#> 1 1 102 -1.6296 0.4423
#> 2 2 97 -0.6650 0.0825
#> 3 3 225 0.3892 0.0676
#> 4 4 76 1.7973 0.3985
# Example 2: Comparing different sketch_k values
set.seed(456)
x <- rnorm(1000, 50, 15)
y <- rbinom(1000, 1, prob = 0.3)
result_k50 <- ob_numerical_sketch(x, y, sketch_k = 50)
result_k200 <- ob_numerical_sketch(x, y, sketch_k = 200)
cat("K=50 IV:", sum(result_k50$iv), "\n")
#> K=50 IV: 0.00906071
cat("K=200 IV:", sum(result_k200$iv), "\n")
#> K=200 IV: 0.01046609
# }