# More on swing states

(This is a followup to the previous post) Suppose you simply said “if two states tended to vote the same, make them close together in the plot”. Then, write some code to evaluate the energy of each possible configuration and optimize for the best one. This is the result I get:

Like derek’s figure over on the comments section of Robert‘s post, this shows pretty clearly the historical swing in 1964. It also shows, however, a few things that weren’t obvious on derek’s image. For example, a large number of states didn’t really change their voting patterns: they voted D in 1964, but went right back to R essentially ever since. derek’s third image, that splits states geographically, achieves many of the same results, but it’s kind of cool that you can do similarly well just by looking at the patterns themselves. It also allows you to do silly things like optimizing for the worst possible plot:

This is not, however, entirely without point. If we look at the energy histogram of a lot of random orders:

Now, the energy for my best plot was 161, and the energy for the worst plot was 731. This means that a random ordering is probably just about average: not bad at all for zero effort!. (Sorry for the ugly histogram – clearly a place where my zero matplotlib skills show…)

### 6 responses to “More on swing states”

1. What criteria did you use for energy, just the number of vote changes that occurred across adjacent states? And how did you quickly solve this over the 50-factorial combinations of states?

2. carlosscheidegger

The energy is just the total number of crossings, and I’m counting “did not vote” as a particular kind of vote too, which is why DC, HI and AK get pushed off to the corner.

wrt optimization: Right now I’m just using a simple randomized search over the space of permutations. This clearly does not guarantee that the best I got was optimal, and I should maybe have been clearer about that. Sorry. The code is pretty trivial Python, get it here. It took maybe 5 tries of 10 seconds each to compute that best result.

The optimizer works as follows. At every step, we take a random contiguous chunk of states and move it somewhere else, possibly flipping it in the process. If this improves the overall energy, keep the new state. Otherwise, toss it. Pretty dumb, but did the job 🙂

It is possible to do better, if you code the triangle-inequality TSP approximation (it is easy to show that this energy does obey the triangle inequality)

3. Cool idea! Even if your best result is not the global maximum, it looks like it must be pretty close. I probably would have made the empty fields a special case that is equal to any decision – that would move DC and HI somewhere close to the center.

Now the next challenge is to arrange the states so that the pattern looks like your WordPress avatar! 😉

4. carlosscheidegger

I probably would have made the empty fields a special case that is equal to any decision – that would move DC and HI somewhere close to the center.

Indeed it does.

5. Great charts. Just one recommendation. In my opinion, the visual perception of this kind of charts is better when the times series is on the horizontal axis (x) and the other data, th states in this case, on the vertical (y).

Thanks for sharing these charts and thoughts.

6. carlosscheidegger

Álvaro: we’ll agree to disagree – but here’s what a vertical chart would look like.