Extremely slow interp1
Hi,
Could you please advise why interp1 with mp is so slow compared to original matlab interp1? belwo is the example I'm running. Thank you for your kind help
mohsen
% Grid length and useful vectors
N = 200;
kp = mp(linspace(1e-6,5,N))';
n = mp( ones(1,2)); nn = mp(ones(N,1));
% parameters & given values
gam = 2; beta = 0.96;
R = 1.05; w0 = 3.44;
prob = mp([0.9500, 0.0500; 0.6750, 0.3250]);
w = mp(sort( w0*( .995 + randn(N,1)/7.5 ) ));
W = [w, 0*nn];
% initialize
Cp = mp(R)*kp*n; k = Cp;
% convergence criterion
Cp_crit = 1;
% Interpolate until convergence
ii = 0; tic
while Cp_crit > 1e-3
Cp0 = Cp;
EMUp = (Cp.^(-mp(gam)))*prob';
C = (mp(beta*R)*EMUp).^(-1/mp(gam));
k = ( kp*n + C - W)/mp(R);
% Use the relation between C and k to ropose a new vector Cp corresponding to kp
for i=1:2
% Update the function by interpolation.
Cp(:,i) = interp1(k(:,i), C(:,i), kp,'linear','extrap');
end
Cp_crit = max(max(abs(Cp0-Cp)./(1+abs(Cp))));
ii = ii+ 1;
end
toc
disp(Cp_crit)
Servicio de atención al cliente por UserEcho
Dear Mohsen,
You are absolutely right - tests show that interp1 is unacceptably slow!
The slowest code is index lookup of interval where x belongs while interpolation.
Now it is simple linear search and of course should be improved.
Basically we have to implement histcounts, which is not trivial.
Will try to improve this in near days and let you know about the result.
Dear Mohsen,
Please download & install new version of toolbox: http://goo.gl/pMXV3
Now it has much faster interp1.
Also I have updated your script with small corrections to be more mp-fiendly:
Older version has following timings for N=50: New one is ~25 times faster: But, still it might take a lot of time for N=200 to converge. Probably you need to re-design your algorithm to converge in less iterations.If you are Ok - I will make this thread public, it might be useful for other users.
Hi Pavel,
Thank you very much for the prompt and kind reply. The newer version works better, so thank you very much for the time and effort you put in this. The improvement is surely there, yet accelerating the execution time of the command would be very welcome.
Please go ahead and make the thread public if you think it may help other users.
Best,
Mohsen
Dear Mohsen,
Later today we will release new version of toolbox with even faster interp1. Now interp1 hot-spots are implemented directly using C++ and thus it is ~100 times faster overall compared to original version.
However, I am sure that the issue is not related speed, but rather to the properties of the method you use in your script. Is convergence guaranteed for all N?
For some N it converges very quickly, for others - no convergence even after thousands of iterations.
N=79:
N=80: Updated version will be available in a few hours.