/usr/include/odindata/correlation.h is in libodin-dev 1.8.8-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | /***************************************************************************
correlation.h - description
-------------------
begin : Fri Apr 6 2001
copyright : (C) 2000-2014 by Thies Jochimsen
email : thies@jochimsen.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef CORRELATION_H
#define CORRELATION_H
#include<odindata/statistics.h>
/**
* @addtogroup odindata
* @{
*/
/**
* Results of a linear correlation analysis
*
*/
struct correlationResult {
correlationResult() : r(0.0), p(0.0), z(0.0) {}
/**
* The correlation coefficient
*/
double r;
/**
* The one-sided error probability
*/
double p;
/**
* z-transformation (z-score) associated with r
*/
double z;
};
/////////////////////////////////////////////////////////////////////////
/**
* Linear correlation between vectors 'x' and 'y'
*
*/
template <typename T, int N_rank>
correlationResult correlation(const Array<T,N_rank>& x, const Array<T,N_rank>& y) {
Log<OdinData> odinlog("","correlation");
correlationResult result;
if(x.shape()!=y.shape()) {
ODINLOG(odinlog,errorLog) << "Shape mismatch" << STD_endl;
return result;
}
int n=x.numElements();
if(n<2) return result;
statisticResult xstat=statistics(x);
statisticResult ystat=statistics(y);
result.r=secureDivision(
sum( (x-xstat.mean)*(y-ystat.mean) ) ,
sqrt( sum( (x-xstat.mean) * (x-xstat.mean) ) ) * sqrt ( sum( (y-ystat.mean) * (y-ystat.mean) ) )
);
#ifdef HAVE_ERFC
result.p=0.5*erfc(fabs(result.r)*sqrt(double(n)/2.0)); // Eq 14.5.2 in NRC
#else
#error erfc() is missing
#endif
double argument=secureDivision(1.0+result.r,1.0-result.r);
if(argument) {
// result.z=0.5*log(argument); // Eq 14.5.6 in NRC
if(n>3) result.z=0.5*sqrt(double(n-3))*log(argument); // http://www.fmrib.ox.ac.uk/~stuart/thesis/chapter_6/section6_4.html
}
return result;
}
/////////////////////////////////////////////////////////////////////////
/**
* Kendall's rank-correlation between vectors 'x' and 'y' re-implemented from NRC, section 14.6
*
*/
template <typename T, int N_rank>
correlationResult kendall(const Array<T,N_rank>& x, const Array<T,N_rank>& y) {
Log<OdinData> odinlog("","kendall");
correlationResult result;
if(x.shape()!=y.shape()) {
ODINLOG(odinlog,errorLog) << "Shape mismatch" << STD_endl;
return result;
}
int n=x.size();
int nx=0;
int ny=0;
int diff=0;
for(int j=0; j<n; j++) {
for(int i=j+1; i<n; i++) {
TinyVector<int,N_rank> ii=index2extent(x.shape(),i);
TinyVector<int,N_rank> jj=index2extent(x.shape(),j);
float dx=x(jj)-x(ii);
float dy=y(jj)-y(ii);
if(dx) nx++;
if(dy) ny++;
float dd=dx*dy;
if(dd>0.0) diff++;
if(dd<0.0) diff--;
}
}
double tau=secureDivision(diff,sqrt(double(nx))*sqrt(double(ny)));
result.r=tau;
result.z=3.0*tau*sqrt( secureDivision(n*(n-1),2*(2*n+5) ) ); // Eq. 30.4 in Handbook of Parametric and Nonparametric Statistical Procedures
return result;
}
/////////////////////////////////////////////////////////////////////////
/**
* Results of an fMRI analysis
*
*/
struct fmriResult {
fmriResult() : Sbaseline(0.0), Srest(0.0), Sstim(0.0), rel_diff(0.0), rel_err(0.0) {}
/**
* Signal during baseline (leading timesteps with zeroes in design file)
*/
float Sbaseline;
/**
* Signal during rest
*/
float Srest;
/**
* Signal during stimulation
*/
float Sstim;
/**
* Relative signal change
*/
float rel_diff;
/**
* Error of relative signal change
*/
float rel_err;
};
/////////////////////////////////////////////////////////////////////////
/**
* Returns an fMRI analysis of 'timecourse' using 'designvec'
*
*/
fmriResult fmri_eval(const Data<float,1>& timecourse, const Data<float,1>& designvec);
/** @}
*/
#endif
|