-
Notifications
You must be signed in to change notification settings - Fork 3
/
EX1Bc.py
532 lines (452 loc) · 16.1 KB
/
EX1Bc.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from __future__ import division
import time
import numpy as np
import numpy.random as npr
import blspricer as bp
import customML as cm
import scipy.stats as scs
from scipy.stats.distributions import norm
class EX1B(object):
def __init__(self):
# --- Parameters ---
self.D = 1
self.S0 = 100.; self.mu = 0.08; self.sigma = 0.2
self.rfr = 0.03; self.T = 1./12.; self.tau = 1./52.
self.K = np.array([101., 110., 114.5])
self.H = np.array([91., 100., 104.5])
self.pos = np.array([1., 1., -1.])
self.c = 0.3608; self.perc = 0.95
self.I_pred = 10**4
# --- portfolio price @ t = 0 ---
self.n = 3
V0 = np.zeros(self.n)
for i in range(self.n):
V0[i] = bp.rear_end_dnOutPut(self.S0, self.K[i], self.rfr, self.T,\
self.sigma, self.H[i], self.tau, q=0.)
self.Value0 = np.sum(self.pos * V0)
def analy(self,N_o):
''' Analytical solution
'''
# --- Analytical solution from 10**7 samples ---
VaR_true = 0.361879
CVaR_true = 0.770773
EEL_true = 0.020454 # not conditional
# --- portfolio loss distribution @ t = \tau via analytical formulas---
ran = bp.gen_sn(0,N_o,anti_paths=True,mo_match=True)
S = np.zeros(N_o)
S[:] = self.S0
S[:] = S[:] * np.exp((self.mu - 0.5 * self.sigma**2.) * self.tau +\
self.sigma * np.sqrt(self.tau) * ran)
t0 = time.time()
Vtau = np.zeros((N_o, self.n))
ValueTau = np.zeros(N_o) # be careful of the vector size
for i in range(N_o):
for j in range(self.n):
Vtau[i][j] = bp.dnOutPut(S[i], self.K[j], self.rfr, self.T-self.tau,\
self.sigma, self.H[j])
ValueTau[i] = np.sum(self.pos * Vtau[i])
y = self.Value0 - ValueTau
t_analy = time.time()-t0
print "%fs spent in re-valuation" % t_analy
L_analy = np.sort(y)
var = scs.scoreatpercentile(L_analy, self.perc*100.)
idx = np.int(np.ceil(N_o*self.perc))
cvar = ((np.float(idx)/np.float(N_o) - self.perc)*L_analy[idx] +
np.sum(L_analy[idx+1:])/np.float(N_o))/(1.-self.perc)
EL = y - VaR_true
EEL = np.mean(EL.clip(0))
print "Analytical results:"
print "VaR: %e vs. %e (true)" % (var, VaR_true)
print "CVaR: %e vs. %e (true)" % (cvar, CVaR_true)
print "EEL: %e vs. %e (true)" % (EEL, EEL_true)
return {'eel':EEL,'t':t_analy}
def ns(self,kk,beta=1.):
''' Standard nested simulations
'''
# --- Computation budget allocatoin ---
N_o = np.int(np.ceil(kk**(2./3.)*beta))
#print "Outer loop: %d" % N_o
N_i = np.int(np.ceil(kk**(1./3.)/beta))
#print "Inner loop: %d" % N_i
#print "Total: %d" % (N_o*N_i)
# --- portfolio loss distribution @ t = \tau via analytical formulas---
t0 = time.time()
ran1 = npr.standard_normal((N_o,1))
S1 = np.zeros((N_o,1))
S1[:] = self.S0
S1[:] = S1[:] * np.exp((self.mu - 0.5*self.sigma*self.sigma)*self.tau + self.sigma *\
np.sqrt(self.tau) * ran1[:])
ran2 = npr.standard_normal((N_o,N_i))
S2 = np.zeros((N_o,N_i))
S2[:,:] = np.dot(S1[:],np.ones((1,N_i))) * np.exp((self.rfr - 0.5*self.sigma*self.sigma)*(self.T-self.tau) \
+ self.sigma * np.sqrt(self.T-self.tau) * ran2[:,:])
prob0 = (1.-np.exp(-2.*(np.log(np.dot(S1[:],np.ones((1,N_i)))/(self.H[0]*np.ones((N_o,N_i))))\
*np.log(S2[:,:]/(self.H[0]*np.ones((N_o,N_i))))/(self.sigma**2)/(self.T-self.tau))))\
*(np.dot(S1[:],np.ones((1,N_i))) >= self.H[0]*np.ones((N_o,N_i))).astype(float)\
*(S2[:,:] >= self.H[0]*np.ones((N_o,N_i))).astype(float)
prob1 = (1.-np.exp(-2.*(np.log(np.dot(S1[:],np.ones((1,N_i)))/(self.H[1]*np.ones((N_o,N_i))))\
*np.log(S2[:,:]/(self.H[1]*np.ones((N_o,N_i))))/(self.sigma**2)/(self.T-self.tau))))\
*(np.dot(S1[:],np.ones((1,N_i))) >= self.H[1]*np.ones((N_o,N_i))).astype(float)\
*(S2[:,:] >= self.H[1]*np.ones((N_o,N_i))).astype(float)
prob2 = (1.-np.exp(-2.*(np.log(np.dot(S1[:],np.ones((1,N_i)))/(self.H[2]*np.ones((N_o,N_i))))\
*np.log(S2[:,:]/(self.H[2]*np.ones((N_o,N_i))))/(self.sigma**2)/(self.T-self.tau))))\
*(np.dot(S1[:],np.ones((1,N_i))) >= self.H[2]*np.ones((N_o,N_i))).astype(float)\
*(S2[:,:] >= self.H[2]*np.ones((N_o,N_i))).astype(float)
Vtau0 = np.dot((np.maximum(self.K[0]-S2[:,:],0)*prob0), np.ones((N_i,1))) / \
float(N_i) * np.exp(-self.rfr*(self.T-self.tau))
Vtau1 = np.dot((np.maximum(self.K[1]-S2[:,:],0)*prob1), np.ones((N_i,1))) / \
float(N_i) * np.exp(-self.rfr*(self.T-self.tau))
Vtau2 = np.dot((np.maximum(self.K[2]-S2[:,:],0)*prob2), np.ones((N_i,1))) / \
float(N_i) * np.exp(-self.rfr*(self.T-self.tau))
ValueTau = Vtau0*self.pos[0] + Vtau1*self.pos[1] + Vtau2*self.pos[2]
y = self.Value0 - ValueTau
t_ns = time.time()-t0
#print "%es spent in re-valuation" % t_ns
L_ns = np.sort(y)
var = scs.scoreatpercentile(L_ns, self.perc*100.)
el = L_ns - self.c
eel = np.mean(el.clip(0))
#print "EEL estimated: %e" % eel
return eel
def regr_data_prep(self,kk,N_i=1):
''' Regression data preparation via nested simulations
'''
import customML as cm
# --- Computation budget allocatoin ---
N_o = int(kk/N_i)
# --- portfolio price @ t = \tau via Nested simulations---
t0 = time.time()
ran1 = npr.standard_normal((N_o,1))
S1 = np.zeros((N_o,1))
S1[:] = self.S0
S1[:] = S1[:] * np.exp((self.mu - 0.5*self.sigma*self.sigma)*self.tau + \
self.sigma * np.sqrt(self.tau) * ran1[:])
ran2 = npr.standard_normal((N_o,N_i))
S2 = np.zeros((N_o,N_i))
S2[:,:] = np.dot(S1[:],np.ones((1,N_i))) * np.exp((self.rfr - 0.5*self.sigma*self.sigma)*(self.T-self.tau) \
+ self.sigma * np.sqrt(self.T-self.tau) * ran2[:,:])
prob0 = (1.-np.exp(-2.*(np.log(np.dot(S1[:],np.ones((1,N_i)))/(self.H[0]*np.ones((N_o,N_i))))\
*np.log(S2[:,:]/(self.H[0]*np.ones((N_o,N_i))))/(self.sigma**2)/(self.T-self.tau))))\
*(np.dot(S1[:],np.ones((1,N_i))) >= self.H[0]*np.ones((N_o,N_i))).astype(float)\
*(S2[:,:] >= self.H[0]*np.ones((N_o,N_i))).astype(float)
prob1 = (1.-np.exp(-2.*(np.log(np.dot(S1[:],np.ones((1,N_i)))/(self.H[1]*np.ones((N_o,N_i))))\
*np.log(S2[:,:]/(self.H[1]*np.ones((N_o,N_i))))/(self.sigma**2)/(self.T-self.tau))))\
*(np.dot(S1[:],np.ones((1,N_i))) >= self.H[1]*np.ones((N_o,N_i))).astype(float)\
*(S2[:,:] >= self.H[1]*np.ones((N_o,N_i))).astype(float)
prob2 = (1.-np.exp(-2.*(np.log(np.dot(S1[:],np.ones((1,N_i)))/(self.H[2]*np.ones((N_o,N_i))))\
*np.log(S2[:,:]/(self.H[2]*np.ones((N_o,N_i))))/(self.sigma**2)/(self.T-self.tau))))\
*(np.dot(S1[:],np.ones((1,N_i))) >= self.H[2]*np.ones((N_o,N_i))).astype(float)\
*(S2[:,:] >= self.H[2]*np.ones((N_o,N_i))).astype(float)
Vtau0 = np.dot((np.maximum(self.K[0]-S2[:,:],0)*prob0), np.ones((N_i,1))) / \
float(N_i) * np.exp(-self.rfr*(self.T-self.tau))
Vtau1 = np.dot((np.maximum(self.K[1]-S2[:,:],0)*prob1), np.ones((N_i,1))) / \
float(N_i) * np.exp(-self.rfr*(self.T-self.tau))
Vtau2 = np.dot((np.maximum(self.K[2]-S2[:,:],0)*prob2), np.ones((N_i,1))) / \
float(N_i) * np.exp(-self.rfr*(self.T-self.tau))
ValueTau = Vtau0*self.pos[0] + Vtau1*self.pos[1] + Vtau2*self.pos[2]
t_ns = time.time() - t0
# prediction samples
#ran3 = norm(loc=0, scale=1).ppf(lhs(D, samples=I_pred))
stratified_gaussian = np.array([(i-0.5)/self.I_pred for i in range(1,self.I_pred+1)])
ran3 = norm.ppf(stratified_gaussian[:,np.newaxis])
S_pred = np.zeros((self.I_pred,1))
S_pred[:] = self.S0
S_pred[:] = S_pred[:] * np.exp((self.mu - 0.5*self.sigma*self.sigma)*self.tau +\
self.sigma*np.sqrt(self.tau) * ran3[:])
self.X = S1
self.X_pred = S_pred
self.y = ValueTau
def poly_regr(self,deg=2):
''' Polynomial linear Regression
'''
# Training
t0 = time.time()
phi = cm.naivePolyFeature(self.X,deg=deg,norm=True)
U, s, V = np.linalg.svd(phi,full_matrices=False)
r = np.dot(V.T,np.dot(U.T,self.y)/s[:,np.newaxis])
t_tr = time.time() - t0
# Predicting
t0 = time.time()
phi_pred = cm.naivePolyFeature(self.X_pred,deg=deg,norm=True)
y_lr = np.dot(phi_pred,r)
t_pr = time.time() - t0
eel = np.mean(np.maximum(self.Value0-self.c-np.sum(y_lr,axis=1),0))
return (eel, t_tr, t_pr)
def spec_regr(self):
''' Problem-specific basis polynomial Regression
'''
# Training
t0 = time.time()
phi = cm.specifiedFeature(self.X,deg=2,norm=True)
U, s, V = np.linalg.svd(phi,full_matrices=False)
r = np.dot(V.T,np.dot(U.T,self.y)/s[:,np.newaxis])
t_tr = time.time() - t0
# Predicting
t0 = time.time()
phi_pred = cm.specifiedFeature(self.X_pred,deg=2,norm=True)
y_lr = np.dot(phi_pred,r)
t_pr = time.time() - t0
eel = np.mean(np.maximum(self.Value0-self.c-np.sum(y_lr,axis=1),0))
return (eel, t_tr, t_pr)
def spec_regr_full(self):
''' Problem-specific basis polynomial Regression with full basis set
'''
# Training
t0 = time.time()
phi = cm.specifiedFeatureFull(self.X,deg=2,norm=True)
U, s, V = np.linalg.svd(phi,full_matrices=False)
r = np.dot(V.T,np.dot(U.T,self.y)/s[:,np.newaxis])
t_tr = time.time() - t0
# Predicting
t0 = time.time()
phi_pred = cm.specifiedFeatureFull(self.X_pred,deg=2,norm=True)
y_lr = np.dot(phi_pred,r)
t_pr = time.time() - t0
eel = np.mean(np.maximum(self.Value0-self.c-np.sum(y_lr,axis=1),0))
return (eel, t_tr, t_pr)
def poly_ridge(self,deg=2):
''' Polynomial Ridge Regression
'''
from sklearn import linear_model
# Training
t0 = time.time()
phi = cm.naivePolyFeature(self.X,deg=deg,norm=True)
lm = linear_model.RidgeCV(alphas=np.logspace(-10,-1,10))
lm.fit(phi,self.y)
print lm.alpha_
t_tr = time.time() - t0
# Predicting
t0 = time.time()
phi_pred = cm.naivePolyFeature(self.X_pred,deg=deg,norm=True)
y_lr = lm.predict(phi_pred)
t_pr = time.time() - t0
eel = np.mean(np.maximum(self.Value0-self.c-np.sum(y_lr,axis=1),0))
return (eel, t_tr, t_pr)
def knn(self):
''' K nearest neighbors Regression
'''
from sklearn import neighbors
from sklearn.grid_search import GridSearchCV
if len(self.y) == 2**5:
knn = GridSearchCV(neighbors.KNeighborsRegressor(n_neighbors=5,weights='uniform'),\
param_grid={"n_neighbors": [5,10,20]})
elif len(self.y) == 3**5:
knn = GridSearchCV(neighbors.KNeighborsRegressor(n_neighbors=10,weights='uniform'),\
param_grid={"n_neighbors": [10,20,50,70,100,150]})
else:
knn = GridSearchCV(neighbors.KNeighborsRegressor(n_neighbors=10,weights='uniform'),\
param_grid={"n_neighbors": [10,20,50,70,100,200,300,400]})
y_knn = knn.fit(self.X/self.S0, self.y).predict(self.X_pred/self.S0)
eel = np.mean(np.maximum(self.Value0-self.c-y_knn,0))
return eel
def svr(self,C=1000.,gamma=1e-2):
''' Support Vector Regression
'''
from sklearn.svm import SVR
# training
t0 = time.time()
svr = SVR(kernel='rbf', C=C, gamma=gamma)
svr.fit(self.X/self.S0, self.y[:,0])
t_tr = time.time() - t0
# predicting
t0 = time.time()
y_svr = svr.predict(self.X_pred/self.S0)
t_pr = time.time() - t0
eel = np.mean(np.maximum(self.Value0-self.c-y_svr,0))
return (eel, t_tr, t_pr)
def svrCV(self):
''' Support Vector Regression with Cross-Validation
'''
from sklearn.svm import SVR
from sklearn.grid_search import GridSearchCV
# training
t0 = time.time()
svr = GridSearchCV(SVR(C=1000.,kernel='rbf',gamma=1e-4), cv=5,
param_grid={"C": [100., 1000.],
"gamma": [1e-3,1e-2,1e-1]}, n_jobs=-1)
svr.fit(self.X, self.y[:,0])
t_tr = time.time() - t0
# predicting
t0 = time.time()
y_svr = svr.predict(self.X_pred)
t_pr = time.time() - t0
eel = np.mean(np.maximum(self.Value0-self.c-y_svr,0))
return (eel, t_tr, t_pr)
def re_poly2(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.poly_regr(deg=2)
eel += (t_ns,)
return eel
def re_poly5(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.poly_regr(deg=5)
eel += (t_ns,)
return eel
def re_poly8(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.poly_regr(deg=8)
eel += (t_ns,)
return eel
def re_ridge2(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.poly_ridge(deg=2)
eel += (t_ns,)
return eel
def re_ridge5(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.poly_ridge(deg=5)
eel += (t_ns,)
return eel
def re_ridge8(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.poly_ridge(deg=8)
eel += (t_ns,)
return eel
def re_spec(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.spec_regr()
eel += (t_ns,)
return eel
def re_spec_full(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.spec_regr_full()
eel += (t_ns,)
return eel
def re_svr(kk,N_i):
from EX1Bc import EX1B
import time
if kk/N_i < 1.:
eel = (np.nan, 0., 0., 0.)
else:
port = EX1B()
t0 = time.time()
port.regr_data_prep(kk,N_i)
t_ns = time.time() - t0
eel = port.svr()
eel += (t_ns,)
return eel
def conv(K,N_i=1,L=1000,regr_method=re_svr,filename='EX1Bc'):
import scipy.io
import ipyparallel
from multiprocessing import cpu_count
import os
print
print "##################################"
print "# Risk Estimation via Regression #"
print "##################################"
print
print "regression method: %s" % str(regr_method)
print "Output file: %s.mat" % filename
print "N_i = %d; L = %d" % (N_i, L)
print
print "Setting up ipyparallel"
print "CPU count: %d" % cpu_count()
print
rc = ipyparallel.Client()
print("Check point 1")
rc.block = True
print("Check point 2")
view = rc.load_balanced_view()
print("Check point 3")
dview = rc[:]
print("Check point 4")
dview.map(os.chdir, ['/home/l366wang/Code/risk_regr/']*cpu_count())
print("Check point 5")
print
print "Checks done. Commensing computations..."
print
EEL_true = 0.0203852730
mse = np.zeros(len(K))
bias2 = np.zeros(len(K))
var = np.zeros(len(K))
t_tr = np.zeros(len(K))
t_pr = np.zeros(len(K))
t_ns = np.zeros(len(K))
for k_idx, kk in enumerate(K):
print "K = %d" % kk
t0 = time.time()
eel_data = view.map(regr_method,[kk]*L,[N_i]*L)
eel = [eel_data[ii][0] for ii in range(L)]
mse[k_idx] = np.mean((np.array(eel)-EEL_true)**2)
bias2[k_idx] = (np.mean(eel)-EEL_true)**2
var[k_idx] = np.mean((np.array(eel)-np.mean(eel))**2)
t_tr[k_idx] = np.mean([eel_data[ii][1] for ii in range(L)])
t_pr[k_idx] = np.mean([eel_data[ii][2] for ii in range(L)])
t_ns[k_idx] = np.mean([eel_data[ii][3] for ii in range(L)])
print "%.2fs elapsed" % (time.time()-t0)
scipy.io.savemat('./Data/EX1B/'+filename+'.mat',mdict={'K':K,'N_i':N_i,'L':L,\
'mse':mse,'bias2':bias2,'var':var,'t_tr':t_tr, 't_pr':t_pr,\
't_ns':t_ns})
print
if __name__ == "__main__":
import EX1Bc as EX1
K = [ii**5 for ii in range(2,23)]
EX1.conv(K, N_i=1, L=1000, regr_method=re_poly2, filename='re_poly2_1')
EX1.conv(K, N_i=1, L=1000, regr_method=re_poly5, filename='re_poly5_1')
EX1.conv(K, N_i=1, L=1000, regr_method=re_poly8, filename='re_poly8_1')
EX1.conv(K, N_i=1, L=1000, regr_method=re_spec, filename='re_spec_1')
EX1.conv(K, N_i=1, L=1000, regr_method=re_spec_full, filename='re_spec_full_1')