问题描述
我正在研究有条件多元化收益(CDB - Christoffersen 等人,2014 年),为此我需要开发 DECO GARCH 模型。不幸的是,由于我缺乏代数,我发现很难从 DCC GARCH 模型的条件相关矩阵中计算出等相关矩阵。
总结: 我有 R 矩阵(动态条件相关矩阵) 从这个矩阵我需要计算动态等相关矩阵 DECO(检查方法)
有人可以帮我吗?不介意语言,可以是 Python、R 或 Stata。 我将在 python 中发布代码,但欢迎在 R 中提供示例或解释。提前谢谢你,我真的很挣扎。回复:https://stats.stackexchange.com/questions/328510/how-to-compute-conditional-correlation-matrix-by-using-standardized-residuals-an
import pandas as pd
import numpy as np
import statsmodels.api as sm
import plotly.express as px
import plotly.figure_factory as ff
from arch import arch_model
from ipywidgets import HBox,VBox,Dropdown,Output
from scipy.optimize import fmin,minimize
from scipy.stats import t
from scipy.stats import norm
from math import inf
from IPython.display import display
import bs4 as bs
import requests
import yfinance as yf
import datetime
def vecl(matrix):
lower_matrix = np.tril(matrix,k=-1)
array_with_zero=np.matrix(lower_matrix).A1
array_without_zero = array_with_zero[array_with_zero!=0]
return array_without_zero
def garch_t_to_u(rets,res):
mu = res.params['mu']
nu = res.params['nu']
est_r = rets - mu
h = res.conditional_volatility
std_res = est_r / np.sqrt(h)
udata = t.cdf(std_res,nu)
return udata
def loglike_norm_dcc_copula(theta,udata):
N,T = np.shape(udata)
llf = np.zeros((T,1))
trdata = np.array(norm.ppf(udata).T,ndmin=2)
Rt,veclRt = dcceq(theta,trdata)
for i in range(0,T):
llf[i] = -0.5* np.log(np.linalg.det(Rt[:,:,i]))
llf[i] = llf[i] - 0.5 * np.matmul(np.matmul(trdata[i,:],(np.linalg.inv(Rt[:,i]) - np.eye(N))),trdata[i,:].T)
llf = np.sum(llf)
return -llf
def dcceq(theta,trdata):
T,N = np.shape(trdata)
a,b = theta
if min(a,b)<0 or max(a,b)>1 or a+b > .999999:
a = .9999 - b
Qt = np.zeros((N,N,T))
Qt[:,0] = np.cov(trdata.T)
Rt = np.zeros((N,T))
veclRt = np.zeros((T,int(N*(N-1)/2)))
Rt[:,0] = np.corrcoef(trdata.T)
for j in range(1,T):
Qt[:,j] = Qt[:,0] * (1-a-b)
Qt[:,j] + a * np.matmul(trdata[[j-1]].T,trdata[[j-1]])
Qt[:,j] + b * Qt[:,j-1]
Rt[:,j] = np.divide(Qt[:,j],np.matmul(np.sqrt(np.array(np.diag(Qt[:,j]),ndmin=2)).T,np.sqrt(np.array(np.diag(Qt[:,ndmin=2))))
for j in range(0,T):
veclRt[j,:] = vecl(Rt[:,j].T)
return Rt,veclRt
model_parameters = {}
udata_list = []
def run_garch_on_return(rets,udata_list,model_parameters):
for x in rets:
am = arch_model(rets[x],dist = 't',rescale=False)
short_name = x
model_parameters[short_name] = am.fit(disp='off')
udata = garch_t_to_u(rets[x],model_parameters[short_name])
udata_list.append(udata)
return udata_list,model_parameters
#Import returns
ret = pd.read_excel('URL',index_col=0,engine='openpyxl')
#Scale returns
returns = ret*100
#Obtain Parameters
udata_list,model_parameters = run_garch_on_return(returns,model_parameters)
#set up dcc model
cons = ({'type': 'ineq','fun': lambda x: -x[0] -x[1] +1})
bnds = ((0,0.5),(0,0.9997))
#Minimize log likelihood
%time opt_out = minimize(loglike_norm_dcc_copula,np.array([0.01,0.95]),args = (udata_list,),bounds=bnds,constraints=cons)
print(opt_out.success)
print(opt_out.x)
llf = loglike_norm_dcc_copula(opt_out.x,udata_list)
#obtain Rt
trdata = np.array(norm.ppf(udata_list).T,ndmin=2)
Rt,veclRt = dcceq(opt_out.x,trdata)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)