Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

StatisticFunctions.h (Maintainer: Clemens Groepl)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Clemens Groepl $
00025 // --------------------------------------------------------------------------
00026 
00027 #include <numeric>
00028 #include <OpenMS/CONCEPT/Types.h>
00029 
00030 // #include <iostream> // debugging
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  /* mcc */ 
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       //no data or different lengths
00202       if (count==0 || end_a-begin_a!=end_b-begin_b)
00203       {
00204         throw Exception::InvalidRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
00205       }
00206         
00207       //calculate average
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   } // namespace Math
00229 } // namespace OpenMS
00230 
00231 #endif // OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H

Generated Tue Apr 1 15:36:38 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1