Feat/sort pref interval strengths descending#371
Open
graceg571 wants to merge 3 commits into
Open
Conversation
bf8429f to
2227c0f
Compare
peterrrock2
requested changes
Jun 10, 2026
| allow_zero_support (bool): If True, candidates with zero support are allowed. If False, | ||
| all candidates must have strictly positive support. | ||
| sort_strengths_descending (bool): | ||
| If True, the candidates are assigned their support values in descending order. |
Collaborator
There was a problem hiding this comment.
Suggested change
| If True, the candidates are assigned their support values in descending order. | |
| If True, the candidates are assigned their support values in descending order | |
| according to the list passed to candidates. |
| probs = [p + 10e-12 if p == 0 else p for p in probs] | ||
|
|
||
| pref_interval = ( | ||
| {c: s for c, s in zip(candidates, sorted(probs, reverse=True))} |
Collaborator
There was a problem hiding this comment.
Suggested change
| {c: s for c, s in zip(candidates, sorted(probs, reverse=True))} | |
| {c: s for c, s in zip(candidates, sorted(probs, reverse=True), strict=True)} |
| pref_interval = ( | ||
| {c: s for c, s in zip(candidates, sorted(probs, reverse=True))} | ||
| if sort_strengths_descending | ||
| else {c: s for c, s in zip(candidates, probs)} |
Collaborator
There was a problem hiding this comment.
Suggested change
| else {c: s for c, s in zip(candidates, probs)} | |
| else {c: s for c, s in zip(candidates, probs, strict=True)} |
Comment on lines
+95
to
+113
| def test_sort_strengths_descending_from_dirchlet(): | ||
| candidates = ["A", "B", "C"] | ||
| pi = PreferenceInterval.from_dirichlet( | ||
| candidates=candidates, alpha=1, sort_strengths_descending=True | ||
| ) | ||
| sorted_candidates = sorted(pi.interval, key=lambda c: pi.interval[c], reverse=True) | ||
| assert sorted_candidates == candidates | ||
|
|
||
| pi = PreferenceInterval.from_dirichlet( | ||
| candidates=candidates, alpha=1, sort_strengths_descending=True | ||
| ) | ||
| sorted_candidates = sorted(pi.interval, key=lambda c: pi.interval[c], reverse=True) | ||
| assert sorted_candidates == candidates | ||
|
|
||
| pi = PreferenceInterval.from_dirichlet( | ||
| candidates=candidates, alpha=1, sort_strengths_descending=True | ||
| ) | ||
| sorted_candidates = sorted(pi.interval, key=lambda c: pi.interval[c], reverse=True) | ||
| assert sorted_candidates == candidates |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #357
PreferenceInterval.from_dirichlet()currently assigns preference strengths randomly to candidates. A flag has been added calledsort_strengths_descendingthat will assign preference strengths in descending order to thecandidateslist if True.If user specifies
candidates = [A,B,C]as input tofrom_dirichlet, a preference interval of[0.3, 0.6, 0.1]could be randomly sampled. Ifsort_strengths_descending = True,from_dirichletwill return aPreferenceIntervalwithinterval = {A:0.6, B:0.3, C:0.1}. Ifsort_strengths_descending = False, the preference interval would be assigned as is withinterval = {A:0.3, B:0.6, C:0.1}.This allows the user to specify an ordering to the preference_interval where they would like their first candidate listed with the highest preference strength, second candidate with the second preference strength, and so on.
sort_strengths_descendingisFalseby default.Tests
Added one test that generates a preference interval 3x with
sort_strengths_descending = Trueto account for the randomness of the preference interval sampling (i.e. could already be in strengths descending order)