Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc19de7f

Browse files
committed
Bug Fixes; Genetic Optimisation algorithm use added.
1 parent478bf0a commitc19de7f

File tree

10 files changed

+215
-115
lines changed

10 files changed

+215
-115
lines changed

‎DESCRIPTION‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ Imports:
2424
lubridate,
2525
crayon,
2626
utils,
27-
FKF,
28-
nloptr
27+
nloptr,
28+
BB,
29+
GA,
30+
dfoptim,
31+
pso,
32+
FKF
2933
Suggests:
3034
longmemo,
3135
yardstick,
3236
tidyverse,
33-
BB,
34-
GA,
35-
pso,
36-
dfoptim,
3737
testthat,
3838
knitr,
3939
rmarkdown

‎NAMESPACE‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ export(garma_ggtsdisplay)
2121
export(gg_raw_pgram)
2222
export(ggbr_semipara)
2323
export(version)
24+
importFrom(BB,BBoptim)
25+
importFrom(GA,de)
2426
importFrom(Rsolnp,gosolnp)
2527
importFrom(Rsolnp,solnp)
28+
importFrom(dfoptim,hjkb)
29+
importFrom(dfoptim,nmkb)
2630
importFrom(forecast,forecast)
2731
importFrom(forecast,ggtsdisplay)
2832
importFrom(ggplot2,aes)
@@ -53,6 +57,7 @@ importFrom(pracma,conv)
5357
importFrom(pracma,deconv)
5458
importFrom(pracma,hessian)
5559
importFrom(pracma,pinv)
60+
importFrom(pso,psoptim)
5661
importFrom(signal,Arma)
5762
importFrom(signal,filter)
5863
importFrom(stats,Box.test)
@@ -62,6 +67,7 @@ importFrom(stats,diffinv)
6267
importFrom(stats,end)
6368
importFrom(stats,frequency)
6469
importFrom(stats,na.pass)
70+
importFrom(stats,optim)
6571
importFrom(stats,optimise)
6672
importFrom(stats,sd)
6773
importFrom(stats,spectrum)

‎NEWS.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#garma 0.9.7
22

3-
Version 0.9.7 adds the "tsdiag" function for garma models - we just call the base version of tsdiag, so all parameters for that should work.
3+
Version 0.9.7 adds the "tsdiag" function for garma models - this is essentially a copy of the base stats 'tsdiag'
4+
but the fitdf for the Ljung-Box test is set to p+q+k*2.
45

5-
The override of the ggplot function for garma models has been removed as this was not standard - the correct way to do this is 'autoplot'
6-
so now the autoplot function has the ability to generate forecasts and plot them.
6+
There was a bug with the standard errors which resulted in Nan being returned for some parameters. This has been fixed.
7+
8+
The override of the ggplot function for garma models has been removed as this was not standard - the correct way to do this
9+
is 'autoplot' so now the autoplot function has the ability to generate forecasts and plot them.
10+
11+
A new optimisation method has been added for the "garma" function - this is a genetic algorithm from package GA.
12+
It can be used by specifying opt_method='ga'. For now please treat this as experimental - it has given some interesting
13+
results on some standard problems and these probably need to be checked further before more general use.
714

815
Otherwise some redundant code has been removed, and also the restriction that integer differencing be restricted to 1 only.
916

‎R/garma-package.R‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#' @author Richard Hunt
99
#'
1010
#' @importFrom nloptr nloptr cobyla directL lbfgs mma auglag
11+
#' @importFrom BB BBoptim
12+
#' @importFrom pso psoptim
13+
#' @importFrom dfoptim hjkb nmkb
14+
#' @importFrom GA de
1115
#' @importFrom pracma pinv hessian conv deconv
1216
#' @importFrom signal Arma filter
1317
#' @importFrom zoo zoo index
@@ -16,7 +20,7 @@
1620
#' @importFrom Rsolnp solnp gosolnp
1721
#' @importFrom ggplot2 autoplot ggplot geom_line aes geom_vline theme theme_bw scale_colour_manual geom_text labs element_blank
1822
#' @importFrom graphics abline lines par plot
19-
#' @importFrom stats diffinv end sd start ts tsp var spectrum frequency optimise arima Box.test acf tsdiag na.pass
23+
#' @importFrom stats diffinv end sd start ts tsp var spectrum frequency optimise arima Box.test acf tsdiag na.pass optim
2024
#' @importFrom utils tail packageVersion head globalVariables
2125
NULL
2226

