0

I would like to do a Bayesian fit of some experimental observations.

The function that wraps up my model is not analytical and requires several numeric integrals. Therefore, it requires some time (approximately ~0.5 s) to be evaluated. This becomes a problem since the fitting procedure calls many times the function, each time varying its parameters.

I attempted to make the code run faster by vectorizing it as much as possible (with numpy) and I also used a just in time compiler (numba.jit). I gained a factor 2 in the computation times, but still this is not enough.

I am aiming at computation times of the order of milliseconds.

I thought that a reasonable solution could be to compute in advance the values of the function for a grid of the input parameters and make a lookup table.

I would like to know which is the best way to implement this. Expecially regarding:

  1. How to save the precomputed values of the function to maximize the speed (data structure, file format)?
  2. How to open/access the precomputed data?
  3. Which interpolation technique to use for evaluating the function on some value of the parameters which are not on the lookup table.
  • 1
    While I'm sure optimizations can and have been done this way, if you really want to go for speed consider using a compiled language like C++. Then compile that, execute it from Python and retrieve the return values for further analysis - or move your entire project over to the other language (see also https://stackoverflow.com/questions/16512817/numerical-integration-in-c) – asdf101 Apr 23 '21 at 14:12
  • @asdf oving everything to C++ could be an option, but an unpleasant one, as I am not proficient in C. Anyhow I used a just-in-time compiler to speed up the code. Which performance increase would I get in C with respect to a just-in-time compiled code? – Davide Dal Bosco Apr 23 '21 at 14:22
  • That seems to depend on whether you also optimize your C++ code instead of just straight up porting it, the difference is much less than I thought so maybe it is not the best option after all: https://murillogroupmsu.com/numba-versus-c/ – asdf101 Apr 23 '21 at 14:29
  • Although your specific case may be the exception, in cases where I have gone to C to optimize lookups, I can't imagine how I could do it better in a language like python. C lets you get closer to what is actually going on. Python puts you on a higher level where it does the lower level stuff for you. Of course if you have never worked with a low level language, you have a learning curve issue there. – Andrew Allaire Apr 23 '21 at 14:34
  • 1
    You can use Cython alternatively to easily mix C and Python codes. That being said, a speed up of two using Numba is quite small. Do you have a *Minimal Reproducible Example* on which we can work on? – Jérôme Richard Apr 23 '21 at 17:38

0 Answers0