泊松分布和均匀分布的 Java 生成器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/750325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:02:56  来源:igfitidea点击:

Java Generator for Poisson and Uniform Distributions?

javadistributiongeneratorrandompoisson

提问by

From what I understand, the standard generator is for the Normal Distribution. I have to generate random numbers according to the Normal, Uniform and Poisson Distributions, but I can't seem to find a class for the last 2.

据我了解,标准生成器用于正态分布。我必须根据正态分布、均匀分布和泊松分布生成随机数,但我似乎找不到最后 2 个的类。

I have to generate them in the range of 0 - 999999.

我必须在 0 - 999999 的范围内生成它们。

回答by David Z

Actually, the standard generatoris for the uniform distribution. The basic random number generator in any language/library will always (in all cases I know of) use the uniform distribution because that's what comes out of all the popular pseudorandom number generator algorithms - basically, uniform random numbers are the easiest.

实际上,标准生成器是针对均匀分布的。任何语言/库中的基本随机数生成器将始终(在我知道的所有情况下)使用均匀分布,因为这是所有流行的伪随机数生成器算法的结果 - 基本上,均匀随机数是最简单的。

I see Eddie already pointed you to a link for other distributions so I'll skip writing the rest of this...

我看到 Eddie 已经向您指出了其他发行版的链接,所以我将跳过编写其余部分...

回答by Tom

Let me preface all this by the fact that none of this is truly random, I am talking about pseudo random number generators.

让我先说明这一切都不是真正随机的,我说的是伪随机数生成器。

Let me also say that I have never had to do this for production quality code. I have done this for a hw assignment though, in Python. I simulated Poisson random variables.

我还要说,对于生产质量代码,我从来没有这样做过。不过,我已经在 Python 中为硬件分配完成了这项工作。我模拟了泊松随机变量。

The way that I did it made use of the following facts:

我这样做的方式利用了以下事实:

  1. A Poisson random variable is a sum of exponential random variables.
  2. We can use the inverse transform method to generate exponential random variables. http://en.wikipedia.org/wiki/Inverse_transform_sampling.
  1. 泊松随机变量是指数随机变量的总和。
  2. 我们可以使用逆变换方法来生成指数随机变量。http://en.wikipedia.org/wiki/Inverse_transform_sampling

In particular, you can use the fact that: if X1, ..., Xnare independent standardexponential random variables, then Z = min(k : X1+ ... + Xk< λ) - 1 is Poisson(λ).

特别是,您可以使用以下事实:如果 X 1, ..., X n是独立的标准指数随机变量,则 Z = min(k : X 1+ ... + X k< λ) - 1 是泊松(λ)。

So, with that, I wrote the following code in python to generate Poisson values:

因此,我在 python 中编写了以下代码来生成泊松值:

class Poisson:
    """Generate Poisson(lambda) values by using exponential
    random variables."""

    def __init__(self, lam):
        self.__lam = lam

    def nextPoisson(self):
        sum = 0
        n = -1
        while sum < self.__lam:
            n += 1
            sum -= math.log(random.random())
        return n

Example usage of the class is:

该类的示例用法是:

# Generates a random value that is Poisson(lambda = 5) distributed
poisson = Poisson(5)
poisson_value = poisson.nextPoisson

I posted this here because it is good to know that these kinds of relationships exist, and this inverse transform method gives you a general way to deal with generating random values following a particular continuous distribution.

我在这里发布这个是因为很高兴知道存在这些类型的关系,并且这种逆变换方法为您提供了一种处理遵循特定连续分布生成随机值的通用方法。

回答by Simon Nickerson

As David has pointed out, the supplied pseudo-random number generator uses the Uniform distribution.

正如大卫所指出的,提供的伪随机数生成器使用均匀分布。

For the other two, I would use the Cern Colt library functions:

对于另外两个,我将使用Cern Colt库函数:

These library functions easily allow you to find a random number taken from each distribution, rather than giving you a probability density function or cumulative density function and expecting you to derive the number yourself (which seems to be the Apache Commons-Math approach):

这些库函数很容易让你从每个分布中找到一个随机数,而不是给你一个概率密度函数或累积密度函数,并期望你自己推导出这个数字(这似乎是 Apache Commons-Math 方法):

RandomEngine engine = new DRand();
Poisson poisson = new Poisson(lambda, engine);
int poissonObs = poisson.nextInt();

Normal normal = new Normal(mean, variance, engine);
double normalObs = normal.nextDouble();

Also, bear in mind that the Poisson distribution P(λ) for large λ can be approximated very well by the normal distribution N(λ, sqrt(λ)).

此外,请记住,大 λ 的泊松分布 P(λ) 可以很好地近似为正态分布 N(λ, sqrt(λ))。

回答by Dan Dyer

The standard Java RNG (java.util.Random), and its subclasses such as java.security.SecureRandom, already generate uniformly distributed values.

标准 Java RNG ( java.util.Random) 及其子类,例如java.security.SecureRandom,已经生成均匀分布的值。

They also have a method, nextGaussian, that returns normally-distributed values. By default, the distribution has mean of zero and standard deviation of 1 but this is trivially tweaked. Just multiply by the required s.d. and add the required mean. So, for example, if you wanted normally-distributed values with a mean of 6 and standard deviation of 2.5, you'd do this:

他们还有一个方法nextGaussian,它返回正态分布的值。默认情况下,分布的均值为 0,标准差为 1,但这是微不足道的调整。只需乘以所需的标准差并加上所需的平均值。因此,例如,如果您想要均值为 6 且标准差为 2.5 的正态分布值,您可以这样做:

double value = rng.nextGaussian() * 2.5 + 6;

The Poisson distribution is not explicitly supported, but you can fake it by doing the same as Tom's Python code.

Poisson 分布没有明确支持,但您可以通过执行与Tom 的 Python 代码相同的操作来伪造它。

Alternatively, you may be interested in my Uncommons Maths library, which provides utility classes for Normal, Poisson and other distributions.

或者,您可能对我的Uncommons Maths 库感兴趣,它为正态分布、泊松分布和其他分布提供实用程序类。