‎R/garma_main.R‎

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ garma<-function(x,
138138
stop('QML method does not support k>1. It is suggested you try either the CSS or Whittle methods.\n')
139139

140140
if (is.null(opt_method)) {
141-
if (k>=2)opt_method<-'solnp'
142-
elseopt_method<- c('directL','solnp')
141+
opt_method<- c('directL','solnp')
142+
# if (k>=2) opt_method <- 'solnp'
143+
# else opt_method <- c('directL','solnp')
143144
}
144145

145146
for (ominopt_method) {
@@ -177,18 +178,14 @@ garma<-function(x,
177178
pars<-numeric(0)
178179
lb<-numeric(0)
179180
ub<-numeric(0)
180-
lb_finite<-numeric(0)
181-
ub_finite<-numeric(0)
182181

183182
mean_methods<- c('QML','CSS')
184183
if (include.mean&method%in%mean_methods) {
185184
n_pars<-n_pars+1
186185
mean_y<- mean(y)
187186
pars<- c(pars,mean_y)
188-
lb_finite<- c(lb_finite,ifelse(mean_y<0,2*mean_y,-2*mean_y))
189-
ub_finite<- c(ub_finite,ifelse(mean_y<0,-2*mean_y,2*mean_y))
190-
lb<- c(lb,-Inf)
191-
ub<- c(ub,Inf)
187+
lb<- c(lb,ifelse(mean_y<0,2*mean_y,-2*mean_y))
188+
ub<- c(ub,ifelse(mean_y<0,-2*mean_y,2*mean_y))
192189
}
193190

194191
if (k>0) {# initial parameter estimates for Gegenbauer factors
@@ -213,8 +210,6 @@ garma<-function(x,
213210
pars<- c(pars,start_u,start_d)
214211
lb<- c(lb,min_u,ifelse(allow_neg_d,-1,0))
215212
ub<- c(ub,max_u,0.5)
216-
lb_finite<- c(lb_finite,min_u,ifelse(allow_neg_d,-1,0))
217-
ub_finite<- c(ub_finite,max_u,0.5)
218213
}
219214
}
220215

@@ -233,21 +228,13 @@ garma<-function(x,
233228
if (p==1&q==0) {# special limits for AR(1)
234229
lb<-c(lb,-1)
235230
if (method%in%methods_to_estimate_var)lb<- c(lb,1e-10)
236-
lb_finite<- c(lb_finite,-1)
237-
if (method%in%methods_to_estimate_var)lb_finite<- c(lb_finite,1e-10)
238231
ub<-c(ub,1)
239-
if (method%in%methods_to_estimate_var)ub<-c(ub,Inf)
240-
ub_finite<- c(ub_finite,1)
241-
if (method%in%methods_to_estimate_var)ub_finite<- c(ub_finite,2*stats::var(y))
232+
if (method%in%methods_to_estimate_var)ub<-c(ub,2*stats::var(y))
242233
}else {
243-
lb<- c(lb,rep(-Inf,p+q))
234+
lb<- c(lb,rep(-10,p+q))
244235
if (method%in%methods_to_estimate_var)lb<- c(lb,1e-10)
245-
ub<- c(ub,rep(Inf,p+q))
246-
if (method%in%methods_to_estimate_var)ub<- c(ub,Inf)
247-
lb_finite<- c(lb_finite,rep(-10,p+q))
248-
if (method%in%methods_to_estimate_var)lb_finite<- c(lb_finite,1e-10)
249-
ub_finite<- c(ub_finite,rep(10,p+q))
250-
if (method%in%methods_to_estimate_var)ub_finite<- c(ub_finite,2*stats::var(y))
236+
ub<- c(ub,rep(10,p+q))
237+
if (method%in%methods_to_estimate_var)ub<- c(ub,2*stats::var(y))
251238
}
252239
}
253240

@@ -260,15 +247,17 @@ garma<-function(x,
260247
message<- c()
261248

262249
# Optimisation functions for each method
263-
fcns<-list('CSS'=.css.ggbr.obj,'Whittle'=.whittle.ggbr.obj,'QML'=.qml.ggbr.obj,'WLL'=.wll.ggbr.obj)
250+
fcns<-list('CSS'=.css.ggbr.obj,'Whittle'=.whittle.ggbr.obj,'QML'=.qml.ggbr.obj,'WLL'=.wll.ggbr.obj)
251+
grads<-list('CSS'=NULL,'Whittle'=.whittle.ggbr.grad,'QML'=NULL,'WLL'=NULL)
264252

265253
if (opt_method[[1]]=='best') {
266-
fit<- .best_optim(initial_pars=pars,fcn=fcns[[method]],lb=lb,ub=ub,lb_finite=lb_finite,ub_finite=ub_finite,params=params,control=control)
254+
fit<- .best_optim(initial_pars=pars,fcn=fcns[[method]],grad=grads[[method]],
255+
lb=lb,ub=ub,params=params,control=control)
267256
}else {
268-
fit<- .generic_optim_list(opt_method_list=opt_method,initial_pars=pars,fcn=fcns[[method]],
269-
lb=lb,ub=ub,lb_finite=lb_finite,ub_finite=ub_finite,params=params,control=control)
257+
fit<- .generic_optim_list(opt_method_list=opt_method,initial_pars=pars,fcn=fcns[[method]],grad=grads[[method]],
258+
lb=lb,ub=ub,params=params,control=control)
270259
}
271-
if (fit$convergence==-999) stop('Failed to converge.')
260+
if (fit$convergence==-999) stop('ERROR:Failed to converge.')
272261

273262
hh<-fit$hessian
274263

@@ -297,33 +286,33 @@ garma<-function(x,
297286
if (opt_method[[1]]=='solnp')conv_ok<- (fit$convergence==0)
298287
elseconv_ok<- (fit$convergence>=0)
299288

289+
vcov1<-matrix(nrow=length(fit$par),ncol=length(fit$par))# default
300290
if (conv_ok&method!='WLL'&!is.null(hh)) {
301291
# Next, find the se's for coefficients
302292
start<-1
303293
se<-c()# default to set this up in the right environment
304294
# next check the hessian
305295
h_inv_diag<- diag(inv_hessian<-pracma::pinv(hh))
306-
if (!any(h_inv_diag<0)) {# if hessian doesn't look valid then... generate another one
296+
if (any(h_inv_diag<0)) {# if hessian doesn't look valid then... generate another one
307297
hh<-pracma::hessian(fcns[[method]],fit$par,params=params)
308298
h_inv_diag<- diag(inv_hessian<-pracma::pinv(hh))
309299
}
310300
if (method=='Whittle') {
311301
se<- suppressWarnings(sqrt(2*pi*h_inv_diag))#var(y)/length(y) *
312-
vcov<-2*pi*inv_hessian
302+
vcov1<-2*pi*inv_hessian
313303
}
314304
if (method=='CSS') {
315305
se<- suppressWarnings(sqrt(h_inv_diag*sigma2*2))
316-
vcov<-inv_hessian*2*sigma2
306+
vcov1<-inv_hessian*2*sigma2
317307
}
318308
if (method=='QML') {
319309
se<- suppressWarnings(sqrt(h_inv_diag*length(y)))
320-
vcov<-inv_hessian*length(y)
310+
vcov1<-inv_hessian*length(y)
321311
}
322312
}
323313
if (method=='WLL') {
324314
se<-rep(NA,length(par))
325315
if (k==1)se[2]<- .wll_d_se(fit$par[1],ss)# result only holds for k=1
326-
vcov<-matrix(nrow=length(fit$par),ncol=length(fit$par))
327316
}
328317
nm<-list()
329318
if (include.mean)nm<- c(nm,'intercept')
@@ -346,7 +335,7 @@ garma<-function(x,
346335
coef<- t(matrix(c(temp_coef,temp_se),nrow=n_coef))
347336
colnames(coef)<-nm
348337
rownames(coef)<- c('','s.e.')
349-
colnames(vcov)<- rownames(vcov)<- tail(nm,nrow(vcov))
338+
colnames(vcov1)<- rownames(vcov1)<- tail(nm,nrow(vcov1))
350339

351340
# build a ggbr_factors object
352341
gf<-list()
@@ -383,7 +372,7 @@ garma<-function(x,
383372
res<-list('call'= match.call(),
384373
'series'= deparse(match.call()$x),
385374
'coef'=coef,
386-
'var.coef'=vcov,
375+
'var.coef'=vcov1,
387376
'sigma2'=sigma2,
388377
'obj_value'=fit$value,
389378
'loglik'=loglik,

‎R/garma_whittle.R‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
# spec_den_inv[spec_den_inv==0] <- NA
4242
I_f<-spec*spec_den_inv
43-
res<- sum(I_f,na.rm=TRUE)#- sum(log(spec_den_inv[spec_den_inv>0]),na.rm=TRUE)
43+
res<- sum(I_f,na.rm=TRUE)- sum(log(spec_den_inv[spec_den_inv>0]),na.rm=TRUE)
4444

4545
return(res)
4646
}
@@ -86,3 +86,89 @@
8686
return(res)
8787
}
8888

89+
.whittle.ggbr.grad<-function(par,params) {
90+
# objective function to be minimised for Whittle estimates
91+
ss<-params$ss
92+
p<-params$p
93+
q<-params$q
94+
k<-params$k
95+
96+
n_freq<-length(ss$freq)
97+
freq<-ss$freq[1:n_freq]
98+
spec<-ss$spec[1:n_freq]
99+
100+
start=1
101+
u<-fd<- c()
102+
for (k1in seq_len(k)) {
103+
u<- c(u,par[start])
104+
fd<- c(fd,par[start+1])
105+
start<-start+2
106+
}
107+
108+
if (p>0)phi_vec<- (-par[start:(start+p-1)])elsephi_vec<-1
109+
if (q>0)theta_vec<- (par[(p+start):length(par)])elsetheta_vec<-1
110+
111+
cos_2_pi_f<- cos(2.0*pi*freq)
112+
mod_phi<-mod_theta<-1
113+
if (p>0)mod_phi<- .a_fcn(phi_vec,freq)
114+
if (q>0)mod_theta<- .a_fcn(theta_vec,freq)
115+
spec_den_inv<-mod_phi/mod_theta# Inverse of spectral density
116+
117+
for (k1in seq_len(k)) {
118+
u_k<-u[k1]
119+
fd_k<-fd[k1]
120+
temp<- (4.0*((cos_2_pi_f-u_k)^2.0))
121+
spec_den_inv<-spec_den_inv* (temp^fd_k)
122+
}
123+
124+
spec_den_inv[spec_den_inv==0]<-NA
125+
I_f<-spec*spec_den_inv
126+
127+
grad<- c()
128+
for (k1in seq_len(k)) {
129+
grad_u<- sum(2*fd[k1]/(cos_2_pi_f-u[k1])*(1.0-I_f),na.rm=TRUE)
130+
grad_fd<- sum(-log((cos_2_pi_f-u[k1])*(cos_2_pi_f-u[k1])*4)*(1.0-I_f),na.rm=TRUE)
131+
grad<- c(grad,grad_u,grad_fd)
132+
}
133+
if (p>0) {
134+
phi_cos_vec<- .a_fcn_cos(phi_vec,freq)
135+
phi_sin_vec<- .a_fcn_sin(phi_vec,freq)
136+
for (p1in seq_len(p)) {
137+
grad_phi<-2.0*sum((phi_cos_vec*cos(p1*2*pi*freq)+phi_sin_vec*sin(p1*2*pi*freq))/mod_phi* (1.0-I_f),na.rm=TRUE)
138+
grad<- c(grad,grad_phi)
139+
}
140+
}
141+
if (q>0) {
142+
theta_cos_vec<- .a_fcn_cos(theta_vec,freq)
143+
theta_sin_vec<- .a_fcn_sin(theta_vec,freq)
144+
for (q1in seq_len(q)) {
145+
grad_theta<-2.0*sum((theta_cos_vec*cos(q1*2*pi*freq)+theta_sin_vec*sin(q1*2*pi*freq))/mod_theta* (I_f+1.0),na.rm=TRUE)
146+
grad<- c(grad,grad_theta)
147+
}
148+
}
149+
150+
return(grad)
151+
}
152+
153+
# params <- list(y=diff(co2_ts), orig_y=co2_ts, ss=stats::spectrum((diff(co2_ts)-mean(diff(co2_ts))),plot=FALSE,detrend=TRUE,demean=TRUE,method='pgram',taper=0,fast=FALSE),
154+
# p=0,q=0,d=1,k=1,
155+
# include.mean=FALSE,
156+
# method='Whittle',
157+
# est_mean=FALSE,
158+
# scale=sd(diff(co2_ts)),m_trunc=50)
159+
# par <- c(cos(0.7), 0.2436360904)
160+
#
161+
# .whittle.ggbr.grad(par,params)
162+
# pracma::grad(.whittle.ggbr.obj,par,params=params)
163+
#
164+
# nloptr::check.derivatives(par,.whittle.ggbr.obj,.whittle.ggbr.grad,params=params)
165+
166+
# params <- list(y=diff(co2_ts), orig_y=co2_ts, ss=stats::spectrum((diff(co2_ts)-mean(diff(co2_ts))),plot=FALSE,detrend=TRUE,demean=TRUE,method='pgram',taper=0,fast=FALSE),
167+
# p=3,q=0,d=1,k=1,
168+
# include.mean=FALSE,
169+
# method='Whittle',
170+
# est_mean=FALSE,
171+
# scale=sd(diff(co2_ts)),m_trunc=50)
172+
# par <- c(cos(0.7), 0.2436360904, 0.2, 0.1, 0.3)
173+
#
174+
# nloptr::check.derivatives(par,.whittle.ggbr.obj,.whittle.ggbr.grad,params=params)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp