Skip to main content

A package containing algorithms for sortition - democratic lotteries.

Project description

sortition-algorithms

Release Build status codecov Commit activity License

A package containing algorithms for sortition - democratic lotteries.

About

This library implements algorithms for sortition - the random selection of representative citizen panels (also known as citizens' assemblies, juries, or deliberative panels). Unlike simple random sampling, these algorithms use stratified selection to ensure the chosen panel reflects the demographic composition of the broader population.

What is Sortition?

Sortition creates representative groups by randomly selecting people while respecting demographic quotas. For example, if your population is 52% women and 48% men, sortition ensures your panel maintains similar proportions rather than risking an all-male or all-female selection through pure chance.

Key Features

  • Stratified Random Selection: Respects demographic quotas while maintaining randomness
  • Household Diversity: Optional address checking to ensure geographic and household spread
  • Multiple Algorithms: Choose from maximin, leximin, nash, diversimax, or legacy selection methods
  • Flexible Data Sources: Works with CSV files, Google Sheets, or direct Python data structures
  • Transparency: Detailed reporting of selection process and quota fulfillment

Quick Example

from sortition_algorithms import run_stratification, read_in_features, read_in_people, Settings

# Load your data
features = read_in_features("demographics.csv")  # Age, Gender, Location quotas
people = read_in_people("candidates.csv", Settings(), features)

# Select a representative panel of 100 people
success, selected_panels, messages = run_stratification(
    features, people, number_people_wanted=100, settings=Settings()
)

if success:
    panel = selected_panels[0]  # Set of selected person IDs
    print(f"Selected {len(panel)} people for the panel")

Research Background

The algorithms are described in this paper (open access). Other relevant papers are linked to from the docs

Installing the library

pip install sortition-algorithms

(Or uv add ... or ...)

Optional dependencies

There are two sets of optional dependencies:

# Install the library to use the leximin algorithm
# This requires a commercial/academic license to use
pip install 'sortition-algorithms[gurobi]'

# Install the basic Command Line Interface
pip install 'sortition-algorithms[cli]'

The Command Line Interface

The library includes a CLI for common operations:

# CSV workflow
python -m sortition_algorithms csv \
  --settings config.toml \
  --features-csv demographics.csv \
  --people-csv candidates.csv \
  --selected-csv selected.csv \
  --remaining-csv remaining.csv \
  --number-wanted 100

Documentation

For detailed usage instructions, API reference, and advanced examples:

Starting Development

Prerequisites

The recommended prerequisites are:

Set Up

To install a virtualenv with the required dependencies and set up pre-commit hooks:

just install

Get going

# run all the tests
just test

# run all the tests that aren't slow
just test

# run all the code quality checks
just check

The CI/CD pipeline will be triggered when you open a pull request, merge to main, or when you create a new release.

To enable the code coverage reports, see here.

Releasing a new version

  • Create an API Token on PyPI.
  • Add the API Token to your projects secrets with the name PYPI_TOKEN by visiting this page.
  • Create a new release on Github.
  • Create a new tag in the form *.*.*.

For more details, see here.


Repository initiated with fpgmaas/cookiecutter-uv.

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

sortition_algorithms-0.11.7.tar.gz (236.2 kB view details)

Uploaded Source

Built Distribution

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

sortition_algorithms-0.11.7-py3-none-any.whl (88.9 kB view details)

Uploaded Python 3

File details

Details for the file sortition_algorithms-0.11.7.tar.gz.

File metadata

  • Download URL: sortition_algorithms-0.11.7.tar.gz
  • Upload date:
  • Size: 236.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sortition_algorithms-0.11.7.tar.gz
Algorithm Hash digest
SHA256 53889bc0192fc60f87f35c0b859c18df5dbcf54e3eeae0a0e9c78e88b07b15d6
MD5 e6fe14902ff5e85ae6d2033425ebff3f
BLAKE2b-256 22dcdbd37269eee7269492a043e57b32911d3df6bb4a86feea2e26d0d0702ff1

See more details on using hashes here.

File details

Details for the file sortition_algorithms-0.11.7-py3-none-any.whl.

File metadata

  • Download URL: sortition_algorithms-0.11.7-py3-none-any.whl
  • Upload date:
  • Size: 88.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sortition_algorithms-0.11.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f6f29f8451f8005ffde28d3a8037bf2bfc6316229261c9b718d2add01968729d
MD5 0e5ca92462ee38d7192330f9fd0393e4
BLAKE2b-256 7e983fa766f63a6ee71b1d5c5aff6c3064e46ac06bbb27b1bc3fff52369891c5

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