Calculate mean and standard deviation from a vector of samples in C++ using Boost











up vote
78
down vote

favorite
34












Is there a way to calculate mean and standard deviation for a vector containing samples using Boost?



Or do I have to create an accumulator and feed the vector into it?










share|improve this question




























    up vote
    78
    down vote

    favorite
    34












    Is there a way to calculate mean and standard deviation for a vector containing samples using Boost?



    Or do I have to create an accumulator and feed the vector into it?










    share|improve this question


























      up vote
      78
      down vote

      favorite
      34









      up vote
      78
      down vote

      favorite
      34






      34





      Is there a way to calculate mean and standard deviation for a vector containing samples using Boost?



      Or do I have to create an accumulator and feed the vector into it?










      share|improve this question















      Is there a way to calculate mean and standard deviation for a vector containing samples using Boost?



      Or do I have to create an accumulator and feed the vector into it?







      c++ algorithm boost statistics mean






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 17 '16 at 18:04









      Peter Mortensen

      13.3k1983111




      13.3k1983111










      asked Sep 30 '11 at 21:59









      user393144

      5401718




      5401718
























          8 Answers
          8






          active

          oldest

          votes

















          up vote
          44
          down vote



          accepted










          Using accumulators is the way to compute means and standard deviations in Boost.



          accumulator_set<double, stats<tag::variance> > acc;
          for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));

          cout << mean(acc) << endl;
          cout << sqrt(variance(acc)) << endl;


           






          share|improve this answer



















          • 4




            Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
            – panda-34
            Dec 7 '15 at 13:36










          • Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
            – Kemin Zhou
            May 11 '17 at 16:11


















          up vote
          181
          down vote













          I don't know if Boost has more specific functions, but you can do it with the standard library.



          Given std::vector<double> v, this is the naive way:



          #include <numeric>

          double sum = std::accumulate(v.begin(), v.end(), 0.0);
          double mean = sum / v.size();

          double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
          double stdev = std::sqrt(sq_sum / v.size() - mean * mean);


          This is susceptible to overflow or underflow for huge or tiny values. A slightly better way to calculate the standard deviation is:



          double sum = std::accumulate(v.begin(), v.end(), 0.0);
          double mean = sum / v.size();

          std::vector<double> diff(v.size());
          std::transform(v.begin(), v.end(), diff.begin(),
          std::bind2nd(std::minus<double>(), mean));
          double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
          double stdev = std::sqrt(sq_sum / v.size());


          UPDATE for C++11:



          The call to std::transform can be written using a lambda function instead of std::minus and std::bind2nd(now deprecated):



          std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });





          share|improve this answer



















          • 1




            Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
            – musiphil
            Nov 23 '11 at 7:31






          • 6




            The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
            – Charles L.
            Feb 4 '15 at 7:06






          • 1




            Does this include bessel's correction?
            – SmallChess
            Apr 22 '15 at 12:21








          • 2




            @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
            – musiphil
            Apr 23 '15 at 7:25






          • 4




            Using std::inner_product for sum of squares is very neat.
            – Paul R
            May 6 '16 at 8:32


















          up vote
          54
          down vote













          If performance is important to you, and your compiler supports lambdas, the stdev calculation can be made faster and simpler: In tests with VS 2012 I've found that the following code is over 10 X quicker than the Boost code given in the chosen answer; it's also 5 X quicker than the safer version of the answer using standard libraries given by musiphil.



          Note I'm using sample standard deviation, so the below code gives slightly different results (Why there is a Minus One in Standard Deviations)



          double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
          double m = sum / v.size();

          double accum = 0.0;
          std::for_each (std::begin(v), std::end(v), [&](const double d) {
          accum += (d - m) * (d - m);
          });

          double stdev = sqrt(accum / (v.size()-1));





          share|improve this answer























          • Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
            – leemes
            Feb 3 '14 at 3:07






          • 2




            what is the difference between using std::end(v) instead of v.end()?
            – spurra
            May 3 '14 at 20:28






          • 3




            The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
            – pepr
            Jun 22 '14 at 19:16










          • Can you explain why is this faster?
            – dev_nut
            Feb 24 '15 at 18:25






          • 3




            Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
            – Josh Greifer
            Mar 4 '15 at 20:44


















          up vote
          1
          down vote













          My answer is similar as Josh Greifer but generalised to sample covariance. Sample variance is just sample covariance but with the two inputs identical. This includes Bessel's correlation.



              template <class Iter> typename Iter::value_type cov(const Iter &x, const Iter &y)
          {
          double sum_x = std::accumulate(std::begin(x), std::end(x), 0.0);
          double sum_y = std::accumulate(std::begin(y), std::end(y), 0.0);

          double mx = sum_x / x.size();
          double my = sum_y / y.size();

          double accum = 0.0;

          for (auto i = 0; i < x.size(); i++)
          {
          accum += (x.at(i) - mx) * (y.at(i) - my);
          }

          return accum / (x.size() - 1);
          }





          share|improve this answer






























            up vote
            1
            down vote













            Improving on the answer by musiphil, you can write a standard deviation function without the temporary vector diff, just using a single inner_product call with the C++11 lambda capabilities:



            double stddev(std::vector<double> const & func)
            {
            double mean = std::accumulate(func.begin(), func.end(), 0.0) / func.size();
            double sq_sum = std::inner_product(func.begin(), func.end(), func.begin(), 0.0,
            (double const & x, double const & y) { return x + y; },
            [mean](double const & x, double const & y) { return (x - mean)*(y - mean); });
            return sq_sum / ( func.size() - 1 );
            }


            I suspect doing the subtraction multiple times is cheaper than using up additional intermediate storage, and I think it is more readable, but I haven't tested the performance yet.






            share|improve this answer






























              up vote
              0
              down vote













              2x faster than the versions before mentioned - mostly because transform() and inner_product() loops are joined.
              Sorry about my shortcut/typedefs/macro: Flo = float. Cit = const iteration. CR const ref. VFlo - vector. Tested in VS2010



              Flo     stdDev2(VFlo CR crVec) {
              SZ n = crVec.size(); if (n < 2) return 0.0f;
              Flo fSqSum = 0.0f, fSum = 0.0f;
              Cit(VFlo, crVec) {
              Flo f = *cx;
              fSqSum += f * f;
              fSum += f;
              }
              Flo fSumSq = fSum * fSum;
              Flo fSumSqDivN = fSumSq / n;
              Flo fSubSqSum = fSqSum - fSumSqDivN;
              Flo preSqrt = fSubSqSum / (n-1);
              return sqrt(preSqrt);
              }





              share|improve this answer




























                up vote
                -3
                down vote













                Create your own container:



                template <class T>
                class statList : public std::list<T>
                {
                public:
                statList() : std::list<T>::list() {}
                ~statList() {}
                T mean() {
                return accumulate(begin(),end(),0.0)/size();
                }
                T stddev() {
                T diff_sum = 0;
                T m = mean();
                for(iterator it= begin(); it != end(); ++it)
                diff_sum += ((*it - m)*(*it -m));
                return diff_sum/size();
                }
                };


                It does have some limitations, but it works beautifully when you know what you are doing.






                share|improve this answer



















                • 3




                  To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                  – Konrad Rudolph
                  Dec 8 '16 at 10:31






                • 1




                  I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                  – Creat
                  May 5 '17 at 10:35


















                up vote
                -6
                down vote













                //means deviation in c++



                /A deviation that is a difference between an observed value and the true value of a quantity of interest (such as a population mean) is an error and a deviation that is the difference between the observed value and an estimate of the true value (such an estimate may be a sample mean) is a residual. These concepts are applicable for data at the interval and ratio levels of measurement./



                #include <iostream>
                #include <conio.h>
                using namespace std;

                /* run this program using the console pauser or add your own getch, system("pause") or input loop */

                int main(int argc, char** argv)
                {
                int i,cnt;
                cout<<"please inter count:t";
                cin>>cnt;
                float *num=new float [cnt];
                float *s=new float [cnt];
                float sum=0,ave,M,M_D;

                for(i=0;i<cnt;i++)
                {
                cin>>num[i];
                sum+=num[i];
                }
                ave=sum/cnt;
                for(i=0;i<cnt;i++)
                {
                s[i]=ave-num[i];
                if(s[i]<0)
                {
                s[i]=s[i]*(-1);
                }
                cout<<"n|ave - number| = "<<s[i];
                M+=s[i];
                }
                M_D=M/cnt;
                cout<<"nn Average: "<<ave;
                cout<<"n M.D(Mean Deviation): "<<M_D;
                getch();
                return 0;


                }






                share|improve this answer




















                  protected by Konrad Rudolph Dec 8 '16 at 10:32



                  Thank you for your interest in this question.
                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                  Would you like to answer one of these unanswered questions instead?














                  8 Answers
                  8






                  active

                  oldest

                  votes








                  8 Answers
                  8






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  44
                  down vote



                  accepted










                  Using accumulators is the way to compute means and standard deviations in Boost.



                  accumulator_set<double, stats<tag::variance> > acc;
                  for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));

                  cout << mean(acc) << endl;
                  cout << sqrt(variance(acc)) << endl;


                   






                  share|improve this answer



















                  • 4




                    Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
                    – panda-34
                    Dec 7 '15 at 13:36










                  • Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
                    – Kemin Zhou
                    May 11 '17 at 16:11















                  up vote
                  44
                  down vote



                  accepted










                  Using accumulators is the way to compute means and standard deviations in Boost.



                  accumulator_set<double, stats<tag::variance> > acc;
                  for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));

                  cout << mean(acc) << endl;
                  cout << sqrt(variance(acc)) << endl;


                   






                  share|improve this answer



















                  • 4




                    Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
                    – panda-34
                    Dec 7 '15 at 13:36










                  • Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
                    – Kemin Zhou
                    May 11 '17 at 16:11













                  up vote
                  44
                  down vote



                  accepted







                  up vote
                  44
                  down vote



                  accepted






                  Using accumulators is the way to compute means and standard deviations in Boost.



                  accumulator_set<double, stats<tag::variance> > acc;
                  for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));

                  cout << mean(acc) << endl;
                  cout << sqrt(variance(acc)) << endl;


                   






                  share|improve this answer














                  Using accumulators is the way to compute means and standard deviations in Boost.



                  accumulator_set<double, stats<tag::variance> > acc;
                  for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));

                  cout << mean(acc) << endl;
                  cout << sqrt(variance(acc)) << endl;


                   







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 31 '17 at 19:30









                  Morgoth

                  2,11232236




                  2,11232236










                  answered Sep 30 '11 at 22:48









                  David Nehme

                  17.7k565105




                  17.7k565105








                  • 4




                    Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
                    – panda-34
                    Dec 7 '15 at 13:36










                  • Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
                    – Kemin Zhou
                    May 11 '17 at 16:11














                  • 4




                    Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
                    – panda-34
                    Dec 7 '15 at 13:36










                  • Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
                    – Kemin Zhou
                    May 11 '17 at 16:11








                  4




                  4




                  Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
                  – panda-34
                  Dec 7 '15 at 13:36




                  Note, that tag::variance calculates variance by an approximate formula. tag::variance(lazy) calculates by an exact formula, specifically: second moment - squared mean which will produce incorrect result if variance is very small because of rounding errors. It can actually produce negative variance.
                  – panda-34
                  Dec 7 '15 at 13:36












                  Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
                  – Kemin Zhou
                  May 11 '17 at 16:11




                  Use the recursive (online) algorithm if you know you are going to have a lots of numbers. This will take care of both under and overflow problems.
                  – Kemin Zhou
                  May 11 '17 at 16:11












                  up vote
                  181
                  down vote













                  I don't know if Boost has more specific functions, but you can do it with the standard library.



                  Given std::vector<double> v, this is the naive way:



                  #include <numeric>

                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size() - mean * mean);


                  This is susceptible to overflow or underflow for huge or tiny values. A slightly better way to calculate the standard deviation is:



                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  std::vector<double> diff(v.size());
                  std::transform(v.begin(), v.end(), diff.begin(),
                  std::bind2nd(std::minus<double>(), mean));
                  double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size());


                  UPDATE for C++11:



                  The call to std::transform can be written using a lambda function instead of std::minus and std::bind2nd(now deprecated):



                  std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });





                  share|improve this answer



















                  • 1




                    Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
                    – musiphil
                    Nov 23 '11 at 7:31






                  • 6




                    The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
                    – Charles L.
                    Feb 4 '15 at 7:06






                  • 1




                    Does this include bessel's correction?
                    – SmallChess
                    Apr 22 '15 at 12:21








                  • 2




                    @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
                    – musiphil
                    Apr 23 '15 at 7:25






                  • 4




                    Using std::inner_product for sum of squares is very neat.
                    – Paul R
                    May 6 '16 at 8:32















                  up vote
                  181
                  down vote













                  I don't know if Boost has more specific functions, but you can do it with the standard library.



                  Given std::vector<double> v, this is the naive way:



                  #include <numeric>

                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size() - mean * mean);


                  This is susceptible to overflow or underflow for huge or tiny values. A slightly better way to calculate the standard deviation is:



                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  std::vector<double> diff(v.size());
                  std::transform(v.begin(), v.end(), diff.begin(),
                  std::bind2nd(std::minus<double>(), mean));
                  double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size());


                  UPDATE for C++11:



                  The call to std::transform can be written using a lambda function instead of std::minus and std::bind2nd(now deprecated):



                  std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });





                  share|improve this answer



















                  • 1




                    Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
                    – musiphil
                    Nov 23 '11 at 7:31






                  • 6




                    The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
                    – Charles L.
                    Feb 4 '15 at 7:06






                  • 1




                    Does this include bessel's correction?
                    – SmallChess
                    Apr 22 '15 at 12:21








                  • 2




                    @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
                    – musiphil
                    Apr 23 '15 at 7:25






                  • 4




                    Using std::inner_product for sum of squares is very neat.
                    – Paul R
                    May 6 '16 at 8:32













                  up vote
                  181
                  down vote










                  up vote
                  181
                  down vote









                  I don't know if Boost has more specific functions, but you can do it with the standard library.



                  Given std::vector<double> v, this is the naive way:



                  #include <numeric>

                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size() - mean * mean);


                  This is susceptible to overflow or underflow for huge or tiny values. A slightly better way to calculate the standard deviation is:



                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  std::vector<double> diff(v.size());
                  std::transform(v.begin(), v.end(), diff.begin(),
                  std::bind2nd(std::minus<double>(), mean));
                  double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size());


                  UPDATE for C++11:



                  The call to std::transform can be written using a lambda function instead of std::minus and std::bind2nd(now deprecated):



                  std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });





                  share|improve this answer














                  I don't know if Boost has more specific functions, but you can do it with the standard library.



                  Given std::vector<double> v, this is the naive way:



                  #include <numeric>

                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size() - mean * mean);


                  This is susceptible to overflow or underflow for huge or tiny values. A slightly better way to calculate the standard deviation is:



                  double sum = std::accumulate(v.begin(), v.end(), 0.0);
                  double mean = sum / v.size();

                  std::vector<double> diff(v.size());
                  std::transform(v.begin(), v.end(), diff.begin(),
                  std::bind2nd(std::minus<double>(), mean));
                  double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
                  double stdev = std::sqrt(sq_sum / v.size());


                  UPDATE for C++11:



                  The call to std::transform can be written using a lambda function instead of std::minus and std::bind2nd(now deprecated):



                  std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 28 '17 at 20:30









                  markgz

                  5,25911435




                  5,25911435










                  answered Sep 30 '11 at 22:42









                  musiphil

                  2,68721322




                  2,68721322








                  • 1




                    Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
                    – musiphil
                    Nov 23 '11 at 7:31






                  • 6




                    The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
                    – Charles L.
                    Feb 4 '15 at 7:06






                  • 1




                    Does this include bessel's correction?
                    – SmallChess
                    Apr 22 '15 at 12:21








                  • 2




                    @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
                    – musiphil
                    Apr 23 '15 at 7:25






                  • 4




                    Using std::inner_product for sum of squares is very neat.
                    – Paul R
                    May 6 '16 at 8:32














                  • 1




                    Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
                    – musiphil
                    Nov 23 '11 at 7:31






                  • 6




                    The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
                    – Charles L.
                    Feb 4 '15 at 7:06






                  • 1




                    Does this include bessel's correction?
                    – SmallChess
                    Apr 22 '15 at 12:21








                  • 2




                    @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
                    – musiphil
                    Apr 23 '15 at 7:25






                  • 4




                    Using std::inner_product for sum of squares is very neat.
                    – Paul R
                    May 6 '16 at 8:32








                  1




                  1




                  Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
                  – musiphil
                  Nov 23 '11 at 7:31




                  Yes; obviously, the bottom part depends on the value of mean calculated in the top part.
                  – musiphil
                  Nov 23 '11 at 7:31




                  6




                  6




                  The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
                  – Charles L.
                  Feb 4 '15 at 7:06




                  The first set of equations does not work. I put int 10 & 2, and got an output of 4. At a glance I think it's b/c it assumes that (a-b)^2 = a^2-b^2
                  – Charles L.
                  Feb 4 '15 at 7:06




                  1




                  1




                  Does this include bessel's correction?
                  – SmallChess
                  Apr 22 '15 at 12:21






                  Does this include bessel's correction?
                  – SmallChess
                  Apr 22 '15 at 12:21






                  2




                  2




                  @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
                  – musiphil
                  Apr 23 '15 at 7:25




                  @StudentT: No, but you can substitute (v.size() - 1) for v.size() in the last line above: std::sqrt(sq_sum / (v.size() - 1)). (For the first method, it's a little complicated: std::sqrt(sq_sum / (v.size() - 1) - mean * mean * v.size() / (v.size() - 1)).
                  – musiphil
                  Apr 23 '15 at 7:25




                  4




                  4




                  Using std::inner_product for sum of squares is very neat.
                  – Paul R
                  May 6 '16 at 8:32




                  Using std::inner_product for sum of squares is very neat.
                  – Paul R
                  May 6 '16 at 8:32










                  up vote
                  54
                  down vote













                  If performance is important to you, and your compiler supports lambdas, the stdev calculation can be made faster and simpler: In tests with VS 2012 I've found that the following code is over 10 X quicker than the Boost code given in the chosen answer; it's also 5 X quicker than the safer version of the answer using standard libraries given by musiphil.



                  Note I'm using sample standard deviation, so the below code gives slightly different results (Why there is a Minus One in Standard Deviations)



                  double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
                  double m = sum / v.size();

                  double accum = 0.0;
                  std::for_each (std::begin(v), std::end(v), [&](const double d) {
                  accum += (d - m) * (d - m);
                  });

                  double stdev = sqrt(accum / (v.size()-1));





                  share|improve this answer























                  • Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
                    – leemes
                    Feb 3 '14 at 3:07






                  • 2




                    what is the difference between using std::end(v) instead of v.end()?
                    – spurra
                    May 3 '14 at 20:28






                  • 3




                    The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
                    – pepr
                    Jun 22 '14 at 19:16










                  • Can you explain why is this faster?
                    – dev_nut
                    Feb 24 '15 at 18:25






                  • 3




                    Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
                    – Josh Greifer
                    Mar 4 '15 at 20:44















                  up vote
                  54
                  down vote













                  If performance is important to you, and your compiler supports lambdas, the stdev calculation can be made faster and simpler: In tests with VS 2012 I've found that the following code is over 10 X quicker than the Boost code given in the chosen answer; it's also 5 X quicker than the safer version of the answer using standard libraries given by musiphil.



                  Note I'm using sample standard deviation, so the below code gives slightly different results (Why there is a Minus One in Standard Deviations)



                  double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
                  double m = sum / v.size();

                  double accum = 0.0;
                  std::for_each (std::begin(v), std::end(v), [&](const double d) {
                  accum += (d - m) * (d - m);
                  });

                  double stdev = sqrt(accum / (v.size()-1));





                  share|improve this answer























                  • Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
                    – leemes
                    Feb 3 '14 at 3:07






                  • 2




                    what is the difference between using std::end(v) instead of v.end()?
                    – spurra
                    May 3 '14 at 20:28






                  • 3




                    The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
                    – pepr
                    Jun 22 '14 at 19:16










                  • Can you explain why is this faster?
                    – dev_nut
                    Feb 24 '15 at 18:25






                  • 3




                    Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
                    – Josh Greifer
                    Mar 4 '15 at 20:44













                  up vote
                  54
                  down vote










                  up vote
                  54
                  down vote









                  If performance is important to you, and your compiler supports lambdas, the stdev calculation can be made faster and simpler: In tests with VS 2012 I've found that the following code is over 10 X quicker than the Boost code given in the chosen answer; it's also 5 X quicker than the safer version of the answer using standard libraries given by musiphil.



                  Note I'm using sample standard deviation, so the below code gives slightly different results (Why there is a Minus One in Standard Deviations)



                  double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
                  double m = sum / v.size();

                  double accum = 0.0;
                  std::for_each (std::begin(v), std::end(v), [&](const double d) {
                  accum += (d - m) * (d - m);
                  });

                  double stdev = sqrt(accum / (v.size()-1));





                  share|improve this answer














                  If performance is important to you, and your compiler supports lambdas, the stdev calculation can be made faster and simpler: In tests with VS 2012 I've found that the following code is over 10 X quicker than the Boost code given in the chosen answer; it's also 5 X quicker than the safer version of the answer using standard libraries given by musiphil.



                  Note I'm using sample standard deviation, so the below code gives slightly different results (Why there is a Minus One in Standard Deviations)



                  double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
                  double m = sum / v.size();

                  double accum = 0.0;
                  std::for_each (std::begin(v), std::end(v), [&](const double d) {
                  accum += (d - m) * (d - m);
                  });

                  double stdev = sqrt(accum / (v.size()-1));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 24 '15 at 9:43

























                  answered Sep 13 '12 at 12:01









                  Josh Greifer

                  2,4051522




                  2,4051522












                  • Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
                    – leemes
                    Feb 3 '14 at 3:07






                  • 2




                    what is the difference between using std::end(v) instead of v.end()?
                    – spurra
                    May 3 '14 at 20:28






                  • 3




                    The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
                    – pepr
                    Jun 22 '14 at 19:16










                  • Can you explain why is this faster?
                    – dev_nut
                    Feb 24 '15 at 18:25






                  • 3




                    Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
                    – Josh Greifer
                    Mar 4 '15 at 20:44


















                  • Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
                    – leemes
                    Feb 3 '14 at 3:07






                  • 2




                    what is the difference between using std::end(v) instead of v.end()?
                    – spurra
                    May 3 '14 at 20:28






                  • 3




                    The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
                    – pepr
                    Jun 22 '14 at 19:16










                  • Can you explain why is this faster?
                    – dev_nut
                    Feb 24 '15 at 18:25






                  • 3




                    Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
                    – Josh Greifer
                    Mar 4 '15 at 20:44
















                  Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
                  – leemes
                  Feb 3 '14 at 3:07




                  Thanks for sharing this answer even a year later. Now I come another year later and made this one generic for both the value type and the container type. See here (Note: I guess that my range-based for loop is as fast as your lambda code.)
                  – leemes
                  Feb 3 '14 at 3:07




                  2




                  2




                  what is the difference between using std::end(v) instead of v.end()?
                  – spurra
                  May 3 '14 at 20:28




                  what is the difference between using std::end(v) instead of v.end()?
                  – spurra
                  May 3 '14 at 20:28




                  3




                  3




                  The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
                  – pepr
                  Jun 22 '14 at 19:16




                  The std::end() function was added by C++11 standard for cases when there is nothing like v.end(). The std::end can be overloaded for the less standard container -- see en.cppreference.com/w/cpp/iterator/end
                  – pepr
                  Jun 22 '14 at 19:16












                  Can you explain why is this faster?
                  – dev_nut
                  Feb 24 '15 at 18:25




                  Can you explain why is this faster?
                  – dev_nut
                  Feb 24 '15 at 18:25




                  3




                  3




                  Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
                  – Josh Greifer
                  Mar 4 '15 at 20:44




                  Well for one thing, the "safe" answer (which is like my answer) makes 3 passes through the array: Once for the sum, once for the diff-mean, and once for the squaring. In my code there's only 2 passes -- It's conflating the second two passes into one. And (when I last looked, quite a while ago now!) the inner_product calls were not optimized away. In addition the "safe" code copies v into an entirely new array of diffs, which adds more delay. In my opinion my code is more readable too - and is easily ported to JavaScript and other languages :)
                  – Josh Greifer
                  Mar 4 '15 at 20:44










                  up vote
                  1
                  down vote













                  My answer is similar as Josh Greifer but generalised to sample covariance. Sample variance is just sample covariance but with the two inputs identical. This includes Bessel's correlation.



                      template <class Iter> typename Iter::value_type cov(const Iter &x, const Iter &y)
                  {
                  double sum_x = std::accumulate(std::begin(x), std::end(x), 0.0);
                  double sum_y = std::accumulate(std::begin(y), std::end(y), 0.0);

                  double mx = sum_x / x.size();
                  double my = sum_y / y.size();

                  double accum = 0.0;

                  for (auto i = 0; i < x.size(); i++)
                  {
                  accum += (x.at(i) - mx) * (y.at(i) - my);
                  }

                  return accum / (x.size() - 1);
                  }





                  share|improve this answer



























                    up vote
                    1
                    down vote













                    My answer is similar as Josh Greifer but generalised to sample covariance. Sample variance is just sample covariance but with the two inputs identical. This includes Bessel's correlation.



                        template <class Iter> typename Iter::value_type cov(const Iter &x, const Iter &y)
                    {
                    double sum_x = std::accumulate(std::begin(x), std::end(x), 0.0);
                    double sum_y = std::accumulate(std::begin(y), std::end(y), 0.0);

                    double mx = sum_x / x.size();
                    double my = sum_y / y.size();

                    double accum = 0.0;

                    for (auto i = 0; i < x.size(); i++)
                    {
                    accum += (x.at(i) - mx) * (y.at(i) - my);
                    }

                    return accum / (x.size() - 1);
                    }





                    share|improve this answer

























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      My answer is similar as Josh Greifer but generalised to sample covariance. Sample variance is just sample covariance but with the two inputs identical. This includes Bessel's correlation.



                          template <class Iter> typename Iter::value_type cov(const Iter &x, const Iter &y)
                      {
                      double sum_x = std::accumulate(std::begin(x), std::end(x), 0.0);
                      double sum_y = std::accumulate(std::begin(y), std::end(y), 0.0);

                      double mx = sum_x / x.size();
                      double my = sum_y / y.size();

                      double accum = 0.0;

                      for (auto i = 0; i < x.size(); i++)
                      {
                      accum += (x.at(i) - mx) * (y.at(i) - my);
                      }

                      return accum / (x.size() - 1);
                      }





                      share|improve this answer














                      My answer is similar as Josh Greifer but generalised to sample covariance. Sample variance is just sample covariance but with the two inputs identical. This includes Bessel's correlation.



                          template <class Iter> typename Iter::value_type cov(const Iter &x, const Iter &y)
                      {
                      double sum_x = std::accumulate(std::begin(x), std::end(x), 0.0);
                      double sum_y = std::accumulate(std::begin(y), std::end(y), 0.0);

                      double mx = sum_x / x.size();
                      double my = sum_y / y.size();

                      double accum = 0.0;

                      for (auto i = 0; i < x.size(); i++)
                      {
                      accum += (x.at(i) - mx) * (y.at(i) - my);
                      }

                      return accum / (x.size() - 1);
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 14 '15 at 0:26

























                      answered Apr 22 '15 at 12:38









                      SmallChess

                      4,88073764




                      4,88073764






















                          up vote
                          1
                          down vote













                          Improving on the answer by musiphil, you can write a standard deviation function without the temporary vector diff, just using a single inner_product call with the C++11 lambda capabilities:



                          double stddev(std::vector<double> const & func)
                          {
                          double mean = std::accumulate(func.begin(), func.end(), 0.0) / func.size();
                          double sq_sum = std::inner_product(func.begin(), func.end(), func.begin(), 0.0,
                          (double const & x, double const & y) { return x + y; },
                          [mean](double const & x, double const & y) { return (x - mean)*(y - mean); });
                          return sq_sum / ( func.size() - 1 );
                          }


                          I suspect doing the subtraction multiple times is cheaper than using up additional intermediate storage, and I think it is more readable, but I haven't tested the performance yet.






                          share|improve this answer



























                            up vote
                            1
                            down vote













                            Improving on the answer by musiphil, you can write a standard deviation function without the temporary vector diff, just using a single inner_product call with the C++11 lambda capabilities:



                            double stddev(std::vector<double> const & func)
                            {
                            double mean = std::accumulate(func.begin(), func.end(), 0.0) / func.size();
                            double sq_sum = std::inner_product(func.begin(), func.end(), func.begin(), 0.0,
                            (double const & x, double const & y) { return x + y; },
                            [mean](double const & x, double const & y) { return (x - mean)*(y - mean); });
                            return sq_sum / ( func.size() - 1 );
                            }


                            I suspect doing the subtraction multiple times is cheaper than using up additional intermediate storage, and I think it is more readable, but I haven't tested the performance yet.






                            share|improve this answer

























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Improving on the answer by musiphil, you can write a standard deviation function without the temporary vector diff, just using a single inner_product call with the C++11 lambda capabilities:



                              double stddev(std::vector<double> const & func)
                              {
                              double mean = std::accumulate(func.begin(), func.end(), 0.0) / func.size();
                              double sq_sum = std::inner_product(func.begin(), func.end(), func.begin(), 0.0,
                              (double const & x, double const & y) { return x + y; },
                              [mean](double const & x, double const & y) { return (x - mean)*(y - mean); });
                              return sq_sum / ( func.size() - 1 );
                              }


                              I suspect doing the subtraction multiple times is cheaper than using up additional intermediate storage, and I think it is more readable, but I haven't tested the performance yet.






                              share|improve this answer














                              Improving on the answer by musiphil, you can write a standard deviation function without the temporary vector diff, just using a single inner_product call with the C++11 lambda capabilities:



                              double stddev(std::vector<double> const & func)
                              {
                              double mean = std::accumulate(func.begin(), func.end(), 0.0) / func.size();
                              double sq_sum = std::inner_product(func.begin(), func.end(), func.begin(), 0.0,
                              (double const & x, double const & y) { return x + y; },
                              [mean](double const & x, double const & y) { return (x - mean)*(y - mean); });
                              return sq_sum / ( func.size() - 1 );
                              }


                              I suspect doing the subtraction multiple times is cheaper than using up additional intermediate storage, and I think it is more readable, but I haven't tested the performance yet.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 21 at 13:25

























                              answered Aug 13 at 13:31









                              codeling

                              8,13432556




                              8,13432556






















                                  up vote
                                  0
                                  down vote













                                  2x faster than the versions before mentioned - mostly because transform() and inner_product() loops are joined.
                                  Sorry about my shortcut/typedefs/macro: Flo = float. Cit = const iteration. CR const ref. VFlo - vector. Tested in VS2010



                                  Flo     stdDev2(VFlo CR crVec) {
                                  SZ n = crVec.size(); if (n < 2) return 0.0f;
                                  Flo fSqSum = 0.0f, fSum = 0.0f;
                                  Cit(VFlo, crVec) {
                                  Flo f = *cx;
                                  fSqSum += f * f;
                                  fSum += f;
                                  }
                                  Flo fSumSq = fSum * fSum;
                                  Flo fSumSqDivN = fSumSq / n;
                                  Flo fSubSqSum = fSqSum - fSumSqDivN;
                                  Flo preSqrt = fSubSqSum / (n-1);
                                  return sqrt(preSqrt);
                                  }





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    2x faster than the versions before mentioned - mostly because transform() and inner_product() loops are joined.
                                    Sorry about my shortcut/typedefs/macro: Flo = float. Cit = const iteration. CR const ref. VFlo - vector. Tested in VS2010



                                    Flo     stdDev2(VFlo CR crVec) {
                                    SZ n = crVec.size(); if (n < 2) return 0.0f;
                                    Flo fSqSum = 0.0f, fSum = 0.0f;
                                    Cit(VFlo, crVec) {
                                    Flo f = *cx;
                                    fSqSum += f * f;
                                    fSum += f;
                                    }
                                    Flo fSumSq = fSum * fSum;
                                    Flo fSumSqDivN = fSumSq / n;
                                    Flo fSubSqSum = fSqSum - fSumSqDivN;
                                    Flo preSqrt = fSubSqSum / (n-1);
                                    return sqrt(preSqrt);
                                    }





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      2x faster than the versions before mentioned - mostly because transform() and inner_product() loops are joined.
                                      Sorry about my shortcut/typedefs/macro: Flo = float. Cit = const iteration. CR const ref. VFlo - vector. Tested in VS2010



                                      Flo     stdDev2(VFlo CR crVec) {
                                      SZ n = crVec.size(); if (n < 2) return 0.0f;
                                      Flo fSqSum = 0.0f, fSum = 0.0f;
                                      Cit(VFlo, crVec) {
                                      Flo f = *cx;
                                      fSqSum += f * f;
                                      fSum += f;
                                      }
                                      Flo fSumSq = fSum * fSum;
                                      Flo fSumSqDivN = fSumSq / n;
                                      Flo fSubSqSum = fSqSum - fSumSqDivN;
                                      Flo preSqrt = fSubSqSum / (n-1);
                                      return sqrt(preSqrt);
                                      }





                                      share|improve this answer












                                      2x faster than the versions before mentioned - mostly because transform() and inner_product() loops are joined.
                                      Sorry about my shortcut/typedefs/macro: Flo = float. Cit = const iteration. CR const ref. VFlo - vector. Tested in VS2010



                                      Flo     stdDev2(VFlo CR crVec) {
                                      SZ n = crVec.size(); if (n < 2) return 0.0f;
                                      Flo fSqSum = 0.0f, fSum = 0.0f;
                                      Cit(VFlo, crVec) {
                                      Flo f = *cx;
                                      fSqSum += f * f;
                                      fSum += f;
                                      }
                                      Flo fSumSq = fSum * fSum;
                                      Flo fSumSqDivN = fSumSq / n;
                                      Flo fSubSqSum = fSqSum - fSumSqDivN;
                                      Flo preSqrt = fSubSqSum / (n-1);
                                      return sqrt(preSqrt);
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Oct 25 '17 at 2:46









                                      slyy2048

                                      17239




                                      17239






















                                          up vote
                                          -3
                                          down vote













                                          Create your own container:



                                          template <class T>
                                          class statList : public std::list<T>
                                          {
                                          public:
                                          statList() : std::list<T>::list() {}
                                          ~statList() {}
                                          T mean() {
                                          return accumulate(begin(),end(),0.0)/size();
                                          }
                                          T stddev() {
                                          T diff_sum = 0;
                                          T m = mean();
                                          for(iterator it= begin(); it != end(); ++it)
                                          diff_sum += ((*it - m)*(*it -m));
                                          return diff_sum/size();
                                          }
                                          };


                                          It does have some limitations, but it works beautifully when you know what you are doing.






                                          share|improve this answer



















                                          • 3




                                            To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                                            – Konrad Rudolph
                                            Dec 8 '16 at 10:31






                                          • 1




                                            I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                                            – Creat
                                            May 5 '17 at 10:35















                                          up vote
                                          -3
                                          down vote













                                          Create your own container:



                                          template <class T>
                                          class statList : public std::list<T>
                                          {
                                          public:
                                          statList() : std::list<T>::list() {}
                                          ~statList() {}
                                          T mean() {
                                          return accumulate(begin(),end(),0.0)/size();
                                          }
                                          T stddev() {
                                          T diff_sum = 0;
                                          T m = mean();
                                          for(iterator it= begin(); it != end(); ++it)
                                          diff_sum += ((*it - m)*(*it -m));
                                          return diff_sum/size();
                                          }
                                          };


                                          It does have some limitations, but it works beautifully when you know what you are doing.






                                          share|improve this answer



















                                          • 3




                                            To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                                            – Konrad Rudolph
                                            Dec 8 '16 at 10:31






                                          • 1




                                            I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                                            – Creat
                                            May 5 '17 at 10:35













                                          up vote
                                          -3
                                          down vote










                                          up vote
                                          -3
                                          down vote









                                          Create your own container:



                                          template <class T>
                                          class statList : public std::list<T>
                                          {
                                          public:
                                          statList() : std::list<T>::list() {}
                                          ~statList() {}
                                          T mean() {
                                          return accumulate(begin(),end(),0.0)/size();
                                          }
                                          T stddev() {
                                          T diff_sum = 0;
                                          T m = mean();
                                          for(iterator it= begin(); it != end(); ++it)
                                          diff_sum += ((*it - m)*(*it -m));
                                          return diff_sum/size();
                                          }
                                          };


                                          It does have some limitations, but it works beautifully when you know what you are doing.






                                          share|improve this answer














                                          Create your own container:



                                          template <class T>
                                          class statList : public std::list<T>
                                          {
                                          public:
                                          statList() : std::list<T>::list() {}
                                          ~statList() {}
                                          T mean() {
                                          return accumulate(begin(),end(),0.0)/size();
                                          }
                                          T stddev() {
                                          T diff_sum = 0;
                                          T m = mean();
                                          for(iterator it= begin(); it != end(); ++it)
                                          diff_sum += ((*it - m)*(*it -m));
                                          return diff_sum/size();
                                          }
                                          };


                                          It does have some limitations, but it works beautifully when you know what you are doing.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jan 31 '17 at 14:03









                                          codeling

                                          8,13432556




                                          8,13432556










                                          answered Aug 8 '16 at 22:50









                                          Sushant Kondguli

                                          112




                                          112








                                          • 3




                                            To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                                            – Konrad Rudolph
                                            Dec 8 '16 at 10:31






                                          • 1




                                            I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                                            – Creat
                                            May 5 '17 at 10:35














                                          • 3




                                            To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                                            – Konrad Rudolph
                                            Dec 8 '16 at 10:31






                                          • 1




                                            I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                                            – Creat
                                            May 5 '17 at 10:35








                                          3




                                          3




                                          To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                                          – Konrad Rudolph
                                          Dec 8 '16 at 10:31




                                          To answer the question: because there’s absolutely no need. Creating your own container has absolutely no benefits compared to writing a free function.
                                          – Konrad Rudolph
                                          Dec 8 '16 at 10:31




                                          1




                                          1




                                          I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                                          – Creat
                                          May 5 '17 at 10:35




                                          I don't even know where to start with this. You're using a list as the underlying data structure, you don't even cache the values, which would be one of the few reasons I can think of to use a container-like structure. Especially if the values chance infrequently and the mean/stddev are needed often.
                                          – Creat
                                          May 5 '17 at 10:35










                                          up vote
                                          -6
                                          down vote













                                          //means deviation in c++



                                          /A deviation that is a difference between an observed value and the true value of a quantity of interest (such as a population mean) is an error and a deviation that is the difference between the observed value and an estimate of the true value (such an estimate may be a sample mean) is a residual. These concepts are applicable for data at the interval and ratio levels of measurement./



                                          #include <iostream>
                                          #include <conio.h>
                                          using namespace std;

                                          /* run this program using the console pauser or add your own getch, system("pause") or input loop */

                                          int main(int argc, char** argv)
                                          {
                                          int i,cnt;
                                          cout<<"please inter count:t";
                                          cin>>cnt;
                                          float *num=new float [cnt];
                                          float *s=new float [cnt];
                                          float sum=0,ave,M,M_D;

                                          for(i=0;i<cnt;i++)
                                          {
                                          cin>>num[i];
                                          sum+=num[i];
                                          }
                                          ave=sum/cnt;
                                          for(i=0;i<cnt;i++)
                                          {
                                          s[i]=ave-num[i];
                                          if(s[i]<0)
                                          {
                                          s[i]=s[i]*(-1);
                                          }
                                          cout<<"n|ave - number| = "<<s[i];
                                          M+=s[i];
                                          }
                                          M_D=M/cnt;
                                          cout<<"nn Average: "<<ave;
                                          cout<<"n M.D(Mean Deviation): "<<M_D;
                                          getch();
                                          return 0;


                                          }






                                          share|improve this answer

























                                            up vote
                                            -6
                                            down vote













                                            //means deviation in c++



                                            /A deviation that is a difference between an observed value and the true value of a quantity of interest (such as a population mean) is an error and a deviation that is the difference between the observed value and an estimate of the true value (such an estimate may be a sample mean) is a residual. These concepts are applicable for data at the interval and ratio levels of measurement./



                                            #include <iostream>
                                            #include <conio.h>
                                            using namespace std;

                                            /* run this program using the console pauser or add your own getch, system("pause") or input loop */

                                            int main(int argc, char** argv)
                                            {
                                            int i,cnt;
                                            cout<<"please inter count:t";
                                            cin>>cnt;
                                            float *num=new float [cnt];
                                            float *s=new float [cnt];
                                            float sum=0,ave,M,M_D;

                                            for(i=0;i<cnt;i++)
                                            {
                                            cin>>num[i];
                                            sum+=num[i];
                                            }
                                            ave=sum/cnt;
                                            for(i=0;i<cnt;i++)
                                            {
                                            s[i]=ave-num[i];
                                            if(s[i]<0)
                                            {
                                            s[i]=s[i]*(-1);
                                            }
                                            cout<<"n|ave - number| = "<<s[i];
                                            M+=s[i];
                                            }
                                            M_D=M/cnt;
                                            cout<<"nn Average: "<<ave;
                                            cout<<"n M.D(Mean Deviation): "<<M_D;
                                            getch();
                                            return 0;


                                            }






                                            share|improve this answer























                                              up vote
                                              -6
                                              down vote










                                              up vote
                                              -6
                                              down vote









                                              //means deviation in c++



                                              /A deviation that is a difference between an observed value and the true value of a quantity of interest (such as a population mean) is an error and a deviation that is the difference between the observed value and an estimate of the true value (such an estimate may be a sample mean) is a residual. These concepts are applicable for data at the interval and ratio levels of measurement./



                                              #include <iostream>
                                              #include <conio.h>
                                              using namespace std;

                                              /* run this program using the console pauser or add your own getch, system("pause") or input loop */

                                              int main(int argc, char** argv)
                                              {
                                              int i,cnt;
                                              cout<<"please inter count:t";
                                              cin>>cnt;
                                              float *num=new float [cnt];
                                              float *s=new float [cnt];
                                              float sum=0,ave,M,M_D;

                                              for(i=0;i<cnt;i++)
                                              {
                                              cin>>num[i];
                                              sum+=num[i];
                                              }
                                              ave=sum/cnt;
                                              for(i=0;i<cnt;i++)
                                              {
                                              s[i]=ave-num[i];
                                              if(s[i]<0)
                                              {
                                              s[i]=s[i]*(-1);
                                              }
                                              cout<<"n|ave - number| = "<<s[i];
                                              M+=s[i];
                                              }
                                              M_D=M/cnt;
                                              cout<<"nn Average: "<<ave;
                                              cout<<"n M.D(Mean Deviation): "<<M_D;
                                              getch();
                                              return 0;


                                              }






                                              share|improve this answer












                                              //means deviation in c++



                                              /A deviation that is a difference between an observed value and the true value of a quantity of interest (such as a population mean) is an error and a deviation that is the difference between the observed value and an estimate of the true value (such an estimate may be a sample mean) is a residual. These concepts are applicable for data at the interval and ratio levels of measurement./



                                              #include <iostream>
                                              #include <conio.h>
                                              using namespace std;

                                              /* run this program using the console pauser or add your own getch, system("pause") or input loop */

                                              int main(int argc, char** argv)
                                              {
                                              int i,cnt;
                                              cout<<"please inter count:t";
                                              cin>>cnt;
                                              float *num=new float [cnt];
                                              float *s=new float [cnt];
                                              float sum=0,ave,M,M_D;

                                              for(i=0;i<cnt;i++)
                                              {
                                              cin>>num[i];
                                              sum+=num[i];
                                              }
                                              ave=sum/cnt;
                                              for(i=0;i<cnt;i++)
                                              {
                                              s[i]=ave-num[i];
                                              if(s[i]<0)
                                              {
                                              s[i]=s[i]*(-1);
                                              }
                                              cout<<"n|ave - number| = "<<s[i];
                                              M+=s[i];
                                              }
                                              M_D=M/cnt;
                                              cout<<"nn Average: "<<ave;
                                              cout<<"n M.D(Mean Deviation): "<<M_D;
                                              getch();
                                              return 0;


                                              }







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 7 '16 at 8:24









                                              ali

                                              11




                                              11

















                                                  protected by Konrad Rudolph Dec 8 '16 at 10:32



                                                  Thank you for your interest in this question.
                                                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                  Would you like to answer one of these unanswered questions instead?



                                                  Popular posts from this blog

                                                  Contact image not getting when fetch all contact list from iPhone by CNContact

                                                  count number of partitions of a set with n elements into k subsets

                                                  A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks