Here is a comparison between deterministic and stochastic population growth over time. The deterministic model (in blue) follows a consistent growth pattern, while the stochastic model (in red) introduces random variations in the growth rate each year, leading to potential fluctuations in the population size.
The stochastic model’s path can vary with each simulation due to the random component.
# Load necessary library
#set.seed(42) # To make the stochastic results reproducible
# Parameters
initial_population <- 1 # Initial population size
deterministic_growth_rate <- 0.01 # Annual growth rate for deterministic model (5%)
stochastic_growth_rate <- 0.01 # Average growth rate for stochastic model
growth_rate_sd <- 0.1 # Standard deviation of the growth rate for stochastic model
years <- 10 # Number of years to project
# Create vectors to store population sizes over time
deterministic_population <- numeric(years + 1)
stochastic_population <- numeric(years + 1)
# Initialize populations
deterministic_population[1] <- initial_population
stochastic_population[1] <- initial_population
# Deterministic population growth calculation
for (t in 1:years) {
deterministic_population[t + 1] <- deterministic_population[t] * (1 + deterministic_growth_rate)
}
# Stochastic population growth calculation
for (t in 1:years) {
random_growth_rate <- rnorm(1, mean = stochastic_growth_rate, sd = growth_rate_sd)
stochastic_population[t + 1] <- stochastic_population[t] * (1 + random_growth_rate)
}
# Years for the x-axis
year_labels <- 0:years
# Plotting the deterministic and stochastic population growth
plot(year_labels, deterministic_population, type = "o", col = "blue", lty = 1,
ylim = range(c(deterministic_population, stochastic_population)),
xlab = "Year", ylab = "Population Size", main = "Deterministic vs Stochastic Population Growth Over Time")
lines(year_labels, stochastic_population, type = "o", col = "red", lty = 2)
legend("topleft", legend = c("Deterministic", "Stochastic"), col = c("blue", "red"), lty = 1:2)