tsb — Rolling Extended Statistics

Higher-order rolling window statistics extending the core pandas.Series.rolling() API: sem, skew, kurt, and quantile.

1. rollingSem — Standard Error of the Mean

The 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]

Live demo — sem with window=3

Comma-separated numbers (nulls accepted):


    

2. rollingSkew — Fisher-Pearson Skewness

Skewness 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

Live demo — skewness with window=4


    

3. rollingKurt — Excess Kurtosis

Kurtosis 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

Live demo — excess kurtosis with window=5


    

4. 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]

Interpolation methods

MethodBehaviour when q falls between two values
linear (default)Linear interpolation — same as NumPy / pandas default
lowerTake the lower of the two surrounding values
higherTake the higher of the two surrounding values
midpointArithmetic mean of the two surrounding values
nearestWhichever surrounding value is closest

Live demo — rolling quantile


    

Common Options

OptionTypeDefaultDescription
minPeriodsnumber= windowMinimum valid obs required per window
centerbooleanfalseCentre the window around each position
Note: Functions are pure — they return new Series objects without modifying the input. Missing values (null, NaN) are excluded from each window calculation.