onlinerake.BatchIPF

class onlinerake.BatchIPF(targets, max_iterations=100, tolerance=1e-06, min_weight=1e-06, max_weight=1000000.0)[source]

Classical Iterative Proportional Fitting for survey raking.

IPF (also called raking ratio estimation) iteratively adjusts weights to match marginal population totals. This is the gold standard batch reference for binary margins. Clipped online algorithms need not produce the same weights.

Parameters:
  • targets (Targets) – Target population proportions for each feature.

  • max_iterations (int) – Maximum number of full passes over features.

  • tolerance (float) – Convergence tolerance for margin differences.

  • min_weight (float) – Lower bound for weights to prevent collapse.

  • max_weight (float) – Upper bound for weights to prevent explosion.

Examples

>>> from onlinerake import BatchIPF, Targets
>>> targets = Targets(female=0.5, college=0.3)
>>> data = [
...     {"female": 1, "college": 1},
...     {"female": 1, "college": 0},
...     {"female": 0, "college": 1},
...     {"female": 0, "college": 0},
...     {"female": 1, "college": 0},
...     {"female": 0, "college": 0},
... ]
>>> ipf = BatchIPF(targets).fit(data)
>>> ipf.converged
True

This is the batch reference the streaming rakers are compared against, and the contrast is the point: IPF sees the whole sample at once and lands on the targets, where OnlineRakingSGD approaches them.

>>> {name: round(value, 6) for name, value in ipf.margins.items()}
{'college': 0.3, 'female': 0.5}
>>> ipf.weights.round(4)
array([0.9 , 1.05, 0.9 , 1.05, 1.05, 1.05])
__init__(targets, max_iterations=100, tolerance=1e-06, min_weight=1e-06, max_weight=1000000.0)[source]
Parameters:
Return type:

None

Methods

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

fit(observations)

Fit IPF weights to the provided observations.

fit_incremental(new_observations)

Incrementally add observations and re-run IPF.

Attributes

converged

Return True if IPF has converged.

effective_sample_size

Return the effective sample size (ESS).

loss

Get current squared-error loss.

loss_history

Return loss at each iteration.

margins

Get current weighted margins.

n_iterations

Return number of iterations performed.

raw_margins

Get unweighted (raw) margins.

weights

Get copy of current weight vector.