Boost Pareto分布随机生成的数字 [英] Randomly Generated Numbers from Boost Pareto Distribution

查看:224
本文介绍了Boost Pareto分布随机生成的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在问这个问题之前,我做了很多关于栈溢出和谷歌的研究.我正在做一个仿真,需要能够生成遵循某些统计分布的0到1.0之间的随机输入数据.

So I did a fair amount of looking around stack overflow and google before asking this question. I have a simulation that I am working on and need to be able to generate random input data between 0 and 1.0 that follow certain statistical distributions.

到目前为止,我已经获得了正态分布和统一的真实分布正常运行的结果,但是仍然受帕累托分布的影响.

So far, I have gotten a normal distribution and a uniform real distribution working properly, but am stuck with the Pareto distribution.

前两个可在boost/random/中使用,但pareto仅可作为原始发行版使用(即在变量生成器中不可用).有谁知道生成所说随机数的方法吗?请注意,我已经遍历了boost文档,包括Pareto发行版.我正在寻找生成随机数,以 遵循 的帕累托分布, 使用帕累托分布来确定统计概率.到目前为止,我唯一能想到的就是使用统一生成器并将这些值插入Pareto分布的CDF中(但必须有一种比这更好的方法).

The first two are available in boost/random/, but the pareto is only available as a raw distribution (i.e. not available to be used in the variate generator). Does anyone know of a way to generate said random numbers? Please note that I have already poured over the boost documentation, Including for the Pareto distribution. I am looking to generate random numbers that follow a Pareto distribution, not use a Pareto distribution to determine statistical probabilities. The only thing I can think of so far is to use a uniform generator and plug those values into the CDF of the Pareto distribution (but there has to be a better way than this).

当我是新手时,任何帮助将不胜感激.

Any help would be greatly appreciated as I am new to boost.

谢谢!

这是我为前两个代码使用的代码,与变体生成器一起使用.这都是非常多的测试代码,所以请不要在样式或约定上对我有所锤击:

Here is the code I am using for the first two, in tandem with a variant generator. This is all very much test code, so please don't hammer me on style or conventions:

#include <time.h>
#include <iostream>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/math/distributions/pareto.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>

int main(){
    boost::mt19937 randGen(time(0));

    boost::normal_distribution<> dist1(.5,.2);
    boost::random::uniform_real_distribution<> dist2(0.0,1.0);

    boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > generator1(randGen,dist1);
    boost::variate_generator<boost::mt19937&,boost::random::uniform_real_distribution<> > generator2(randGen,dist2);

    for(int x = 0; x < 10; x++)
        std::cout << generator1() << std::endl;

    std::cout << "\n\n\n";

    for(int x = 0; x < 10; x++)
        std::cout << generator2() << std::endl;

    return 0;
}

推荐答案

在进行了更多研究并咨询了统计部门的一些人员之后,我找到了一种使用Uniform_real分布进行此操作的方法.我最初尝试使用分位数功能,如 this <中所述/a>发布,但总是得到1或0的字符串作为我的结果.

After doing some more research and consulting some people in the stats department I found a way to do this using a uniform_real distribution. I originally tried to use the quantile function, as described in this post, but always got strings of 1's or 0's as my results.

经过一些反复试验后,我发现实际上您需要做的就是将均匀实随机数的结果插入cdf补数函数中.

After some additional trial and error I found that essentially all you need to do is plug the results of uniform real random into the cdf complement function.

Boost很有趣,因为它使用非成员函数来计算cdf值,因此cdf并不是parteo分布本身的属性.相反,在boost中执行此操作的正确方法是:

Boost is interesting in that it uses non-member functions to calculate the cdf values, so the cdf is not a property of the parteo distribution itself. Instead the proper way to do this in boost is:

#include <boost/random/uniform_real_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/math/distributions/pareto.hpp>

int main(){
     boost::mt19937 randGen(15); //held constant for repeatability
     boost:math::pareto_distribution<> dist;
     boost::random::uniform_real_distribution<> uniformReal(1.0,10.0); //this range can be adjusted to effect values

     boost::variate_generator<boost::mt19937&,boost::random::uniform_real_distribution<> > generator(randGen, dist);

     double cdfComplement;
     for(int i = 0; i < 5000; i++){
          cdfComplement = boost::math::cdf(complement(dist,generator()));
          //do something with value
     }         

     return 0;
}

到目前为止,我还没有找到将分布的值限制为恰好在0.0到1.0范围内的值的好方法.有些离群值略低于0.0,而其他离群值略高于1.0(尽管这完全取决于您输入的实数范围).您可以轻松地舍弃所寻找范围之外的值.

There is no good way that I have found, as of yet, to limit the values of the distribution to values exactly within the 0.0 to 1.0 range. There are outliers that dip slightly below 0.0 and others that go just over 1.0 (though this depends entirely on the range of real numbers that you feed into it). You can easily throw away values outside the range you are look for.

我能够使用默认的形状参数和上述方法获得这些结果.显示了5,000个数据点:

I was able to achieve these results using the default shape parameters and the method described above. There are 5,000 data points shown:

这篇关于Boost Pareto分布随机生成的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