Skip to content
Prev 12220 / 15274 Next

Spline GARCH

Thanks a lot, Paul. Here is some reproducible code. Do you know of any 
good reference how to build up these models from scratch, e.g. 
calculating standard errors of estimates coming from a numerical 
optimizer, information criteria etc. ?



library('BB')
library('alabama')
library('nloptr')

### Spline GARCH ###

r = rnorm(13551, 0.0004, 0.016)    # pseudo returns, 13551 is the number 
of obs. of real market data I apply the model to, inputs arbitrary


### Specifying time trend


k = 3                                                       # number of 
knots
bounds = floor(1:k * 13551/k)                # partition of time horizon
bounds = c(0, bounds[1:(k-1)])
time.lin = 0:13550                           # linear time trend
time.nonlin <- matrix(rep(time.lin, k), length(time.lin), k) # quadratic 
time trend

for(i in 1:k) {                                              #
time.nonlin[,i] <- time.nonlin[,i] - bounds[i]               #
time.nonlin[which(time.nonlin[,i] < 0), i] <- 0 #
time.nonlin[, i] <- time.nonlin[, i]^2                       #
               }                                              #

time.trend = cbind(time.lin, time.nonlin)

for(i in 1:dim(time.trend)[2]) time.trend[,i] <- 
time.trend[,i]/time.trend[dim(time.trend)[1], i]    # normalizing 
between 0 and 1

head(time.trend)
tail(time.trend)




### Spline function

splgarch <- function(para) {
mu <- para[1]
omega <- para[2]
alpha <- para[3]
beta <- para[4]
cc <- para[5]
w <- para[6:(k+6)]
Tau <- cc * exp( apply(t(diag(w)%*%t(time.trend)), 1, sum) )
e2 <- (r-mu)^2
e2t <- omega + alpha * c(mean(e2), e2[-length(r)]) / Tau^2
s2 <- filter(e2t, beta, "recursive", init = mean(e2))
sig2 <- s2 * Tau
0.5*sum( log(2*pi) + log(sig2) + e2/sig2) }



### Spline parameter initialization
mu <- mean(r)
small <- 1e-6
alpha <- 0.1
beta <- 0.8
omega <- (1-alpha-beta)
para <- c(mu, omega, alpha, beta, 1, rep(small, length(4:(k+4))))

lo <- c(-10*abs(mu), small, small, small, rep(-10, length(3:(k+4))))
hi <- c(10*abs(mu), 100*abs(mu), 1-small, 1-small, rep(10, length(3:(k+4))))



### Spline optimization

fit <- nlminb(start = para, objective = splgarch, lower = lo, upper = 
hi, control = list(x.tol = 1e-8,trace=0))
names(fit$par) <- c('alpha', 'beta', 'c', paste('w', sep = '', 0:k))
round(fit$par, 6)

fit.hessian = hessian(splgarch, fit$par, method="complex")
fit.hessian









R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United 
States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

other attached packages:
[1] nloptr_0.9.6      alabama_2011.9-1  numDeriv_2012.9-1 BB_2014.1-1



Am 2/7/2014 10:28 AM, schrieb Paul Gilbert: