Simulation of Stock and Option prices

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Simulation of Stock and Option prices

Unread post by rbelhach95 »

Dear Tom,

Hope all is well. I have attached the code. The first part of this code is an estimation which works fine. The second part I simulate my distribution which will be used to simulate stock prices and call option prices using Monte Carlo. The code terminates at the end (com ST(i) = ST, Call(i) = Call) and gives an error unassigned. I tried to print "ST and Call" but I get nothing, I have stored both of them at the beginning of the code as "set ST 1 N = 0. set Call 1 N = 0." Please advise.
Thanks
Rachid
Attachments
Code_T.PRG
(6.9 KiB) Downloaded 1089 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

In the code below, aren't you just trying to simulate a single entry of entry ST and CALL on each pass through the DO loop? If that's the case, then all those EWISE instructions should just be COMPUTE and the SET instructions should be COMPUTE xxx(i)=.... rather than SET xxx =. And you don't want to do a PRINT instruction inside the DO loop, since you aren't done with the series until the loop is finished.

Code: Select all

do i =1,N
ewise v(i) = %ran(1.0)
ewise simul1(i)= %uniform(0,1)
ewise u1(i) = %invnormal(%cdf(as(i))+(1-%cdf(as(i)))*simul1(i))
ewise u2(i) = %invnormal(%cdf(bs(i))*simul1(i))
ewise simul2(i)= %uniform(0,1)
ewise j1(i) = %sign(simul2(i)-pistr1(i))
ewise j2(i) = j1(i)+1
ewise j3(i) = %sign(j2(i))
ewise s(i) = cs(i)*(lambda1(i)*u1(i)+v(i))*(1-j3(i)) + ds(i)*(lambda2(i)*u2(i)+v(i))*j3(i)
ewise estr(i) =  (sqrt(d1(i))*s(i)+muxstr1(i))*(1-j3(i)) + (sqrt(d2(i))*s(i)+muxstr2(i))*j3(i)
set d11 = beta10^2+(beta11^2)*(estr(t)-gama11)^2+(beta12^2)*temp22{1}+(alpha11^2)*f1{1}^2+(alpha12^2)*f2{1}^2
set d22 = beta20^2+(beta21^2)*(estr(t)-gama12)^2+(beta22^2)* temp33{1}+(alpha21^2)*f1{1}^2+(alpha22^2)*f2{1}^2
set k11 = lambda10^2+(lambda11^2)*(estr(t)-gama21)^2+(lambda12^2)* temp44{1}+(alpha31^2)*f1{1}^2+(alpha32)^2*f2{1}^2
set k22 = lambda20^2+(lambda21^2)*(estr(t)-gama22)^2+(lambda22^2)*temp55{1}+(alpha41^2)*f1{1}^2+(alpha42^2)*f2{1}^2
set sigmaTx11 = (beta10*lambda10)+(beta11*lambda11)*(estr(t)-gama11)*(estr(t)-gama21)+(beta12*lambda12)*sqrt(temp22{1})*sqrt(temp44{1})+(alpha11*alpha31)*f1{1}^2+(alpha12*alpha32)*f2{1}^2
set sigmaTx22 = (beta20*lambda20)+(beta21*lambda21)*(estr(t)-gama12)*(estr(t)-gama22)+(beta22*lambda22)*sqrt(temp33{1})*sqrt(temp55{1})+(alpha21*alpha41)*f1{1}^2+(alpha22*alpha42)*f2{1}^2
set mstr1 = exp(-nu*muxstr1+nu^2*d11/2)
set mstr2 = exp(-nu*muxstr2+nu^2*d22/2)
set mstr3 =  %cdf((muT-mux-(1-nu)*(sigmaTx11-d11))/sqrt(k11+d11-2*sigmaTx11))
set mstr4 =  %cdf(-(muT-mux-(1-nu)*(sigmaTx22-d22))/sqrt(k22+d22-2*sigmaTx22))
set mstr31 =  %cdf((muT-mux)/sqrt(k11+d11-2*sigmaTx11))
set mstr41 =  %cdf(-(muT-mux)/sqrt(k22+d22-2*sigmaTx22))
set psinustr1 = mstr1*mstr3*exp(-muxstr1+d11/2)
set psinustr2 = mstr2*mstr4*exp(-muxstr2+d22/2)
set psinustr = log(pistr1*psinustr1/mstr31+pistr2*psinustr2/mstr41)
set Rtstr = r-psinustr+estr(t)
set ST = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S0*exp(Rtstr)
set Call = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), exp(-r)*%max(0,ST-K)/N
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Dear Tom,

