From 26bbf02480be75dc850f0c291a1486b0e69c75b9 Mon Sep 17 00:00:00 2001 From: Sandu Ursu Date: Tue, 25 Feb 2020 04:22:47 +0100 Subject: [PATCH 1/3] Clean DataFrame.ewm --- pandas/core/window/ewm.py | 66 +++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index e045d1c2211d7..ea816e86b0ea6 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -35,13 +35,13 @@ class EWM(_Rolling): ---------- com : float, optional Specify decay in terms of center of mass, - :math:`\alpha = 1 / (1 + com),\text{ for } com \geq 0`. + :math:`\alpha = 1 / (1 + com)`, for :math:`com \geq 0`. span : float, optional Specify decay in terms of span, - :math:`\alpha = 2 / (span + 1),\text{ for } span \geq 1`. + :math:`\alpha = 2 / (span + 1)`, for :math:`span \geq 1`. halflife : float, optional Specify decay in terms of half-life, - :math:`\alpha = 1 - exp(log(0.5) / halflife),\text{for} halflife > 0`. + :math:`\alpha = 1 - exp(-ln(2) / halflife)`, for :math:`halflife > 0`. alpha : float, optional Specify smoothing factor :math:`\alpha` directly, :math:`0 < \alpha \leq 1`. @@ -71,30 +71,42 @@ class EWM(_Rolling): Notes ----- - Exactly one of center of mass, span, half-life, and alpha must be provided. - Allowed values and relationship between the parameters are specified in the - parameter descriptions above; see the link at the end of this section for - a detailed explanation. - - When adjust is True (default), weighted averages are calculated using - weights (1-alpha)**(n-1), (1-alpha)**(n-2), ..., 1-alpha, 1. - - When adjust is False, weighted averages are calculated recursively as: - weighted_average[0] = arg[0]; - weighted_average[i] = (1-alpha)*weighted_average[i-1] + alpha*arg[i]. - - When ignore_na is False (default), weights are based on absolute positions. - For example, the weights of x and y used in calculating the final weighted - average of [x, None, y] are (1-alpha)**2 and 1 (if adjust is True), and - (1-alpha)**2 and alpha (if adjust is False). - - When ignore_na is True (reproducing pre-0.15.0 behavior), weights are based - on relative positions. For example, the weights of x and y used in - calculating the final weighted average of [x, None, y] are 1-alpha and 1 - (if adjust is True), and 1-alpha and alpha (if adjust is False). - - More details can be found at - https://pandas.pydata.org/pandas-docs/stable/user_guide/computation.html#exponentially-weighted-windows + Exactly one center of mass paramter: ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + Allowed values and the relation between the parameters are specified in the + parameter descriptions above (see the link at the end of this section for + a detailed explanation). + + Available EW (exponentially weighted) methods: ``mean()``, ``var()``, ``std()``, ``corr()``, ``cov()``. + + When ``adjust=True`` (default), the EW function is calculated using + weights :math:`w_i = (1 - \alpha)^i`. + For example, the EW moving average of the series :math:`x_0, x_1, ..., x_t` would be: + + .. math:: + y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... +(1 - \alpha)^t x_{0}}{1 + + (1 - \alpha) + (1 - \alpha)^2 + ... + (1 - \alpha)^t} + + When ``adjust=False``, the exponentially weighted function is calculated recursively: + + .. math:: + \begin{split} + y_0 &= x_0 \\ + y_t &= (1 - \alpha) y_{t-1} + \alpha x_t, + \end{split} + + When ``ignore_na=False`` (default), weights are based on absolute positions. + For example, the weights of :math:`x_0` and :math:`x_2` used in calculating the final weighted + average of [:math:`x_0`, None, :math:`x_2`] are :math:`(1-\alpha)^2` and :math:`1` (if ``adjust=True``), and + :math:`(1-\alpha)^2` and :math:`\alpha` (if ``adjust=False``). + + When ``ignore_na=True`` (reproducing pre-0.15.0 behavior), weights are based + on relative positions. For example, the weights of :math:`x_0` and :math:`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 :math:`1-\alpha` and :math:`\alpha` (if ``adjust=False``). + + More details can be found at: + `Exponentially weighted windows + `_. Examples -------- From 8251a3afad8537aa31f9082041815fc1efdf9c54 Mon Sep 17 00:00:00 2001 From: Sandu Ursu Date: Tue, 25 Feb 2020 04:51:01 +0100 Subject: [PATCH 2/3] Remove brackets --- pandas/core/window/ewm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index ea816e86b0ea6..0deb71f1a7e7b 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -80,7 +80,7 @@ class EWM(_Rolling): When ``adjust=True`` (default), the EW function is calculated using weights :math:`w_i = (1 - \alpha)^i`. - For example, the EW moving average of the series :math:`x_0, x_1, ..., x_t` would be: + For example, the EW moving average of the series [:math:`x_0, x_1, ..., x_t`] would be: .. math:: y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... +(1 - \alpha)^t x_{0}}{1 + @@ -96,13 +96,13 @@ class EWM(_Rolling): When ``ignore_na=False`` (default), weights are based on absolute positions. For example, the weights of :math:`x_0` and :math:`x_2` used in calculating the final weighted - average of [:math:`x_0`, None, :math:`x_2`] are :math:`(1-\alpha)^2` and :math:`1` (if ``adjust=True``), and - :math:`(1-\alpha)^2` and :math:`\alpha` (if ``adjust=False``). + average of [:math:`x_0`, None, :math:`x_2`] are :math:`(1-\alpha)^2` and :math:`1` if ``adjust=True``, and + :math:`(1-\alpha)^2` and :math:`\alpha` if ``adjust=False``. When ``ignore_na=True`` (reproducing pre-0.15.0 behavior), weights are based on relative positions. For example, the weights of :math:`x_0` and :math:`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 :math:`1-\alpha` and :math:`\alpha` (if ``adjust=False``). + if ``adjust=True``, and :math:`1-\alpha` and :math:`\alpha` if ``adjust=False``. More details can be found at: `Exponentially weighted windows From 0b25053d058b967bcc6aa4affd1428031c21a93e Mon Sep 17 00:00:00 2001 From: Sandu Ursu Date: Tue, 25 Feb 2020 05:03:10 +0100 Subject: [PATCH 3/3] Trim lines <88 --- pandas/core/window/ewm.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index 0deb71f1a7e7b..8231521d1414a 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -71,22 +71,26 @@ class EWM(_Rolling): Notes ----- - Exactly one center of mass paramter: ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + Exactly one center of mass paramter: ``com``, ``span``, ``halflife``, or ``alpha`` + must be provided. Allowed values and the relation between the parameters are specified in the parameter descriptions above (see the link at the end of this section for a detailed explanation). - Available EW (exponentially weighted) methods: ``mean()``, ``var()``, ``std()``, ``corr()``, ``cov()``. + Available EW (exponentially weighted) methods: ``mean()``, ``var()``, ``std()``, + ``corr()``, ``cov()``. When ``adjust=True`` (default), the EW function is calculated using weights :math:`w_i = (1 - \alpha)^i`. - For example, the EW moving average of the series [:math:`x_0, x_1, ..., x_t`] would be: + For example, the EW moving average of the series [:math:`x_0, x_1, ..., x_t`] would + be: .. math:: - y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... +(1 - \alpha)^t x_{0}}{1 + - (1 - \alpha) + (1 - \alpha)^2 + ... + (1 - \alpha)^t} + y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... + + (1 - \alpha)^t x_0}{1 + (1 - \alpha) + (1 - \alpha)^2 + ... + (1 - \alpha)^t} - When ``adjust=False``, the exponentially weighted function is calculated recursively: + When ``adjust=False``, the exponentially weighted function is calculated + recursively: .. math:: \begin{split} @@ -95,14 +99,16 @@ class EWM(_Rolling): \end{split} When ``ignore_na=False`` (default), weights are based on absolute positions. - For example, the weights of :math:`x_0` and :math:`x_2` used in calculating the final weighted - average of [:math:`x_0`, None, :math:`x_2`] are :math:`(1-\alpha)^2` and :math:`1` if ``adjust=True``, and - :math:`(1-\alpha)^2` and :math:`\alpha` if ``adjust=False``. - - When ``ignore_na=True`` (reproducing pre-0.15.0 behavior), weights are based - on relative positions. For example, the weights of :math:`x_0` and :math:`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 :math:`1-\alpha` and :math:`\alpha` if ``adjust=False``. + For example, the weights of :math:`x_0` and :math:`x_2` used in calculating the + final weighted average of [:math:`x_0`, None, :math:`x_2`] are :math:`(1-\alpha)^2` + and :math:`1` if ``adjust=True``, and :math:`(1-\alpha)^2` and + :math:`\alpha` if ``adjust=False``. + + When ``ignore_na=True`` (reproducing pre-0.15.0 behavior), weights are based on + relative positions. For example, the weights of :math:`x_0` and :math:`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 :math:`1-\alpha` and + :math:`\alpha` if ``adjust=False``. More details can be found at: `Exponentially weighted windows