Global variable in functions block

I want a global variable (which is a vector in this case), which I can declare once and use throughout my functions block. The compiler is not accepting it. Is there such provision in stan?

No. There are no global variables.

We, the Stan development team, have discussed things like this, but it’s never gotten further. Part of it is because the cons outweigh the pros, but also because it’s lower priority (even if we were to design it properly) and we’re just tackling other things.

We’re always looking for more contributors. If that’s of interest, it would help to explain why it’s useful, how exactly that would be specified. If you have time to work on it to help Stan, then even better!

I think at most we’d add closures so that function bodies could implicitly depend on all variables in scope.

1 Like

What if one would like to use the reduce_sum function to speed up computation time, but at the same time he/she wish to store some intermediate values computed in the partial_sum function? In that case being able to access and update a global variable could be useful.

For example, I am using a finite mixture model and I would like to store the value of the log likelihood for a certain mixture component and for each data point. at the same time, I am using the reduce_sum function to add partial sums to the total log likelihood, and those values are computed inside partial_sums. How should I write the program to still take advantage of the reduce_sum speed up while accessing the probabilities for a data point to be generated by a given component?

I guess one should use map_rect instead of reduce_sum in this case.