Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bessel Kernel #48

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/libshogun/kernel/BesselKernel.cpp
@@ -0,0 +1,63 @@
#include "BesselKernel.h"
#include "lib/Mathematics.h"
#include <math.h>

using namespace shogun;

CBesselKernel::CBesselKernel():CKernel(0),distance(NULL)
{
init();
set_sigma(1.0);
}

CBesselKernel::CBesselKernel(int32_t cache, float64_t sigma, CDistance* dist)
: CKernel(cache), distance(dist), sigma(sigma)
{
init();
ASSERT(distance);
SG_REF(distance);
}

CBesselKernel::CBesselKernel(CFeatures *l, CFeatures *r, float64_t sigma, CDistance* dist)
: CKernel(10), distance(dist), sigma(sigma)
{
init();
ASSERT(distance);
SG_REF(distance);
init(l, r);
}

CBesselKernel::~CBesselKernel()
{
cleanup();
SG_UNREF(distance);
}


bool CBesselKernel::init(CFeatures* l, CFeatures* r)
{

CKernel::init(l,r);
distance->init(l,r);
return init_normalizer();
}

void CBesselKernel::load_serializable_post(void) throw (ShogunException)
{
CKernel::load_serializable_post();
}


void CBesselKernel::init()
{
ASSERT(distance);
m_parameters->add(&sigma, "sigma", "Sigma kernel parameter.");
m_parameters->add((CSGObject**) &distance, "distance", "Distance to be used.");
}

float64_t CBesselKernel::compute(int32_t idx_a, int32_t idx_b)
{
float64_t dist = distance->distance(idx_a, idx_b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry but what is j1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j1 is Bessel function of first order

return -j1(sigma*(dist*dist));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi essentially they are the same if you do some quick derivations. j1,j2 etc represent the order (http://en.wikipedia.org/wiki/Bessel_function#Bessel_functions_of_the_first_kind_:_J.CE.B1)

}

124 changes: 124 additions & 0 deletions src/libshogun/kernel/BesselKernel.h
@@ -0,0 +1,124 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Tanmoy Mukherjee
* Copyright (C) 2007-2011 Fraunhofer Institute FIRST and Max-Planck-Society
* Copyright (C) 2011 IIIT Hyderabad
*/

#include "lib/config.h"

#ifndef BESSELKERNEL_H_
#define BESSELKERNEL_H_

#include "lib/common.h"
#include "kernel/Kernel.h"
#include "distance/Distance.h"

namespace shogun {

class CDistance;
/** Bessel kernel
*
* It is described as
*
* \f[
* K(x,x') = -Bessel^{n}_{nu+1}{1}{\sigma*\|x-x'\|^2}
* \f]
*
*/


class CBesselKernel: public CKernel
{

public:
/** Default Constructor */
CBesselKernel();
/** constructor
*
* @param size cache size
* @param sigma kernel parameter sigma
* @param dist distance
*/

CBesselKernel(int32_t size, float64_t sigma, CDistance* dist);
/** initialize kernel with features
*
* @param l features of left-side
* @param r features of right-side
* @return true if successful
*/

CBesselKernel(CFeatures *l, CFeatures *r, float64_t coef, CDistance* dist);
/** initialize kernel with features
* @param l features left-side
* @param r features right-side
* @return true if successful
*/

virtual bool init(CFeatures* l, CFeatures* r);




inline virtual EKernelType get_kernel_type() { return K_BESSEL; }

/**
* @return type of features
*/
inline virtual EFeatureType get_feature_type() { return distance->get_feature_type(); }
/**
* @return class of features
*/

inline virtual EFeatureClass get_feature_class() { return distance->get_feature_class(); }

/**
* @return name of kernel
*/
inline virtual const char* get_name() const { return "Bessel"; }

inline virtual void set_sigma(float64_t s)
{
sigma=s;
}

virtual ~CBesselKernel();


protected:
/// distance to be used
CDistance* distance;

/// sigma parameter of kernel
float64_t sigma;

/**
* compute kernel for specific feature vectors
* corresponding to [idx_a] of left-side and [idx_b] of right-side
* @param idx_a left-side index
* @param idx_b right-side index
* @return kernel value
*/
virtual float64_t compute(int32_t idx_a, int32_t idx_b);

/** Can (optionally) be overridden to post-initialize some
* member variables which are not PARAMETER::ADD'ed. Make
* sure that at first the overridden method
* BASE_CLASS::LOAD_SERIALIZABLE_POST is called.
*
* @exception ShogunException Will be thrown if an error
* occurres.
*/
virtual void load_serializable_post(void) throw (ShogunException);

private:
void init();

};
}
#endif /* BESSELKERNEL_H_ */