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

Uploaded Python 3

File details

Details for the file election_forecasting-0.1.dev50.tar.gz.

File metadata

File hashes

Hashes for election_forecasting-0.1.dev50.tar.gz
Algorithm Hash digest
SHA256 e1183044e7dad2edaababcc4ac7b93d037fa45844601982a7408a4377f79463c
MD5 35b1daba9d3813bcbbae77c488d4614f
BLAKE2b-256 eddee1169a45474c90fbfd4379b6879850cbddb16c032e68b406069617197069

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for election_forecasting-0.1.dev50-py3-none-any.whl
Algorithm Hash digest
SHA256 b81b54216754114d4c20850405949023be3f2da2685bb51e9205358075243d85
MD5 a754c12a174ecc06d99fa7a4e219beb6
BLAKE2b-256 48ab8dc2f76d64a1d85ab81f1e9cfc3afece76be906598bdeb4bd05c8f255de0

See more details on using hashes here.

Provenance

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