Filter löschen
Filter löschen

reduce execution time in matlab

3 Ansichten (letzte 30 Tage)
tilfani oussama
tilfani oussama am 10 Okt. 2018
Kommentiert: dpb am 12 Okt. 2018
I have a matlab programm with a loop, it takes with a machine of 16GO RAM, 7 hours to be executed. The aim is to compute a cross-correlation coefficient on a moving window (for each 1000 observations we compute using a DCCA function a detrended cross correlation); The program is as follows:
X and Y are two vectors with same length (5000 observation), for a given n=10 (20,40,....)
ft=zeros(1000,1);
gt=zeros(1000,1);
FT=zeros(1000,1) ;
Gt=zeros(1000,1) ;
CovXY=zeros(length(X)-999,1) ;
VarX=zeros(length(X)-999,1) ;
VarY=zeros(length(X)-999,1) ;
rho=zeros(1,length(X)-999) ;
x=zeros(1000,1) ;
y=zeros(1000,1);
for j=1:length(X)-999
x=X(j :j+999);
y=Y(j :j+999) ;
[parameters, ll, Ft, VCV, scores] = garchpq(x,1,1) ;
clear parameters ll VCV scores
[parameters, ll, Gt, VCV, scores] = garchpq(y,1,1) ;
clear parameters ll VCV scores
for k=1 :1000
ft(k)=x(k)/sqrt(Ft(k));
gt(k)=y(k)/sqrt(Gt(k)) ;
end
[CovXY(j, :),VarX(j, :),VarY(j, :)]=DCCA(ft,gt,n);
rho(j)=CovXY(j)/(VarX(j)*VarY(j)) ;
end
end
I'm asking someone to help me, in order to reduce execution time, i tried preallocation. I don't know what can i do else. For one segment the program takes some second.
  15 Kommentare
Greg
Greg am 10 Okt. 2018
You've ignored both suggestions to run the profiler. It is specifically designed to help you do exactly what you are trying to do - identify slow lines of code to try to speed them up.
We can guess what is slowing you down. The profile will know what is doing it.
dpb
dpb am 11 Okt. 2018
"Yes garch0 (p q) are parameters"
Well, that's not what' written in the code posted. Nor does the return match the profile...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 11 Okt. 2018
Bearbeitet: Bruno Luong am 11 Okt. 2018
Please try to replace the DCCA with this function
function [CovXY,VarX,VarY]=DCCA_vec(Data1,Data2,n)
X=cumsum(Data1); %%integrate and set profile series
Y=cumsum(Data2);
X=transpose(X);
Y=transpose(Y);
N=length(X);
x = (0:n).';
V = [x ones(n+1,1)];
P = V*pinv(V);
Q = eye(n+1)-P;
Q = fliplr(Q);
R0=conv2(X,Q);
R0=R0(:,n+1:N);
R1=conv2(Y,Q);
R1=R1(:,n+1:N);
CovXY = sum(R0.*R1,1)/(n+1);
CovX = sum(R0.*R0,1)/(n+1);
CovY = sum(R1.*R1,1)/(n+1);
CovXY = mean(CovXY);
VarX = sqrt(mean(CovX));
VarY = sqrt(mean(CovY));
end
The loop
for k=1 :1000
ft(k)=x(k)/sqrt(Ft(k));
gt(k)=y(k)/sqrt(Gt(k)) ;
end
can be replaced by
ft = x ./ sqrt(Ft);
gt = y ./ sqrt(Gt);
  8 Kommentare
Bruno Luong
Bruno Luong am 12 Okt. 2018
Bearbeitet: Bruno Luong am 12 Okt. 2018
@tilfani
Sorry, the garchpq() calls a big hammer of optimization, the fmincon() with an unknown objective function heavy_likelihood() (not include in the code).
I'm not surprised that takes most of the time, even look at the objective function. There is not much I can do to improve it, without warranty that you get an as good accurate output.
You should go back and report the time issue to your advisor/boss and trying to find another alternative for whatever problem you want to solve.
OCDER
OCDER am 12 Okt. 2018
Hm, saw this in the garchpq code. nested for loop AND growing matrix for K^2 potential matrix copies. Times that by 8000 iterations (two garchpq summons for X = 1:5000-999) for your application. The time could add up quickly. I wish we had a way to profile how much time each step actually takes....
temp = O';
for i=1:K
for j = 1:K
temp = [temp squeeze(A(i,j,1:p(i,j)))]; %#ok<AGROW>
end
end
Optimization is a time-consuming process. Perhaps hire someone who is good at MATLAB to optimize this code? Otherwise, you'll have to go through every code and rework it, test it, rework, test, ....
If a built-in matlab process becomes the bottleneck, you'll have to use some advance tricks to speed things up like what Yair does:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Bruno Luong
Bruno Luong am 12 Okt. 2018
Bearbeitet: Bruno Luong am 12 Okt. 2018
@dpb
The comment %#ok<AGROW> tells the author (Kevin Sheppard) is well aware about growing, and he obviously doesn't care.
The code garhcpq.m looks well written, contrary to the function DCCA() which is ugly IMHO.
  5 Kommentare
Bruno Luong
Bruno Luong am 12 Okt. 2018
Bearbeitet: Bruno Luong am 12 Okt. 2018
This loop is just for building the starting guess vector (composed by few segments) for FMINCON, and believe me this is next to nothing the work that would be carried out later.
The delta growing is not constant, granted can be computed without the loop growing. But there are few reasons why the author prefer coding that way:
  • Because it's not important?
  • The author prefer spending his time for something else?
  • Because the code is more readable in this way?
  • Because the code is more easily to be modified and maintainable in this way?
dpb
dpb am 12 Okt. 2018
I'm not arguing it would make any noticeable improvement; just stuck out at first glance so pointed it out is all...
As for the other three points all it would have taken is writing one line using an already-defined variable and modifications would have been automagic that way just as are as is...took almost as long for the author to acknowledge the "ignore" message as it would have to have fixed at the time.
A big deal? In this case, no. In general, poor coding practice is still poor coding practice.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by