~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

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