我想手动编程一个滚动的风险价值。所以我不想使用 PerformanceAnalytics 包中的 VaR。我想根据时间序列的对数返回进行计算。输入:
getSymbols('^GDAXI', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-01")
GDAXI.DE=GDAXI[ , "GDAXI.Close"]
log_r1=diff(log(GDAXI.DE)) #log_r1=data
alpha=0.95
VaR 函数:
VatR=function(data, alpha)
{
x=diff(log(data))
mu=mean(x)
sigma=sqrt(var(x))
quant=qnorm(alpha, mean=0, sd=1)
vatr=tail(data,n=1)*(1-exp((-sigma)*quant+mu))
}
data=GDAXI.DE
alpha=0.95
t=125
l=(-1)*diff(data) #if GDAXI used code must be changed here diff
loss=c(0,l)
ValueatRisk=matrix(rep(0),length(data),1)
violations=matrix(rep(0),length(data),1)
for(i in (t+1):length(data))
{
ValueatRisk[i]=VatRnorm(data[(i-t):(i-1)] ,alpha) #failure source
violations[i]=(loss[i] > ValueatRisk[i])
}
outputtheo=(1-alpha)*(length(data)-t)
print(outputtheo)
outputreal=sum(violations)
print(outputreal)
我想结合这些图形。好像是缩放问题,我试过qplot、ggplot等都没有成功。
graph1=plot(loss[(t+1):length(data)], type="l", col="blue")
graph2=plot(ValueatRisk[(t+1):length(data)], type="l", col="red")
如何将它们聚集在一个情节中?