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.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

Do you understand how a SET instruction works? That's why I pointed you to the chapter in the Programming Manual. By default, it has an implied loop over T from 1 to the length of the workspace. It looks like you think the SET is working over your J loop range. It isn't. The J loop is doing nothing of use---everything that happens when J=1 gets overwritten by J=2, etc.

You're also having a problem with the range here:

cal 1971 2 12
all 2008:12
OPEN DATA "C: \ Desktop\ heterosc_option_Last_2.xls"
DATA(FORMAT=xls,ORG=COL) / f1 f2 x r
OPEN DATA "C: \ Desktop\ Data_T.xls"
DATA(FORMAT=xls,ORG=COL) / S T1 r2 K T2

That second DATA instruction is going to execute over the range to 2008:12 even though there is nothing about that data set that has to do with the CALENDAR. Put the range you want on that DATA instruction, whether it's

DATA(FORMAT=xls,ORG=COL) 1 61 S T1 r2 K T2

or

DATA(FORMAT=xls,ORG=COL) 1 10964 S T1 r2 K T2
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Many Thanks Tom for your inputs. How do I test for this condition then

Code: Select all

%if(T2(j)>T1(m), estr, %na)
? What I want is this:
For each 61 inputs S, T1, r2, test that condition for each T2 and K, a total of 10964 obs, i.e 61*10964 times.
Does T2>T2 mean the same thing without the M and J index?
Thanks
Rachid
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

T2 and T1 are both data. What's being simulated?

Please, comment your program!
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Many thanks for your help. Please see the attached code with comments. I am simulating

Code: Select all

set estr = (sqrt(d1)*z+muxstr1)*(1-j3) + (sqrt(d2)*z+muxstr2)*j3 
Best
Rachid
Attachments
Code_T.PRG
(6.23 KiB) Downloaded 773 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

All those SET instructions are running over 1971:2 to 2008:12. Is that what you want?

*
*For each (61 obs) S, T1, r2, test the condition T2 > T1.
*
set dummy = %if(T2 > T1, Rtstr, %na)

What do T2 and T1 have to do with the simulations?

From the "RATS Programming Manual"
And if you have a part of your program which you’re not sure is correct, commenting it can often help you spot errors. (If you can’t explain why you’re doing something, that might be a good sign that you’re doing it wrong).
You're not really commenting the parts that really matter.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks Tom. I am starting to follow your advice of using comments as the code is written.

All those SET instructions are running over 1971:2 to 2008:12. Is that what you want? Yes, up to the condition T2>T1 where out of sampling forecasts start.

*
*For each (61 obs) S, T1, r2, test the condition T2 > T1.
*
set dummy = %if(T2 > T1, Rtstr, %na)

What do T2 and T1 have to do with the simulations? There were J and M loops there, however since you said that the J loop does nothing of value, removed both of them knowing full well that I need to somehow alter the code using some kind of loop to satisfy that condition.

Many thanks.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

rbelhach95 wrote:Thanks Tom. I am starting to follow your advice of using comments as the code is written.

All those SET instructions are running over 1971:2 to 2008:12. Is that what you want? Yes, up to the condition T2>T1 where out of sampling forecasts start.
These are the first values of T1 and T2. What do these have to do with the simulations?

39843.00 39864.00
39871.00 39864.00
39903.00 39864.00
39933.00 39864.00
39962.00 39864.00
39994.00 39864.00
40025.00 39864.00
40056.00 39864.00

You need to write down in detail what you're trying to do with the simulated price sequences.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks Tom.
These are the first values of T1 and T2. What do these have to do with the simulations?

39843.00 39864.00
39871.00 39864.00
39903.00 39864.00
39933.00 39864.00
39962.00 39864.00
39994.00 39864.00
40025.00 39864.00
40056.00 39864.00
I don't know whether I understand your question, but those were dates that I converted to numbers in excel, T2 = expiration time, T1=initial time.
My second data set from 2009-2013 is used for out sample forecast as I explained in an earlier email.
Rachid
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

T1 and T2 are those numerical values. RATS does not interpret them as dates. Even if they were interpreted as dates, I still don't understand how they are supposed to interact with your simulation. You need to make the effort to explain in detail what you are trying to do.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Thanks Tom. The only part of the code that is simulated is:

Code: Select all

