Higher-order rolling window statistics extending the core
pandas.Series.rolling()
API:
sem, skew, kurt, and
quantile.
rollingSem — Standard Error of the MeanThe standard error of the mean measures how much the sample mean would vary across repeated samples. For a window of n values:
sem = std(ddof=1) / √n
Requires at least 2 valid observations per window.
import { rollingSem, Series } from "tsb";
const s = new Series({ data: [2, 4, 4, 4, 5, 5, 7, 9], name: "x" });
const sem3 = rollingSem(s, 3);
// [null, null, 0.667, 0, 0.577, 0.577, 1.155, 2.082]
Comma-separated numbers (nulls accepted):
rollingSkew — Fisher-Pearson SkewnessSkewness measures asymmetry of the distribution in each window. Positive = right tail heavier; negative = left tail heavier. Uses the unbiased Fisher-Pearson formula (same as pandas):
skew = [n/((n-1)(n-2))] × Σ[(xᵢ−x̄)/s]³
Requires ≥ 3 valid observations.
import { rollingSkew, Series } from "tsb";
const s = new Series({ data: [1, 2, 3, 4, 5] });
rollingSkew(s, 3);
// [null, null, 0, 0, 0] ← symmetric windows → zero skew
rollingKurt — Excess KurtosisKurtosis measures how heavy the tails are relative to a normal distribution. The excess kurtosis subtracts 3, so a normal distribution gives 0. Uses the Fisher (1930) unbiased formula:
kurt = [n(n+1)/((n-1)(n-2)(n-3))] × Σ[(xᵢ−x̄)/s]⁴ − 3(n-1)²/((n-2)(n-3))
Requires ≥ 4 valid observations.
import { rollingKurt, Series } from "tsb";
const s = new Series({ data: [1, 2, 3, 4] });
rollingKurt(s, 4);
// [null, null, null, -1.2] ← uniform distribution has kurt = -1.2
rollingQuantile — Rolling Quantile
Computes any quantile within each sliding window using configurable
interpolation. When q = 0.5 this is identical to
rolling.median().
import { rollingQuantile, Series } from "tsb";
const s = new Series({ data: [1, 2, 3, 4, 5] });
rollingQuantile(s, 0.5, 3); // rolling median: [null, null, 2, 3, 4]
rollingQuantile(s, 0.25, 3); // [null, null, 1.5, 2.5, 3.5]
rollingQuantile(s, 0.75, 3); // [null, null, 2.5, 3.5, 4.5]
| Method | Behaviour when q falls between two values |
|---|---|
linear (default) | Linear interpolation — same as NumPy / pandas default |
lower | Take the lower of the two surrounding values |
higher | Take the higher of the two surrounding values |
midpoint | Arithmetic mean of the two surrounding values |
nearest | Whichever surrounding value is closest |
| Option | Type | Default | Description |
|---|---|---|---|
minPeriods | number | = window | Minimum valid obs required per window |
center | boolean | false | Centre the window around each position |
null, NaN)
are excluded from each window calculation.