简体   繁体   English

使用 Python 从单尾分布生成范围内的随机数

[英]Generate Random Number in Range from Single-Tailed Distribution with Python

I want to generate a random float in the range [0, 1) from a one-tailed distribution that looks like this我想从一个看起来像这样的单尾分布生成一个范围 [0, 1) 的随机浮点数在此处输入图片说明

The above is the chi-squared distribution.以上是卡方分布。 I can only find resources on drawing from a uniform distribution in a range, however.但是,我只能从范围内的均匀分布中找到有关绘图的资源。

You could use a Beta distribution , eg您可以使用Beta 发行版,例如

import numpy as np

np.random.seed(2018)
np.random.beta(2, 5, 10)
#array([ 0.18094173,  0.26192478,  0.14055507,  0.07172968,  0.11830031,
#        0.1027738 ,  0.20499125,  0.23220654,  0.0251325 ,  0.26324832])

Here we draw numbers from a Beta(2, 5) distribution这里我们从Beta(2, 5)分布中抽取数字

在此处输入图片说明

The Beta distribution is a very versatile and fundamental distribution in statistics; Beta 分布是统计学中非常通用的基本分布; without going into any details, by changing the parameters alpha and beta you can make the distribution left-skewed, right-skewed, uniform, symmetric etc. The distribution is defined on the interval [0, 1] which is consistent with what you're after.无需详细说明,通过更改参数alphabeta您可以使分布左偏、右偏、均匀、对称等。分布定义在区间[0, 1] ,与您的分布一致重新之后。


A more technical comment更具技术性的评论

While the Kumaraswamy distribution certainly has more benign algebraic properties than the Beta distribution I would argue that the latter is the more fundamental distribution;虽然Kumaraswamy 分布肯定比Beta 分布具有更多的良性代数特性,但我认为后者是更基本的分布; for example, in Bayesian inference, the Beta distribution often enters as the conjugate prior when dealing with binomial(-like) processes.例如,在贝叶斯推理中,在处理二项式(类)过程时,Beta 分布通常作为共轭先验进入。

Secondly, the mean and variance of the Beta distribution can be expressed quite simply in terms of the parameters alpha , beta ;其次,Beta 分布的均值和方差可以很简单地用参数alpha , beta for example, the mean is simply given by alpha / (alpha + beta) .例如,均值简单地由alpha / (alpha + beta)

Lastly, from a computational and statistical inference point of view, fitting a Beta distribution to data is usually done in a few lines of code in Python (or R), where most Python libraries like numpy and scipy already include methods to deal with the Beta distribution.最后,从计算和统计推断的角度来看,将 Beta 分布拟合到数据通常是在 Python(或 R) scipy几行代码完成的,其中大多数 Python 库,如numpyscipy已经包含处理 Beta 分布的方法分配。

I would leaning toward distribution which is naturally bounded on [0...1] interval (or any other [a...b] interval which could be rescaled later), like @MauritsEvers answer.我倾向于自然地限定在 [0...1] 区间(或任何其他 [a...b] 区间,以后可以重新调整)的分布,就像@MauritsEvers 的回答一样。 Reason is, you know the distribution and could derive (or read) some interesting facts about it.原因是,您知道分布并且可以推导出(或阅读)有关它的一些有趣的事实。 If you use chi2 adn truncate it, it is unclear how to argue about properties of what you've got.如果您使用 chi2 和 truncate 它,则不清楚如何争论您所拥有的属性。

Personally I prefer Kumaraswamy distribution over Beta distribution, expressions for mean,mode, variance etc are a lot simpler.我个人更喜欢Kumaraswamy 分布而不是 Beta 分布,均值、众数、方差等的表达式要简单得多。

Just install it安装就行

pip install kumaraswamy

and sample和样品

from kumaraswamy import kumaraswamy

d = kumaraswamy(a=2.0, b=5.0)

q = d.rvs(10)
print(q)

will produce 10 numbers following magenta curve in the Wiki article.将按照 Wiki 文章中的洋红色曲线生成 10 个数字。

If you don't want Beta or Kumaraswamy, there is fe Logit-normal distribution and quite a few others如果您不想要 Beta 或 Kumaraswamy,则可以使用 fe Logit-normal 分布和很多其他分布

Look at the numpy.random.chisquare method library.查看numpy.random.chisquare 方法库。

numpy.random.chisquare(df, size=None)

>>> np.random.chisquare(2,4)
array([ 1.89920014,  9.00867716,  3.13710533,  5.62318272])

If you want to draw a sample of size N = 5 from a ChiSquare distribution, you can try OpenTURNS library:如果要从 ChiSquare 分布中抽取大小为N = 5的样本,可以尝试OpenTURNS库:

import openturns as ot`
# define your distribution. Here, nu = 3. (nu is a float > 0)
distribution = ot.ChiSquare(3) 

# draw a sample of size N from `distribution`
N=5
sample = distribution.getSample(N)

A complete list of distributions is available here完整的发行版列表可在此处获得

sample has an OpenTURNS format but you can manipulate it as a Numpy array: sample具有 OpenTURNS 格式,但您可以将其作为 Numpy 数组进行操作:

s = np.array(Sample)
print(s)
>>>array([[1.65299759],
      [6.78405097],
      [0.88528975],
      [0.87900211],
      [0.25031129]])

You can also easily plot the distribution PDF just by calling : distribution.drawPDF()您还可以通过调用轻松绘制分布 PDF: distribution.drawPDF()

Customizations:定制:

from openturns.viewer import View
graph = distribution.drawPDF()
title = str(distribution)[:100].split('\n')[0]
graph.setTitle(title)
View(graph, add_legend=False)

在此处输入图片说明

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

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