r - 如何在 r 中实现分数驱动的 Copula 模型
问题描述
我想实现一个基于Creal et all (2013)的 GAS 框架的动态 copula 。我从一个双变量学生的copula 开始,并考虑到 copula 参数的更新方程是:
copula 的对数密度分数在哪里
,我为 copula 得出这个分数(如Ayala 和 Blazsek https://www.tandfonline.com/doi/full/10.1080/1351847X.2018.1464488?src=recsys,这里有也是copula密度函数):
我的问题是,当我尝试优化最大似然参数估计时,不会以任何方式收敛。不知道是不是参数约束相关的问题,还是我在函数中插入的循环判断错误。这是代码
loglik_t = function(param) {
dfactor = numeric()
rho = numeric()
score = numeric()
tau = cor(u_sp, u_eu, method = "kendall")
dfactor[1] = tau
tu = qt(u_sp, nu)
tv = qt(u_eu, nu)
omega = param[1]
dA = exp(param[2])
dB = exp(param[3])/(1 + exp(param[3]))
x2 = log(gamma((nu + 2)/2)) + log(gamma(nu/2)) - log(digamma((nu + 1)/2))
LL = 0
for (i in 1:length(u_sp)){
rho[i] = sin((pi/2)*dfactor[i])
#log-likelihood for observation i
x1 = -0.5*log(1 - rho[i]^2)
x3 =((-nu + 2)/2)*log(1 + (tu[i]^2+tv[i]^2-2*rho[i]*tu[i]*tv[i])/(nu*(1-rho[i]^2)))
x4 = ((-nu + 1)/2)*log(1 + tu[i]^2/nu) + ((-nu + 1)/2)*log(1 + tv[i]^2/nu)
c_t = x1 + x2 + x3 - x4
LL = LL + c_t
#GAS dynamic
x1score = rho[i]/(1 - rho[i]^2)
x2score = (nu + 2)/(rho[i]^2 - 1)
x3score = rho[i]*(tu[i]^2 + tv[i]^2) - (rho[i]^2 + 1)*tu[i]*tv[i]
x4score = tu[i]^2 + tv[i]^2 - 2*rho[i]*tu[i]*tv[i] - nu*(rho[i]^2 - 1)
score[i] = x1score + x2score*(x3score/x4score)
dfactor[i + 1] = omega + dA*score[i] + dB*rho[i]
}
return(list(LL, rho, score))
}
n = length(u_sp)
qlogL.hat = -10^10
for (i in (1:10)) {
# Initial values
start.theta = runif(3, min = 0.01, max = 0.9999)
qml.fit = optim(start.theta, fn = function(x) loglik_t(x)[[1]],
hessian = TRUE, method = "L-BFGS-B",
lower = c(-2,0.0001,0.0001),
upper = c(0.9999, 0.999, 0.9999),
control = list(trace = 1, fnscale = -1))
if (qml.fit$value > qlogL.hat) {
theta.hat = qml.fit$par
inv.J.hat = solve(-qml.fit$hessian/(n-1))
jacobian.hat = pracma:: jacobian(loglik_t1,theta.hat)
I.hat = (t(jacobian.hat) %*% jacobian.hat)/(n-1)
sd.hat = sqrt(diag(inv.J.hat %*% I.hat %*% inv.J.hat)/(n-1))
qlogL.hat = qml.fit$value
i
}
}
解决方案
推荐阅读
- r - R rowSums 使用 mutate 和 for 循环的多组变量,通过变量名的前缀
- sql - 透视行取消透视多列
- c++ - 如何从点找到平均线?
- android - 降序数组列表
- android - 为什么我无法在颤振绘图应用程序中绘制已擦除的区域?
- javascript - 在文件内容更改时自动执行 js 函数
- python - ModelForm 看不到我的属性字段,表单 ProfileForm 需要更新
- python - 使用 Google API 在 Google 表格中通过 Python 创建新标签
- javascript - 如何从 html 页面调用 react 道具
- python - 为什么在 python 中下载图像时必须同时保存 png 和 txt 文件?