001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.math.model; 031 032import gov.sandia.cognition.learning.algorithm.bayes.VectorNaiveBayesCategorizer; 033import gov.sandia.cognition.learning.data.DefaultInputOutputPair; 034import gov.sandia.cognition.learning.data.InputOutputPair; 035import gov.sandia.cognition.math.matrix.Vector; 036import gov.sandia.cognition.math.matrix.VectorFactory; 037import gov.sandia.cognition.statistics.distribution.UnivariateGaussian.PDF; 038 039import java.util.ArrayList; 040import java.util.List; 041 042import org.openimaj.util.pair.IndependentPair; 043 044/** 045 * An implementation of a {@link EstimatableModel} that uses a 046 * {@link VectorNaiveBayesCategorizer} to associate vectors (actually double[]) 047 * with a category based on the naive bayes model. 048 * 049 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 050 * 051 * @param <T> 052 * The type of class/category predicted by the model 053 */ 054public class GaussianVectorNaiveBayesModel<T> implements EstimatableModel<double[], T> { 055 VectorNaiveBayesCategorizer.BatchGaussianLearner<T> learner = new VectorNaiveBayesCategorizer.BatchGaussianLearner<T>(); 056 private VectorNaiveBayesCategorizer<T, PDF> model; 057 058 @Override 059 public boolean estimate(List<? extends IndependentPair<double[], T>> data) { 060 final List<InputOutputPair<Vector, T>> cfdata = new ArrayList<InputOutputPair<Vector, T>>(); 061 062 for (final IndependentPair<double[], T> d : data) { 063 final InputOutputPair<Vector, T> iop = new DefaultInputOutputPair<Vector, T>(VectorFactory.getDefault() 064 .copyArray(d.firstObject()), d.secondObject()); 065 cfdata.add(iop); 066 } 067 068 model = learner.learn(cfdata); 069 070 return true; 071 } 072 073 @Override 074 public T predict(double[] data) { 075 return model.evaluate(VectorFactory.getDefault().copyArray(data)); 076 } 077 078 @Override 079 public int numItemsToEstimate() { 080 return 0; 081 } 082 083 @Override 084 @SuppressWarnings("unchecked") 085 public GaussianVectorNaiveBayesModel<T> clone() { 086 try { 087 return (GaussianVectorNaiveBayesModel<T>) super.clone(); 088 } catch (final CloneNotSupportedException e) { 089 throw new RuntimeException(e); 090 } 091 } 092 093 /** 094 * Testing 095 * 096 * @param args 097 */ 098 public static void main(String[] args) { 099 final GaussianVectorNaiveBayesModel<Boolean> model = new GaussianVectorNaiveBayesModel<Boolean>(); 100 101 final List<IndependentPair<double[], Boolean>> data = new ArrayList<IndependentPair<double[], Boolean>>(); 102 103 data.add(IndependentPair.pair(new double[] { 0 }, true)); 104 data.add(IndependentPair.pair(new double[] { 0.1 }, true)); 105 data.add(IndependentPair.pair(new double[] { -0.1 }, true)); 106 107 data.add(IndependentPair.pair(new double[] { 9.9 }, false)); 108 data.add(IndependentPair.pair(new double[] { 10 }, false)); 109 data.add(IndependentPair.pair(new double[] { 10.1 }, false)); 110 111 model.estimate(data); 112 113 final double[] q = new double[] { 5.0 }; 114 115 System.out.println(model.predict(q)); 116 117 System.out.println(" logP(true): " 118 + model.model.computeLogPosterior(VectorFactory.getDefault().copyArray(q), true)); 119 System.out.println("logP(false): " 120 + model.model.computeLogPosterior(VectorFactory.getDefault().copyArray(q), false)); 121 122 System.out.println(" P(true): " + model.model.computePosterior(VectorFactory.getDefault().copyArray(q), true)); 123 System.out 124 .println(" P(false): " + model.model.computePosterior(VectorFactory.getDefault().copyArray(q), false)); 125 126 System.out.println(model.model.getPriors()); 127 } 128}