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
for 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 to me very inefficient. Does Pandas have built-in functionality for doing something like this?
Typically, [finance-type] people quote volatility in annualized terms of percent changes in price.
Assuming you have daily prices in a dataframe
df
and there are 252 trading days in a year, something like the following is probably what you want:df.pct_change().rolling(window_size).std()*(252**0.5)
It looks like you are looking for
Series.rolling
. You can apply thestd
calculations to the resulting object:If you don't plan on using the rolling window object again, you can write a one-liner:
Keep in mind that
ddof=0
is necessary in this case because the normalization of the standard deviation is bylen(Ser)-ddof
, and thatddof
defaults to1
in pandas.Here's one NumPy approach -
Sample run -
Runtime test
Loopy approach -
Timings -
A speedup of close to
7000x
there with the two vectorized approaches over the loopy one!"Volatility" is ambiguous even in a financial sense. The most commonly referenced type of volatility is realized volatility which is the square root of realized variance. The key differences from the standard deviation of returns are:
There are a variety of methods for computing realized volatility; however, I have implemented the two most common below: