Nimble creating a variable from a constant

52 views
Skip to first unread message

frederic....@gmail.com

unread,
Mar 26, 2025, 6:22:59 AM3/26/25
to nimble-users
Hello,

I have come to a problem of calculations on a MCMC nimble model based on nimbleFunctions that seems to be explained by the following problem: when adding in my model nimble Code the following element:
+sum(eta[1:5] * matrix_exp_quiz_PM[i,2:6]) 
- together with priors for the new parameter eta- Nimble identifies matrix_exp_quiz_PM as a variable - through e.g. the command: CModel[[i]]$getVarNames() where CModel  is the result of the function nimble::compileNimble - although it is provided in the Model Constants list - and does not have a prior.

The MCMC works fine but subsequent calculations not apparently due to this problem.

Sincerely,

Frédéric

Perry de Valpine

unread,
Mar 26, 2025, 3:07:04 PM3/26/25
to frederic....@gmail.com, nimble-users
Dear Frédéric,

A variable in a model does not necessarily have a prior. But I think we should be talking about nodes, because a variable is just a label for an object that may contain one or more nodes. A node that is stochastic and is not data will have a prior.

Scalar constants are absorbed completely without creating a variable. Non-scalar constants become variables.

In your example, `matrix_exp_quiz_PM[i, 2:6]` (for each i) will be labeled as a right-hand-side only (or RHSonly) node, meaning it does not have a declaration in which it appears on the left-hand-side of `<-` or `~`. A typical example is a covariate or explanatory variable.

If you use `model$getNodeNames`, you will find an option to include RHS only nodes or not (default not).

With clarification on these points, are you able to modify your use of the model to get the correct calculations done as you need?

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 visit https://groups.google.com/d/msgid/nimble-users/df086fd8-16cc-4a1a-9205-e978d7a6af6an%40googlegroups.com.

frederic....@gmail.com

unread,
Mar 31, 2025, 11:08:06 AM3/31/25
to nimble-users

Dear Perry,

I thank you for your answer. The trouble I have is that matrix_exp_quiz_PM is in the constants of the Nimble model (i.e. in the list of constants other than data provided to Nimble). Can it then be considered as a variable? I understood that no. But maybe I did not catch something.

Sincerely,

Frédéric

Chris Paciorek

unread,
Mar 31, 2025, 1:47:47 PM3/31/25
to frederic....@gmail.com, nimble-users
Hi Frédéric,

Perry is out for the week, so let me see if I can help.  Even if you are providing `matrix_exp_quiz_PM` in`constants`, nimble treats it as a variable because it is not a scalar. As a result, for each i, `matrix_exp_quiz_PM[i, 2:6]` will be right-hand-side only nodes. However, because they only appear on the right-hand side of expressions, there are no calculations associated with it (another way of saying this is that the nodes have no prior).

Could you give more details about the problem you see with the subsequent calculations? You may need to show us your code.

-chris

frederic....@gmail.com

unread,
Apr 1, 2025, 4:43:17 PM4/1/25
to nimble-users
Hi Chris,

you are right. I should have better understood the explanation made by Perry. The problem seemed - but I am unsure, this a guess - to come from calculation of logProbs:
                                model$calculate(notDatavarNames)
return(model$calculate(DatavarNames))

What is clear is when I remove this node from the output of $getVarNames(), the problem is solved. This is the solution I have found.

The model is rather complex. Maybe I should try to reproduce the pb with a toy model. I will try.

Sincerely,

Frédéric

Chris Paciorek

unread,
Apr 1, 2025, 6:41:07 PM4/1/25
to frederic....@gmail.com, nimble-users
Hi Frédéric,

One would not want to pass a right-hand-side only variable to $calculate. Rather than using `$getVarNames()` (which includes right-hand-side only variables in its output), please use `$getNodeNames()` and `$getDependencies()` to determine nodes for calculation. By default, those methods will not include right-hand-side only nodes in their output.

-chris

Reply all
Reply to author
Forward
0 new messages