package jsat.distributions.kernels;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import jsat.DataSet;
import jsat.classifiers.ClassificationDataSet;
import jsat.distributions.Distribution;
import jsat.distributions.Exponential;
import jsat.distributions.LogUniform;
import jsat.distributions.Uniform;
import jsat.linear.Vec;
import jsat.linear.distancemetrics.EuclideanDistance;
import jsat.math.OnLineStatistics;
import jsat.text.GreekLetters;
import jsat.utils.DoubleList;
import jsat.utils.IntList;
import jsat.utils.ListUtils;

/**
 * Provides a kernel for the Radial Basis Function, which is of the form
 * 
* k(x, y) = exp(-||x-y||2/(2*σ2)) * * @author Edward Raff */ public class RBFKernel extends BaseL2Kernel { private static final long serialVersionUID = -6733691081172950067L; private double sigma; private double sigmaSqrd2Inv; /** * Creates a new RBF kernel with σ = 1 */ public RBFKernel() { this(1.0); } /** * Creates a new RBF kernel * @param sigma the sigma parameter */ public RBFKernel(double sigma) { setSigma(sigma); } @Override public double eval(Vec a, Vec b) { if(a == b)//Same refrence means dist of 0, exp(0) = 1 return 1; return Math.exp(-Math.pow(a.pNormDist(2, b),2) * sigmaSqrd2Inv); } @Override public double eval(int a, int b, List trainingSet, List cache) { if(a == b) return 1; return Math.exp(-getSqrdNorm(a, b, trainingSet, cache)* sigmaSqrd2Inv); } @Override public double eval(int a, Vec b, List qi, List vecs, List cache) { return Math.exp(-getSqrdNorm(a, b, qi, vecs, cache)* sigmaSqrd2Inv); } /** * Sets the sigma parameter, which must be a positive value * @param sigma the sigma value */ public void setSigma(double sigma) { if(sigma <= 0) throw new IllegalArgumentException("Sigma must be a positive constant, not " + sigma); this.sigma = sigma; this.sigmaSqrd2Inv = 0.5/(sigma*sigma); } public double getSigma() { return sigma; } @Override public String toString() { return "RBF Kernel( " + GreekLetters.sigma +" = " + sigma +")"; } @Override public RBFKernel clone() { return new RBFKernel(sigma); } /** * Another common (equivalent) form of the RBF kernel is k(x, y) = * exp(-γ||x-y||2). This method converts the σ value * used by this class to the equivalent γ value. * @param sigma the value of σ * @return the equivalent γ value. */ public static double sigmaToGamma(double sigma) { if(sigma <= 0 || Double.isNaN(sigma) || Double.isInfinite(sigma)) throw new IllegalArgumentException("sigma must be positive, not " + sigma); return 1/(2*sigma*sigma); } /** * Another common (equivalent) form of the RBF kernel is k(x, y) = * exp(-γ||x-y||2). This method converts the γ value * equivalent σ value used by this class. * @param gamma the value of γ * @return the equivalent σ value */ public static double gammToSigma(double gamma) { if(gamma <= 0 || Double.isNaN(gamma) || Double.isInfinite(gamma)) throw new IllegalArgumentException("gamma must be positive, not " + gamma); return 1/Math.sqrt(2*gamma); } /** * Guess the distribution to use for the kernel width term * {@link #setSigma(double) σ} in the RBF kernel. * * @param d the data set to get the guess for * @return the guess for the σ parameter in the RBF Kernel */ public static Distribution guessSigma(DataSet d) { return GeneralRBFKernel.guessSigma(d, new EuclideanDistance()); } @Override public boolean normalized() { return true; } }

Ads help maintain this website.