cmtkMathUtil_Statistics.txx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2425 $
00026 //
00027 //  $LastChangedDate: 2010-10-07 16:14:23 -0700 (Thu, 07 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "Numerics/sevd.h"
00034 #include "Numerics/studentttests.h"
00035 
00036 namespace
00037 cmtk
00038 {
00039 
00042 
00043 template<class T>                                                                     
00044 T                                                                                     
00045 MathUtil::Mean                                                                                  
00046 ( const unsigned int nValues, const T* values )                                       
00047 {                                                                                     
00048   T mean = 0.0;                                                                       
00049                                                                                       
00050   for ( unsigned int j = 0; j < nValues; j++ )                                        
00051     mean += values[j];                                                                
00052   mean /= nValues;                                                                    
00053                                                                                       
00054   return mean;                                                                        
00055 }                                                                                     
00056                                                                                       
00057 template<class T>                                                                     
00058 T                                                                                     
00059 MathUtil::Variance                                                                              
00060 ( const unsigned int nValues, const T* values, const T mean, const bool unbiased )           
00061 {                                                                                            
00062   T sumOfSquares = 0.0;                                                                          
00063                                                                                              
00064   T sum = 0.0;                                                                               
00065   for ( unsigned int j = 0; j < nValues; j++ ) 
00066     { 
00067     const T s = values[j] - mean;                                                                  
00068     sum += s;                                                                                
00069     sumOfSquares += s*s;                                                                         
00070     }                                                                                          
00071   
00072   if ( unbiased )                                                                            
00073     return (sumOfSquares - sum*sum/nValues) / (nValues-1);                                   
00074   else                                                                                       
00075     return (sumOfSquares - sum*sum/nValues) / (nValues);                                     
00076 }        
00077 
00078 template<class T>                                                                     
00079 T                                                                                     
00080 MathUtil::Mean                                                                                  
00081 ( const std::vector<T>& values )                                       
00082 {                                                                                     
00083   const size_t nValues = values.size();
00084   
00085   T mean = 0.0;
00086   for ( size_t j = 0; j < nValues; j++ )                                        
00087     mean += values[j];                                                                
00088   mean /= nValues;                                                                    
00089                                                                                       
00090   return mean;                                                                        
00091 }                                                                                     
00092                                                                                       
00093 template<class T>                                                                     
00094 T                                                                                     
00095 MathUtil::Variance                                                                              
00096 ( const std::vector<T>& values, const T mean, const bool unbiased )           
00097 {                                                                                            
00098   const size_t nValues = values.size();
00099 
00100   T sumOfSquares = 0.0;                                                                          
00101   T sum = 0.0;                                                                               
00102   for ( size_t j = 0; j < nValues; j++ ) 
00103     { 
00104     const T s = values[j] - mean;                                                                  
00105     sum += s;                                                                                
00106     sumOfSquares += s*s;                                                                         
00107     }                                                                                          
00108   
00109   if ( unbiased )                                                                            
00110     return (sumOfSquares - sum*sum/nValues) / (nValues-1);                                   
00111   else                                                                                       
00112     return (sumOfSquares - sum*sum/nValues) / (nValues);                                     
00113 }        
00114 
00115 template<class T>
00116 T
00117 MathUtil::Correlation
00118 ( const std::vector<T>& x, const std::vector<T>& y )
00119 {
00120   const size_t n = std::min( x.size(), y.size() );
00121 
00122   // compute means
00123   T meanx = 0, meany = 0;
00124   for ( size_t i = 0; i < n; ++i )
00125     {
00126       meanx += x[i];
00127       meany += y[i];
00128     }
00129   meanx /= n;
00130   meany /= n;
00131 
00132   // compute parameter correlations
00133   T c = 0, xSq = 0, ySq = 0;
00134   T dx, dy;
00135   for ( size_t i = 0; i < n; ++i )
00136     {
00137     c += (dx=x[i]-meanx) * (dy=y[i]-meany);
00138     xSq += dx * dx;
00139     ySq += dy * dy;
00140     }
00141   
00142   return static_cast<T>( c / (sqrt(xSq*ySq)+1e-20) );
00143 }
00144   
00145 template<class T>
00146 T
00147 MathUtil::TTest
00148 ( const std::vector<T>& valuesX, const std::vector<T>& valuesY, T& t )
00149 {
00150   T averageX, averageY;
00151   return TTest( valuesX, valuesY, t, averageX, averageY );
00152 }
00153 
00154 template<class T>
00155 T
00156 MathUtil::PairedTTest
00157 ( const std::vector<T>& valuesX, const std::vector<T>& valuesY, T& t, T& avgX, T& avgY )
00158 {
00159   const size_t nValues = valuesX.size();
00160 
00161   avgX = Mean( valuesX );
00162   avgY = Mean( valuesY );
00163 
00164   T SSD = 0;
00165   for ( size_t i = 0; i < nValues; ++i )
00166     SSD += Square( (valuesX[i]-avgX) - (valuesY[i]-avgY) );
00167 
00168   t = (avgX - avgY) * sqrt( (nValues * (nValues-1)) / SSD );
00169   
00170   double s = alglib::studenttdistribution(nValues-1, t);
00171   double p1 = 2 * ap::minreal(s, 1-s);
00172 
00173   return (T) p1; // probability
00174 }
00175 
00178 template<class T>
00179 T
00180 MathUtil::TTest
00181 ( const std::vector<T>& valuesX, const std::vector<T>& valuesY, T& t, T& avgX, T& avgY )
00182 {
00183   const size_t nValuesX = valuesX.size();
00184   const size_t nValuesY = valuesY.size();
00185 
00186   ap::real_1d_array apValuesX;
00187   apValuesX.setbounds( 0, nValuesX-1 );
00188   for (unsigned int i = 0; i < nValuesX; i++)
00189     apValuesX(i) = (double)(1.0 * valuesX[i]);
00190 
00191   ap::real_1d_array apValuesY;
00192   apValuesY.setbounds( 0, nValuesY-1 );
00193   for (unsigned int i = 0; i < nValuesY; i++)
00194     apValuesY(i) = (double)(1.0 * valuesY[i]);
00195   
00196   ap::real_value_type t_temp, p1, p2, p3;
00197 
00198   avgX = MathUtil::Mean<T>( valuesX );
00199   avgY = MathUtil::Mean<T>( valuesY );
00200   
00201   alglib::studentttest2( apValuesX, nValuesX, apValuesY, nValuesY, t_temp, p1, p2, p3 );
00202 
00203   t = static_cast<T>( t_temp );
00204 
00205   return static_cast<T>( p1 ); // probability
00206 }
00207 
00210 template<class T>
00211 T
00212 MathUtil::TTest
00213 ( const std::vector<T>& valuesX, T& t, T& avgX )
00214 {
00215   const size_t nValuesX = valuesX.size();
00216 
00217   avgX = Mean( valuesX );
00218   const T varianceX = Variance( valuesX, avgX, true /*unbiased*/ );
00219 
00220   t = avgX * nValuesX / sqrt( varianceX );
00221 
00222   const double s = alglib::studenttdistribution(nValuesX-1, t);
00223   const double p1 = 2 * ap::minreal(s, 1-s);
00224 
00225   return (T) p1; // probability
00226 }
00227 
00228 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines