2021年4月2日星期五

Tricks to improve the performance of a cunstom function in Julia

I am replicating using Julia a sequence of steps originally made in Matlab. In Octave, this procedure takes 1.4582 seconds and in Julia (using Jupyter) it takes approximately 10 seconds. I'll try to be brief in the scripts. My goal is to achieve or improve Octave's performance. First of all, I will describe my variables and some function:

  1. zgrid (double 1x7 size)
  2. kgrid (double 500x1 size)
  3. V0 (double 500x7 size)
  4. P (double 7x7 size) a transition matrix
  5. delta and beta are fixed parameters.
  6. F(z,k) and u(c) are particular functions
% Octave script  % V0 is given  [K, Z, K2] = meshgrid(kgrid, zgrid, kgrid);  K = permute(K, [2, 1, 3]);  Z = permute(Z, [2, 1, 3]);  K2 = permute(K2, [2, 1, 3]);    C = max(f(Z,K) + (1-delta)*K - K2,0);  U = u(C);    EV = V0*P';% EV is a 500x7 matrix size  EV = permute(repmat(EV, 1, 1, 500), [3, 2, 1]);  H = U + beta*EV;  [TV, index] = max(H, [], 3);  

In Julia, I created a function that replicates this procedure. I used loops, but it has a performance 9 times longer.

% Julia script  % V0 is the input of my T operator function  V0 = repeat(sqrt.(kgrid), outer = [1,7]);    function T(V)      E=V*P'      T1 = zeros(Float64, 500, 7 )      aux = zeros(Float64, 500)      for i = 1:7          for j = 1:500              for l = 1:500                  c= maximum( (F(zrid[i],kgrid[j]) +(1-δ)*kgrid[j] - kgrid[l]  ,0))                  aux[l] =  u(c) + β*E[l,i]              end              T1[j,i] = maximum(aux)               end      end      return T1  end  

I would very much like to improve my performance in Julia. I believe there is a way to improve, but I am new in Julia programming.

https://stackoverflow.com/questions/66926866/tricks-to-improve-the-performance-of-a-cunstom-function-in-julia April 03, 2021 at 09:05AM

没有评论:

发表评论