2013-03-01 124 views
6

我需要一個像betarand(a,b)這樣的函數的c或C++源代碼,它可以產生帶有beta分佈的隨機數。我知道我可以使用boost庫,但是我將把它移植到CUDA架構中,所以我需要代碼。有人能幫助我嗎?
同時我有betapdf(Beta概率密度函數)。但我不知道如何使用它來創建隨機數字:)。Beta分配的隨機數發生器

+0

嗯,我想根據定義,如果數字是以某種方式產生的,他們可以被描述爲分佈的一部分,他們不能是隨機的。 :)但無論如何.. [此測試版功能](http://projects.scipy.org/numpy/browser/trunk/numpy/random/mtrand/distributions.c)爲你工作? – Mike 2013-03-01 19:25:06

+0

@Mike,哪個定義?你是否暗示線性分佈比任何其他分佈「更隨機」? – Kos 2013-03-01 19:30:12

+0

根據http://en.wikipedia.org/wiki/Beta_distribution#Generating_beta-distributed_random_variates,您可以從2個Gamma分佈中生成Beta分佈。由於C++ 11提供了伽瑪分佈,因此應該可以使用它們來創建測試版分佈。 – 2013-03-01 19:32:56

回答

14

的C++ 11的隨機數庫不提供Beta分佈。但是,Beta分佈可以用兩個伽馬分佈來建模,其中庫確實提供了。按照std::gamma_distribution爲您實施beta_distribution。據我所知,它完全符合隨機數分佈的要求。

#include <iostream> 
#include <sstream> 
#include <string> 
#include <random> 

namespace sftrabbit { 

    template <typename RealType = double> 
    class beta_distribution 
    { 
    public: 
     typedef RealType result_type; 

     class param_type 
     { 
     public: 
      typedef beta_distribution distribution_type; 

      explicit param_type(RealType a = 2.0, RealType b = 2.0) 
      : a_param(a), b_param(b) { } 

      RealType a() const { return a_param; } 
      RealType b() const { return b_param; } 

      bool operator==(const param_type& other) const 
      { 
      return (a_param == other.a_param && 
        b_param == other.b_param); 
      } 

      bool operator!=(const param_type& other) const 
      { 
      return !(*this == other); 
      } 

     private: 
      RealType a_param, b_param; 
     }; 

     explicit beta_distribution(RealType a = 2.0, RealType b = 2.0) 
     : a_gamma(a), b_gamma(b) { } 
     explicit beta_distribution(const param_type& param) 
     : a_gamma(param.a()), b_gamma(param.b()) { } 

     void reset() { } 

     param_type param() const 
     { 
     return param_type(a(), b()); 
     } 

     void param(const param_type& param) 
     { 
     a_gamma = gamma_dist_type(param.a()); 
     b_gamma = gamma_dist_type(param.b()); 
     } 

     template <typename URNG> 
     result_type operator()(URNG& engine) 
     { 
     return generate(engine, a_gamma, b_gamma); 
     } 

     template <typename URNG> 
     result_type operator()(URNG& engine, const param_type& param) 
     { 
     gamma_dist_type a_param_gamma(param.a()), 
         b_param_gamma(param.b()); 
     return generate(engine, a_param_gamma, b_param_gamma); 
     } 

     result_type min() const { return 0.0; } 
     result_type max() const { return 1.0; } 

     result_type a() const { return a_gamma.alpha(); } 
     result_type b() const { return b_gamma.alpha(); } 

     bool operator==(const beta_distribution<result_type>& other) const 
     { 
     return (param() == other.param() && 
       a_gamma == other.a_gamma && 
       b_gamma == other.b_gamma); 
     } 

     bool operator!=(const beta_distribution<result_type>& other) const 
     { 
     return !(*this == other); 
     } 

    private: 
     typedef std::gamma_distribution<result_type> gamma_dist_type; 

     gamma_dist_type a_gamma, b_gamma; 

     template <typename URNG> 
     result_type generate(URNG& engine, 
     gamma_dist_type& x_gamma, 
     gamma_dist_type& y_gamma) 
     { 
     result_type x = x_gamma(engine); 
     return x/(x + y_gamma(engine)); 
     } 
    }; 

    template <typename CharT, typename RealType> 
    std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os, 
    const beta_distribution<RealType>& beta) 
    { 
    os << "~Beta(" << beta.a() << "," << beta.b() << ")"; 
    return os; 
    } 

    template <typename CharT, typename RealType> 
    std::basic_istream<CharT>& operator>>(std::basic_istream<CharT>& is, 
    beta_distribution<RealType>& beta) 
    { 
    std::string str; 
    RealType a, b; 
    if (std::getline(is, str, '(') && str == "~Beta" && 
     is >> a && is.get() == ',' && is >> b && is.get() == ')') { 
     beta = beta_distribution<RealType>(a, b); 
    } else { 
     is.setstate(std::ios::failbit); 
    } 
    return is; 
    } 

} 

使用它,像這樣:

std::random_device rd; 
std::mt19937 gen(rd()); 
sftrabbit::beta_distribution<> beta(2, 2); 
for (int i = 0; i < 10000; i++) { 
    std::cout << beta(gen) << std::endl; 
} 
+0

你好。希望你在幾年後仍然可以閱讀,但如果我沒有錯誤地閱讀這一代處方:https://en.wikipedia.org/wiki/Beta_distribution#Generating_beta-distributed_random_variates 生成函數應該返回x_gamma(引擎)/ (x_gamma(引擎)+ y_gamma(引擎),並且不使用相同的x值兩次。 – Daniel 2016-06-21 14:54:41

+0

@Daniel我已經在堆棧溢出幾年來一直處於非活動狀態,並且我終於查看了關於我的帖子的任何評論。現在我有點擔心萬一任何人使用這種實現,它可能是不正確的 - 但是,我試圖實現它,而不是像你所描述的,它似乎並沒有產生適當的分配。然而,在這個答案中,分配看起來是正確的。不幸的是,我找不到一個很好的來源,顯示X伽馬分佈是否被採樣一次或兩次。如果您有更多信息,請告訴我! – 2017-12-19 11:19:13

2

也許您可以使用gsl用於生成隨機數字的代碼與beta分佈。他們使用一種奇怪的方式來生產它們,因爲你必須將一個隨機數發生器傳遞給函數,但肯定你可以得到你所需要的。

這裏的documentationweb page

1

升壓 「逆完全β」 是模擬貝塔另一個快速(簡單)的方式。

#include <random> 
#include <boost/math/special_functions/beta.hpp> 
template<typename URNG> 
double beta_sample(URNG& engine, double a, double b) 
{ 
    static std::uniform_real_distribution<double> unif(0,1); 
    double p = unif(engine); 
    return boost::math::ibeta_inv(a, b, p); 
    // Use Boost policies if it's not fast enough 
}