/Users/deen/code/yugabyte-db/src/yb/util/math_util.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/util/math_util.h" |
15 | | |
16 | | #include <algorithm> |
17 | | #include <cmath> |
18 | | #include <numeric> |
19 | | #include <vector> |
20 | | |
21 | | using std::vector; |
22 | | |
23 | | namespace yb { |
24 | | |
25 | 9 | double standard_deviation(vector<double> data) { |
26 | 9 | if (data.empty()) { |
27 | 0 | return 0.0; |
28 | 0 | } |
29 | 9 | double mean = std::accumulate(data.begin(), data.end(), 0.0) / data.size(); |
30 | 9 | vector<double> deltas(data.size()); |
31 | 23 | std::transform(data.begin(), data.end(), deltas.begin(), [mean](double x) { |
32 | 23 | return x - mean; |
33 | 23 | }); |
34 | 9 | double inner_product = std::inner_product(deltas.begin(), deltas.end(), deltas.begin(), 0.0); |
35 | 9 | double stdev = std::sqrt(inner_product / data.size()); |
36 | 9 | return stdev; |
37 | 9 | } |
38 | | } // namespace yb |