Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460424
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:13:40+00:00 2026-05-18T10:13:40+00:00

I wanted to use boost accumulators to calculate statistics of a variable that is

  • 0

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. 😛

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. 😛

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:13:41+00:00Added an answer on May 18, 2026 at 10:13 am

    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 ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to use full import names in my project (see this question ).
I wanted to use find to find every file in a directory that starts
I wanted to use groovy for a little ftp script and found this post
I wanted to use boost::thread in my program, but get the following compiler error
I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and
Boost does not provide single-threaded event-driven model, such that was widespread on Unix before
I wanted to protect the access to a log file that I use for
I wanted to use threads in my code and thought that the upcoming C++0x
I recently wanted use regex in Cocoa app. But I found that Cocoa does
I wanted to use PHP Simple HTML DOM Parser to grab the Google Apps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.