Created
October 12, 2014 18:11
-
-
Save mbusigin/55a1687715a402e9e7d9 to your computer and use it in GitHub Desktop.
Relating Future Volatility with Realized Volatility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getSymbols("^GSPC", from="1950-01-01") | |
p = 10 | |
a = na.omit(merge( volatility(GSPC, n=p)*100, lag(volatility(GSPC, n=p), k=-p)*100 )) | |
names(a) = c("hv", "rv") | |
medianvalues = c() | |
meanvalues = c() | |
maxvalues = c() | |
minvalues = c() | |
keys = c() | |
for ( x in seq( 0, max(a$hv), 2 ) ) | |
{ | |
x2 = x + 2 | |
y = median(a[a$hv >= x & a$hv <x2, 2]) | |
if ( !is.na(y) && y != -Inf && y != Inf ) | |
{ | |
y = median(a[a$hv >= x & a$hv <x2, 2]) | |
medianvalues = c(medianvalues, y) | |
y = mean(a[a$hv >= x & a$hv <x2, 2]) | |
meanvalues = c(meanvalues, y) | |
y = min(a[a$hv >= x & a$hv <x2, 2]) | |
minvalues = c(minvalues, y) | |
y = max(a[a$hv >= x & a$hv <x2, 2]) | |
maxvalues = c(maxvalues, y) | |
keys = c(keys, x) | |
} | |
} | |
buckets = c(keys, medianvalues) | |
bucketm = na.omit(matrix(buckets, nrow=length(keys))) | |
scatter.smooth(bucketm, xlab=paste(p, "-day Annualized Historical Volatility", sep=""), ylab=paste(p, "-day Annualized Future Volatility", sep=""), pch=16, col="dodgerblue3") | |
m = lmrob(bucketm[,2] ~ bucketm[,1]) | |
abline(m, col="dodgerblue3") | |
median_equation = paste("y = ", round(m$coefficients[1], 2), " + x(", round(m$coefficients[2], 2), ")", sep="") | |
buckets = c(keys, maxvalues) | |
bucketm = na.omit(matrix(buckets, nrow=length(keys))) | |
points(bucketm, col="red", pch=13) | |
m = lmrob(bucketm[,2] ~ bucketm[,1]) | |
abline(m, col="red") | |
max_equation = paste("y = ", round(m$coefficients[1], 2), " + x(", round(m$coefficients[2], 2), ")", sep="") | |
buckets = c(keys, minvalues) | |
bucketm = na.omit(matrix(buckets, nrow=length(keys))) | |
points(bucketm, col="green", pch=13) | |
m = lmrob(bucketm[,2] ~ bucketm[,1]) | |
abline(m, col="green") | |
min_equation = paste("y = ", round(m$coefficients[1], 2), " + x(", round(m$coefficients[2], 2), ")", sep="") | |
legend("bottomright", | |
c(paste("Median:", median_equation), paste("Max:", max_equation), paste("Min:", min_equation)), | |
col=c("dodgerblue3", "red", "green"), | |
lty=1, | |
pch=c(16, 13, 13) , | |
cex=.7) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment