source: src/main/java/weka/estimators/PoissonEstimator.java @ 8

Last change on this file since 8 was 4, checked in by gnappo, 14 years ago

Import di weka.

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