set v  =  %ran(1.0)
set simul1 = %uniform(0,1)
set u1 = %invnormal(%cdf(as)+(1-%cdf(as))*simul1)
set u2 = %invnormal(%cdf(bs)*simul1)
set simul2 = %uniform(0,1)
set j1 = %sign(simul2-pistr)
set j2 = j1+1
set j3 = %sign(j2)
set z  = cs*(lambda1*u1+v)*(1-j3) + ds*(lambda2*u2+v)*j3
set estr = (sqrt(d1)*z+muxstr1)*(1-j3) + (sqrt(d2)*z+muxstr2)*j3
Then the conditional volatilities and covolatilities are used under the each simulated "estr":

Code: Select all

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
set k11 = lambda10^2+(lambda11^2)*(estr(t-1)-gama21)^2+(lambda12^2)* temp44{1}+(alpha31^2)*f1{1}^2+(alpha32)^2*f2{1}^2
set k22 = lambda20^2+(lambda21^2)*(estr(t-1)-gama22)^2+(lambda22^2)*temp55{1}+(alpha41^2)*f1{1}^2+(alpha42^2)*f2{1}^2
set sigmaTx11 = (beta10*lambda10)+(beta11*lambda11)*(estr(t-1)-gama11)*(estr(t-1)-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-1)-gama12)*(estr(t-1)-gama22)+(beta22*lambda22)*sqrt(temp33{1})*sqrt(temp55{1})+(alpha21*alpha41)*f1{1}^2+(alpha22*alpha42)*f2{1}^2
To produce the new return dynamics under the risk neutral measure

Code: Select all

set Rtstr = r2-psinustr0+psinustr1+estr
Then if given T2, we have T2 > T1 for each T1 (T2 and T1 are given, these are not simulated value) , then

Compute the stock price and call using "Rtstr" if T2>T1, otherwise call(i)=%na

Code: Select all

set ST(i)     = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S*exp(Rtstr)
set Call(i)   = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), exp(-r2*(T2-T1)/365)*%max(0,ST(i)-K)
The only problem that I am having is how to set that condition.
Hope this helps. Many thanks for your patience
Rachid
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

First off, in your simulations, you have the same problem that you had earlier that the tempxx series aren't defined until the end of the loop, so the ones used in (say) set d11 are from the previous complete simulation.

I still am puzzled by the interaction between the range of your SET instructions and the T1 and T2. All the SET instructions are running from 1971:2 to 2008:12. Your SET ST(i) and SET CALL(i) are likewise running from 1971:2 to 2008:12. If you're trying to do something out-of-sample, that sounds wrong.

It sounds like what you're trying to do is ultimately governed by the cross sectional data set. T1 and T2 are supposed to represent dates. What do those dates have to do with the 1971:2 to 2008:12 range? And what are you trying to do with each line in that cross sectional data set? In the end, what do you want to end up with?
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Simulation of Stock and Option prices

Unread post by rbelhach95 »

Many Thanks for you inputs.

First off, in your simulations, you have the same problem that you had earlier that the tempxx series aren't defined until the end of the loop, so the ones used in (say) set d11 are from the previous complete simulation.

I get the point, instead, tempxx should be here

Code: Select all

set Rtstr = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), r2-psinustr0+psinustr1+estr
and removing them from ST(i) and Call(i)

I still am puzzled by the interaction between the range of your SET instructions and the T1 and T2. All the SET instructions are running from 1971:2 to 2008:12. Your SET ST(i) and SET CALL(i) are likewise running from 1971:2 to 2008:12. If you're trying to do something out-of-sample, that sounds wrong.

I realized this after running the code, you are right, the period 1971:2 to 2008:12 should only be used for estimation, my out of sample period is from 2009-20013 for S, T1 and r2. T2 are expiration times (which I have changed to numbers) from dead options and some few still alive options from 2014-2015.

It sounds like what you're trying to do is ultimately governed by the cross sectional data set. T1 and T2 are supposed to represent dates. What do those dates have to do with the 1971:2 to 2008:12 range? And what are you trying to do with each line in that cross sectional data set? In the end, what do you want to end up with?

T2 and T1 have nothing to do with those dates, what I would like to end up with is a list of 10964 values of calls some with numerical values some with NA if the condition fails.
Thanks again
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

tomdoan wrote: First off, in your simulations, you have the same problem that you had earlier that the tempxx series aren't defined until the end of the loop, so the ones used in (say) set d11 are from the previous complete simulation.
rbelhach95 wrote: I get the point, instead, tempxx should be here

Code: Select all

