Skip to content

Commit f82be6d

Browse files
Merge pull request #11361 from matthewgilbert/master
DOC: added exp weighting clarifications from #8861
2 parents f44a83a + 5e33d4f commit f82be6d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

doc/source/computation.rst

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,18 @@ In general, a weighted moving average is calculated as
528528
529529
y_t = \frac{\sum_{i=0}^t w_i x_{t-i}}{\sum_{i=0}^t w_i},
530530
531-
where :math:`x_t` is the input at :math:`y_t` is the result.
531+
where :math:`x_t` is the input and :math:`y_t` is the result.
532+
533+
The EW functions support two variants of exponential weights.
534+
The default, ``adjust=True``, uses the weights :math:`w_i = (1 - \alpha)^i`
535+
which gives
536+
537+
.. math::
538+
539+
y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ...
540+
+ (1 - \alpha)^t x_{0}}{1 + (1 - \alpha) + (1 - \alpha)^2 + ...
541+
+ (1 - \alpha)^t}
532542
533-
The EW functions support two variants of exponential weights:
534-
The default, ``adjust=True``, uses the weights :math:`w_i = (1 - \alpha)^i`.
535543
When ``adjust=False`` is specified, moving averages are calculated as
536544

537545
.. math::
@@ -556,6 +564,34 @@ which is equivalent to using weights
556564
557565
y_t = \alpha' y_{t-1} + (1 - \alpha') x_t.
558566
567+
The difference between the above two variants arises because we are
568+
dealing with series which have finite history. Consider a series of infinite
569+
history:
570+
571+
.. math::
572+
573+
y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ...}
574+
{1 + (1 - \alpha) + (1 - \alpha)^2 + ...}
575+
576+
Noting that the denominator is a geometric series with initial term equal to 1
577+
and a ratio of :math:`1 - \alpha` we have
578+
579+
.. math::
580+
581+
y_t &= \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ...}
582+
{\frac{1}{1 - (1 - \alpha)}}\\
583+
&= [x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ...] \alpha \\
584+
&= \alpha x_t + [(1-\alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ...]\alpha \\
585+
&= \alpha x_t + (1 - \alpha)[x_{t-1} + (1 - \alpha) x_{t-2} + ...]\alpha\\
586+
&= \alpha x_t + (1 - \alpha) y_{t-1}
587+
588+
which shows the equivalence of the above two variants for infinite series.
589+
When ``adjust=True`` we have :math:`y_0 = x_0` and from the last
590+
representation above we have :math:`y_t = \alpha x_t + (1 - \alpha) y_{t-1}`,
591+
therefore there is an assumption that :math:`x_0` is not an ordinary value
592+
but rather an exponentially weighted moment of the infinite series up to that
593+
point.
594+
559595
One must have :math:`0 < \alpha \leq 1`, but rather than pass :math:`\alpha`
560596
directly, it's easier to think about either the **span**, **center of mass
561597
(com)** or **halflife** of an EW moment:

pandas/stats/moments.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
relative positions. For example, the weights of x and y used in calculating
125125
the final weighted average of [x, None, y] are 1-alpha and 1 (if adjust is
126126
True), and 1-alpha and alpha (if adjust is False).
127+
128+
More details can be found at
129+
http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-moment-functions
127130
"""
128131

129132
_expanding_kw = """min_periods : int, default None

0 commit comments

Comments
 (0)