Changing parameter values in bootstrap filter

16 views
Skip to first unread message

Fanny Empacher

unread,
Apr 5, 2021, 7:16:03 AM4/5/21
to nimble-users
Hi,

I'm trying to use the bootstrap filter in nimble to estimate the likelihood of a time series for different parameter values.

The only way it has worked so far is by changing the value in the model (with model$a <- 1), then recompiling the model, then building the bootstrap filter, then compiling the bootstrap filter and finally running it. I've tried a few things to avoid some of these steps but haven't succeeded yet. Is there any way to skip some of the intermediate steps?

Here's some example code of what I'm trying to do, using the model in the documentation:

timeModelCode <- nimbleCode({
  x[1] ~ dnorm(mu_0, 1)
  y[1] ~ dnorm(x[1], 1)
  for(i in 2:t){
    x[i] ~ dnorm(x[i-1] * a + b, 1)
    y[i] ~ dnorm(x[i] * c, 1)
  }
  
  a ~ dunif(0, 1)
  b ~ dnorm(0, 1)
  c ~ dnorm(1,1)
  mu_0 ~ dnorm(0, 1)
})

# simulate some data
t <- 25; mu_0 <- 1
x <- rnorm(1 ,mu_0, 1)
y <- rnorm(1, x, 1)
a <- 0.5; b <- 1; c <- 1
for(i in 2:t){
  x[i] <- rnorm(1, x[i-1] * a + b, 1)
  y[i] <- rnorm(1, x[i] * c, 1)
}

# build the model
rTimeModel <- nimbleModel(timeModelCode, constants = list(t = t), 
                          data <- list(y = y), check = FALSE )

# Set parameter values and compile the model
rTimeModel$a <- 0.5
rTimeModel$c <- 1
rTimeModel$mu_0 <- 1

b_candidates <- seq(-4, 4, by = 1)
b_ll <- numeric(length(b_candidates))
parNum <- 5000

for(i in 1:length(b_candidates)){
  print(i)
  rTimeModel$b <- b_candidates[i]
  cTimeModel <- compileNimble(rTimeModel)
  rBootF <- buildBootstrapFilter(rTimeModel, "x", 
                                 control = list(thresh = 0.8, saveAll = TRUE, 
                                                smoothing = FALSE))
  cBootF <- compileNimble(rBootF,project = rTimeModel)
  b_ll[i] <- cBootF$run(parNum)
}

plot(b_candidates, b_ll)

Daniel Turek

unread,
Apr 5, 2021, 7:49:24 AM4/5/21
to Fanny Empacher, nimble-users
If your only trying to change the value of "a" as far as the filtering algorithm is concerned, then you should be fine to change that value in the compiled model object (cTimeModel), as:

cTimeModel$a <- 1

Then, for good measure, adding in a call to calculate() to make sure to propagate this modified value downwards through the entire model:

cTimeModel$calculate()

Point being, the compiled filtering algorithm (cBootF) operates using the compiled model object. Should be no need for  the other intermediate steps.

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/c6a1fe2e-6ad3-437a-a91d-7b5733ed8a5dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages