'Does Pandas, SciPy, or NumPy provide a cumulative standard deviation function?

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 an existing function which efficiently calculates that?

I noticed that there are the cummax and cummin functions.



Solution 1:[1]

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()

Enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen