Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does pandas/scipy/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.

like image 462
bbc Avatar asked Jun 23 '14 15:06

bbc


People also ask

How does Python Numpy calculate standard deviation?

The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(x)) , where x = abs(a - a. mean())**2 . The average squared deviation is typically calculated as x. sum() / N , where N = len(x) .

How do you find the standard deviation in Python?

Coding a stdev() Function in Python sqrt() to take the square root of the variance. With this new implementation, we can use ddof=0 to calculate the standard deviation of a population, or we can use ddof=1 to estimate the standard deviation of a population using a sample of data.

What is standard deviation Python?

stdev() method calculates the standard deviation from a sample of data. Standard deviation is a measure of how spread out the numbers are. A large standard deviation indicates that the data is spread out, - a small standard deviation indicates that the data is clustered closely around the mean.

Why do pandas use Numpy?

Numpy is memory efficient. Pandas has a better performance when a number of rows is 500K or more. Numpy has a better performance when number of rows is 50K or less. Indexing of the pandas series is very slow as compared to numpy arrays.


1 Answers

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

like image 64
Paul H Avatar answered Sep 24 '22 12:09

Paul H