DDConditionalEstimator.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    DDConditionalEstimator.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.estimators;
  22. import java.util.*;
  23. import weka.core.*;
  24. /** 
  25.  * Conditional probability estimator for a discrete domain conditional upon
  26.  * a discrete domain.
  27.  *
  28.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  29.  * @version $Revision: 1.4 $
  30.  */
  31. public class DDConditionalEstimator implements ConditionalEstimator {
  32.   /** Hold the sub-estimators */
  33.   private DiscreteEstimator [] m_Estimators;
  34.   /**
  35.    * Constructor
  36.    *
  37.    * @param numSymbols the number of possible symbols (remember to include 0)
  38.    * @param numCondSymbols the number of conditioning symbols 
  39.    * @param laplace if true, sub-estimators will use laplace
  40.    */
  41.   public DDConditionalEstimator(int numSymbols, int numCondSymbols,
  42. boolean laplace) {
  43.     
  44.     m_Estimators = new DiscreteEstimator [numCondSymbols];
  45.     for(int i = 0; i < numCondSymbols; i++) {
  46.       m_Estimators[i] = new DiscreteEstimator(numSymbols, laplace);
  47.     }
  48.   }
  49.   /**
  50.    * Add a new data value to the current estimator.
  51.    *
  52.    * @param data the new data value 
  53.    * @param given the new value that data is conditional upon 
  54.    * @param weight the weight assigned to the data value 
  55.    */
  56.   public void addValue(double data, double given, double weight) {
  57.     
  58.     m_Estimators[(int)given].addValue(data, weight);
  59.   }
  60.   /**
  61.    * Get a probability estimator for a value
  62.    *
  63.    * @param data the value to estimate the probability of
  64.    * @param given the new value that data is conditional upon 
  65.    * @return the estimator for the supplied value given the condition
  66.    */
  67.   public Estimator getEstimator(double given) {
  68.     
  69.     return m_Estimators[(int)given];
  70.   }
  71.   /**
  72.    * Get a probability estimate for a value
  73.    *
  74.    * @param data the value to estimate the probability of
  75.    * @param given the new value that data is conditional upon 
  76.    * @return the estimated probability of the supplied value
  77.    */
  78.   public double getProbability(double data, double given) {
  79.     
  80.     return getEstimator(given).getProbability(data);
  81.   }
  82.   /** Display a representation of this estimator */
  83.   public String toString() {
  84.     
  85.     String result = "DD Conditional Estimator. " 
  86.       + m_Estimators.length + " sub-estimators:n";
  87.     for(int i = 0; i < m_Estimators.length; i++) {
  88.       result += "Sub-estimator " + i + ": " + m_Estimators[i];
  89.     }
  90.     return result;
  91.   }
  92.   /**
  93.    * Main method for testing this class.
  94.    *
  95.    * @param argv should contain a sequence of pairs of integers which
  96.    * will be treated as symbolic.
  97.    */
  98.   public static void main(String [] argv) {
  99.     
  100.     try {
  101.       if (argv.length == 0) {
  102. System.out.println("Please specify a set of instances.");
  103. return;
  104.       }
  105.       int currentA = Integer.parseInt(argv[0]);
  106.       int maxA = currentA;
  107.       int currentB = Integer.parseInt(argv[1]);
  108.       int maxB = currentB;
  109.       for(int i = 2; i < argv.length - 1; i += 2) {
  110. currentA = Integer.parseInt(argv[i]);
  111. currentB = Integer.parseInt(argv[i + 1]);
  112. if (currentA > maxA) {
  113.   maxA = currentA;
  114. }
  115. if (currentB > maxB) {
  116.   maxB = currentB;
  117. }
  118.       }
  119.       DDConditionalEstimator newEst = new DDConditionalEstimator(maxA + 1,
  120.  maxB + 1,
  121.  true);
  122.       for(int i = 0; i < argv.length - 1; i += 2) {
  123. currentA = Integer.parseInt(argv[i]);
  124. currentB = Integer.parseInt(argv[i + 1]);
  125. System.out.println(newEst);
  126. System.out.println("Prediction for " + currentA + '|' + currentB 
  127.    + " = "
  128.    + newEst.getProbability(currentA, currentB));
  129. newEst.addValue(currentA, currentB, 1);
  130.       }
  131.     } catch (Exception e) {
  132.       System.out.println(e.getMessage());
  133.     }
  134.   }
  135. }