Skip to content

Commit

Permalink
Voting rights assignment may now export information about entities
Browse files Browse the repository at this point in the history
(cumulative trust, minimal voting right, overtrust)
  • Loading branch information
lenhoanglnh committed Jan 20, 2024
1 parent 4f19ef5 commit f9bed23
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion solidago/src/solidago/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __call__(
trusts = self.trust_propagation(pretrusts, vouches)

logger.info(f"Pipeline 2. Computing voting rights with {self.voting_rights}")
voting_rights = self.voting_rights(trusts, vouches, privacy)
voting_rights, entities = self.voting_rights(trusts, entities, vouches, privacy)

logger.info(f"Pipeline 3. Computing user models with {self.preference_learning}")
user_models = {
Expand Down
9 changes: 7 additions & 2 deletions solidago/src/solidago/voting_rights/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ class VotingRightsAssignment(ABC):
def __call__(
self,
users: pd.DataFrame,
entities: pd.DataFrame,
vouches: pd.DataFrame,
privacy: pd.DataFrame,
) -> VotingRights:
privacy: pd.DataFrame
) -> tuple[VotingRights, pd.DataFrame]:
""" Compute voting rights
Parameters
----------
users: DataFrame with columns
* user_id (int, index)
* trust_score (float)
entities: DataFrame with columns
* entity_id (int, index)
vouches: DataFrame with columns
* voucher (int)
* vouchee (int)
Expand All @@ -83,6 +86,8 @@ def __call__(
-------
voting_rights[user, entity] is the voting right
of a user on entity for criterion
entities: DataFrame with columns
* entity_id (int, index)
"""
raise NotImplementedError

26 changes: 22 additions & 4 deletions solidago/src/solidago/voting_rights/affine_overtrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
def __call__(
self,
users: pd.DataFrame,
entities: pd.DataFrame,
vouches: pd.DataFrame,
privacy: PrivacySettings,
) -> VotingRights:
Expand All @@ -37,6 +38,8 @@ def __call__(
users: DataFrame with columns
* user_id (int, index)
* trust_score (float)
entities: DataFrame with columns
* entity_id (int, index)
vouches: DataFrame
This is not used by VotingRightsWithLimitedOvertrust
privacy: PrivacySettings
Expand All @@ -47,12 +50,18 @@ def __call__(
-------
voting_rights[user, entity] is the voting right
of a user on entity for criterion
entities: DataFrame with columns
* entity_id (int, index)
* cumulative_trust (float)
* min_voting_right (float)
* overtrust (float)
"""
voting_rights = VotingRights()
if len(users) == 0:
return voting_rights
return voting_rights, entities

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

privacy_weights = {
u: self.privacy_penalty if privacy[u, e] else 1
Expand All @@ -64,12 +73,21 @@ def __call__(
cumulative_trust = self.cumulative_trust(users, privacy_weights)
max_overtrust = self.maximal_overtrust(cumulative_trust)
min_voting_right = self.min_voting_right(max_overtrust, users, privacy_weights)


overtrust = 0
for u in privacy_weights:
voting_rights[u, e] = max(min_voting_right, users.loc[u, "trust_score"])
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)

return voting_rights
entities = entities.assign(cumulative_trust=cumulative_trusts)
entities = entities.assign(min_voting_right=min_voting_rights)
entities = entities.assign(overtrust=overtrusts)
return voting_rights, entities

def cumulative_trust(
self,
Expand Down
7 changes: 5 additions & 2 deletions solidago/tests/test_voting_rights.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def test_affine_overtrust():
users = pd.DataFrame(dict(trust_score=[0.5, 0.6, 0.0, 0.4, 1]))
users.index.name = "user_id"
vouches = None
entities = pd.DataFrame(dict(entity_id=range(6)))
entities.set_index("entity_id")
privacy = PrivacySettings({
0: { 0: True, 2: False, 3: False },
1: { 1: False, 2: True, 3: False },
Expand All @@ -188,7 +190,7 @@ def test_affine_overtrust():
min_overtrust=2.0,
overtrust_ratio=0.1
)
voting_rights = voting_rights_assignment(users, vouches, privacy)
voting_rights, entities = voting_rights_assignment(users, entities, vouches, privacy)

@pytest.mark.parametrize("test", range(5))
def test_affine_overtrust_test_data(test):
Expand All @@ -197,7 +199,8 @@ def test_affine_overtrust_test_data(test):
min_overtrust=2.0,
overtrust_ratio=0.1
)
rights = voting_rights_assignment(td.users[test], td.vouches[test], td.privacy[test])
rights, entities = voting_rights_assignment(
td.users[test], td.entities[test], td.vouches[test], td.privacy[test])
for entity in td.voting_rights[test].entities():
for user in td.voting_rights[test].on_entity(entity):
assert td.voting_rights[test][user, entity] == rights[user, entity], (entity, user, td.voting_rights, rights)

0 comments on commit f9bed23

Please sign in to comment.