-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
120 lines (100 loc) · 3.87 KB
/
plot.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
from collections import defaultdict
from pathlib import Path
from argparse import ArgumentParser
import numpy as np
import scipy.stats as stats
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
def plot_combined(name, results):
results = np.array(results)
xs = np.arange(results.shape[1])
ys = np.mean(results, axis=0)
# get the 95% confidence interval
yerrs = stats.sem(results, axis=0) * stats.t.ppf((1 + 0.95) / 2, results.shape[0] - 1)
plt.fill_between(xs, ys - yerrs, ys + yerrs, alpha=0.25)
plt.plot(xs, ys, label=name)
def plot_accuracy(all_results, title, filename):
plt.figure()
plt.title(title)
plt.xlabel("Iteration")
for name, results in all_results.items():
plot_combined(name, results)
plt.ylim(0.2, 0.8)
plt.legend()
plt.savefig(filename, bbox_inches="tight")
plt.close()
def plot_regret(all_results, title, filename):
plt.figure()
plt.title(title)
plt.xlabel("Iteration")
for name, results in all_results.items():
plot_combined(name, results)
plt.legend()
plt.savefig(filename, bbox_inches="tight")
plt.close()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--agents", required=True, help="Comma-separated list of agents to plot"
)
args = parser.parse_args()
agents = args.agents.split(",")
agent_mapping = {
"fixed": "Fixed baseline",
"linear": "Linear baseline",
"ucb": "UCB",
"linucb": "LinUCB",
"supervised-lin": "Supervised Linear Regression",
# this one represnts the optimal ridge regression
"supervised-ridge": "Supervised Ridge Regression (alpha=0.5)",
"supervised-ridge-0.01": "alpha=0.01",
"supervised-ridge-0.05": "alpha=0.05",
"supervised-ridge-0.1": "alpha=0.1",
"supervised-ridge-0.5": "alpha=0.5",
"supervised-ridge-1": "alpha=1",
"supervised-ridge-5": "alpha=5",
"thompson-0": "variance=0",
"thompson-0.01": "variance=0.01",
"thompson-0.1": "variance=0.1",
"thompson-0.3": "variance=0.3",
"thompson-0.5": "variance=0.5",
"thompson-0.7": "variance=0.7",
"thompson-1": "variance=1",
"thompson-2": "variance=2",
"thompson-5": "variance=5",
"thompson-10": "Thompson (variance=10)",
"thompson-20": "variance=20",
"thompson-100": "variance=100",
"ensemble-0": "noise_std = 0",
"ensemble-0.01": "noise_std = 0.01",
"ensemble-0.1": "Ensemble (noise_std = 0.1)",
"ensemble-0.5": "noise_std = 0.5",
"ensemble-1": "Ensemble (noise_std = 1)",
"ensemble-2": "noise_std = 2",
"ensemble-5": "noise_std = 5",
"ensemble-10": "noise_std = 10",
"ensemble-20": "noise_std = 20",
"ensemble-100": "noise_std = 100",
}
all_results_accuracy = {}
all_results_regret = {}
for seed in range(0, 20):
format_str = f"{{}}-seed={seed}"
for agent in agents:
if agent not in agent_mapping:
raise ValueError(f"Agent {agent} not recognized")
name = agent_mapping[agent]
if name not in all_results_accuracy:
all_results_accuracy[name] = []
all_results_regret[name] = []
all_results_accuracy[name].append(
np.load("results/" + format_str.format(agent) + "/accuracy.npy")
)
all_results_regret[name].append(
np.load("results/" + format_str.format(agent) + "/regret.npy")
)
accuracy_title = '_'.join(agents) + '_accuracy.png'
regret_title = '_'.join(agents) + '_regret.png'
plot_accuracy(all_results_accuracy, "Fraction of Incorrect Dosing Decisions", f"plots/{accuracy_title}")
plot_regret(all_results_regret, "Cumulative Regret", f"plots/{regret_title}")