-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_hedging.py
57 lines (51 loc) · 1.56 KB
/
local_hedging.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
import numpy as np
import matplotlib.pyplot as plt
import data
import functions
from random import randint
def plot_k(obj, data, labels, h, min_k, max_k, step_k, title):
# calculate values of the objective
ks = []
objs = []
prev_fail = False
for i in range(int((max_k-min_k)/step_k)):
new_k = min_k + i * step_k
new_obj, success = obj(data, labels, new_k, h)
print('k= ', new_k, 'optimization success= ', success)
if success:
ks.append(new_k)
objs.append(new_obj)
elif prev_fail:
break
else:
prev_fail = True
for x, y in zip(ks, objs):
label = "{:.2f}".format(y)
plt.annotate(label, # this is the text
(x, y), # this is the point to label
ha='center') # horizontal alignment can be left, right or center
plt.xlabel('kappa')
plt.ylabel('f')
plt.title(title)
plt.plot(ks, objs, 'go--')
plt.show()
if __name__ == '__main__':
n = 1000
m = 10
min_k = 0.1
max_k = 2
step_k = 0.1
#title = 'lower semideviation-based risk measure'
#fun = functions.ex4_objective
#title = 'standard deviation-based risk measure'
#fun = functions.ex5_objective
title = 'CVaR'
fun = functions.ex6_objective
# create h
h = np.ones((n, m)) / np.sqrt(m)
for _ in range(int(n*m/2)):
i = randint(0, n-1)
j = randint(0, m-1)
h[i, j] = -1.0
data, labels = data.get_toy_dataset(n, m)
plot_k(fun, data, labels, h, min_k, max_k, step_k, title)