Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

r - Rugarch - Estimate models for various error distributions and input data

I am an R programming beginner.

I need to repeat the code of the following post answer Adding EGARCH flavor into a loop over fGARCH-flavor models over two different arguments of the estimation function and in the following order.

The first argument to reiterate over is the error distribution. Here is the vector which is supposed to change in the model estimation command (see linked post):

error_distribution=c("norm","std","ged")

model_2012=ugarchspec(variance.model = list(model="fGARCH", submodel = j, garchOrder = c(1, i)),mean.model = list(armaOrder = c(0, 0)),distribution.model=z)

modelfit_2012=ugarchfit(spec=model_2012, data=bchain_2012_logreturns, solver="hybrid") 

The second argument to reiterate over is the input data which are given by five vectors containing log-returns for years 2012-2016 like the following:

bchain_2012_logreturns
bchain_2013_logreturns 
bchain_2014_logreturns
bchain_2015_logreturns  
bchain_2016_logreturns 

The expected result is a single dataframe which ranks 45 GARCH models every year according to the respective AICs. See the following output:

   model_name error_distribution order         AIC model_name error_distribution order         AIC model_name error_distribution order         AIC
31     eGARCH                ged (1,1) -4.75865287     TGARCH                ged (1,1) -3.08102463     eGARCH                ged (1,1) -4.05490216
33      GARCH                ged (1,1) -4.75652725    AVGARCH                ged (1,1) -3.08045023     TGARCH                ged (1,1) -4.05374702
43    NAGARCH                ged (1,1) -4.75490561    AVGARCH                std (1,1) -3.07788929    AVGARCH                ged (1,1) -4.05331759
39   GJRGARCH                ged (1,1) -4.75278902     TGARCH                std (1,1) -3.07725803      GARCH                ged (1,1) -4.05294280
41     NGARCH                ged (1,1) -4.75108028     NGARCH                ged (1,1) -3.07641387     NGARCH                ged (1,1) -4.05211362
37     TGARCH                ged (1,1) -4.74863010     APARCH                ged (1,1) -3.07511457    NAGARCH                ged (1,1) -4.05184210
45     APARCH                ged (1,1) -4.74737629   GJRGARCH                ged (1,1) -3.07409228   GJRGARCH                ged (1,1) -4.05104571
35    AVGARCH                ged (1,1) -4.74587528      GARCH                ged (1,1) -3.07400130     APARCH                ged (1,1) -4.05064538
16     eGARCH                std (1,1) -4.69989618    NAGARCH                ged (1,1) -3.07318299    AVGARCH                std (1,1) -4.04762280
18      GARCH                std (1,1) -4.69719283     APARCH                std (1,1) -3.07315401     TGARCH                std (1,1) -4.04601307
28    NAGARCH                std (1,1) -4.69405444     eGARCH                std (1,1) -3.07269706     eGARCH                std (1,1) -4.04406967
26     NGARCH                std (1,1) -4.69404219     NGARCH                std (1,1) -3.06768570    NAGARCH                std (1,1) -4.04226303
24   GJRGARCH                std (1,1) -4.69226266     eGARCH                ged (1,1) -3.06481282     APARCH                std (1,1) -4.04141302
30     APARCH                std (1,1) -4.68892186    NAGARCH                std (1,1) -3.05214076   GJRGARCH                std (1,1) -4.03922662
20    AVGARCH                std (1,1) -4.68635636   GJRGARCH                std (1,1) -3.04994480     NGARCH                std (1,1) -4.03902956
22     TGARCH                std (1,1) -4.68545602      GARCH                std (1,1) -3.04671032      GARCH                std (1,1) -4.03587846
17      GARCH                std (1,0) -4.63367371     NGARCH                std (1,0) -2.96723963     NGARCH                std (1,0) -4.02141342
27    NAGARCH                std (1,0) -4.63086175     TGARCH                std (1,0) -2.96703818     APARCH                std (1,0) -4.01926563
25     NGARCH                std (1,0) -4.62854627      GARCH                std (1,0) -2.96419867     TGARCH                std (1,0) -4.01739401
23   GJRGARCH                std (1,0) -4.62836367     APARCH                std (1,0) -2.96180909    NAGARCH                std (1,0) -4.00275498
21     TGARCH                std (1,0) -4.62233634    NAGARCH                std (1,0) -2.96011709      GARCH                std (1,0) -4.00197866
19    AVGARCH                std (1,0) -4.60732059   GJRGARCH                std (1,0) -2.95925904   GJRGARCH                std (1,0) -3.99942924
29     APARCH                std (1,0) -4.60040491    AVGARCH                std (1,0) -2.93447870    AVGARCH                std (1,0) -3.97651737
1      eGARCH               norm (1,1) -4.37707925    AVGARCH               norm (1,1) -2.84142521     eGARCH               norm (1,1) -3.89143994
5     AVGARCH               norm (1,1) -4.36877118   GJRGARCH               norm (1,1) -2.83307523      GARCH               norm (1,1) -3.88777006
7      TGARCH               norm (1,1) -4.35992439      GARCH               norm (1,1) -2.83172032     NGARCH               norm (1,1) -3.88227550
9    GJRGARCH               norm (1,1) -4.35675975    NAGARCH               norm (1,1) -2.83096966    NAGARCH               norm (1,1) -3.88075879
13    NAGARCH               norm (1,1) -4.35669056     NGARCH               norm (1,1) -2.82936979     TGARCH               norm (1,1) -3.87732087
15     APARCH               norm (1,1) -4.35639445     APARCH               norm (1,1) -2.82908704     APARCH               norm (1,1) -3.87128476
3       GARCH               norm (1,1) -4.34457950     TGARCH               norm (1,1) -2.82664751     NGARCH               norm (1,0) -3.86962700
11     NGARCH               norm (1,1) -4.34333420     eGARCH               norm (1,1) -2.79347854   GJRGARCH               norm (1,1) -3.86727511
8    GJRGARCH               norm (1,0) -4.09031551     NGARCH               norm (1,0) -2.71997549    AVGARCH               norm (1,1) -3.86704031
2       GARCH               norm (1,0) -4.08846458      GARCH               norm (1,0) -2.71897732      GARCH               norm (1,0) -3.79428684
14     APARCH               norm (1,0) -4.08213105     APARCH               norm (1,0) -2.71487076    NAGARCH               norm (1,0) -3.79161679
10     NGARCH               norm (1,0) -4.06826972    NAGARCH               norm (1,0) -0.87217161    AVGARCH               norm (1,0) -3.73878504
4     AVGARCH               norm (1,0) -3.97389330    AVGARCH               norm (1,0) -0.30823178     APARCH               norm (1,0) -3.73584478
12    NAGARCH               norm (1,0) -3.95954524     TGARCH                ged (1,0)  0.03351648     TGARCH                ged (1,0)  0.03351648
6      TGARCH               norm (1,0) -1.08711502    AVGARCH                ged (1,0)  0.03901099    AVGARCH                ged (1,0)  0.03901099
36     TGARCH                ged (1,0)  0.03342466     APARCH                ged (1,0)  0.03901099     APARCH                ged (1,0)  0.03901099
34    AVGARCH                ged (1,0)  0.03890411   GJRGARCH               norm (1,0)  1.05494307   GJRGARCH               norm (1,0)  3.06619678
44     APARCH                ged (1,0)  0.03890411     TGARCH               norm (1,0)  1.66969764     TGARCH               norm (1,0) 13.23452126
42    NAGARCH                ged (1,0)  8.03028586    NAGARCH                ged (1,0) 30.06384244   GJRGARCH                ged (1,0) 32.95929809
32      GARCH                ged (1,0)  8.13606127      GARCH                ged (1,0) 30.29489274    NAGARCH                ged (1,0) 33.19740991
40     NGARCH                ged (1,0)  8.14154072     NGARCH                ged (1,0) 30.30038725      GARCH                ged (1,0) 33.20214247
38   GJRGARCH                ged (1,0)  8.16366093   GJRGARCH                ged (1,0) 30.34334891     NGARCH                ged (1,0) 33.20763698
   model_name error_distribution order          AIC model_name error_distribution order         AIC
