Increasingly Functional.
by Joshua Miller | on Twitter | on the web | on github

Quick Baseball Series Simulations With Julia

October 11th 2016

Tagged: julia

Listening to game three of the National League Divisional Series between the Washington Nationals and Los Angeles Dodgers today on the way to the airport, I heard a startingly weighty statistic: The team that wins game three of a five-game series wins 77% of series! That's one pivotal game!

After mulling over that for a second, I thought to myself, "Hmm, I wonder if that's significantly different from what you'd expect even with each game being a coin flip. After all, it's not like the winner of any game in a series is only 50% to win the whole best-of-five." And with Gogo internet on the flight in typical fine form, I had plenty of extra time to jump into a quick Julia REPL and find out.

We can simulate a 5-game series between teams 1 and 0 like:

julia> (round(Int, rand(1,5)))
1x5 Array{Int64,2}:
 1  1  1  1  0

In real life they don't play games 4 and 5, but there's less effort making the machine calculate those dead rubber games than real major league playoff games.

Here's a function to see whether the winner of game three won the series:

julia> function gameThreeWonSeries(series)
            winner = sum(series) > 2 ? 1 : 0
            series[3] == winner

So now let's just run that simulation 100,000 times and see what happens.

julia> wonGameThree = Array{Int}(10000)
100000-element Array{Int64,1}:

julia> for i = 1:100000
            wonGameThree[i] = gameThreeWonSeries(round(Int, rand(1,5))) ? 1 : 0

julia> mean(wonGameThree)

So even if every game were a simple coin flip, the team that won game three would win about 69% of series. And of course, the games aren't exactly coin flips — one of the teams is probably a little bit better than the other, skewing that number even higher.

A nice thought experiment realized while flying without internet. What I failed to figure out is how my laptop could chat with a ground-based customer service rep to troubleshoot the broken airborne Wi-Fi.