This is a nice and neat simulation that I can demonstrate with R code. I would start off by defining the initial values:
population <- 100
consumption_rate <- 5
Next, we could define a few functions to vary the population parameters, as specified in the 3 steps:
## Take a uniform value in 0.01-0.02 as a percentage of our current population
death_rate <- function(population) {
prop <- runif(1, 1, 2) / 100
round(population * prop)
}
## Generate a random number births in range 20-40
birth_rate <- function() {
round(runif(1, 20, 40))
}
## Generate an 0-2.5% increase in consumption rate
consumption_increase <- function(consumption_rate) {
increase <- runif(1, 0, 2.5) / 100
consumption_rate * (1 + increase)
}
The final step would be to run a number of simulations where we chain these simple functions together. The function replicate is very useful for situations like this.
simulations <- replicate(10000, {
total_consumption <- 0
for (i in 1:5) {
population <- population - death_rate(population)
population <- population + birth_rate()
consumption_rate <- consumption_increase(consumption_rate)
consumption_this_month <- population * consumption_rate
total_consumption <- total_consumption + consumption_this_month
}
total_consumption
}
)
We now have total cumulative consumption measures from each of these 5 month simulations, and can easily visualise and calculate quantiles:
hist(simulations, breaks = "FD")
q <- quantile(simulations, c(0.1, 0.5, 0.9))
abline(v = q, lty = "dashed")
You'll no doubt notice that the results of the product of all of these uniformly distributed processes is actually (approximately) a normal distribution.
Note: the order in which actions happen (births, deaths, increase in consumption) in the simulation loop could have a strong effect on the outcome. This is something you would have to think about, I've just picked an arbitrary order.