-
Notifications
You must be signed in to change notification settings - Fork 1
/
synthetic.py
151 lines (125 loc) · 6.23 KB
/
synthetic.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
import json
import sys
import numpy as np
from src import estimate as estimate
from src import ranksets as ranksets
import argparse
import os
def generate_data(k, n, theta, thetatilde):
n = n // k * k
winner1_human, winner2_human = np.zeros(shape=(n, 1)), np.zeros(shape=(n, 1))
winner1_llm, winner2_llm = [np.zeros(shape=(n, 1)) for _ in range(len(thetatilde))], [np.zeros(shape=(n, 1)) for _
in range(len(thetatilde))]
model_a_matrix = np.zeros(shape=(k, n))
model_b_matrix = np.zeros(shape=(k, n))
for i in range(n // k * k):
m1 = i % k
model_a_matrix[m1][i] = 1
m2 = (i // k) % k
model_b_matrix[m2][i] = 1
match = np.random.rand(1)[0]
if match < 2 * theta[m1]:
winner1_human[i] = 1
for j in range(len(thetatilde)):
if match < 2 * thetatilde[j][m1]:
winner1_llm[j][i] = 1
summarized = [{'winner1_predicted': winner1_llm[j],
'winner2_predicted': winner2_llm[j],
'winner1_human': winner1_human,
'winner2_human': winner2_human,
'model_a_matrix': model_a_matrix,
'model_b_matrix': model_b_matrix} for j in range(len(thetatilde))]
return summarized
def run_synthetic_one(k, n, N, noises, alpha, seed):
np.random.seed(seed)
theta=np.random.uniform(low=0.2, high=0.8, size=(k,))
theta=sorted(theta, reverse=True)
thetatildes=[theta+np.random.uniform(low=-noises[i], high=noises[i], size=(k,)) for i in range(len(noises))]
thetatildes=[[max(0.01,min(0.99,thetatildes[j][i])) for i in range(k)] for j in range(len(noises))]
theta=[theta[i]/sum(theta) for i in range(k)]
thetatildes=[[thetatildes[j][i]/sum(thetatildes[j]) for i in range(k)] for j in range(len(noises))]
Dn=generate_data(k,n,theta,thetatildes)
DN=generate_data(k,N-n,theta,thetatildes)
DnN=[{'winner1_human': np.concatenate((Dn[0]['winner1_human'],DN[0]['winner1_human'])),
'winner2_human': np.concatenate((Dn[0]['winner2_human'],DN[0]['winner2_human'])),
'winner1_predicted': np.concatenate((Dn[i]['winner1_predicted'],DN[i]['winner1_predicted'])),
'winner2_predicted': np.concatenate((Dn[i]['winner2_predicted'],DN[i]['winner2_predicted'])),
'model_a_matrix': np.concatenate((Dn[0]['model_a_matrix'],DN[0]['model_a_matrix']),axis=1),
'model_b_matrix': np.concatenate((Dn[0]['model_b_matrix'],DN[0]['model_b_matrix']),axis=1)
} for i in range(len(noises))]
results=dict()
thetas_results=dict()
thetas_results['baseline']=theta
for j in range(len(noises)):
thetas_results['thetatilde '+str(noises[j])]=thetatildes[j]
lhat_opt=estimate.lhat_opt(k,Dn[j],DN[j])
thetahat,sigma=estimate.estimate(k, Dn[j], DN[j],lhat=lhat_opt)
thetas_results['ppr '+str(noises[j])]=np.ndarray.flatten(thetahat).tolist()
rank_sets=ranksets.find_ranksets(k,thetahat,sigma,alpha=alpha)
results['ppr '+str(noises[j])]={i+1:rank_sets[i] for i in range(k)}
thetahat,sigma = estimate.sample_avg(k, DnN[j],'llm')
thetas_results['llm ' + str(noises[j])] = np.ndarray.flatten(thetahat).tolist()
rank_sets = ranksets.find_ranksets(k, thetahat, sigma, alpha=alpha)
results['llm ' + str(noises[j])] = {i + 1: rank_sets[i] for i in range(k)}
#
thetahat,sigma=estimate.sample_avg(k, Dn[0], 'human')
rank_sets=ranksets.find_ranksets(k,thetahat,sigma,alpha=alpha)
thetas_results['human only'] = np.ndarray.flatten(thetahat).tolist()
results['human only']={i+1:rank_sets[i] for i in range(k)}
results['baseline']={i+1:[i+1] for i in range(k)}
return results, thetas_results
def main(iterations=300,
k=8,
n=[400,1000,5000,10000,20000],
N=50000,
alpha=0.05,
noises=[0.05, 0.1, 0.3],
seed=12345678,
output_dir='outputs/synthetic/') -> 0:
if type(n)==str:
n=n[1:-1]
n=[int(x) for x in n.split(',')]
if type(noises)==str:
noises=noises[1:-1]
noises=[float(x) for x in noises.split(',')]
np.random.seed(seed)
seeds=np.random.randint(low=0,high=10000000,size=iterations*len(n))
if os.path.exists(output_dir):
files = os.listdir(output_dir)
for file in files:
os.remove(os.path.join(output_dir, file))
else:
os.makedirs(output_dir)
for i in range(iterations):
if i%20==0:
print(i)
for nn in n:
results, thetas = run_synthetic_one(k,nn,N,noises,alpha,seeds[i])
results['iteration']=i
thetas['iteration']=i
with open(output_dir+'/k'+str(k)+'_'+str(nn)+'_'+str(alpha).split('.')[1]+'_ranksets.json', 'a') as f:
json.dump(results, f)
f.write('\n')
with open(output_dir+'/k'+str(k)+'_'+str(nn)+'_'+str(alpha).split('.')[1]+'_thetas.json', 'a') as f:
json.dump(thetas, f)
f.write('\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run synthetic experiment.')
parser.add_argument('--iterations', type=int, default=300, help='Number of iterations')
parser.add_argument('--k', type=int, default=8, help='Number of models')
parser.add_argument('--n', type=str, default='400,1000,5000,10000,20000', help='Number of pairwise comparisons by humans')
parser.add_argument('--N', type=int, default=50000, help='Number of pairwise comparisons by LLMs')
parser.add_argument('--alpha', type=float, default=0.05, help='Error probability')
parser.add_argument('--noises', type=str, default='0.05,0.1,0.3', help='Noise levels')
parser.add_argument('--seed', type=int, default=12345678, help='Random seed')
parser.add_argument('--output_dir', type=str, default='outputs/synthetic/sameNn', help='Output directory')
args = parser.parse_args()
sys.exit(main(iterations=args.iterations,
k=args.k,
n=args.n,
N=args.N,
alpha=args.alpha,
noises=args.noises,
seed=args.seed,
output_dir=args.output_dir))
sys.exit(main())