Home

Awesome

pcw-regrs

pypi-release

Fast, optimal, extensible and cross-validated heterogeneous piecewise polynomial regression for timeseries.

The algorithm is implemented in Rust in the pcw_regrs subdirectory. A Python API can be found in pcw_regrs_py and installed via maturin.

[!NOTE] Please also see the accompanying paper's preprint at arXiv.

Real world example

Here we've fitted a piecwise polynomial model to the construction dataset of the Turing Change Point Dataset in a two-step procedure. The first fit determines local behaviour (our model in green):

A plot of the construction dataset together with our model. It can be seen that the data is piecewise quadratic and the fitted model matches the data quite well.

We calculated means based on the resulting piecewise polynomial model and used these to fit another model to capture global trends:

Another plot of the construction dataset. Now with the calculated means and the model based on those

The determined changepoints (dashed lines) can be seen to coincide reasonably to economically significant events (shaded regions).

How to use

Please check out the example notebook for a quick-start on how to use the Python-API.

General structure

A basic pipeline diagram for the algorithm. The input consists of a timeseries sample as well as the optional parameters. These are sent into a preprocessing stage for validation and to optionally calculate the training errors. Once that's finished we move on to the main algorithm which solves the main dynamic program, determines the CV and model functions and then determines the CV- or OSE-optimal piecewise functions. Finally a postprocessing stage is used to select cut representatives either by calculating segment middlepoints or running a continuity optimization

Optional features

Note that the crate comes with a variety of optional features:

Building

[!NOTE] Note that building currently (at the time of writing: September 2023) requires a nightly rust compiler which may be easily installed using rustup toolchain install nightly. We're only using it for let-chains; these could be removed relatively easily if a stable variant is needed, at the expense of making the code a bit more verbose. See also the https://crates.io/crates/if_chain crate in this regard.

Rust

The pure Rust part is a regular cargo project and can be managed accordingly. Note also that pcw-regrs has been published to crates.io.

Python

[!IMPORTANT] Usually building from source is not necessary and you can simply install pcw-regrs-py via pip.

To build the python package install maturin for example via pip by running

pip install maturin

Running

maturin develop --release

from the python subfolder will compile an release (=optimized) build, build a python wheel and install it locally. For more details like how to cross compile please consult the maturin documentation.