/
premium_project_oop.py
214 lines (169 loc) · 7.3 KB
/
premium_project_oop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
__author__ = 'vincent'
import numpy, math, util
import pdb
from Option import BasketOption,BasketOptionWithControlVariate,AsianOption,AsianOptionWithControlVariate
from Option import EuropeanOption
STANDARD = 'Standard'
GEO_MEAN = 'Geometric mean Asian'
GEO_MEAN_STRIKE = 'Geometric mean Asian with adjusted strike'
import os
base_path = os.path.dirname(os.path.abspath(__file__))
def GPU_arithmetic_basket_option(S1, S2, V1, V2, R, T, K, geo_K, rou, option_type, path_num=10000,
control_variate='Standard', Quasi=True):
if control_variate == STANDARD:
S1 = numpy.float32(S1)
S2 = numpy.float32(S2)
V1 = numpy.float32(V1)
V2 = numpy.float32(V2)
R = numpy.float32(R)
K = numpy.float32(K)
T = numpy.float32(T)
rou = numpy.float32(rou)
option_type = numpy.float32(option_type)
kernelargs = (S1, S2, V1, V2, R, K, T, rou, option_type)
code = base_path + "/cl/standard_arithmetic_basket_option.cl"
return BasketOption(path_num, Quasi, kernelargs).cal(code)
elif control_variate == GEO_MEAN:
geo = util.geometric_basket_option(S1, S2, V1, V2, R, T, geo_K, rou, option_type)
S1 = numpy.float32(S1)
S2 = numpy.float32(S2)
V1 = numpy.float32(V1)
V2 = numpy.float32(V2)
R = numpy.float32(R)
K = numpy.float32(K)
T = numpy.float32(T)
rou = numpy.float32(rou)
option_type = numpy.float32(option_type)
geo_K = numpy.float32(geo_K)
kernelargs = (S1, S2, V1, V2, R, K, geo_K, T, rou, option_type)
code = base_path+"/cl/geo_mean_arithmetic_basket_option.cl"
return BasketOptionWithControlVariate(path_num, Quasi, geo_K, geo, kernelargs).cal(code)
elif control_variate == GEO_MEAN_STRIKE:
# K = K + mean(bgT)-mean(baT)
# mean(bgT) = e^(mu*T)*bg_0 = e^(mu*T)*S0
# mean(baT) = 1/n * sum(S(T)) = 1/n * sum(S0*e^(rt))
bg_0 = math.sqrt(S1 * S2)
sigma_bg = math.sqrt(V1 * V1 + V1 * V2 * rou + V2 * V1 * rou + V2 * V2) / 2
miu_bg = R - 0.5 * (V1 * V1 + V2 * V2) / 2 + 0.5 * sigma_bg * sigma_bg
E_bg = bg_0 * math.exp(miu_bg * T)
E_ba = (S1 * math.exp(R * T) + S2 * math.exp(R * T)) / 2
geo_K = K + E_bg - E_ba
return GPU_arithmetic_basket_option(S1, S2, V1, V2, R, T, K, geo_K, rou, option_type, path_num, GEO_MEAN)
def GPU_arithmetic_asian_option(K, geo_K, T, R, V, S0, N, option_type, path_num=10000, control_variate='Standard',
Quasi=True):
if control_variate == STANDARD:
dt = T / N
sigma = V
drift = math.exp((R - 0.5 * sigma * sigma) * dt)
sigma_sqrt = sigma * math.sqrt(dt)
exp_RT = math.exp(-R * T)
N = numpy.float32(N)
K = numpy.float32(K)
S0 = numpy.float32(S0)
sigma_sqrt = numpy.float32(sigma_sqrt)
drift = numpy.float32(drift)
exp_RT = numpy.float32(exp_RT)
option_type = numpy.float32(option_type)
kernelargs = (N, K, S0, sigma_sqrt, drift, exp_RT, option_type)
code = base_path+"/cl/standard_arithmetic_asian_option.cl"
return AsianOption(path_num, Quasi, N, kernelargs).cal(code)
elif control_variate == GEO_MEAN:
geo = util.geometric_asian_option(geo_K, T, R, V, S0, N, option_type)
dt = T / N
sigma = V
drift = math.exp((R - 0.5 * sigma * sigma) * dt)
sigma_sqrt = sigma * math.sqrt(dt)
exp_RT = math.exp(-R * T)
N = numpy.float32(N)
K = numpy.float32(K)
geo_K = numpy.float32(geo_K)
S0 = numpy.float32(S0)
sigma_sqrt = numpy.float32(sigma_sqrt)
drift = numpy.float32(drift)
exp_RT = numpy.float32(exp_RT)
option_type = numpy.float32(option_type)
kernelargs = (N, K, geo_K, S0, sigma_sqrt, drift, exp_RT, option_type)
code = base_path+"/cl/geo_mean_arithmetic_asian_option.cl"
return AsianOptionWithControlVariate(path_num, Quasi, N, geo_K, geo, kernelargs).cal(code)
elif control_variate == GEO_MEAN_STRIKE:
# K = K + mean(agT)-mean(aaT)
# mean(agT) = e^(mu*T)*ag_0 = e^(mu*T)*S0
# mean(aaT) = 1/n * sum(S(T)) = 1/n * sum(S0*e^(rt))
sigma = util._get_geometric_sigma(V, N)
miu = util._get_geometric_miu(R, V, N, sigma)
E_ag = S0 * math.exp(miu * T)
dt = T / N
E_aa = sum([math.exp(R * (i + 1) * dt) for i in xrange(int(N))]) * S0 / N
geo_K = K + E_ag - E_aa
return GPU_arithmetic_asian_option(K, geo_K, T, R, V, S0, N, option_type, path_num, GEO_MEAN)
# @util.print_use_time
def standardMC_european_option(K, T, R, V, S0, N, option_type, path_num=10000):
dt = T / N
sigma = V
drift = math.exp((R - 0.5 * sigma * sigma) * dt)
sigma_sqrt = sigma * math.sqrt(dt)
exp_RT = math.exp(-R * T)
european_payoff = []
for i in xrange(path_num):
former = S0
for j in xrange(int(N)):
former = former * drift * math.exp(sigma_sqrt * numpy.random.normal(0, 1))
european_option = former
if option_type == 1.0:
european_payoff_call = exp_RT * max(european_option - K, 0)
european_payoff.append(european_payoff_call)
elif option_type == 2.0:
european_payoff_put = exp_RT * max(K - european_option, 0)
european_payoff.append(european_payoff_put)
# Standard Monte Carlo
p_mean = numpy.mean(european_payoff)
p_std = numpy.std(european_payoff)
p_confmc = (p_mean - 1.96 * p_std / math.sqrt(path_num), p_mean + 1.96 * p_std / math.sqrt(path_num))
return p_mean, p_std, p_confmc
def GPU_european_option(K, T, R, V, S0, N, option_type, path_num=10000, Quasi=True):
# pdb.set_trace()
dt = T / N
sigma = V
drift = math.exp((R - 0.5 * sigma * sigma) * dt)
sigma_sqrt = sigma * math.sqrt(dt)
exp_RT = math.exp(-R * T)
N = numpy.float32(N)
K = numpy.float32(K)
S0 = numpy.float32(S0)
sigma_sqrt = numpy.float32(sigma_sqrt)
drift = numpy.float32(drift)
exp_RT = numpy.float32(exp_RT)
option_type = numpy.float32(option_type)
kernelargs = (N, K, S0, sigma_sqrt, drift, exp_RT, option_type)
code = base_path+"/cl/european_option.cl"
return EuropeanOption(path_num, Quasi, N, kernelargs).cal(code)
if __name__ == '__main__':
pass
print"S=100,K=100,t=0,T=0.5,v=20%,and r=1%."
import time
s = time.time()
S = S0 = S1 = S2 = 100.0
T = 3.0
R = 0.05
V = V1 = V2 = 0.3
geo_K = K = 100.0
n = 50.0
rou = 0.5
m = 10000
# print GPU_arithmetic_asian_option(K, K, T, R, V, S0, n, 1.0, path_num=100, control_variate=STANDARD, Quasi=False)
#
import project
#
# # print project.arithmetic_asian_option(K, K, T, R, V, S0, n, 'call', path_num=100, control_variate=GEO_MEAN)
#
# e = time.time()
# print "use", e - s
#
# print GPU_arithmetic_basket_option(S1, S2, V1, V2, R, T, K, geo_K, rou, 1.0, path_num=10000,
# control_variate=GEO_MEAN_STRIKE, Quasi=False)
print standardMC_european_option(K, T, R, V, S0, n, 1.0, path_num=10000)
print project.bs(S0, K, T, V, R, 'call')
# s = time.time()
# print GPU_european_option(K, T, R, V, S0, n, 1.0, path_num=100000, Quasi=False)
# e = time.time()
# print "use", e - s