Thanks for your feedback. I have the edited attached code with the changes you have suggested. I still get an error here in this line
com s(i) = cs*(lambda1*u1(i)+v(i))*(1-j3(i)) + ds*(lambda2*u2(i)+v(i))*j3(i). I am trying to simulate 400 stock paths and call prices 100 times.
Please advise. thanks again for all your support
Rachid
Attachments
Code_T.PRG
(6.91 KiB) Downloaded 1057 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

Do you want 100 replications of series of ST and CALL, each of which are supposed to 412 entries long? If that's the case, then each COMPUTE in this needs to be a SET over the 1 M range, and you need to get rid of the (i)'s on the RHS expressions. There's no reason to save all those intermediate results (the V's, J1's etc.) since all that matters is the ST and CALL.

Code: Select all

com v(i) = %ran(1.0)
com simul1(i)= %uniform(0,1)
com u1(i) = %invnormal(%cdf(as)+(1-%cdf(as))*simul1(i))
com u2(i) = %invnormal(%cdf(bs)*simul1(i))
com simul2(i)= %uniform(0,1)
com j1(i) = %sign(simul2(i)-pistr1(i))
com j2(i) = j1(i)+1
com j3(i) = %sign(j2(i))
com s(i) = cs*(lambda1*u1(i)+v(i))*(1-j3(i)) + ds*(lambda2*u2(i)+v(i))*j3(i)
com estr(i) =  (sqrt(d1)*s(i)+muxstr1)*(1-j3(i)) + (sqrt(d2)*s(i)+muxstr2)*j3(i)
com d11(i) = beta10^2+(beta11^2)*(estr(t)-gama11)^2+(beta12^2)*temp22{1}+(alpha11^2)*f1{1}^2+(alpha12^2)*f2{1}^2
com d22(i) = beta20^2+(beta21^2)*(estr(t)-gama12)^2+(beta22^2)* temp33{1}+(alpha21^2)*f1{1}^2+(alpha22^2)*f2{1}^2
com k11(i) = lambda10^2+(lambda11^2)*(estr(t)-gama21)^2+(lambda12^2)* temp44{1}+(alpha31^2)*f1{1}^2+(alpha32)^2*f2{1}^2
com k22(i) = lambda20^2+(lambda21^2)*(estr(t)-gama22)^2+(lambda22^2)*temp55{1}+(alpha41^2)*f1{1}^2+(alpha42^2)*f2{1}^2
com sigmaTx11(i) = (beta10*lambda10)+(beta11*lambda11)*(estr(t)-gama11)*(estr(t)-gama21)+(beta12*lambda12)*sqrt(temp22{1})*sqrt(temp44{1})+(alpha11*alpha31)*f1{1}^2+(alpha12*alpha32)*f2{1}^2
com sigmaTx22(i) = (beta20*lambda20)+(beta21*lambda21)*(estr(t)-gama12)*(estr(t)-gama22)+(beta22*lambda22)*sqrt(temp33{1})*sqrt(temp55{1})+(alpha21*alpha41)*f1{1}^2+(alpha22*alpha42)*f2{1}^2
com mstr1(i) = exp(-nu*muxstr1+nu^2*d11/2)
com mstr2(i) = exp(-nu*muxstr2+nu^2*d22/2)
com mstr3(i) =  %cdf((muT-mux-(1-nu)*(sigmaTx11-d11))/sqrt(k11+d11-2*sigmaTx11))
com mstr4(i) =  %cdf(-(muT-mux-(1-nu)*(sigmaTx22-d22))/sqrt(k22+d22-2*sigmaTx22))
com mstr31(i) =  %cdf((muT-mux)/sqrt(k11+d11-2*sigmaTx11))
com mstr41(i) =  %cdf(-(muT-mux)/sqrt(k22+d22-2*sigmaTx22))
com psinustr1(i) = mstr1*mstr3*exp(-muxstr1+d11/2)
com psinustr2(i) = mstr2*mstr4*exp(-muxstr2+d22/2)
com psinustr(i) = log(pistr1*psinustr1/mstr31+pistr2*psinustr2/mstr41)
com Rtstr(i) = r-psinustr+estr(t)
If you want to save the full set of simulations for ST and CALL, you do

