The ‘spam comments’ puzzle: tidy simulation of stochastic processes in R
Want to share your content on Rbloggers? click here if you have a blog, or here if you don't.
Previously in this series:
 The “lost boarding pass” puzzle
 The “deadly board game” puzzle
 The “knight on an infinite chessboard” puzzle
 The “largest stock profit or loss” puzzle
 The “birthday paradox” puzzle
 The “Spelling Bee honeycomb” puzzle
 Feller’s “cointossing” puzzle
I love 538’s Riddler column, and the April 10 puzzle is another interesting one. I’ll quote:
Over the course of three days, suppose the probability of any spammer making a new comment on this week’s Riddler column over a very short time interval is proportional to the length of that time interval. (For those in the know, I’m saying that spammers follow a Poisson process.) On average, the column gets one brandnew comment of spam per day that is not a reply to any previous comments. Each spam comment or reply also gets its own spam reply at an average rate of one per day.
For example, after three days, I might have four comments that were not replies to any previous comments, and each of them might have a few replies (and their replies might have replies, which might have further replies, etc.).
After the three days are up, how many total spam posts (comments plus replies) can I expect to have?
This is a great opportunity for tidy simulation in R, and also for reviewing some of the concepts of stochastic processes (this is known as a Yule process). As we’ll see, it’s even thematically relevant to current headlines, since it involves exponential growth.
Solving a puzzle generally involves a few false starts. So I recorded this screencast showing how I originally approached the problem. It shows not only how to approach the simulation, but how to use those results to come up with an exact answer.
Simulating a Poisson process
The Riddler puzzle describes a Poisson process, which is one of the most important stochastic processes. A Poisson process models the intuitive concept of “an event is equally likely to happen at any moment.” It’s named because the number of events occurring in a time interval of length is distributed according to , for some rate parameter (for this puzzle, the rate is described as one per day, ).
How can we simulate a Poisson process? This is an important connection between distributions. The waiting time for the next event in a Poisson process has an exponential distribution, which can be simulated with rexp()
.
For example, in this case we waited 0.14 days for the first comment, then 2.8 after that for the second one, and so on. On average, we’ll be waiting one day for each new comment, but it could be a lot longer or shorter.
You can take the cumulative sum of these waiting periods to come up with the event times (new comments) in the Poisson process.
Simulating a Yule process
Before the first comment happened, the rate of new comments/replies was 1 per day. But as soon as the first comment happened, the rate increased: the comment could spawn its own replies, so the rate went up to 2 per day. Once there were two comments, the rate goes up to 3 per day, and so on.
This is a particular case of a stochastic process known as a Yule process (which is a special case of a birth process. We could prove a lot of mathematical properties of that process, but let’s focus on simulating it.
The waiting time for the first commentwould be , but the waiting time for the second is , then , and so on. We can use the vectorized rexp()
function to simulate those. The waiting times will, on average, get shorter and shorter as there are more comments that can spawn replies.
In this case, the first 15 events happened before the third day. Notice that in this simulation, we’re not keeping track of which comment received a reply: we’re treating all the comments as interchangeable. This lets our simulation run a lot faster since we just have to generate the waiting times.
All combined, we could perform this simulation in one line:
So in one line with replicate()
, here’s one million simulations. We simulate 300 waiting periods from each, and see how many happen before the first day.
It looks like it’s about 19.1.
Turning this into an exact solution
Why 19.1? Could we get an exact answer that is intuitively satisfying?
One trick to get a foothold is to vary one of our inputs: rather than looking at 3 days, let’s look at the expected comments after time . That’s easier if we expand this into a tidy simulation, using one of my favorite functions crossing()
.
We can confirm that the average number of comments in the first three days is about 19.
But we can also use crossing()
(again) to look at the expected number of cumulative comments as we vary .
(Notice how often “solve the problem for one value” can be turned into “solve the problem for many values” with one use of crossing()
: one of my favorite tricks).
How does the average number of comments increase over time?
At a glance, this looks like an exponential curve. With a little experimentation, and noticing that the curve starts at , we can find that the expected number of comments at time follows . This fits with our simulation: is 19.0855.
Intuitively, it makes sense that on average the growth is exponential. If we’d described the process as “bacteria in a dish, each of which could divide at any moment”, we’d expect exponential growth. The “minus one” is because the original post is generating comments just like all the others do, but doesn’t itself count as a comment.^{1}
Distribution of comments at a given time
It’s worth noting we’re still only describing an average path. There could easily be more, or fewer, spam comments by the third day. Our tidy simulation gives us a way to plot many such paths.
The red line shows the overall average, reaching about 19.1 at 3 days. However, we can see that it can sometimes be much smaller or much larger (even even more than 100).
What is the probability distribution of comments after three days the probability there is one comment, or two, or three? Let’s take a look at the distribution.
Interestingly, at a glance this looks a lot like an exponential curve. Since it’s a discrete distribution (with values 0, 1, 2…), this suggests it’s a geometric distribution: the expected number of “tails” flipped before we see the first “heads”.
We can confirm that by comparing it to the probability mass function, . If it is a geometric distribution, then because we know the expected value is we know the rate parameter (the probability of a success on each heads) is .
This isn’t a mathematical proof, but it’s very compelling. So what we’ve learned overall is:
These are true because the rate of comments is one per day. If the rate of new comments were , you’d replace above with .
I don’t have an immediate intuition for why the distribution is geometric. Though it’s interesting that the parameter for the geometric distribution (the probability of a “success” on the coin flip that would stop the process) is equal to the probability that there are no events in time for a Poisson process.
Conclusion: Yule process
I wasn’t familiar with it when I first tried out the riddle, but this is known as a Yule process. For confirmation of some of the results above you can check out this paper or the Wikipedia entry, among others.
What I love about simulation is how it builds an intuition for these processes from the ground up. These simulated datasets and visualizations are a better “handle” for me for grasp the concepts than mathematical equations would be. After I’ve gotten a feel for the distributions, I can check my answer by looking through the mathematical literature.

If you don’t like the , you could have counted the post as a comment, started everything out at , and then you would find that . This is the more traditional definition of a Yule process. ↩
Rbloggers.com offers daily email updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/datascience job.
Want to share your content on Rbloggers? click here if you have a blog, or here if you don't.