Search

Search papers and lessons

Weighted Quantile Sketch

Level 4 · 12 min read

The weighted quantile sketch picks the candidate split points for one feature so that each gap holds an even share of the boosting weights, using a compact summary that can be merged across machines.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Classical models
  • Learning basics
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

A tree learner that boosts needs a short list of candidate split points for each feature. Testing every distinct value is too slow on a large table, so the candidates are chosen from the feature's boosting statistics. The weighted quantile sketch chooses them so that each gap between neighbouring candidates holds an even share of the weights, where each row's weight is its second-order statistic. It works from a compact summary, so the summaries from many machines can be merged and still carry a stated error bound.

Builds on: Gradient Boosting, Decision Tree, Cumulative Distribution Function, Taylor Series · Leads to: XGBoost

Where it's used

  • XGBoost on large tables. The XGBoost paper describes a system that is "used widely by data scientists to achieve state-of-the-art results on many machine learning challenges." The sketch is how that system picks split candidates for a feature without testing every distinct value.
  • Training across many machines. Each machine builds a summary of its own rows. Two summaries can be merged into one, and the merged summary keeps the error of the worse input. The paper calls this a distributed weighted quantile sketch, and it is the reason the summary is built from merge and prune operations.
  • Checking that approximation costs little accuracy. The paper's Figure 3 compares test AUC on the Higgs 10M dataset, which has 10 million training instances, for several settings of the sketch's approximation parameter. The paper reports that the quantile strategy "can get the same accuracy as exact greedy given reasonable approximation level."

How it works

Take one feature, x, and five rows. Each row also has a weight h, which in boosting is the second derivative of the loss at the current prediction. For now, read h as "how much this row should count."

Row value x12345
Weight h11411

The goal is a short list of cut points, such as 5, that splits the weight into roughly equal pieces. A plain percentile would count rows. This one counts weight.

  1. Add up the weights. 1 + 1 + 4 + 1 + 1 = 8. Check: the total is 8, not 5, because the row at x = 3 counts four times.
  2. Turn the weights into a running share. Divide each running total by 8. After x = 1 the running total is 1, a share of 1/8 = 0.125. After x = 2 it is 2, a share of 0.25. After x = 3 it is 6, a share of 0.75. After x = 4 it is 7, a share of 0.875. After x = 5 it is 8, a share of 1. Check: the last share is 1, as it must be.
  3. Set targets every 0.25 of the share. The targets are 0.25, 0.5, 0.75 and 1. For each target, take the first value whose running share reaches it. The target 0.25 is reached at x = 2. The targets 0.5 and 0.75 are both reached at x = 3, because its running share is 0.75. The target 1 is reached at x = 5. Check: three targets, with one value covering two of them.
  4. Add the minimum. The cut points are the minimum, 1, plus the values from step 3: 5. Check: the largest value, 5, is always a cut point.
  5. Compare with plain quantiles. Without weights the running shares are 0.2, 0.4, 0.6, 0.8 and 1, so the same targets would be reached at x = 2, 3, 4 and 5. The cut at 4 disappears in the weighted version because the heavy row at 3 already covers the 0.75 target. Check: the weighted list has one fewer cut in the region where the weight is concentrated.
flowchart LR
  A["Sort the rows by feature value"] --> B["Running share of the total weight"]
  B --> C["Targets every epsilon: 0.25, 0.5, 0.75, 1"]
  C --> D["First value that reaches each target"]
  D --> E["Candidate cut points"]

The paper says there are roughly 1/epsilon candidates, where epsilon is the approximation factor. With epsilon = 0.25 that is about four, which matches the four targets above.

The math

Write the rows as pairs (xi,hi)(x_i, h_i) with hi≥0h_i \ge 0, and let W=∑ihiW = \sum_i h_i be the total weight. The rank function r(z)r(z) is the share of the weight on values strictly below z. Step 2 of the walkthrough uses a close cousin of it, the running share, defined below.

r(z)=1W∑i: xi<zhir(z) = \frac{1}{W}\sum_{i:\, x_i \lt z} h_i

Definition: Weighted quantile sketch
Given weighted values (xi,hi)(x_i, h_i) of one feature and an approximation factor ϵ\epsilon, find candidate split points s1<s2<⋯<sls_1 \lt s_2 \lt \dots \lt s_l with s1=min⁡ixis_1 = \min_i x_i and sl=max⁡ixis_l = \max_i x_i, such that consecutive candidates satisfy ∣r(sj)−r(sj+1)∣<ϵ|r(s_j) - r(s_{j+1})| \lt \epsilon. The paper's wording is "roughly 1/ϵ1/\epsilon candidate points."

Step 1 in symbols

The total is W=1+1+4+1+1=8W = 1 + 1 + 4 + 1 + 1 = 8.

Step 2 in symbols

The running share at a value z counts the weight at values less than or equal to z:

R(z)=1W∑i: xi≤zhiR(z) = \frac{1}{W}\sum_{i:\, x_i \le z} h_i

