简体   繁体   中英

What are the right includes for using the boost accumulators library?

I'm trying to compile this example

But that one header isn't enough and I've already spent half an hour trying to clobber all of the errors. Why aren't the required includes specified?

I did this:

#include <boost/accumulators/statistics/weighted_p_square_cumulative_distribution.hpp>
#include <vector>
#include <boost/accumulators/accumulators.hpp> 
#include <boost/test/test_tools.hpp>
#include <boost/random/lagged_fibonacci.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/fusion/support/void.hpp>
#include <boost/accumulators/framework/depends_on.hpp>

using namespace boost::accumulators;
using namespace boost;

int main() {
    // tolerance in %
    double epsilon = 4;

    typedef accumulator_set<double, stats<tag::weighted_p_square_cumulative_distribution>, double > accumulator_t;

    accumulator_t acc_upper(tag::weighted_p_square_cumulative_distribution::num_cells = 100);
    accumulator_t acc_lower(tag::weighted_p_square_cumulative_distribution::num_cells = 100);

    // two random number generators
    double mu_upper = 1.0;
    double mu_lower = -1.0;
    boost::lagged_fibonacci607 rng;
    boost::normal_distribution<> mean_sigma_upper(mu_upper,1);
    boost::normal_distribution<> mean_sigma_lower(mu_lower,1);
    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_upper(rng, mean_sigma_upper);
    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_lower(rng, mean_sigma_lower);

    for (std::size_t i=0; i<100000; ++i)
    {
        double sample = normal_upper();
        acc_upper(sample, weight = std::exp(-mu_upper * (sample - 0.5 * mu_upper)));
    }

    for (std::size_t i=0; i<100000; ++i)
    {
        double sample = normal_lower();
        acc_lower(sample, weight = std::exp(-mu_lower * (sample - 0.5 * mu_lower)));
    }

    typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type;
    histogram_type histogram_upper = weighted_p_square_cumulative_distribution(acc_upper);
    histogram_type histogram_lower = weighted_p_square_cumulative_distribution(acc_lower);

    // Note that applaying importance sampling results in a region of the distribution 
    // to be estimated more accurately and another region to be estimated less accurately
    // than without importance sampling, i.e., with unweighted samples

    for (std::size_t i = 0; i < histogram_upper.size(); ++i)
    {
        // problem with small results: epsilon is relative (in percent), not absolute!

        // check upper region of distribution
        if ( histogram_upper[i].second > 0.1 )    
            BOOST_CHECK_CLOSE( 0.5 * (1.0 + erf( histogram_upper[i].first / sqrt(2.0) )), histogram_upper[i].second, epsilon );
        // check lower region of distribution
        if ( histogram_lower[i].second < -0.1 )    
            BOOST_CHECK_CLOSE( 0.5 * (1.0 + erf( histogram_lower[i].first / sqrt(2.0) )), histogram_lower[i].second, epsilon );
    }
}

And I got these errors:

In file included from /opt/local/include/boost/accumulators/statistics/weighted_p_square_cumulative_distribution.hpp:17,
                 from a.cc:1:
/opt/local/include/boost/accumulators/framework/extractor.hpp: In instantiation of 'boost::accumulators::detail::accumulator_set_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution>':
/opt/local/include/boost/mpl/eval_if.hpp:38:   instantiated from 'boost::mpl::eval_if<boost::accumulators::detail::is_accumulator_set<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double> >, boost::accumulators::detail::accumulator_set_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution>, boost::accumulators::detail::argument_pack_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution> >'
/opt/local/include/boost/accumulators/framework/extractor.hpp:57:   instantiated from 'boost::accumulators::detail::extractor_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution>'
a.cc:47:   instantiated from here
/opt/local/include/boost/accumulators/framework/extractor.hpp:36: error: no type named 'result_type' in 'struct boost::fusion::void_'
a.cc: In function 'int main()':
a.cc:47: error: no match for call to '(const boost::accumulators::extractor<boost::accumulators::tag::weighted_p_square_cumulative_distribution>) (main()::accumulator_t&)'
a.cc:48: error: no match for call to '(const boost::accumulators::extractor<boost::accumulators::tag::weighted_p_square_cumulative_distribution>) (main()::accumulator_t&)'

The examples shown in the docs are extracted from the library's unit tests. Take a look at libs/accumulators/test/weighted_p_square_cum_dist.cpp

Unfortunately you don't show the compile errors you get, so I'll try a shot in the dark:

Did you use

using namespace boost::accumulators;

in your code? Most examples require this for brevity.

Edit:
Now that you posted your code (and I checked with the code sample from the link, too) it looks to me the example is broken. The key error seems to be:

error: no type named 'result_type' in 'struct boost::fusion::void_'

But as I have no experience with boost::accumulators I'll have to give up here :-(

Maybe you can contact the author of boost::accumulators and let him know that the example is not compiling out of the box. He should be able to fix the example. And you might even get credit for spotting the problem :-)

 error: no type named ‘result_type’ in ‘struct boost::fusion::void_’

I am sorry i can't provide a clear solution, but i hope i can help figure out what the problem is. Boost fusion enables you to build struct-equivalent objects where fields can be accessed using template arguments. Why am i telling you this ? Because the error you get is typically the error you get when trying to access a field that does not exist inside a fusion-struct.

These errors are generated by the following lines :

accumulator_t acc_upper(tag::weighted_p_square_cumulative_distribution::num_cells = 100);
accumulator_t acc_lower(tag::weighted_p_square_cumulative_distribution::num_cells = 100);

I am pretty sure the errors disappear if you remove the arguments of the constructor

accumulator_t acc_upper;
accumulator_t acc_lower;

I don't know much about boost accumulators, but the error may come from two causes : either your definition of accumulator_set is incorrect (is there a difference between "stats", which you use, and "features", which is suggested in boost accumulator documentation ?), or your definition of the argument used in the constructor is (incorrect). Find out which and you're likely to have solved your compilation error.

使用boost的bcp --list来创建依赖项列表,然后您可以确定包含所有这些依赖项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM