How to compute volatility (standard deviation) in rolling window in Pandas

前端 未结 4 2168
醉话见心
醉话见心 2021-02-08 04:23

I have a time series \"Ser\" and I want to compute volatilities (standard deviations) with a rolling window. My current code correctly does it in this form:

w=10         


        
4条回答
  •  忘掉有多难
    2021-02-08 04:43

    It looks like you are looking for Series.rolling. You can apply the std calculations to the resulting object:

    roller = Ser.rolling(w)
    volList = roller.std(ddof=0)
    

    If you don't plan on using the rolling window object again, you can write a one-liner:

    volList = Ser.rolling(w).std(ddof=0)
    

    Keep in mind that ddof=0 is necessary in this case because the normalization of the standard deviation is by len(Ser)-ddof, and that ddof defaults to 1 in pandas.

提交回复
热议问题