For the example, R(2)=2/8=0.25R(2) = 2/8 = 0.25 and R(3)=6/8=0.75R(3) = 6/8 = 0.75. The weight sitting exactly at z is what separates R from r: r(z)=R(z)−1W∑i: xi=zhir(z) = R(z) - \frac{1}{W}\sum_{i:\, x_i = z} h_i. At z = 3 the weight at 3 is 4, so r(3)=0.75−0.5=0.25r(3) = 0.75 - 0.5 = 0.25, which matches the rank above.

Step 3 in symbols

For a target t, the candidate is the smallest value s with R(s)≥tR(s) \ge t. With ϵ=0.25\epsilon = 0.25 the targets are tm=mϵt_m = m\epsilon for m=1,2,3,4m = 1, 2, 3, 4. Here R(3)=0.75R(3) = 0.75 already satisfies both t=0.5t = 0.5 and t=0.75t = 0.75, so one value serves two targets.

Step 4 in symbols

The candidate set is {min⁡ixi}∪{the values chosen in step 3}\{\min_i x_i\} \cup \{\text{the values chosen in step 3}\}, which gives {1,2,3,5}\{1, 2, 3, 5\}.

Why the weights are the second derivatives

This is the step that connects the sketch to boosting. Suppose the current prediction for row i is y^i\hat{y}_i, and a new tree adds a value ff to it. A second-order Taylor expansion of the loss gives

l(yi,y^i+f)≈l(yi,y^i)+gif+12hif2,l(y_i, \hat{y}_i + f) \approx l(y_i, \hat{y}_i) + g_i f + \tfrac{1}{2} h_i f^2,

where gig_i is the first derivative of the loss and hih_i is the second derivative, both taken with respect to the prediction. Summed over rows, the split score depends on the data only through the gig_i and hih_i.

Complete the square in f:

gif+12hif2=12hi(f+gihi)2−gi22hig_i f + \tfrac{1}{2} h_i f^2 = \tfrac{1}{2} h_i \left(f + \frac{g_i}{h_i}\right)^2 - \frac{g_i^2}{2 h_i}

The last term does not depend on f. So choosing the tree is the same as fitting a squared loss with label −gi/hi-g_i / h_i and weight hih_i. A split that is good for that weighted squared loss is good for boosting, and the candidate cuts should therefore spread the weight hih_i evenly, not the rows. This is the reason for the weights in the sketch.

Two standard losses show what h looks like:

  • For squared error, l=12(y−y^)2l = \tfrac{1}{2}(y - \hat{y})^2 gives g=y^−yg = \hat{y} - y and h=1h = 1. Every row has weight 1, so the sketch is an ordinary quantile.
  • For logistic loss, with predicted probability pp, g=p−yg = p - y and h=p(1−p)h = p(1 - p). A row at p=0.5p = 0.5 has h=0.25h = 0.25. A row at p=0.99p = 0.99 has h=0.0099h = 0.0099, so rows the model is sure about count for little.

The summary and its error

The sketch stores a summary that supports two operations, proved in the paper's appendix:

  • Merge. Two summaries with errors ϵ1\epsilon_1 and ϵ2\epsilon_2 combine into one with error max⁡(ϵ1,ϵ2)\max(\epsilon_1, \epsilon_2).
  • Prune. A summary with error ϵ\epsilon is cut down to b+1b + 1 values, and its error becomes ϵ+1/b\epsilon + 1/b.

Merging lets each machine work on its own rows. Pruning keeps the summary small.

Worked examples

Example
1. Three rows, hand-computable

Rows (x, h): (1, 1), (2, 2), (3, 1). Use ϵ=0.5\epsilon = 0.5.

  1. Total weight: 1+2+1=41 + 2 + 1 = 4.
  2. Running shares: after x = 1, 1/4=0.251/4 = 0.25. After x = 2, 3/4=0.753/4 = 0.75. After x = 3, 4/4=14/4 = 1.
  3. Targets: 0.5 and 1.0.
  4. Target 0.5 is first reached at x = 2, whose share is 0.75. Target 1.0 is reached at x = 3.
  5. Add the minimum, x = 1. The candidates are {1,2,3}\{1, 2, 3\}.

Interpretation: the row at x = 2 carries half the weight, so it is the cut that serves the middle target.

Example
2. Equal weights, the standard case

Rows x = 1 to 8, each with h = 1, as with squared error. Use ϵ=0.25\epsilon = 0.25.

  1. Total weight: 8.
  2. The running share after x is x/8x/8, so the shares are 0.125, 0.25, 0.375, ..., 1.
  3. Targets: 0.25, 0.5, 0.75 and 1.0.
  4. The targets are first reached at x = 2, 4, 6 and 8.
  5. Add the minimum, x = 1. The candidates are {1,2,4,6,8}\{1, 2, 4, 6, 8\}: four buckets of equal weight, which is 1/ϵ1/\epsilon.

