生成一个范围内的数字并考虑均值 [英] Generate a number within a range and considering a mean val

查看:61
本文介绍了生成一个范围内的数字并考虑均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在考虑平均值的同时生成一个范围内的随机数.

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

但是,如果我使用这种方法,可以以相同的概率创建每个数字,这在这种情况下没有多大意义,因为 65 岁的人比 105 岁的人多.因此,我想包括一个平均值.我找到了 random-normal 但由于我没有标准差而且我的值不是正态分布的,我不能使用 这种方法.

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.

一个例子:我有两种代理类型.代理类型 1 的平均年龄为 79 岁,年龄范围为 67-90 岁.代理类型 2 的平均年龄为 77 岁,年龄范围为 67-92 岁.

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.

如果我如上所述在 NetLogo 中实现代理类型,代理类型 1 的平均年龄为 78 岁,代理类型 2 的平均年龄为 79 岁.原因是每个年龄的代理数量完全相同生成.这最终给了我人工种群的错误结果.

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.

[编者注:此处添加了提问者的评论.]

我想要一个值的分布,其中最小值为最大值,最大值为最小值.然而,分布曲线不一定是负线性的.因此,我需要平均值.我需要这种方法,因为有可能一种代理类型的年龄范围为 65 - 90 岁,平均年龄为 70 岁,而另一种代理类型的年龄范围相同,但平均年龄为 75 岁.所以代理的实际年龄分布看起来会不一样.

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.

推荐答案

这是一个数学问题而不是 NetLogo 问题.您还没有确定您希望分布的样子(许多不同的曲线可以具有相同的最小值、最大值和平均值).如果您不知道自己的曲线是什么样子,则很难在 NetLogo 中对其进行编码.

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.

如果 L 是你的最小值(低值),H 是你的最大值(高值),M 是平均值,那么你需要找到概率 P 从较低的范围中选择,(1-P)为较高的范围.但是您知道下限的总概率必须等于上限的总概率必须等于 0.5,因为您要在均值处切换范围,而均值也必须是组合分布的均值.因此,每个矩形的大小相同.即 P(M-L) = (1-P)(H-M).求解 P 得到你:

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)

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

将其放入一个函数中:

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

您应该考虑的另一件事 - 年龄有多大影响?这是一个设计问题.您只需要包含改变代理行为的内容.如果 70 岁的代理和 80 岁的代理做出相同的决定,那么您真正需要的是年龄在这个范围内,而不是具体值.

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.

这篇关于生成一个范围内的数字并考虑均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