rheon.polars.eval.merge_sums¶
- rheon.polars.eval.merge_sums(first: DataFrame, *rest: DataFrame) DataFrame[source]¶
Add the sums of disjoint sets of rows, as if their rows were one chunk.
Use it to add the
sums()of the chunks of a stream, in any number and any split, before reading the scores withfrom_sums().Parameters
first,*restFrames
sums()returned, over disjoint sets of rows, each with the same key columns. Merging one frame returns its sums again, exactly, sorted by the keys.
Returns
A
DataFramewith the same columns: the keys, thenSUM_FIELDS, one row per key, sorted by the keys. A key present in one frame and not another is carried through as it is.Raises
TypeErrorA frame is not a
DataFrame.ValueErrorA frame lacks a column of
SUM_FIELDS, such as the scoresmetrics()orfrom_sums()returns; or the frames’ key columns differ, in their names or their dtypes.
Update equation
The means are pooled by weight, and each moment about the mean picks up the parallel-axis term for the distance between its frame’s mean and the pooled one. With
grunning over the frames, andmeanandm2standing for each ofmean_yandm2_y, andmean_predandm2_pred, in turn:w = sum(w_g) mean = sum(w_g * mean_g) / w m2 = sum(m2_g + w_g * (mean_g - mean)**2) cov = sum(cov_g + w_g * (mean_y_g - mean_y) * (mean_pred_g - mean_pred))
n,sse,hitsandsignedare summed. A frame whose rows all weigh 0 (wis 0, its means 0/0) adds itsnand nothing else. Where every frame that weighs has one mean, the pooled mean is that value, exactly, so a constant target or prediction keeps a second moment of exactly 0, and itsr2oricthe null offrom_sums(), through any number of merges.Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) out = rp.ModelBank([spec]).fit_predict(df) chunks = [rp.eval.sums(out.slice(i, 100), spec) for i in range(0, 400, 100)] merged = rp.eval.merge_sums(*chunks) # four chunks of 100 rows, added whole = rp.eval.sums(out, spec) # the 400 rows as one chunk print(f"m2_y {merged['m2_y'][0]:.6f} merged, {whole['m2_y'][0]:.6f} whole")
m2_y 1932.454328 merged, 1932.454328 whole
See also
sums(), which makes the frames this adds.from_sums(), which reads the scores from the result.
Notes
This is the n-way form of Chan, Golub and LeVeque’s merge. Every frame enters as one term of a sum, never as a difference of running totals, so merging a thousand chunks loses about as much as merging two. Measured on 100,000 rows at a level of 1e4 standard deviations, against the exact sums in rational arithmetic, over eight random streams: 2 chunks merged within 6e-15 of the exact
m2_y,m2_predandcov, relative to each, and 1,000 chunks within 9e-15. Most of that is each frame’s mean rounded to a double at that level, before any merging. A difference of running totals loses the level’s square over the variance times the rounding, about 1e-8 there.That rounding of each frame’s mean enters the merged moments at first order, about
eps * |mean| / (spread * sqrt(N))relative, withNthe key’s rows: it does not grow with the number of frames, nor shrink as they grow larger, but it grows as a key gets shorter, to 1e-13 at a level of 1e4 over 1,000 rows, asmetrics()’ Notes measure.