python - How to compute volatility (standard deviation) in rolling window in Pandas -
i have time series "ser" , want compute volatilities (standard deviations) rolling window. current code correctly in form:
w=10 timestep in range(length): subser=ser[timestep:timestep+w] mean_i=np.mean(subser) vol_i=(np.sum((subser-mean_i)**2)/len(subser))**0.5 vollist.append(w_i)
this seems me inefficient. pandas have built-in functionality doing this?
it looks looking series.rolling
. can apply std
calculations resulting object:
roller = ser.rolling(w) vollist = roller.std(ddof=0)
if don't plan on using rolling window object again, can write one-liner:
vollist = ser.rolling().std(ddof=0)
keep in mind ddof=0
necessary in case because normalization of standard deviation len(ser)-ddof
, , ddof
defaults 1
in pandas.
Comments
Post a Comment