stats.C

Go to the documentation of this file.
00001 /*!@file Util/stats.C STATS classes */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: T Nathan Mundhenk <mundhenk@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/stats.C $
00035 // $Id: stats.C 9632 2008-04-15 05:50:25Z mundhenk $
00036 //
00037 
00038 // ############################################################
00039 // ############################################################
00040 // ##### ---STATS---
00041 // ##### Some basic statistical methods:
00042 // ##### T. Nathan Mundhenk nathan@mundhenk.com
00043 // ############################################################
00044 // ############################################################
00045 
00046 #include "Util/Assert.H"
00047 #include "Util/stats.H"
00048 #include "Util/Types.H"
00049 #include "Util/log.H"
00050 #include <cmath>
00051 
00052 template <class T>
00053 stats<T>::stats()
00054 {
00055   GGC = false;
00056 }
00057 template <class T>
00058 stats<T>::~stats()
00059 {
00060 }
00061 
00062 
00063 
00064 template <class T>
00065 T stats<T>::mean(std::vector<T> &X)
00066 {
00067   ASSERT(X.size() > 0);
00068   T Xi = 0;
00069   for(unsigned int i = 0; i < X.size(); i++)
00070   {
00071     Xi = Xi + X[i];
00072   }
00073   return Xb = Xi/X.size();
00074 };
00075 
00076 template <class T>
00077 T stats<T>::findS(std::vector<T> &X, T Xbar)
00078 {
00079   ASSERT(X.size() > 0);
00080   T Xi = 0;
00081   for(unsigned int i = 0; i < X.size(); i++)
00082   {
00083     Xi = (pow(X[i],2)/X.size()) + Xi;
00084   }
00085   S2 = Xi - pow(Xbar,2);
00086   if(S > 0)
00087     return S = sqrt(S2);
00088   else
00089     return S = 0;
00090 };
00091 
00092 template <class T>
00093 T stats<T>::findS(std::vector<T> &X, T Xbar, T adj)
00094 {
00095   ASSERT(X.size() > 0);
00096   T Xi = 0;
00097   for(unsigned int i = 0; i < X.size(); i++)
00098   {
00099     Xi = (pow((X[i]+adj),2)/X.size()) + Xi;
00100   }
00101   S2 = Xi - pow((Xbar+adj),2);
00102   if(S2 > 0)
00103     S = sqrt(S2);
00104   else
00105     S = 0;
00106   return S;
00107 }
00108 
00109 template <class T>
00110 T stats<T>::rRegression(std::vector<T> &X, std::vector<T> &Y)
00111 {
00112   ASSERT(X.size() == Y.size());
00113   T Xmean = mean(X);
00114   T Ymean = mean(Y);
00115   Sx = findS(X,Xmean);
00116   Sy = findS(Y,Ymean);
00117   T hold = 0;
00118   for(unsigned int i = 0; i < X.size(); i++)
00119   {
00120     hold = ((X[i] - Xmean)*(Y[i] - Ymean)) + hold;
00121   }
00122   return r = hold/(X.size()*Sx*Sy);
00123 };
00124 
00125 #if 0
00126 // FIXME this function does not compile
00127 template <class T>
00128 T stats<T>::bRegression(std::vector<T> &X, std::vector<T> &Y)
00129 {
00130   ASSERT(X.size() == Y.size());
00131   T Xmean = mean(X);
00132   T Ymean = mean(Y);
00133   Sx = S(X,Xmean); // <-- FIXME compilation error here
00134   Sy = S(Y,Ymean); // <-- FIXME compilation error here
00135   T Zxy = 0;
00136   for(unsigned int i = 0; i < X.size(); i++)
00137   {
00138     Zxy = (((X[i] - Xmean)/Sx)*((Y[i] - Ymean)/Sy)) + Zxy;
00139   }
00140   return b = Zxy/X.size();
00141 };
00142 #endif
00143 
00144 template <class T>
00145 T stats<T>::Bxy(T r, T Sx, T Sy)
00146 {
00147   return r*(Sy/Sx);
00148 };
00149 
00150 template <class T>
00151 T stats<T>::simpleANOVA(std::vector<T> &X, std::vector<T> &Y)
00152 {
00153   ASSERT(X.size() == Y.size());
00154   float mean, meanX, meanY;
00155   float sumX = 0, sumY = 0;
00156   //find mean values
00157   for(unsigned int i = 0; i < X.size(); i++)
00158   {
00159     sumX += X[i];
00160     sumY += Y[i];
00161   }
00162   meanX = sumX/X.size();
00163   meanY = sumY/Y.size();
00164   mean = (sumY+sumX)/(X.size()+Y.size());
00165   //find SSwithin and SStotal
00166   SSwithin = 0;
00167   SStotal = 0;
00168   for(unsigned int i = 0; i < X.size(); i++)
00169   {
00170     SSwithin += pow((X[i]-meanX),2);
00171     SSwithin += pow((Y[i]-meanY),2);
00172 
00173     SStotal += pow((X[i]-mean),2);
00174     SStotal += pow((Y[i]-mean),2);
00175   }
00176   //find SSbetween
00177   SSbetween = 0;
00178   SSbetween = X.size() * (pow((meanX - mean),2));
00179   SSbetween += Y.size() * (pow((meanY - mean),2));
00180 
00181   //create anova table stuff
00182   DFwithin = (X.size()+Y.size())-2;
00183   DFbetween = 1;
00184   MSbetween = (SSbetween/DFbetween);
00185   MSwithin = (SSwithin/DFwithin);
00186   return F = MSbetween/MSwithin;
00187 }
00188 
00189 #if 0
00190 // FIXME this function does not compile
00191 template <class T>
00192 T stats<T>::decisionGGC(T mu1, T mu2, T sigma1, T sigma2, T PofA)
00193 {
00194   T PofB = 1 - PofA;
00195 
00196   //find parts of the equation first
00197   T LeftTop = (mu2*pow(sigma1,2))-(mu1*pow(sigma2,2));
00198   T Bottom = pow(sigma1,2)-pow(sigma2,2);
00199   T logVal = log((PofB*sigma1)/(PofA*sigma2));
00200   T preLog = pow((mu1-mu2),2)+(2*(pow(sigma1,2)-pow(sigma2,2)));
00201 
00202   //find D and D'
00203   // FIXME error here ('sgrt' unknown)...
00204   D = (LeftTop - ((sigma1*sigma2)*sgrt(preLog*logVal)))/Bottom;
00205   // FIXME and here ('sgrt' unknown)...
00206   Dprime = (LeftTop + ((sigma1*sigma2)*sgrt(preLog*logVal)))/Bottom;
00207   GGC = true;
00208   return D;
00209 }
00210 #endif
00211 
00212 template <class T>
00213 T stats<T>::getDPrime()
00214 {
00215   ASSERT(GGC);
00216   return Dprime;
00217 }
00218 
00219 template <class T>
00220 T stats<T>::getErrorGGC_2AFC(T mu1, T mu2, T sigma1, T sigma2)
00221 {
00222   LINFO("INPUT 2AFC u1 = %f, u2 = %f, s1 = %f, s2 = %f",mu1,mu2,sigma1,sigma2);
00223   float temp = fabs((mu1-mu2)/(sqrt(2*(pow(sigma1,2)+pow(sigma2,2)))));
00224   LINFO("ERFC(%f)",temp);
00225   return erfc(temp)/2;
00226 }
00227 
00228 template <class T>
00229 T stats<T>::gauss(T x, T mu,T sigma)
00230 {
00231   return (1/(sqrt(2*3.14159*pow(sigma,2))))*exp((-1*pow((x-mu),2))/(2*pow(sigma,2)));
00232 }
00233 
00234 
00235 template class stats<float>;
00236 template class stats<double>;
00237 
00238 // ######################################################################
00239 /* So things look consistent in everyone's emacs... */
00240 /* Local Variables: */
00241 /* indent-tabs-mode: nil */
00242 /* End: */
Generated on Sun May 8 08:06:59 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3