Simulation of Stock and Option prices
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Simulation of Stock and Option prices
Dear Tom,
I am trying to do out of sampling forecasts for call options, please see the attached code and the corresponding data set. The in sample estimation period works well. For the out-of sample period, I am using three loops:
First loop is for Monte Carlo simulation,
Second loop is for the three inputs S, r1, and T1 (61 obs)
Third loop is such that for each of the inputs in the second loop (i.e S, r1 and T1), loop through T2(expiry date) and K (strike price) (10964 obs) if condition on T2 holds (Since I have many NA in my sample)
The problem is that it took more than three days for N=5 replications only, and the program is still running, my question is there an easy way to accomplish these three steps in an efficient manner? I would like to get an output of 10964 call options with many NA of course
Thank you so much
I am trying to do out of sampling forecasts for call options, please see the attached code and the corresponding data set. The in sample estimation period works well. For the out-of sample period, I am using three loops:
First loop is for Monte Carlo simulation,
Second loop is for the three inputs S, r1, and T1 (61 obs)
Third loop is such that for each of the inputs in the second loop (i.e S, r1 and T1), loop through T2(expiry date) and K (strike price) (10964 obs) if condition on T2 holds (Since I have many NA in my sample)
The problem is that it took more than three days for N=5 replications only, and the program is still running, my question is there an easy way to accomplish these three steps in an efficient manner? I would like to get an output of 10964 call options with many NA of course
Thank you so much
- Attachments
-
- Data_T.xls
- (4.19 MiB) Downloaded 662 times
-
- Code_T.PRG
- (6.29 KiB) Downloaded 971 times
Re: Simulation of Stock and Option prices
First, did you try this with a much smaller number first to get an idea of the timing?
You need to look carefully at how you are using the loop indexes. I'm seeing that you're using
set ST(j) ...
when ST is dimensioned N which is the limit on the I loop, not the J loop.
You need to look carefully at how you are using the loop indexes. I'm seeing that you're using
set ST(j) ...
when ST is dimensioned N which is the limit on the I loop, not the J loop.
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Thanks for your feedback, I was editing the old code that I sent you some time ago, and I forgot to put an i for ST(i) and Call(i). No I haven't tried it with less observations, but will my code produce the things I set out to do in my last email, or is there a shorter way in terms of these loops to reduce the timing? Please advise.
Thanks
Rachid
Thanks
Rachid
Re: Simulation of Stock and Option prices
You do have some duplicated calculations, but nothing that's grossly inefficient. You are solving about 10^9 prices so it may take a while, but you should fix the errors and try it with a cut down set of simulations to see how long it will take.
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Thanks Tom for all the help. Could you point out to me those duplication and when you say errors, you mean the j changed to an i in ST and Call.
Thanks
Rachid
Thanks
Rachid
Re: Simulation of Stock and Option prices
For instance:
set m_33 = %cdf((muT-mux-nu*(sigmaTx11-d11))/sqrt(k11+d11-2*sigmaTx11))
set m_44 = %cdf(-(muT-mux-nu*(sigmaTx22-d22))/sqrt(k22+d22-2*sigmaTx22))
set m_13 = %cdf((muT-mux+(1-nu)*(sigmaTx11-d11))/sqrt(k11+d11-2*sigmaTx11))
set m_14 = %cdf(-(muT-mux+(1-nu)*(sigmaTx22-d22))/sqrt(k22+d22-2*sigmaTx22))
There are only slight differences in the two pairs of calculations, so, for instance, the two sqrt(...)'s can be computed once each.
As I said, check to make sure that your loop indexes are doing what you want.
set m_33 = %cdf((muT-mux-nu*(sigmaTx11-d11))/sqrt(k11+d11-2*sigmaTx11))
set m_44 = %cdf(-(muT-mux-nu*(sigmaTx22-d22))/sqrt(k22+d22-2*sigmaTx22))
set m_13 = %cdf((muT-mux+(1-nu)*(sigmaTx11-d11))/sqrt(k11+d11-2*sigmaTx11))
set m_14 = %cdf(-(muT-mux+(1-nu)*(sigmaTx22-d22))/sqrt(k22+d22-2*sigmaTx22))
There are only slight differences in the two pairs of calculations, so, for instance, the two sqrt(...)'s can be computed once each.
As I said, check to make sure that your loop indexes are doing what you want.
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Thanks Tom, very much appreciated
Rachid
Rachid
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Dear Tom,
Sorry for bothering you again. I have tired tried the code with few obs, N=5, M=20, J=100, but I get 61 obs for S, T2, r2 as opposed to M=20 obs, the same thing goes for T2 and K, I get either 61 obs or fewer, but I need to get J = 100. I used the table instruction, and I see that my "set dummy = %if(T2(j)>T1,estr,%na)" always turns out to be equal to 0, # of obs. in S and K are only 61, but it should be 10946. As I explained in my first email yesterday, I would like to get a total, as in this simplified example, of 100 calls replicated 5 times. The logic of the program looks fine, but maybe the data is not read properly. Could you please shed some light on this.
Thanks
Rachid
Sorry for bothering you again. I have tired tried the code with few obs, N=5, M=20, J=100, but I get 61 obs for S, T2, r2 as opposed to M=20 obs, the same thing goes for T2 and K, I get either 61 obs or fewer, but I need to get J = 100. I used the table instruction, and I see that my "set dummy = %if(T2(j)>T1,estr,%na)" always turns out to be equal to 0, # of obs. in S and K are only 61, but it should be 10946. As I explained in my first email yesterday, I would like to get a total, as in this simplified example, of 100 calls replicated 5 times. The logic of the program looks fine, but maybe the data is not read properly. Could you please shed some light on this.
Thanks
Rachid
Re: Simulation of Stock and Option prices
I'm not sure what's going on here, but I can't imagine that it's right:
What does changing the CALENDAR have to do with the second data set? Isn't that a list of options, thus basically a cross sectional data set?
You also have DO M=1,61, but I don't see anywhere where M is used. So what's the point of that?
You would probably be helped quite a bit by reading Chapter Six of the RATS Programming Manual as you seem to be making at least two mistakes that are covered in that (changing the CALENDAR, and not understanding default workspace length).
Code: Select all
cal 1971 2 12
all 2008:12
OPEN DATA "C: \ Desktop\ heterosc_option_Last_3.xls"
DATA(FORMAT=xls,ORG=COL) / f1 f2 x r1
cal 2009 1 12
OPEN DATA "C: \ Desktop\ inputcallsandputs_Newtest.xls"
DATA(FORMAT=xls,ORG=COL) / S K T1 T2 r2
You also have DO M=1,61, but I don't see anywhere where M is used. So what's the point of that?
You would probably be helped quite a bit by reading Chapter Six of the RATS Programming Manual as you seem to be making at least two mistakes that are covered in that (changing the CALENDAR, and not understanding default workspace length).
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Thanks Tom for your useful advice, I refer in my code to M in this line, set ST(j) = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S(m)*exp(Rtstr), I need to use this loop for S, T1 and r2, and for each of these 61 obs. I need to loop 10649 times over T2 and K.I will figure out how to fix my data set, Are you also saying I can do without this M loop since the info is contained in chapter 6?
Rachid
Rachid
Re: Simulation of Stock and Option prices
set ST(j) = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S(m)*exp(Rtstr)
has M on the right side and J on the left. The next time through the M loop, you'll overwrite each one of the ST(J)---there's nothing about a trip through the M loop that sticks.
While you're checking out the RATS Programming Manual, you might want to read section 1.4 General Stylistic Tips. You would benefit a lot from indenting the loops and commenting what you're doing.
has M on the right side and J on the left. The next time through the M loop, you'll overwrite each one of the ST(J)---there's nothing about a trip through the M loop that sticks.
While you're checking out the RATS Programming Manual, you might want to read section 1.4 General Stylistic Tips. You would benefit a lot from indenting the loops and commenting what you're doing.
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Thanks a lot Tom for all your continuous support and advice, the J in fact is an I which I alluded to that change in my email yesterday. I guess I need to do some reading first.
All the best
Rachid
All the best
Rachid
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Dear Tom,
I apologize for bringing up the same old question again, but I really need your help on this one, I went through your suggestions, adjusted the length of my workspace, the data is read by rats fine, but I still can't figure out why the output skips many data points, in the M and J loops. As I have explained in my previous emails, my code (attached is an edited new code with a data file) consists of three steps:
1. Estimation, this will generate 448 observation (in sample)
2. Simulate estr distribution using and using the parameter estimates from 1.
3. For each input S, T1, r2, from my second data sets with a total of 61 observations, (in my code this is referred to as an M loop), test the condition: , this is done for each T2 and K, a total of 10964 obs, I refer to this as a J loop.
This is the output I get using table:
Series Obs Mean Std Error Minimum Maximum
F1 455 0.040330 1.004415 -4.970000 2.670000
F2 455 0.948697 0.218155 0.386000 1.447000
X 455 0.004929 0.044528 -0.245428 0.151043
R 455 0.066818 0.035520 0.010200 0.207700
S 61 1287.994098 259.649113 735.090000 1848.360000
T1 61 40755.114754 540.529435 39843.000000 41670.000000
R2 61 0.003534 0.001751 0.001900 0.009800
K 10964 1151.908519 381.379458 100.000000 3000.000000
T2 10964 40899.318679 584.740460 39864.000000 42174.000000
ST(1) 7 2119.485219 216.419092 1955.193925 2527.573634
ST(2) 8 1953.633547 370.851496 1641.172842 2812.513109
ST(3) 7 1843.679394 233.944140 1514.869634 2145.740724
CALL(1) 7 1109.870324 218.463262 944.027229 1521.813308
CALL(2) 8 942.452112 374.354346 627.040083 1809.444157
CALL(3) 7 831.459396 236.153842 499.543888 1136.373824
TEMP1 455 -0.014814 0.052503 -0.272030 0.158624
TEMP2 456 0.003051 0.021287 0.000000 0.455182
TEMP3 456 0.022509 0.373831 0.000000 7.987350
TEMP4 456 0.012670 0.270553 0.000000 5.777428
TEMP5 456 16350584615.243677 3345106684.350837 0.000000 19798670416.531475
MUXSTR1 455 0.003791 0.003109 0.000040 0.015328
MUXSTR2 455 0.009247 0.007717 0.001732 0.073880
MUTSTR1 455 -0.632581 0.120852 -0.989552 -0.093188
MUTSTR2 455 -11342.587411 3969.327804 -29983.952038 -1736.042863
LAMBDA01 455 -1994844.834977 983978.036582 -9312806.803272 -309695.863366
LAMBDA02 455 -0.149341 0.100371 -1.569558 -0.030493
LAMBDA1 455 139551.746045 119951.714030 12834.398133 1190856.235173
LAMBDA2 455 -1.256066 0.599634 -6.831248 -0.389508
AS 455 19.488442 17.293803 1.285206 153.558822
BS 455 0.089102 0.029049 0.028414 0.227339
CS 455 0.000011 0.000009 0.000001 0.000078
DS 455 0.648310 0.132476 0.144842 0.931810
G3S 455 0.000227 0.004712 0.000000 0.100493
G4S 455 0.540123 0.014998 0.512903 0.605697
PISTR 455 0.000341 0.007024 0.000000 0.149796
V 448 0.014904 1.027887 -2.778213 3.312204
SIMUL1 448 0.506574 0.291862 0.000951 0.998224
U1 36 6.356172 1.636907 1.635979 8.160727
U2 447 -0.724677 0.606684 -3.288935 0.143271
SIMUL2 448 0.489109 0.281009 0.003058 0.998813
J1 447 1.000000 0.000000 1.000000 1.000000
J2 447 2.000000 0.000000 2.000000 2.000000
J3 447 1.000000 0.000000 1.000000 1.000000
Z 36 0.504480 0.839347 -1.395217 2.515806
ESTR 36 0.072322 0.089597 -0.138410 0.228648
TEMP22 36 0.001109 0.001196 0.000026 0.004357
TEMP33 36 0.004565 0.004045 0.000498 0.019382
TEMP44 36 0.000000 0.000000 0.000000 0.000000
TEMP55 36 823693640.059687 49003222.793250 771030277.326579 947888841.201279
D11 36 0.001109 0.001196 0.000026 0.004357
D22 36 0.004565 0.004045 0.000498 0.019382
K11 36 0.000000 0.000000 0.000000 0.000000
K22 36 823693640.059687 49003222.793250 771030277.326579 947888841.201279
SIGMATX11 36 -0.000000 0.000000 -0.000000 0.000000
SIGMATX22 36 273.490418 1470.423402 -2944.023096 2801.982372
M_11 36 1.001870 0.002018 1.000044 1.007357
M_22 36 1.007731 0.006899 1.000838 1.033141
M_33 36 0.000503 0.003015 0.000000 0.018090
M_44 36 0.492940 0.037530 0.429543 0.577099
M_31 36 0.000617 0.003704 0.000000 0.022224
M_41 36 0.500007 0.000002 0.500001 0.500011
C2 36 1.997609 0.014182 1.914883 1.999987
M_1 36 0.992948 0.073966 0.878079 1.176467
M_12 36 1.004477 0.004838 1.000104 1.017655
M_21 36 1.018635 0.016792 1.002003 1.080956
M_13 36 0.000448 0.002688 0.000000 0.016127
M_14 36 0.489139 0.057748 0.391913 0.618103
M_2 36 0.995422 0.115578 0.825852 1.310562
PSINUSTR0 36 -0.010982 0.114070 -0.191340 0.270456
PSINUSTR1 36 -0.009745 0.073909 -0.130018 0.162516
DUMMY 0
RTSTR 7 0.026731 0.127823 -0.162738 0.185417
I apologize for bringing up the same old question again, but I really need your help on this one, I went through your suggestions, adjusted the length of my workspace, the data is read by rats fine, but I still can't figure out why the output skips many data points, in the M and J loops. As I have explained in my previous emails, my code (attached is an edited new code with a data file) consists of three steps:
1. Estimation, this will generate 448 observation (in sample)
2. Simulate estr distribution using
Code: Select all
"set estr = (sqrt(d1)*z+muxstr1)*(1-j3) + (sqrt(d2)*z+muxstr2)*j3" 3. For each input S, T1, r2, from my second data sets with a total of 61 observations, (in my code this is referred to as an M loop), test the condition:
Code: Select all
%if(T2(j)>T1(m), estr, %na)This is the output I get using table:
Series Obs Mean Std Error Minimum Maximum
F1 455 0.040330 1.004415 -4.970000 2.670000
F2 455 0.948697 0.218155 0.386000 1.447000
X 455 0.004929 0.044528 -0.245428 0.151043
R 455 0.066818 0.035520 0.010200 0.207700
S 61 1287.994098 259.649113 735.090000 1848.360000
T1 61 40755.114754 540.529435 39843.000000 41670.000000
R2 61 0.003534 0.001751 0.001900 0.009800
K 10964 1151.908519 381.379458 100.000000 3000.000000
T2 10964 40899.318679 584.740460 39864.000000 42174.000000
ST(1) 7 2119.485219 216.419092 1955.193925 2527.573634
ST(2) 8 1953.633547 370.851496 1641.172842 2812.513109
ST(3) 7 1843.679394 233.944140 1514.869634 2145.740724
CALL(1) 7 1109.870324 218.463262 944.027229 1521.813308
CALL(2) 8 942.452112 374.354346 627.040083 1809.444157
CALL(3) 7 831.459396 236.153842 499.543888 1136.373824
TEMP1 455 -0.014814 0.052503 -0.272030 0.158624
TEMP2 456 0.003051 0.021287 0.000000 0.455182
TEMP3 456 0.022509 0.373831 0.000000 7.987350
TEMP4 456 0.012670 0.270553 0.000000 5.777428
TEMP5 456 16350584615.243677 3345106684.350837 0.000000 19798670416.531475
MUXSTR1 455 0.003791 0.003109 0.000040 0.015328
MUXSTR2 455 0.009247 0.007717 0.001732 0.073880
MUTSTR1 455 -0.632581 0.120852 -0.989552 -0.093188
MUTSTR2 455 -11342.587411 3969.327804 -29983.952038 -1736.042863
LAMBDA01 455 -1994844.834977 983978.036582 -9312806.803272 -309695.863366
LAMBDA02 455 -0.149341 0.100371 -1.569558 -0.030493
LAMBDA1 455 139551.746045 119951.714030 12834.398133 1190856.235173
LAMBDA2 455 -1.256066 0.599634 -6.831248 -0.389508
AS 455 19.488442 17.293803 1.285206 153.558822
BS 455 0.089102 0.029049 0.028414 0.227339
CS 455 0.000011 0.000009 0.000001 0.000078
DS 455 0.648310 0.132476 0.144842 0.931810
G3S 455 0.000227 0.004712 0.000000 0.100493
G4S 455 0.540123 0.014998 0.512903 0.605697
PISTR 455 0.000341 0.007024 0.000000 0.149796
V 448 0.014904 1.027887 -2.778213 3.312204
SIMUL1 448 0.506574 0.291862 0.000951 0.998224
U1 36 6.356172 1.636907 1.635979 8.160727
U2 447 -0.724677 0.606684 -3.288935 0.143271
SIMUL2 448 0.489109 0.281009 0.003058 0.998813
J1 447 1.000000 0.000000 1.000000 1.000000
J2 447 2.000000 0.000000 2.000000 2.000000
J3 447 1.000000 0.000000 1.000000 1.000000
Z 36 0.504480 0.839347 -1.395217 2.515806
ESTR 36 0.072322 0.089597 -0.138410 0.228648
TEMP22 36 0.001109 0.001196 0.000026 0.004357
TEMP33 36 0.004565 0.004045 0.000498 0.019382
TEMP44 36 0.000000 0.000000 0.000000 0.000000
TEMP55 36 823693640.059687 49003222.793250 771030277.326579 947888841.201279
D11 36 0.001109 0.001196 0.000026 0.004357
D22 36 0.004565 0.004045 0.000498 0.019382
K11 36 0.000000 0.000000 0.000000 0.000000
K22 36 823693640.059687 49003222.793250 771030277.326579 947888841.201279
SIGMATX11 36 -0.000000 0.000000 -0.000000 0.000000
SIGMATX22 36 273.490418 1470.423402 -2944.023096 2801.982372
M_11 36 1.001870 0.002018 1.000044 1.007357
M_22 36 1.007731 0.006899 1.000838 1.033141
M_33 36 0.000503 0.003015 0.000000 0.018090
M_44 36 0.492940 0.037530 0.429543 0.577099
M_31 36 0.000617 0.003704 0.000000 0.022224
M_41 36 0.500007 0.000002 0.500001 0.500011
C2 36 1.997609 0.014182 1.914883 1.999987
M_1 36 0.992948 0.073966 0.878079 1.176467
M_12 36 1.004477 0.004838 1.000104 1.017655
M_21 36 1.018635 0.016792 1.002003 1.080956
M_13 36 0.000448 0.002688 0.000000 0.016127
M_14 36 0.489139 0.057748 0.391913 0.618103
M_2 36 0.995422 0.115578 0.825852 1.310562
PSINUSTR0 36 -0.010982 0.114070 -0.191340 0.270456
PSINUSTR1 36 -0.009745 0.073909 -0.130018 0.162516
DUMMY 0
RTSTR 7 0.026731 0.127823 -0.162738 0.185417
- Attachments
-
- Data_T.xls
- (507.5 KiB) Downloaded 682 times
-
- Code_T.PRG
- (6.32 KiB) Downloaded 916 times
Re: Simulation of Stock and Option prices
I don't have the original data set so I can't run that. Also, that second ALLOCATE instruction is changing the workspace length throughout, so all those SET instructions are trying to operate over 10000+ entries rather than the time series length. Skip the second ALLOCATE and just do the DATA for the options over the needed range.
You need to put comment lines in the program. This is long and there's a lot going on and I can't make any sense out of what's happening.
You need to put comment lines in the program. This is long and there's a lot going on and I can't make any sense out of what's happening.
-
rbelhach95
- Posts: 115
- Joined: Sat Feb 09, 2013 9:30 am
Re: Simulation of Stock and Option prices
Many thanks for your reply. Please see the edited code and original data attached. I got rid of allocate, and it works fine up to this point
in the attached code I put J=600, but it only loops through J=447 obs. What I don't get is in the output I get numbers for ST(1) and Call(1) for j = 2, but in the %if condition above it should be %na, and I also get many of those for other js. Please advise. Thanks
Rachid
Code: Select all
do m = 1, M
do j = 1,J
set Rtstr = r2(m)-psinustr0+psinustr1+estr
set dummy = %if(T2(j) > T1(m), Rtstr, %na)
set ST(i) = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), S(m)*exp(Rtstr)
set Call(i) = (temp22=d11), (temp33 = d22), (temp44 = k11), (temp55 = k22), exp(-r2(m)*(T2(j)-T1(m))/365)*%max(0,ST(i)-K(j))
end do j
end do mRachid
- Attachments
-
- Data_originaldata.xls
- (115.5 KiB) Downloaded 664 times
-
- Code_T.PRG
- (5.98 KiB) Downloaded 939 times