-
Notifications
You must be signed in to change notification settings - Fork 3
/
pval.cpp
57 lines (44 loc) · 1.79 KB
/
pval.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
49
50
51
52
53
54
55
56
57
#ifndef PVAL_h
#define PVAL_h
#include <math.h>
#include <gsl/gsl_cdf.h>
#define PI 3.14159265358979323846264338327950288
// Define the t-distribution PDF
double t_pdf(double x, int dof) {
double numerator = tgamma((dof + 1) / 2.0);
double denominator = sqrt(dof * PI) * tgamma(dof / 2.0);
return numerator / denominator * pow(1 + (x * x) / dof, -(dof + 1) / 2.0);
}
double t_cdf(double x, int dof, double step = 0.001) {
double result = 0.0;
double current_x = -20.0; // Starting from a very negative value
while (current_x <= x) {
result += t_pdf(current_x, dof) * step;
current_x += step;
}
return result;
}
double calculate_p_value(double test_statistic, int dof) {
double p_value = 1.0 - t_cdf(test_statistic, dof);
return p_value;
}
double calculate_two_tailed_p_value(double t_statistic, int degrees_of_freedom, int num_segments) {
double cdf_positive = t_cdf(fabs(t_statistic), degrees_of_freedom, num_segments);
double cdf_negative = t_cdf(-fabs(t_statistic), degrees_of_freedom, num_segments);
double p_value = 2.0 * fmin(cdf_positive, cdf_negative); // Two-tailed p-value
return p_value;
}
double calculate_one_tailed_p_value(double t_statistic, int degrees_of_freedom, int num_segments) {
double cdf_positive = t_cdf(t_statistic, degrees_of_freedom, num_segments);
double one_tailed_p_value = 1.0 - cdf_positive; // One-tailed p-value (upper tail)
return one_tailed_p_value;
}
double calculate_z_score(double confidence_level) {
// Calculate the (1 - alpha/2) quantile for a two-tailed distribution
double alpha = 1.0 - confidence_level;
double quantile = 1.0 - alpha / 2.0;
// Estimate the Z-score using the quantile function
double z_score = gsl_cdf_ugaussian_Pinv(quantile);
return z_score;
}
#endif