DEC VECT[SERIES] ST(n) CALL(n)

above the loop and

set ST(i) = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S0*exp(Rtstr)
set Call(i) = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), exp(-r)*%max(0,ST-K)/N

at the end of it. (That would be the only reference to (i) in the entire loop). Then you can do a

COPY(FORMAT=XLS,ORG=COLUMNS) / ST CALL

outside the loop to get a spreadsheet with the generated data. The spreadsheet will have 200 columns -- the first 100 will be the ST's and the second 100 will be the corresponding CALL's.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Dear Tom,

Thanks for all your help. in your last email, when you say that (That would be the only reference to (i) in the entire loop). do you mean that I should remove all the i's inside the loop. Please advise
Thanks
Rachid
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

Correct. However, you also have to change the declarations of all the size M vectors to SERIES, since that's what they'll be.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks Tom for all your insights. Very much appreciated. The estimation takes some time to run, so I will try your suggestions tomorrow and hope it works.
Rachid
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Dear Tom,

I have implemented all your suggestions in your last email, please see the attached edited code, but the code stops with an error
## Unassigned 122
>>>>set v <<<<
I have declared all the vectors as series, I even tried leaving the index i on the LHS of each equation at the start of the loop, but I also get an error, like MATrIX doesn't match REAL, something of this sort. Please advise
Thanks
Rachid
Attachments
Code_T.PRG
(6.74 KiB) Downloaded 1085 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

The only VECT[SERIES] are ST and CALL. All those others (V, SIMUL1, etc) are just SERIES.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks Tom for your help. this time it runs fine, but the output is giving me only ST(1) and Call(1), the rest is NA, Please see the attached output. Also the output is laid out as rows as opposed to columns which I have specified as columns in the code. Do I need to remove the i from ST(i) in the "set Call(i)"? Is there anyway to skip the estimation and go directly to simulating stock prices? Thanks
Rachid
Attachments
Output_T.PRG
(9.33 KiB) Downloaded 1064 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

That looks like the output from a PRINT instruction, which wasn't in the previous program. If you put the PRINT inside the loop, then yes, that's what you would get. You don't want a PRINT or a REPORT; you want just the COPY instruction outside the loop.

I assume the simulations are based upon the estimated coefficients. If you don't want to re-estimate each time you do the simulations, you can save the results and put them in instead of the guess values. Of course, you could also debug the simulations by using just the guess values and only hook up the estimation when you have the simulation code working.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks a lot Tom, very much appreciated. Actually "print / ST Call" was outside the loop, I changed it after you alluded to it in one of your emails and placed it outside the loop. I thought about using the guess values, but I would need e, d1, d2, etc... form the estimation to be used again.
Thanks
Rachid
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Dear Tom,

Hope all is well. Please forgive me for asking too many questions, but this will be my last question on this topic. The output I get looks good for ST(1), but the remaining 4 replications are fine except ST(2) skips one value, ST(3) skips two values, .......ST(5) skips 4 values. Please see the attached output and its code. I tried to start my values as
set ST(i) 12 *= (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S0*exp(Rtstr)
set Call(i) 12 * = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), exp(-r)*%max(0,ST(i)-K)

but it gives the same output. Please advise
Thanks
Rachid
Attachments
Code_T._Edited.PRG
(6.19 KiB) Downloaded 1041 times
N=5.XLS
(80.05 KiB) Downloaded 1067 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

TABLE and PRINT are your friends. If you use them, you'll see that you're leaking off data points with the TEMPxx and Dxx series. In:

set d11 = beta10^2+(beta11^2)*(estr(t-1)-gama11)^2+(beta12^2)*temp22{1}+(alpha11^2)*f1{1}^2+(alpha12^2)*f2{1}^2
set d22 = beta20^2+(beta21^2)*(estr(t-1)-gama12)^2+(beta22^2)* temp33{1}+(alpha21^2)*f1{1}^2+(alpha22^2)*f2{1}^2

you're using TEMP22{1} and TEMP33{1} but not defining then until later on.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks so much Tom. it is working properly now when I placed the tempxx inside the loop after estr.
Best
Rachid
Post Reply