0

I want to generate a random number within a range while considering a mean value.

I have a solution for generating the range:

turtles-own [age]

to setup
  crt 2 [
   get-age 
  ]     
end

to get-age
  let min-age 65
  let max-age 105
  set age ( min-age + random ( max-age - min-age ) )
end

However, if I use this approach every number can be created with the same probability, which doesn't make much sense in this case as way more people are 65 than 105 years old. Therefore, I want to include a mean value. I found random-normal but as I don't have a standard deviation and my values are not normally distributed, I can't use this approach.

Edit:

An example: I have two agent typologies. Agent typology 1 has the mean age 79 and the age range 67-90. Agent typology 2 has the mean age 77 and the age range 67-92.

If I implement the agent typologies in NetLogo as described above, I get for agent typlogy 1 the mean age 78 and for agent typology 2 the mean age 79. The reason for that is that for every age the exact same number of agents is generated. This gives me in the end the wrong result for my artificial population.


[Editor's note: Comment from asker added here.]

I want a distribution of values with most values for the min value and fewest values for the max value. However, the curve of the distribution is not necessarily negative linear. Therefore, I need the mean value. I need this approach because there is the possibility that one agent typology has the range for age 65 - 90 and the mean age 70 and another agent typology has the same age range but the mean age is 75. So the real age distribution for the agents would look different.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Hannah H.
  • 266
  • 3
  • 14
  • Does this answer your question? [Generate a random number with max, min and mean(average) in Java](https://stackoverflow.com/questions/5314323/generate-a-random-number-with-max-min-and-meanaverage-in-java) – Peter O. Jul 29 '20 at 17:56
  • 1
    what do you mean by including a mean value? What do you actually want the distribution to look like? – JenB Jul 29 '20 at 17:57
  • 1
    So it appears you want to sample from a continuous distribution (1) in the interval [`min`, `max`], (2) that has a monotonically decreasing curve from [`min`, `max`], and (3) that has a mean at `mean`. The only way I know for you to get that distribution is to optimize for these parameters, similar to [this question](https://stackoverflow.com/questions/61433438), and that sounds non-trivial to do in NetLogo. – Peter O. Jul 29 '20 at 20:32
  • @PeterO. seems about right for me, what you describe. However, I need it in NetLogo. Otherwise I would have reschedule my entire model... – Hannah H. Jul 29 '20 at 20:46

1 Answers1

2

This is a maths problem rather than a NetLogo problem. You haven't worked out what you want your distribution to look like (lots of different curves can have the same min, max and mean). If you don't know what your curve looks like, it's pretty hard to code it in NetLogo.

However, let's take the simplest curve. This is two uniform distributions, one from the min to the mean and the other from the mean to the max. While it's not decreasing along the length, it will give you the min, max and mean that you want and the higher values will have lower probability as long as the mean is less than the midway point from min to max (as it is if your target is decreasing). The only question is what is the probability to select from each of the two uniform distributions.

If L is your min (low value), H is your max (high value) and M for mean, then you need to find the probability P to select from the lower range, with (1-P) for the upper range. But you know that the total probability of the lower range must equal the total probability of the upper range must equal 0.5 because you want to switch ranges at the mean and the mean must also be the mean of the combined distribution. Therefore, each rectangle is the same size. That is P(M-L) = (1-P)(H-M). Solving for P gets you:

P = (H-M) / (H - L)

Put it into a function:

to-report random-tworange [#min #max #mean]
  let prob (#max - #mean) / (#max - #min)
  ifelse random-float 1 < prob
  [ report #min + random-float (#mean - #min) ]
  [ report #mean + random-float (#max - #mean) ]
end

To test this, try different values in the following code:

to testme
  let testvals []
  let low 77
  let high 85
  let target 80
  repeat 10000 [set testvals lput (random-tworange low high target) testvals]
  print mean testvals
end

One other thing you should think about - how much does age matter? This is a design question. You only need to include things that change an agent's behaviour. If agents with age 70 make the same decisions as those with age 80, then all you really need is that the age is in this range and not the specific value.

JenB
  • 17,620
  • 2
  • 17
  • 45
  • Thank you. I am not at home in the moment, therefore I can't test your approach now. But I will test it on the weekend. – Hannah H. Aug 05 '20 at 08:37
  • To your question: age matters a lot as it is one of the major drivers of the behavior of the agents. Furthermore, I implement the mortality rate which is influenced by age and gender. If the age is off, then mortality of the agents doesn't work properly – Hannah H. Aug 05 '20 at 08:38
  • 1
    In that case, why not just use the real age distribution and do a weighted draw from that? – JenB Aug 05 '20 at 08:59
  • What do you mean by that? Take the percentage from every age group as weight? – Hannah H. Aug 06 '20 at 09:54
  • 1
    Yes, that's correct. Let's say you only care about child (0-18), adult (19-64), older adult (65+) and you knew that 15% of the real-world population is child, 60% is adult and 25% is older adult [note, I made this up as a simple example]. Then you give a 15% probability to assign the age of your turtle as 'child', 60% as 'adult' and 25% as 'older'. If you want to do that but don't know how, make an attempt and then post a new question if you get stuck. – JenB Aug 06 '20 at 13:25