Skip to main content

socialchoice: a library for Social Choice Theory

This library implements social choice mechanisms.

Install

socialchoice is on PyPI, so:

pip3 install socialchoice

A minimal example

Here, we have two pairwise votes submitted, where "a" beat "b", and "c" lost to "b". We want to compute the ranked pairs result for this vote set.

>>> from socialchoice import PairwiseBallotBox, Election   
>>> pairwise_ballots = PairwiseBallotBox([("a", "b", "win"), ("c", "b", "loss")])
>>> election = Election(pairwise_ballots)
>>> election.ranking_by_ranked_pairs()
['a', 'b', 'c']

A slightly less minimal example

Let's run a ranked choice election, using ranked pairs again.

>>> from socialchoice import RankedChoiceBallotBox, Election   
>>> ranked_ballots = RankedChoiceBallotBox([[1,2,3,4], [1, {2,3}, 4]])
>>> election = Election(ranked_ballots)
>>> election.ranking_by_ranked_pairs()
[1, 2, 3, 4]

Notice that ties are allowed - simply submit a set as one element of your ranking, and each of those will be counted as ties. Ranked choice ballots are converted into pairwise preferences, and then ranked pairs can be run on the result.

Converting pairwise votes to orderings

Suppose you've collected pairwise votes from several voters, and you want to run a position-based method like Borda count. This is easy if each set of pairwise votes is transitive and complete - that is, the voters have consistent preferences (if A is preferred to B, and B is preferred to C, the voter doesn't also prefer C to A) and they have a preference submitted for each candidate pairing. Then, you can topologically sort the graph of their votes, and the resulting sorted list of candidates is their ordering.

But what if the vote set is intransitive or incomplete? If intransitive, you will have cycles, meaning there will be no clear ordering. If incomplete, the output will not contain each candidate. Both make it impossible to run position count directly. However, there are ways of making a vote set transitive, or filling in blanks in an ordering.

Here is an example of removing an edge from a cycle.

>>> from socialchoice import *
>>> ballots = VoterTrackingPairwiseBallotBox([(1, 2, "win", 1), (2, 3, "win", 1), (3, 1, "win", 1)])
>>> break_random_link = IntransitivityResolverFactory(ballots).make_break_random_link()
>>> add_random_edges = IncompletenessResolverFactory(ballots).make_add_random_edges()

>>> ballots.enable_ordering_based_methods(break_random_link, add_random_edges)
>>> Election(ballots).ranking_by_borda_count() in [[1,2,3], [2,3,1], [3,1,2]]
True

As the names imply, this resolves intransitivity by breaking random links, and then ensures that the output is complete by adding random edges. This example resolves an intransitive set of votes (1 beats 2, 2 beats 3, 3 beats 1) to one of the three orderings listed on the last line.

Metadata

Release files for socialchoice 0.0.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for socialchoice 0.0.12
File Size Uploaded
socialchoice-0.0.12.tar.gz 14.1 kB Details

Release files / socialchoice-0.0.12.tar.gz

Download URL socialchoice-0.0.12.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
92fc0458920fea1e2b9969416512f2e3028c01e6b39d8f773b38fac41664ddd3
BLAKE2b-256 checksum
How to use checksums
c9c3b6540461571213a8562bbabce25281b7402fce6c0aaf36850e4d260211bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.0.12 This release

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

2 release files

0.0.1

1 release file

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page