rollit.std: NumPy Rolling Standard Deviation
The rollit.std() function calculates the rolling sample standard deviation across a sliding window on a 1D NumPy array. To ensure 100% parity with Pandas default statistics, it defaults to Bessel's correction with ddof=1.
Formula
The standard deviation uses Bessel's correction (denominator of W - 1) for sample statistics:
Where W is the window size, μ is the local window mean, and x represents the local values.
Parameters
arr (np.ndarray)
The input 1D flat NumPy array containing floats or integers. Optimization is fine-tuned for 1D arrays to run at C-speed using raw strides.
window (int)
The size of the sliding calculation frame. Must be a positive integer smaller than the total array size.
min_periods (int | None, optional)
The minimum number of valid (non-NaN) points required to return a numeric output. If window items contain missing values below this threshold, NaN is returned.
Bessel's Correction Notice: Standard NumPy standard deviation functions default to ddof=0. Because rollit.std() matches the Pandas default of ddof=1, it serves as a direct drop-in replacement when cleaning up Pandas dependencies in your codebase.
Basic Example
import numpy as np
import rollit
# Generate flat 1D array
data = np.array([1.0, 2.0, 4.0, 8.0, 16.0])
# Compute rolling sample standard deviation (window size = 3)
# Uses Bessel's correction (ddof=1) to match Pandas defaults
stds = rollit.std(data, window=3)
print("Input data:", data)
print("Rolling standard deviation:", stds)
# Output: [1.5275, 3.0551, 6.1101]Verifying Parity with Pandas
The results of rollit.std() are mathematically equivalent to Pandas Series rolling std calculations:
import numpy as np
import pandas as pd
import rollit
data = np.array([10.0, 20.0, 40.0, 10.0, 20.0])
# Calculated using rollit (Zero dependencies, 50 KB package size)
rollit_std = rollit.std(data, window=3)
# Calculated using Pandas (requires 35 MB library import)
pandas_std = pd.Series(data).rolling(window=3).std().dropna().values
# They are mathematically identical!
print("rollit std: ", rollit_std)
print("pandas std: ", pandas_std)
assert np.allclose(rollit_std, pandas_std)