onlinerake.StreamingRaker

class onlinerake.StreamingRaker(targets, calibration_strength=0.3, min_weight=0.001, max_weight=100.0, trim=None, outcomes=(), keep_weights=False)[source]

Rake a stream without storing it.

Parameters:
  • targets (Targets) – Population targets (binary shares or continuous means).

  • calibration_strength (float) – Penalty c on the margin residual totals. Larger values match the targets more closely at the cost of more variable weights. Default 0.3, which held up across the easy, extreme-target and many-margin scenarios in ms/onlinerake.pdf. Design weights are divided by their running mean and continuous features by the running root mean square of their deviations from target, so c does not depend on either’s units.

  • min_weight (float) – Lower bound on a row’s weight relative to its design weight, w / d. Default 0.001.

  • max_weight (float) – Upper bound on w / d. Default 100.

  • trim (float | None) – Cap each weight at this multiple of the mean weight assigned so far, trading some bias for lower variance as in Potter’s (1990) trimming; common choices are 3 to 6. Past weights cannot absorb the trimmed amount, so the penalty shifts it onto later rows. The cap overrides min_weight. Default None, no trimming.

  • outcomes (Sequence[str]) – Names of outcomes whose weighted means to track.

  • keep_weights (bool) – Store each assigned weight. Off by default, which keeps memory independent of stream length.

Weights are returned relative to the mean design weight so far, so they sum to about the number of rows. To express them as population counts, multiply each kept weight by N / total_weight at the end.

Examples

>>> from onlinerake import StreamingRaker, Targets
>>> raker = StreamingRaker(Targets(female=0.5), outcomes=["spend"])
>>> for female, spend in [(1, 10.0), (1, 12.0), (1, 11.0), (0, 20.0)] * 50:
...     _ = raker.partial_fit({"female": female}, outcomes={"spend": spend})
>>> round(raker.margins["female"], 2)
0.52
>>> round(raker.estimate("spend"), 1)
15.3
__init__(targets, calibration_strength=0.3, min_weight=0.001, max_weight=100.0, trim=None, outcomes=(), keep_weights=False)[source]
Parameters:
Return type:

None

Methods

__init__(targets[, calibration_strength, ...])

estimate(name)

Weighted mean of one registered outcome.

partial_fit(obs[, design_weight, outcomes])

Weight one row and fold it into the running state.

Attributes

effective_sample_size

(sum w)^2 / sum w^2; 0.0 before any row.

estimates

Weighted means of all registered outcomes.

loss

Sum of squared margin errors; NaN before any row.

margins

Weighted shares (binary) or means (continuous); NaN before any row.

n_obs

Number of rows processed.

residuals

Copy of the running totals sum_i w_i (x_i - t).

total_weight

Sum of assigned weights, in units of the mean design weight.

weights

Assigned weights in arrival order.