31     APARCH                ged (1,1)  -4.43376898      GARCH                ged (1,1) -5.23989609
33     NGARCH                ged (1,1)  -4.43125597    NAGARCH                ged (1,1) -5.23846855
43      GARCH                ged (1,1)  -4.43024900   GJRGARCH                ged (1,1) -5.23694535
39   GJRGARCH                ged (1,1)  -4.42919245     NGARCH                ged (1,1) -5.23441634
41    NAGARCH                std (1,1)  -4.42526122     APARCH                ged (1,1) -5.23278501
37    AVGARCH                ged (1,1)  -4.41838285     eGARCH                std (1,1) -5.23184947
45     eGARCH                ged (1,1)  -4.41674667    AVGARCH                std (1,1) -5.23141871
35   GJRGARCH                std (1,1)  -4.41582574     NGARCH                std (1,1) -5.23124651
16     APARCH                std (1,1)  -4.41051921     eGARCH                ged (1,1) -5.23097095
18      GARCH                std (1,1)  -4.40684699    NAGARCH                std (1,1) -5.22847769
28     eGARCH                std (1,1)  -4.40379536     TGARCH                ged (1,1) -5.22758361
26     NGARCH                std (1,1)  -4.40146116      GARCH                std (1,1) -5.22727192
24     TGARCH                ged (1,1)  -4.40045970     TGARCH                std (1,1) -5.22670653
30     TGARCH                std (1,1)  -4.39302792   GJRGARCH                std (1,1) -5.22562117
20    AVGARCH                std (1,1)  -4.38983868    AVGARCH                ged (1,1) -5.22542480
22      GARCH                std (1,0)  -4.38731276    AVGARCH                std (1,0) -5.21402359
17    NAGARCH                std (1,0)  -4.38699935    NAGARCH                std (1,0) -5.20394522
27    NAGARCH                ged (1,1)  -4.38672794     APARCH                std (1,1) -5.19630862
25   GJRGARCH                std (1,0)  -4.38216336      GARCH                std (1,0) -5.18779640
23     NGARCH                std (1,0)  -4.38182166     NGARCH                std (1,0) -5.18468472
21    AVGARCH                std (1,0)  -4.37746757   GJRGARCH                std (1,0) -5.18242298
19     APARCH                std (1,0)  -4.37668704     APARCH                std (1,0) -5.17696075
29     TGARCH                std (1,0)  -4.36889276     TGARCH                std (1,0) -5.16984529
1      APARCH               norm (1,1)  -4.22868663      GARCH               norm (1,1) -4.86762096
5      NGARCH               norm (1,1)  -4.22662065   GJRGARCH               norm (1,1) -4.86355228
7       GARCH               norm (1,1)  -4.18924653     NGARCH               norm (1,1) -4.86339583
9    GJRGARCH               norm (1,1)  -4.18544590    NAGARCH               norm (1,1) -4.86270926
13    NAGARCH               norm (1,1)  -4.18480391     eGARCH               norm (1,1) -4.85324513
15     eGARCH               norm (1,1)  -4.16017862    AVGARCH               norm (1,1) -4.84896561
3      NGARCH               norm (1,0)  -4.13398631     TGARCH               norm (1,1) -4.84311948
11    AVGARCH               norm (1,1)  -4.12548973    NAGARCH               norm (1,0) -4.76807819
8      APARCH               norm (1,0)  -4.12331541     APARCH               norm (1,1) -4.75358590
2     NAGARCH               norm (1,0)  -4.11927591      GARCH               norm (1,0) -4.73585352
14     TGARCH               norm (1,1)  -4.09717462     NGARCH               norm (1,0) -4.72947512
10   GJRGARCH               norm (1,0)  -0.03847664     APARCH               norm (1,0) -4.72645043
4      TGARCH                ged (1,0)   0.03351648    AVGARCH               norm (1,0) -3.71073192
12    AVGARCH                ged (1,0)   0.03901099   GJRGARCH               nor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With expand.grid we can create combinations of such as ("bchain_2012_logreturns","norm"),("bchain_2012_logreturns","std") etc.

