Interpretations of probabilities and percentages - netlogo

Thank you very much for your help for coding my model,
If you do not mind please, I want to ask you about some interpretations in the coding
I am sorry I am not expert in mathematics
to move
ask turtles with [gender = "male" ]
[ if ( random-float 1) <= 0.025]
why it is <= and what is the interpretation of this code,
and for the percentage
ask turtles
[ if random 100 <= 50
[become-fat]]
the same question why <= if we always say 50 % in the group will be fat why we put this sign???
and what is the different between random and random float
sorry for disturbance

The difference between the two primitives is that:
random gives you only integer numbers, e.g.: 0, 1, 2, 3, etc.
random-float gives you floating point numbers, e.g.: 0.0, 0.125, 0.528476587245, 3.66, etc.
Both can be used to make things happen probabilistically in NetLogo. I'll start with the use of random, which is slightly easier to understand.
Using random
As stated in the documentation, if you pass a positive number to random, it will give you a number that is greater or equal to 0, but strictly less than that number.
For example, random 2 will always give you either 0 or 1. You could use that to simulate the flipping of a coin:
ifelse random 2 = 0 [ print "heads" ] [ print "tail" ].
That will print "heads" 50% of the time (when random 2 gives you 0), and "tail" 50% of the time (when random 2 gives you 1).
Now it's easy to generalize this to probabilities expressed in percentages by using random 100 instead of random 2. I'll use an example with 50%, but it could very well be 25%, 80% or even 1% or 100%.
Now since random 100 gives you a number between 0 and 99 inclusively, it means that the first 50 numbers it can give you are: 0, 1, 2, 3... all the way to 49. And the next 50 are: 50, 51, 52, 53... all the way to 99. You can imagine a 100-sided dice labeled from 0 to 99 if you wish.
If you want your turtles to "become fat" 50% of the time, you can do:
ask turtles [ if random 100 < 50 [become-fat] ]
Notice that I used the < (strictly less) sign instead of the <= (less or equal) sign. This is because I only want the turtles to become fat if the "dice" lands on one of the first 50 faces, from 0 to 49.
(If you used random 100 <= 50, like in the code you posted above, they would actually have a 51% probability of becoming fat and a 49% probability to not become fat. You should now also be able to figure out why something like if random 100 = 50 doesn't make sense: it would only be true if the "dice" lands exactly on 50, which happens only 1% of the time.)
If you wanted your turtles to become fat only 20% of the time, you'd want to use the first 20 faces of the dice, from 0 to 19:
ask turtles [ if random 100 < 20 [become-fat] ]
It is often enough to use random 100 when dealing with probabilities in NetLogo.
Using random-float
Sometimes, however, you need a little more precision. And mathematically oriented work often expresses probabilities as numbers between 0.0 (for 0%) and 1.0 (for 100%). In those cases, random-float 1 comes handy. Again, as stated in the documentation, random-float will give you a number between 0 (inclusively) and the number you pass to it (exclusively). Thus, random-float 1 gives you a number between 0.0 and 1.0 (but never exactly 1.0).
This expression:
random-float 1 < 0.025
will be true 2.5% of the time.
The dice metaphor doesn't work for random-float, but you can imagine a roulette wheel (or a wheel of fortune). Asking if random-float 1 < 0.025 is like painting a "pie slice" that's 2.5% of the circumference of the wheel, spinning the wheel, and checking if the ball (or the arrow, or whatever) falls in that slice.
Now does it matter if you use <= instead of < with random-float? Not a whole lot. It would only make a difference if the wheel falls exactly on the line that separates your pie slice from the rest of the wheel, and the probability of that happening is very very small.

Related

Netlogo: create a stopping rule considering multiple conditions

I am modelling a network of agents who interact with each other. Each agent is randomly connected to 8 others in the network. Each agent has an initial float-value between 0 and 1. If two agents' values are close enough (determined by the threshold "x" e.g. x = 0.4, v1 = 0.1, v2 = 0.3, so distance of 0.2 < x), one agent influences the other so that the values go even closer together.
However, if the values are greater than the other threshold "y", e.g. y = 0.8, one agent influences the other contrarily, so that the values drift further away from each other. If the difference is between x and y, no influence is taken place.
Now I wanted to create a stopping rule for my network, where an equilibrium is found.
Thank you very much!!!
I started with: if the total number of links (connections between agents) equals to the sum of 1.) number of links where the value difference is below 0.001 (so the two agents have almost the same value) and 2.) links with difference > 0.9, the iterations shall stop. But it is quasi never the case. Is there a possibility where it stops not too late and where it works for whatever values x and y have?
here is my initial code:
if count links = (count links with [value_difference < 0.001] + count links with [value_difference > 0.9])
[print number-of-clusters-conti
stop]

