简体   繁体   English

如何在Ruby中使用分布宝石创建指数分布?

[英]How do I create an exponential distribution in Ruby with the distribution gem?

I need the mean to be 2, with starting at 1 up to 10,000 random numbers. 我需要的平均值是2,从1开始最多10,000个随机数。

require 'distribution'

mean = 2

# generate a rng with exponential distribution
rng = Distribution::Exponential.rng(mean)

Well, I'm not much of a statistician, so I'm a little lost in the code here. 好吧,我不是一个统计学家,所以我对这里的代码有些迷惑。

Can you tell me, then, is the first parameter to rng (it is named l ) supposed to stand for "limit"? 那么,您能告诉我, rng的第一个参数(名为l )应该代表“极限”吗?

If so, I don't really know, I'm getting the same sort of results that you must be getting, but again, I'm not a statistician or even a mathematician, I just like to code, so I am probably not of too much help. 如果是这样,我真的不知道,我得到的结果必须与您必须获得的结果相同,但是同样,我不是统计学家,甚至不是数学家,我只是喜欢编码,所以我可能不是太多帮助。

EDIT: So, again, I dont really know what is going on or is supposed to be going on here. 编辑:所以,再次,我真的不知道这是怎么回事或应该发生的事。 Here is what gives me a relatively close mean of 2 (I got this just by messing around with the l value): 这就是给我一个相对接近的平均值2的原因(我只是弄乱了l值而得到了它):

require 'distribution'
rng=Distribution::Exponential.rng(0.5)
sum=0
1000.times { sum += rng.call }
sum /= 1000
puts sum

Seems to give a value generally between 1.9 and 2.1. 似乎给出的值通常在1.9和2.1之间。 Hope this helps :) 希望这可以帮助 :)

PRINT EACH VALUE: 打印每个值:

require 'distribution'
rng = Distribution::Exponential.rng(0.5)
values = [] # we'll store each value in an array

# calculate the results
how_many = 1000 # let's store 1000 in a variable since we'll reuse the value
how_many.times { values << rng.call } # rng is now a Proc, so we use call to get our value

# print out the results
values.each_with_index { |v,i| puts "#{i+1}: #{v}" } # print each value one by one
puts
puts "Total sum: #{values.inject(&:+)}"
puts "Mean: #{values.inject(&:+)/how_many}"

Again, not sure if this is exactly "right" or what you're looking for, but it definitely seems to approach 2. Try a bigger number than 1000, like 100000. 再次,不确定这是“正确”的还是您要寻找的东西,但是绝对可以接近2。尝试大于1000的数字,例如100000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM