Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Vectorize a for loop (with fminsearch inside)

Subject: Vectorize a for loop (with fminsearch inside)

From: Saad

Date: 18 Aug, 2011 11:01:10

Message: 1 of 9

Dear all,

I have a long for loop that slows down my code. Please read below.


%%%the command file%%%%%%

%%upload data

Data_Bond=xlsread('H:\paper 3\5 Year Bond Spread\five_year_Bond_Spread.xls', 'BBB','G121:G1403');


n_b=0.4;
n_ask=0.5;
n_bid=0.6;
n_gb=0.1;
n_gcask=0.8;
n_gcbid=0.10;
sig=100;
beta=1;
alpha=0.1;
n=[n_b, n_ask, n_bid, n_gb, n_gcask, n_gcbid, sig, beta, alpha];

H=eye(7);

start = [4, -1 , 1 , 7 ,1, 2 , 2 ];

 for i=1:length(Data_Bond)

 x = fminsearch(@(bb)yy2( Data_Bond(i,:), bb, H, n), start)
 
end

%%%%%%the function to minimize%%%%%

function y1= yy2(Data_Bond, bb,H,n)

 y1= (bond3(bb,H,n)-Data_Bond)^2;

%% bond3(bb,H,n) is function that gives a scalar as output

My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much

Subject: Vectorize a for loop (with fminsearch inside)

From: John D'Errico

Date: 18 Aug, 2011 11:55:11

Message: 2 of 9

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2irdm$p38$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I have a long for loop that slows down my code. Please read below.
>
>
> %%%the command file%%%%%%
>
> %%upload data
>
> Data_Bond=xlsread('H:\paper 3\5 Year Bond Spread\five_year_Bond_Spread.xls', 'BBB','G121:G1403');
>
>
> n_b=0.4;
> n_ask=0.5;
> n_bid=0.6;
> n_gb=0.1;
> n_gcask=0.8;
> n_gcbid=0.10;
> sig=100;
> beta=1;
> alpha=0.1;
> n=[n_b, n_ask, n_bid, n_gb, n_gcask, n_gcbid, sig, beta, alpha];
>
> H=eye(7);
>
> start = [4, -1 , 1 , 7 ,1, 2 , 2 ];
>
> for i=1:length(Data_Bond)
>
> x = fminsearch(@(bb)yy2( Data_Bond(i,:), bb, H, n), start)
>
> end
>
> %%%%%%the function to minimize%%%%%
>
> function y1= yy2(Data_Bond, bb,H,n)
>
> y1= (bond3(bb,H,n)-Data_Bond)^2;
>
> %% bond3(bb,H,n) is function that gives a scalar as output
>
> My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much

No. Not with fminsearch inside the loop.

John

Subject: Vectorize a for loop (with fminsearch inside)

From: Saad

Date: 18 Aug, 2011 13:59:09

Message: 3 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <j2iuiv$4rq$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2irdm$p38$1@newscl01ah.mathworks.com>...
> > Dear all,
> >
> > I have a long for loop that slows down my code. Please read below.
> >
> >
> > %%%the command file%%%%%%
> >
> > %%upload data
> >
> > Data_Bond=xlsread('H:\paper 3\5 Year Bond Spread\five_year_Bond_Spread.xls', 'BBB','G121:G1403');
> >
> >
> > n_b=0.4;
> > n_ask=0.5;
> > n_bid=0.6;
> > n_gb=0.1;
> > n_gcask=0.8;
> > n_gcbid=0.10;
> > sig=100;
> > beta=1;
> > alpha=0.1;
> > n=[n_b, n_ask, n_bid, n_gb, n_gcask, n_gcbid, sig, beta, alpha];
> >
> > H=eye(7);
> >
> > start = [4, -1 , 1 , 7 ,1, 2 , 2 ];
> >
> > for i=1:length(Data_Bond)
> >
> > x = fminsearch(@(bb)yy2( Data_Bond(i,:), bb, H, n), start)
> >
> > end
> >
> > %%%%%%the function to minimize%%%%%
> >
> > function y1= yy2(Data_Bond, bb,H,n)
> >
> > y1= (bond3(bb,H,n)-Data_Bond)^2;
> >
> > %% bond3(bb,H,n) is function that gives a scalar as output
> >
> > My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much
>
> No. Not with fminsearch inside the loop.
>
> John

Hi John,

If we cannot vectorize it. Do you think it is possible to write it in a way that make the code run faster? Thanks a lot

S

Subject: Vectorize a for loop (with fminsearch inside)

From: Matt J

Date: 18 Aug, 2011 14:14:10