netlogo: Minimum patch coordinate unit and monitoring availability?

I would like to change the minimum coordinate unit of the patch space, which is set to exist only one turtle per patch space, to less than one, and monitor it. We want to set the minimum spatial unit to the same tick size n as in tick-advance (e.g. n=0.1). Here is a piece of code, but it doesn't work. Can anyone help me? Thanks in advance.
globals [n] ; n<=1 i.e. n=0.1
; omitted
ask turtles [forward n]
tick-advance n
I find myself in the same boat as Matteo- I don't know that what you're looking to do is possible / makes sense in the context of NetLogo. The coordinates of patches are fixed (ie, one patch is one unit) but arbitrary (1 in Netlogo can mean 1 m or 1 km, depending on the model). In other words, a patch's coordinates are discrete, while turtles can move around in continuous space. So you can, of course, have a turtle wander around in step sizes of 1/10:
globals [n]
to setup
ca
set n 0.1
crt 10
reset-ticks
end
to go
ask turtles [
forward n
]
tick-advance n
end
After one run of go above, you could conceivably have a turtle at coordinates [xcor = 0.1, ycor = 0.1], although it would still be on patch 0 0 since pxcor values are integers.
It seems that what you are actually needing to do is not coming across as needed- can you edit your question to provide a little more detail / context? Perhaps knowing the why behind your need to model in this way will help askers get you pointed in the right direction. I personally am curious about:
Why you are using tick-advance instead of just tick
How you have implemented your one-turtle-per-patch restrictions- in other words, can you show a Minimal Reproducible Example? That may prompt other ways to approach what you're after.
Here is an example world with time tied to ticks:
globals [ seconds ]
to setup
ca
set seconds 0
resize-world 0 50 0 50
ask patches with [
floor (pxcor / 10) mod 2 + floor (pycor / 10) mod 2 = 1
] [
set pcolor white
]
crt 10
reset-ticks
end
to go
ask turtles [
fd 1
]
set seconds precision (seconds + 0.1) 2
if seconds mod 1 = 0 [print ( word "It has been " seconds " seconds.")]
tick
end

NetLogo: Neet to set the variable's value to a random number within a range

Dear NetLogo Community,
I am aiming to set a variable's value to a number between -1 and 1. I have tried the following code but in vain.
to xyz
[
set probability-of-wom compute-wom [-1 1]
if probability-wom > 0 [...]
]
end
to-report compute-wom -1 1
report -1 + random-float (1 - -1)
end
probablity-wom is a global variable in this case.
Appreciate your support in advance.
Thank you.
Regards,
Shreesha
Shreesha
Let's say you want to generate a random number between lower and upper. Then your compute-wom would be
to-report compute-wom [lower upper]
report upper - random-float (upper - lower)
end
In your case, you would
set probability-of-wom compute-wom -1 1
But a couple of comments. First, what you are generating here is a random number between two limits (as your title suggests), so calling it a probability could be misleading to anyone reading your code. Probabilities will normally be in the range from zero to one. If you really are just looking to do something with a 50% probability, you can simply say
if random-float 1 >= 0.5 [...]
Second, reporters should generally take variable arguments if they are to have arguments at all. Note that since you hard code -1 and 1 in the body of your to-report compute-wom reporter, passing them as arguments is redundant, and possibly misleading to anyone reading your code.

How to test if mortality rate is correct

