-
Notifications
You must be signed in to change notification settings - Fork 0
/
eigen.py
218 lines (179 loc) · 7.63 KB
/
eigen.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
202
203
204
205
206
207
208
209
210
211
212
213
214
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import my_graphs
#G = nx.Graph()
#G = nx.powerlaw_cluster_graph(n=10, m=2, p=0.1, seed=143)
#print(nx.to_dict_of_lists(my_graphs.G_BIPARTITE))
#print(nx.to_dict_of_lists(my_graphs.G_COMPLETE))
#print('hi')
display = False
debug = False
def eigentrust_alg(G, p_creditw, p_truepos, p_falsepos, gamma):
std_dev = 0.1
num_nodes = G.number_of_nodes()
creditw_truth = np.random.random(num_nodes) < p_creditw
curr_cost = 0
round_num = 0
# Begin eigen trust algorithm
G_adj = nx.to_numpy_matrix(G)
local_trust = np.array(G_adj.copy() )
for (x,y), element in np.ndenumerate(G_adj):
if element:
if creditw_truth[y]:
rec = min(np.random.normal(p_truepos, std_dev),1)
else:
rec = max(np.random.normal(p_falsepos, std_dev),0)
local_trust[x][y] = rec
# else, not connected
np.set_printoptions(precision=3)
if debug:
print(local_trust)
local_trust = np.array(local_trust)
if debug:
print(np.sum(local_trust, axis=1))
normalized_local_trust = (local_trust.T / np.sum(local_trust, axis=1)).T
if debug:
print(normalized_local_trust)
# iterate until convergence
e = np.average(normalized_local_trust, axis=0)
C = normalized_local_trust
if debug:
print(e)
t = np.dot(C.T, e)
if debug:
print('t')
scores = t.copy()
dist = 999
while dist > gamma:
round_num += 1
new_scores = np.dot(C.T, scores)
dist = np.linalg.norm(new_scores - scores)
scores = new_scores
if debug:
print('dist: ', dist, 'scores: ', scores)
#print('percent mistake of false positive', num_fkups /
#len(discard_list))
THRESH = 0.03
final_lend_to = scores > THRESH
indices = np.where(final_lend_to == 1)
if debug:
print('\n!-- 4 Done, round nums = ', round_num)
print('! -- decided to lend', final_lend_to)#, indices)
print('creditw truth', creditw_truth)
correct = np.array(final_lend_to) == np.array(creditw_truth)
if debug:
print('i deem these correct! ')
#fp = fkups / number loans
#FAIL fp = sum(np.invert(correct)) / sum(creditw_truth)
if debug:
print('2 FALSE POSE rate', fp)
# number correctly lent to, over total number of creditworthy people
if debug:
print('number of creditworthy people in here',sum(creditw_truth))
# positive, where it was a negative
fp = np.logical_not(creditw_truth) *final_lend_to
percent_discovered = sum(final_lend_to * creditw_truth * 1)/ sum(creditw_truth)
if debug:
print('correct or not?' ,correct * 1)
print('final lend to', final_lend_to * 1)
#print('number failed at', sum(np.logical_not(creditw_truth) *final_lend_to))
print('creditw_truth', creditw_truth * 1)
print('correctly lent', final_lend_to * creditw_truth * 1)
print(percent_discovered)
#if debug:
print('3 percent creditwoth discovered %0.3f' % percent_discovered)
#print('\nout of total good', nx.get_node_attributes(G, 'creditw').values())
curr_cost = -1 * sum(correct) + 5 * fp
if debug:
print('1 COST ', curr_cost)
return curr_cost, fp, percent_discovered, round_num
p_creditw = 0.8
p_truepos = 0.9
p_falsepos = 0.1
p_initrec = 0.1
gamma = 0.001
#discovery_alg(my_graphs.G_COMPLETE, p_creditw, p_truepos, p_falsepos, p_initrec)
eigentrust_alg(my_graphs.G_COMPLETE, p_creditw, p_truepos, p_falsepos, p_initrec)
## --------
## PAPER RUN
results = []
subgraphs_complete = False # run with 3 subgraphs
subgraphs_bipartite = True
for p_truepos, p_falsepos in [(.8,.2), (.95,.05)]:
for p_initrec in [.1, .2]:
for p_creditw in [.5, .8, .95]:
cost_avg, fpos_avg, percdisc_avg, rounds_avg = [], [], [], []
if debug:
print("\n!-----------------!\n")
print("GAMMA is 0.0001 CONDITIONS: pCW %0.2f, pinit %.2f" % (p_creditw, p_initrec),
" truepos, false pos: ", p_truepos, p_falsepos)
for i in range(10):
if subgraphs_complete:
# NOTE!
num_nodes = 10
sub_complete = nx.complete_graph(num_nodes)
curr_cost_total, fp_list, cdisc_list = 0, [], []
for i in range(3):
curr_cost, fpos, percent_discovered, round_num = \
eigentrust_alg(sub_complete, p_creditw, p_truepos,
p_falsepos, p_initrec)
curr_cost_total += curr_cost
fp_list.append(fpos)
cdisc_list.append(percent_discovered)
curr_cost = curr_cost_total
fpos = np.average(fp_list)
percent_discovered = np.average(cdisc_list)
elif subgraphs_bipartite:
# NOTE!
n_part = 5
p_connect = 0.9
sub_bipartite = nx.bipartite.generators.random_graph(n_part, n_part, p_connect, seed=88)
curr_cost_total, fp_list, cdisc_list = 0, [], []
for i in range(3):
curr_cost, fpos, percent_discovered, round_num = \
eigentrust_alg(sub_bipartite, p_creditw, p_truepos,
p_falsepos, p_initrec)
curr_cost_total += curr_cost
fp_list.append(fpos)
cdisc_list.append(percent_discovered)
curr_cost = curr_cost_total
fpos = np.average(fp_list)
percent_discovered = np.average(cdisc_list)
# round_num = round_num
else:
curr_cost, fpos, percent_discovered, round_num = \
eigentrust_alg(my_graphs.G_COMPLETE, p_creditw, p_truepos,
p_falsepos, 0.0001)
#discovery_alg(my_graphs.G_COMPLETE, p_creditw, p_truepos,
cost_avg.append(curr_cost)
fpos_avg.append(fpos)
percdisc_avg.append(percent_discovered)
rounds_avg.append(round_num)
# print('cost per run, cost avg', cost_avg, len(cost_avg))
if debug:
print('fpos per run, fpos avg', fpos_avg)
c = np.average(cost_avg)
fp = np.average(fpos_avg)
pd = np.average(percdisc_avg)
r = np.average(rounds_avg)
results.append([c, fp, pd, r])
if debug:
print('AVERAGED cost: %.2f, fpos: %.3f, p_disc: %.3f, round: %.1f'
%(c, fp, pd, r))
print("\n!-----------------!\n")
print(np.array(results))
np.savetxt('./data.csv', np.array(results))
# eigentrust_alg(my_graphs.G_COMPLETE_SUB1
# , p_creditw, p_truepos, p_falsepos, p_initrec)
# eigentrust_alg(my_graphs.G_COMPLETE_SUB2
# , p_creditw, p_truepos, p_falsepos, p_initrec)
# eigentrust_alg(my_graphs.G_COMPLETE_SUB3
# , p_creditw, p_truepos, p_falsepos, p_initrec)
# eigentrust_alg(my_graphs.G_BIPARTITE, p_creditw, p_truepos, p_falsepos, p_initrec)
# eigentrust_alg(my_graphs.G_BIPARTITE_SUB1
# , p_creditw, p_truepos, p_falsepos, p_initrec)
# eigentrust_alg(my_graphs.G_BIPARTITE_SUB2
# , p_creditw, p_truepos, p_falsepos, p_initrec)
# eigentrust_alg(my_graphs.G_BIPARTITE_SUB3
# , p_creditw, p_truepos, p_falsepos, p_initrec)