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

« back to all changes in this revision

Viewing changes to weka/estimators/PoissonEstimator.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
 *    PoissonEstimator.java
 
19
 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.estimators;
 
24
 
 
25
import weka.core.Capabilities.Capability;
 
26
import weka.core.Capabilities;
 
27
import weka.core.Utils;
 
28
 
 
29
/** 
 
30
 * Simple probability estimator that places a single Poisson distribution
 
31
 * over the observed values.
 
32
 *
 
33
 * @author Len Trigg (trigg@cs.waikato.ac.nz)
 
34
 * @version $Revision: 1.7 $
 
35
 */
 
36
public class PoissonEstimator
 
37
  extends Estimator
 
38
  implements IncrementalEstimator {
 
39
 
 
40
  /** for serialization */
 
41
  private static final long serialVersionUID = 7669362595289236662L;
 
42
  
 
43
  /** The number of values seen */
 
44
  private double m_NumValues;
 
45
  
 
46
  /** The sum of the values seen */
 
47
  private double m_SumOfValues;
 
48
  
 
49
  /** 
 
50
   * The average number of times
 
51
   * an event occurs in an interval.
 
52
   */
 
53
  private double m_Lambda;
 
54
  
 
55
  
 
56
  /**
 
57
   * Calculates the log factorial of a number.
 
58
   *
 
59
   * @param x input number.
 
60
   * @return log factorial of x.
 
61
   */
 
62
  private double logFac(double x) {
 
63
    
 
64
    double result = 0;
 
65
    for (double i = 2; i <= x; i++) {
 
66
      result += Math.log(i);
 
67
    }
 
68
    return result;
 
69
  }
 
70
  
 
71
  /**
 
72
   * Returns value for Poisson distribution
 
73
   *
 
74
   * @param x the argument to the kernel function
 
75
   * @return the value for a Poisson kernel
 
76
   */
 
77
  private double Poisson(double x) {
 
78
    
 
79
    return Math.exp(-m_Lambda + (x * Math.log(m_Lambda)) - logFac(x));
 
80
  }
 
81
  
 
82
  /**
 
83
   * Add a new data value to the current estimator.
 
84
   *
 
85
   * @param data the new data value 
 
86
   * @param weight the weight assigned to the data value 
 
87
   */
 
88
  public void addValue(double data, double weight) {
 
89
    
 
90
    m_NumValues += weight;
 
91
    m_SumOfValues += data * weight;
 
92
    if (m_NumValues != 0) {
 
93
      m_Lambda = m_SumOfValues / m_NumValues;
 
94
    }
 
95
  }
 
96
  
 
97
  /**
 
98
   * Get a probability estimate for a value
 
99
   *
 
100
   * @param data the value to estimate the probability of
 
101
   * @return the estimated probability of the supplied value
 
102
   */
 
103
  public double getProbability(double data) {
 
104
    
 
105
    return Poisson(data);
 
106
  }
 
107
  
 
108
  /** Display a representation of this estimator */
 
109
  public String toString() {
 
110
    
 
111
    return "Poisson Lambda = " + Utils.doubleToString(m_Lambda, 4, 2) + "\n";
 
112
  }
 
113
  
 
114
  /**
 
115
   * Returns default capabilities of the classifier.
 
116
   *
 
117
   * @return      the capabilities of this classifier
 
118
   */
 
119
  public Capabilities getCapabilities() {
 
120
    Capabilities result = super.getCapabilities();
 
121
    
 
122
    // attributes
 
123
    result.enable(Capability.NUMERIC_ATTRIBUTES);
 
124
    return result;
 
125
  }
 
126
  
 
127
  /**
 
128
   * Main method for testing this class.
 
129
   *
 
130
   * @param argv should contain a sequence of numeric values
 
131
   */
 
132
  public static void main(String [] argv) {
 
133
    
 
134
    try {
 
135
      if (argv.length == 0) {
 
136
        System.out.println("Please specify a set of instances.");
 
137
        return;
 
138
      }
 
139
      PoissonEstimator newEst = new PoissonEstimator();
 
140
      for(int i = 0; i < argv.length; i++) {
 
141
        double current = Double.valueOf(argv[i]).doubleValue();
 
142
        System.out.println(newEst);
 
143
        System.out.println("Prediction for " + current 
 
144
            + " = " + newEst.getProbability(current));
 
145
        newEst.addValue(current, 1);
 
146
      }
 
147
      
 
148
    } catch (Exception e) {
 
149
      System.out.println(e.getMessage());
 
150
    }
 
151
  }
 
152
}
 
153
 
 
154
 
 
155
 
 
156
 
 
157
 
 
158
 
 
159
 
 
160