File: mgarch-FitFromFormula.R

package info (click to toggle)
fgarch 4052.93-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 896 kB
  • sloc: fortran: 339; ansic: 18; makefile: 14
file content (143 lines) | stat: -rw-r--r-- 4,787 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA


################################################################################
# FUNCTION:               PARAMETER ESTIMATION:
#  .gogarchFit             Fits the parameters of a GO-GARCH process
################################################################################


.gogarchFit <- 
  function(formula = ~ garch(1, 1), data, 
    init.rec = c("mci", "uev"), 
    delta = 2, 
    skew = 1, 
    shape = 4, 
    cond.dist = c("norm", "snorm", "ged", "sged", "std", "sstd", "snig", "QMLE"), 
    include.mean = TRUE, 
    include.delta = NULL, 
    include.skew = NULL, 
    include.shape = NULL, 
    leverage = NULL, 
    trace = TRUE, 
    algorithm = c("nlminb", "lbfgsb", "nlminb+nm", "lbfgsb+nm"), 
    hessian = c("ropt", "rcd"), 
    control = list(), 
    title = NULL, 
    description = NULL, 
    ...) 
{
    # A function implemented by Diethelm Wuertz
    
    # Description:
    #   Fits a GO-Garch Model using Independent Component Analysis
    
    # Arguments:
    #   The arguments are the same as for the univariate case.
    #   formula - formula for all marginal models
    #   data - multivariate timeSeries object
    #   ...
    
    # Value:
    #   S4 Object of class (univariate) fGARCH ...
    
    # Notes:
    #   This function has still a preliminary status ...
    #   This function was inspired from the contributed gogarch 
    #   package of Bernhard Pfaff.
    
    # Example:
    #   require(fEcofin); data(DowJones30)
    #   X = returns(as.timeSeries(DowJones30)); head(X)
    #   N = 5; ans = .gogarchFit(data = X[, 1:N], trace = FALSE); ans
    #   ans@h.t

    # FUNCTION:
      
    # Multivariate ?
    stopifnot(isMultivariate(data))
    
    
    # Data:
    X = data
    
    # Marginal Garch Models:
    garchControl = list(
        init.rec = init.rec, delta = delta, skew = skew, shape = shape, 
        cond.dist = cond.dist, include.mean = include.mean, 
        include.delta = include.delta, include.skew = include.skew, 
        include.shape = include.shape, leverage = leverage, 
        trace = trace, algorithm = algorithm, hessian = hessian, 
        control = control, title = title, description = description)
        
    # Compute fastICA:
    #   ... the following lines of code were borrowed from 
    #   Bernhard Pfaff's contributed package gogarch
    V <- t(X) %*% X / nrow(X)
    svd <- svd(V)
    P <- svd$u
    Dsqr <- diag(sqrt(svd$d))
    # set.seed(4711) 
    ica <- fastICA::fastICA(X, n.comp = ncol(X))
    Z <- P %*% Dsqr %*% t(P) %*% ica$W
    colnames(Z) = rownames(Z) = colnames(data)
    Y <- X %*% solve(Z)

    # Fit Marginal Garch Models:
    fit <- apply(Y, 2, function(x) do.call("garchFit", 
        c(list(formula = formula, data = x), garchControl)))
    
    # Compute Conditional Variances:
    #   ... the following lines of code were borrowed from 
    #   Bernhard Pfaff's contributed package gogarch
    H <- matrix(unlist(lapply(fit, function(x) x@h.t)), 
        ncol = ncol(X), nrow = nrow(X))
    Hdf <- data.frame(t(H))
    rownames(Hdf) <- colnames(data)
    colnames(Hdf) <- rownames(data)
    H.t <- lapply(Hdf, function(x) Z %*% diag(x) %*% t(Z))
    
    # Add Title and Description:
    if(is.null(title)) title = "ICA GO-GARCH Modelling"
    if(is.null(description)) description = description()
    
    # Result:
    ans <- new("fGARCH",
        call = as.call(match.call()),
        formula = formula,
        method = "ICA go-Garch Parmeter Estimation",
        data = c(Records = nrow(data), Instruments = ncol(data)),               
        fit = fit,
        residuals = numeric(),      
        fitted = numeric(),         
        h.t = c(Records = length(H.t), Dimension =dim(H.t[[1]])),             
        sigma.t = numeric(),         
        title = title,               
        description = description
    )
    
    # Multivariate Series:
    attr(ans@data, "data") <- data
    attr(ans@h.t, "H.t") <- H.t
    
    # Return Value:
    ans   
}


################################################################################