在 Ruby 中生成符合概率分布的数字数组?

发布于 2024-10-09 16:09:35 字数 966 浏览 8 评论 0 原文

假设我有 100 条记录,我想模拟 created_at 日期,以便它符合某些曲线。有没有一个库可以做到这一点,或者我可以使用什么公式?我认为这是沿着相同的轨道:

生成具有概率分布的随机数

我不太了解它们在数学中如何分类,但我正在研究以下内容:

  • 钟形曲线
  • 对数(典型的生物学/进化)曲线? ...

只是在代码中查找一些公式,这样我就可以这样说:

  • 给定 100 条记录,时间跨度为 1.week,间隔为 12.hours,
  • 设置<对每条记录进行 code>created_at ,使其大致适合曲线

非常感谢!

更新

我发现这篇关于 ruby​​ 算法的论坛帖子,其中让我找到了 rsruby,一个 R/Ruby 桥接器,但这似乎太多了。

更新 2

我写了这个小片段,尝试使用 gsl 库,到达那里...

在 Rails 中生成测试数据,其中created_at 遵循统计分布

Say I have 100 records, and I want to mock out the created_at date so it fits on some curve. Is there a library to do that, or what formula could I use? I think this is along the same track:

Generate Random Numbers with Probabilistic Distribution

I don't know much about how they are classified in mathematics, but I'm looking at things like:

  • bell curve
  • logarithmic (typical biology/evolution) curve?
    ...

Just looking for some formulas in code so I can say this:

  • Given 100 records, a timespan of 1.week, and an interval of 12.hours
  • set created_at for each record such that it fits, roughly, to curve

Thanks so much!

Update

I found this forum post about ruby algorithms, which led me to rsruby, an R/Ruby bridge, but that seems like too much.

Update 2

I wrote this little snippet trying out the gsl library, getting there...

Generate test data in Rails where created_at falls along a Statistical Distribution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经暖 2024-10-16 16:09:36

您可以生成实际上只是整数的 UNIX 时间戳。首先弄清楚你想什么时候开始,例如现在:

start = DateTime::now().to_time.to_i

找出你的间隔应该什么时候结束(比如 1 周后):

finish = (DateTime::now()+1.week).to_time.to_i

Ruby 使用 此算法来生成随机数。几乎是统一的。然后生成两者之间的随机数:

r = Random.new.rand(start..finish)

然后将其转换回日期:

d = Time.at(r)

这看起来也很有希望:
http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html

这也是:
http://rb-gsl.rubyforge.org/files/rdoc/rng_rdoc.html

You can generate UNIX timestamps which are really just integers. First figure out when you want to start, for example now:

start = DateTime::now().to_time.to_i

Find out when the end of your interval should be (say 1 week later):

finish = (DateTime::now()+1.week).to_time.to_i

Ruby uses this algorithm to generate random numbers. It is almost uniform. Then generate random numbers between the two:

r = Random.new.rand(start..finish)

Then convert that back to a date:

d = Time.at(r)

This looks promising as well:
http://rb-gsl.rubyforge.org/files/rdoc/randist_rdoc.html

And this too:
http://rb-gsl.rubyforge.org/files/rdoc/rng_rdoc.html

天生の放荡 2024-10-16 16:09:36

来自 wiki

有几种方法可以
生成一个基于随机数
概率密度函数。这些
方法涉及改造制服
以某种方式随机数。由于
这,这些方法同样有效
在生成伪随机和
真正的随机数。

一种方法,称为反转
方法
,涉及积分高达
面积大于或等于
随机数(应该是
生成 0 到 1 之间的正确值
分布)。

第二种方法,称为
接受-拒绝方法,
涉及选择 x 和 y 值以及
测试 x 的函数是否为
大于 y 值。如果是的话,
x 值被接受。否则,
x 值被拒绝并且
算法再次尝试。

第一种方法是您SO链接问题中接受的答案中使用的方法: 生成随机数概率分布

From wiki:

There are a couple of methods to
generate a random number based on a
probability density function. These
methods involve transforming a uniform
random number in some way. Because of
this, these methods work equally well
in generating both pseudo-random and
true random numbers.

One method, called the inversion
method
, involves integrating up to
an area greater than or equal to the
random number (which should be
generated between 0 and 1 for proper
distributions).

A second method, called the
acceptance-rejection method,
involves choosing an x and y value and
testing whether the function of x is
greater than the y value. If it is,
the x value is accepted. Otherwise,
the x value is rejected and the
algorithm tries again.

The first method is the one used in the accepted answer in your SO linked question: Generate Random Numbers with Probabilistic Distribution

私藏温柔 2024-10-16 16:09:36

另一个选择是 Distribution gem nofollow">SciRuby。您可以通过以下方式生成正态数字:

require 'distribution'

rng = Distribution::Normal.rng
random_numbers = Array.new(100).map { rng.call }

还有用于各种其他分布的 RNG。

Another option is the Distribution gem under SciRuby. You can generate normal numbers by:

require 'distribution'

rng = Distribution::Normal.rng
random_numbers = Array.new(100).map { rng.call }

There are RNGs for various other distributions as well.

孤星 2024-10-16 16:09:35

我最近遇到了 croupier,这是一个 ruby​​ gem,旨在根据各种统计分布生成数字。

我还没有尝试过,但听起来很有希望。

I recently came across croupier, a ruby gem that aims to generate numbers according to a variety of statistical distributions.

I have yet to try it but it sounds quite promising.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文