Friday, May 2, 2014

Color-Coded Direction Fields

My differential equations professor mentioned that for autonomous systems of differential equations, direction fields help us visualize the direction, but not the speed, of solutions. However, he also mentioned that we might be able to encode the speed of solutions using colors – red for fast and blue for slow – so I took it upon myself to do that. The MATLAB code for this is available on my github.

For example, consider the linear Romeo and Juliet system of differential equations described in a paper called The Lighter Side of Differential Equations, where $x$ is Romeo's love for Juliet and $y$ is Juliet's love for Romeo:

\[
\left\{
\begin{array}{l}
x' = -0.2y \\
y' = 0.8x \\
\end{array}
\right.
\]

The solutions seem to be cyclic. Near the origin, the solutions are close to blue because they move relatively slowly, and near the edges, the solutions are close to red because they move more quickly.

For another example, consider a simple nonlinear predator-prey model. Let's say that $x$ is the population of rabbits and $y$ is the population of foxes:

\[
\left\{
\begin{array}{l}
x' = x - xy \\
y' = -y + xy \\
\end{array}
\right.
\]

Here, all of the vectors in the first quadrant are blue because, relative to the vector at $(-2, -2)$, they have very small magnitudes. We can zoom in near the critical point $(1, 1)$ to get a better idea of the relative speeds of solutions near that point:


This looks remarkably similar to the direction field for the Romeo and Juliet system! Shall I compare the love of Romeo and Juliet to foxes eating rabbits on a summer's day?

Tuesday, March 18, 2014

How good am I at Flappy Bird?


Good news – I just played 100 games of Flappy Bird and recorded my scores.

Flappy Bird synopsis: Your job in this game is to navigate a bird through a series of gaps between pipes. If your bird manages to pass through 3 openings but then crashes into a pipe or falls to the ground, the game is over, and your final score is 3. The game is immensely difficult, despite the fact that the gameplay never speeds up and the gaps between the pipes never shrink.

I don't mean to brag, but I've wasted a lot of time on this game, so I'm pretty good. Here's what I've become curious about: Let's say I want to break my high score of 146. If I play one game, what are the odds that I surpass 146? How many games should I expect to play before I beat my high score?

To take a stab at this problem, I'm going to make a not-technically-correct-but-not-totally-unreasonable assumption: I can navigate my bird through each gap with some probability $p$, and whether my bird makes it through one gap has no bearing on whether he1 survives the next gap (i.e., these events are independent). Once I know what $p$ is, I can find the probability that I get a score of, say, 3, by computing $p^{3}(1-p)$ (the probability that my bird survives the first three gaps but not the fourth).2 In general:
\[P(\mbox{I get a score of } k) = p^{k}(1 - p)\]
In order to approximate $p$, I recorded my scores from 100 dreadful games of Flappy Bird. During those 100 games, I guided my bird through 2,551 gaps. That means that I encountered 2,651 gaps, 2,551 of them successfully, which gives me $p \approx 2551/2651 \approx 0.9623$. There's about a 96 percent chance that my bird will survive each gap – assuming, of course, that I didn't get significantly better or worse during those 100 games. Now, we have a simple model:
\[P(\mbox{I get a score of } k) = (0.9623)^{k}(1 - 0.9623)\]
Let's put this model to the test by plotting a histogram of my scores (the blue bars) along with the distribution of scores predicted by our model (the red circles):


I'm actually quite pleased with this – the distribution of my scores doesn't follow the model's predictions perfectly, but I wouldn't expect perfection since I only played 100 games. If I recorded 100 more scores (which isn't going to happen), the plot would probably look even smoother.

Recall the question at hand: How many games do I have to play before I beat my high score of 146? Using my value of $p$, we can predict the probability of achieving a score of at least 147:
\[P(\mbox{I get a score of at least } 147) = (0.9623)^{147} = 0.003509\]
This means that I should expect to exceed 146 points roughly 0.3509 percent3 of the time, or about once every 285 games. Conveniently, this also means that I should expect to play about 285 games before I beat my high score.4 Of course, 285 is just an expected value, and it could take me 2 games or 2,000 games for me to beat my high score.

Notice that even though the game never increases in difficultly (i.e., the hundredth gap is no more difficult than the fiftieth gap), getting a score of 100 is significantly more difficult than getting a score of 50. In fact, for me, getting a score of 100 is only $(0.9623)^{50} = 0.1462$ times as likely as getting a score of 50.

Rhett Allain of Wired Science wrote about this same topic from the perspective of someone who (no offense) isn't good at Flappy Bird. His article links to a blog post by Frank Noschese about gravity in Flappy Bird.


1 Or she.
2 This is a geometric distribution.
3 If you're playing along and you got 0.3521 percent, you're using the rounded value of 0.9623 where I'm using the not-as-rounded value of something like 0.9622783855149. This doesn't change anything significantly, and as you may have noticed, I'm not super focused on precision here.
4 The number of games it will take for me to beat my high score also follows a geometric distribution.