This question is related to a question I asked some time ago.
I am trying to create a population in NetLogo. Therefore, I am trying to start with the mortality rate. However, I am not sure how to test if my code works. The age of the oldest dying turtles seems to be fine, however when I print the mean death-ages it is way too high and I don't know how to solve that.
The model is available here.
The problem might be the calculation of the probability of dying which I did as follows:
if (gender = "female") [
let die-val mortality-female age ; mortality prob included for the age of the turtle
if (die-val >= random-float 1)[
set prob-die? true
]
]
It looks like your code is working fine, but your implementation may be unrealistic.Your turtles all eventually die, and because your mortality likelihoods increase as age increases, your older turtles contribute far more to the death-ages list than the younger turtles. For example, after running go until all turtles were 90, there were still 52 turtles alive- so all of those (over half) would contribute their old age to the death-ages list. So, your death-ages list is strongly left-skewed, something like:
Where your average death-age over 100 replicates is around 90.5 years. I think this is due to your aging process- try only having your turtle age increase by 1 instead of 5- see if that gives output more like you're expecting. It immediately changes the distribution to look more normal, where your mean death-age for 100 reps is around 81.2 years:
Edit:
You can quickly set up a histogram using a plot widget with settings like this:
Note that the code
if length death-ages > 0 [
set-plot-x-range ( min death-ages - 5 ) ( max death-ages + 5)
]
is included because auto-scaling does not work for the x-scale of histograms. You can exclude that code in the "Plot update commands" field and manually set your x min and max if you'd like, but you'll miss any turtles that die after whatever x max value that you choose.
You also need to change your pen settings from line to bar- click the pencil icon at the right of the pen field to open the menu below and select "Bar" from the "Mode" drop-down menu. Note as well that I've set my "Interval" to 5 to get the plots as shown above.

explanation of roulette wheel selection example

I have a lisp program on roulette wheel selection,I am trying to understand the theory behind it but I cannot understand anything.
How to calculate the fitness of the selected strng?
For example,if I have a string 01101,how did they get the fitness value as 169?
Is it that the binary coding of 01101 evaluates to 13,so i square the value and get the answer as 169?
That sounds lame but somehow I am getting the right answers by doing that.
The fitness function you have is therefore F=X^2.
The roulette wheel calculates the proportion (according to its fitness) of the whole that that individual (string) takes, this is then used to randomly select a set of strings for the next generation.
Suggest you read this a few times.
The "fitness function" for a given problem is chosen (often) arbitrarily keeping in mind that as the "fitness" metric rises, the solution should approach optimality. For example for a problem in which the objective is to minimize a positive value, the natural choice for F(x) would be 1/x.
For the problem at hand, it seems that the fitness function has been given as F(x) = val(x)*val(x) though one cannot be certain from just a single value pair of (x,F(x)).
Roulette-wheel selection is just a commonly employed method of fitness-based pseudo-random selection. This is easy to understand if you've ever played roulette or watched 'Wheel of Fortune'.
Let us consider the simplest case, where F(x) = val(x),
Suppose we have four values, 1,2,3 and 4.
This implies that these "individuals" have fitnesses 1,2,3 and 4 respectively. Now the probability of selection of an individual 'x1' is calculated as F(x1)/(sum of all F(x)). That is to say here, since the sum of the fitnesses would be 10, the probabilities of selection would be, respectively, 0.1,0.2,0.3 and 0.4.
Now if we consider these probabilities from a cumulative perspective the values of x would be mapped to the following ranges of "probability:
1 ---> (0.0, 0.1]
2 ---> (0.1, (0.1 + 0.2)] ---> (0.1, 0.3]
3 ---> (0.3, (0.1 + 0.2 + 0.3)] ---> (0.3, 0.6]
4 ---> (0.6, (0.1 + 0.2 + 0.3 + 0.4)] ---> (0.6, 1.0]
That is, an instance of a uniformly distributed random variable generated, say R lying in the normalised interval, (0, 1], is four times as likely to be in the interval corresponding to 4 as to that corresponding to 1.
To put it another way, suppose you were to spin a roulette-wheel-type structure with each x assigned a sector with the areas of the sectors being in proportion to their respective values of F(x), then the probability that the indicator will stop in any given sector is directly propotional to the value of F(x) for that x.