~ubuntu-branches/ubuntu/vivid/weka/vivid

« back to all changes in this revision

Viewing changes to weka/classifiers/bayes/net/estimate/BayesNetEstimator.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/*
 
18
 * BayesNetEstimator.java
 
19
 * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
 
20
 * 
 
21
 */
 
22
 
 
23
package weka.classifiers.bayes.net.estimate;
 
24
 
 
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;
 
30
 
 
31
import java.io.Serializable;
 
32
import java.util.Enumeration;
 
33
import java.util.Vector;
 
34
 
 
35
/** 
 
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.
 
38
 * <p/>
 
39
 <!-- globalinfo-end -->
 
40
 *
 
41
 <!-- options-start -->
 
42
 * Valid options are: <p/>
 
43
 * 
 
44
 * <pre> -A &lt;alpha&gt;
 
45
 *  Initial count (alpha)
 
46
 * </pre>
 
47
 * 
 
48
 <!-- options-end -->
 
49
 * 
 
50
 * @author Remco Bouckaert (rrb@xm.co.nz)
 
51
 * @version $Revision: 1.3 $
 
52
 */
 
53
public class BayesNetEstimator 
 
54
    implements OptionHandler, Serializable {
 
55
  
 
56
    /** for serialization */
 
57
    static final long serialVersionUID = 2184330197666253884L;
 
58
    
 
59
    /**
 
60
     * Holds prior on count
 
61
     */
 
62
    protected double m_fAlpha = 0.5;
 
63
 
 
64
    /**
 
65
     * estimateCPTs estimates the conditional probability tables for the Bayes
 
66
     * Net using the network structure.
 
67
     * 
 
68
     * @param bayesNet the bayes net to use
 
69
     * @throws Exception always throws an exception, since subclass needs to be used
 
70
     */
 
71
    public void estimateCPTs(BayesNet bayesNet) throws Exception {
 
72
        throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
 
73
    }
 
74
 
 
75
    /**
 
76
     * Updates the classifier with the given instance.
 
77
     * 
 
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
 
81
     */
 
82
    public void updateClassifier(BayesNet bayesNet, Instance instance) throws Exception {
 
83
        throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
 
84
    }
 
85
 
 
86
    /**
 
87
     * Calculates the class membership probabilities for the given test
 
88
     * instance.
 
89
     * 
 
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
 
94
     */
 
95
    public double[] distributionForInstance(BayesNet bayesNet, Instance instance) throws Exception {
 
96
        throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
 
97
    }
 
98
 
 
99
    /** 
 
100
     * initCPTs reserves space for CPTs and set all counts to zero
 
101
     * 
 
102
     * @param bayesNet the bayes net to use
 
103
     * @throws Exception always throws an exception, since subclass needs to be used
 
104
     */
 
105
    public void initCPTs(BayesNet bayesNet) throws Exception {
 
106
        throw new Exception("Incorrect BayesNetEstimator: use subclass instead.");
 
107
    } // initCPTs
 
108
 
 
109
    /**
 
110
     * Returns an enumeration describing the available options
 
111
     * 
 
112
     * @return an enumeration of all the available options
 
113
     */
 
114
    public Enumeration listOptions() {
 
115
        Vector newVector = new Vector(1);
 
116
 
 
117
        newVector.addElement(new Option("\tInitial count (alpha)\n", "A", 1, "-A <alpha>"));
 
118
 
 
119
        return newVector.elements();
 
120
    } // listOptions
 
121
 
 
122
    /**
 
123
     * Parses a given list of options. <p/>
 
124
     *
 
125
     <!-- options-start -->
 
126
     * Valid options are: <p/>
 
127
     * 
 
128
     * <pre> -A &lt;alpha&gt;
 
129
     *  Initial count (alpha)
 
130
     * </pre>
 
131
     * 
 
132
     <!-- options-end -->
 
133
     * 
 
134
     * @param options the list of options as an array of strings
 
135
     * @throws Exception if an option is not supported
 
136
     */
 
137
    public void setOptions(String[] options) throws Exception {
 
138
        String sAlpha = Utils.getOption('A', options);
 
139
 
 
140
        if (sAlpha.length() != 0) {
 
141
            m_fAlpha = (new Float(sAlpha)).floatValue();
 
142
        } else {
 
143
            m_fAlpha = 0.5f;
 
144
        }
 
145
 
 
146
        Utils.checkForRemainingOptions(options);
 
147
    } // setOptions
 
148
 
 
149
    /**
 
150
     * Gets the current settings of the classifier.
 
151
     * 
 
152
     * @return an array of strings suitable for passing to setOptions
 
153
     */
 
154
    public String[] getOptions() {
 
155
        String[] options = new String[2];
 
156
        int current = 0;
 
157
 
 
158
        options[current++] = "-A";
 
159
        options[current++] = "" + m_fAlpha;
 
160
 
 
161
        return options;
 
162
    } // getOptions
 
163
 
 
164
    /**
 
165
     * Set prior used in probability table estimation
 
166
     * @param fAlpha representing prior
 
167
     */
 
168
    public void setAlpha(double fAlpha) {
 
169
        m_fAlpha = fAlpha;
 
170
    }
 
171
 
 
172
    /**
 
173
     * Get prior used in probability table estimation
 
174
     * @return prior
 
175
     */
 
176
    public double getAlpha() {
 
177
        return m_fAlpha;
 
178
    }
 
179
 
 
180
 
 
181
    /**
 
182
     * @return a string to describe the Alpha option.
 
183
     */
 
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.";
 
187
    }
 
188
 
 
189
    /**
 
190
     * This will return a string describing the class.
 
191
     * @return The string.
 
192
     */
 
193
    public String globalInfo() {
 
194
        return 
 
195
            "BayesNetEstimator is the base class for estimating the "
 
196
          + "conditional probability tables of a Bayes network once the "
 
197
          + "structure has been learned.";
 
198
    }
 
199
 
 
200
} // BayesNetEstimator