简体   繁体   English

使用boost accumulators库有哪些正确的选择?

[英]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 看一下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_' 错误:'struct boost :: fusion :: void_'中没有名为'result_type'的类型

But as I have no experience with boost::accumulators I'll have to give up here :-( 但由于我没有使用boost :: accumulators的经验,我将不得不放弃这里:-(

Maybe you can contact the author of boost::accumulators and let him know that the example is not compiling out of the box. 也许你可以联系boost :: accumulators的作者,让他知道这个例子不是开箱即用的。 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. Boost fusion使您能够构建结构等效对象,其中可以使用模板参数访问字段。 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. 因为您获得的错误通常是您在尝试访问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). 我对增强累加器知之甚少,但错误可能来自两个原因:你对accumulator_set的定义是不正确的(你使用的“stats”和“功能”之间有什么区别,这在boost中有建议累加器文档?),或者您在构造函数中使用的参数的定义是(不正确)。 Find out which and you're likely to have solved your compilation error. 找出哪些,您可能已经解决了编译错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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