Most efficient way (run time) to enable/disable part of a model via toggles

Hello!

Suppose I have some likelihood statement

y ~ normal(a + toggle*f(x), sigma)

where:

  • toggle is either 0 or 1 and specified as data
  • f(x) is some computationally intensive function that is a function of x (or many other variables).

How does the auto-diff treat this? Is f(x) evaluated even if toggle=0?

Would a more efficient way of coding this be the following?

if(toggle == 0){
   y ~ normal(a, sigma)
} else {
   y ~ normal(a + f(x), sigma)
}

…or is there no difference between them?

Thanks!

Hi Julian,

The second option would be more efficient, since the first would still evaluate the function and then multiply it by zero.

Cheers,
Andrew