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 | * DKConditionalEstimator.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.estimators; |
---|
24 | |
---|
25 | import weka.core.RevisionUtils; |
---|
26 | |
---|
27 | /** |
---|
28 | * Conditional probability estimator for a discrete domain conditional upon |
---|
29 | * a numeric domain. |
---|
30 | * |
---|
31 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
32 | * @version $Revision: 1.8 $ |
---|
33 | */ |
---|
34 | public class DKConditionalEstimator implements ConditionalEstimator { |
---|
35 | |
---|
36 | /** Hold the sub-estimators */ |
---|
37 | private KernelEstimator [] m_Estimators; |
---|
38 | |
---|
39 | /** Hold the weights for each of the sub-estimators */ |
---|
40 | private DiscreteEstimator m_Weights; |
---|
41 | |
---|
42 | /** |
---|
43 | * Constructor |
---|
44 | * |
---|
45 | * @param numSymbols the number of symbols |
---|
46 | * @param precision the precision to which numeric values are given. For |
---|
47 | * example, if the precision is stated to be 0.1, the values in the |
---|
48 | * interval (0.25,0.35] are all treated as 0.3. |
---|
49 | */ |
---|
50 | public DKConditionalEstimator(int numSymbols, double precision) { |
---|
51 | |
---|
52 | m_Estimators = new KernelEstimator [numSymbols]; |
---|
53 | for(int i = 0; i < numSymbols; i++) { |
---|
54 | m_Estimators[i] = new KernelEstimator(precision); |
---|
55 | } |
---|
56 | m_Weights = new DiscreteEstimator(numSymbols, true); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Add a new data value to the current estimator. |
---|
61 | * |
---|
62 | * @param data the new data value |
---|
63 | * @param given the new value that data is conditional upon |
---|
64 | * @param weight the weight assigned to the data value |
---|
65 | */ |
---|
66 | public void addValue(double data, double given, double weight) { |
---|
67 | |
---|
68 | m_Estimators[(int)data].addValue(given, weight); |
---|
69 | m_Weights.addValue((int)data, weight); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Get a probability estimator for a value |
---|
74 | * |
---|
75 | * @param given the new value that data is conditional upon |
---|
76 | * @return the estimator for the supplied value given the condition |
---|
77 | */ |
---|
78 | public Estimator getEstimator(double given) { |
---|
79 | |
---|
80 | Estimator result = new DiscreteEstimator(m_Estimators.length,false); |
---|
81 | for(int i = 0; i < m_Estimators.length; i++) { |
---|
82 | //System.out.println("Val " + i |
---|
83 | // + " Weight:" + m_Weights.getProbability(i) |
---|
84 | // +" EstProb(" + given + ")=" |
---|
85 | // + m_Estimators[i].getProbability(given)); |
---|
86 | result.addValue(i, m_Weights.getProbability(i) |
---|
87 | * m_Estimators[i].getProbability(given)); |
---|
88 | } |
---|
89 | return result; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Get a probability estimate for a value |
---|
94 | * |
---|
95 | * @param data the value to estimate the probability of |
---|
96 | * @param given the new value that data is conditional upon |
---|
97 | * @return the estimated probability of the supplied value |
---|
98 | */ |
---|
99 | public double getProbability(double data, double given) { |
---|
100 | |
---|
101 | return getEstimator(given).getProbability(data); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Display a representation of this estimator |
---|
106 | */ |
---|
107 | public String toString() { |
---|
108 | |
---|
109 | String result = "DK Conditional Estimator. " |
---|
110 | + m_Estimators.length + " sub-estimators:\n"; |
---|
111 | for(int i = 0; i < m_Estimators.length; i++) { |
---|
112 | result += "Sub-estimator " + i + ": " + m_Estimators[i]; |
---|
113 | } |
---|
114 | result += "Weights of each estimator given by " + m_Weights; |
---|
115 | return result; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Returns the revision string. |
---|
120 | * |
---|
121 | * @return the revision |
---|
122 | */ |
---|
123 | public String getRevision() { |
---|
124 | return RevisionUtils.extract("$Revision: 1.8 $"); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Main method for testing this class. |
---|
129 | * |
---|
130 | * @param argv should contain a sequence of pairs of integers which |
---|
131 | * will be treated as pairs of symbolic, numeric. |
---|
132 | */ |
---|
133 | public static void main(String [] argv) { |
---|
134 | |
---|
135 | try { |
---|
136 | if (argv.length == 0) { |
---|
137 | System.out.println("Please specify a set of instances."); |
---|
138 | return; |
---|
139 | } |
---|
140 | int currentA = Integer.parseInt(argv[0]); |
---|
141 | int maxA = currentA; |
---|
142 | int currentB = Integer.parseInt(argv[1]); |
---|
143 | int maxB = currentB; |
---|
144 | for(int i = 2; i < argv.length - 1; i += 2) { |
---|
145 | currentA = Integer.parseInt(argv[i]); |
---|
146 | currentB = Integer.parseInt(argv[i + 1]); |
---|
147 | if (currentA > maxA) { |
---|
148 | maxA = currentA; |
---|
149 | } |
---|
150 | if (currentB > maxB) { |
---|
151 | maxB = currentB; |
---|
152 | } |
---|
153 | } |
---|
154 | DKConditionalEstimator newEst = new DKConditionalEstimator(maxA + 1, |
---|
155 | 1); |
---|
156 | for(int i = 0; i < argv.length - 1; i += 2) { |
---|
157 | currentA = Integer.parseInt(argv[i]); |
---|
158 | currentB = Integer.parseInt(argv[i + 1]); |
---|
159 | System.out.println(newEst); |
---|
160 | System.out.println("Prediction for " + currentA + '|' + currentB |
---|
161 | + " = " |
---|
162 | + newEst.getProbability(currentA, currentB)); |
---|
163 | newEst.addValue(currentA, currentB, 1); |
---|
164 | } |
---|
165 | } catch (Exception e) { |
---|
166 | System.out.println(e.getMessage()); |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|