GaussianShiftKernel.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2008 Gunnar Raetsch
00008  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "kernel/GaussianShiftKernel.h"
00013 #include "features/Features.h"
00014 #include "features/SimpleFeatures.h"
00015 #include "lib/io.h"
00016 
00017 CGaussianShiftKernel::CGaussianShiftKernel(
00018     int32_t size, float64_t w, int32_t ms, int32_t ss)
00019 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss)
00020 {
00021 }
00022 
00023 CGaussianShiftKernel::CGaussianShiftKernel(
00024     CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t ms, int32_t ss,
00025     int32_t size)
00026 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss)
00027 {
00028     init(l,r);
00029 }
00030 
00031 CGaussianShiftKernel::~CGaussianShiftKernel()
00032 {
00033 }
00034 
00035 float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b)
00036 {
00037     int32_t alen, blen;
00038     bool afree, bfree;
00039 
00040     float64_t* avec=
00041         ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00042     float64_t* bvec=
00043         ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00044     ASSERT(alen==blen);
00045 
00046     float64_t result = 0.0 ;
00047     float64_t sum=0.0 ;
00048     for (int32_t i=0; i<alen; i++)
00049         sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]);
00050     result += exp(-sum/width) ;
00051 
00052     for (int32_t shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++)
00053     {
00054         sum=0.0 ;
00055         for (int32_t i=0; i<alen-shift; i++)
00056             sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]);
00057         result += exp(-sum/width)/(2*s) ;
00058 
00059         sum=0.0 ;
00060         for (int32_t i=0; i<alen-shift; i++)
00061             sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]);
00062         result += exp(-sum/width)/(2*s) ;
00063     }
00064 
00065     ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00066     ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00067 
00068     return result;
00069 }

SHOGUN Machine Learning Toolbox - Documentation