# polars-online > Streaming / online regression, clustering and regime models for Polars. A Rust core, > reachable three ways with identical numerics: a chunk-fed `ModelBank` (its `fit` > chunks a `LazyFrame`), a `LazyFrame` query that streams, and the `polars-online` > command line over files. Memory grows with the models' state and the rows a delay or > window holds, never with the rows passed, so a model fits a stream larger than RAM. Python 3.12+, `polars>=1.37.0,<3`. `pip install polars-online`. `numpy` is an optional extra, needed only by `ModelBank.gram()`, by `po.gram`, `po.corr`, `po.sim` and `po.gbdt`, and by `bank.check()`'s collinearity and leakage checks, which report `not_checked` without it. No pandas. The two Python surfaces, in full: ```python import polars as pl import polars_online as po spec = po.spec.ewridge( "ridge", targets=["ret"], features=["signal_a", "signal_b"], clock="ts", # a Datetime column, so the clock parameters are durations half_life=pl.duration(minutes=10), gap_cap=pl.duration(minutes=5), group="stock_id", # one independent state per stock ) # As a loop: feed chunks of any size; the state is yours to save bank = po.ModelBank([spec]) for chunk in chunks: out = bank.fit_predict(chunk) # a column of records per spec bank.save("bank.state") # As a query: the same numbers, streamed by polars, state written when the stream ends (pl.scan_parquet("ticks/*.parquet") .online.fit_predict([spec], save_state="bank.state") .sink_parquet("fitted.parquet")) ``` **Rules that are easy to get wrong.** - **A model is spelled the same way in both places**: the Python builder `po.spec.ewridge(...)` is `type = "ewridge"` in a spec dict or a TOML file, and so for every model. The old spelling, `ew_ridge`, is refused, naming `ewridge`. - **Predictions are out-of-sample by construction.** A row's `pred` is computed from the state as it stands *before* the model learns that row's target. Never fit a row and then predict it: the `pred` the library returns is already out-of-sample. - **Chunk invariance is a guarantee.** The same stream fed as one chunk or a thousand gives identical output, bit for bit, and a test enforces it. Only which rows carry `coef` (and `support_coef` and `se_coef` beside it) can move, by default: they sit on each group's last row not skipped in every chunk, so they follow the chunking. Under a cadence those rows do not move either: `coef_every` is a clock parameter, as `solve_every` is (`0` every row, `"5m"` a row once five minutes of the clock have passed), and `max_rows_between_coefs` writes one after that many rows. - **Decay runs on the clock, not on row count.** A `Datetime`, `Date` or `Duration` clock takes its clock parameters (`half_life`, `gap_cap`, `window_size`, ...) as durations: `pl.duration(minutes=10)`, a `timedelta` or `"10m"`. A numeric clock takes plain numbers in its own units. Either mixture is refused, naming the parameter. `gap_cap`, `session` / `session_gap` and `restart_after_step_back` say what happens across a gap, a session boundary or a clock that steps back. A step back is refused unless `restart_after_step_back` is set and the step is larger, which resets the model. One no larger is a late row, refused. - **A `clock` needs a `gap_cap`.** A spec that names a clock and no cap is refused: "gap_cap is required when clock is given". The cap is the most one step between two rows can age the state, in the clock's units, so a gap such as a weekend does not forget everything at once. - **The clock need not be a time.** Sort by a feature, clock on it with a `gap_cap` in its units, and the half-life is a bandwidth in that feature's units. The fit is then a local regression in it (LOESS with a one-sided exponential kernel), and `ew_cov` clocked the same way gives local moments. - **`weight_sum` is accumulated weight**, before the model learns this row and before its own decay — the same definition in every model. `min_weight` is in those units, so it is a weight, not a number of rows. - **A zero-weight row is legal** and means "advance the clock, learn nothing", including as the first row of a stream. - **Make a target from its row's columns as a column** before the bank, `lf.with_columns(ret=(pl.col("p") / pl.col("mid")).log())`, and name it in `targets`: a log ratio sits about zero, where `hit_rate` takes its sign. `po.target` only names a target's output fields. - **Every spec adds one column**, named after the spec, each row a record of named fields. `weight_sum` is in every one. The linear models add `pred_`, `resid_` and `coef`, and diagnostics add theirs when switched on. [OUTPUTS.md](https://github.com/hgilde/polars-online/blob/main/docs/OUTPUTS.md) lists every model's fields, and `frame.online.unnest(specs)` flattens the column into plain columns. **Every surface streams.** There is no in-memory-only form, and no expression namespace: Polars hands a stateful user expression its whole column, so it cannot stream. The three surfaces, and how each loads and saves a state: | surface | run it | load a state | save a state | |---|---|---|---| | `ModelBank`, fed chunks or handed a `LazyFrame` | `bank.fit_predict(chunk)` | `po.ModelBank.load(path)` | `bank.save(path)` | | a query | `lf.online.fit_predict(...)` | `load_state=` | `save_state=` | | the `polars-online` command line | `polars-online --config bank.toml` | `--load-state`, or `load_state` in the TOML | `--save-state`, or `save_state` in the TOML | A state file is versioned msgpack, written atomically, and loads on macOS, Linux and Windows alike. ## Start here - [Every feature](https://github.com/hgilde/polars-online/blob/main/docs/FEATURES.md): each model, function, method, shared parameter, flag and environment variable, a line each, linked - [README](https://github.com/hgilde/polars-online#readme): the landing page -- what the library is, install, [a first fit](https://github.com/hgilde/polars-online#a-first-fit), and [a map](https://github.com/hgilde/polars-online#where-to-go-next) from each task to its pages - [Four use cases](https://github.com/hgilde/polars-online/blob/main/docs/HIGHLIGHTS.md): model selection, stability, factors, asynchronous streams - [What every spec takes](https://github.com/hgilde/polars-online/blob/main/docs/MODELS.md#what-every-spec-takes): the parameters every model shares -- columns, clock and units, the clock's policy, a hard window, the warm-up gates -- and what the bank refuses - [Models](https://github.com/hgilde/polars-online/blob/main/docs/MODELS.md): all 22, each with its recursion, its builder and its Rust source. [Choose a model](https://github.com/hgilde/polars-online/blob/main/docs/how-to/choose-a-model.md) says which for what - [Every document](https://github.com/hgilde/polars-online/blob/main/docs/README.md): every guide, grouped by kind and task ## API reference - [Spec builders](https://hgilde.github.io/polars-online/spec.html): every keyword and its default - [ModelBank, fit_predict, predict](https://hgilde.github.io/polars-online/polars_online.html): the objects and functions the package exports - [Frame namespaces](https://hgilde.github.io/polars-online/namespaces.html): `lf.online.*`, `df.online.*` - [Evaluation](https://hgilde.github.io/polars-online/eval.html) - [Gram accessors](https://hgilde.github.io/polars-online/gram.html) - [Correlation toolkit](https://hgilde.github.io/polars-online/corr.html) - [Stream transforms](https://hgilde.github.io/polars-online/stream.html) - [Window operators](https://hgilde.github.io/polars-online/ops.html): `po.ewm_mean`, `po.ewm_sum`, `po.ewm_rate`, `po.ewm_quantile` (each looking ahead under `reverse=True`), `po.ewm_var`, `po.ewm_std` -- Polars expressions over a stream's clock -- and `po.increment`, a column's step from its last value, all run by `lf.online.with_windows(*exprs, **named, clock=...)` in one pass. A look-ahead one is also a spec's target, learned once its window has passed: `fit_predict` refuses an `embargo` shorter than its `window_size` - [Training inputs for XGBoost and LightGBM](https://hgilde.github.io/polars-online/gbdt.html): `po.gbdt.xgboost_matrix`, `po.gbdt.lightgbm_dataset`, binned from Arrow IPC, Parquet or a query one chunk at a time, for data larger than memory (unstable) - [Simulation](https://hgilde.github.io/polars-online/sim.html) ## Tutorials - [Fit over parquet files in a query](https://github.com/hgilde/polars-online/blob/main/docs/tutorials/stream-from-parquet.md) - [Follow a local fit through a day](https://github.com/hgilde/polars-online/blob/main/docs/tutorials/local-fit-with-a-clock.md) - [Fit, save and serve](https://github.com/hgilde/polars-online/blob/main/docs/tutorials/fit-save-and-serve.md) ## How-tos [Every document](https://github.com/hgilde/polars-online/blob/main/docs/README.md) lists every how-to and explanation by task. The most used how-tos: run a bank [in a query](https://github.com/hgilde/polars-online/blob/main/docs/how-to/run-a-bank-in-a-query.md) or [from the command line](https://github.com/hgilde/polars-online/blob/main/docs/how-to/run-a-bank-from-the-command-line.md); [resume](https://github.com/hgilde/polars-online/blob/main/docs/how-to/resume-from-a-saved-state.md) or [serve](https://github.com/hgilde/polars-online/blob/main/docs/how-to/serve-from-a-saved-state.md) from a state; [a target that looks ahead](https://github.com/hgilde/polars-online/blob/main/docs/how-to/make-a-target-that-looks-ahead.md); [readiness](https://github.com/hgilde/polars-online/blob/main/docs/how-to/decide-when-a-model-is-ready.md); [settings from the stream's own errors](https://github.com/hgilde/polars-online/blob/main/docs/CALIBRATION.md); [trees on files larger than memory](https://github.com/hgilde/polars-online/blob/main/docs/GBDT.md). ## Reference [OUTPUTS.md](https://github.com/hgilde/polars-online/blob/main/docs/OUTPUTS.md) (every model's output fields), [RUNNER.md](https://github.com/hgilde/polars-online/blob/main/docs/RUNNER.md) (every TOML key and flag), [STATE-WORKFLOW.md](https://github.com/hgilde/polars-online/blob/main/docs/STATE-WORKFLOW.md), [DIAGNOSTICS.md](https://github.com/hgilde/polars-online/blob/main/docs/DIAGNOSTICS.md), [DATA-QUALITY.md](https://github.com/hgilde/polars-online/blob/main/docs/DATA-QUALITY.md), [PERFORMANCE.md](https://github.com/hgilde/polars-online/blob/main/docs/PERFORMANCE.md), [VERSIONS.md](https://github.com/hgilde/polars-online/blob/main/docs/VERSIONS.md), [EXAMPLE-DATA.md](https://github.com/hgilde/polars-online/blob/main/docs/EXAMPLE-DATA.md) ## Explanations - [Why every prediction is out-of-sample](https://github.com/hgilde/polars-online/blob/main/docs/explanation/out-of-sample.md) - [Chunk invariance and streaming](https://github.com/hgilde/polars-online/blob/main/docs/explanation/chunk-invariance-and-streaming.md) - [Time and decay](https://github.com/hgilde/polars-online/blob/main/docs/explanation/time-and-decay.md) - [Warm-up and readiness](https://github.com/hgilde/polars-online/blob/main/docs/explanation/warm-up-and-readiness.md) - [A bank's state and resuming it](https://github.com/hgilde/polars-online/blob/main/docs/explanation/state-and-resuming.md) - [How a window weighs its rows](https://github.com/hgilde/polars-online/blob/main/docs/WINDOWS.md) - [How a Gram stands in for the rows](https://github.com/hgilde/polars-online/blob/main/docs/GRAM.md) - [Against scikit-learn](https://github.com/hgilde/polars-online/blob/main/docs/explanation/against-scikit-learn.md) ## Optional - [Changelog](https://github.com/hgilde/polars-online/blob/main/CHANGELOG.md) - [Extending](https://github.com/hgilde/polars-online/blob/main/docs/EXTENDING.md): adding a model - [Testing](https://github.com/hgilde/polars-online/blob/main/docs/TESTING.md): what the suite proves