Message: 4 of 9

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2irdm$p38$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I have a long for loop that slows down my code. Please read below.
>
>
> %%%the command file%%%%%%
>
> %%upload data
>
> Data_Bond=xlsread('H:\paper 3\5 Year Bond Spread\five_year_Bond_Spread.xls', 'BBB','G121:G1403');
>
>
> n_b=0.4;
> n_ask=0.5;
> n_bid=0.6;
> n_gb=0.1;
> n_gcask=0.8;
> n_gcbid=0.10;
> sig=100;
> beta=1;
> alpha=0.1;
> n=[n_b, n_ask, n_bid, n_gb, n_gcask, n_gcbid, sig, beta, alpha];
>
> H=eye(7);
>
> start = [4, -1 , 1 , 7 ,1, 2 , 2 ];
>
> for i=1:length(Data_Bond)
>
> x = fminsearch(@(bb)yy2( Data_Bond(i,:), bb, H, n), start)
>
> end
>
> %%%%%%the function to minimize%%%%%
>
> function y1= yy2(Data_Bond, bb,H,n)
>
> y1= (bond3(bb,H,n)-Data_Bond)^2;
>
> %% bond3(bb,H,n) is function that gives a scalar as output
>
> My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much

I think the following will help, but I'm not sure. The main idea for acceleration is to start fminsearch at iteration i with the result of i-1. Assuming your time series progresses in a more or less continuous way, the previous solution should be pretty close to the current one. Also, it slows things greatly to omit the semicolon like you were doing, to display the result of each iteration of the loop. Save them all in a vector bbs and display them at the end.

start = [4, -1 , 1 , 7 ,1, 2 , 2 ];
bbs=nan(length(start), length(Data_Bond));

 for i=1:length(Data_Bond)

 x = fminsearch(@(bb) abs( Data_Bond(i,:) - bond3(bb,H,n) ), start); %<--INCLUDE SEMICOLON!!
 start=x;
 bbs(:,i)=x(:);

end

Subject: Vectorize a for loop (with fminsearch inside)

From: Matt J

Date: 18 Aug, 2011 14:48:09

Message: 5 of 9

"Matt J" wrote in message <j2j6ni$2ee$1@newscl01ah.mathworks.com>...
>
>
> I think the following will help, but I'm not sure. The main idea for acceleration is to start fminsearch at iteration i with the result of i-1. Assuming your time series progresses in a more or less continuous way, the previous solution should be pretty close to the current one.
========================

you should also check to see if your Data_Bond time series assumes the same values multiple times (or approximately so) using HIST or HISTC. There's not point repeating the fminsearch with the same essential Data_Bond input multiple times.

Subject: Vectorize a for loop (with fminsearch inside)

From: John D'Errico

Date: 18 Aug, 2011 14:49:10

Message: 6 of 9

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2j5rd$sq5$1@newscl01ah.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <j2iuiv$4rq$1@newscl01ah.mathworks.com>...
> > "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2irdm$p38$1@newscl01ah.mathworks.com>...
> > > Dear all,
> > >
> > > I have a long for loop that slows down my code. Please read below.
> > >
> > >
> > > %%%the command file%%%%%%
> > >
> > > %%upload data
> > >
> > > Data_Bond=xlsread('H:\paper 3\5 Year Bond Spread\five_year_Bond_Spread.xls', 'BBB','G121:G1403');
> > >
> > >
> > > n_b=0.4;
> > > n_ask=0.5;
> > > n_bid=0.6;
> > > n_gb=0.1;
> > > n_gcask=0.8;
> > > n_gcbid=0.10;
> > > sig=100;
> > > beta=1;
> > > alpha=0.1;
> > > n=[n_b, n_ask, n_bid, n_gb, n_gcask, n_gcbid, sig, beta, alpha];
> > >
> > > H=eye(7);
> > >
> > > start = [4, -1 , 1 , 7 ,1, 2 , 2 ];
> > >
> > > for i=1:length(Data_Bond)
> > >
> > > x = fminsearch(@(bb)yy2( Data_Bond(i,:), bb, H, n), start)
> > >
> > > end
> > >
> > > %%%%%%the function to minimize%%%%%
> > >
> > > function y1= yy2(Data_Bond, bb,H,n)
> > >
> > > y1= (bond3(bb,H,n)-Data_Bond)^2;
> > >
> > > %% bond3(bb,H,n) is function that gives a scalar as output
> > >
> > > My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much
> >
> > No. Not with fminsearch inside the loop.
> >
> > John
>
> Hi John,
>
> If we cannot vectorize it. Do you think it is possible to write it in a way that make the code run faster? Thanks a lot
>
> S

As Matt suggests, IF the problems are highly related, then
starting one iteration with the start point of the last
MAY make sense.

If each successive problem is essentially random, so
the final point from the last solution is no better a
choice than the arbitrary start point you have chosen,
then this is of utility.

I think a better solution is to ask why in the name of
god and little green apples, are you using fminsearch
when you are looking for efficiency and speed? That
tool is barely acceptable when you are using it for 7
variable problems.

Use a better tool from the optimization toolbox. If
you don't have that toolbox, then there must be many
optimization tools on the file exchange, or consider
TOMLAB.

John

Subject: Vectorize a for loop (with fminsearch inside)

From: Matt J

