[R] fSeries

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Thu Nov 11 09:42:38 CET 2004


Diethelm Wuertz wrote:

Tabs removed from the code, makes a nicer printout ....

garchSim =
function(model = list(omega = 1.0e-06, alpha = 0.1, beta = 0.8, mu = 0),
n = 100, innov = NULL, n.start = 100, start.innov = NULL, rand.gen = 
rnorm, ...)
{    
    # Doesn't work, replace the three following three lines ... 
    # if (!exists("model$alpha")) model$alpha = 0
    # if (!exists("model$beta")) model$beta = 0
    # if (!exists("model$mu")) model$mu = 0
   
    # with ...
    if (is.null(model$alpha)) model$alpha = 0
    if (is.null(model$beta)) model$beta = 0
    if (is.null(model$mu)) model$mu = 0

    max.order = max(length(model$alpha), length(model$beta))
    if (n.start < max.order)
        stop("n.start must be greater or equal max(alpha,beta)")
    if (is.null(start.innov))
        start.innov = rand.gen(n.start, ...)
    if (is.null(innov))
        innov = rand.gen(n, ...)
    h = x = z = c(start.innov, innov)
    for (i in 1:max.order) {
        h[i] = model$omega/(1 - sum(model$alpha) - sum(model$beta))
        x[i] = sqrt(h[i]) * z[i] + model$mu
    }
    n.alpha = length(model$alpha)
    n.beta = length(model$beta)
    for (i in (max.order + 1):(n.start + n)) {
        h[i] = model$omega + sum(model$alpha * x[i - (1:n.alpha)]^2) +
            sum(model$beta * h[i - (1:n.beta)])
        x[i] = sqrt(h[i]) * z[i] + model$mu
    }
    as.ts(x[-(1:n.start)])
}




More information about the R-help mailing list