Search code examples
matlabsimulationmontecarloquantitative-finance

Creating a 1000x252 Path Simulation


I am trying to create a Monte Carlo simulation with 1000 simulations and 252 days. So I need to create a 1000x252 matrix, my code is:

T=252
L=zeros(i)
eps=normrnd(0,1,[i,T])
S0=2809
K=2750

for i=1:252
    for c=1:L
    S(c,i)=S0*exp((.0295-.5*(.2^2))*.004+.0295*sqrt(.004)*eps(c,i))
    end
end

Before, I only had one for statement, so I didn't have for c=1:L, and I was able to generate the results I wanted. But now that I want 1000x252 I get:

row 1 column 1 to 252 that is correct

row 2 to 1000, column 1 is just a copy of row 1 column 1

the rest, row 2 to 1000, column 2 to 252, are all zeros.

Is my problem with the way I am indexing or the way I am referencing my variable eps in my for loop?


Solution

  • Few mistakes

    • Set L = 1000;
    • Replace i here by L eps=normrnd(0,1,[i,T]);
    • Initialize matrix S with zeros and fixed size S = zeros(L,T);

    The code is as follow

    T=252;
    L= 1000;
    eps=normrnd(0,1,[L,T]);
    S0=2809;
    K=2750;
    S = zeros(L,T);
    for i=1:252
        for c=1:L
        S(c,i)=S0*exp((.0295-.5*(.2^2))*.004+.0295*sqrt(.004)*eps(c,i));
       
        end
    end