7.2 Random walk with time-varying parameters

Let’s fit a random walk where the first 50 time steps have one process variance and the rest of the time series has a different process variance. The model is \[\begin{equation} \begin{gathered} x_{t} = x_{t-1} + u + w_{t}, \text{ } w_t \sim \,\text{N}(0,q_t) \\ y_{t} = x_{t} + v_{t}, \text{ } v_t \sim \,\text{N}(0,r) \end{gathered} \tag{7.3} \end{equation}\]

Create some data:

set.seed(123)
TT <- 100
sT <- 50
u <- 0.01
r <- 0.01
q <- c(rep(0.1, sT), rep(0.2, TT - sT))
yt <- cumsum(rnorm(TT, u, sqrt(q))) + rnorm(TT, 0, sqrt(r))

Fit with MARSS:

Q <- array(0, dim = c(1, 1, TT))
Q[1, 1, ] <- c(rep("q1", sT), rep("q2", TT - sT))
fit <- MARSS(yt, model = list(Q = Q), silent = TRUE)
broom::tidy(fit)
   term    estimate  std.error     conf.low    conf.up
1   R.R  0.02103312 0.01287598 -0.004203342 0.04626958
2   U.U  0.04315283 0.02999130 -0.015629036 0.10193470
3  Q.q1  0.06849550 0.02514959  0.019203209 0.11778779
4  Q.q2  0.12424866 0.03710242  0.051529256 0.19696806
5 x0.x0 -0.26716186 0.29456436 -0.844497393 0.31017367

We can do the same with the drift term, \(u\). We have one \(u\) for the first 50 time steps and another for the last 50. Let’s fit a random walk where the first 50 time steps have one process variance and the rest of the time series has a different process variance. The model is \[\begin{equation} \begin{gathered} x_{t} = x_{t-1} + u + w_{t}, \text{ } w_t \sim \,\text{N}(0,q_t) \\ y_{t} = x_{t} + v_{t}, \text{ } v_t \sim \,\text{N}(0,r) \end{gathered} \tag{7.4} \end{equation}\]

Create some data:

set.seed(123)
q <- 0.1
u <- c(rep(0.1, sT), rep(-0.1, TT - sT))
yt <- cumsum(rnorm(TT, u, sqrt(q))) + rnorm(TT, 0, sqrt(r))

Fit with MARSS:

U <- array(0, dim = c(1, 1, TT))
U[1, 1, ] <- c(rep("u1", sT), rep("u2", TT - sT))
fit <- MARSS(yt, model = list(U = U), silent = TRUE)
broom::tidy(fit)
   term    estimate   std.error      conf.low    conf.up
1   R.R  0.01878371 0.009873106 -0.0005672203 0.03813464
2  U.u1  0.11495619 0.035842744  0.0447057067 0.18520668
3  U.u2 -0.05358255 0.035480829 -0.1231236961 0.01595860
4   Q.Q  0.06237204 0.017600922  0.0278748647 0.09686921
5 x0.x0 -0.24493471 0.281911468 -0.7974710376 0.30760161