[英]Is it possible to use boost accumulators with vectors?
我想使用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);
也許這很明顯,但無論如何我都試過了。 :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)是一個向量本身,其entry [i]將是累積向量的條目[i]的平均值。
在Boost的文檔中有一個暗示,但沒有任何明確的。 而我有點愚蠢。 :P
我已經調查了一下你的問題,在我看來,Boost.Accumulators已經為std::vector
提供了支持。 以下是我在用戶指南的一部分中可以找到的內容 :
數值運算符子庫有用的另一個示例是類型未定義將其用於某些統計計算所需的運算符重載。 例如,
std::vector<>
不會重載任何算術運算符,但使用std::vector<>
作為樣本或變量類型可能很有用。 Numeric Operators子庫在boost::numeric::operators
命名空間中定義必要的運算符重載,該命名空間由Accumulators Framework使用using指令引入。
實際上,在驗證之后,文件boost/accumulators/numeric/functional/vector.hpp
確實包含了“天真”解決方案工作所必需的運算符。
我相信你應該嘗試:
boost/accumulators/numeric/functional/vector.hpp
BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
boost/accumulators/numeric/functional.hpp
using namespace boost::numeric::operators;
將運算符帶入范圍using namespace boost::numeric::operators;
。 剩下的只有最后一個細節:執行將在運行時中斷,因為初始累計值是默認構造的,並且當嘗試將大小為n的向量添加到空向量時將發生斷言。 為此,您似乎應該初始化累加器(其中n是向量中的元素數):
accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(n));
我嘗試了下面的代碼, mean
給我一個大小為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);
}
我相信這就是你想要的?
我沒有立即嘗試,但如果所有boost :: accumulators需要正確定義的數學運算符,那么你可能能夠使用不同的向量類型: http : //www.boost.org /doc/libs/1_37_0/libs/numeric/ublas/doc/vector.htm
那文檔怎么樣?
// 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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.