Java使用beta分布生成介于0到1之间的随机数

3

我需要使用Beta分布Beta(a, b)来生成一个从0到1的随机数。

我找到了BetaDist类,它有一个构造函数BetaDist(double alpha, double beta),可以使用参数α=alpha和β=beta以及默认域(0,1)构造BetaDist对象。

但是,我找不到一种方法,可以只使用alpha和beta来返回一个使用BetaDist对象随机抽取的x(0, 1)。

我在stackoverflow上读到另一篇帖子说: 从具有无跳跃的cdf的任意分布中生成随机数的通用方法是使用cdf的反函数:G(y)=F^{-1}(y)。如果u(1),…,u(n)是从均匀分布(0,1)中随机选出的数字,则G(u(1)),…,G(u(n))是来自具有cdf F(x)的分布的随机样本。

BetaDist类确实有cdf(double x)方法,但我仍然不知道下一步该怎么做。我还没有学过统计学,上面的帖子对我来说仍然太复杂了。

非常感谢。


3
"我在Stack Overflow上读了另一篇文章" -> 哪篇文章? - reprogrammer
BetaDist是指位于http://www.iro.umontreal.ca/~simardr/ssj/doc/html/umontreal/iro/lecuyer/probdist/BetaDist.html的类吗? - reprogrammer
是的,那就是这个类。帖子链接是https://dev59.com/ilHTa4cB1Zd3GeqPVeO8。 - user1864404
1个回答

2
我是一名有用的助手,以下是您需要翻译的内容:

我遇到了与你相同的问题。你提到的方案是有效的,在我的情况下我已经测试过了。

具体步骤如下:

  1. generate a beta distribution--beta with parameter alpha and beta;
  2. generate a random number from uniform distribution --x;
  3. call the inverse cdf to acquire the random number of beta distribution--b, here "x" is used as function input and the inverse cdf can return the random number you want. Notice: the beta distribution should have an inverse cdf to do this, rather than just cdf.

    import org.apache.commons.math3.distribution.BetaDistribution;
    
    public class test {
        /**
         * @param args
         */
        public static void main(String[] args) {
            double x;
            double b;
            BetaDistribution beta = new BetaDistribution(40.0, 40.0);
            for (int i = 0; i < 100; i++) {
                x = Math.random();
                b = beta.inverseCumulativeProbability(x);
                System.out.println(b);
            }
        }
    }
    

5
如果你已经在使用Apache commons BetaDistribution,那么你可以直接使用beta.sample()进行采样。这是从AbstractRealDistribution继承而来的采样方法。 - obuzek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接