2015-12-12 137 views
0

我想從反伽瑪分佈採樣使用下面的代碼,獲取修改我發現在線上的一些代碼。 我不是C++編程的專家,所以我需要一些解釋和幫助。從反伽瑪分佈隨機生成隨機採樣C++

#include <iostream> 
#include <fstream> 
#include <random> 
#include "boost/random.hpp" 
#include "boost/generator_iterator.hpp" 
#include <boost/math/distributions.hpp> 

using namespace std; 

int main(){ 

    boost::mt19937 rng; 
    boost::math::inverse_gamma_distribution<>invg(2.0, 3.0); 
    cout << " probability variance > 50: " << boost::math::cdf(boost::math::complement(invg, 50.0)); 

    boost::variate_generator<boost::mt19937& , boost::math::inverse_gamma_distribution<> > sampleIG(rng, invg); 


     for (int i = 0; i < 10; ++i) 
     { 
     double d = sampleIG(); 
     std::cout << d << std::endl; 
     } 
} 

我得到的錯誤是:

In file included from /usr/include/boost/random.hpp:55:0, 
       from vesSimQ.cpp:5: 
/usr/include/boost/random/variate_generator.hpp: In instantiation of ‘class boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&, boost::math::inverse_gamma_distribution<double> >’: 
vesSimQ.cpp:20:98: required from here 
/usr/include/boost/random/variate_generator.hpp:59:48: error: no type named ‘result_type’ in ‘class boost::math::inverse_gamma_distribution<double>’ 
    typedef typename Distribution::result_type result_type; 
               ^
vesSimQ.cpp: In function ‘int main()’: 
vesSimQ.cpp:25:26: error: no match for call to ‘(boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&, boost::math::inverse_gamma_distribution<double> >)()’ 
     double d = sampleIG(); 
+0

目前我解決了從伽馬分佈反轉樣本,適時重新調整。 – user58058

回答

1

對於其他發行此錯誤是由使用boost ::數學,而不是提升::隨機在variate_generator的實例化而引起的。下面是關於如何創建一個威布爾隨機數生成器的例子:

#include <boost/random/mersenne_twister.hpp>  // For boost::mt19937 
#include <boost/random/variate_generator.hpp> 
#include <boost/random/weibull_distribution.hpp> 

int main(){ 
    double shape = 3.0; 
    double scale = 0.5; 

    boost::mt19937 rng(12); 
    boost::random::weibull_distribution<> myWeibull(shape, scale); 
    boost::random::variate_generator<boost::mt19937&, boost::random::weibull_distribution<> > rand_Weibull(rng, myWeibull); 
    double rn; 
    for (int i=0; i < 10; ++i) 
    { 
     rn = rand_Weibull(); 
     cout << rn << endl; 
    } 
} 

我看到,雖然在升壓::隨機範圍gamma分佈,沒有反伽馬分佈。

我安裝了Boost 1.60.0,它有一個名爲\ boost_1_60_0 \ libs \ math \ test \的文件夾,它有一些代碼可以幫助您。