-3

I need to calculate the mean, median and s.d. of the values inside the vector. I can sort the vector to find out the median but is there an easier way to find the mean and standard deviation rather than adding stuff up?

user2117875
  • 99
  • 1
  • 1
  • 7
  • 1
    I had thought twice before saying this: what have you tried? – Mark Garcia Feb 28 '13 at 03:16
  • There's an `accumulate` algorithm. – chris Feb 28 '13 at 03:17
  • 3
    What is difficult about "adding stuff up" exactly? – Ed S. Feb 28 '13 at 03:18
  • Sorting `O(N log N)` will take longer than adding stuff up `O(N)`. However, to find the median you don't need a full sort, see http://stackoverflow.com/questions/1719070/what-is-the-right-approach-when-using-stl-container-for-median-calculation – amdn Feb 28 '13 at 03:21

3 Answers3

1

You can find the median with std::nth_element. Contrary to (apparently) popular belief, this is normally faster than sorting, then finding the middle element -- it's normally O(N) (linear) where sorting is normally O(N log N).

To add the elements for the mean, you can ust std::accumulate, something like:

 double total = std::accumulate(std::begin(v), std::end(v), 0.0);

[Note: depending on how old your compiler is, you may need to use v.begin() and v.end() instead of begin(v) andend(v)`). ]

Computing the variance has been covered in a previous question. The standard deviation is simply the square root of the variance.

Community
  • 1
  • 1
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
0

In order to find the mean, you're simply going to have to add the vector contents up. You can find the median without actually sorting the vector first, but an algorithm for calculating the median on an unsorted vector would almost certainly be much more complex than if it's sorted. Also, I pretty sure that if you calculate the time to find the median on an unsorted vector, it's almost certainly going to be more than the combined time of sorting and extracting the median. (if you're doing it for just the technical challenge, I'll write one for you...)

Since you're probably going to have to sort the vector, you could calculate the mean whilst you're sorting.

The Welder
  • 725
  • 5
  • 21
  • You lost me here... `if you calculate the time to find the median on an unsorted vector, it's almost certainly going to be more than the combined time of sorting and extracting the median` – amdn Feb 28 '13 at 03:27
0

EDIT: Didn't see the C++ tag!

If you are using a language that offers functional programming tools, you can foldl the vector with the + function and divide by its length for mean.

For stddev, you can use a lambda : x -> (x - mean)^2 and fold the result with a +.

It's not more computationally efficient, but it probably saves a lot in developer time!

David
  • 305
  • 1
  • 3
  • 8
  • Info on the relevant higher order functions in C++: http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29#Folds_in_various_languages http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B http://en.wikipedia.org/wiki/Map_%28higher-order_function%29#Language_comparison – David Feb 28 '13 at 03:45