00001 #ifndef Impala_Core_Training_KernelFunctions_h
00002 #define Impala_Core_Training_KernelFunctions_h
00003
00004 #include "Core/Vector/VectorSet.h"
00005
00006 namespace Impala
00007 {
00008 namespace Core
00009 {
00010 namespace Training
00011 {
00012
00013 double Chi2Distance(const Vector::VectorTem<double>& v1, const Vector::VectorTem<double>& v2)
00014 {
00015 int length=v1.Size();
00016 double accumulator = 0;
00017 for(int i=0 ; i<length ; ++i)
00018 {
00019 double sum = v1[i] + v2[i];
00020 if(sum)
00021 accumulator += (v1[i] - v2[i]) * (v1[i] - v2[i]) / sum;
00022 }
00023
00024 return accumulator / 2.0;
00025 }
00026
00042 class MpoChi2Kernel
00043 {
00044 public:
00045 MpoChi2Kernel(const std::vector<double>& weights,
00046 const std::vector<double>& averages)
00047 {
00048 int length = weights.size();
00049 mNormalisation = 0;
00050 int i;
00051 for(i=0 ; i<length ; ++i)
00052 mNormalisation += weights[i];
00053 mNormalisation = 1.0 / mNormalisation;
00054
00055 mWeights.resize(length);
00056 for(i=0 ; i<length ; ++i)
00057 mWeights[i] = -1 * weights[i] / averages[i];
00058 }
00059
00060 double DoIt(const std::vector<double>& d)
00061 {
00062 double sum=0;
00063 int l = d.size();
00064 for(int i=0 ; i<l ; ++i)
00065 {
00066 sum += d[i] * mWeights[i];
00067 }
00068 return exp(sum / mNormalisation);
00069 }
00070
00071 private:
00072 double mNormalisation;
00073 std::vector<double> mWeights;
00074 };
00075
00076 }
00077 }
00078 }
00079
00080
00081 #endif