00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CORRELATION_H
00019 #define CORRELATION_H
00020
00021 #include<odindata/statistics.h>
00022
00032 struct correlationResult {
00033
00034 correlationResult() : r(0.0), p(0.0), z(0.0) {}
00035
00039 double r;
00040
00044 double p;
00045
00049 double z;
00050 };
00051
00052
00054
00055
00060 template <typename T, int N_rank>
00061 correlationResult correlation(const Array<T,N_rank>& x, const Array<T,N_rank>& y) {
00062 Log<OdinData> odinlog("","correlation");
00063
00064 correlationResult result;
00065
00066 if(x.shape()!=y.shape()) {
00067 ODINLOG(odinlog,errorLog) << "Shape mismatch" << STD_endl;
00068 return result;
00069 }
00070
00071 int n=x.numElements();
00072
00073 if(n<2) return result;
00074
00075 statisticResult xstat=statistics(x);
00076 statisticResult ystat=statistics(y);
00077
00078 result.r=secureDivision(
00079 sum( (x-xstat.mean)*(y-ystat.mean) ) ,
00080 sqrt( sum( (x-xstat.mean) * (x-xstat.mean) ) ) * sqrt ( sum( (y-ystat.mean) * (y-ystat.mean) ) )
00081 );
00082
00083 #ifdef HAVE_ERFC
00084 result.p=0.5*erfc(fabs(result.r)*sqrt(double(n)/2.0));
00085 #else
00086 #error erfc() is missing
00087 #endif
00088
00089 double argument=secureDivision(1.0+result.r,1.0-result.r);
00090 if(argument) {
00091
00092 if(n>3) result.z=0.5*sqrt(double(n-3))*log(argument);
00093 }
00094
00095 return result;
00096 }
00097
00099
00100
00105 template <typename T, int N_rank>
00106 correlationResult kendall(const Array<T,N_rank>& x, const Array<T,N_rank>& y) {
00107 Log<OdinData> odinlog("","kendall");
00108
00109 correlationResult result;
00110
00111 if(x.shape()!=y.shape()) {
00112 ODINLOG(odinlog,errorLog) << "Shape mismatch" << STD_endl;
00113 return result;
00114 }
00115
00116 int n=x.size();
00117
00118 int nx=0;
00119 int ny=0;
00120 int diff=0;
00121 for(int j=0; j<n; j++) {
00122 for(int i=j+1; i<n; i++) {
00123 TinyVector<int,N_rank> ii=index2extent(x.shape(),i);
00124 TinyVector<int,N_rank> jj=index2extent(x.shape(),j);
00125 float dx=x(jj)-x(ii);
00126 float dy=y(jj)-y(ii);
00127 if(dx) nx++;
00128 if(dy) ny++;
00129 float dd=dx*dy;
00130 if(dd>0.0) diff++;
00131 if(dd<0.0) diff--;
00132 }
00133 }
00134
00135 double tau=secureDivision(diff,sqrt(double(nx))*sqrt(double(ny)));
00136
00137 result.r=tau;
00138
00139 result.z=3.0*tau*sqrt( secureDivision(n*(n-1),2*(2*n+5) ) );
00140
00141 return result;
00142 }
00143
00145
00146
00151 struct fmriResult {
00152
00153 fmriResult() : Sbaseline(0.0), Srest(0.0), Sstim(0.0), rel_diff(0.0), rel_err(0.0) {}
00154
00158 float Sbaseline;
00159
00163 float Srest;
00164
00168 float Sstim;
00169
00173 float rel_diff;
00174
00178 float rel_err;
00179 };
00180
00182
00187 fmriResult fmri_eval(const Data<float,1>& timecourse, const Data<float,1>& designvec);
00188
00189
00193 #endif
00194