-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorrelation.C
117 lines (85 loc) · 2.22 KB
/
Correlation.C
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/****************************************************************
Correlation.C
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include <iostream>
#include <math.h>
#include "Correlation.H"
#include <gsl/gsl_cdf.h>
#include "String.H"
using namespace std;
using namespace BOOM;
Correlation::Correlation(const DblPointVector &v)
{
n=v.size();
double sumXX=0.0, sumYY=0.0, sumXY=0.0, sumY=0.0, sumX=0.0;
for(int i=0 ; i<n ; ++i)
{
const DblPoint &point=v[i];
double x=point.first;
double y=point.second;
sumX+=x;
sumY+=y;
sumXX+=x*x;
sumYY+=y*y;
sumXY+=x*y;
}
r=computeR(sumX,sumXX,sumY,sumYY,sumXY,n);
}
Correlation::Correlation(const Vector<float> &a,const Vector<float> &b)
{
n=a.size();
double sumXX=0.0, sumYY=0.0, sumXY=0.0, sumY=0.0, sumX=0.0;
for(int i=0 ; i<n ; ++i)
{
double x=a[i], y=b[i];
sumX+=x;
sumY+=y;
sumXX+=x*x;
sumYY+=y*y;
sumXY+=x*y;
}
r=computeR(sumX,sumXX,sumY,sumYY,sumXY,n);
}
Correlation::Correlation(const Vector<double> &a,const Vector<double> &b)
{
n=a.size();
double sumXX=0.0, sumYY=0.0, sumXY=0.0, sumY=0.0, sumX=0.0;
for(int i=0 ; i<n ; ++i)
{
double x=a[i], y=b[i];
sumX+=x;
sumY+=y;
sumXX+=x*x;
sumYY+=y*y;
sumXY+=x*y;
}
r=computeR(sumX,sumXX,sumY,sumYY,sumXY,n);
}
double Correlation::getR()
{
return r;
}
bool Correlation::isSignificant(double alpha)
{
return getP()<=alpha;
}
double Correlation::getP()
{
double t=r*sqrt(double(n-2))/sqrt(double(1-r*r));
return gsl_cdf_tdist_Q(t,n-2);
}
double Correlation::computeR(double sumX,double sumXX,double sumY,
double sumYY,double sumXY,int n)
{
const double yBar=sumY/n;
const double xBar=sumX/n;
const double Sxx=sumXX-sumX*sumX/n;
if(Sxx==0.0) throw String("Error in BOOM::LinRegressor: Sxx is 0");
const double Syy=sumYY-sumY*sumY/n;
const double Sxy=sumXY-sumX*sumY/n;
return Sxy/sqrt(Syy*Sxx);
}