Skip to main content

State-level presidential election forecasting models

Project description

Election Forecasting Models

CI codecov PyPI Python

State-level presidential election forecasting using polling time-series data from the 2016 U.S. presidential election.

Installation

Local Installation

# Install with uv
uv pip install -e .

Docker

# Build the Docker image
docker build -t election-forecasting .

# Run forecasts in container
docker run -v $(pwd)/predictions:/app/predictions \
           -v $(pwd)/metrics:/app/metrics \
           election-forecasting election-forecast --dates 8

# Run with parallel execution (utilize host CPU cores)
docker run -v $(pwd)/predictions:/app/predictions \
           -v $(pwd)/metrics:/app/metrics \
           election-forecasting election-forecast --dates 16 --parallel 4

The Docker setup automatically mounts volumes for predictions/ and metrics/ so results persist on your host machine.

Usage

Quick Start: Run Everything

# Run complete pipeline: forecast, compare, and plot
election-run-all

# With custom number of forecast dates
election-run-all --dates 8

Individual Commands

Run All Models

# Run with default 4 forecast dates
election-forecast

# Run with custom number (n) of forecast dates
election-forecast --dates n

# Run with verbose output
election-forecast -v

# Run with parallel execution (recommended for many dates)
election-forecast --dates 16 --parallel 4

# Set random seed for reproducibility
election-forecast --seed 42

Parallel Execution: Use --parallel N (or -w N) to enable multi-core processing. The workload is parallelized by forecast date, so this is most beneficial when using many dates (e.g., 8+). With 4 workers and 8+ dates, you can see significant speedup on multi-core machines.

Compare Model Performance

election-compare

This generates:

  • model_comparison.csv - Detailed metrics table
  • model_comparison.png - Performance visualization
  • Console output with rankings

Generate State-Level Plots

# Plot key swing states (default)
election-plot

# Plot all states with polling data
election-plot --all

# Plot specific states
election-plot --states FL PA MI WI

Models

1. Hierarchical Bayes (Best Overall)

Advanced Bayesian model combining fundamentals prior with Kalman-filtered polls and systematic bias correction.

File: election_forecasting/models/hierarchical_bayes.py

2. Poll Average

Simple weighted poll-of-polls average with empirical uncertainty estimation.

File: election_forecasting/models/poll_average.py

3. Improved Kalman

Brownian motion with drift using Kalman filter/RTS smoother and stronger regularization.

File: election_forecasting/models/improved_kalman.py

4. Kalman Diffusion

Basic diffusion model with EM algorithm for parameter estimation.

File: election_forecasting/models/kalman_diffusion.py

Data Sources

  • Polls: FiveThirtyEight 2016 state-level polling data (4,209 polls across 50 states)
  • Election Results: MIT Election Lab 1976-2020 presidential election results (we use 2016)

Outputs

All results are saved to:

  • predictions/ - Model predictions in CSV format
  • metrics/ - Evaluation metrics (Brier Score, Log Loss, MAE)
  • plots/ - State-level forecast visualizations (organized by model)

License

MIT

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

election_forecasting-0.1.1.dev4.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

election_forecasting-0.1.1.dev4-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file election_forecasting-0.1.1.dev4.tar.gz.

File metadata

File hashes

Hashes for election_forecasting-0.1.1.dev4.tar.gz
Algorithm Hash digest
SHA256 487ec3351af7960c8a8affd2fd9284a5610932de0c4cf8c91085202eb215fcc3
MD5 68232ea0183ca1f645da0248f1ece5f9
BLAKE2b-256 64ef6be9a2fee3dc253eb12f54f3fa5611b69965d0394d0a89e3911d271fa897

See more details on using hashes here.

Provenance

The following attestation bundles were made for election_forecasting-0.1.1.dev4.tar.gz:

Publisher: publish.yml on cmaloney111/election-forecasting-am215

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file election_forecasting-0.1.1.dev4-py3-none-any.whl.

File metadata

File hashes

Hashes for election_forecasting-0.1.1.dev4-py3-none-any.whl
Algorithm Hash digest
SHA256 0002d000c89b7794d198e16f0aec16063400ecd6eddad79fbf06d1e4c45d2132
MD5 0d06d8ce2171e6886bea8726b167c5ab
BLAKE2b-256 c057fb70f6683a9ee8212faa5977722d97cb30e8f19235b9867165c28ae2a554

See more details on using hashes here.

Provenance

The following attestation bundles were made for election_forecasting-0.1.1.dev4-py3-none-any.whl:

Publisher: publish.yml on cmaloney111/election-forecasting-am215

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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