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:
我想使用boost accumulators来计算作为向量的变量的统计信息。有一个简单的方法来做到这一点。我认为不可能使用最愚蠢的事情:
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
也许这很明显,但无论如何我都试过了。 :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.
我希望mean(acc)是一个向量本身,其条目[i]将是累积向量的条目[i]的平均值。
Theres a hint about this in the docs of Boost, but nothing explicit. And I'm a bit dumb. :P
在Boost的文档中有一个暗示,但没有任何明确的。而我有点愚蠢。 :P
3 个解决方案
#1
9
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 :
我已经调查了一下你的问题,在我看来,Boost.Accumulators已经为std :: vector提供了支持。以下是我在用户指南的一部分中可以找到的内容:
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 usestd::vector<>
as a sample or variate type. The Numeric Operators Sub-Library defines the necessary operator overloads in theboost::numeric::operators
namespace, which is brought into scope by the Accumulators Framework with a using directive.数值运算符子库有用的另一个示例是,类型未定义将其用于某些统计计算所需的运算符重载。例如,std :: vector <>不会重载任何算术运算符,但使用std :: vector <>作为样本或变量类型可能很有用。 Numeric Operators子库在boost :: numeric :: operators命名空间中定义必要的运算符重载,该命名空间由Accumulators Framework使用using指令引入范围。
Indeed, after verification, the file boost/accumulators/numeric/functional/vector.hpp
does contain the necessary operators for the 'naive' solution to work.
实际上,在验证之后,文件boost / accumulators / numeric / functional / vector.hpp确实包含了“天真”解决方案工作所必需的运算符。
I believe you should try :
我相信你应该尝试:
- Including either
-
boost/accumulators/numeric/functional/vector.hpp
before any other accumulators header - 在任何其他累加器头之前的boost / accumulators / numeric / functional / vector.hpp
-
boost/accumulators/numeric/functional.hpp
while definingBOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
- 定义BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT时的boost / accumulators / numeric / functional.hpp
-
- 在定义BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT时,在任何其他累加器头文件boost / accumulators / numeric / functional.hpp之前包括boost / accumulators / numeric / functional / vector.hpp
- Bringing the operators into scope with a
using namespace boost::numeric::operators;
. - 使用使用命名空间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) :
剩下的只有最后一个细节:执行将在运行时中断,因为初始累计值是默认构造的,并且在尝试将大小为n的向量添加到空向量时会发生断言。为此,您似乎应该使用(其中n是向量中的元素数)初始化累加器:
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 :
我尝试了下面的代码,意思是给我一个大小为2的std :: vector:
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 ?
我相信这就是你想要的?
#2
2
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
我没有设置现在尝试,但如果所有boost :: accumulators需要正确定义的数学运算符,那么你可能能够使用不同的向量类型:http://www.boost.org /doc/libs/1_37_0/libs/numeric/ublas/doc/vector.htm
#3
-3
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 );
#1
9
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 :
我已经调查了一下你的问题,在我看来,Boost.Accumulators已经为std :: vector提供了支持。以下是我在用户指南的一部分中可以找到的内容:
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 usestd::vector<>
as a sample or variate type. The Numeric Operators Sub-Library defines the necessary operator overloads in theboost::numeric::operators
namespace, which is brought into scope by the Accumulators Framework with a using directive.数值运算符子库有用的另一个示例是,类型未定义将其用于某些统计计算所需的运算符重载。例如,std :: vector <>不会重载任何算术运算符,但使用std :: vector <>作为样本或变量类型可能很有用。 Numeric Operators子库在boost :: numeric :: operators命名空间中定义必要的运算符重载,该命名空间由Accumulators Framework使用using指令引入范围。
Indeed, after verification, the file boost/accumulators/numeric/functional/vector.hpp
does contain the necessary operators for the 'naive' solution to work.
实际上,在验证之后,文件boost / accumulators / numeric / functional / vector.hpp确实包含了“天真”解决方案工作所必需的运算符。
I believe you should try :
我相信你应该尝试:
- Including either
-
boost/accumulators/numeric/functional/vector.hpp
before any other accumulators header - 在任何其他累加器头之前的boost / accumulators / numeric / functional / vector.hpp
-
boost/accumulators/numeric/functional.hpp
while definingBOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
- 定义BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT时的boost / accumulators / numeric / functional.hpp
-
- 在定义BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT时,在任何其他累加器头文件boost / accumulators / numeric / functional.hpp之前包括boost / accumulators / numeric / functional / vector.hpp
- Bringing the operators into scope with a
using namespace boost::numeric::operators;
. - 使用使用命名空间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) :
剩下的只有最后一个细节:执行将在运行时中断,因为初始累计值是默认构造的,并且在尝试将大小为n的向量添加到空向量时会发生断言。为此,您似乎应该使用(其中n是向量中的元素数)初始化累加器:
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 :
我尝试了下面的代码,意思是给我一个大小为2的std :: vector:
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 ?
我相信这就是你想要的?
#2
2
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
我没有设置现在尝试,但如果所有boost :: accumulators需要正确定义的数学运算符,那么你可能能够使用不同的向量类型:http://www.boost.org /doc/libs/1_37_0/libs/numeric/ublas/doc/vector.htm
#3
-3
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 );