Skip to content

Commit 20474f5

Browse files
authored
DOC: DataFrame.ewm docstring clean-up (#32212)
1 parent 3d08aa5 commit 20474f5

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

pandas/core/window/ewm.py

+45-32
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@
2929

3030
class EWM(_Rolling):
3131
r"""
32-
Provide exponential weighted functions.
32+
Provide exponential weighted (EW) functions.
33+
34+
Available EW functions: ``mean()``, ``var()``, ``std()``, ``corr()``, ``cov()``.
35+
36+
Exactly one parameter: ``com``, ``span``, ``halflife``, or ``alpha`` must be
37+
provided.
3338
3439
Parameters
3540
----------
3641
com : float, optional
3742
Specify decay in terms of center of mass,
38-
:math:`\alpha = 1 / (1 + com),\text{ for } com \geq 0`.
43+
:math:`\alpha = 1 / (1 + com)`, for :math:`com \geq 0`.
3944
span : float, optional
4045
Specify decay in terms of span,
41-
:math:`\alpha = 2 / (span + 1),\text{ for } span \geq 1`.
46+
:math:`\alpha = 2 / (span + 1)`, for :math:`span \geq 1`.
4247
halflife : float, optional
4348
Specify decay in terms of half-life,
44-
:math:`\alpha = 1 - exp(log(0.5) / halflife),\text{for} halflife > 0`.
49+
:math:`\alpha = 1 - \exp\left(-\ln(2) / halflife\right)`, for
50+
:math:`halflife > 0`.
4551
alpha : float, optional
4652
Specify smoothing factor :math:`\alpha` directly,
4753
:math:`0 < \alpha \leq 1`.
@@ -50,11 +56,39 @@ class EWM(_Rolling):
5056
(otherwise result is NA).
5157
adjust : bool, default True
5258
Divide by decaying adjustment factor in beginning periods to account
53-
for imbalance in relative weightings
54-
(viewing EWMA as a moving average).
59+
for imbalance in relative weightings (viewing EWMA as a moving average).
60+
61+
- When ``adjust=True`` (default), the EW function is calculated using weights
62+
:math:`w_i = (1 - \alpha)^i`. For example, the EW moving average of the series
63+
[:math:`x_0, x_1, ..., x_t`] would be:
64+
65+
.. math::
66+
y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... + (1 -
67+
\alpha)^t x_0}{1 + (1 - \alpha) + (1 - \alpha)^2 + ... + (1 - \alpha)^t}
68+
69+
- When ``adjust=False``, the exponentially weighted function is calculated
70+
recursively:
71+
72+
.. math::
73+
\begin{split}
74+
y_0 &= x_0\\
75+
y_t &= (1 - \alpha) y_{t-1} + \alpha x_t,
76+
\end{split}
5577
ignore_na : bool, default False
56-
Ignore missing values when calculating weights;
57-
specify True to reproduce pre-0.15.0 behavior.
78+
Ignore missing values when calculating weights; specify ``True`` to reproduce
79+
pre-0.15.0 behavior.
80+
81+
- When ``ignore_na=False`` (default), weights are based on absolute positions.
82+
For example, the weights of :math:`x_0` and :math:`x_2` used in calculating
83+
the final weighted average of [:math:`x_0`, None, :math:`x_2`] are
84+
:math:`(1-\alpha)^2` and :math:`1` if ``adjust=True``, and
85+
:math:`(1-\alpha)^2` and :math:`\alpha` if ``adjust=False``.
86+
87+
- When ``ignore_na=True`` (reproducing pre-0.15.0 behavior), weights are based
88+
on relative positions. For example, the weights of :math:`x_0` and :math:`x_2`
89+
used in calculating the final weighted average of
90+
[:math:`x_0`, None, :math:`x_2`] are :math:`1-\alpha` and :math:`1` if
91+
``adjust=True``, and :math:`1-\alpha` and :math:`\alpha` if ``adjust=False``.
5892
axis : {0 or 'index', 1 or 'columns'}, default 0
5993
The axis to use. The value 0 identifies the rows, and 1
6094
identifies the columns.
@@ -71,30 +105,9 @@ class EWM(_Rolling):
71105
72106
Notes
73107
-----
74-
Exactly one of center of mass, span, half-life, and alpha must be provided.
75-
Allowed values and relationship between the parameters are specified in the
76-
parameter descriptions above; see the link at the end of this section for
77-
a detailed explanation.
78-
79-
When adjust is True (default), weighted averages are calculated using
80-
weights (1-alpha)**(n-1), (1-alpha)**(n-2), ..., 1-alpha, 1.
81-
82-
When adjust is False, weighted averages are calculated recursively as:
83-
weighted_average[0] = arg[0];
84-
weighted_average[i] = (1-alpha)*weighted_average[i-1] + alpha*arg[i].
85-
86-
When ignore_na is False (default), weights are based on absolute positions.
87-
For example, the weights of x and y used in calculating the final weighted
88-
average of [x, None, y] are (1-alpha)**2 and 1 (if adjust is True), and
89-
(1-alpha)**2 and alpha (if adjust is False).
90-
91-
When ignore_na is True (reproducing pre-0.15.0 behavior), weights are based
92-
on relative positions. For example, the weights of x and y used in
93-
calculating the final weighted average of [x, None, y] are 1-alpha and 1
94-
(if adjust is True), and 1-alpha and alpha (if adjust is False).
95-
96-
More details can be found at
97-
https://pandas.pydata.org/pandas-docs/stable/user_guide/computation.html#exponentially-weighted-windows
108+
109+
More details can be found at:
110+
:ref:`Exponentially weighted windows <stats.moments.exponentially_weighted>`.
98111
99112
Examples
100113
--------

0 commit comments

Comments
 (0)