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.
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.
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.
18
* NDConditionalEstimator.java
19
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
23
package weka.estimators;
26
* Conditional probability estimator for a numeric domain conditional upon
27
* a discrete domain (utilises separate normal estimators for each discrete
28
* conditioning value).
30
* @author Len Trigg (trigg@cs.waikato.ac.nz)
31
* @version $Revision: 1.6 $
33
public class NDConditionalEstimator implements ConditionalEstimator {
35
/** Hold the sub-estimators */
36
private NormalEstimator [] m_Estimators;
41
* @param numCondSymbols the number of conditioning symbols
42
* @param precision the precision to which numeric values are given. For
43
* example, if the precision is stated to be 0.1, the values in the
44
* interval (0.25,0.35] are all treated as 0.3.
46
public NDConditionalEstimator(int numCondSymbols, double precision) {
48
m_Estimators = new NormalEstimator [numCondSymbols];
49
for(int i = 0; i < numCondSymbols; i++) {
50
m_Estimators[i] = new NormalEstimator(precision);
55
* Add a new data value to the current estimator.
57
* @param data the new data value
58
* @param given the new value that data is conditional upon
59
* @param weight the weight assigned to the data value
61
public void addValue(double data, double given, double weight) {
63
m_Estimators[(int)given].addValue(data, weight);
67
* Get a probability estimator for a value
69
* @param given the new value that data is conditional upon
70
* @return the estimator for the supplied value given the condition
72
public Estimator getEstimator(double given) {
74
return m_Estimators[(int)given];
78
* Get a probability estimate for a value
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
84
public double getProbability(double data, double given) {
86
return getEstimator(given).getProbability(data);
90
* Display a representation of this estimator
92
public String toString() {
94
String result = "ND Conditional Estimator. "
95
+ m_Estimators.length + " sub-estimators:\n";
96
for(int i = 0; i < m_Estimators.length; i++) {
97
result += "Sub-estimator " + i + ": " + m_Estimators[i];
103
* Main method for testing this class.
105
* @param argv should contain a sequence of pairs of integers which
106
* will be treated as numeric, symbolic.
108
public static void main(String [] argv) {
111
if (argv.length == 0) {
112
System.out.println("Please specify a set of instances.");
115
int currentA = Integer.parseInt(argv[0]);
117
int currentB = Integer.parseInt(argv[1]);
119
for(int i = 2; i < argv.length - 1; i += 2) {
120
currentA = Integer.parseInt(argv[i]);
121
currentB = Integer.parseInt(argv[i + 1]);
122
if (currentA > maxA) {
125
if (currentB > maxB) {
129
NDConditionalEstimator newEst = new NDConditionalEstimator(maxB + 1,
131
for(int i = 0; i < argv.length - 1; i += 2) {
132
currentA = Integer.parseInt(argv[i]);
133
currentB = Integer.parseInt(argv[i + 1]);
134
System.out.println(newEst);
135
System.out.println("Prediction for " + currentA + '|' + currentB
137
+ newEst.getProbability(currentA, currentB));
138
newEst.addValue(currentA, currentB, 1);
140
} catch (Exception e) {
141
System.out.println(e.getMessage());