From: Christopher on
I have a simulation that uses a process where the next "step" in the simulation depends on the previous one (interest rates, CIR model).

Right now, I am looping through the steps in order to calculate this. Is there a way to vectorize it?
From: Andy on
"Christopher " <christophermcleester(a)removethisexeloncorp.com> wrote in message <i43eh1$hmc$1(a)fred.mathworks.com>...
> I have a simulation that uses a process where the next "step" in the simulation depends on the previous one (interest rates, CIR model).
>
> Right now, I am looping through the steps in order to calculate this. Is there a way to vectorize it?

How can we possibly know whether your code is vectorizable when you haven't told us what your simulation does; you haven't said why your next step depends on the previous one; and you haven't shown us your code?
From: Christopher on
Sorry, I was trying to offer a general question and not bug people to fix my personal code. It uses a rather basic interest rate simulation process called CIR. I simulate it in log space and then exponentiate. You can see that I have a loop through the time steps.

http://en.wikipedia.org/wiki/CIR_model


function [ r ] = CIR2(seed, a, b, s, steps, numsims)
%CIR Model

r=zeros(steps,numsims);
r(1,:)=seed;

for i=1:steps; %number of steps, step length must match parameters
logr(i,:)=log(r(i,:));
drift(i,:)=(1./r(i,:)).*(a*(b-r(i,:))-.5.*s.^2);
stoc(i,:)=(s./sqrt(r(i,:))).*randn(1,numsims);
logstep(i,:)=drift(i,:)+stoc(i,:);
logr(i,:)=logr(i,:)+logstep(i,:);
r(i+1,:)=exp(logr(i,:));
end

end


"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i43f7g$2eu$1(a)fred.mathworks.com>...
> "Christopher " <christophermcleester(a)removethisexeloncorp.com> wrote in message <i43eh1$hmc$1(a)fred.mathworks.com>...
> > I have a simulation that uses a process where the next "step" in the simulation depends on the previous one (interest rates, CIR model).
> >
> > Right now, I am looping through the steps in order to calculate this. Is there a way to vectorize it?
>
> How can we possibly know whether your code is vectorizable when you haven't told us what your simulation does; you haven't said why your next step depends on the previous one; and you haven't shown us your code?
From: Andy on
I'm not sure about how to vectorize this. (I don't have time to read the Wikipedia article right now, and I'm unfamiliar with the theory.) But you can speed up your code by fixing a few things. The variable logr, drift, stoc, and logstep all grow on each iteration of the for loop. Normally, I would say preallocate them instead. However, in this case, since you don't save any of these variables or output them, you should just get rid of their indexing and use them as temporary variables inside the loop:

function [ r ] = CIR2(seed, a, b, s, steps, numsims)
%CIR Model

r=zeros(steps,numsims);
r(1,:)=seed;

for ix=1:steps; %number of steps, step length must match parameters
logr=log(r(ix,:));
drift=(1./r(ix,:)).*(a*(b-r(ix,:))-.5.*s.^2);
stoc=(s./sqrt(r(ix,:))).*randn(1,numsims);
logstep=drift+stoc;
logr=logr+logstep;
r(ix+1,:)=exp(logr);
end

end

Note: I also changed the indexing variable to ix instead of i. Both i and j in MATLAB are used for the complex unit. Standard practice is to use ix and jx for indexing variables.
From: Christopher on
Thanks for the advice, Andy!