-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_sim.py
72 lines (60 loc) · 1.9 KB
/
run_sim.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
import numpy as np
from frechet import Frechet
from burr import Burr
from half_t import HalfT
import multiprocessing as mp
from cvar_iter import cvar_iter
# generate samples from distributions
def gen_samples(dists, s, n):
data = []
for d in dists:
data.append(d.rand((s, n)))
return np.array(data)
# generate CVaR estimates from sample data
def get_cvars(dist_data, alph, sampsizes):
n_cpus = mp.cpu_count()
pool = mp.Pool(n_cpus)
sa = [] # sample average estimates
bpot = [] # biased POT estimates
upot = [] # unbiased POT estimates
#parameter estimates
xi = []
sigma = []
rho = []
k = []
ae = []
for d in dist_data:
result = [pool.apply_async(cvar_iter, args=(x, alph, sampsizes)) for x in d]
result = np.array([r.get() for r in result])
sa.append(result[:,:,0])
bpot.append(result[:,:,1])
upot.append(result[:,:,2])
xi.append(result[:,:,3])
sigma.append(result[:,:,4])
rho.append(result[:,:,5])
k.append(result[:,:,6])
ae.append(result[:,:,7])
return np.array([sa, bpot, upot, xi, sigma, rho, k, ae])
if __name__ == '__main__':
# CVaR level
alph = 0.998
xi = 2/3
d = np.array([4, 3, 2.25, 0.75, 0.45])
c = 1/(xi*d)
burr_params = np.around([(i,j) for i,j in zip(c,d)], 2)
fs_parms = np.linspace(1.5, 2.5, 5)
dists = [Burr(*p) for p in burr_params]
dists += [Frechet(p) for p in fs_parms]
dists += [HalfT(p) for p in fs_parms]
# sample sizes to test CVaR estimation
sampsizes = np.linspace(5000, 50000, 10).astype(int)
n_max = sampsizes[-1]
# number of independent runs
s = 1000
# generate data
np.random.seed(0)
data = gen_samples(dists, s, n_max)
np.save('data/samples.npy', data)
# get CVaR and parameter estimates
result = get_cvars(data, alph, sampsizes)
np.save('data/cvars.npy', result)