001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022
023package org.biojava.stats.svm;
024
025/**
026 * This kernel implements a three layer neural net. This is calculated as:
027 *   tanh(a*k(x,y)+c)
028 *
029 * @author Matthew Pocock
030 */
031public class SigmoidKernel implements SVMKernel {
032    private double a;
033    private double c;
034    private SVMKernel kernel;
035
036    public SigmoidKernel() {
037      a = 1.0;
038      c = 1.0;
039      kernel = null;
040    }
041
042    public double evaluate(Object a, Object b) {
043      return tanh(getMultiplier()*getWrappedKernel().evaluate(a, b)
044                  + getConstant());
045    }
046
047    public double getConstant() {
048      return c;
049    }
050
051    public void setConstant(double c) {
052      this.c = c;
053    }
054    
055    public double getMultiplier() {
056      return a;
057    }
058    
059    public void setMultiplier(double m) {
060      this.a = m;
061    }
062
063    public SVMKernel getWrappedKernel() {
064      return kernel;
065    }
066    
067    public void setWrappedKernel(SVMKernel kernel) {
068      this.kernel = kernel;
069    }
070    
071    public String toString() {
072      return "Sigmoid kernel K(x, k) = tanh("
073        + getMultiplier() + ".k(x) + " + c + ")"
074        + ". k = " + getWrappedKernel().toString();
075    }
076    
077    public double tanh(double a) {
078      double x = Math.exp(a);
079      double y = Math.exp(-a);
080      
081      return (x - y) / (x + y);
082    }
083}