Date: 19 Aug, 2011 08:03:10

Message: 7 of 9

> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2irdm$p38$1@newscl01ah.mathworks.com>...
>
> > My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much
==================


Another direction you could try is to see if it is possible to vectorize
bond3(bb,H,n) so that instead of being a mapping R^7--->R^1
it will be mapping R^7M--->R^M where M is the length of your time series.

Then your objective function could be
 f(bb)= abs( Data_Bond - bond3(bb,H,n) )
and you can estimate bb for all Data_Bond points in a single call.

Now, as John alluded to, having 7 parameters is bad enough for FMINSEARCH and having 7M parameters would normally make things a lot worse. However, the new objective function f(bb) is additively separable across your M data points. So, I'm pretty sure FMINSEARCH would essentially operate separably on f(bb) processing it essentially as a series of parallel 7 parameter problems. I therefore wouldn't expect that working in R^7M would worsen convergence.

Subject: Vectorize a for loop (with fminsearch inside)

From: Saad

Date: 22 Aug, 2011 13:03:10

Message: 8 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <j2j8p6$9k7$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2j5rd$sq5$1@newscl01ah.mathworks.com>...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <j2iuiv$4rq$1@newscl01ah.mathworks.com>...
> > > "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2irdm$p38$1@newscl01ah.mathworks.com>...
> > > > Dear all,
> > > >
> > > > I have a long for loop that slows down my code. Please read below.
> > > >
> > > >
> > > > %%%the command file%%%%%%
> > > >
> > > > %%upload data
> > > >
> > > > Data_Bond=xlsread('H:\paper 3\5 Year Bond Spread\five_year_Bond_Spread.xls', 'BBB','G121:G1403');
> > > >
> > > >
> > > > n_b=0.4;
> > > > n_ask=0.5;
> > > > n_bid=0.6;
> > > > n_gb=0.1;
> > > > n_gcask=0.8;
> > > > n_gcbid=0.10;
> > > > sig=100;
> > > > beta=1;
> > > > alpha=0.1;
> > > > n=[n_b, n_ask, n_bid, n_gb, n_gcask, n_gcbid, sig, beta, alpha];
> > > >
> > > > H=eye(7);
> > > >
> > > > start = [4, -1 , 1 , 7 ,1, 2 , 2 ];
> > > >
> > > > for i=1:length(Data_Bond)
> > > >
> > > > x = fminsearch(@(bb)yy2( Data_Bond(i,:), bb, H, n), start)
> > > >
> > > > end
> > > >
> > > > %%%%%%the function to minimize%%%%%
> > > >
> > > > function y1= yy2(Data_Bond, bb,H,n)
> > > >
> > > > y1= (bond3(bb,H,n)-Data_Bond)^2;
> > > >
> > > > %% bond3(bb,H,n) is function that gives a scalar as output
> > > >
> > > > My aim is that for each data point in Data_Bond, I would like to minimize the function "bond3" by changing bb. It works fine but it is taking too long because my time series is long. The loop minimizes at each iteration. Is there any way I could vectorize the code to make it more efficient? Thanks so much
> > >
> > > No. Not with fminsearch inside the loop.
> > >
> > > John
> >
> > Hi John,
> >
> > If we cannot vectorize it. Do you think it is possible to write it in a way that make the code run faster? Thanks a lot
> >
> > S
>
> As Matt suggests, IF the problems are highly related, then
> starting one iteration with the start point of the last
> MAY make sense.
>
> If each successive problem is essentially random, so
> the final point from the last solution is no better a
> choice than the arbitrary start point you have chosen,
> then this is of utility.
>
> I think a better solution is to ask why in the name of
> god and little green apples, are you using fminsearch
> when you are looking for efficiency and speed? That
> tool is barely acceptable when you are using it for 7
> variable problems.
>
> Use a better tool from the optimization toolbox. If
> you don't have that toolbox, then there must be many
> optimization tools on the file exchange, or consider
> TOMLAB.
>
> John

Hi Matt/ John
Thank you very much for the suggestion below


 for i=1:length(Data_Bond)

 x = fminsearch(@(bb) abs( Data_Bond(i,:) - bond3(bb,H,n) ), start); %<--INCLUDE SEMICOLON!!
 start=x;
 bbs(:,i)=x(:);

end

It increased the speed dramatically. John you mentioned optimization toolbox ( I have access to it) what tools do you recommend to use for optimization with many parameters say 7+ ? Thanks a lot

Best
S

Subject: Vectorize a for loop (with fminsearch inside)

From: Matt J

Date: 22 Aug, 2011 13:29:10

Message: 9 of 9

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2tk2e$8kj$1@newscl01ah.mathworks.com>...
>
> It increased the speed dramatically. John you mentioned optimization toolbox ( I have access to it) what tools do you recommend to use for optimization with many parameters say 7+ ? Thanks a lot
===================

It depends on the form of bond3. Probably LSQCURVEFIT.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us