Pref Grade Profiles¶
Profiles of voters that each submit both a (truncated) strict weak order and an assignment of grades from a fixed set to each candidate. The ballots for a PrefGradeProfile are Rankings and Grades.
PrefGradeProfile Class¶
- class pref_voting.pref_grade_profile.PrefGradeProfile(rankings, grade_maps, grades, rcounts=None, candidates=None, cmap=None, gmap=None, grade_order=None)[source]¶
An anonymous profile in which each voter submits both a (truncated) strict weak order and an assignment of grades.
- Parameters:
rankings (list[dict[int or str: int]] or list[Ranking]) – List of rankings in the profile, where a ranking is either a
Rankingobject or a dictionary.grade_maps (list[dict[int or str: int or str]] or list[Grade]) – List of grades in the profile, where a grade is either a
Gradeobject or a dictionary.grades (list[int or str]) – List of grades.
rcounts (list[int], optional) – List of the number of voters associated with each ranking/grade pair. Should be the same length as rankings and grade_maps. If not provided, it is assumed that 1 voter submitted each element.
candidates (list[int] or list[str], optional) – List of candidates in the profile. If not provided, this is the union of candidates appearing in the rankings and grade maps.
cmap (dict[int or str: str], optional) – Dictionary mapping candidates to candidate names (strings). If not provided, each candidate name is mapped to itself.
gmap (dict[int or str: str], optional) – Dictionary mapping grades to grade names (strings). If not provided, each grade is mapped to itself.
grade_order (list[int or str], optional) – A list of the grades representing the order of the grades. It is assumed the grades are listed from largest to smallest. If not provided, the grades are assumed to be numbers and compared using the greater-than relation.
- Example:
The following code creates a profile in which 2 voters submit the ranking 0 first, 1 second, 2 third along with grades {0: 5, 1: 3, 2: 1}; and 3 voters submit the ranking with 1 and 2 tied for first and 0 second along with grades {0: 2, 1: 4, 2: 4}:
pgprof = PrefGradeProfile( [{0: 1, 1: 2, 2: 3}, {1: 1, 2: 1, 0: 2}], [{0: 5, 1: 3, 2: 1}, {0: 2, 1: 4, 2: 4}], [1, 2, 3, 4, 5], rcounts=[2, 3], ) pgprof.display()
- approval_scores()[source]¶
Return a dictionary representing the approval scores of the candidates in the profile.
- avg(c)[source]¶
Return the average of the grades of
c. Ifcis not assigned a grade by any voter, return None.
- cindex_to_cand¶
Maps candidates to their index in the list of candidates and vice versa.
- cmap¶
The candidate map is a dictionary associating a candidate with the name used when displaying a candidate.
- condorcet_loser(curr_cands=None)[source]¶
Returns the Condorcet loser in the profile restricted to
curr_candsif one exists, otherwise return None.A candidate \(c\) is a Condorcet loser if every other candidate is majority preferred to \(c\).
- condorcet_winner(curr_cands=None)[source]¶
Returns the Condorcet winner in the profile restricted to
curr_candsif one exists, otherwise return None.The Condorcet winner is the candidate that is majority preferred to every other candidate.
- copeland_scores(curr_cands=None, scores=(1, 0, -1))[source]¶
The Copeland scores in the profile restricted to the candidates in
curr_cands.The Copeland score for candidate \(c\) is calculated as follows: \(c\) receives
scores[0]points for every candidate that \(c\) is majority preferred to,scores[1]points for every candidate that is tied with \(c\), andscores[2]points for every candidate that is majority preferred to \(c\). The defaultscoresis(1, 0, -1).- Parameters:
curr_cands (list[int], optional) – restrict attention to candidates in this list. Defaults to all candidates in the profile if not provided.
scores (tuple[int], optional) – the scores used to calculate the Copeland score of a candidate \(c\):
scores[0]is for the candidates that \(c\) is majority preferred to;scores[1]is the number of candidates tied with \(c\); andscores[2]is the number of candidate majority preferred to \(c\). The default value isscores = (1, 0, -1)
- Returns:
a dictionary associating each candidate in
curr_candswith its Copeland score.
- display(cmap=None, style='pretty', curr_cands=None, show_grades=True, show_totals=False)[source]¶
Display the profile as an ascii table (using tabulate).
The rankings are displayed as in
ProfileWithTies. Ifshow_gradesis True, the grade assignment for each voter type is also displayed below the ranking table.- Parameters:
cmap (dict[int,str], optional) – the candidate map (overrides the cmap associated with this profile)
style (str, optional) – the table style for tabulate (default
"pretty")curr_cands (list[int], optional) – list of candidates to display
show_grades (bool, optional) – whether to also display the grade assignments (default True)
show_totals (bool, optional) – whether to display grade totals (sum, median) when showing grades (default False)
- Return type:
None
- display_margin_graph(cmap=None, curr_cands=None)[source]¶
Display the margin graph of the profile (restricted to
curr_cands) using thecmap. SeeMarginGraph.
- display_support_graph(cmap=None, curr_cands=None)[source]¶
Display the support graph of the profile (restricted to
curr_cands) using thecmap. SeeSupportGraph.
- dominates(cand, curr_cands=None)[source]¶
Returns the list of candidates that
candis majority preferred to in the majority graph restricted tocurr_cands.
- dominators(cand, curr_cands=None)[source]¶
Returns the list of candidates that are majority preferred to
candin the profile restricted to the candidates incurr_cands.
- gmap¶
The grade map is a dictionary associating a grade with the name used when displaying a grade.
- property grade_functions¶
Return all of the grade functions in the profile.
- grade_order¶
The order of the grades. If None, then order from largest to smallest
- grades¶
The grades in the profile.
- property grades_counts¶
Returns the grades and the counts of each grade.
- property is_truncated_linear¶
Return True if the profile only contains (truncated) linear orders.
- is_uniquely_weighted()[source]¶
Returns True if the profile is uniquely weighted.
A profile is uniquely weighted when there are no 0 margins and all the margins between any two candidates are unique.
- majority_graph()[source]¶
Returns the majority graph of the profile. See
MajorityGraph.
- margin(c1, c2)[source]¶
Returns the margin of candidate
c1over candidatec2, where the margin is the number of voters that rankc1strictly abovec2minus the number of voters that rankc2strictly abovec1.
- margin_graph()[source]¶
Returns the margin graph of the profile. See
MarginGraph.
- property margin_matrix¶
Returns the margin matrix of the profile, where the entry at row
iand columnjis the margin of candidateiover candidatej.
- max(c)[source]¶
Return the maximum of the grade of
c. Ifcis not assigned a grade by any voter, return None.
- median(c, use_lower=True, use_average=False)[source]¶
Return the median of the grades of
c. Ifcis not assigned a grade by any voter, return None.
- min(c)[source]¶
Return the minimum of the grades of
c. Ifcis not assigned a grade by any voter, return None.
- num_cands¶
The number of candidates in the profile.
- num_ranked_all_candidates()[source]¶
Return the number of rankings that rank all candidates in the profile.
- num_ranking_each_candidate()[source]¶
Return a dictionary mapping each candidate to the number of voters that rank the candidate.
- num_voters¶
The number of voters in the profile.
- plurality_scores(curr_cands=None)[source]¶
Return the Plurality Scores of the candidates, assuming that each voter ranks a single candidate in first place.
- plurality_scores_ignoring_overvotes(curr_cands=None)[source]¶
Return the Plurality scores ignoring empty rankings and overvotes.
- proportion(cand, grade)[source]¶
Return the proportion of voters that assign
candthe gradegrade.Note that
gradecould be None, in which case the proportion of voters that do not assigncanda grade is returned.
- proportion_with_grade(cand, grade)[source]¶
Return the proportion of voters that assign a
gradetocand.
- proportion_with_higher_grade(cand, grade)[source]¶
Return the proportion of voters that assign a strictly higher grade to
candthangrade.
- proportion_with_lower_grade(cand, grade)[source]¶
Return the proportion of voters that assign a strictly lower grade to
candthangrade.
- property ranking_types¶
Return a list of the types of rankings in the profile.
- property rankings¶
Return a list of all individual rankings in the profile.
- property rankings_as_dicts_counts¶
Returns the rankings represented as dictionaries and the counts of each ranking.
- property rankings_as_indifference_list¶
Return a list of all individual rankings as indifference lists in the profile.
- property rankings_counts¶
Returns the rankings and the counts of each ranking.
- ranks¶
The ranks that are possible in the profile.
- remove_candidates(cands_to_ignore)[source]¶
Remove all candidates from
cands_to_ignorefrom the profile.- Parameters:
cands_to_ignore (list[int]) – list of candidates to remove from the profile
- Returns:
a profile with candidates from
cands_to_ignoreremoved.
- strength_matrix(curr_cands=None, strength_function=None)[source]¶
Return the strength matrix of the profile. The strength matrix is a matrix where the entry in row \(i\) and column \(j\) is the number of voters that rank the candidate with index \(i\) over the candidate with index \(j\). If
curr_candsis provided, then the strength matrix is restricted to the candidates incurr_cands. Ifstrength_functionis provided, then the strength matrix is computed using the strength function.
- sum(c)[source]¶
Return the sum of the grades of
c. Ifcis not assigned a grade by any voter, return None.
- support(c1, c2)[source]¶
Returns the support of candidate
c1over candidatec2, where the support is the number of voters that rankc1strictly abovec2.
- support_graph()[source]¶
Returns the support graph of the profile. See
SupportGraph.
- to_ranking_profile()[source]¶
Return a
ProfileWithTiescorresponding to the ranking data in this profile.
- tops_scores(curr_cands=None, score_type='approval')[source]¶
Return the tops scores of the candidates.
- use_extended_strict_preference()[source]¶
Redefine the supports so that extended strict preferences are used. Using extended strict preference may change the margins between candidates.
- use_strict_preference()[source]¶
Redefine the supports so that strict preferences are used. Using strict preference may change the margins between candidates.
- using_extended_strict_preference¶
A flag indicating whether the profile is using extended strict preferences when calculating supports, margins, etc.
- weak_condorcet_winner(curr_cands=None)[source]¶
Returns a list of the weak Condorcet winners in the profile restricted to
curr_cands(which may be empty).A candidate \(c\) is a weak Condorcet winner if there is no other candidate that is majority preferred to \(c\).
Note
While the Condorcet winner is unique if it exists, there may be multiple weak Condorcet winners.