Skip to content

Commit

Permalink
Cleaned the code a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhoanglnh committed Jan 21, 2024
1 parent e60d407 commit 0cb624f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
18 changes: 8 additions & 10 deletions solidago/src/solidago/generative_model/engagement_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __call__(
judgments[user]["comparisons"] yields the user's comparisons
judgments[user]["assessments"] yields the user's assessments
"""
comparison_dct = dict(user_id=list(), criteria=list(), entity_a=list(), entity_b=list())
comparison_list = list()
privacy = PrivacySettings()

for user, row in users.iterrows():
Expand All @@ -89,18 +89,16 @@ def __call__(
for b in compared[a_index + 1:]:
if np.random.random() >= p_compare_ab:
continue
for c in self.p_per_criterion:
if np.random.random() <= self.p_per_criterion[c]:
comparison_dct["user_id"].append(user)
comparison_dct["criteria"].append(c)
for criterion in self.p_per_criterion:
if np.random.random() <= self.p_per_criterion[criterion]:
if np.random.random() <= 0.5:
comparison_dct["entity_a"].append(a)
comparison_dct["entity_b"].append(b)
comparison_list.append((user, criterion, a, b))
else:
comparison_dct["entity_a"].append(b)
comparison_dct["entity_b"].append(a)
comparison_list.append((user, criterion, b, a))

return privacy, DataFrameJudgments(pd.DataFrame(comparison_dct))
c = list(zip(*comparison_list))
return privacy, DataFrameJudgments(pd.DataFrame(dict(
user_id=c[0], criteria=c[1], entity_a=c[2], entity_b=c[3])))

def __str__(self):
properties = f"p_per_criterion={self.p_per_criterion}, p_private={self.p_private}"
Expand Down
7 changes: 4 additions & 3 deletions solidago/src/solidago/generative_model/entity_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def __call__(self, n_entities: int):
"""
dct = dict()
svd = [self.svd_distribution(self.svd_dimension) for _ in range(n_entities)]
for i in range(self.svd_dimension):
dct[f"svd{i}"] = [svd[e][i] for e in range(n_entities)]
df = pd.DataFrame(dct)
df = pd.DataFrame({
f"svd{i}": [svd[e][i] for e in range(n_entities)]
for i in range(self.svd_dimension)
})
df.index.name = "entity_id"
return df

Expand Down
11 changes: 5 additions & 6 deletions solidago/src/solidago/generative_model/vouch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __call__(self, users: pd.DataFrame):
* `vouchee`: int
* `vouch`: float
"""
vouchers, vouchees, vouches = list(), list(), list()
vouch_list = list()
n_trustworthy = len(users[users["is_trustworthy"]])

for voucher, voucher_row in users.iterrows():
Expand All @@ -59,9 +59,8 @@ def __call__(self, users: pd.DataFrame):
continue
can_vouch = (voucher_row["is_trustworthy"] == vouchee_row["is_trustworthy"])
if can_vouch and (np.random.random() < p_vouch):
vouchers.append(voucher)
vouchees.append(vouchee)
vouches.append(1)

return pd.DataFrame(dict(voucher=vouchers, vouchee=vouchees, vouch=vouches))
vouch_list.append((voucher, vouchee, 1 - np.random.random()**2))

v = list(zip(*vouch_list))
return pd.DataFrame(dict(voucher=v[0], vouchee=v[1], vouch=v[2]))

11 changes: 4 additions & 7 deletions solidago/src/solidago/voting_rights/affine_overtrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __call__(
if len(users) == 0:
return voting_rights, entities

cumulative_trusts, min_voting_rights, overtrusts = list(), list(), list()
new_records = list()
for e in entities.index:

privacy_weights = {
Expand All @@ -80,13 +80,10 @@ def __call__(
voting_rights[u, e] *= privacy_weights[u]
overtrust += voting_rights[u, e]

cumulative_trusts.append(cumulative_trust)
min_voting_rights.append(min_voting_right)
overtrusts.append(overtrust)
new_records.append((cumulative_trust, min_voting_right, overtrust))

entities = entities.assign(cumulative_trust=cumulative_trusts)
entities = entities.assign(min_voting_right=min_voting_rights)
entities = entities.assign(overtrust=overtrusts)
r = list(zip(*new_records))
entities = entities.assign(cumulative_trust=r[0], min_voting_right=r[1], overtrust=r[2])
return voting_rights, entities

def cumulative_trust(
Expand Down

0 comments on commit 0cb624f

Please sign in to comment.