set Rtstr = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), r2-psinustr0+psinustr1+estr
and removing them from ST(i) and Call(i)
That still isn't going to work. From what I can see, there's absolutely no reason for them. D11 relies on lagged D11, D22 on lagged D22, etc. with no apparent interactions in their definitions so just use d11 for temp22 etc. If there were more complicated dynamics (i.e. if D11 depends upon both lagged D11 and lagged D22), then you would have to use FORECAST rather than a series of SET instructions to extrapolate the model. The SET instructions are self-contained so each runs from 1971:2 to 2008:12 (or whatever range you put on it) without stopping to do the calculations for the others. FORECAST, on the other hand, runs through the periods one at a time and solves all the equations, then moves on to the next time period.
tomdoan wrote: I still am puzzled by the interaction between the range of your SET instructions and the T1 and T2. All the SET instructions are running from 1971:2 to 2008:12. Your SET ST(i) and SET CALL(i) are likewise running from 1971:2 to 2008:12. If you're trying to do something out-of-sample, that sounds wrong.
rbelhach95 wrote: I realized this after running the code, you are right, the period 1971:2 to 2008:12 should only be used for estimation, my out of sample period is from 2009-20013 for S, T1 and r2. T2 are expiration times (which I have changed to numbers) from dead options and some few still alive options from 2014-2015.
You would benefit a great deal from writing this as "pseudo-code". (You can look that up on Wikipedia). Your main problem throughout this has been on the meaning of the loop indexes. So concentrate on writing down what each loop is doing---not the formulas that you're using, but just a verbal description. Something like

for each draw from 1 to N
simulate returns from here to there
for each portfolio from 1 to howevermany
check condition ????
if ????
compute ????
else
compute ???
save something somewhere
end portfolio
end draw
tomdoan wrote: It sounds like what you're trying to do is ultimately governed by the cross sectional data set. T1 and T2 are supposed to represent dates. What do those dates have to do with the 1971:2 to 2008:12 range? And what are you trying to do with each line in that cross sectional data set? In the end, what do you want to end up with?
rbelhach95 wrote: T2 and T1 have nothing to do with those dates, what I would like to end up with is a list of 10964 values of calls some with numerical values some with NA if the condition fails.
If they have nothing to do with the dates, then what difference does it make what dates are associated with the simulations?
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 being extremely helpful and patient with me all this time. Please look at this piece of the code that I have also attached. I replaced the tempxx with d11, etc..but I get all zeros for d11,.... Also should I use the out of sampling data set (that I also have attached) in this format using dates or as a cross sectional data?, I have tried to use the former but it gives me an error. I have made some changes to this piece of the code, it gives me an output of only 61 calls, and I hope that I followed your advice here. Thanks so much

Code: Select all

set Rtstr 1 M = (temp22 = d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), r2-psinustr0+psinustr1+estr
set return 1 M = 0.
do t = 1, M
do j = 1,J
if T1(t) <T2(t+j-1) {
set ST(i)     =  (return = Rtstr), S*exp(Rtstr)
set Call(i)   = exp(-r2*(T2-T1)/365)*%max(0,ST(i)-K)
}
else {
set ST(i)  = %na
set Call(i) = %na
}
end do j
end do t
end do i
copy(Format=xls,ORG=col) / ST Call
tab
[code]
Attachments
Code_T.PRG
(6.01 KiB) Downloaded 740 times
Data_T1.xls
(507.5 KiB) Downloaded 642 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Simulation of Stock and Option prices

Unread post by TomDoan »

Don't use T as a loop index---T is the internal loop index for the SET instructions.

Perhaps the problems you're having is rooted in a misunderstanding of what SET does.

set x start end = expression(t)

is a self-contained operation which runs over an internal loop for T over the range start to end. It evaluates expression(t) (where any explicit mention of t is often suppressed since a series name by itself means series(t) and seriesname{lag} means seriesname(t-lag)) and saves it into x(t). A common error is thinking that you can do

if condition(t)
set x = expression(t)

where the intent is to have set do expression(t) when for the entries where condition(t) is met. That doesn't work because SET is self-contained. Instead, if you want to do something like that, you need to use the %IF function inside the expression(t).

I can't answer your question because I have no idea what you're trying to do. I can't understand your code, I can't understand your explanations. Please take the time to write out in detail what you're trying to do---what the simulations are supposed to be, what range they're supposed to be over, what the structure is of your file of options, what you want to do with each row in that, what you want to produce from each option for each simulation.

It's also long past time to take this off the forum. Please e-mail anything else to support@estima.com.
Post Reply