Deterministic node only returning NAs

43 views
Skip to first unread message

Mark Taylor

unread,
Sep 30, 2024, 5:41:08 PM9/30/24
to nimble-users
Hello nimble community,

I'm trying to fit a model to estimate two parameters which govern the speed and temporal autocorrelation of movement tracking data. I'm using a bivariate normal distribution for each 2D velocity (v_x,v_y), where the mean depends on the previous velocity. The covariance matrix is proportional to the identity matrix but I'm having trouble with this - I can't declare the variance associated with each velocity component. It might be an indexing issue or something else that I've forgotten or haven't considered. I've attached the R script. Here is the model part with the error:

code <- nimbleCode({
  # Priors for nu and tau (only positive values)
  nu  ~ dinvgamma(0.1,0.1)  # Prior for nu
  tau ~ dinvgamma(0.1,0.1)  # Prior for tau

  for (i in 1:n) {
    dt[i] <- y[i,3] # time step of this iteration. This is given as data since future examples might not have regular gaps

    # mean is dependent on previous velocity and tends to zero as dt increases
    mu_decay_factor[i] <- exp(-dt[i]/tau) # factor between 0 and 1
    mu[i, 1:2] <- y[i, 4:5] * mu_decay_factor[i] # data columns 4 and 5 are the previous x and y components respectively

    # Diagonal covariance matrix
    var[i] <- (2 / pi) * (nu^2) * (1-exp(-2*dt[i]/tau)) # variance of the velocity in x and y directions respectively ### THIS PRODUCES ALL NAs
    cov_matrix[i,1,1] <- var[i] # variance in x direction
    cov_matrix[i,2,2] <- var[i] # variance in y-direction
    cov_matrix[i,1,2] <- 0  # Off-diagonal set to 0
    cov_matrix[i,2,1] <- 0  # Off-diagonal set to 0

    # Likelihood for y[i, 1:2] with data dependent mean
    y[i, 1:2] ~ dmnorm(mu[i, 1:2], cov = cov_matrix[i,1:2, 1:2])
  }
})
Error in chol.default(model$cov_matrix[getNodeFunctionIndexedInfo(INDEXEDNODEINFO_,  :
  the leading minor of order 1 is not positive definite

The issue is in the line declaring the var[i] nodes. 

I'm returning to using nimble after a few years away so apologies if the error is a simple one and I should've spotted it. Any help would be greatly appreciated!

Best,
Mark
forage nimble attempt.R

Perry de Valpine

unread,
Sep 30, 2024, 5:59:58 PM9/30/24
to Mark Taylor, nimble-users
Dear Mark,
Thanks for the question. I tried running your code, but package smoove is not on CRAN and when I made an effort to look it up and use install_github, it failed. I didn't pursue it further.
So I couldn't run your code, but can I ask if the chol.default message was surrounded by a statement that errors are not necessarily a problem at the nimbleModel stage? The error arises (I'm guessing) when nimbleModel attempts to run calculate() on the entire model. If something is not fully initialized, there can be errors that are harmless later. The chol.default is a common one. I can see you've started looking at values in the model and comment that model$var is all NAs. What are the values of nu, dt, and tau? Your code would show nu and tau, but I can't run your code. Feel free to provide values in some way that does not depend on smoove.
Perry



--
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/45dd138f-fa79-4460-9312-737797e52fc6n%40googlegroups.com.

Mark Taylor

unread,
Sep 30, 2024, 6:19:57 PM9/30/24
to nimble-users
Hi Perry,

Thanks for your quick response. I added the install_github() line to the script so hopefully that should work now.

Yes, the error is surrounded by 'not all errors are a problem' sentence. The full error is:  

Defining model
Building model
Setting data and initial values
Running calculate on model
  [Note] Any error reports that follow may simply reflect missing values in model variables.

Error in chol.default(model$cov_matrix[getNodeFunctionIndexedInfo(INDEXEDNODEINFO_,  :
  the leading minor of order 1 is not positive definite

Checking model sizes and dimensions
  [Note] This model is not fully initialized. This is not an error.
         To see which variables are not initialized, use model$initializeInfo().
         For more information on model initialization, see help(modelInitialization).
> model$mu
            [,1]       [,2]
 [1,]  0.3446361  0.7426614
 [2,]  0.1320599  0.3182296
...
> model$var
 [1] NA NA NA NA NA NA NA NA NA NA

The initial values of nu and tau are 1 and 0.5, respectively. In this example dt is always 0.1, but this won't always be the case as it will vary within a sample in later work.

Thanks again, and hopefully the code runs this time.

Best,
Mark
forage nimble attempt.R

Dan Gibson

unread,
Oct 1, 2024, 11:14:40 AM10/1/24
to Mark Taylor, nimble-users
HI Mark,

I forget if JAGS innately recognized pi as pi, but it needs to be specified (in the data or constants statement) in nimble. Making this change gets the toy example to function correctly.


PI = pi
# Build the model
model <- nimbleModel(code,
                     constants = list(n = nrow(v_data), pi = PI),
                     data = list(y = v_data),
                     inits = inits)

> model$var # I get all NAs here
 [1] 0.2098808 0.2098808 0.2098808 0.2098808 0.2098808 0.2098808 0.2098808 0.2098808 0.2098808 0.2098808
> model$cov_matrix[1,,] # diagonal NAs here
          [,1]      [,2]
[1,] 0.2098808 0.0000000
[2,] 0.0000000 0.2098808
> model$calculate()
[1] -17.85664


Best,
-Dan

--
Reply all
Reply to author
Forward
0 new messages