diff --git a/solidago/src/solidago/pipeline/__init__.py b/solidago/src/solidago/pipeline/__init__.py index ecfba77e7b..140be63cb3 100644 --- a/solidago/src/solidago/pipeline/__init__.py +++ b/solidago/src/solidago/pipeline/__init__.py @@ -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 = { diff --git a/solidago/src/solidago/voting_rights/__init__.py b/solidago/src/solidago/voting_rights/__init__.py index 1f15e6103b..15b117a4bd 100644 --- a/solidago/src/solidago/voting_rights/__init__.py +++ b/solidago/src/solidago/voting_rights/__init__.py @@ -62,9 +62,10 @@ 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 @@ -72,6 +73,8 @@ def __call__( 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) @@ -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 diff --git a/solidago/src/solidago/voting_rights/affine_overtrust.py b/solidago/src/solidago/voting_rights/affine_overtrust.py index 8a7a293701..53139c1d92 100644 --- a/solidago/src/solidago/voting_rights/affine_overtrust.py +++ b/solidago/src/solidago/voting_rights/affine_overtrust.py @@ -27,6 +27,7 @@ def __init__( def __call__( self, users: pd.DataFrame, + entities: pd.DataFrame, vouches: pd.DataFrame, privacy: PrivacySettings, ) -> VotingRights: @@ -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 @@ -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 @@ -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, diff --git a/solidago/tests/test_voting_rights.py b/solidago/tests/test_voting_rights.py index ec5a1a713a..eae497b34f 100644 --- a/solidago/tests/test_voting_rights.py +++ b/solidago/tests/test_voting_rights.py @@ -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 }, @@ -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): @@ -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)