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
* BayesNetEstimator.java
19
* Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
23
package weka.classifiers.bayes.net.estimate;
25
import weka.classifiers.bayes.BayesNet;
26
import weka.core.Instance;
27
import weka.core.Option;
28
import weka.core.OptionHandler;
29
import weka.core.Utils;
31
import java.io.Serializable;
32
import java.util.Enumeration;
33
import java.util.Vector;
36
<!-- globalinfo-start -->
37
* BayesNetEstimator is the base class for estimating the conditional probability tables of a Bayes network once the structure has been learned.
39
<!-- globalinfo-end -->
41
<!-- options-start -->
42
* Valid options are: <p/>
44
* <pre> -A <alpha>
45
* Initial count (alpha)
50
* @author Remco Bouckaert (rrb@xm.co.nz)
51
* @version $Revision: 1.3 $
53
public class BayesNetEstimator
54
implements OptionHandler, Serializable {
56
/** for serialization */
57
static final long serialVersionUID = 2184330197666253884L;
60
* Holds prior on count
62
protected double m_fAlpha = 0.5;
65
* estimateCPTs estimates the conditional probability tables for the Bayes
66
* Net using the network structure.
68
* @param bayesNet the bayes net to use
69
* @throws Exception always throws an exception, since subclass needs to be used
71
public void estimateCPTs(BayesNet bayesNet) throws Exception {
72
throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
76
* Updates the classifier with the given instance.
78
* @param bayesNet the bayes net to use
79
* @param instance the new training instance to include in the model
80
* @throws Exception always throws an exception, since subclass needs to be used
82
public void updateClassifier(BayesNet bayesNet, Instance instance) throws Exception {
83
throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
87
* Calculates the class membership probabilities for the given test
90
* @param bayesNet the bayes net to use
91
* @param instance the instance to be classified
92
* @return predicted class probability distribution
93
* @throws Exception always throws an exception, since subclass needs to be used
95
public double[] distributionForInstance(BayesNet bayesNet, Instance instance) throws Exception {
96
throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
100
* initCPTs reserves space for CPTs and set all counts to zero
102
* @param bayesNet the bayes net to use
103
* @throws Exception always throws an exception, since subclass needs to be used
105
public void initCPTs(BayesNet bayesNet) throws Exception {
106
throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
110
* Returns an enumeration describing the available options
112
* @return an enumeration of all the available options
114
public Enumeration listOptions() {
115
Vector newVector = new Vector(1);
117
newVector.addElement(new Option("\tInitial count (alpha)\n", "A", 1, "-A <alpha>"));
119
return newVector.elements();
123
* Parses a given list of options. <p/>
125
<!-- options-start -->
126
* Valid options are: <p/>
128
* <pre> -A <alpha>
129
* Initial count (alpha)
134
* @param options the list of options as an array of strings
135
* @throws Exception if an option is not supported
137
public void setOptions(String[] options) throws Exception {
138
String sAlpha = Utils.getOption('A', options);
140
if (sAlpha.length() != 0) {
141
m_fAlpha = (new Float(sAlpha)).floatValue();
146
Utils.checkForRemainingOptions(options);
150
* Gets the current settings of the classifier.
152
* @return an array of strings suitable for passing to setOptions
154
public String[] getOptions() {
155
String[] options = new String[2];
158
options[current++] = "-A";
159
options[current++] = "" + m_fAlpha;
165
* Set prior used in probability table estimation
166
* @param fAlpha representing prior
168
public void setAlpha(double fAlpha) {
173
* Get prior used in probability table estimation
176
public double getAlpha() {
182
* @return a string to describe the Alpha option.
184
public String alphaTipText() {
185
return "Alpha is used for estimating the probability tables and can be interpreted"
186
+ " as the initial count on each value.";
190
* This will return a string describing the class.
191
* @return The string.
193
public String globalInfo() {
195
"BayesNetEstimator is the base class for estimating the "
196
+ "conditional probability tables of a Bayes network once the "
197
+ "structure has been learned.";
200
} // BayesNetEstimator