All Downloads are FREE. Search and download functionalities are using the official Maven repository.

smile.math.kernel.BinarySparseGaussianKernel Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
/******************************************************************************
 *                   Confidential Proprietary                                 *
 *         (c) Copyright Haifeng Li 2011, All Rights Reserved                 *
 ******************************************************************************/

package smile.math.kernel;

import smile.math.Math;

/**
 * The Gaussian Mercer Kernel. k(u, v) = e-||u-v||2 / (2 * σ2),
 * where σ > 0 is the scale parameter of the kernel. The kernel works
 * on sparse binary array as int[], which are the indices of nonzero elements.
 * 

* The Gaussian kernel is a good choice for a great deal of applications, * although sometimes it is remarked as being overused. * @author Haifeng Li */ public class BinarySparseGaussianKernel implements MercerKernel { /** * The width of the kernel. */ private double gamma; /** * Constructor. * @param sigma the smooth/width parameter of Gaussian kernel. */ public BinarySparseGaussianKernel(double sigma) { if (sigma <= 0) throw new IllegalArgumentException("sigma is not positive."); this.gamma = 0.5 / (sigma * sigma); } @Override public String toString() { return String.format("Sparse Binary Gaussian Kernel (\u02E0 = %.4f)", Math.sqrt(0.5/gamma)); } @Override public double k(int[] x, int[] y) { double d = 0.0; int p1 = 0, p2 = 0; while (p1 < x.length && p2 < y.length) { int i1 = x[p1]; int i2 = y[p2]; if (i1 == i2) { p1++; p2++; } else if (i1 > i2) { d++; p2++; } else { d++; p1++; } } d += x.length - p1; d += y.length - p2; return Math.exp(-gamma * d); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy