Is Netlogo implementation of rngs:rnd-negbinomial getting the wrong results? - netlogo

I'd like to use the negative binomial distribution to assign values to turtles, but the outcome seems not to be correct.
In my model I'd like to assign values with the negative binomial distribution to the breeds-own variable time-treatment of the breed children.
I used the following code for this:
extensions [ rngs ]
breed [children child]
children-own [
time-treatment
]
to setup
clear-all
rngs:init
rngs:set-seed 1 500
reset-ticks
end
to go
create-children 100 [
setxy random-xcor random-ycor
set time-treatment rngs:rnd-negbinomial 1 20 0.78
]
tick
end
When showing results and calculating the mean of time-treatment the value will be around 70.
However, according the mathematical formula for the mean, it should be r(1-p)/p = 20(1-0.78)/0.78 = 5.641... How is this possible?

The reverse formula rp/(1-p) gives 70.91 which is very close to the result you got here. I assume that is where the problem lies.
According to wikipedia there are a few alternative formulations of the negative binomial distribution.
The link you provided counts the number of failures before 20 successes happen, whereas it looks like the formula here counts the number of successes before 20 failures happen.

Related

Percentage in netlogo

I want to write in Netlogo that a certain percentage of the agent's population has this attribute. How do I do that in NetLogo?
So far, in a toy model, I do it manually. i.e: ask n-of 740 households [set composition 1] when in fact I want to say: ask 8% of the households to set composition 1.
There are two ways. I will call them ex-ante and ex-post.
Ex-ante
A frequent approach is to let each agent have a certain chance (expressed as the percentage value) of doing something. In this case you will use the random-float command in combination with your percentage value, which is the standard way to make things happen in NetLogo according to a certain probability (see here, or also see just random if you're working with integers). It can be used directly within the create-turtles block of commands:
globals [
the-percentage
]
turtles-own [
my-attribute
]
to setup
clear-all
set the-percentage 0.083 ; In this example we set the percentage to be 8.3%.
create-turtles 500 [
if (random-float 1 < the-percentage) [
set attribute 1
]
]
end
This means that you will not always have the exact number of turtles having that attribute. If you check count turtles with [attribute = 1] on multiple iterations, you will see that the number varies. This approach is good if you want to reproduce the probability of something happening (over a population of agents or over time), which is the case for many uses of NetLogo models.
Ex-post
The ex-post approach follows the logic that you more or less expressed: first you create a number of turtles, later you assign to them the attribute. In this case, you simply need to treat the percentage as in any other mathematical expression: multiply it by the total number of turtles to get the relevant turtles:
globals [
the-percentage
]
turtles-own [
my-attribute
]
to setup
clear-all
set the-percentage 0.083
create-turtles 500
ask n-of (count turtles * the-percentage) turtles [
set attribute 1
]
end
With this approach, you will always have the exact same number of turtles with that attribute. In fact, if you run count turtles with [attribute = 1] on multiple iterations you'll see that the result is always 41 (500 * 0.083 = 41.5, in fact if the number passed to n-of is fractional, it will be rounded down).

Adding multiple turtle values in a list on NETLOGO to then calculate the variance

I am calculating the consumption rate of each turtle in the model. I then want to create a list of each turtle's consumption rate value so I can then calculate the variance of it. I can not figure out how to add those values into my list so that I can calculate the variance, any help? Here is my code so far:
ask turtles
[ set consumption-rate ( [ quality ] of patch-here ) / ( strength-of-competition * count turtles-here )]
show variance [ ]
NetLogo makes this kind of thing very easy. It's just:
show variance [ consumption-rate ] of turtles
The of primitive isn't just for accessing the variables of individual agents: it also works with agentsets, in which case it constructs a list of the results, which is exactly what you need here.

Netlogo: average cluster size

I have an urban growth model that outputs clusters of urban areas and want to measure them. Progress has been made to the point of plotting the frequency distribution of patch size following the code stated in this post.
Now all that is needed is to plot the average cluster size but I'm stuck in the coding. I have the idea of using the mean primitive but don't know how to make the model estimate this mean cluster size using the data that the model already outputs. Here is the current code:
to find-clusters
loop [
;; pick a random patch that isn't in a cluster yet
let seed one-of patches with [cluster = nobody
and pcolor = 8
]
;; if we can't find one, then we're done!
if seed = nobody
[
stop ]
;; otherwise, make the patch the "leader" of a new cluster
;; by assigning itself to its own cluster, then call
;; grow-cluster to find the rest of the cluster
ask seed
[ set cluster self
grow-cluster ]]
display
end
to grow-cluster ;; patch procedure
ask neighbors4 with [(cluster = nobody
and pcolor = 8
) and
(pcolor = [pcolor] of myself)]
[ set cluster [cluster] of myself
grow-cluster ]
end
and the code for estimating frequency:
to calc-frequency
let freq map [[i] -> count patches with [cluster = i]] remove-
duplicates [cluster] of patches
set-current-plot "Frequency distribution of urban patch size"
histogram freq
end
Any help will be appreciated. Thanks.
I think the object named 'freq' in your final block of code is a list of cluster sizes. If it is, then you can simply take mean freq to get the average cluster size. However, you then get into a tangle with accessing the result. If you have a global variable to store that average (called aveCluster in my code), then you can simply include a line set aveCluster mean freq.
However, a cleaner way to do it, would be to change your code block into a reporter. Something like (note that I have assumed the code for calculating sizes is correct):
to-report cluster-frequencies
report map [[i] -> count patches with [cluster = i]] remove-duplicates [cluster] of patches
end
Then you can do your histogram with histogram cluster-frequencies and the average with set aveCluster mean cluster-frequencies. Note that this will be less efficient because the list of sizes is calculated twice - once for the histogram and once for the average. If you have the two requirements close together then you can instead:
...
let freqs cluster-frequencies
histogram freq
set aveCluster mean freq
...
and it will only have to calculate once.

Get the mean age of all turtles on the time of death

I want to get the mean age of all dying turtles. I try to achieve that with the following code snipped:
globals [mean-death-age]
turtles-own [age prob-die?]
to-report mean-age
if (prob-die? = true) [
set mean-death-age mean [age] of turtles
]
report mean-death-age
end
Every turtle has the probability of dying (prob-die?) which should be calculated new with every time step (tick). If prob-die? is set to true mean-death-age should be updated. At the moment I have the problem that
I don't know how to initiate the variable mean-death-age. Maybe, working with an if loop would help
if (ticks >= 1) [
if (prob-die? = true) [
set mean-death-age mean [age] of turtles
]
]
I don't get an update of the already calculated mean-death-age but the variable is overwritten and the mean death age of the turtles of this tick is returned
I have problems accesing the value. When I try to call it with e.g.
print mean-age
I get the error message that mean-age is turtle-only even though I tried to save is as a global variable.
The entire code is available here.
I think you're making this more complicated than it needs to be. The easiest approach would be to just keep a list of ages at which turtles die, and then take the mean of that list:
globals [death-ages]
turtles-own [age]
to setup
clear-all
create-turtles 100 [
setxy random-xcor random-ycor
set age random 100
]
set death-ages [] ; start with an empty list
reset-ticks
end
to go
if not any? turtles [ stop ]
ask turtles [
if random 100 < age [
set death-ages lput age death-ages ; add age to our list of ages of death
die
]
]
print mean death-ages
tick
end
The only downside is that your list of death-ages is going to keep growing as your model runs. If that turns out the be a problem (though it probably won't), you will need to keep track of the current average and the current count of observations and update your average with something like set avg ((n * avg) + age) / (n + 1) set n n + 1.
As for why your current code doesn't work, there would be a lot of unpacking to do to explain it all (I'm sorry I can't now). As a general comment, I would suggest trying to take a step back when you run into difficulties like this and think about the minimum amount of information that you need to solve the problem. In this case, you need to mean age of death. What the simplest way to get that? Keep a list of ages of death and take the mean of that list when needed­.

Update a monitor once per tick instead of continuously. Slow model

I know that monitors are updated several times per second, and that can be helpful when checking the output of a model; however, that is not the case for my model, it just weighing it down.
I am trying to plot data from a monitor. I want the monitor to only update the reporter once per tick, if possible.
My model currently functions but it is bogged down updating multiple times a second. I was hoping someone could help me minimize my model's computational effort by updating once per tick.
sample of current code:
globals [initial-patch0-health patch0-health intial-patch2-health patch2-health]
patches-own [ptype penergy max-penergy alive?]
to setup
clear-all
set patch-health 0
ask-patches [
setup-patches
]
reset-ticks
end
to setup-patches
let temp random 100
if temp <= 50 [
set ptype 2
set max-penergy random-in-range 0 5
set alive? true
]
if temp > 50 and temp <= 75 [
set ptype 0
set max-penergy random 10
set alive? true
]
set penergy max-penergy
set patch2-health (ptype2-health)
set patch0-health (ptype0-health)
end
to go
ask-patches
update-patch-health
tick
end
to patch-health
if ptype = 2[
set patch2-health (ptype2-health)
]
if ptype = 0 [
set patch0-health (ptype0-health)
]
end
to-report ptype2-health
report [penergy] of patches with [ptype = 2]
end
to-report ptype0-health
report [penergy] of patches with [ptype = 0]
end
My monitors and plot read (same for patch2-health):
sum (initial-patch0-health)
and
plot sum (patch0-health)
I use sum in this situation because the reporter delivers a list.
For context, I am doing a simple "sheep-wolf predation" style model but I want to monitor the initial grass health vs grass health over time, with multiple grass types (ptype). I have turtles but did not include that code here. Let me know if you need more code from me.
This code gives me the output I desire just at the cost of speed. I figured only reporting once every tick would save some computing time. Any suggestions for cleaning and speeding it up?
Your code example is not usable to me (undefined variables, patch0-health is not output from a reporter, other errors)- check the MCVE guidelines. With that in mind, I'm having trouble replicating the issue you describe- I ran a few profiler tests with a monitor present and not present, and didn't get any difference in runtime. With this setup:
extensions [ profiler ]
globals [ patch-turtle-sum profiler-results]
to setup
ca
ask n-of ( ( count patches ) / 2 ) patches [
set pcolor red
]
crt 1000 [
setxy random-pxcor random-pycor
]
set profiler-results []
reset-ticks
end
to profile-check
repeat 20 [
profiler:start
repeat 20 [
go
]
set profiler-results lput profiler:inclusive-time "go" profiler-results
profiler:reset
]
print profiler-results
end
to go
ask turtles [
fd 1
]
tick
end
to-report patch-turtle-sum-report
report sum [count turtles-here] of patches with [ pcolor = red ]
end
I ran the profile-check procedure from the interface, once with a monitor that monitors patch-turtle-sum-report present (mean go inclusive time: 678.59 ms) and once without (mean go inclusive time: 678.56 ms)- no detectable difference. However, I'm not sure if profiler accounts for monitor, so maybe that assessment is just not useful in this case. It could also be that the number of patches you're dealing with is quite large (in my test example I was doing 100 X 100 patches) so that the calculations get bogged down.
I wonder if you could get around your issue by using a monitor that just reports a variable that you manually calculate once per tick (instead of a to-report reporter)- eg to follow my example above, monitor a variable that is updated with something like:
set patch-turtle-sum sum [count turtles-here] of patches with [ pcolor = red ]
That way you control when the calculation is done, which may speed up the model if the calculation is what's actually slowing down your model.