Skip to content

Instantly share code, notes, and snippets.

@hugo1005
Created December 23, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugo1005/1c9845e13cba91bf7e10c72505dfcf1c to your computer and use it in GitHub Desktop.
Save hugo1005/1c9845e13cba91bf7e10c72505dfcf1c to your computer and use it in GitHub Desktop.
Fitting ARMA GARCH Models to many time series and saving the results
library(TSA)
library(forecast)
library(tseries)
garchAutoTryFit = function(
ll,
data,
trace=FALSE,
forecast.length=1,
with.forecast=TRUE,
ic="AIC",
garch.model="garch" )
{
formula = as.formula( paste( sep="",
"~ arma(", ll$order[1], ",", ll$order[2], ")+",
garch.model,
"(", ll$order[3], ",", ll$order[4], ")" ) )
fit = tryCatch( garchFit( formula=formula,
data=data,
trace=FALSE,
cond.dist=ll$dist ),
error=function( err ) TRUE,
warning=function( warn ) FALSE )
pp = NULL
if( !is.logical( fit ) ) {
if( with.forecast ) {
pp = tryCatch( predict( fit,
n.ahead=forecast.length,
doplot=FALSE ),
error=function( err ) FALSE,
warning=function( warn ) FALSE )
if( is.logical( pp ) ) {
fit = NULL
}
}
} else {
fit = NULL
}
if( trace ) {
if( is.null( fit ) ) {
cat( paste( sep="",
" Analyzing (", ll$order[1], ",", ll$order[2],
",", ll$order[3], ",", ll$order[4], ") with ",
ll$dist, " distribution done.",
"Bad model.\n" ) )
} else {
if( with.forecast ) {
cat( paste( sep="",
" Analyzing (", ll$order[1], ",", ll$order[2], ",",
ll$order[3], ",", ll$order[4], ") with ",
ll$dist, " distribution done.",
"Good model. ", ic, " = ", round(fit@fit$ics[[ic]],6),
", forecast: ",
paste( collapse=",", round(pp[,1],4) ), "\n" ) )
} else {
cat( paste( sep="",
" Analyzing (", ll[1], ",", ll[2], ",", ll[3], ",", ll[4], ") with ",
ll$dist, " distribution done.",
"Good model. ", ic, " = ", round(fit@fit$ics[[ic]],6), "\n" ) )
}
}
}
return( fit )
}
garchAuto = function(
xx,
min.order=c(0,0,1,1),
max.order=c(5,5,1,1),
trace=FALSE,
cond.dists="sged",
with.forecast=TRUE,
forecast.length=1,
arma.sum=c(0,1e9),
cores=1,
ic="AIC",
garch.model="garch" )
{
require( fGarch )
require( parallel )
len = NROW( xx )
models = list( )
for( dist in cond.dists )
for( p in min.order[1]:max.order[1] )
for( q in min.order[2]:max.order[2] )
for( r in min.order[3]:max.order[3] )
for( s in min.order[4]:max.order[4] )
{
pq.sum = p + q
if( pq.sum <= arma.sum[2] && pq.sum >= arma.sum[1] )
{
models[[length( models ) + 1]] = list( order=c( p, q, r, s ), dist=dist )
}
}
res = mclapply( models,
garchAutoTryFit,
data=xx,
trace=trace,
ic=ic,
garch.model=garch.model,
forecast.length=forecast.length,
with.forecast=TRUE,
mc.cores=cores )
best.fit = NULL
best.ic = 1e9
for( rr in res )
{
if( !is.null( rr ) )
{
current.ic = rr@fit$ics[[ic]]
if( current.ic < best.ic )
{
best.ic = current.ic
best.fit = rr
}
}
}
if( best.ic < 1e9 )
{
return( best.fit )
}
return( NULL )
}
returns_data = read.csv(file='./train_set.csv')
fund_names = colnames(returns_data)
all_results = data.frame()
# n_funds = ncol(returns_data)
n_funds = 4
for(i in 2:ncol(returns_data)) {
fund = unlist(returns_data[i], use.names=FALSE)
res = garchAuto(fund, min.order=c(0,0,1,1), max.order=c(4,4,2,2))
if (typeof(res) == typeof(NULL)) {
print(paste0("Failed for fund: ", fund_names[i]))
} else {
coeffs = res@fit$par
se = res@fit$se.coef
fund_name = fund_names[i]
coeff_frame = data.frame(coeffs, se, fund_name)
all_results = rbind(coeff_frame, all_results)
print(paste0("Completed for fund: ", fund_names[i]))
}
}
write.csv(all_results, './500_mutual_fund_returns_params.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment