Skip to content

Cusum

polars_ts.changepoint.cusum

cusum(df, target_col='y', id_col='unique_id', normalize=True)

Compute the cumulative sum (CUSUM) of deviations from the mean.

CUSUM detects shifts in the mean level of a time series. The statistic accumulates deviations from the overall mean: C[t] = sum_{i=1}^{t} (x[i] - mean(x)). A sustained change in mean produces a clear slope change in the CUSUM curve.

Parameters:

Name Type Description Default
df DataFrame

Polars DataFrame containing the time series data.

required
target_col str

The column containing the time series values. Defaults to y.

'y'
id_col str

The column to group by for multiple time series. Defaults to unique_id.

'unique_id'
normalize bool

If True, divide by the standard deviation to get a standardized CUSUM (unitless). Defaults to True.

True

Returns:

Type Description
DataFrame

The input DataFrame with an additional cusum column.

Raises:

Type Description
ValueError

If the DataFrame is empty.

KeyError

If the required columns are missing.