-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.py
174 lines (146 loc) · 5.59 KB
/
analyze.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
import matplotlib.pyplot as plt
import numpy as np
import math
import pickle
import sys
import argparse
parser = argparse.ArgumentParser(description='Plot the experimental results.')
parser.add_argument('-n_rep', '--n_rep', default=0, help="The number of repeted simulations for an experiment.", type=int)
parser.add_argument('-title', '--title', help="Plot title.",)
parser.add_argument('-baseline', '--baseline', default=0, help="With baseline (0: None, 1: VOTING, 2: VOTING & SWITCH)", type=int)
parser.add_argument('-x', '--x_max', default=1000, help='the maximum x (number of assignment) range for plotting (deafult 1000)', type=int)
def plot_estimates(x_y_list, title, x=np.arange(10,1000,10), label='T-WALK'):
estimates = []
for x_y in x_y_list:
y_ = []
for x_ in x:
if x_ not in x_y:
y_.append(float('nan'))
else:
y_.append(x_y[x_])
estimates.append(y_)
y = np.mean(estimates, axis=0)
y_std = np.std(estimates, axis=0)
plt.figure()
plt.errorbar(x, y, yerr=y_std, fmt='o-', label=label)
plt.legend()
plt.title(title)
plt.xlabel('Num. assignments')
plt.ylabel('Num. errors')
plt.grid(True)
plt.savefig('figs/estimates1.png')
def plot_with_baseline(x_y_list1, x_y_list2, title, labels=['T-WALK','VOTING'], x=np.arange(10,1000,10)):
estimates1 = []
for x_y in x_y_list1:
y_ = []
for x_ in x:
y_.append(x_y[x_])
estimates1.append(y_)
estimates2 = []
for x_y in x_y_list2:
y_ = []
for x_ in x:
y_.append(x_y[x_])
estimates2.append(y_)
y1 = np.mean(estimates1, axis=0)
y_std1 = np.std(estimates1, axis=0)
y2 = np.mean(estimates2, axis=0)
y_std2 = np.std(estimates2, axis=0)
plt.figure()
plt.xlim([0,x[-1]])
plt.errorbar(x, y1, yerr=y_std1, fmt='ro-', label=labels[0])
plt.errorbar(x, y2, yerr=y_std2, fmt='g^-', label=labels[1])
plt.title(title)
plt.legend()
plt.xlabel('Num. assignments')
plt.ylabel('Num. errors')
plt.grid(True)
plt.savefig('figs/estimates2.png')
def plot_with_baselines(x_y_list1, x_y_list2, x_y_list3, title, labels=['T-WALK','VOTING','SWITCH'], x=np.arange(10,1000,10)):
estimates1 = []
for x_y in x_y_list1:
y_ = []
for x_ in x:
y_.append(x_y[x_])
estimates1.append(y_)
estimates2 = []
for x_y in x_y_list2:
y_ = []
for x_ in x:
y_.append(x_y[x_])
estimates2.append(y_)
estimates3 = []
for x_y in x_y_list3:
y_ = []
for x_ in x:
y_.append(x_y[x_])
estimates3.append(y_)
y1 = np.mean(estimates1, axis=0)
y_std1 = np.std(estimates1, axis=0)
y2 = np.mean(estimates2, axis=0)
y_std2 = np.std(estimates2, axis=0)
y3 = np.mean(estimates3, axis=0)
y_std3 = np.std(estimates3, axis=0)
plt.figure()
plt.xlim([0,x[-1]])
plt.ylim([0, 40])
plt.errorbar(x, y1, yerr=y_std1, fmt='ro-', label=labels[0])
plt.errorbar(x, y2, yerr=y_std2, fmt='g^-', label=labels[1])
plt.errorbar(x, y3, yerr=y_std3, fmt='b^-', label=labels[2])
plt.title(title)
plt.legend()
plt.xlabel('Num. assignments')
plt.ylabel('Num. errors')
plt.grid(True)
plt.savefig('figs/estimates3.png')
def main():
args = parser.parse_args()
n_rep = args.n_rep
title = args.title
baseline = args.baseline
x_max = args.x_max
x_ = np.arange(40,x_max,40)
x_y_list = []
if n_rep == 0:
x_y = pickle.load(open('results/restaurant_estimates_real.p','rb'))
#x_y = pickle.load(open('results/restaurant_estimates_real_voting1_1000resp.p', 'rb'))
x_y_list.append(x_y)
else:
for i in range(n_rep):
x_y = pickle.load(open('results/restaurant_estimates_simulated%s.p'%i,'rb'))
x_y_list.append(x_y)
plot_estimates(x_y_list, title, x=x_)
if baseline == 1:
x_y_list2 = []
if n_rep == 0:
x_y = pickle.load(open('results/restaurant_estimates_real_voting.p','rb'))
#x_y = pickle.load(open('results/restaurant_estimates_real_voting1_1000resp.p', 'rb'))
x_y_list2.append(x_y)
else:
for i in range(n_rep):
x_y = pickle.load(open('results/restaurant_estimates_simulated_voting%s.p'%i,'rb'))
x_y_list2.append(x_y)
plot_with_baseline(x_y_list, x_y_list2, title, ['T-WALK','VOTING'], x=x_)
if baseline == 2:
x_y_list2 = []
if n_rep == 0:
x_y = pickle.load(open('results/restaurant_estimates_real_voting.p','rb'))
#x_y = pickle.load(open('results/restaurant_estimates_real_voting1_1000resp.p', 'rb'))
x_y_list2.append(x_y)
else:
for i in range(n_rep):
x_y = pickle.load(open('results/restaurant_estimates_simulated_voting%s.p'%i,'rb'))
x_y_list2.append(x_y)
plot_with_baseline(x_y_list, x_y_list2, title, ['T-WALK','VOTING'], x=x_)
x_y_list3 = []
if n_rep == 0:
x_y = pickle.load(open('results/restaurant_estimates_real_switch.p','rb'))
#x_y = pickle.load(open('results/restaurant_estimates_real_voting1_1000resp.p', 'rb'))
x_y_list3.append(x_y)
else:
for i in range(n_rep):
x_y = pickle.load(open('results/restaurant_estimates_simulated_switch%s.p'%i,'rb'))
x_y_list3.append(x_y)
plot_with_baselines(x_y_list, x_y_list2, x_y_list3, title, ['T-WALK','VOTING','SWITCH'], x=x_)
if __name__ == "__main__":
main()