Exemplo n.º 1
0
bool do_test(int n, int max) {
    std::cout << "running piecewise_constant(p0, p1, ..., p" << n-1 << ")" << " " << max << " times: " << std::flush;

    std::vector<double> weights;
    {
        boost::mt19937 egen;
        for(int i = 0; i < n; ++i) {
            weights.push_back(egen());
        }
    }
    std::vector<double> intervals;
    for(int i = 0; i <= n; ++i) {
        intervals.push_back(i);
    }

    piecewise_constant expected(intervals, weights);
    
    boost::random::piecewise_constant_distribution<> dist(intervals, weights);
    boost::mt19937 gen;
    kolmogorov_experiment test(max);
    boost::variate_generator<boost::mt19937&, boost::random::piecewise_constant_distribution<> > vgen(gen, dist);

    double prob = test.probability(test.run(vgen, expected));

    bool result = prob < 0.99;
    const char* err = result? "" : "*";
    std::cout << std::setprecision(17) << prob << err << std::endl;

    std::cout << std::setprecision(6);

    return result;
}
Exemplo n.º 2
0
bool do_test(int n, long long max) {
    std::cout << "running discrete(p0, p1, ..., p" << n-1 << ")" << " " << max << " times: " << std::flush;

    std::vector<double> expected;
    {
        boost::mt19937 egen;
        for(int i = 0; i < n; ++i) {
            expected.push_back(egen());
        }
        double sum = std::accumulate(expected.begin(), expected.end(), 0.0);
        for(std::vector<double>::iterator iter = expected.begin(), end = expected.end(); iter != end; ++iter) {
            *iter /= sum;
        }
    }
    
    boost::random::discrete_distribution<> dist(expected);
    boost::mt19937 gen;
    std::vector<long long> results(expected.size());
    for(long long i = 0; i < max; ++i) {
        ++results[dist(gen)];
    }

    long long sum = std::accumulate(results.begin(), results.end(), 0ll);
    if(sum != max) {
        std::cout << "*** Failed: incorrect total: " << sum << " ***" << std::endl;
        return false;
    }
    double chsqr = chi_squared_test(results, expected, max);

    bool result = chsqr < 0.99;
    const char* err = result? "" : "*";
    std::cout << std::setprecision(17) << chsqr << err << std::endl;

    std::cout << std::setprecision(6);

    return result;
}