• 统计学金融
  • 哥哥姐姐,请问IGARCH模型的参数估计怎么编程实现啊.

哥哥姐姐,请求帮助



请问在S-Plus 中,如果我已经一个数据可以用IGARCH建模,怎么编程实现将参数估计出来?

我应该调用哪个函数啊?:)
估计是做金融的



如果模型已经有了,生成了一个object

那么先使用names(YourModel)。

然后使用$运算符,提取你要的参数吧

或者干脆,使用str,unclass函数看看



如果还没有模型,问需要哪个函数,你就用help吧,我记得FinMetrics模块里面是有专门做这个的,而R里面绝对是有这个冬冬的。
fSeries: The Dynamical Process Behind Financial Markets
谢谢刚才这位哥哥姐姐的解答,但是我可能是没将我的问题表示清楚.

我的问题是我要做IGARCH模型,应该调用什么函数.

例如,我一直SP500数据,我想做GARCH模型,我调用函数,用语句"garch(sp500~1,~garch(1,1))"即可,现在我想做IGARCH模型,不知调用哪个函数,还是将上面的语句做什么改动?

谢谢!
其实我也没用过IGARCH,但是查了一下发现fSeries包中有(garchOxFit函数吧)。
splus和fseries里都没有函数估计IGARCH,对常见的garch,egarch,garch-m,均可以估计,但是igarch不行。方差方程中含有单位根即为IGARCH,可以通过控制估计算法中的若干项来达到估计IGARCh(1,1)模型
3 年 后

<br />
#安装rgarch包<br />
install.packages("rgarch", repos="http://R-Forge.R-project.org")</p>
<p>#载入rgarch<br />
library(rgarch);</p>
<p>#设定模型为igarch</p>
<p>variance.model=list(model="iGARCH",garchOrder=c(1,1),submodel=NULL,external.regressors=NULL);<br />
mean.model=list(armaOrder=c(1,1),include.mean=T,garchInMean=F,inMeanType=1,arfima=F,exteranl.regressors=NULL);<br />
spec=ugarchspec(variance.model=variance.model,mean.mode=mean.model,distribution.model="norm");</p>
<p>#拟合参数<br />
fit=ugarchfit(data=DATA,spec=spec,out.sample=0,solver="solnp")</p>
<p>#查看参数<br />
fit<br />
</p>
9 个月 后

回复 第7楼 的 dengyishuo:

哪里下载rgarch包和它的帮助文档?

回复 第8楼 的 lolo:这包好像还没推上CRAN主站, 到 http://rgarch.r-forge.r-project.org/ 找找.

7 年 后

刚刚无意中发现,分享下rugarch中的模式最优化...

2. 数据

首先读取数据...

> require('forecast')
> require('quantmod')
> require('rugarch')
> require('plyr')
> require('dplyr')

> getFX('AUD/USD', from = (today('GMT') - 1) %m-% years(1))
> summary(AUDUSD)
     Index               AUD.USD      
 Min.   :2017-05-17   Min.   :0.7399  
 1st Qu.:2017-06-30   1st Qu.:0.7607  
 Median :2017-08-14   Median :0.7817  
 Mean   :2017-08-14   Mean   :0.7768  
 3rd Qu.:2017-09-28   3rd Qu.:0.7930  
 Max.   :2017-11-12   Max.   :0.8079

3. 统计建模

3.1 ARFIMA:ARMA 最优化(新程序)

然后计算最优arma order... 包括d值。

> best.ARMA <- function(mbase){
+   #ARMA Modeling minimum AIC value of `p,d,q`
+   fit <- auto.arima(mbase)
+   arimaorder(fit)
+   }
> calC <- function(mbase) {
    
    armaOrder = best.ARMA(mbase)
    
    ## Set arma order for `p, d, q` for GARCH model.
    #'@ https://stats.stackexchange.com/questions/73351/how-does-one-specify-arima-p-d-q-in-ugarchspec-for-ugarchfit-in-rugarch
    spec = ugarchspec(
      variance.model = list(
        model = 'gjrGARCH', garchOrder = c(1, 1), 
        submodel = NULL, external.regressors = NULL, 
        variance.targeting = FALSE), 
      mean.model = list(
        armaOrder = armaOrder[c(1, 3)], #set arma order for `p` and `q`.
        include.mean = TRUE, archm = FALSE, 
        archpow = 1, arfima = TRUE, #set arima = TRUE
        external.regressors = NULL, 
        archex = FALSE), 
      fixed.pars = list(arfima = armaOrder[2]), #set fixed.pars for `d` value
      distribution.model = 'snorm')
    
    fit = ugarchfit(spec, mbase, solver = 'hybrid')
    
    return(fit)
  }

3.2 ARFIMA:ARMA 最优化(旧程序)

计算最优arma order中的p值与q值... 不包括d值。

