NOAA-FIMS / FIMS

The repository for development of FIMS
https://noaa-fims.github.io/FIMS/
GNU General Public License v3.0
12 stars 8 forks source link

Vectorize distributions #192

Open Andrea-Havron-NOAA opened 2 years ago

Andrea-Havron-NOAA commented 2 years ago

It is more efficience to calculate vectorized negative log-likelihoods than to loop through individual observations.

We may want to vectorize this in the future. It's redundant/inefficient to calculate the constants logy.size() times. Stan is very strict about this in their code base.

_Originally posted by @Cole-Monnahan-NOAA in https://github.com/NOAA-FIMS/FIMS/pull/181#discussion_r908960117_

Andrea-Havron-NOAA commented 12 months ago

Related to Issue #334

msupernaw commented 12 months ago

Vectorizing won't make the calculations more efficient, the function to do the vectorization will still have to loop through all the elements. It may be easier for developers to rely on vectorized methods to simply coding though.

Cole-Monnahan-NOAA commented 5 months ago

I forgot to respond earlier but from the Stan manual (page 14 of this pdf):

"Unlike in Python and R, which are interpreted, Stan is translated to C++ and compiled, so loops and assignment statements are fast. Vectorized code is faster in Stan because (a) the expression tree used to compute derivatives can be simplified, leading to fewer virtual function calls, and (b) computations that would be repeated in the looping version, such as log(sigma) in the above model, will be computed once and reused."

I'm not sure about (a) but I was referring to (b) when I made the issue. Overall this will be a very small effect and is thus very low priority in my opinion.

ChristineStawitz-NOAA commented 1 month ago

I think @msupernaw has a prototype for this in the linked branch