Skip to content

UCARIMA models

Jean Palate edited this page Oct 31, 2023 · 2 revisions

Model-based representation of an Hodrick-Prescott filter

Creation of the UCARIMA model corresponding to an Hodrick-Prescott filter

Sum of an I2 and of a white noise

hp_ucm<-function(lambda=1600){
  i2<-rjd3toolkit::arima_model("trend", delta = c(1, -2, 1))
  noise<-rjd3toolkit::arima_model(variance=lambda)
  ucm<-rjd3toolkit::ucarima_model(components=list(i2, noise))

  return (ucm)
}

Estimation of the UCARIMA model by means of the Kalman smoother

The first columns contain the smoothed states and the last ones contain their standard deviations

ucm_estimate<-function(x, ucm, stdev=TRUE){
  jucm<-rjd3toolkit:::.r2jd_ucarima(ucm)
  jcmps<-rJava::.jcall("jdplus/toolkit/base/r/arima/UcarimaModels", "Ljdplus/toolkit/base/api/math/matrices/Matrix;", "estimate",
                       as.numeric(x), jucm, as.logical(stdev))
  return (rjd3toolkit:::.jd2r_matrix(jcmps))
}

hp_estimate<-function(s, lambda=1600, saSpec=NULL, useTrend=TRUE){
  ucm<-hp_ucm(lambda = lambda)
  if (!is.null(saSpec)){
    sa<-rjd3tramoseats::tramoseats_fast(s, saSpec)
    if (useTrend) s<-sa$final$t$data else s<-sa$final$sa$data
  }

  rslt<-ucm_estimate(s, ucm)

  return (list(options=c(lambda=lambda, trend=useTrend, sa=saSpec), sa=sa, hp=rslt))
}

Example

s<-log(rjd3toolkit::ABS$X0.2.05.10.M)
q<-hp_estimate(s, lambda = 120000, saSpec = "rsa0")
plot(q$hp[,2], type='h', ylab="cycle")
matplot(cbind(s, q$hp[,1], q$sa$final$t$data), type='l', col=c('lightgray', 'blue', 'red'), ylab='logs')
legend("topleft", inset=.1, legend=c( "raw", "trend", "tc"), col=c('lightgray', 'blue', 'red'), lty=1:3)

The raw series, its trend-cycle and the long-term trend are displayed in the following chart

hp