DNConditionalEstimator.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
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.  *    DNConditionalEstimator.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 numeric domain.
  27.  *
  28.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  29.  * @version $Revision: 1.4 $
  30.  */
  31. public class DNConditionalEstimator implements ConditionalEstimator {
  32.   /** Hold the sub-estimators */
  33.   private NormalEstimator [] m_Estimators;
  34.   /** Hold the weights for each of the sub-estimators */
  35.   private DiscreteEstimator m_Weights;
  36.   /**
  37.    * Constructor
  38.    *
  39.    * @param numSymbols the number of symbols 
  40.    * @param precision the  precision to which numeric values are given. For
  41.    * example, if the precision is stated to be 0.1, the values in the
  42.    * interval (0.25,0.35] are all treated as 0.3. 
  43.    */
  44.   public DNConditionalEstimator(int numSymbols, double precision) {
  45.     m_Estimators = new NormalEstimator [numSymbols];
  46.     for(int i = 0; i < numSymbols; i++) {
  47. m_Estimators[i] = new NormalEstimator(precision);
  48.     }
  49.     m_Weights = new DiscreteEstimator(numSymbols, true);
  50.   }
  51.   /**
  52.    * Add a new data value to the current estimator.
  53.    *
  54.    * @param data the new data value 
  55.    * @param given the new value that data is conditional upon 
  56.    * @param weight the weight assigned to the data value 
  57.    */
  58.   public void addValue(double data, double given, double weight) {
  59.     m_Estimators[(int)data].addValue(given, weight);
  60.     m_Weights.addValue((int)data, weight);
  61.   }
  62.   /**
  63.    * Get a probability estimator for a value
  64.    *
  65.    * @param data the value to estimate the probability of
  66.    * @param given the new value that data is conditional upon 
  67.    * @return the estimator for the supplied value given the condition
  68.    */
  69.   public Estimator getEstimator(double given) {
  70.     Estimator result = new DiscreteEstimator(m_Estimators.length,false);
  71.     for(int i = 0; i < m_Estimators.length; i++) {
  72.       result.addValue(i,m_Weights.getProbability(i)
  73.       *m_Estimators[i].getProbability(given));
  74.     }
  75.     return result;
  76.   }
  77.   /**
  78.    * Get a probability estimate for a value
  79.    *
  80.    * @param data the value to estimate the probability of
  81.    * @param given the new value that data is conditional upon 
  82.    * @return the estimated probability of the supplied value
  83.    */
  84.   public double getProbability(double data, double given) {
  85.     return getEstimator(given).getProbability(data);
  86.   }
  87.   /** Display a representation of this estimator */
  88.   public String toString() {
  89.     String result = "DN Conditional Estimator. " 
  90.       + m_Estimators.length + " sub-estimators:n";
  91.     for(int i = 0; i < m_Estimators.length; i++) {
  92.       result += "Sub-estimator " + i + ": " + m_Estimators[i];
  93.     }
  94.     result += "Weights of each estimator given by " + m_Weights;
  95.     return result;
  96.   }
  97.   /**
  98.    * Main method for testing this class.
  99.    *
  100.    * @param argv should contain a sequence of pairs of integers which
  101.    * will be treated as pairs of symbolic, numeric.
  102.    */
  103.   public static void main(String [] argv) {
  104.     
  105.     try {
  106.       if (argv.length == 0) {
  107. System.out.println("Please specify a set of instances.");
  108. return;
  109.       }
  110.       int currentA = Integer.parseInt(argv[0]);
  111.       int maxA = currentA;
  112.       int currentB = Integer.parseInt(argv[1]);
  113.       int maxB = currentB;
  114.       for(int i = 2; i < argv.length - 1; i += 2) {
  115. currentA = Integer.parseInt(argv[i]);
  116. currentB = Integer.parseInt(argv[i + 1]);
  117. if (currentA > maxA) {
  118.   maxA = currentA;
  119. }
  120. if (currentB > maxB) {
  121.   maxB = currentB;
  122. }
  123.       }
  124.       DNConditionalEstimator newEst = new DNConditionalEstimator(maxA + 1,
  125.  1);
  126.       for(int i = 0; i < argv.length - 1; i += 2) {
  127. currentA = Integer.parseInt(argv[i]);
  128. currentB = Integer.parseInt(argv[i + 1]);
  129. System.out.println(newEst);
  130. System.out.println("Prediction for " + currentA + '|' + currentB 
  131.    + " = "
  132.    + newEst.getProbability(currentA, currentB));
  133. newEst.addValue(currentA, currentB, 1);
  134.       }
  135.     } catch (Exception e) {
  136.       System.out.println(e.getMessage());
  137.     }
  138.   }
  139. }