For each combination we invoke a custom function to calculate AIC for all models. This function is modified version of my previous solution.

Due to lack of data I cannot verify the results but this should work.

Create Combinations:

error_distribution=c("norm","std","ged")

dataList = c("bchain_2012_logreturns","bchain_2013_logreturns","bchain_2014_logreturns","bchain_2015_logreturns","bchain_2016_logreturns")

paramCombinations = expand.grid(dataList,error_distribution)

paramCombinations
#                     Var1 Var2
#1  bchain_2012_logreturns norm
#2  bchain_2013_logreturns norm
#3  bchain_2014_logreturns norm
#4  bchain_2015_logreturns norm
#5  bchain_2016_logreturns norm
#6  bchain_2012_logreturns  std

AIC Calculations:

fn_AIC_Calc = function(yearlyReturns = "bchain_2012_logreturns" ,errorDistrib = "norm" ) {

cat(paste0("Begin AIC calc with data =",deparse(substitute(yearlyReturns)),":","errorDistrib=",deparse(substitute(errorDistrib)),"
"))

yearlyReturns = eval(parse(text="yearlyReturns"))


AIC_DF = do.call(rbind,lapply(model_names,function( x ) {

   if ( x=="eGARCH" ){

   model_year_0 = ugarchspec(variance.model = list(model= x,garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)),distribution.model = errorDistrib)
   modelfit_year_0 =ugarchfit(spec=model_year_0   , data = yearlyReturns, solver="hybrid")

   DF = data.frame(model_name = x,error_distribution = errorDistrib ,order= "1,1" ,AIC = infocriteria(modelfit_year_0)[1])

   }else {

   model_year_0 = ugarchspec(variance.model = list(model="fGARCH", submodel = x, garchOrder = c(1, 0)),mean.model = list(armaOrder = c(0, 0)),,distribution.model = errorDistrib)
   model_year_1 = ugarchspec(variance.model = list(model="fGARCH", submodel = x, garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)),,distribution.model = errorDistrib)

   modelfit_year_0 =ugarchfit(spec=model_year_0   , data = yearlyReturns, solver="hybrid")
   modelfit_year_1 =ugarchfit(spec=model_year_1, data = yearlyReturns, solver="hybrid")

   DF_0 = data.frame(model_name = x,error_distribution = errorDistrib, order= "1,0" , AIC = infocriteria(modelfit_year_0)[1])
   DF_1 = data.frame(model_name = x,error_distribution = errorDistrib, order= "1,1" , AIC = infocriteria(modelfit_year_1)[1])

   DF = rbind(DF_0,DF_1)

   }

   return(DF)

}))


cat(paste0("End AIC calc with data =",deparse(substitute(yearlyReturns)),":","errorDistrib=",deparse(substitute(errorDistrib)),"
"))

}

Call function:

AIC_Aggregate = do.call(rbind,lapply(1:nrow(paramCombinations),function(x) {

#For each combination of yearly returns dataset and error distribution compute AIC for all models

yearErrorDistribDF = fn_AIC_Calc(yearlyReturns = as.character(paramCombinations[x,"Var1"]) ,errorDistrib = as.character(paramCombinations[x,"Var2"]))

return(yearErrorDistribDF)

}))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...