Skip to main content

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

Releases are published to PyPI via Trusted Publishing — no API token is stored in the repo. To cut a release:

  • Create a new release on Github.
  • Create a new tag in the form *.*.*.

For one-time setup (or if the trusted publisher needs reconfiguring), see docs/pypi-trusted-publisher-setup.md.


Repository initiated with fpgmaas/cookiecutter-uv.

Metadata

Release files for sortition-algorithms 0.12.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sortition-algorithms 0.12.14
File Size Uploaded Provenance Actions
sortition_algorithms-0.12.14.tar.gz 442.9 kB With provenance Details for sortition_algorithms-0.12.14.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sortition-algorithms 0.12.14
File Interpreter ABI Platform Actions
sortition_algorithms-0.12.14-py3-none-any.whl Python 3 none any Details for sortition_algorithms-0.12.14-py3-none-any.whl

Total release size: 543.6 kB

Release files / sortition_algorithms-0.12.14.tar.gz

Download URL sortition_algorithms-0.12.14.tar.gz
Size 442.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4a24a720a8730f52d911697641d954f7e003d798a1e2bf4a9b8ac4f8c4276ae5
BLAKE2b-256 checksum
How to use checksums
485884068e855f6276332ce27be6bce315219212a98af59ef34d720ca015ae82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

Release files / sortition_algorithms-0.12.14-py3-none-any.whl

Download URL sortition_algorithms-0.12.14-py3-none-any.whl
Size 100.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6731de4bd3a5de49627d6b970c4fe6f2f505d106e27d7600cd875359f17a9d6e
BLAKE2b-256 checksum
How to use checksums
946c525ad1767e94f5ed089bb2006cb3e3ad7526c697f17eccb7a3c4983c5e97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

Security for sortition-algorithms 0.12.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sortition_algorithms-0.12.14.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.12.14 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page