tsb — EWM (Exponentially Weighted Moving)
📉 EWM — Interactive Playground
Series.ewm() and
DataFrame.ewm() provide
Exponentially Weighted Moving aggregations,
mirroring pandas.Series.ewm(). Unlike rolling windows (fixed size)
or expanding windows (all past data equally), EWM weights recent observations more
heavily using an exponential decay.
Edit any code block below and press ▶ Run
(or Ctrl+Enter) to execute it live in your browser.
1 · Decay Parameters
Specify the decay via exactly one of span, com,
halflife, or alpha:
span → alpha = 2 / (span + 1)
com → alpha = 1 / (1 + com)
halflife → alpha = 1 − exp(−ln(2) / halflife)
alpha → used directly (must be in (0, 1])
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
2 · EWM Mean
With adjust=true (default), the mean at position t
is the weighted average of all past values, where the weight for
xi is (1−α)t−i.
St = xt + (1−α)·St−1
Wt = 1 + (1−α)·Wt−1
meant = St / Wt
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
3 · adjust=false: Simple IIR Filter
With adjust=false, EWM uses a simple Infinite Impulse Response
formula — the same as an exponential smoothing filter:
yt = α·xt + (1−α)·yt−1
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
4 · EWM Variance and Standard Deviation
EWM variance uses reliability-weights Bessel correction
(bias=false by default, matching pandas).
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
5 · EWM Covariance
Compute pairwise exponentially weighted covariance between two Series. Negative covariance means the series move in opposite directions.
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
6 · EWM Correlation
EWM Pearson correlation between two Series. Perfectly correlated series produce values of +1 or −1.
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
7 · Missing Values (ignoreNa)
The ignoreNa option controls how missing values affect the
exponential weights. With ignoreNa=false (default), null
positions still advance time and cause extra decay. With
ignoreNa=true, nulls are completely skipped.
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
8 · DataFrame EWM
Apply EWM to every numeric column of a DataFrame independently.
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
9 · Custom apply
Use apply(fn) to implement custom EWM aggregations. The
function receives the accumulated values and their EW weights.
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
API Reference
Specify exactly one decay parameter. All methods return Series-like objects
with .values for inspection.
// Series.ewm(options) → EWM
// DataFrame.ewm(options) → DataFrameEwm
// EwmOptions (exactly one decay parameter required):
{
span?: number, // ≥ 1, alpha = 2/(span+1)
com?: number, // ≥ 0, alpha = 1/(1+com)
halflife?: number, // > 0, alpha = 1 − exp(−ln2/halflife)
alpha?: number, // (0, 1]
adjust?: boolean, // default: true
ignoreNa?: boolean, // default: false
minPeriods?: number // default: 0
}
// EWM methods:
ewm.mean() → Series (EwmSeriesLike)
ewm.std(bias?) → Series
ewm.var(bias?) → Series
ewm.cov(other, bias?) → Series
ewm.corr(other) → Series
ewm.apply(fn) → Series // fn: (values, weights) => number
// DataFrameEwm methods:
dfEwm.mean() → DataFrame
dfEwm.std(bias?) → DataFrame
dfEwm.var(bias?) → DataFrame