I'm relatively new to this field and would like to ask a couple of questions.
I'm doing some analysis and I would like to compare/plot VIX vs historical volatility of SPX. I have daily VIX and SPX data. So the first thing I tried to do was to compute historical volatility, I came up with the following function
from numpy import sqrt,mean,log,diff
def get_historical_volatility(df, days):
close = df['Close']
r = diff(log(close))
volatility = []
for index in range(days, len(r)):
range_r = r[index-days:days+index]
r_mean = mean(range_r)
diff_square = [(range_r[i]-r_mean)**2 for i in range(0,len(range_r))]
std = sqrt(sum(diff_square)*(1.0/(len(range_r)-1)))
volatility.append(std*sqrt(252)*100)
return volatility
Does this function look right?
How many days should I use for historical volatility calculation in order to compare it to VIX? 30 or 21 (average trading days per month)?
This is the current plot I get when plotting VIX vs Historical volatility of 30 days.
Is there any tool/data provider where could I get the historical volatility of SPX so I could compare it with the results I have got?
r_mean = 0.0
instead ofr_mean = mean(range_r)
The use of a short term mean introduces unnecessary noise into the estimate of std deviation. (3) Visually your chart looks reasonable (historical vol has been declining while vix remains elevated) $\endgroup$