PoissonEstimator.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
Category:

Windows Develop

Development Platform:

Java

  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.  *    PoissonEstimator.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.estimators;
  22. import java.util.*;
  23. import weka.core.*;
  24. /** 
  25.  * Simple probability estimator that places a single Poisson distribution
  26.  * over the observed values.
  27.  *
  28.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  29.  * @version $Revision: 1.4 $
  30.  */
  31. public class PoissonEstimator implements Estimator {
  32.   /** The number of values seen */
  33.   private double m_NumValues;
  34.   /** The sum of the values seen */
  35.   private double m_SumOfValues;
  36.   /** 
  37.    * The average number of times
  38.    * an event occurs in an interval.
  39.    */
  40.   private double m_Lambda;
  41.   /**
  42.    * Calculates the log factorial of a number.
  43.    *
  44.    * @param x input number.
  45.    * @return log factorial of x.
  46.    */
  47.   private double logFac(double x) {
  48.     double result = 0;
  49.     for (double i = 2; i <= x; i++) {
  50.       result += Math.log(i);
  51.     }
  52.     return result;
  53.   }
  54.   /**
  55.    * Returns value for Poisson distribution
  56.    *
  57.    * @param x the argument to the kernel function
  58.    * @return the value for a Poisson kernel
  59.    */
  60.   private double Poisson(double x) {
  61.     
  62.     return Math.exp(-m_Lambda + (x * Math.log(m_Lambda)) - logFac(x));
  63.   }
  64.   
  65.   /**
  66.    * Add a new data value to the current estimator.
  67.    *
  68.    * @param data the new data value 
  69.    * @param weight the weight assigned to the data value 
  70.    */
  71.   public void addValue(double data, double weight) {
  72.     
  73.     m_NumValues += weight;
  74.     m_SumOfValues += data * weight;
  75.     if (m_NumValues != 0) {
  76.       m_Lambda = m_SumOfValues / m_NumValues;
  77.     }
  78.   }
  79.   /**
  80.    * Get a probability estimate for a value
  81.    *
  82.    * @param data the value to estimate the probability of
  83.    * @return the estimated probability of the supplied value
  84.    */
  85.   public double getProbability(double data) {
  86.     
  87.     return Poisson(data);
  88.   }
  89.   /** Display a representation of this estimator */
  90.   public String toString() {
  91.     
  92.     return "Poisson Lambda = " + Utils.doubleToString(m_Lambda, 4, 2) + "n";
  93.   }
  94.   /**
  95.    * Main method for testing this class.
  96.    *
  97.    * @param argv should contain a sequence of numeric values
  98.    */
  99.   public static void main(String [] argv) {
  100.     
  101.     try {
  102.       if (argv.length == 0) {
  103. System.out.println("Please specify a set of instances.");
  104. return;
  105.       }
  106.       PoissonEstimator newEst = new PoissonEstimator();
  107.       for(int i = 0; i < argv.length; i++) {
  108. double current = Double.valueOf(argv[i]).doubleValue();
  109. System.out.println(newEst);
  110. System.out.println("Prediction for " + current 
  111.    + " = " + newEst.getProbability(current));
  112. newEst.addValue(current, 1);
  113.       }
  114.     } catch (Exception e) {
  115.       System.out.println(e.getMessage());
  116.     }
  117.   }
  118. }