Performance tip: StatsFromWeightedGrid

When calibrating/estimating a model, it’s important to compute moments in an efficient way. In the current version of the toolkit, interp1 in the function StatsFromWeightedGrid is a bottleneck.

In my experience, interp1 is an old/slow routine for linear interpolation and it could be replaced with griddedInterpolant (which also works on gpu arrays). I quote from the Matlab website:

" There are memory and performance benefits to using griddedInterpolant objects over the interp functions. griddedInterpolant offers substantial performance improvements for repeated queries of the interpolant object, whereas the interp functions perform a new calculation each time they are called. Also, griddedInterpolant stores the sample points in a memory-efficient format (as a compact grid) and is multithreaded to take advantage of multicore computer processors."
Reference: https://www.mathworks.com/help/matlab/math/interpolating-gridded-data.html

Other alternatives, way faster than interp1:

If you replace interp1 with griddedInterpolant in StatsFromWeightedGrid is it actually faster? If yes, I will change it.

[Needs to be shown to be faster, not just that it should be faster in principle.]

(Or you can even set it up as a pull request :slight_smile: )

1 Like

We may have to change the code slightly, because you cannot just replace interp1 with griddedInterpolant. The difference is:

xq = interp1(xgrid,ygrid,xy,'linear');

vs

F = griddedInterpolant(xgrid,ygrid,'linear');
yq = F(xq);

Moreover, there can be differences in how the two routines deal with points outside of the grid i.e. if xq<xgrid(1) or xq>xgrid(end)

I will do some tests and write again in this post

1 Like

Great! :smiley:

Because of how they are used in StatsFromWeightedGrid, they are never going to extrapolate so any differences there shouldn’t matter.

1 Like