improve efficiency of a matlab code that includes for loop and if statement

I have the followig script which I am running for a matrix that consists of several thousand rows and which makes the full ccode very slow. Is there any way to improve the speed of the code?
Here you find the code which is saved in a seperate script and which is called by the function run the speciific script:
dataset=zeros(size(data9,1),9);
dataset(:,1:9)=data9;for i=1:length(dataset(:,1))
%h=msgbox(num2str(i))
if dataset(i,3)==1
vola(i)=blkimpv(dataset(i,1),dataset(i,2),dataset(i,7),dataset(i,5),dataset(i,4)); elseif dataset(i,3)==2
vola(i)=blkimpv(dataset(i,1),dataset(i,2),dataset(i,7),dataset(i,5),dataset(i,4),10,[],{'Put'});
end
end
Which improvements are there to get the code execution the desired functions much faster?

 Akzeptierte Antwort

Cedric
Cedric am 22 Apr. 2013
Bearbeitet: Cedric am 22 Apr. 2013
It really depends whether BLKIMPV can work on vectors. If so, you can go for something like:
dataset = zeros(size(data9,1), 9) ;
dataset(:,1:9) = data9;
vola = zeros(size(dataset,1), 1) ;
id = dataset(i,3) == 1 ;
vola(id) = blkimpv(dataset(id,1), dataset(id,2), dataset(id,7), ...
dataset(id,5), dataset(id,4)) ;
id = dataset(i,3) == 2 ;
vola(id) = blkimpv(dataset(id,1), dataset(id,2), dataset(id,7), ...
dataset(id,5), dataset(id,4), 10, [], {'Put'}) ;
where you might have to replace 10 and {'Put'} with arrays of these values.
I don't have the Financial Toolbox though, so I can't test.

6 Kommentare

I change the code to this here:
dataset = zeros(size(data9,1), 9) ;
dataset(:,1:9) = data9;
vector=ones(size(data9,1), 1) ;
vector=10*vector;
for i=1:length(dataset(:,1))
if dataset(i,3)==1
S(i)=1;
elseif dataset (i,3)==2
S(i)=0;
end
end
dataset1=zeros(size(data9,1), 10);
dataset1(:,1:9) = dataset;
dataset1(:,10)=S;
numberEl=length(dataset1(:,1));
i=[1:numberEl]';
vola = zeros(size(dataset,1), 1) ;
vola(i)=blkimpv(dataset1(i,1), dataset1(i,2), dataset1(i,7), ...
dataset1(i,5), dataset1(i,4), vector(i),1e-6, dataset1(i,10));
%create the new matrix called data9 which includes the implied volatility
data10=zeros(size(dataset1,1),10);
data10(:,1:9)=dataset1(1:9);
It's not that slow anymore, but it still takes around 6 minuted to computed it for the whole dataset and I will probably have to increase the dataset about 8 times what it is now. Is there a way to further speed up the code or is just the blkimpv so slow that there is nothing that can be done about it? data10(:,10)=vola;
It seems then that BLKIMPV can work on vectors. I would get rid of the FOR loop that defines S the same way you got rid of the loop calling BLKIMPV. Also, I don't understand why you define dataset when it is strictly equal to data9. You could reduce what you just wrote to:
nData = size(data9, 1) ;
% - Define limits and tolerance.
lim = 10 * ones(nData, 1) ;
tol = 1e-6 ;
% - Define classes.
clas = true(nData, 1) ; % Default is true/call.
id = data9(:,3) == 2 ;
clas(id) = false ; % Update relevant elements to false/put.
% - Compute volatilities.
vola = blkimpv(data9(:,1), data9(:,2), data9(:,7), data9(:,5), ...
data9(:,4), lim, tol, clas);
% - Build data10 merging data9 and the volatilities.
data10 = [data9, vola] ;
Now if you want to see what takes time, you can either use TIC/TOC commands around a few of these statements (see doc) or use the profiler. To use the latter, type
profile viewer
in the command line. Then type the name of the script that you want to profile in the field "Run this code:", and click on "Start profiling". You will see the time taken by every line, the number of calls, etc.
I dit that with the function you gave me and it shows me that the blkimpv takes 129s so I guess there is nothing I can do to improve it?
You can read very carefully its doc and see if it takes extra params that could be used to better select how your data is processed (eliminate operations, etc), but other than that, I don't think that there is much that you can do except using another function (changing the approach), or rewritting it specifically for what you need.
ok, I will try that. thanks for your help!
You're welcome! Try to use the profiler on a regular basis in the beginning (even when you don't really need to); it will give you a lot of insights into your code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Historical Contests finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 22 Apr. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by