State Election Map – fixing the “votes to change the election” calculation

As I mentioned at the end of my “Lucky” book review, the book said that Trump in 2020 was only 43K votes away from winning the election. In my 2020 updates to the State Election Map, I had also this calculation, but came up with a different number. (76K) I already knew there was one problem that I hadn’t addressed, which was breaking the vote down by congressional district for Maine and Nebraska, so decided to bite the bullet and fix it up.

Along the way I discovered there were another issue causing wrong results: the difference between an electoral vote tie and a win can be substantial in some cases, and I was just calculating for a win. (in the case of a tie the vote goes to the House of Representatives, but each state gets a vote so it’s complicated, but in 2020 Trump probably would have one)

Anyway, I used Dave Leip’s Atlas of U.S. Presidential Elections to gather the data by congressional district, and improved the Rust code to do both of those calculations, and made the Javascript code show both calculations when they’re different. And lo and behold, Trump really was only 43K votes away from winning in 2020!

Here’s the commit for these changes. It was a lot more work than I expected, and I’m actually in the middle of another project that I put on pause because I was so irritated these numbers were wrong 🙂

As an aside, the Rust code to calculate this got a bit more complicated and the more that happens the more I sprinkle in .clone() calls just to make things work. Which is fine for correctness but not great for performance. In this case it’s fine, though, the script still runs in ~1 second, and I guess it’s nice to have markers where I can try to optimize things if necessary later!

2 thoughts on “State Election Map – fixing the “votes to change the election” calculation”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s