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.0is outdated and is not maintained anymore. Users still using this version are encouraged to migrate to version1.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file biprop-1.0.3.tar.gz.
File metadata
- Download URL: biprop-1.0.3.tar.gz
- Upload date:
- Size: 325.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3918a87369b5a63875ff21a65a5b9e82e9be638ef1c4c8628c62347ebceb6c89
|
|
| MD5 |
96460eaeadbe918cbe346cf5cd1bf999
|
|
| BLAKE2b-256 |
2378b027d412b72bb8eb3c1cef7df87c0871fa7fd185ad95e47a095a01550d37
|
File details
Details for the file biprop-1.0.3-py3-none-any.whl.
File metadata
- Download URL: biprop-1.0.3-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.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcf6d76578b0224aa42062c964111cbfadc66aef484446bab13f8b53d86202fa
|
|
| MD5 |
7e67bdf6ed90c839a70d03f15d7e93e8
|
|
| BLAKE2b-256 |
85aaef5385c3fc65869b80b13719c14b251d544759d50dbfce16271642a1d6ac
|