Wybory - przeliczanie głosów A Python implementation of common apportionment methods
Go to file
Martin Lackner 25f7a87226 update README 2022-02-17 20:35:46 +01:00
.github/workflows deactivate code coverage 2022-02-16 19:43:46 +01:00
apportionment fix automatic unittests 2022-02-16 17:56:12 +01:00
examples black formatting 2022-02-16 17:41:55 +01:00
tests fix automatic unittests 2022-02-16 17:56:12 +01:00
.gitignore deactivate code coverage 2022-02-16 19:43:46 +01:00
LICENSE Initial commit 2019-06-14 16:01:33 +02:00
README.md update README 2022-02-17 20:35:46 +01:00
pyproject.toml github actions; black formatting 2022-02-16 17:10:21 +01:00
setup.py black formatting 2022-02-16 17:41:55 +01:00

README.md

DOI MIT License PyPi Python versions Build badge Unittests badge

A Python implementation of common apportionment methods

This is a collection of common apportionment methods. Apportionment has two main applications: to assign a fixed number of parliamentary seats to parties (proportionally to their vote count), and to assign representatives in a senate to states (proportionally to their population count). A recommendable overview of apportionment methods can be found in the book "Fair Representation" by Balinski and Young [2].

The following apportionment methods are implemented:

  • the largest remainder method (or Hamilton method)
  • the class of divisor methods including
    • D'Hondt (or Jefferson)
    • Sainte-Laguë (or Webster)
    • Modified Sainte-Laguë (as used e.g. in Norway)
    • Huntington-Hill
    • Adams
  • the quota method [1]

Installation

Using pip:

pip install apportionment

Latest development version from source:

git clone https://github.com/martinlackner/apportionment/
python setup.py install

Requirements:

  • Python 3.7+
  • numpy

A simple example

The following example calculates the seat distribution of Austrian representatives in the European Parliament based on the D'Hondt method and the 2019 election results. Parties that received less than 4% are excluded from obtaining seats and are thus excluded in the calculation.

import apportionment.methods as app
parties = ['OEVP', 'SPOE', 'FPOE', 'GRUENE', 'NEOS']
votes = [1305956, 903151, 650114, 532193, 319024]
seats = 18
app.compute("dhondt", votes, seats, parties, verbose=True)

The output is

D'Hondt (Jefferson) method
  OEVP: 7
  SPOE: 5
  FPOE: 3
  GRUENE: 2
  NEOS: 1

which is indeed the official result.

Another example can be found in examples/simple.py. We verify results from recent Austrian National Council elections in examples/austria.py and from recent elections of the Israeli Knesset in examples/israel.py.

References

[1] Balinski, M. L., & Young, H. P. (1975). The quota method of apportionment. The American Mathematical Monthly, 82(7), 701-730.

[2] Balinski, M. L., & Young, H. P. (1982). Fair Representation: Meeting the Ideal of One Man, One Vote. Yale University Press, 1982. (There is a second edition from 2001 by Brookings Institution Press.)