Skip to main content

Python library for biproportional and other apportionment methods.

Project description

Biprop - A Python Library for Calculating Biproportional Apportionments

[!CAUTION] DO NOT use this repository to calculate the official results of actual elections! The code in this repository was not formally verified and might not always deliver correct results or terminate.

Welcome to the biprop library. This library is meant for election enthusiats and statistics nerds like me who want to ask questions like

How would the parliament of country X look like if they used biproportional apportionment in their last election?

or

How would the apportionment change if party Y and Z ran together as one and if we introduced a quorum that parties have to fulfill?

The biprop library contains all the tools you need to answer these questions and more.

Installation

You can install biprop with pip using

pip install biprop

Basic Usage

To demonstrate the basic usage of the biprop library, we will replicate the calculations from the example from this Wikipedia article. We start with importing the biprop library and defining the votes, region and party names from the example:

>>> import biprop as bp
>>> party_names = ['A', 'B', 'C']
>>> region_names = ['I', 'II', 'III']
>>> votes = [[123,  45, 815],
...          [912, 714, 414],
...          [312, 255, 215]]

Now we can use the votes to define an election and calculate the upper and lower apportionments:

>>> e = bp.Election(votes, party_names=party_names, region_names=region_names, total_seats=20)
>>> e.upper_apportionment(which='parties')
array([ 5, 11,  4])
>>> e.upper_apportionment(which='regions')
array([7, 5, 8])
>>> e.lower_apportionment()
Lower apportionment converged after 2 iterations.
array([[1, 0, 4],
       [4, 4, 3],
       [2, 1, 1]])

We indeed arrive at the same seat distribution as the example in the Wikipedia article. You can find more usage examples and more detailed explanations in the Examples directory of the project's GitHub page.

Examples and Tutorial

You can find practical examples and detailed explanations in the tutorial iPython notebooks in the Examples directory of the project's GitHub page. The tutorials are still being worked on and are not yet complete.

Version 0

[!WARNING] Version 0.1.0 is outdated and is not maintained anymore. Users still using this version are encouraged to migrate to version 1.x.x.

If you still need access to the version 0.1.0, you can find the code for it in the v0 directory. This directory includes the biprop.py module which implements the version 0.1.0. It also contains an example.py module that uses the 2019 and 2023 Swiss National Council Elections to demonstrate how to use biprop.py.

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

biprop-1.0.2.tar.gz (321.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

biprop-1.0.2-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

Details for the file biprop-1.0.2.tar.gz.

File metadata

  • Download URL: biprop-1.0.2.tar.gz
  • Upload date:
  • Size: 321.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for biprop-1.0.2.tar.gz
Algorithm Hash digest
SHA256 3a57d23380fa5c8f4685fa581f3504e8a67d0f308c0dc00686fff094cb57f733
MD5 0c606b163306b2f95e7f1ce8e7f100d2
BLAKE2b-256 6e492209ccd97d4abc3a3198a9d8cc0761f89258fce4d4a291ffb78de2c4bc95

See more details on using hashes here.

File details

Details for the file biprop-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: biprop-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 35.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for biprop-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 999ea30d61b62818a2f1e5276876da28c1ae8e5bc6ae1f7ed7b31b70c288d821
MD5 b744a0024525c1387caf11d52f54a868
BLAKE2b-256 e0a64e6991d217d85713f01c9600df2d376366ad86bf2e30cde11bd3f41fee4b

See more details on using hashes here.

Supported by

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