哥哥姐姐,请求帮助
请问在S-Plus 中,如果我已经一个数据可以用IGARCH建模,怎么编程实现将参数估计出来?
我应该调用哪个函数啊?:)
请问在S-Plus 中,如果我已经一个数据可以用IGARCH建模,怎么编程实现将参数估计出来?
我应该调用哪个函数啊?:)
<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>回复 第7楼 的 dengyishuo:
哪里下载rgarch包和它的帮助文档?
回复 第8楼 的 lolo:这包好像还没推上CRAN主站, 到 http://rgarch.r-forge.r-project.org/ 找找.
刚刚无意中发现,分享下rugarch
中的模式最优化...
首先读取数据...
> 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
然后计算最优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)
}
计算最优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)
有关多种GARCH
模式比较,请参考参考文献中的链接3... 包括比较
> 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。