core/vnl/algo/vnl_chi_squared.txx
Go to the documentation of this file.
00001 // This is core/vnl/algo/vnl_chi_squared.txx
00002 #ifndef vnl_chi_squared_txx_
00003 #define vnl_chi_squared_txx_
00004 //:
00005 // \file
00006 // \verbatim
00007 //  Modifications
00008 //   24 Mar 2010  Peter Vanroose  renamed from .cxx to .txx and moved out template instantiations
00009 // \endverbatim
00010 
00011 #include "vnl_chi_squared.h"
00012 
00013 //------------------------------------------------------------
00014 
00015 // FORTRAN routine
00016 #include <vnl/algo/vnl_netlib.h> // for dchscdf_()
00017 
00018 template <class T>
00019 double vnl_chi_squared_cumulative(T chisq, long dof)
00020 {
00021   double cdf, chisqr = chisq;
00022   v3p_netlib_dchscdf_(&chisqr,&dof,&cdf);
00023   return cdf;
00024 }
00025 
00026 //------------------------------------------------------------
00027 
00028 template <class T>
00029 double vnl_chi_squared_statistic_1 (T const *A, T const *B, int n, bool normalize)
00030 {
00031   double sum = 0;
00032 
00033   if (normalize)
00034   {
00035     T sumA = 0;
00036     T sumB = 0;
00037     for (int i=0; i<n; ++i) {
00038       sumA += A[i];
00039       sumB += B[i];
00040     }
00041 
00042     for (int i=0; i<n; ++i)
00043       if (A[i]) {
00044         double a = double(A[i])/sumA;
00045         double b = double(B[i])/sumB;
00046         double tmp = a - b;
00047         sum += tmp*tmp/a;
00048       }
00049   }
00050   else
00051   {
00052     for (int i=0; i<n; ++i)
00053       if (A[i]) {
00054         double tmp = A[i] - B[i];
00055         sum += tmp*tmp/A[i];
00056       }
00057   }
00058 
00059   return sum;
00060 }
00061 
00062 template <class T>
00063 double vnl_chi_squared_statistic_2 (T const *A, T const *B, int n, bool normalize)
00064 {
00065   return vnl_chi_squared_statistic_1(B, A, n, normalize);
00066 }
00067 
00068 template <class T>
00069 double vnl_chi_squared_statistic_12(T const *A, T const *B, int n, bool normalize)
00070 {
00071   double sum = 0;
00072 
00073   if (normalize)
00074   {
00075     T sumA = 0;
00076     T sumB = 0;
00077     for (int i=0; i<n; ++i) {
00078       sumA += A[i];
00079       sumB += B[i];
00080     }
00081 
00082     for (int i=0; i<n; ++i)
00083       if (A[i] || B[i]) {
00084         double a = double(A[i])/sumA;
00085         double b = double(B[i])/sumB;
00086         double tmp = a - b;
00087         sum += tmp*tmp/(a + b);
00088       }
00089   }
00090   else
00091   {
00092     for (int i=0; i<n; ++i)
00093       if (A[i] || B[i]) {
00094         double tmp = A[i] - B[i];
00095         sum += tmp*tmp/(A[i] + B[i]);
00096       }
00097   }
00098 
00099   return sum;
00100 }
00101 
00102 #undef VNL_CHI_SQUARED_INSTANTIATE
00103 #define VNL_CHI_SQUARED_INSTANTIATE(T) \
00104 template double vnl_chi_squared_cumulative  (T chisq, long dof); \
00105 template double vnl_chi_squared_statistic_1 (T const *, T const *, int, bool); \
00106 template double vnl_chi_squared_statistic_2 (T const *, T const *, int, bool); \
00107 template double vnl_chi_squared_statistic_12(T const *, T const *, int, bool)
00108 
00109 #endif // vnl_chi_squared_txx_