armaSearch <- function(data, .method = 'CSS-ML'){ 
    ## ARMA Modeling寻找AIC值最小的p,q
    ##
    ## I set .method = 'CSS-ML' as default method since the AIC value we got is 
    ##  smaller than using method 'ML' while using method 'CSS' facing error.
    ## 
    ## https://stats.stackexchange.com/questions/209730/fitting-methods-in-arima
    ## According to the documentation, this is how each method fits the model:
    ##  - CSS minimises the sum of squared residuals.
    ##  - ML maximises the log-likelihood function of the ARIMA model.
    ##  - CSS-ML mixes both methods: first, CSS is run, the starting parameters 
    ##    for the optimization algorithm are set to zeros or to the values given 
    ##    in the optional argument init; then, ML is applied passing the CSS 
    ##    parameter estimates as starting parameter values for the optimization algorithm.
    
    .methods = c('CSS-ML', 'ML', 'CSS')
    
    if(!.method %in% .methods) stop(paste('Kindly choose .method among ', 
                                          paste0(.methods, collapse = ', '), '!'))
    
    armacoef <- data.frame()
    for (p in 0:5){
      for (q in 0:5) {
        #data.arma = arima(diff(data), order = c(p, 0, q))
        #'@ data.arma = arima(data, order = c(p, 1, q), method = .method)
        if(.method == 'CSS-ML') {
          data.arma = tryCatch({
            arma = arima(data, order = c(p, 1, q), method = 'CSS-ML')
            mth = 'CSS-ML'
            list(arma, mth)
          }, error = function(e) {
            arma = arima(data, order = c(p, 1, q), method = 'ML')
            mth = 'ML'
            list(arma = arma, mth = mth)
          })
        } else if(.method == 'ML') {
          data.arma = tryCatch({
            arma = arima(data, order = c(p, 1, q), method = 'ML')
            mth = 'ML'
            list(arma = arma, mth = mth)
          }, error = function(e) {
            arma = arima(data, order = c(p, 1, q), method = 'CSS-ML')
            mth = 'CSS-ML'
            list(arma = arma, mth = mth)
          })
        } else if(.method == 'CSS') {
          data.arma = tryCatch({
            arma = arima(data, order = c(p, 1, q), method = 'CSS')
            mth = 'CSS'
            list(arma = arma, mth = mth)
          }, error = function(e) {
            arma = arima(data, order = c(p, 1, q), method = 'CSS-ML')
            mth = 'CSS-ML'
            list(arma = arma, mth = mth)
          })
        } else {
          stop(paste('Kindly choose .method among ', paste0(.methods, collapse = ', '), '!'))
        }
        names(data.arma) <- c('arma', 'mth')
        
        #cat('p =', p, ', q =', q, 'AIC =', data.arma$arma$aic, '\n')
        armacoef <- rbind(armacoef,c(p, q, data.arma$arma$aic))
      }
    }
    colnames(armacoef) <- c('p', 'q', 'AIC')
    pos <- which(armacoef$AIC == min(armacoef$AIC))
    cat(paste0('method = \'', data.arma$mth, '\', the min AIC = ', armacoef$AIC[pos], 
               ', p = ', armacoef$p[pos], ', q = ', armacoef$q[pos], '\n'))
    return(armacoef)
  }
  
calC2 <- function(mbase, ahead = 1, price = 'Cl') {
    
    armaOrder = armaSearch(mbase)
    armaOrder %<>% dplyr::filter(AIC==min(AIC)) %>% .[c('p', 'q')] %>% unlist
    
    spec = ugarchspec(
      variance.model = list(
        model = 'gjrGARCH', garchOrder = c(1, 1), 
        submodel = NULL, external.regressors = NULL, 
        variance.targeting = FALSE), 
      mean.model = list(
        armaOrder = armaOrder, 
        include.mean = TRUE, archm = FALSE, 
        archpow = 1, arfima = FALSE, 
        external.regressors = NULL, 
        archex = FALSE), 
      distribution.model = 'snorm')
    fit = ugarchfit(spec, mbase, solver = 'hybrid')
    #'@ fc = ugarchforecast(fit, n.ahead = ahead)
    #'@ res = attributes(fc)$forecast$seriesFor
    #'@ colnames(res) = names(mbase)
    
    #'@ tmp = list(latestPrice = tail(mbase, 1), forecastPrice = res)
    #'@ return(tmp)
return(fit)
  }
> fit <- calC(AUDUSD)
> fit2 <- calC2(AUDUSD)

4. 模式比较

有关多种GARCH模式比较,请参考参考文献中的链接3... 包括比较

  • auto.arima
  • exponential smoothing models (ETS)
  • GARCH (包括GARCH、eGARCH、iGARCH、fGARCH、gjrGARCH等模式)
  • exponential weighted models
> infocriteria(fit)
                      
Akaike       -9.098131
Bayes        -8.956222
Shibata      -9.101862
Hannan-Quinn -9.040593
> infocriteria(fit2)
                      
Akaike       -9.072344
Bayes        -8.948174
Shibata      -9.075221
Hannan-Quinn -9.021998

结果fit胜出,比fit2更佳... 证明p值、d值与q值都可以优化(包括模式中设定默认的d值)... 目前正在编写着Q1App2自动交易应用,除了模式最优化以外,程序上分秒必争... microbenchmark测试效率,今天刚编写了个DataCollection应用采集实时数据以方便之后的高频率交易自动化建模,更多详情请参阅Real Time FXCM

5. 参考文献

  1. How does one specify arima (p,d,q) in ugarchspec for ugarchfit in rugarch?
  2. How to read p,d and q of auto.arima()?
  3. binary.com : Job Application - Quantitative Analyst