-
Notifications
You must be signed in to change notification settings - Fork 1
/
alpha.py
47 lines (39 loc) · 1.28 KB
/
alpha.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
import numpy as np
import matplotlib.pyplot as plt
def calculate_CVaR(alpha, scenario_costs, beta=0.8):
scenario_costs_expr = np.vstack(scenario_costs)
M = scenario_costs_expr - alpha
postM = np.maximum(M, 0)
CVaR_loss = alpha + (1/(len(scenario_costs)*(1-beta))) * np.sum(postM)
return CVaR_loss, M
# Generate random scenario costs between 50 and 100
np.random.seed(42) # For reproducibility
scenario_costs = np.random.uniform(500, 10000, 20)
# Define a range of alpha values
alpha_values = np.linspace(min(scenario_costs), max(scenario_costs), 100)
CVaR_values = []
M_values = []
# Calculate CVaR and M for each alpha
for alpha in alpha_values:
CVaR_loss, M = calculate_CVaR(alpha, scenario_costs)
CVaR_values.append(CVaR_loss)
M_values.append(M)
# Plot CVaR as a function of alpha
plt.figure(figsize=(10, 5))
plt.plot(alpha_values, CVaR_values, label='CVaR')
plt.xlabel('Alpha')
plt.ylabel('CVaR')
plt.title('CVaR as a function of Alpha')
plt.legend()
plt.grid(True)
plt.show()
# Plot M as a function of alpha
plt.figure(figsize=(10, 5))
for i in range(len(scenario_costs)):
plt.plot(alpha_values, [M[i] for M in M_values], label=f'Scenario {i+1}')
plt.xlabel('Alpha')
plt.ylabel('M')
plt.title('M as a function of Alpha')
plt.legend()
plt.grid(True)
plt.show()