-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
48 lines (40 loc) · 1.2 KB
/
util.cpp
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
#include <vector>
#include <math.h>
#include "util.hpp"
double expit(double x) {
return exp(x) / (1 + exp(x));
}
double logit(double x) {
return log(x / (1 - x));
}
double regression(vector <double> &x, vector <double> &y) {
double n = x.size();
double sum_x = 0.0, sum_y = 0.0, sum_x_x = 0.0, sum_x_y = 0.0;
for (int l = 0; l < n; l++) {
sum_x += x[l];
sum_y += y[l];
sum_x_x += x[l] * x[l];
sum_x_y += x[l] * y[l];
}
return (n * sum_x_y - sum_x * sum_y) / (n * sum_x_x - sum_x * sum_x);
}
double log2_regression(vector <double> &y) {
int n = y.size() - 1;
vector<double> x(n), log2_y(n);
for (int l = 1; l <= n; l++) {
x[l - 1] = l;
log2_y[l - 1] = -log2(y[l]);
}
return regression(x, log2_y);
}
pair<double, double> beta_param(double m, double var) {
double alpha = ((1.0 - m) / var - 1.0 / m) * m * m;
double beta = alpha * (1.0 / m - 1.0);
return make_pair(alpha, beta);
}
pair<double, double> log_param(double m, double s) {
double log_m = log(m) - 0.5 * log(1 + s * s / (m * m));
double log_s2 = log(1 + s * s / (m * m));
double log_sigma = sqrt(log_s2);
return make_pair(log_m, log_sigma);
}