-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_strata.py
201 lines (154 loc) · 5.67 KB
/
analyze_strata.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import pickle
import numpy as np
import matplotlib.pyplot as plt
import os
from itertools import combinations
def get_max_value(nested_dict):
max_value = 0
for _, v in nested_dict.items():
if isinstance(v, dict):
v = get_max_value(v)
if v > max_value:
max_value = v
return max_value
def get_sum_value(nested_dict):
sum_value = 0
for _, v in nested_dict.items():
if isinstance(v, dict):
v = get_sum_value(v)
sum_value += v
return sum_value
################################################################################
# Folktables
probs_file = os.path.join("data", "folktables", "all_nks.pkl")
ts = 5916565
print("="*5, "Computing for Folktables", "="*5)
with open(probs_file, 'rb') as f:
ns = pickle.load(f)
n = 5
max_ns = [[] for _ in range(1, n+1)]
for k in range(1, n+1):
possible_collaborators = list(range(n))
agent_combinations_list = list(combinations(possible_collaborators, k))
for agent_combination in agent_combinations_list:
agent_comb_str = ''.join([str(elem) for elem in agent_combination])
base_agent = agent_combination[0]
rem_agents_str = agent_comb_str[1:]
ns_dict = ns[base_agent][k-1][rem_agents_str]
max_ns[k-1].append(get_max_value(ns_dict))
max_ps = [[x/ts for x in max_ns[k]] for k in range(n)]
# create data points from max ps
xs1 = []; ys1 = []
for k in range(n):
# sample a small number between 0 and 1
# to add some noise to the data points
noise = np.random.uniform(0, 0.2, len(max_ps[k]))
xs1.extend([k+1+noise[i] for i in range(len(max_ps[k]))])
ys1.extend([max_ps[k][i] for i in range(len(max_ps[k]))])
################################################################################
# German Credit
probs_file = os.path.join("data", "german_credit", "all_nks.pkl")
ts = 1000
print("="*5, "Computing for German Credit", "="*5)
with open(probs_file, 'rb') as f:
ns = pickle.load(f)
n = 5
max_ns = [[] for _ in range(1, n+1)]
for k in range(1, n+1):
possible_collaborators = list(range(n))
agent_combinations_list = list(combinations(possible_collaborators, k))
for agent_combination in agent_combinations_list:
agent_comb_str = ''.join([str(elem) for elem in agent_combination])
base_agent = agent_combination[0]
rem_agents_str = agent_comb_str[1:]
ns_dict = ns[base_agent][k-1][rem_agents_str]
max_ns[k-1].append(get_max_value(ns_dict))
max_ps = [[x/ts for x in max_ns[k]] for k in range(n)]
# create data points from max ps
xs2 = []; ys2 = []
for k in range(n):
# sample a small number between 0 and 1
# to add some noise to the data points
noise = np.random.uniform(0, 0.2, len(max_ps[k]))
xs2.extend([k+1+noise[i] for i in range(len(max_ps[k]))])
ys2.extend([max_ps[k][i] for i in range(len(max_ps[k]))])
################################################################################
# propublica
probs_file = os.path.join("data", "propublica", "all_nks.pkl")
ts = 6172
print("="*5, "Computing for Propublica", "="*5)
with open(probs_file, 'rb') as f:
ns = pickle.load(f)
n = 5
max_ns = [[] for _ in range(1, n+1)]
for k in range(1, n+1):
possible_collaborators = list(range(n))
agent_combinations_list = list(combinations(possible_collaborators, k))
for agent_combination in agent_combinations_list:
agent_comb_str = ''.join([str(elem) for elem in agent_combination])
base_agent = agent_combination[0]
rem_agents_str = agent_comb_str[1:]
ns_dict = ns[base_agent][k-1][rem_agents_str]
max_ns[k-1].append(get_max_value(ns_dict))
max_ps = [[x/ts for x in max_ns[k]] for k in range(n)]
# create data points from max ps
xs3 = []; ys3 = []
for k in range(n):
# sample a small number between 0 and 1
# to add some noise to the data points
noise = np.random.uniform(0, 0.2, len(max_ps[k]))
xs3.extend([k+1+noise[i] for i in range(len(max_ps[k]))])
ys3.extend([max_ps[k][i] for i in range(len(max_ps[k]))])
# regression line y = 1/2x
reg_xs = np.linspace(1, 5, 100)
reg_ys = [1/(2*x) for x in reg_xs]
################################################################################
print("="*5, "Generating plot", "="*5)
# Set the params
s = 6
params = {
'legend.fontsize': s,
'legend.title_fontsize': s,
'xtick.labelsize': 6,
'ytick.labelsize': 6,
'axes.labelsize': s+1,
'text.usetex': False,
'figure.figsize': [3.5, 1.4],
'lines.linewidth': 1,
'lines.markersize': 3,
'axes.titlesize': s,
}
# set rc params
plt.rcParams.update(params)
# no background for the legend
plt.rc('legend', frameon=False) # no background for the legend
# make 1 x 3 subplots
fig, ax = plt.subplots(nrows=1, ncols=3)
# add grid to the subplots
for i in range(3):
ax[i].grid(True, which='both', linestyle='--', linewidth=0.5)
# scatter each dataset across the subplots
ax[0].scatter(xs1, ys1)
ax[1].scatter(xs2, ys2)
ax[2].scatter(xs3, ys3)
# plot the regression line y = 1/2x
for i in range(3):
ax[i].plot(reg_xs, reg_ys, color='red')
# set the x and y labels
ax[0].set_xlabel('No. of agents')
ax[0].set_ylabel('Relative size of \n largest stratum')
ax[1].set_xlabel('No. of agents')
ax[2].set_xlabel('No. of agents')
# set the title for each subplot
ax[0].set_title('Folktables')
ax[1].set_title('German Credit')
ax[2].set_title('Propublica')
# show all xticks
for i in range(3):
ax[i].set_xticks(range(1, 6))
plt.tight_layout()
plot_name = 'largest_stratum.pdf'
save_file_path = os.path.join("results", "plots")
if not os.path.exists(save_file_path):
os.makedirs(save_file_path)
plt.savefig(os.path.join(save_file_path, plot_name), bbox_inches='tight', dpi=300)