我试图将多元t分布拟合到R中的CRSPday数据。但优化失败,每次都有非有限的有限差分值[11]误差。有人可以建议我做错了什么。
基本上我将平均值,方差/相关性和DF作为参数并进行优化。
我尝试了几次黑客攻击:改变边界等,使用BFGS但没有任何工作
代码段:
mtfit <- function(series){
loglik <- function(par) {
mean <- par[1:4]
rho12 <- par[5]; rho13 <- par[6]; rho14 <- par[7]
rho23 <- par[8]; rho24 <- par[9]; rho34 <- par[10]
var1 <- par[11]; var2 <- par[12]; var3 <- par[13]; var4 <- par[14]
nu <- par[15]
cov12 <- rho12*sqrt(var1*var2); cov13 <- rho13*sqrt(var1*var3); cov14 <-
rho14*sqrt(var1*var4)
cov23 <- rho23*sqrt(var2*var3); cov24 <- rho24*sqrt(var2*var4); cov34 <-
rho34*sqrt(var3*var4)
covar <-matrix(c(var1,cov12,cov13,cov14,cov12,var2,cov23,cov24,cov13,cov23,var3,cov34,
cov14,cov24,cov34,var4),4,4)
f <- -sum(log(dmvt(x=series, delta=mean, sigma=covar, df=nu, log=FALSE)))
f
}
cov1 <- c(1e-2,1e-2,1e-2,1e-2,1e-2,1e-2,1e-10,1e-10,1e-10,1e-10)
cov2 <- c(0.99,0.99,0.99,0.99,0.99,0.99,1e-3,1e-3,1e-3,1e-3)
lower <- append(append(c(-1,-1,-1,-1),cov1),2.1)
upper <- append(append(c(0.01,0.001,0.001,0.001),cov2),7)
start <- lower
results <- optim(start, loglik, method = "L-BFGS-
B",lower=lower,upper=upper,hessian=T)
return(results)
}
fit_mine <- mtfit(CRSPday[,c(4:7)])
答案 0 :(得分:1)
找到解决方案,从而分享。基本上使用cholesky分解而不是相关来表示协方差矩阵。在这种情况下,边界不会导致梯度误差。
mtfit <- function(series){
loglik <- function(par) {
mean <- par[1:4]
A <-
matrix(c(par[5],par[6],par[7],par[8],0,par[9],par[10],par[11],0,0,par[12],
par[13],0,0,0,par[14]),nrow=4,byrow=T)
covar <- t(A)%*%A
f <- -sum(log(dmvt(x=series, delta=mean, sigma=covar, df=nu, log=FALSE)))
f
}
A <- chol(cov(series))
cov1 <- c(-.1,-.1,-.1,-.1,-.1,-.1,-.1,-.1,-.1,-.1)
cov2 <- c(.1,.1,.1,.1,.1,.1,.1,.1,.1,.1)
lower <- append(append(c(-0.02,-0.02,-0.02,-0.02),cov1),2.1)
upper <- append(append(c(0.02,0.02,0.02,0.02),cov2),15)
start <-
as.vector(c(apply(series,2,mean),A[1,1],A[1,2],A[1,3],A[1,4],A[2,2],
A[2,3],A[2,4],A[3,3],A[3,4],A[4,4],4))
results <- optim(start, loglik, method = "L-BFGS-B",lower=lower,
upper=upper,hessian=T)
return(results)
}
fit_mine <- mtfit(CRSPday[,c(4:7)])