1 /*
2    This file is part of the BOLT-LMM linear mixed model software package
3    developed by Po-Ru Loh.  Copyright (C) 2014-2019 Harvard University.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <vector>
20 #include <cmath>
21 #include <cstdlib>
22 
23 #include "Types.hpp"
24 #include "StatsUtils.hpp"
25 #include "NumericUtils.hpp"
26 
27 namespace StatsUtils {
28 
29   using std::vector;
30 
stdDev(const vector<double> & x,bool isPop)31   double stdDev(const vector <double> &x, bool isPop) {
32     uint64 n = x.size();
33     if (n <= 1) return NAN;
34     double s = 0.0, s2 = 0.0;
35     for (uint64 i = 0; i < n; i++) {
36       if (std::isnan(x[i])) return NAN;
37       if (std::isinf(x[i])) return INFINITY;
38       s += x[i];
39       s2 += x[i]*x[i];
40     }
41     if (isPop) return sqrt((s2 - s*s/n) / n);
42     else return sqrt((s2 - s*s/n) / (n-1));
43   }
44 
zScore(const vector<double> & x)45   double zScore(const vector <double> &x) {
46     return NumericUtils::mean(x) / stdDev(x);
47   }
48 
49   // zScore for x-y
zScoreDiff(vector<double> x,const vector<double> & y)50   double zScoreDiff(vector <double> x, const vector <double> &y) {
51     if (x.size() <= 1) return NAN;
52     for (uint64 i = 0; i < x.size(); i++)
53       x[i] -= y[i];
54     return zScore(x);
55   }
56 }
57