KDConditionalEstimator.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.  *    KDConditionalEstimator.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 kernel 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 KDConditionalEstimator implements ConditionalEstimator {
  33.   /** Hold the sub-estimators */
  34.   private KernelEstimator [] 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 KDConditionalEstimator(int numCondSymbols, double precision) {
  44.     m_Estimators = new KernelEstimator [numCondSymbols];
  45.     for(int i = 0; i < numCondSymbols; i++) {
  46.       m_Estimators[i] = new KernelEstimator(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.   /** Display a representation of this estimator */
  80.   public String toString() {
  81.     String result = "KD Conditional Estimator. " 
  82.       + m_Estimators.length + " sub-estimators:n";
  83.     for(int i = 0; i < m_Estimators.length; i++) {
  84.       result += "Sub-estimator " + i + ": " + m_Estimators[i];
  85.     }
  86.     return result;
  87.   }
  88.   /**
  89.    * Main method for testing this class.
  90.    *
  91.    * @param argv should contain a sequence of pairs of integers which
  92.    * will be treated as numeric, symbolic.
  93.    */
  94.   public static void main(String [] argv) {
  95.     
  96.     try {
  97.       if (argv.length == 0) {
  98. System.out.println("Please specify a set of instances.");
  99. return;
  100.       }
  101.       int currentA = Integer.parseInt(argv[0]);
  102.       int maxA = currentA;
  103.       int currentB = Integer.parseInt(argv[1]);
  104.       int maxB = currentB;
  105.       for(int i = 2; i < argv.length - 1; i += 2) {
  106. currentA = Integer.parseInt(argv[i]);
  107. currentB = Integer.parseInt(argv[i + 1]);
  108. if (currentA > maxA) {
  109.   maxA = currentA;
  110. }
  111. if (currentB > maxB) {
  112.   maxB = currentB;
  113. }
  114.       }
  115.       KDConditionalEstimator newEst = new KDConditionalEstimator(maxB + 1,
  116.  1);
  117.       for(int i = 0; i < argv.length - 1; i += 2) {
  118. currentA = Integer.parseInt(argv[i]);
  119. currentB = Integer.parseInt(argv[i + 1]);
  120. System.out.println(newEst);
  121. System.out.println("Prediction for " + currentA + '|' + currentB 
  122.    + " = "
  123.    + newEst.getProbability(currentA, currentB));
  124. newEst.addValue(currentA, currentB, 1);
  125.       }
  126.     } catch (Exception e) {
  127.       System.out.println(e.getMessage());
  128.     }
  129.   }
  130. }