1 | |
---|
2 | /* |
---|
3 | * This program is free software; you can redistribute it and/or modify |
---|
4 | * it under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation; either version 2 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * This program is distributed in the hope that it will be useful, |
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | * GNU General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program; if not, write to the Free Software |
---|
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | * DiscreteEstimatorFullBayes.java |
---|
20 | * |
---|
21 | */ |
---|
22 | package weka.classifiers.bayes.net.estimate; |
---|
23 | |
---|
24 | import weka.core.RevisionUtils; |
---|
25 | import weka.estimators.DiscreteEstimator; |
---|
26 | |
---|
27 | /** |
---|
28 | * Symbolic probability estimator based on symbol counts and a prior. |
---|
29 | * |
---|
30 | * @author Remco Bouckaert (rrb@xm.co.nz) |
---|
31 | * @version $Revision: 1.3 $ |
---|
32 | */ |
---|
33 | public class DiscreteEstimatorFullBayes |
---|
34 | extends DiscreteEstimatorBayes { |
---|
35 | |
---|
36 | /** for serialization */ |
---|
37 | static final long serialVersionUID = 6774941981423312133L; |
---|
38 | |
---|
39 | /** |
---|
40 | * Constructor |
---|
41 | * |
---|
42 | * @param nSymbols the number of possible symbols (remember to include 0) |
---|
43 | * @param w1 |
---|
44 | * @param w2 |
---|
45 | * @param EmptyDist |
---|
46 | * @param ClassDist |
---|
47 | * @param fPrior |
---|
48 | */ |
---|
49 | public DiscreteEstimatorFullBayes(int nSymbols, |
---|
50 | double w1, double w2, |
---|
51 | DiscreteEstimatorBayes EmptyDist, |
---|
52 | DiscreteEstimatorBayes ClassDist, |
---|
53 | double fPrior) { |
---|
54 | |
---|
55 | super(nSymbols, fPrior); |
---|
56 | |
---|
57 | m_SumOfCounts = 0.0; |
---|
58 | for (int iSymbol = 0; iSymbol < m_nSymbols; iSymbol++) { |
---|
59 | double p1 = EmptyDist.getProbability(iSymbol); |
---|
60 | double p2 = ClassDist.getProbability(iSymbol); |
---|
61 | m_Counts[iSymbol] = w1 * p1 + w2 * p2; |
---|
62 | m_SumOfCounts += m_Counts[iSymbol]; |
---|
63 | } |
---|
64 | } // DiscreteEstimatorFullBayes |
---|
65 | |
---|
66 | /** |
---|
67 | * Returns the revision string. |
---|
68 | * |
---|
69 | * @return the revision |
---|
70 | */ |
---|
71 | public String getRevision() { |
---|
72 | return RevisionUtils.extract("$Revision: 1.3 $"); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Main method for testing this class. |
---|
77 | * |
---|
78 | * @param argv should contain a sequence of integers which |
---|
79 | * will be treated as symbolic. |
---|
80 | */ |
---|
81 | public static void main(String[] argv) { |
---|
82 | try { |
---|
83 | if (argv.length == 0) { |
---|
84 | System.out.println("Please specify a set of instances."); |
---|
85 | |
---|
86 | return; |
---|
87 | } |
---|
88 | |
---|
89 | int current = Integer.parseInt(argv[0]); |
---|
90 | int max = current; |
---|
91 | |
---|
92 | for (int i = 1; i < argv.length; i++) { |
---|
93 | current = Integer.parseInt(argv[i]); |
---|
94 | |
---|
95 | if (current > max) { |
---|
96 | max = current; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | DiscreteEstimator newEst = new DiscreteEstimator(max + 1, true); |
---|
101 | |
---|
102 | for (int i = 0; i < argv.length; i++) { |
---|
103 | current = Integer.parseInt(argv[i]); |
---|
104 | |
---|
105 | System.out.println(newEst); |
---|
106 | System.out.println("Prediction for " + current + " = " |
---|
107 | + newEst.getProbability(current)); |
---|
108 | newEst.addValue(current, 1); |
---|
109 | } |
---|
110 | } catch (Exception e) { |
---|
111 | System.out.println(e.getMessage()); |
---|
112 | } |
---|
113 | } // main |
---|
114 | |
---|
115 | } // class DiscreteEstimatorFullBayes |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|