random walk with drift

458 views
Skip to first unread message

ximing

unread,
May 17, 2013, 4:34:33 PM5/17/13
to r-inla-disc...@googlegroups.com
In the R-INLA package, we have the model options of  random walk of order 1 and 2 for the latent states; 
can we also fit a random walk model with drift ?   Thanks.        

Finn Lindgren

unread,
May 17, 2013, 5:01:10 PM5/17/13
to ximing, r-inla-disc...@googlegroups.com
On 17 maj 2013, at 21:34, ximing <siem...@gmail.com> wrote:
In the R-INLA package, we have the model options of  random walk of order 1 and 2 for the latent states; 
can we also fit a random walk model with drift ?   Thanks.

The easiest method is to reparameterise the processes.  The solutions to
  x'(t) = mu + dB(t)
can be written as
  x(t) = mu * t + z(t)
where z(t) is the RW from
  z'(t) = dB(t)
and similarly for second order models.

So a RW(1) with drift according to this definition is equivalent to a non drift RW plus a linear effect of time, which can be simply added as an ordinary covariate, and mu estimated as a "fixed effect".
This also gives the posteriors for x(t) in the linear predictor.

If you had some other model in mind, please specify.

Finn

ximing

unread,
May 20, 2013, 4:27:51 PM5/20/13
to r-inla-disc...@googlegroups.com, ximing
Thank you very much, Dr. Lindgren. It works, and I do not have other models to work on for now.

Ximing   

Frank van Berkum

unread,
Aug 20, 2013, 7:55:12 AM8/20/13
to r-inla-disc...@googlegroups.com, ximing
Hi,

I've got a similar problem, but I would like to estimate the random walk with drift through a linear combination-specification to obtain the posterior marginal. The reason for this is that later on I will add other latent factors, so I can't get the posterior marginal from the linear predictor.

Below I've attached some sample code that contains my current attempt. In this attempt, the posterior marginal for the random walk with drift is not what I expected, when compared with the marginal of the random walk itself. The marginal of the random walk jumps up and down, but the marginal of the linear combination contains straight lines. The jumps we observe in the marginal of the random walk mode I would expect to observe as well in the marginal of the linear combination. Can some point me to what I'm doing wrong?

Your help is kindly appreciated.

Regards,

Frank van Berkum

___________________________________________________________

require(INLA)

iN    <- 100
x     <- cumsum( rnorm( n=iN, mean= -1, sd=3) )
y    <- x + rnorm( n=iN, mean=0, sd=3 )

# Indices
i.rw    <- 1:iN    # random walk part
i.dr    <- 1:iN    # drift part through linear effect

# Formula for random walk with drift
f.rwd    <- y ~ f(i.dr, model="linear", mean.linear = 0, prec.linear=0.1) +
        f(i.rw, model="rw1") - 1

# Define data frame
d.rwd    <- data.frame(i.dr=i.dr, i.rw=i.rw, y=y)

# Linear combination(s) to combine rw and drift
lc.rwd <- inla.make.lincombs(i.dr=i.dr, i.rw=rep(1, iN))
# lc.rwd <- inla.make.lincombs(i.dr=i.dr, i.rw=i.rw)

# Estimate the model
r.rwd <- inla(f.rwd, data=d.rwd, lincomb=lc.rwd, control.inla=list(lincomb.derived.only=TRUE))

# Marginal of linear combination (supposedly random walk with drift)
rwd <- matrix(data=NA, nrow=iN, ncol=4)
rwd[,1] <- as.numeric(summary(r.rwd)$lincomb.derived[,"mean"])
rwd[,2] <- as.numeric(summary(r.rwd)$lincomb.derived[,"0.025quant"])
rwd[,3] <- as.numeric(summary(r.rwd)$lincomb.derived[,"0.975quant"])
rwd[,4] <- as.numeric(summary(r.rwd)$lincomb.derived[,"0.5quant"])
ylims.rwd <- c(min(rwd), max(rwd))

# Marginals of rw
rw <- matrix(data=NA, nrow=iN, ncol=4)
rw[,1] <- as.numeric(r.rwd$summary.random$i.rw[,"mean"])
rw[,2] <- as.numeric(r.rwd$summary.random$i.rw[,"0.025quant"])
rw[,3] <- as.numeric(r.rwd$summary.random$i.rw[,"0.975quant"])
rw[,4] <- as.numeric(r.rwd$summary.random$i.rw[,"0.5quant"])
ylims.rw <- c(min(rw), max(rw))

# Compare both marginals
par(mfrow=c(1,2))
plot(rwd[,1], ylim=ylims.rwd, type="l")
for(i in 2:4) lines(rwd[,i], lty=2)
plot(rw[,1], ylim=ylims.rw, type="l")
for(i in 2:4) lines(rw[,i], lty=2)


______________________________________________________-

Op maandag 20 mei 2013 22:27:51 UTC+2 schreef ximing:
Reply all
Reply to author
Forward
0 new messages