对于对数似然估计保持矩阵正定的约束

时间:2016-02-22 12:52:17

标签: r optimization

我正在尝试通过R中的GARCH模型(VECH(1,1))来优化对数似然函数并计算我的协方差矩阵。我的问题是,如何编制使我的协方差矩阵保持正定的约束?

我知道我的矩阵alpha + beta = Q,其中Q的特征值必须在单位圆中。但是我怎么编程呢?输入是

getSymbols('^GDAXI', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
GDAXI.DE=GDAXI[ , "GDAXI.Close"]
#S&P 500
getSymbols('^GSPC', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
GSPC=GSPC[ , "GSPC.Close"]
log_r1=diff(log(GDAXI.DE[39:2575]))
log_r2=diff(log(GSPC))
r_t=data.frame(log_r1,log_r2) 
COV=cov(r_t) #positive definit
e1=matrix(rep(0),length(r_t$log_r1),1)
e2=matrix(rep(0),length(r_t$log_r1),1)
ee=matrix(rep(0),(length(r_t$log_r1)*2),2)
e=matrix(rep(0),(length(r_t$log_r1)*2),2)
j = seq(from=1, to=((length(r_t$log_r1))*2)-1, by=2)
k = seq(from=2, to=((length(r_t$log_r1))*2), by=2)
for(i in 1:length(r_t$log_r1))
{
e1[i]=r_t$log_r1[i]-mean(r_t$log_r1)
e2[i]=r_t$log_r2[i]-mean(r_t$log_r2)
ee[(j[i]:k[i]),]=t(cbind(e1[i],e2[i]))%*%(cbind(e1[i],e2[i])) #matrix of squared residuals
}
e=cbind(e1,e2)
Sig=cov(e) #is.positvie.definit(Sig) TRUE
th=matrix(rep(0),3, 14)

需要限制的功能:

covariance=function(th, ee)
{
h_t=matrix(rep(0),length(r_t$log_r1)*2,2)
h_t[1:2,]=Sig
omega0=matrix(rep(0),3,1)
alpha0=matrix(rep(0),3,3)
beta0=matrix(rep(0),3,3)
omega=matrix(rep(0),3,1)
alpha=matrix(rep(0),3,3)
beta=matrix(rep(0),3,3)
th[1]->omega0[1,1]
th[2]->omega0[2,1]
th[3]->omega0[3,1]
th[4]->alpha0[1,1]
th[5]->alpha0[2,1]
th[6]->alpha0[3,1]
th[7]->alpha0[1,2]
th[8]->alpha0[2,2]
th[9]->alpha0[3,2]
th[10]->alpha0[1,3]
th[11]->alpha0[2,3]
th[12]->alpha0[3,3]
th[13]->beta0[1,1]
th[14]->beta0[2,1]
th[15]->beta0[3,1]
th[16]->beta0[1,2]
th[17]->beta0[2,2]
th[18]->beta0[3,2]
th[19]->beta0[1,3]
th[20]->beta0[2,3]
th[21]->beta0[3,3]
th[22]->omega[1,1]
th[23]->omega[2,1]
th[24]->omega[3,1]
th[25]->alpha[1,1]
th[26]->alpha[2,1]
th[27]->alpha[3,1]
th[28]->alpha[1,2]
th[29]->alpha[2,2]
th[30]->alpha[3,2]
th[31]->alpha[1,3]
th[32]->alpha[2,3]
th[33]->alpha[3,3]
th[34]->beta[1,1]
th[35]->beta[2,1]
th[36]->beta[3,1]
th[37]->beta[1,2]
th[38]->beta[2,2]
th[39]->beta[3,2]
th[40]->beta[1,3]
th[41]->beta[2,3]
th[42]->beta[3,3]
for(i in 1:(length(r_t$log_r1)-1))
{
h_tvech=omega+alpha%*%vech(ee[j[i]:k[i],])+beta%*%vech(h_t[j[i]:k[i],])
h_t[j[i+1]:k[i+1],]=vech2full(h_tvech)
}
return(h_t) 
}

是否可以将约束放入for循环中?我需要h_tvech为正定,以便我可以将其转换为h_t我将在LogLik函数中使用的协方差矩阵。 th []是我的优化值omega,alpha和beta的startign值和持有者。

LogLik=function(th, ee)
{
input=covariance(th, ee)
Sigma=input
error=ee
d=2
for(i in 1:(length(r_t$log_r1)-1))
{
LL=-(d/2)*log(2*pi)-(d/2)*log(det(Sigma[j[i]:k[i],]))-  (1/2)*t(error[j[i]:k[i],])*solve(Sigma[j[i]:k[i],])*error[j[i]:k[i],]
}
}

0 个答案:

没有答案