forked from tjhladish/dengue-abm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cpp
More file actions
50 lines (44 loc) · 1.8 KB
/
Utility.cpp
File metadata and controls
50 lines (44 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Utility.h"
namespace dengue {
namespace util {
Fit* lin_reg(const std::vector<double> &x, const std::vector<double> &y) {
assert( x.size() == y.size() );
Fit* fit = new Fit();
const int n = x.size();
double sumx = 0.0; // sum of x
double sumx2 = 0.0; // sum of x**2
double sumxy = 0.0; // sum of x * y
double sumy = 0.0; // sum of y
double sumy2 = 0.0; // sum of y**2
for (int i=0; i<n; i++) {
sumx += x[i];
sumx2 += pow(x[i],2);
sumxy += x[i] * y[i];
sumy += y[i];
sumy2 += pow(y[i],2);
}
double denom = n * sumx2 - pow(sumx,2);
if (denom == 0) {
// singular matrix. can't solve the problem.
fit->m = 0;
fit->b = 0;
fit->rsq = 0;
return fit;
}
fit->m = (n * sumxy - sumx * sumy) / denom;
fit->b = (sumy * sumx2 - sumx * sumxy) / denom;
// compute correlation coeff
fit->rsq = pow((sumxy - sumx * sumy / n) / sqrt((sumx2 - pow(sumx,2)/n) * (sumy2 - pow(sumy,2)/n)),2);
return fit;
}
vector<string> split(const string &s, char delim) {
vector<string> tokens;
stringstream ss(s);
string token;
while (getline(ss, token, delim)) {
tokens.push_back(token);
}
return tokens;
}
}
}