source: tags/MetisMQIDemo/src/main/java/weka/classifiers/bayes/net/estimate/BayesNetEstimator.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

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