2017-04-17 2 views
1

이 문제는 (Using boost's skewed_normal_distribution) 전에 해결되었지만 어려움이 있으며 이전 스레드에는 해결책 코드가 포함되어 있지 않습니다.부스트 사용 skew_normal_distribution

나는 내가 알고있는 코드를 채택하고

부스트 라이브러리에 내장 된 기능을 사용하여 왜곡 정규 분포에서 샘플링하고 싶은는 normal_distribution <> 클래스 잘 작동하지만이 코드를 실행하면 나는 점점 계속 오류는 아래와 같습니다. 누군가가 이것으로 나를 도울 수 있다면 매우 감사 할 것입니다.

#include <boost/random.hpp> 
#include <boost/random/normal_distribution.hpp> 
#include <boost/math/distributions/skew_normal.hpp> 

int main() { 

boost::mt19937 rng2; 

boost::math::skew_normal_distribution<> snd(0.0, 1.0, 1.0); 

boost::variate_generator<boost::mt19937&, 
     boost::math::skew_normal_distribution<> > var_snd(rng2, snd); 

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


    return 0; 
} 

오류 :

[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o 
In file included from /usr/include/boost/random.hpp:55:0, 
       from /home/jack/CLionProjects/untitled/main.cpp:1: 
/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::skew_normal_distribution<double> >’: 
/home/jack/CLionProjects/untitled/main.cpp:13:63: required from here 
/usr/include/boost/random/variate_generator.hpp:59:48: error: no type named ‘result_type’ in ‘class boost::math::skew_normal_distribution<double>’ 
    typedef typename Distribution::result_type result_type; 

답변

0

가 모두 유통 클래스가 포함 부스트에서 두 개의 서로 다른 네임 스페이스에 있습니다 : 부스트 : : 무작위 및 부스트 : 수학. 불행히도이 두 개의 서로 다른 네임 스페이스는 서로 다른 목적을 염두에두고 작성되었으므로 여기에서 수행하려고하는 것처럼 기본 배포본을 즉시 바꿀 수 없습니다.

처음에 시작한 표준 배포 클래스는 boost :: random 네임 스페이스에 속합니다. skew_normal 클래스는 boost :: math 네임 스페이스에 속하지만; 따라서 비 호환성. 당신은 단순히 부스트 :: 수학 :: 그러나 skew_normal 분포 샘플을 생성하고자하는 경우 (C++ 11을 사용하고 당신을 가정)

, 다음과 같은 방법을 사용하여 그렇게 할 수 있습니다

#include <boost/math/distributions/skew_normal.hpp> 

// Setup generators 
std::random_device rd; 
std::default_random_engine noise_generator; 

// Sample from a uniform distribution i.e. [0,1) 
std::uniform_real_distribution<double> uniform_dist(0,1.0); 

// Take a different value every time to generate probabilities from 0 to 1 
noise_generator.seed(rd()); 
auto probability = uniform_dist(noise_generator); 

auto skew_norm_dist = boost::math::skew_normal_distribution<double>(
    0, 1., 10.); 

// Use the probability from the uniform distribution with the percent point 
// function of the skew_normal 
double skew_normal_sample_point = boost::math::quantile(skew_norm_dist, probability); 
std::cout << "Sample point: " << skew_normal_sample_point << std::endl; 

여기에서는 본질적으로 균일 분포에서 확률 값을 생성 한 다음이 값을 사용하여 skew_normal_distribution의 백분율 포인트 함수에서 값을 조회합니다.

마지막 4 줄을 루프에 넣고 많은 수의 포인트를 생성하면

for(unsigned int i = 0; i < 10000; ++i) 
{ 
    noise_generator.seed(rd()); 
    auto probability = uniform_dist(noise_generator); 
    double skew_normal_sample_point = boost::math::quantile(skew_norm_dist, probability); 
    std::cout << skew_normal_sample_point << std::endl; 
} 

그 결과를 히스토그램으로 플롯하면, 사용자가 작성한 비뚤어진 정규 분포를 따르는 것을 알 수 있습니다.