rsvp/fecon235

pandas deprecation of Exponential Moving Average function ewma

rsvp opened this issue · 5 comments

rsvp commented

Description of specific issue

As of pandas 0.18 the use of its ewma() function will produce a warning which looks like:

    FutureWarning: pd.ewm_mean is deprecated for DataFrame and will be 
    removed in a future version, replace with
    DataFrame.ewm(ignore_na=False,span=30.007751938,min_periods=0,adjust=True).mean()

therefore, this deprecation will adversely impact fecon235 exponential moving average
function ema in the yi_1tools module -- when pandas goes to 0.19.

Expected behavior

No warning to be raised.

Observed behavior

Computed result has no errors. The warning is given only the first time when the
exponential moving average function is used.

Strangely, pd.ewm_mean?? produces no results, so the warning is cryptic.

Why would the improvement/fix be useful to most users?

  • Bug prevention for pandas 0.19 and above

Additional helpful details for bugs

  • Problem started recently, but not in older versions
  • Problem happens with all files, not only some files
  • Problem can be reliably reproduced
  • Problem happens randomly
  • fecon235 version: v4.16.0525
  • pandas version: 0.18.0
  • Python version: 2.7.11
rsvp commented

This will be a easy fix for all pandas versions since we will not rely on the suggestion
in the warning to use awkward DataFrame.ewm().mean()

Instead we have re-written our function ema in terms of
another function of ours holtlevel as follows:

def ema( y, alpha=0.20 ):
    '''EXPONENTIAL MOVING AVERAGE using traditional weight arg.'''
    #  y could be a dataframe.
    #  ema is mathematically equivalent to holtlevel with beta=0,
    #  thus issue #5 can be easily resolved for all pandas versions.
    return holtlevel( y, alpha, beta=0 )

Exponential moving average is mathematically a special case of
Holt-Winters smoothing where beta is constrained to zero.
The latter is computational more expensive, so this fix
is justified by logic and simplicity.
(Numerically there may be a tiny constant difference
resulting from varied algorithmic treatments of the beginning
of the time-series as the smoothing warms up :)

To emphasize this fact, we have moved our ema() function
from the yi_1tools module to yi_timeseries module.
The move should not pose a problem in our notebooks
since calls of the explicit form yi_1tools.ema() were never used.

This fix shall be effective as of fecon235 v4.16.1030

rsvp commented

ema() bug fix -- 2016.12.14

The new dependency of ema() on holtlevel() revealed a bug which produced
bizarre exponential moving averages when beta=0.
The details of the CRITICAL fix are here:
35d3783

REMEDY: pull the latest master branch.

rsvp commented

SUMMARY: code for Holt-Winters filter and Exponential moving average

They are bundled in this module:
https://github.com/rsvp/fecon235/blob/master/lib/yi_timeseries.py

ema() is written as a special case of holtlevel()

thanks :P

rsvp commented

2018 Addendum for ema()

The fecon235 source code was refactored in https://git.io/fecon236

Here's the specific module for Holt-Winters which includes ema():
https://github.com/MathSci/fecon236/blob/master/fecon236/tsa/holtwinters.py

The code is also useful in finding the optimal parameter used in the
Exponential moving average, given specific data.

And the tests give some numerical examples:
https://github.com/MathSci/fecon236/blob/master/tests/test_holtwinters.py

... requires at least Python 2.7 or Python 3.4.