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