From: Anthony on
Hi,

I'm trying to find a faster way to run through this loop. I have a matrix size(Length-D+2,NSims) and I have to run through each column, and check if there are D (=6) consecutive rows of values < H. If there is at least one series of 6 where all the values are less than H then I can assign to the Payoff for that particular column 0. If not, I must move down in the column and check the next 6 values.

i.e: if between 1-6 the values are not all < H, then check 2-7 etc...

If there are no 6 consecutive values < H in the colum then the Payoff is the max instruction below.

This works reasonably fast for an array with NSims = 10 000, but I have to do 1 000 000 and it becomes very slow...

Any ideas on how to make this quicker?

for i = 1:NSims
for j = 1:Length-D+2
if StockPrice(j:j+D-1,i) <= H
ParisPayoffs(1,i) = 0;
break
else
ParisPayoffs(1,i) = max(StockPrice(end,i) - K,0);
end
end
end

P.S. First post, I hope it's a good one!

Thanks!
Anthony
From: Matt Fig on
If your StockPrice array is size:

Length-D+2

(as you state) and you index into it with jj equal to

Length-D+2

as:

jj:(jj+D-1)

then you should be getting a Matrix exceeds dimensions error because:

(Length + 1) > (Length - D + 2)

for D=6.

Have you not encountered this error before? After you solve that mystery, what are typical values for Length? And are you pre-allocating ParisPayoffs before the loop?
From: Bruno Luong on
"Anthony " <antfarinaccio(a)gmail.com> wrote in message <hpp1b4$r6p$1(a)fred.mathworks.com>...
> Hi,
>
> I'm trying to find a faster way to run through this loop. I have a matrix size(Length-D+2,NSims) and I have to run through each column, and check if there are D (=6) consecutive rows of values < H. If there is at least one series of 6 where all the values are less than H then I can assign to the Payoff for that particular column 0. If not, I must move down in the column and check the next 6 values.
>
> i.e: if between 1-6 the values are not all < H, then check 2-7 etc...
>
> If there are no 6 consecutive values < H in the colum then the Payoff is the max instruction below.
>
> This works reasonably fast for an array with NSims = 10 000, but I have to do 1 000 000 and it becomes very slow...
>
> Any ideas on how to make this quicker?
>
> for i = 1:NSims
> for j = 1:Length-D+2
> if StockPrice(j:j+D-1,i) <= H
> ParisPayoffs(1,i) = 0;
> break
> else
> ParisPayoffs(1,i) = max(StockPrice(end,i) - K,0);
> end
> end
> end

As Matt pointed out, I guess the j must run up to Length-D+1. Here is a solution using a tool on FEX:

http://www.mathworks.com/matlabcentral/fileexchange/24705-minmax-filter

% Data
NSims = 100;
Length = 1000;
D = 6;
H=0.3;
K = 0.1;
StockPrice=rand(Length,NSims);

% Engine
MaxD = minmaxfilt(StockPrice,[D 1],'max','valid');
OK = ~any(MaxD<=H,1);
ParisPayoffs = zeros(size(OK));
ParisPayoffs(OK) = max(StockPrice(end,OK) - K,0)

% Bruno
From: Anthony on

Hey Bruno,

I get the following error when I try to run the minmaxfilter:

??? Attempt to execute SCRIPT lemire_nd_maxengine as a function:

Any ideas?

Thanks!
Anthony




"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hppggq$f4a$1(a)fred.mathworks.com>...
> "Anthony " <antfarinaccio(a)gmail.com> wrote in message <hpp1b4$r6p$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I'm trying to find a faster way to run through this loop. I have a matrix size(Length-D+2,NSims) and I have to run through each column, and check if there are D (=6) consecutive rows of values < H. If there is at least one series of 6 where all the values are less than H then I can assign to the Payoff for that particular column 0. If not, I must move down in the column and check the next 6 values.
> >
> > i.e: if between 1-6 the values are not all < H, then check 2-7 etc...
> >
> > If there are no 6 consecutive values < H in the colum then the Payoff is the max instruction below.
> >
> > This works reasonably fast for an array with NSims = 10 000, but I have to do 1 000 000 and it becomes very slow...
> >
> > Any ideas on how to make this quicker?
> >
> > for i = 1:NSims
> > for j = 1:Length-D+2
> > if StockPrice(j:j+D-1,i) <= H
> > ParisPayoffs(1,i) = 0;
> > break
> > else
> > ParisPayoffs(1,i) = max(StockPrice(end,i) - K,0);
> > end
> > end
> > end
>
> As Matt pointed out, I guess the j must run up to Length-D+1. Here is a solution using a tool on FEX:
>
> http://www.mathworks.com/matlabcentral/fileexchange/24705-minmax-filter
>
> % Data
> NSims = 100;
> Length = 1000;
> D = 6;
> H=0.3;
> K = 0.1;
> StockPrice=rand(Length,NSims);
>
> % Engine
> MaxD = minmaxfilt(StockPrice,[D 1],'max','valid');
> OK = ~any(MaxD<=H,1);
> ParisPayoffs = zeros(size(OK));
> ParisPayoffs(OK) = max(StockPrice(end,OK) - K,0)
>
> % Bruno
From: Bruno Luong on
"Anthony " <antfarinaccio(a)gmail.comremove.spam> wrote in message <hpq07v$9jo$1(a)fred.mathworks.com>...
>
> Hey Bruno,
>
> I get the following error when I try to run the minmaxfilter:
>
> ??? Attempt to execute SCRIPT lemire_nd_maxengine as a function:
>
> Any ideas?
>

Yes, your have to install by running (you need to have MEX setup)
minmaxfilter_install

Bruno