【问题标题】:Rugarch - Estimate models for various error distributions and input dataRugarch - 估计各种错误分布和输入数据的模型
【发布时间】:2017-01-28 11:28:57
【问题描述】:

我是一名 R 编程初学者。

我需要在估计函数的两个不同参数上并按以下顺序重复以下帖子答案 Adding EGARCH flavor into a loop over fGARCH-flavor models 的代码。

要重申的第一个参数是错误分布。这是应该在模型估计命令中更改的向量(请参阅链接帖子):

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") 

要重申的第二个参数是输入数据,它由五个向量给出,其中包含 2012-2016 年的对数回报,如下所示:

bchain_2012_logreturns
bchain_2013_logreturns 
bchain_2014_logreturns
bchain_2015_logreturns  
bchain_2016_logreturns 

预期结果是单个数据框,每年根据各自的 AIC 对 45 个 GARCH 模型进行排名。请参阅以下输出:

   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               norm (1,0) -0.85666042
6      APARCH                ged (1,0)   0.03901099     TGARCH               norm (1,0) -0.40795391
36      GARCH               norm (1,0)   5.28913383     TGARCH                ged (1,0)  0.03342466
34     TGARCH               norm (1,0)  22.84463294    AVGARCH                ged (1,0)  0.03890411
44   GJRGARCH                ged (1,0)  31.04458936     APARCH                ged (1,0)  0.03890411
42      GARCH                ged (1,0)  31.04836775      GARCH                ged (1,0) 15.92143895
32    NAGARCH                ged (1,0)  31.04957524     NGARCH                ged (1,0) 15.92691840
40     NGARCH                ged (1,0)  31.05386225   GJRGARCH                ged (1,0) 16.00220144
38    AVGARCH               norm (1,0) 128.26641562    NAGARCH                ged (1,0) 16.00801808

实现预期结果的一种简单方法是在每次迭代时手动更改两个必需的参数,对五个 AIC 数据帧进行排序,最后将它们连接成一个数据帧。

实现预期结果的更好方法可能是在继续对 AIC 数据帧进行排序和连接之前使用两个“for”循环。但是,我无法将这两个循环插入到 lapply 函数中以产生预期的结果。那么,为此目的有什么建议吗?您能否为 R 初学者指出任何其他更智能的解决方案?

【问题讨论】:

  • 能否请您添加一个代码 sn-p 以在模型中包含错误分布(长度类似于前面的示例)
  • 我编辑了这篇文章。误差分布通过 ugarchspec 的 distribution.model 参数改变。我放了一个不同的字母 (z) 进行迭代。

标签: r loops lapply


【解决方案1】:

使用expand.grid,我们可以创建诸如 ("bchain_2012_logreturns","norm")、("bchain_2012_logreturns","std") 等的组合。

对于每个组合,我们调用一个自定义函数来计算所有模型的 AIC。这个函数是我之前解决方案的修改版。

由于缺乏数据,我无法验证结果,但这应该可以。

创建组合:

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 计算:

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

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

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)),"\n"))

}

调用函数:

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)

}))

【讨论】:

  • 当我调用该函数时,我收到以下错误消息--->Begin AIC calc with data =as.character(paramCombinations[x, "Var1"]):errorDistrib=as.character(paramCombinations [x, "Var2"]) .egarchfit 中的错误(spec = spec, data = data, out.sample = out.sample, : ugarchfit-->error: function requires at least 100 data
  • 至于输入数据,您可以使用玩具数据。您为价格生成 365/366 正数,然后使用 diff(log(prices)) 获取对数返回。我无法在此处共享输入数据,因为它们来自非公共数据库
  • top("\nugarchfit-->错误:函数需要至少 100 个数据\n 点才能运行\n")。这是我从回溯中得到的。我不明白为什么该命令没有读取完整的数据子样本
  • 我检查了 rugarch:::.egarchfit 的源代码,确实它需要 N >100 次观察。帕特里克伯恩斯有很棒的统计/量化博客here。对于类似的问题,请参阅他的回答 herethis
  • 是的,我阅读了 PortfolioProbe 上的帖子。好点。但正如我在上面的 cmets 中写给你的那样,我的 bchain_logreturns 子样本由 364 个(如果年份是 2012 年或 2016 年,则为 365 个)logreturns 组成。因此,在我的情况下,样本量 N 不应该成为进行稳健估计的问题。如果我运行另一篇文章的代码,我不会收到关于观察次数的错误消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2012-07-21
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
相关资源
最近更新 更多