分布的随机数,c++。

时间:2022-09-09 08:53:57

I've written a simulation in C++ that generates (1,000,000)^2 numbers from a specific probability distribution and then does something with them. So far I've used Exponential, Normal, Gamma, Uniform and Poisson distributions. Here is the code for one of them:

我写一个模拟在c++中产生(1000000)^ 2数字从一个特定的概率分布,然后与他们的东西。到目前为止,我已经用了指数分布,正态分布,均匀分布和泊松分布。下面是其中一个的代码:

#include <boost/random.hpp>

...main...

    srand(time(NULL)) ;
    seed = rand();
    boost::random::mt19937 igen(seed) ;
    boost::random::variate_generator<boost::random::mt19937, boost::random::normal_distribution<> >
    norm_dist(igen, boost::random::normal_distribution<>(mu,sigma)) ;

Now I need to run it for the Beta distribution. All of the distributions I've done so far took 10-15 hours. The Beta distribution is not in the boost/random package so I had to use the boost/math/distributions package. I found this page on * which proposed a solution. Here it is (copy-pasted):

现在我需要运行它来进行测试。到目前为止,我所做的所有发行版都花了10到15个小时。Beta分布不在boost/random包中,所以我必须使用boost/math/distribution包。我在*上找到了这个页面,它提出了一个解决方案。在这里(充满):

#include <boost/math/distributions.hpp> 
using namespace boost::math;  
double alpha, beta, randFromUnif;  
//parameters and the random value on (0,1) you drew  
beta_distribution<> dist(alpha, beta); 
double randFromDist = quantile(dist, randFromUnif); 

I replicated it and it worked. The run time estimates of my simulation are linear and accurately predictable. They say that this will run for 25 days. I see two possibilities: 1. the method proposed is inferior to the one I was using previously for other distributions 2. the Beta distribution is just much harder to generate random numbers from

我复制了它,它成功了。我的仿真的运行时间估计是线性的,并且可以准确地预测。他们说这将持续25天。我看到了两种可能性:1。所提出的方法不如我以前在其他分布中使用的方法2。贝塔分布很难产生随机数

Bare in mind that I have below minimal understanding of C++ coding, so the questions I'm asking may be silly. I can't wait for a month for this simulation to complete, so is there anything I can do to improve that? Perhaps use the initial method that I was using and modify it to work with the boost/math/distributions package? I don't even know if that's possible.

请记住,我对c++代码的理解还不够深入,所以我提出的问题可能很愚蠢。我等不及一个月的时间来完成这个模拟,有什么可以改进的吗?也许使用我使用的初始方法并修改它以使用boost/math/分发包?我甚至不知道这是否可能。

Another piece of information that may be useful is that the parameters are the same for all (1,000,000)^2 of the numbers that I need to generate. I'm saying this because the Beta distribution does have a nasty PDF and perhaps the knowledge that the parameters are fixed can somehow be used to simplify the process? Just a random guess.

另一个可能有用的信息是,所有的参数都是相同的(1000000)^ 2的数字,我需要生成。我这么说是因为Beta分布确实有一个令人讨厌的PDF,也许参数是固定的知识可以用来简化过程?只是一个随机的猜测。

3 个解决方案

#1


5  

The beta distribution is related to the gamma distribution. Let X be a random number drawn from Gamma(α,1) and Y from Gamma(β,1), where the first argument to the gamma distribution is the shape parameter. Then Z=X/(X+Y) has distribution Beta(α,β). With this transformation, it should only take twice as much time as your gamma distribution test.

贝塔分布与伽玛分布有关。假设X是一个随机数从γ(α,1)和Yγ(β1),在第一个参数γ分布形状参数。然后Z = X /(X + Y)分布β(α,β)。通过这个转换,它只需要花费您的伽马分布测试的两倍时间。

Note: The above assumes the most common representation of the gamma distribution, Gamma(shape,scale). Be aware that different implementations of the gamma distribution random generator will vary with the meaning and order of the arguments.

注:上述假设伽玛分布最常见的表示形式,伽玛(形状,比例尺)。请注意,伽玛分布随机生成器的不同实现会随参数的含义和顺序而变化。

#2


2  

If you want a distribution that is very Beta-like, but has a very simple closed-form inverse CDF, it's worth considering the Kumaraswamy distribution:

如果你想要一个非常像beta的分布,但是有一个非常简单的封闭式逆CDF,那么值得考虑Kumaraswamy分布:

http://en.wikipedia.org/wiki/Kumaraswamy_distribution

http://en.wikipedia.org/wiki/Kumaraswamy_distribution

It's used as an alternative to the Beta distribution when a large number of random samples are required quickly.

当需要大量的随机样本时,它可以替代Beta分布。

#3


0  

Try compiling with optimization. Using a flag -O3 will usually speed things up. See this post on optimisation flags or this overview for slightly more detail.

试着编译优化。使用标志-O3通常会加快速度。请参阅optimisation flags上的这篇文章,或者稍微详细一点的概述。

#1


5  

The beta distribution is related to the gamma distribution. Let X be a random number drawn from Gamma(α,1) and Y from Gamma(β,1), where the first argument to the gamma distribution is the shape parameter. Then Z=X/(X+Y) has distribution Beta(α,β). With this transformation, it should only take twice as much time as your gamma distribution test.

贝塔分布与伽玛分布有关。假设X是一个随机数从γ(α,1)和Yγ(β1),在第一个参数γ分布形状参数。然后Z = X /(X + Y)分布β(α,β)。通过这个转换,它只需要花费您的伽马分布测试的两倍时间。

Note: The above assumes the most common representation of the gamma distribution, Gamma(shape,scale). Be aware that different implementations of the gamma distribution random generator will vary with the meaning and order of the arguments.

注:上述假设伽玛分布最常见的表示形式,伽玛(形状,比例尺)。请注意,伽玛分布随机生成器的不同实现会随参数的含义和顺序而变化。

#2


2  

If you want a distribution that is very Beta-like, but has a very simple closed-form inverse CDF, it's worth considering the Kumaraswamy distribution:

如果你想要一个非常像beta的分布,但是有一个非常简单的封闭式逆CDF,那么值得考虑Kumaraswamy分布:

http://en.wikipedia.org/wiki/Kumaraswamy_distribution

http://en.wikipedia.org/wiki/Kumaraswamy_distribution

It's used as an alternative to the Beta distribution when a large number of random samples are required quickly.

当需要大量的随机样本时,它可以替代Beta分布。

#3


0  

Try compiling with optimization. Using a flag -O3 will usually speed things up. See this post on optimisation flags or this overview for slightly more detail.

试着编译优化。使用标志-O3通常会加快速度。请参阅optimisation flags上的这篇文章,或者稍微详细一点的概述。