| 1 |
# Example n. 7 |
| 2 |
|
| 3 |
# Compares the results |
| 4 |
# between Gretl (native and gig) and R (fGarch). |
| 5 |
|
| 6 |
|
| 7 |
set verbose off |
| 8 |
|
| 9 |
include gig.gfn |
| 10 |
open b-g.gdt |
| 11 |
genr rr = Y |
| 12 |
setinfo rr -d "weekly returns" |
| 13 |
|
| 14 |
# Estimate a GARCH model natively |
| 15 |
set stopwatch |
| 16 |
garch 1 1 ; rr const rr(-1) --quiet |
| 17 |
t0 = $stopwatch |
| 18 |
series u0 = $uhat |
| 19 |
series s0 = sqrt($h) |
| 20 |
|
| 21 |
matrix cf = $coeff |
| 22 |
matrix se = $stderr |
| 23 |
matrix tratios = cf ./ se |
| 24 |
matrix pvalues = 2*pvalue(n, abs(tratios)) |
| 25 |
matrix gretl_model = cf ~ se ~ tratios ~ pvalues |
| 26 |
|
| 27 |
# Estimate a GARCH model via gig |
| 28 |
set stopwatch |
| 29 |
mod = gig_setup(rr, 1, const, null, 1) |
| 30 |
mod.vcvtype = 1 # use Hessian for compatibility |
| 31 |
gig_estimate(&mod, 0) |
| 32 |
t1 = $stopwatch |
| 33 |
|
| 34 |
# Extract the info from the results matrix |
| 35 |
series u1 = mod.uhat |
| 36 |
series s1 = sqrt(mod.h) |
| 37 |
matrix cf = mod.coeff |
| 38 |
matrix cf = cf[mod.active] |
| 39 |
matrix se = sqrt(diag(mod.vcv)) |
| 40 |
matrix tratios = cf ./ se |
| 41 |
matrix pvalues = 2*pvalue(n, abs(tratios)) |
| 42 |
|
| 43 |
# Pack all the above in a matrix |
| 44 |
matrix gig_model = cf ~ se ~ tratios ~ pvalues |
| 45 |
|
| 46 |
# Starts R with data from Gretl |
| 47 |
# Loads the fGarch library |
| 48 |
# Performs a GARCH(1,1) estimation |
| 49 |
# Sends back the results |
| 50 |
set stopwatch |
| 51 |
foreign language=R --send-data |
| 52 |
library(fGarch) |
| 53 |
rr <- gretldata[-1,"rr"] |
| 54 |
mod <- garchFit(~arma(1,0)+garch(1,1), data=rr, trace=F) |
| 55 |
coef <- mod@fit$matcoef |
| 56 |
gretl.export(coef) |
| 57 |
uh <- as.data.frame(cbind([email protected], mod@residuals)) |
| 58 |
names(uh) <- c("csd", "resid") |
| 59 |
gretl.export(uh) |
| 60 |
end foreign |
| 61 |
t2 = $stopwatch |
| 62 |
R_model = mread("@dotdir/coef.mat") |
| 63 |
smpl +1 ; |
| 64 |
append @dotdir/uh.csv --quiet |
| 65 |
smpl full |
| 66 |
|
| 67 |
# Compare results |
| 68 |
printf "Results via gretl (native)\n" |
| 69 |
print gretl_model |
| 70 |
printf "Results via gretl/gig\n" |
| 71 |
print gig_model |
| 72 |
printf "Results via R/fGarch\n" |
| 73 |
print R_model |
| 74 |
summary u0 u1 resid s0 s1 csd --simple |
| 75 |
|
| 76 |
printf "Performance:\n" |
| 77 |
printf "\t native: %10.3f seconds\n", t0 |
| 78 |
printf "\t gig: %10.3f seconds\n", t1 |
| 79 |
printf "\tR/fGarch: %10.3f seconds\n", t2 |