2

Suppose I have X patches, and patches have a patch variable called "patch-quality", with values between 0 and 1. I want to generate a random number between 0 and 1 for that variable following an exponential distribution.

When you set up an exponential distribution in NetLogo, you need a mean:

random-exponential mean

The mean of my exponential distribution is 0.13 (I want most patches to be close to 0 quality).

set patch-quality random-exponential 0.13

The problem is that, sometimes, I got few patches with patch-quality above 1.

Is there a way to generate an exponential distribution in NetLogo that wouldn't generate patches with patch-quality above 1?

JoseNav
  • 55
  • 6
  • It's not recommended to ask multiple questions in the same post. Can you make your post for the first question, clearly explain with code etc, and create another question with his own explaination, and so don't have a full title which is unreadable – Elikill58 Mar 16 '23 at 15:04
  • Sorry for the multiple question. I followed your recommendation! – JoseNav Mar 16 '23 at 15:09
  • Does this answer your question? [Generate random number between 0 and 1 with (negative)exponential distribution](https://stackoverflow.com/questions/20385964/generate-random-number-between-0-and-1-with-negativeexponential-distribution) – pjs Mar 16 '23 at 16:13

1 Answers1

1

A few months back, I was struggling with this exact problem.

An exponential distribution by nature does not have an upper limit so this can't be done. You could however use a truncated exponential distribution that follows the exact shape of an exponential distribution between its upper and lower bound. One of the results of this is that the mean of your truncated exponential distribution is NOT the same as the mean of your exponential distribution on which it is based.

Creating a truncated exponential distribution is quite easy. You assign a random number to each patch depending on the exponential distribution. Then you check whether or not that number is within the bounds you set. If it is not, you take a new number until it is.

  let exp-mean 0.13
  ask patches [
    set baseline-quality random-exponential exp-mean
    while [baseline-quality > 1] [set baseline-quality random-exponential exp-mean
  ]
LeirsW
  • 2,070
  • 4
  • 18
  • 1
    This can be done by direct inversion as per the link in the comments above. That's usually more efficient than acceptance/rejection approaches. – pjs Mar 16 '23 at 17:15
  • 2
    @pjs I appreciate that that is a more efficient approach. The reason I suggested this one (and use it myself) is that it intuitively shows what is going on without needing to completely understand the mathematical background of the distribution. At the same time, efficiency in a setup procedure in Netlogo (where I used mine) is of a lower concern. – LeirsW Mar 16 '23 at 17:37