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: * NDConditionalEstimator.java
019: * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.estimators;
024:
025: /**
026: * Conditional probability estimator for a numeric domain conditional upon
027: * a discrete domain (utilises separate normal estimators for each discrete
028: * conditioning value).
029: *
030: * @author Len Trigg (trigg@cs.waikato.ac.nz)
031: * @version $Revision: 1.6 $
032: */
033: public class NDConditionalEstimator implements ConditionalEstimator {
034:
035: /** Hold the sub-estimators */
036: private NormalEstimator[] m_Estimators;
037:
038: /**
039: * Constructor
040: *
041: * @param numCondSymbols the number of conditioning symbols
042: * @param precision the precision to which numeric values are given. For
043: * example, if the precision is stated to be 0.1, the values in the
044: * interval (0.25,0.35] are all treated as 0.3.
045: */
046: public NDConditionalEstimator(int numCondSymbols, double precision) {
047:
048: m_Estimators = new NormalEstimator[numCondSymbols];
049: for (int i = 0; i < numCondSymbols; i++) {
050: m_Estimators[i] = new NormalEstimator(precision);
051: }
052: }
053:
054: /**
055: * Add a new data value to the current estimator.
056: *
057: * @param data the new data value
058: * @param given the new value that data is conditional upon
059: * @param weight the weight assigned to the data value
060: */
061: public void addValue(double data, double given, double weight) {
062:
063: m_Estimators[(int) given].addValue(data, weight);
064: }
065:
066: /**
067: * Get a probability estimator for a value
068: *
069: * @param given the new value that data is conditional upon
070: * @return the estimator for the supplied value given the condition
071: */
072: public Estimator getEstimator(double given) {
073:
074: return m_Estimators[(int) given];
075: }
076:
077: /**
078: * Get a probability estimate for a value
079: *
080: * @param data the value to estimate the probability of
081: * @param given the new value that data is conditional upon
082: * @return the estimated probability of the supplied value
083: */
084: public double getProbability(double data, double given) {
085:
086: return getEstimator(given).getProbability(data);
087: }
088:
089: /**
090: * Display a representation of this estimator
091: */
092: public String toString() {
093:
094: String result = "ND Conditional Estimator. "
095: + m_Estimators.length + " sub-estimators:\n";
096: for (int i = 0; i < m_Estimators.length; i++) {
097: result += "Sub-estimator " + i + ": " + m_Estimators[i];
098: }
099: return result;
100: }
101:
102: /**
103: * Main method for testing this class.
104: *
105: * @param argv should contain a sequence of pairs of integers which
106: * will be treated as numeric, symbolic.
107: */
108: public static void main(String[] argv) {
109:
110: try {
111: if (argv.length == 0) {
112: System.out
113: .println("Please specify a set of instances.");
114: return;
115: }
116: int currentA = Integer.parseInt(argv[0]);
117: int maxA = currentA;
118: int currentB = Integer.parseInt(argv[1]);
119: int maxB = currentB;
120: for (int i = 2; i < argv.length - 1; i += 2) {
121: currentA = Integer.parseInt(argv[i]);
122: currentB = Integer.parseInt(argv[i + 1]);
123: if (currentA > maxA) {
124: maxA = currentA;
125: }
126: if (currentB > maxB) {
127: maxB = currentB;
128: }
129: }
130: NDConditionalEstimator newEst = new NDConditionalEstimator(
131: maxB + 1, 1);
132: for (int i = 0; i < argv.length - 1; i += 2) {
133: currentA = Integer.parseInt(argv[i]);
134: currentB = Integer.parseInt(argv[i + 1]);
135: System.out.println(newEst);
136: System.out.println("Prediction for " + currentA + '|'
137: + currentB + " = "
138: + newEst.getProbability(currentA, currentB));
139: newEst.addValue(currentA, currentB, 1);
140: }
141: } catch (Exception e) {
142: System.out.println(e.getMessage());
143: }
144: }
145:
146: }
|