简体   繁体   English

在Apache Commons Math中的程序流程期间更改分发参数

[英]Changing distribution parameters during program flow in Apache Commons Math

I need to generate random numbers in my code but I want to change the parameters of the Distribution based on the current scenario. 我需要在代码中生成随机数,但是我想根据当前场景更改“分发”的参数。 The application can run as single or multi-threaded application. 该应用程序可以作为单线程或多线程应用程序运行。

My question is that, should I initialize the RandomGenerator object in the constructor of my class and then use that RandomGenerator object to (re-)initialize the NormalDistribution , BetaDistribution or any other object of AbstractRealDistribution repeatedly, or just initialize my distribution object after I need to update the parameters. 我的问题是,我应该在类的构造函数中初始化RandomGenerator对象,然后使用该RandomGenerator对象重复(重新)初始化NormalDistributionBetaDistributionAbstractRealDistribution任何其他对象,还是在需要后才初始化我的分发对象更新参数。

Which is is a better option in terms of generating good random numbers and also in terms of optimality? 就生成良好的随机数以及最优性而言,哪个是更好的选择?

Case 1: 情况1:

class Test {
    protected RandomGenerator rng;
    public Test() {
        rng = new Well19937c();
    }
    private void someFunction(double mean, doube std_dev) {
        NormalDistribution norm = new NormalDistribution(this.rng, mean, std_dev);
        while (condition is met) {
            // do some calculation, create some random numbers, get new mean and std_dev
            norm = new NormalDistribution(this.rng, new_mean, new_std_dev);
        }
    }
}

Case 2: 情况2:

class Test {
    private void someFunction(double mean, doube std_dev) {
        NormalDistribution norm = new NormalDistribution(mean, std_dev);
        while (condition is met) {
            // do some calculation, create some random numbers, get new mean and std_dev
            norm = new NormalDistribution(new_mean, new_std_dev);
        }
    }
}

You should reuse the RandomGenerator across calls. 您应该在调用之间重用RandomGenerator The simplest way to generate random values with different distribution parameters is to use the RandomDataGenerator class in the random package: 生成具有不同分布参数的随机值的最简单方法是在random包中使用RandomDataGenerator类:

RandomDataGenerator generator = new RandomDataGenerator(new Well19937c());
// Unit normal
double normDev = generator.nextGaussian(0, 1);
// mean = 0.5, std dev = 2
double normDev2 = generator.nextGaussian(0.5, 2);
// exponential, mean = 1
double expDev = generator.nextExponential(1);

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

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