polars_online.eval.merge_sums

polars_online.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 with from_sums().

Parameters

first, *rest

Frames 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 DataFrame with the same columns: the keys, then SUM_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

TypeError

A frame is not a DataFrame.

ValueError

A frame lacks a column of SUM_FIELDS, such as the scores metrics() or from_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 g running over the frames, and mean and m2 standing for each of mean_y and m2_y, and mean_pred and m2_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, hits and signed are summed. A frame whose rows all weigh 0 (w is 0, its means 0/0) adds its n and 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 its r2 or ic the null of from_sums(), through any number of merges.

Examples

This code reads df from Example data:

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
out = po.ModelBank([spec]).fit_predict(df)
chunks = [po.eval.sums(out.slice(i, 100), spec) for i in range(0, 400, 100)]
merged = po.eval.merge_sums(*chunks)                  # four chunks of 100 rows, added
whole = po.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_pred and cov, 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, with N the 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, as metrics()’ Notes measure.