Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pandas rolling_std and np.std on a window of an array

I have some problems regarding the rolling_std function of pandas.stats.moments. Strangely I get different results using this functionality compared to the numpy.std function applied to a rolling window over an array.

here is the code to reproduce this error:

# import the modules
import numpy as np
import pandas as pd

# define timeseries and sliding window size
timeseries = np.arange(10)
periods = 4

# output of different results
pd.stats.moments.rolling_std(timeseries, periods)
[np.std(timeseries[max(i-periods+1,0):i+1]) for i in np.arange(10)]

Yielding:

#pandas
array([        nan,         nan,         nan,  1.29099445,  1.29099445,
    1.29099445,  1.29099445,  1.29099445,  1.29099445,  1.29099445])
#numpy
[0.0, 0.5, 0.81649658092772603, 1.1180339887498949, 1.1180339887498949, 1.1180339887498949, 1.1180339887498949, 1.1180339887498949, 1.1180339887498949, 1.1180339887498949]

If I calculate this by hand the numpy results seems to be correct. Has anyone encountered this before or has an explanation?

like image 905
Momo Avatar asked Dec 15 '13 22:12

Momo


1 Answers

Pandas' rolling_std is computed using default delta degrees of freedom, ddof, equal to 1, being more like R in that aspect. While default ddof for numpy's std is 0. You will get the equivalent results while specifying ddof=1 for np.std

>>> [np.std(timeseries[max(i-periods+1,0):i+1], ddof=1) for i in np.arange(10)]
[nan, 0.70710678118654757, 1.0, 1.2909944487358056, 1.2909944487358056, 1.2909944487358056, 1.2909944487358056, 1.29099444873580
56, 1.2909944487358056, 1.2909944487358056]

Or ddof=0 for rolling_std:

>>> pd.stats.moments.rolling_std(timeseries, periods, ddof=0)
array([        nan,         nan,         nan,  1.11803399,  1.11803399,
        1.11803399,  1.11803399,  1.11803399,  1.11803399,  1.11803399])
like image 146
alko Avatar answered Nov 04 '22 21:11

alko