00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <numeric>
00028 #include <OpenMS/CONCEPT/Types.h>
00029
00030
00031
00032 #ifndef OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H
00033 #define OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H
00034
00035 namespace OpenMS
00036 {
00037
00038 namespace Math
00039 {
00040
00052 template < typename IteratorType1, typename IteratorType2 >
00053 inline static
00054 DoubleReal meanSquareError ( IteratorType1 begin_a, const IteratorType1 end_a,
00055 IteratorType2 begin_b, const IteratorType2 end_b
00056 )
00057 {
00058 Int count = 0;
00059 DoubleReal error = 0;
00060 IteratorType1 & it_a = begin_a;
00061 IteratorType2 & it_b = begin_b;
00062
00063 while(it_a != end_a && it_b != end_b)
00064 {
00065 DoubleReal diff = *it_a - *it_b;
00066 error += diff * diff;
00067 ++count;
00068 ++it_a;
00069 ++it_b;
00070 }
00071
00072 return error / count;
00073
00074 }
00075
00086 template < typename IteratorType1, typename IteratorType2 >
00087 inline static
00088 Real classificationRate ( IteratorType1 begin_a, const IteratorType1 end_a,
00089 IteratorType2 begin_b, const IteratorType2 end_b
00090 )
00091 {
00092 Int count = 0;
00093 DoubleReal error = 0;
00094 IteratorType1 & it_a = begin_a;
00095 IteratorType2 & it_b = begin_b;
00096
00097 if (it_a == end_a || it_b == end_b)
00098 {
00099 return 0;
00100 }
00101
00102 while(it_a != end_a && it_b != end_b)
00103 {
00104 if ((*it_a < 0 && *it_b >= 0)
00105 || (*it_a >= 0 && *it_b < 0))
00106 {
00107 error += 1;
00108 }
00109 ++count;
00110 ++it_a;
00111 ++it_b;
00112 }
00113
00114 return (count - error) / count;
00115
00116 }
00117
00132 template < typename IteratorType1, typename IteratorType2 >
00133 inline static
00134 DoubleReal matthewsCorrelationCoefficient( IteratorType1 begin_a, const IteratorType1 end_a,
00135 IteratorType2 begin_b, const IteratorType2 end_b
00136 )
00137 {
00138 IteratorType1 & it_a = begin_a;
00139 IteratorType2 & it_b = begin_b;
00140 DoubleReal tp = 0;
00141 DoubleReal fp = 0;
00142 DoubleReal tn = 0;
00143 DoubleReal fn = 0;
00144
00145 if (it_a == end_a || it_b == end_b)
00146 {
00147 return 0;
00148 }
00149
00150 while(it_a != end_a && it_b != end_b)
00151 {
00152 if (*it_a < 0 && *it_b >= 0)
00153 {
00154 ++fn;
00155 }
00156 else if (*it_a < 0 && *it_b < 0)
00157 {
00158 ++tn;
00159 }
00160 else if (*it_a >= 0 && *it_b >= 0)
00161 {
00162 ++tp;
00163 }
00164 else if (*it_a >= 0 && *it_b < 0)
00165 {
00166 ++fp;
00167 }
00168
00169 ++it_a;
00170 ++it_b;
00171 }
00172
00173 return ((tp * tn - fp * fn) /
00174 sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)));
00175
00176 }
00177
00193 template < typename IteratorType1, typename IteratorType2 >
00194 inline static
00195 DoubleReal pearsonCorrelationCoefficient ( IteratorType1 begin_a, IteratorType1 end_a,
00196 IteratorType2 begin_b, IteratorType2 end_b
00197 )
00198 throw (Exception::InvalidRange)
00199 {
00200 UInt count = end_a-begin_a;
00201
00202 if (count==0 || end_a-begin_a!=end_b-begin_b)
00203 {
00204 throw Exception::InvalidRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
00205 }
00206
00207
00208 DoubleReal avg_a = std::accumulate(begin_a,end_a,0.0) / count;
00209 DoubleReal avg_b = std::accumulate(begin_b,end_b,0.0) / count;
00210
00211 DoubleReal numerator = 0;
00212 DoubleReal denominator_a = 0;
00213 DoubleReal denominator_b = 0;
00214 while(begin_a != end_a)
00215 {
00216 DoubleReal temp_a = *begin_a - avg_a;
00217 DoubleReal temp_b = *begin_b - avg_b;
00218 numerator += (temp_a * temp_b);
00219 denominator_a += (temp_a * temp_a);
00220 denominator_b += (temp_b * temp_b);
00221 ++begin_a;
00222 ++begin_b;
00223 }
00224
00225 return numerator / sqrt(denominator_a * denominator_b);
00226 }
00227
00228 }
00229 }
00230
00231 #endif // OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H