7

I have a pandas series. I need to get sigma_i, which is the standard deviation of a series up to index i. Is there any existing function which efficiently calculates that?

I noticed that there are cummax, cummin functions.

Jason Aller
  • 3,391
  • 28
  • 37
  • 36
bbc
  • 961
  • 3
  • 11
  • 20

1 Answers1

10

See pandas.expanding_std.

for instance:

import numpy as np
import matplotlib.pyplot as plt
import pandas
%matplotlib inline

data = pandas.Series(np.random.normal(size=37))

full_std = np.std(data)
expand_std = pandas.expanding_std(data, min_periods=1)

fig, ax = plt.subplots()
expand_std.plot(ax=ax, color='k', linewidth=1.5, label='Expanded Std. Dev.')
ax.axhline(y=full_std, color='g', label='Full Value')
ax.legend()

Edit: oops, forgot the picture:

enter image description here

Paul H
  • 52,530
  • 16
  • 137
  • 125
  • Interesting -- thoughts why the last value of `expand_std` isn't exactly equal to `full_std`? Shouldn't those be equivalent? – Vincent Nov 06 '17 at 21:30
  • @Vincent, different default values for the degrees of freedom parameter (`ddof`: for numpy it's 0, for pandas, it's 1) – Paul H Nov 06 '17 at 21:36