Skip to main content

Social choice functions and CLI tool

Project description

socho

As in python Social Choice Function. The main export is the class Profile, as in a voting profile. Then there are methods to get the best candidate in a profile, or to get the score of an individual candidate.

A profile is a set of (number of votes, ballot) pairs where a ballot is some ordering of the candidates. For example:

collectedVotes = Profile({(40,(0,1,2)),(28,(1,2,0)),(32,(2,1,0))})

That means 40 people like candidate 0 most, then candidate 1 middle, and hate candidate 2. 28 people like candidate 1 the most and so on.

If your set of candidates is S, then each the ballot should contain each element in S exactly once, in any order. Your candidates can be any hashable objects.

weirdProfile = Profile({(40,(2,1,56.78)),(28,(1,56.78,2)),(32,(56.78,1,2))})

>>> weirdProfile.mayors
set(56.78, 1, 'fish')

And of course you can run elections:

>>> weirdProfile.singleTransferableVote()
set([56.78])

Installation

$ pip3 install social-choice

Getting Started

from socho.profile import Profile, ballot_box, plurality

ballots = [(2,1,56.78), (2,1,56.78), (2,1,56.78), (1,56.78,2), (1,56.78,2), (56.78,1,2), (56.78,1,2)]

# Create a profile
profile = Profile.ballot_box(ballots)
scorer = profile.borda  # function to calculate score for each mayor

# A mayor list ranked by score
# [(2, 10), (0, 7), (1, 4)]
# maps to [(56.78, 10), (1, 7), (2, 4)]
rank = profile.ranking(scorer)

# 56.78 is the winner
# 1 as 2nd place
# 2 as 3rd place

Command Line Usage

usage: socho [-h] -i INPUT_FILEPATH [-p PREDICTIONS_FILEPATH] [-s SEP] -f
               {borda,plurality,simpson,copeland,dowdall,symmetric_borda}
               [-o OUTPUT_FILEPATH] [-c COMPARE_FILEPATH]

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_FILEPATH, --input INPUT_FILEPATH
                        Path to input file.
  -p PREDICTIONS_FILEPATH, --predictions PREDICTIONS_FILEPATH
                        Path to predictions file (required for plurality).
  -s SEP, --sep SEP     File separator, i. e., ',' or ' '...
  -f {borda,plurality,simpson,copeland,dowdall,symmetric_borda}, --function {borda,plurality,simpson,copeland,dowdall,symmetric_borda}
                        Social choice function.
  -o OUTPUT_FILEPATH, --output OUTPUT_FILEPATH
                        Path to output file.
  -c COMPARE_FILEPATH, --compare COMPARE_FILEPATH
                        Path to rank file to be compared.

Example

$ socho -i tests/input.txt -f borda -o tests/output.txt

Test files

Examples of input and output files are in tests/ folder. Please follow the same structure.

Methods

  • Baldwin -> winner
  • Borda -> ranking
  • Condorcet -> set of winners
  • Copeland -> ranking
  • Dowdall -> ranking
  • Kemeny Young -> ranking
  • Nanson -> winner
  • Pareto's check -> boolean
  • Plurality -> ranking
  • Raynaud -> winner
  • Schulze -> ranking
  • Sequential Majority Comparison -> winner
  • Simpson -> ranking
  • Symmetric Borda -> ranking
  • Single Transferable Vote -> set of winners

Note

The Kemeny Young method's complexity is O(n!). So, be careful with your input.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

socho-1.3.3.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

socho-1.3.3-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file socho-1.3.3.tar.gz.

File metadata

  • Download URL: socho-1.3.3.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for socho-1.3.3.tar.gz
Algorithm Hash digest
SHA256 ef4b28bc83db6c4e7e8ec3620edb6ccdf42e2149ac9008b74c9aa1cb1a38e2cc
MD5 4f83e2273fd441d322aa0cb6d2ac97e3
BLAKE2b-256 c561e0906957907763bd9e0d30f7679dd5a4768cacbd402d9062fb44ced44466

See more details on using hashes here.

File details

Details for the file socho-1.3.3-py3-none-any.whl.

File metadata

  • Download URL: socho-1.3.3-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for socho-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 94020107cb67e28bae51ead98e8a06c3331ebb398477fed3dc9c1269622ab947
MD5 90b1ffeea4804bca59e720cf82f63e23
BLAKE2b-256 b595bf69d83ae2bb4f3c9db919bbd7b3ce537d4171644e7126845e45029b74b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page