001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * DiscreteEstimatorFullBayes.java
019: *
020: */
021: package weka.classifiers.bayes.net.estimate;
022:
023: import weka.estimators.DiscreteEstimator;
024:
025: /**
026: * Symbolic probability estimator based on symbol counts and a prior.
027: *
028: * @author Remco Bouckaert (rrb@xm.co.nz)
029: * @version $Revision: 1.2 $
030: */
031: public class DiscreteEstimatorFullBayes extends DiscreteEstimatorBayes {
032:
033: /** for serialization */
034: static final long serialVersionUID = 6774941981423312133L;
035:
036: /**
037: * Constructor
038: *
039: * @param nSymbols the number of possible symbols (remember to include 0)
040: * @param w1
041: * @param w2
042: * @param EmptyDist
043: * @param ClassDist
044: * @param fPrior
045: */
046: public DiscreteEstimatorFullBayes(int nSymbols, double w1,
047: double w2, DiscreteEstimatorBayes EmptyDist,
048: DiscreteEstimatorBayes ClassDist, double fPrior) {
049:
050: super (nSymbols, fPrior);
051:
052: m_SumOfCounts = 0.0;
053: for (int iSymbol = 0; iSymbol < m_nSymbols; iSymbol++) {
054: double p1 = EmptyDist.getProbability(iSymbol);
055: double p2 = ClassDist.getProbability(iSymbol);
056: m_Counts[iSymbol] = w1 * p1 + w2 * p2;
057: m_SumOfCounts += m_Counts[iSymbol];
058: }
059: } // DiscreteEstimatorFullBayes
060:
061: /**
062: * Main method for testing this class.
063: *
064: * @param argv should contain a sequence of integers which
065: * will be treated as symbolic.
066: */
067: public static void main(String[] argv) {
068: try {
069: if (argv.length == 0) {
070: System.out
071: .println("Please specify a set of instances.");
072:
073: return;
074: }
075:
076: int current = Integer.parseInt(argv[0]);
077: int max = current;
078:
079: for (int i = 1; i < argv.length; i++) {
080: current = Integer.parseInt(argv[i]);
081:
082: if (current > max) {
083: max = current;
084: }
085: }
086:
087: DiscreteEstimator newEst = new DiscreteEstimator(max + 1,
088: true);
089:
090: for (int i = 0; i < argv.length; i++) {
091: current = Integer.parseInt(argv[i]);
092:
093: System.out.println(newEst);
094: System.out.println("Prediction for " + current + " = "
095: + newEst.getProbability(current));
096: newEst.addValue(current, 1);
097: }
098: } catch (Exception e) {
099: System.out.println(e.getMessage());
100: }
101: } // main
102:
103: } // class DiscreteEstimatorFullBayes
|