Пример #1
0
 def __init__(self,
              *args,
              points_scheme: list = None,
              points_fill: Union[Number, None] = 0,
              points_unordered: Union[Number, None] = 0,
              points_absent: Union[Number, None] = None,
              **kwargs):
     self.points_scheme = [convert_number(x) for x in points_scheme]
     self.points_fill = convert_number(points_fill)
     self.points_unordered = convert_number(points_unordered)
     self.points_absent = convert_number(points_absent)
     super().__init__(*args, **kwargs)
 def scores_(self) -> NiceDict:
     levels_ = NiceDict({c: [] for c in self.candidates_})
     weights_ = NiceDict({c: [] for c in self.candidates_})
     for ballot, weight, voter in self.profile_converted_.items():
         for c, level in self.scorer(ballot=ballot, voter=voter, candidates=self.candidates_).scores_.items():
             levels_[c].append(level)
             weights_[c].append(weight)
     scores_ = NiceDict()
     for c in self.candidates_:
         if not levels_[c]:
             scores_[c] = (self.default_median, 0)
             continue
         indexes = self.scorer.scale.argsort(levels_[c])
         levels_[c] = [levels_[c][i] for i in indexes]
         weights_[c] = [weights_[c][i] for i in indexes]
         total_weight = sum(weights_[c])
         half_total_weight = my_division(total_weight, 2)
         cumulative_weight = 0
         median = None
         for i, weight in enumerate(weights_[c]):
             cumulative_weight += weight
             if cumulative_weight >= half_total_weight:
                 median = levels_[c][i]
                 break
         support = convert_number(sum([
             weights_[c][i] for i, level in enumerate(levels_[c]) if self.scorer.scale.ge(level, median)]))
         scores_[c] = (median, support)
     return scores_
Пример #3
0
    def append(self, ballot: object, weight: Number=1, voter: object=None) -> None:
        """
        Append a ballot to the profile.

        Parameters
        ----------
        ballot : object
            A ballot or, more generally, an input that can be interpreted by :class:`ConverterBallotGeneral`.
        weight : Number
            The weight of the ballot.
        voter : object
            The voter.

        Examples
        --------
            >>> profile = Profile(['a > b'])
            >>> profile.append('b > a')
            >>> print(profile)
            a > b
            b > a
        """
        self._ballots.append(ConverterBallotGeneral()(ballot))
        self._weights.append(convert_number(weight))
        self._voters.append(voter)
        self.delete_cache()
 def scores_(self) -> NiceDict:
     matrix = self.matrix_weighted_majority_
     return NiceDict({
         c: convert_number(
             sum([
                 v for (i, j), v in matrix.as_dict_.items()
                 if i == c and j != c and v < 0
             ]))
         for c in matrix.candidates_
     })
Пример #5
0
 def __init__(self,
              *args,
              converter: ConverterBallot = None,
              matrix_weighted_majority: Matrix = None,
              greater: Number = 1,
              lower: Number = 0,
              equal: Number = Fraction(1, 2),
              diagonal: Number = Fraction(1, 2),
              **kwargs):
     if converter is None:
         converter = ConverterBallotToOrder()
     if matrix_weighted_majority is None:
         matrix_weighted_majority = MatrixWeightedMajority()
     self.matrix_weighted_majority = matrix_weighted_majority
     self.greater = convert_number(greater)
     self.lower = convert_number(lower)
     self.equal = convert_number(equal)
     self.diagonal = convert_number(diagonal)
     super().__init__(*args, converter=converter, **kwargs)
Пример #6
0
    def _parse(self, b: dict) -> None:
        """
        For this subclass, the internal representation is of the form {'a': 10, 'b': 7, 'c': 3}, meaning
        that a has evaluation 10; b, 7; and c, 3.

        Parameters
        ----------
        b : dict
        """
        self._internal_representation = NiceDict(
            {c: convert_number(v)
             for c, v in b.items()})
Пример #7
0
    def __mul__(self, other: Number) -> 'Profile':
        """
        Multiply the weights.

        Parameters
        ----------
        other : Number

        Returns
        -------
        Profile
            This profile, with weights multiplied by the number.

        Examples
        --------
            >>> profile = Profile(['a > b', 'b > a'])
            >>> print(profile * 3)
            (3): a > b
            (3): b > a
        """
        other = convert_number(other)
        return Profile(ballots=self.ballots, weights=[convert_number(w * other) for w in self.weights],
                       voters=self.voters)
Пример #8
0
 def __init__(self, ballots: Union[list, 'Profile'], weights: list = None, voters: list = None):
     converter = ConverterBallotGeneral()
     self._ballots = [converter(b) for b in ballots]
     if weights is None:
         if isinstance(ballots, Profile):
             weights = ballots.weights
         else:
             weights = [1] * len(ballots)
     else:
         weights = [convert_number(w) for w in weights]
     self._weights = weights
     if voters is None:
         if isinstance(ballots, Profile):
             self._voters = ballots.voters
         else:
             self._voters = [None] * len(ballots)
     else:
         self._voters = voters
Пример #9
0
 def __init__(self, low: Number = 0, high: Number = 1):
     self._low = convert_number(low)
     self._high = convert_number(high)
Пример #10
0
 def __init__(self,
              *args,
              converter: ConverterBallot = None,
              higher_vs_lower: Union[Number, None] = 1,
              lower_vs_higher: Union[Number, None] = 0,
              indifference: Union[Number, None] = Fraction(1, 2),
              ordered_vs_unordered: Union[Number, None] = 1,
              unordered_vs_ordered: Union[Number, None] = 0,
              unordered_vs_unordered: Union[Number, None] = Fraction(1, 2),
              ordered_vs_absent: Union[Number, None] = None,
              absent_vs_ordered: Union[Number, None] = None,
              unordered_vs_absent: Union[Number, None] = None,
              absent_vs_unordered: Union[Number, None] = None,
              absent_vs_absent: Union[Number, None] = None,
              diagonal_score: Number = 0,
              default_score: Number = 0,
              antisymmetric: bool = False,
              **kwargs):
     if converter is None:
         converter = ConverterBallotToOrder()
     self.higher_vs_lower = convert_number(higher_vs_lower)
     self.lower_vs_higher = convert_number(lower_vs_higher)
     self.indifference = convert_number(indifference)
     self.ordered_vs_unordered = convert_number(ordered_vs_unordered)
     self.unordered_vs_ordered = convert_number(unordered_vs_ordered)
     self.unordered_vs_unordered = convert_number(unordered_vs_unordered)
     self.ordered_vs_absent = convert_number(ordered_vs_absent)
     self.absent_vs_ordered = convert_number(absent_vs_ordered)
     self.unordered_vs_absent = convert_number(unordered_vs_absent)
     self.absent_vs_unordered = convert_number(absent_vs_unordered)
     self.absent_vs_absent = convert_number(absent_vs_absent)
     self.diagonal_score = convert_number(diagonal_score)
     self.default_score = convert_number(default_score)
     self.antisymmetric = antisymmetric
     super().__init__(*args, converter=converter, **kwargs)