4

I calculate simple moving average:

def sma(data_frame, length=15):
    # TODO: Be sure about default values of length.
    smas = data_frame.Close.rolling(window=length, center=False).mean()
    return smas

Using the rolling function is it possible to calculate weighted moving average? As I read in the documentation, I think that I have to pass win_type parameter. But I'm not sure which one I have to choose.

Here is a definition for weighted moving average.

Thanks in advance,

xkcd
  • 2,538
  • 11
  • 59
  • 96
  • 2
    Have a look at [`np.average`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.average.html) which supports weights to be provided. – Nickil Maveli Sep 28 '16 at 09:16
  • 3
    I would file an issue on [Github](https://github.com/pydata/pandas). The documentation is indeed misleading. [It suggests](https://github.com/pydata/pandas/blob/37f95cef85834207db0930e863341efb285e38a2/doc/source/computation.rst#id49) one could pass _custom_ _weights_, but it doesn't explain how one would do so, and there are no examples in the repository where this is done. – Daniel Sep 28 '16 at 10:38

1 Answers1

7

Yeah, that part of pandas really isn't very well documented. I think you might have to use rolling.apply() if you aren't using one of the standard window types. I poked at it and got this to work:

>>> import numpy as np
>>> import pandas as pd
>>> d = pd.DataFrame({'a':range(10), 'b':np.random.random(size=10)})
>>> d.b = d.b.round(2)
>>> d
   a     b
0  0  0.28
1  1  0.70
2  2  0.28
3  3  0.99
4  4  0.72
5  5  0.43
6  6  0.71
7  7  0.75
8  8  0.61
9  9  0.14
>>> wts = np.array([-1, 2])
>>> def f(w):                        
        def g(x):
            return (w*x).mean()
        return g
>>> d.rolling(window=2).apply(f(wts))
     a      b
0  NaN    NaN
1  1.0  0.560
2  1.5 -0.070
3  2.0  0.850
4  2.5  0.225
5  3.0  0.070
6  3.5  0.495 
7  4.0  0.395
8  4.5  0.235
9  5.0 -0.165

I think that is correct. The reason for the closure there is that the signature for rolling.apply is rolling.apply(func, *args, **kwargs), so the weights get tuple-unpacked if you just send them to the function directly, unless you send them as a 1-tuple (wts,), but that's weird.

Dthal
  • 3,216
  • 1
  • 16
  • 10