kups.core.utils.ema
¶
Exponential Moving Average (EMA) with numerical stability.
This module implements an exponential moving average that maintains numerical stability using Kahan summation to reduce floating-point errors during accumulation.
EMA
¶
Exponential moving average for arbitrary PyTree structures.
Computes a weighted moving average where recent values have exponentially higher weight. Uses Kahan summation for numerical stability to prevent accumulation of floating-point errors.
The EMA update follows:
The final average is: data_t / weight_t
Attributes:
| Name | Type | Description |
|---|---|---|
data |
PyTree
|
Accumulated weighted sum as a PyTree. |
weight |
Array
|
Total accumulated weight as a scalar. |
alpha |
float
|
Decay factor in range (0, 1). Higher values give more weight to history. |
_compensate |
PyTree
|
Kahan summation error compensation for data (internal). |
_weight_compensate |
Array
|
Kahan summation error compensation for weight (internal). |
Example
Source code in src/kups/core/utils/ema.py
get()
¶
Compute the current moving average.
Returns:
| Type | Description |
|---|---|
PyTree
|
The weighted average of all observed data. |
init(data, alpha)
staticmethod
¶
Initialize an EMA tracker with zero state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
T
|
Template PyTree structure to track. Used only for shape/structure. |
required |
alpha
|
float
|
Decay factor in range (0, 1). Values closer to 1 give more weight to historical data. |
required |
Returns:
| Type | Description |
|---|---|
EMA[T]
|
Initialized EMA with zero-valued data and weight. |