Search code examples
netlogopoisson

how to create turtles with the numbers following a poisson distribution over time


I'd like to produce turtles for every tick.

But the condition is, the number of them should follow Poisson distribution.

So if the x-axis is the number of tick with certain limitation and the y-axis represents the new turtles created in that tick, the graph should resemble the Poisson distribution.

What I've done so far is

to setup
ca
reset-ticks
end

to go
produce
tick
end

to produce
create-events random-poisson n [ 
set color red
set random-xcor random-ycor
]
end

But I don't think this is right.


Solution

  • What you need is the mass function for the Poisson distribution. It can be calculated for any n and lambda (where lambda is the "mean" of the distribution either by

    e^−λ * λ^n / n!

    or by

    incompleteGammaComplement(n+1, λ) - incompleteGammaComplement(n, λ)

    The latter requires the use of the stats extension, but handles larger values of n. (The n! in the first formula blows up pretty quickly for large n.)

    Here is a model that does what I think you want. It creates a total of 100 events, with the largest number of new events at tick 20. poisson uses the first formula; poisson1 uses the second. You can use them interchangeably in let to-be-created ... line.

    extensions [stats]  ;; you need this only for poisson1
    globals [total-events lambda]
    breed [events event]
    
    to setup
      ca
      set total-events 100
      set lambda 20
      reset-ticks
    end
    
    to go
      produce
      show count events
      tick
    end
    
    to produce
      let to-be-created round (total-events * poisson ticks lambda)
      show to-be-created
      create-events to-be-created [
        set color red
        setxy random-xcor random-ycor
      ]
    end
    
    to-report poisson [n lamda]
      let n-factorial ifelse-value (n = 0) [1] [reduce * n-values n [i -> i + 1]]
      report e ^ (- lamda) * lamda ^ n / n-factorial
    end
    
    to-report poisson1 [n lamda]
      ifelse (n = 0) [
        report stats:incompleteGammaComplement (n + 1) lamda
      ]
      [
        report stats:incompleteGammaComplement (n + 1) lamda 
        - stats:incompleteGammaComplement n lamda
      ]
    end