--- title: "Time varying trading days" author: "Jean Palate" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Time varying trading days} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ## Time-varying trading days ### Introduction The state space framework allows an easy estimation of time-varying trading days effects. ```{r echo=FALSE} suppressPackageStartupMessages(library(rjd3toolkit)) suppressPackageStartupMessages(library(rjd3sts)) library(knitr) ``` ```{r data} # take a (transformed) series s<-log(rjd3toolkit::ABS$X0.2.09.10.M) ``` ### Modelling We model such effects by integrating in the state array the regression coefficients. As usual, the regression variables are modeled by using contrasts (for instance: #days-#Sundays). the coefficient follow a multivariate random walk. As for the usual regression, we put the constraint that the sum of the coefficients is 0 (which implies that one coefficient is derived from the others). To be noted that fixed coefficients correspond to no innovation in the random walk. We consider that the same innovation variance applies on the coefficients of each day (option contrast=FALSE) or of each contrasts (option contrast=TRUE). In this latter case, the variance of the innovation of the contrasting (group of) days is larger. Such time varying trading days can be plugged in any representation of the series. ### SARIMA + TDvar ```{r tdvar1} # create the model model<-rjd3sts::model() # create the components and add them to the model rjd3sts::add(model, rjd3sts::sarima("airline", 12, c(0,1,1), c(0,1,1))) rjd3sts::add(model, vtd<-rjd3sts::reg_td("td", 12, start(s), length(s), contrast = FALSE)) #estimate the model rslt<-rjd3sts::estimate(model, s) ss<-rjd3toolkit::result(rslt, "ssf.smoothing.states") vss<-rjd3toolkit::result(rslt, "ssf.smoothing.vstates") ``` ### Available results The results can be retrieved through the "result" function. All the available information are displayed by means of the "dictionary" method ```{r} print(rjd3toolkit::dictionary(rslt)) ``` Time varying trading days ```{r tdvar1_chart, fig.width=6, fig.height=4} colfunc<-colorRampPalette(c("red","blue","green","#196F3D")) colors <- (colfunc(7)) pos<-rjd3toolkit::result(rslt, "ssf.cmppos") plot(ss[,pos[2]+1], type='l', col=colors[1], ylim=c(-0.04, 0.03), ylab='coeff') lines(ss[,pos[2]+2], col=colors[2]) lines(ss[,pos[2]+3], col=colors[3]) lines(ss[,pos[2]+4], col=colors[4]) lines(ss[,pos[2]]+5, col=colors[5]) lines(ss[,pos[2]+6], col=colors[6]) lines(-rowSums(ss[,pos[2]+(1:6)]), col=colors[7]) ``` ### BSM + TDvar ```{r tdvar2} # take a (transformed) series s<-log(rjd3toolkit::ABS$X0.2.09.10.M) # create the model model<-rjd3sts::model() # create the components and add them to the model llt<-rjd3sts::locallineartrend('l') seas<-rjd3sts::seasonal("s", 12, "HarrisonStevens") n<-rjd3sts::noise('n') rjd3sts::add(model, llt) rjd3sts::add(model, seas) rjd3sts::add(model, n) rjd3sts::add(model, vtd<-rjd3sts::reg_td("td", 12, start(s), length(s), contrast = FALSE)) #estimate the model rslt<-rjd3sts::estimate(model, s) ss<-rjd3toolkit::result(rslt, "ssf.smoothing.states") vss<-rjd3toolkit::result(rslt, "ssf.smoothing.vstates") ``` Time varying trading days ```{r tdvar2_chart, fig.width=6, fig.height=4} colfunc<-colorRampPalette(c("red","blue","green","#196F3D")) colors <- (colfunc(7)) pos<-rjd3toolkit::result(rslt, "ssf.cmppos") plot(ss[,pos[4]+1], type='l', col=colors[1], ylim=c(-0.04, 0.03), ylab='coeff') lines(ss[,pos[4]+2], col=colors[2]) lines(ss[,pos[4]+3], col=colors[3]) lines(ss[,pos[4]+4], col=colors[4]) lines(ss[,pos[4]]+5, col=colors[5]) lines(ss[,pos[4]+6], col=colors[6]) lines(-rowSums(ss[,pos[4]+(1:6)]), col=colors[7]) ```