我认为我的结果与他们的结果相去甚远.我的函数使用boost库中的元组,它看起来像这样:
double add_square(double prev_sum,double new_val){ return prev_sum + new_val*new_val;}template <typename V>double vec_add_squares(const V<double>& v){ return std::accumulate(v.begin(),v.end(),0.0,add_square);} template <class T> boost::tuple<double,double> get_std_dev_and_error(const vector<T>& input,double r,double N){ double M = double(input.size()); double sum = std::accumulate(input.begin(),input.end(),0.0); double Squared_sum = vec_add_squares(input); std::cout << "sum " << Squared_sum << endl; // Calls Sum double term1 = Squared_sum - (sum/M)*sum; double SD = (sqrt(term1) * exp(-2.0 * r *N))/(M-1) ; double SE = SD/sqrt(M); std::cout << "SD = " << SD << endl; std::cout << "SE = " << SE << endl; return boost::tuple<double,double>(SD,SE) ; }
>谁能在这里看到任何错误?
>此外,STL库中还有“累积”功能 – 是否存在累积平方(容器成员)?
#include <boost/accumulators/accumulators.hpp>#include <boost/accumulators/statistics.hpp>#include <boost/range/algorithm.hpp>#include <iostream>#include <ostream>using namespace boost;using namespace boost::accumulators;using namespace std;int main(){ accumulator_set<double,stats<tag::sum,tag::variance,tag::mean > > acc; double data[] = {1.,2.,3.}; acc = for_each(data,acc); cout << "sum = " << sum(acc) << endl; cout << "variance = " << variance(acc) << endl; cout << "sqrt(variance()) = " << sqrt(variance(acc)) << endl; cout << "mean = " << mean(acc) << endl;}
输出是:
sum = 6variance = 0.666667sqrt(variance()) = 0.816497mean = 2总结
以上是内存溢出为你收集整理的蒙特卡洛C标准差的通用函数全部内容,希望文章能够帮你解决蒙特卡洛C标准差的通用函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)