Skip to main content

State-level presidential election forecasting models

Project description

Election Forecasting Models

CI codecov PyPI

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.dev2.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.dev2-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for election_forecasting-0.1.1.dev2.tar.gz
Algorithm Hash digest
SHA256 c8669e83e63063dafc9f9bf70ed9c81929104b3bde952b8ad24b338502315f88
MD5 462df02f5bfbb37ef3a2110569cabc1e
BLAKE2b-256 0a539ae0deacb6ce6613b5ec05a7580312ce0c8ed33ccf836f2c6185c7047e5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for election_forecasting-0.1.1.dev2.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.dev2-py3-none-any.whl.

File metadata

File hashes

Hashes for election_forecasting-0.1.1.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 053bfa9d450dceeffd235343be19e4c9d0e242fdd14ce92dfe34c4407ab1f3fb
MD5 bd1a6a06b413927852738e2e1cbfb10f
BLAKE2b-256 bd6ed1a6eb81240f946d819833a235548e99ba7d8790f7962b9cca71f5ffce2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for election_forecasting-0.1.1.dev2-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