Created
April 18, 2014 14:13
-
-
Save kdungs/11046314 to your computer and use it in GitHub Desktop.
Calculate mean and standard error of the mean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
std::pair<T, T> MeanAndError(const std::vector<T> &v) { | |
using namespace std; | |
T mean = accumulate(begin(v), end(v), T(0)) / v.size(); | |
vector<T> diff(v.size()); | |
transform(begin(v), end(v), begin(diff), std::bind2nd(minus<T>(), mean)); | |
T sq_sum = inner_product(begin(diff), end(diff), begin(diff), T(0)); | |
T sem = sqrt(sq_sum / (v.size() * (v.size() - 1))); | |
return make_pair(mean, sem); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment