State-level presidential election forecasting models
Project description
Election Forecasting Models
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 tablemodel_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 formatmetrics/- Evaluation metrics (Brier Score, Log Loss, MAE)plots/- State-level forecast visualizations (organized by model)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file election_forecasting-0.1.dev50.tar.gz.
File metadata
- Download URL: election_forecasting-0.1.dev50.tar.gz
- Upload date:
- Size: 1.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1183044e7dad2edaababcc4ac7b93d037fa45844601982a7408a4377f79463c
|
|
| MD5 |
35b1daba9d3813bcbbae77c488d4614f
|
|
| BLAKE2b-256 |
eddee1169a45474c90fbfd4379b6879850cbddb16c032e68b406069617197069
|
Provenance
The following attestation bundles were made for election_forecasting-0.1.dev50.tar.gz:
Publisher:
publish.yml on cmaloney111/election-forecasting-am215
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
election_forecasting-0.1.dev50.tar.gz -
Subject digest:
e1183044e7dad2edaababcc4ac7b93d037fa45844601982a7408a4377f79463c - Sigstore transparency entry: 735743039
- Sigstore integration time:
-
Permalink:
cmaloney111/election-forecasting-am215@7b03db1ebd193642c0b02b31fee745ecf6dab568 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cmaloney111
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7b03db1ebd193642c0b02b31fee745ecf6dab568 -
Trigger Event:
push
-
Statement type:
File details
Details for the file election_forecasting-0.1.dev50-py3-none-any.whl.
File metadata
- Download URL: election_forecasting-0.1.dev50-py3-none-any.whl
- Upload date:
- Size: 27.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b81b54216754114d4c20850405949023be3f2da2685bb51e9205358075243d85
|
|
| MD5 |
a754c12a174ecc06d99fa7a4e219beb6
|
|
| BLAKE2b-256 |
48ab8dc2f76d64a1d85ab81f1e9cfc3afece76be906598bdeb4bd05c8f255de0
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
election_forecasting-0.1.dev50-py3-none-any.whl -
Subject digest:
b81b54216754114d4c20850405949023be3f2da2685bb51e9205358075243d85 - Sigstore transparency entry: 735743040
- Sigstore integration time:
-
Permalink:
cmaloney111/election-forecasting-am215@7b03db1ebd193642c0b02b31fee745ecf6dab568 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cmaloney111
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7b03db1ebd193642c0b02b31fee745ecf6dab568 -
Trigger Event:
push
-
Statement type: