If I write this:
f <- function(lambda, a_0, n) {
a <- numeric(n)
a[1] <- a_0
for (i in 2:n) {
a[i] <- lambda * a[i-1] * (1 - a[i-1])
}
return(a)
}
f(4, 0.75, 100)
then a list is produced that contains only the number 0.75. This is expected.
However, if I write:
lambda <- 4
n <- 100
a_0_values <- seq(0.05, 0.95, by = 0.05)
df <- data.frame(matrix(ncol = length(a_0_values), nrow = n))
for (i in 1:length(a_0_values)) {
df[, i] <- f(lambda, a_0_values[i], n)
}
df[,15]
then a list is produced that initially contains 0.75, but after a while, it starts changing (see the table I provide at the end).
I tried increasing the length of the generated list by writing up to f(4, 0.75, 1000000)
, and it still worked as expected. It only produced 0.75, as it should. Of course, even if it did change after a certain point, it still doesn't explain why f(4, 0.75, 100)
and df[,15]
produce different results.
Why is that?
df[,15]
[1] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[7] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[13] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[19] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[25] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.499999e-01
[31] 7.500001e-01 7.499998e-01 7.500005e-01 7.499990e-01 7.500019e-01 7.499962e-01
[37] 7.500076e-01 7.499847e-01 7.500305e-01 7.499390e-01 7.501221e-01 7.497558e-01
[43] 7.504881e-01 7.490228e-01 7.519506e-01 7.460836e-01 7.577714e-01 7.342156e-01
[49] 7.805721e-01 6.851171e-01 8.629266e-01 4.731372e-01 9.971136e-01 1.151244e-02
[55] 4.551962e-02 1.737903e-01 5.743490e-01 9.778889e-01 8.648884e-02 3.160341e-01
[61] 8.646262e-01 4.681910e-01 9.959528e-01 1.612345e-02 6.345395e-02 2.377102e-01
[67] 7.248162e-01 7.978306e-01 6.451876e-01 9.156822e-01 3.088332e-01 8.538210e-01
[73] 4.992429e-01 9.999977e-01 9.170494e-06 3.668164e-05 1.467212e-04 5.867986e-04
[79] 2.345817e-03 9.361256e-03 3.709449e-02 1.428740e-01 4.898440e-01 9.995874e-01
[85] 1.649634e-03 6.587650e-03 2.617701e-02 1.019671e-01 3.662792e-01 9.284750e-01
[91] 2.656366e-01 7.802952e-01 6.857385e-01 8.620049e-01 4.758098e-01 9.976593e-01
[97] 9.340721e-03 3.701389e-02 1.425754e-01 4.889907e-01
I think it might be the precision of a_0
value, since you can see that
> a_0_values[15] - 0.75
[1] 1.110223e-16
a_0_values[15]
to f
> f(lambda, a_0_values[15], n)
[1] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[6] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[11] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[16] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[21] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01
[26] 7.500000e-01 7.500000e-01 7.500000e-01 7.500000e-01 7.499999e-01
[31] 7.500001e-01 7.499998e-01 7.500005e-01 7.499990e-01 7.500019e-01
[36] 7.499962e-01 7.500076e-01 7.499847e-01 7.500305e-01 7.499390e-01
[41] 7.501221e-01 7.497558e-01 7.504881e-01 7.490228e-01 7.519506e-01
[46] 7.460836e-01 7.577714e-01 7.342156e-01 7.805721e-01 6.851171e-01
[51] 8.629266e-01 4.731372e-01 9.971136e-01 1.151244e-02 4.551962e-02
[56] 1.737903e-01 5.743490e-01 9.778889e-01 8.648884e-02 3.160341e-01
[61] 8.646262e-01 4.681910e-01 9.959528e-01 1.612345e-02 6.345395e-02
[66] 2.377102e-01 7.248162e-01 7.978306e-01 6.451876e-01 9.156822e-01
[71] 3.088332e-01 8.538210e-01 4.992429e-01 9.999977e-01 9.170494e-06
[76] 3.668164e-05 1.467212e-04 5.867986e-04 2.345817e-03 9.361256e-03
[81] 3.709449e-02 1.428740e-01 4.898440e-01 9.995874e-01 1.649634e-03
[86] 6.587650e-03 2.617701e-02 1.019671e-01 3.662792e-01 9.284750e-01
[91] 2.656366e-01 7.802952e-01 6.857385e-01 8.620049e-01 4.758098e-01
[96] 9.976593e-01 9.340721e-03 3.701389e-02 1.425754e-01 4.889907e-01
2
digits> f(lambda, round(a_0_values[15], 2), n)
[1] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
[16] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
[31] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
[46] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
[61] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
[76] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
[91] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75