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

2024/9/28 5:31:57

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.

Answer

See pandas.expanding_std.

For instance:

import numpy as np
import matplotlib.pyplot as plt
import pandas
%matplotlib inlinedata = 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

https://en.xdnf.cn/q/71378.html

Related Q&A

Python: compile into an Unix commandline app

I am not sure if I searched for the wrong terms, but I could not find much on this subject. I am on osx and Id like to compile a commandline python script into a small commandline app, that I can put i…

ModuleNotFoundError in PySpark Worker on rdd.collect()

I am running an Apache Spark program in python, and I am getting an error that I cant understand and cant begin to debug. I have a driver program that defines a function called hound in a file called h…

Sphinx is not able to import anything

I am trying to use sphinx to document a project of mine. I have used autodoc strings within all of my modules and files. I used sphinx-apidoc to automatically generate rst files for my code. So far, so…

Python : why a method from super class not seen?

i am trying to implement my own version of a DailyLogFile from twisted.python.logfile import DailyLogFileclass NDailyLogFile(DailyLogFile):def __init__(self, name, directory, rotateAfterN = 1, defaultM…

Extract features from last hidden layer Pytorch Resnet18

I am implementing an image classifier using the Oxford Pet dataset with the pre-trained Resnet18 CNN. The dataset consists of 37 categories with ~200 images in each of them. Rather than using the final…

Python Graphs: Latex Math rendering of node labels

I am using the following code to create a pygraphviz graph. But is it possible to make it render latex math equations (see Figure 1)? If not, is there an alternative python library that plots similar…

Given general 3D plane equation

Lets say I have a 3D plane equation:ax+by+cz=dHow can I plot this in python matplotlib?I saw some examples using plot_surface, but it accepts x,y,z values as 2D array. I dont understand how can I conv…

Spark-submit fails to import SparkContext

Im running Spark 1.4.1 on my local Mac laptop and am able to use pyspark interactively without any issues. Spark was installed through Homebrew and Im using Anaconda Python. However, as soon as I try…

Is there a Python API for event-driven Kafka consumer?

I have been trying to build a Flask app that has Kafka as the only interface. For this reason, I want have a Kafka consumer that is triggered when there is new message in the stream of the concerned to…

SWIG python initialise a pointer to NULL

Is it possible to initialise a ptr to NULL from the python side when dealing with SWIG module?For example, say I have wrapped a struct track_t in a swig module m (_m.so), I can create a pointer to the…