Interpretation: with equal weights the sketch is an ordinary quantile sketch, and the paper says that is the case that existing algorithms already solved.

Example
3. A realistic column, ending with a try-it

A training column has 1,000,000 rows with equal weights, and the approximation factor is ϵ=0.01\epsilon = 0.01.

  1. The number of targets is 1/ϵ=1001/\epsilon = 100.
  2. Each bucket holds 1/1001/100 of the total weight, which is 1,000,000/100=10,0001{,}000{,}000 / 100 = 10{,}000 rows.
  3. The candidates fall about every 10,000 rows in sorted order.
  4. Now change the loss to logistic. Each row at p=0.5p = 0.5 has weight 0.25, about 25 times a row at p=0.99p = 0.99 (weight 0.0099). Each unsure row counts for about 25 confident rows, so the candidates crowd into the region of unsure rows.

Try it: a column has 250,000 rows with equal weights, and ϵ=0.02\epsilon = 0.02. How many targets are there, and how many rows fall in each bucket? Answer: 1/0.02=501/0.02 = 50 targets, and 250,000/50=5,000250{,}000 / 50 = 5{,}000 rows per bucket.

Common mistakes and tradeoffs

It is tempting to think the sketch is just a percentile of the feature. In fact each row counts with its weight hih_i, which in boosting is the second derivative of the loss. A row the model is confident about counts for little, so the cut points move toward the rows the model is unsure about. Plain percentiles would ignore that.

It is tempting to think hih_i is a sample weight the user supplies. In fact the boosting loop computes it from the loss and the current predictions, and it changes every round.

It is tempting to think the candidates are exact. In fact they are approximate, and ϵ\epsilon sets how far off they can be. More candidates cost time and memory, so ϵ\epsilon is a tradeoff between accuracy and speed.

Use the sketch when the data is large and the splits must be found approximately, for example when training is distributed. Do not use it on a small table: the paper also supports exact greedy search on a single machine, which needs no candidate list. A different alternative is approximate histograms of gradient statistics, which the paper cites as a route used by earlier distributed systems. Both are alternatives to the sketch, not corrections to it.

History

Quantile summaries were studied in the database community. Greenwald and Khanna's 2001 paper, "Space-efficient online computation of quantile summaries," gave a summary that answers quantile queries with a stated accuracy, and later work extended it. Those methods assumed every row counts the same. Boosting needs rows to count by hih_i. The XGBoost paper, published in 2016, says that to its authors' knowledge the weighted sketch it introduced was the first method for that problem. Its merge and prune operations let the sketch run on many machines, which is part of why XGBoost spread quickly.

Recap

  • Gradient boosting fits each new tree to the first and second derivatives of the loss, called gig_i and hih_i.
  • A tree needs candidate cut points for each feature before it can score a split.
  • Ordinary quantiles spread the candidates evenly by row count.
  • The Taylor expansion of the loss shows that the split score is a weighted squared loss with weights hih_i, so the candidates should spread the weight hih_i.
  • The sketch keeps a summary that merges across machines and prunes to a fixed size, with a stated error.

Read next: the XGBoost paper (Chen and Guestrin, 2016) for the full system, and Histogram for the binning alternative.

Check yourself

QuestionWhat is hih_i in the weighted quantile sketch?Show answer
It is the second derivative of the loss with respect to the prediction for row i, evaluated at the current prediction. Boosting computes it each round, and the sketch uses it as the weight of row i.
QuestionRows x = 1, 2, 3, 4 have weights h = 2, 1, 1, 4. Find the candidate cut points for ϵ=0.5\epsilon = 0.5.Show answer
The total weight is 8. The running shares are 0.25 at x = 1, 0.375 at x = 2, 0.5 at x = 3 and 1 at x = 4. Target 0.5 is first reached at x = 3, and target 1 at x = 4. Adding the minimum gives {1,3,4}\{1, 3, 4\}, three cut points.
QuestionWhy does the sketch weight rows by hih_i instead of counting them?Show answer
Taylor expansion of the loss shows that choosing a tree is the same as fitting a squared loss with weights hih_i. A split that is good for that weighted loss needs candidates that spread hih_i evenly, so counting rows would put the candidates in the wrong places.
QuestionA model gives p=0.99p = 0.99 for most rows of a feature and p=0.5p = 0.5 for a few others, under logistic loss. Where do the candidate cut points land, and why?Show answer
Rows at p=0.5p = 0.5 have h=0.25h = 0.25, about 25 times the h=0.0099h = 0.0099 of rows at p=0.99p = 0.99. Each uncertain row therefore counts for about 25 confident rows, so the cut points cluster around the values of the uncertain rows.

Sources

  • Chen and Guestrin, "XGBoost: A Scalable Tree Boosting System," arXiv 1603.02754, Section 3.3 and Appendix A.
  • The same paper, ACM DOI 10.1145/2939672.2939785.
  • Wikipedia, "Quantile."