是否可以将 boost 累加器与向量一起使用?

发布于 2024-10-05 09:37:11 字数 921 浏览 7 评论 0原文

我想使用 boost 累加器来计算向量变量的统计数据。有没有一种简单的方法可以做到这一点。我认为不可能使用最愚蠢的东西:

  using namespace boost::accumulators;
  //stuff...

  accumulator_set<vector<double>, stats<tag::mean> > acc;
  vector<double> some_vetor;
  //stuff 
  some_vector = doStuff();
  acc(some_vector);

也许这是显而易见的,但我还是尝试了。 :P

我想要的是有一个累加器来计算一个向量,该向量是许多向量分量的平均值。有简单的出路吗?

编辑:

我不知道我是否完全清楚。我不想要这个:

 for_each(vec.begin(), vec.end(),acc); 

这将计算给定向量的条目的平均值。我需要的是不同的。我有一个会吐出向量的函数:

 vector<double> doSomething(); 
 // this is a monte carlo simulation;

我需要运行多次并计算这些向量的向量平均值:

  for(int i = 0; i < numberOfMCSteps; i++){
  vec = doSomething();
  acc(vec);
  }
  cout << mean(acc);

我希望mean(acc)本身就是一个向量,其条目[i ] 将是累积向量的条目 [i] 的平均值。

Boost 的文档中有对此的暗示,但没有明确的。而且我有点傻。 :P

I wanted to use boost accumulators to calculate statistics of a variable that is a vector. Is there a simple way to do this. I think it's not possible to use the dumbest thing:

  using namespace boost::accumulators;
  //stuff...

  accumulator_set<vector<double>, stats<tag::mean> > acc;
  vector<double> some_vetor;
  //stuff 
  some_vector = doStuff();
  acc(some_vector);

maybe this is obvious, but I tried anyway. :P

What I wanted was to have an accumulator that would calculate a vector which is the mean of the components of many vectors. Is there an easy way out?

EDIT:

I don't know if I was thoroughly clear. I don't want this:

 for_each(vec.begin(), vec.end(),acc); 

This would calculate the mean of the entries of a given vector. What I need is different. I have a function that will spit vectors:

 vector<double> doSomething(); 
 // this is a monte carlo simulation;

And I need to run this many times and calculate the vectorial mean of those vectors:

  for(int i = 0; i < numberOfMCSteps; i++){
  vec = doSomething();
  acc(vec);
  }
  cout << mean(acc);

And I want mean(acc) to be a vector itself, whose entry [i] would be the means of the entries [i] of the accumulated vectors.

Theres a hint about this in the docs of Boost, but nothing explicit. And I'm a bit dumb. :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人接听 2024-10-12 09:37:11

我稍微研究了一下你的问题,在我看来,Boost.Accumulators 已经提供了对 std::vector 的支持。这是我可以在 用户指南的一部分

另一个例子,其中数字
运算符子库很有用的是
当类型没有定义时
使用它所需的运算符重载
用于一些统计计算。
例如,std::vector 不会重载任何算术运算符,但
使用 std::vector 可能很有用
作为样本或变量类型。这
数字运算符子库定义
必要的运算符重载
boost::numeric::operators
命名空间,被纳入范围
通过累加器框架
使用指令。

验证,文件 boost/accumulators/numeric/functions/vector.hpp 确实包含“简单”解决方案所需的运算符去工作。

我相信你应该尝试:

  • 包括
    • boost/accumulators/numeric/function/vector.hpp 在任何其他累加器标头之前
    • 定义BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
    • boost/accumulators/numeric/functional.hpp
  • 将运算符纳入 using namespace boost::numeric::operators; 的范围。

只剩下最后一个细节:执行将在运行时中断,因为初始累积值是默认构造的,并且当尝试将大小为 n 的向量添加到空向量时会发生断言。为此,似乎您应该使用以下方式初始化累加器(其中 n 是向量中的元素数量):

accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(n));

我尝试了以下代码,mean 给了我一个 std::vector 大小为 2:

int main()
{
    accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(2));

    const std::vector<double> v1 = boost::assign::list_of(1.)(2.);
    const std::vector<double> v2 = boost::assign::list_of(2.)(3.);
    const std::vector<double> v3 = boost::assign::list_of(3.)(4.);
    acc(v1);
    acc(v2);
    acc(v3);

    const std::vector<double> &meanVector = mean(acc);
}

我相信这就是您想要的?

I've looked into your question a bit, and it seems to me that Boost.Accumulators already provides support for std::vector. Here is what I could find in a section of the user's guide :

Another example where the Numeric
Operators Sub-Library is useful is
when a type does not define the
operator overloads required to use it
for some statistical calculations.
For instance, std::vector<> does not overload any arithmetic operators, yet
it may be useful to use std::vector<>
as a sample or variate type. The
Numeric Operators Sub-Library defines
the necessary operator overloads in
the boost::numeric::operators
namespace, which is brought into scope
by the Accumulators Framework with a
using directive.

Indeed, after verification, the file boost/accumulators/numeric/functional/vector.hpp does contain the necessary operators for the 'naive' solution to work.

I believe you should try :

  • Including either
    • boost/accumulators/numeric/functional/vector.hpp before any other accumulators header
    • boost/accumulators/numeric/functional.hpp while defining BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
  • Bringing the operators into scope with a using namespace boost::numeric::operators;.

There's only one last detail left : execution will break at runtime because the initial accumulated value is default-constructed, and an assertion will occur when trying to add a vector of size n to an empty vector. For this, it seems you should initialize the accumulator with (where n is the number of elements in your vector) :

accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(n));

I tried the following code, mean gives me a std::vector of size 2 :

int main()
{
    accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(2));

    const std::vector<double> v1 = boost::assign::list_of(1.)(2.);
    const std::vector<double> v2 = boost::assign::list_of(2.)(3.);
    const std::vector<double> v3 = boost::assign::list_of(3.)(4.);
    acc(v1);
    acc(v2);
    acc(v3);

    const std::vector<double> &meanVector = mean(acc);
}

I believe this is what you wanted ?

開玄 2024-10-12 09:37:11

我现在还没有设置它来尝试,但如果所有 boost::accumulators 需要的是正确定义的数学运算符,那么您可能可以使用不同的向量类型: http://www.boost.org/doc/libs/1_37_0/libs/numeric/ ublas/doc/vector.htm

I don't have it set up to try right now, but if all boost::accumulators need is properly defined mathematical operators, then you might be able to get away with a different vector type: http://www.boost.org/doc/libs/1_37_0/libs/numeric/ublas/doc/vector.htm

魂归处 2024-10-12 09:37:11

那么文档怎么样

// The data for which we wish to calculate statistical properties:
std::vector< double > data( /* stuff */ );

// The accumulator set which will calculate the properties for us:    
accumulator_set< double, features< tag::min, tag::mean > > acc;

// Use std::for_each to accumulate the statistical properties:
acc = std::for_each( data.begin(), data.end(), acc );

And what about the documentation?

// The data for which we wish to calculate statistical properties:
std::vector< double > data( /* stuff */ );

// The accumulator set which will calculate the properties for us:    
accumulator_set< double, features< tag::min, tag::mean > > acc;

// Use std::for_each to accumulate the statistical properties:
acc = std::for_each( data.begin(), data.end(), acc );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文