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.
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.
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.
18
* PoissonEstimator.java
19
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
23
package weka.estimators;
25
import weka.core.Capabilities.Capability;
26
import weka.core.Capabilities;
27
import weka.core.Utils;
30
* Simple probability estimator that places a single Poisson distribution
31
* over the observed values.
33
* @author Len Trigg (trigg@cs.waikato.ac.nz)
34
* @version $Revision: 1.7 $
36
public class PoissonEstimator
38
implements IncrementalEstimator {
40
/** for serialization */
41
private static final long serialVersionUID = 7669362595289236662L;
43
/** The number of values seen */
44
private double m_NumValues;
46
/** The sum of the values seen */
47
private double m_SumOfValues;
50
* The average number of times
51
* an event occurs in an interval.
53
private double m_Lambda;
57
* Calculates the log factorial of a number.
59
* @param x input number.
60
* @return log factorial of x.
62
private double logFac(double x) {
65
for (double i = 2; i <= x; i++) {
66
result += Math.log(i);
72
* Returns value for Poisson distribution
74
* @param x the argument to the kernel function
75
* @return the value for a Poisson kernel
77
private double Poisson(double x) {
79
return Math.exp(-m_Lambda + (x * Math.log(m_Lambda)) - logFac(x));
83
* Add a new data value to the current estimator.
85
* @param data the new data value
86
* @param weight the weight assigned to the data value
88
public void addValue(double data, double weight) {
90
m_NumValues += weight;
91
m_SumOfValues += data * weight;
92
if (m_NumValues != 0) {
93
m_Lambda = m_SumOfValues / m_NumValues;
98
* Get a probability estimate for a value
100
* @param data the value to estimate the probability of
101
* @return the estimated probability of the supplied value
103
public double getProbability(double data) {
105
return Poisson(data);
108
/** Display a representation of this estimator */
109
public String toString() {
111
return "Poisson Lambda = " + Utils.doubleToString(m_Lambda, 4, 2) + "\n";
115
* Returns default capabilities of the classifier.
117
* @return the capabilities of this classifier
119
public Capabilities getCapabilities() {
120
Capabilities result = super.getCapabilities();
123
result.enable(Capability.NUMERIC_ATTRIBUTES);
128
* Main method for testing this class.
130
* @param argv should contain a sequence of numeric values
132
public static void main(String [] argv) {
135
if (argv.length == 0) {
136
System.out.println("Please specify a set of instances.");
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);
148
} catch (Exception e) {
149
System.out.println(e.getMessage());