此函数需要一段时间才能运行。所以,我想知道如何提高它的性能。
这个函数的目的是计算几个gjr garch dcc回归,然后将标准差和相关性存储在一个列表中。
sigma_function<-function(index_ret, ret){
sigma<-data.frame(Date=index_ret[,1])
rho<-data.frame(Date=index_ret[,1])
for (i in c(2:ncol(ret))){
x<-data.frame(index_ret, ret[,i])
gjrgarch.spec <- ugarchspec(variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)), mean.model = list(armaOrder = c(0,0), include.mean = TRUE), distribution.model = "norm")
dcc.gjrgarch.spec = dccspec(uspec = multispec(replicate(2,gjrgarch.spec)), dccOrder = c(1, 1), distribution = "mvnorm")
dcc.fit <- dccfit(dcc.gjrgarch.spec, data = na.omit(x[,-1]))
h<-dcc.fit@model[["sigma"]] #this are the conditional standard deviations
h_1<-data.frame(na.omit(x),h)
names(h_1)[c(4,5)]<-c(paste("sigma_rm",i), paste("sigma",names(ret)[i]))
sigma<-merge(sigma,h_1[,c(1,4,5)], all = TRUE, by="Date")
p<-dcc.fit@mfit$R #this is the conditional correlation matrices (3D matrices)
#each matrix corresponds to one date
rho_1<-c()
for (j in c(1:nrow(na.omit(x)))){
rho_1[j]<-p[[j]][1,2] #extract the correlation between the index and the stock for each date
}
p_1<-data.frame(na.omit(x),rho_1)
names(p_1)[4]<-paste("rho",i)
rho<-merge(rho,p_1[,c(1,4)], all = TRUE, by="Date")
}
return(list(sigma, rho))
}
避免与merge
在for
环中生长对象。相反,可以考虑用lapply
构建一个对象列表,以便在循环外进行链合并。
sigma_function <- function(index_ret, ret){
# COLLECT RESULTS IN A LIST
results_list <- lapply(2:ncol(ret), function(i){
x <- data.frame(Date=index_ret, ret[,i])
gjrgarch.spec <- ugarchspec(
variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0,0), include.mean = TRUE),
distribution.model = "norm"
)
dcc.gjrgarch.spec = dccspec(
uspec = multispec(replicate(2,gjrgarch.spec)),
dccOrder = c(1, 1),
distribution = "mvnorm"
)
dcc.fit <- dccfit(
dcc.gjrgarch.spec, data = na.omit(x[,-1])
)
h <- dcc.fit@model[["sigma"]] #conditional standard deviations
h_df <- data.frame(na.omit(x), h)
names(h_df)[c(4,5)] <- c(paste0("sigma_rm",i), paste0("sigma_",names(ret)[i]))
p <- dcc.fit@mfit$R #conditional correlation matrices (3D matrices)
# each matrix corresponds to one date
x_rows <- nrow(na.omit(x))
rhos <- vector(size=x_rows, mode="numeric") # PRE-DEFINE VECTOR LENGTH AND TYPE
for (j in 1:x_rows){
rhos[j] <- p[[j]][1,2] #extract the correlation between the index and the stock for each date
}
rho_df <- data.frame(na.omit(x), rhos)
names(rho_df)[4] <- paste0("rho",i)
# RETURN NAMED LIST
return(list(sigma=h_df[,c(1,4,5)], rho=rho_df[,c(1,4)]))
})
# CHAIN MERGE OUTSIDE LOOP
sigma_df <- Reduce(
function(x, y) merge(x, y, all=TRUE, by="Date"),
lapply(results_list, "[", "sigma")
)
rho_df <- Reduce(
function(x, y) merge(x, y, all=TRUE, by="Date"),
lapply(results_list, "[", "rho")
)
# RETURN NAMED LIST
return(list(sigma=sigma_df, rho=rho_df))
}