ewm(self, com: 'float | None' = None, span: 'float | None' = None, halflife: 'float | TimedeltaConvertibleTypes | None' = None, alpha: 'float | None' = None, min_periods: 'int | None' = 0, adjust: 'bool_t' = True, ignore_na: 'bool_t' = False, axis: 'Axis' = 0, times: 'str | np.ndarray | DataFrame | Series | None' = None, method: 'str' = 'single') -> 'ExponentialMovingWindow'
Exactly one parameter: com
, span
, halflife
, or alpha
must be provided.
See Windowing Operations <window.exponentially_weighted>
for further usage details and examples.
Specify decay in terms of center of mass
$\alpha = 1 / (1 + com)$ , for $com \geq 0$ .
Specify decay in terms of span
$\alpha = 2 / (span + 1)$ , for $span \geq 1$ .
Specify decay in terms of half-life
$\alpha = 1 - \exp\left(-\ln(2) / halflife\right)$ , for $halflife > 0$ .
If times
is specified, the time unit (str or timedelta) over which an observation decays to half its value. Only applicable to mean()
, and halflife value will not apply to the other functions.
Specify smoothing factor $\alpha$ directly
$0 < \alpha \leq 1$ .
Minimum number of observations in window required to have a value; otherwise, result is np.nan
.
Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
When adjust=True
(default), the EW function is calculated using weights $w_i = (1 - \alpha)^i$
. For example, the EW moving average of the series [:math:None:None:`x_0, x_1, ..., x_t`
] would be:
When adjust=False
, the exponentially weighted function is calculated recursively:
Ignore missing values when calculating weights.
When ignore_na=False
(default), weights are based on absolute positions. For example, the weights of $x_0$
and $x_2$
used in calculating the final weighted average of [:math:`x_0`, None, :math:`x_2`] are $(1-\alpha)^2$
and $1$
if adjust=True
, and $(1-\alpha)^2$
and $\alpha$
if adjust=False
.
When ignore_na=True
, weights are based on relative positions. For example, the weights of $x_0$
and $x_2$
used in calculating the final weighted average of [:math:`x_0`, None, :math:`x_2`] are :math:`1-\alpha` and :math:`1` if adjust=True
, and $1-\alpha$
and $\alpha$
if adjust=False
.
If 0
or 'index'
, calculate across the rows.
If 1
or 'columns'
, calculate across the columns.
Only applicable to mean()
.
Times corresponding to the observations. Must be monotonically increasing and datetime64[ns]
dtype.
If 1-D array like, a sequence with the same shape as the observations.
If str, the name of the column in the DataFrame representing the times.
Execute the rolling operation per single column or row ( 'single'
) or over the entire object ( 'table'
).
This argument is only implemented when specifying engine='numba'
in the method call.
Only applicable to mean()
Provide exponentially weighted (EW) calculations.
expanding
Provides expanding transformations.
rolling
Provides rolling window calculations.
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})This example is valid syntax, but we were not able to check execution
... df B 0 0.0 1 1.0 2 2.0 3 NaN 4 4.0
>>> df.ewm(com=0.5).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.670213This example is valid syntax, but we were not able to check execution
>>> df.ewm(alpha=2 / 3).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.670213
adjust
This example is valid syntax, but we were not able to check execution>>> df.ewm(com=0.5, adjust=True).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.670213This example is valid syntax, but we were not able to check execution
>>> df.ewm(com=0.5, adjust=False).mean() B 0 0.000000 1 0.666667 2 1.555556 3 1.555556 4 3.650794
ignore_na
This example is valid syntax, but we were not able to check execution>>> df.ewm(com=0.5, ignore_na=True).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.225000This example is valid syntax, but we were not able to check execution
>>> df.ewm(com=0.5, ignore_na=False).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.670213
times
Exponentially weighted mean with weights calculated with a timedelta halflife
relative to times
.
>>> times = ['2020-01-01', '2020-01-03', '2020-01-10', '2020-01-15', '2020-01-17']See :
... df.ewm(halflife='4 days', times=pd.DatetimeIndex(times)).mean() B 0 0.000000 1 0.585786 2 1.523889 3 1.523889 4 3.233686
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.generic.NDFrame.rolling
pandas.core.generic.NDFrame.expanding
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them