MOA 12.03
Real Time Analytics for Data Streams
WindowRegressionPerformanceEvaluator.java
Go to the documentation of this file.
00001 /*
00002  *    WindowRegressionPerformanceEvaluator.java
00003  *    Copyright (C) 2011 University of Waikato, Hamilton, New Zealand
00004  *    @author Albert Bifet (abifet at cs dot waikato dot ac dot nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.evaluation;
00021 
00022 import moa.core.Measurement;
00023 import moa.core.ObjectRepository;
00024 import moa.options.AbstractOptionHandler;
00025 import moa.options.IntOption;
00026 import moa.tasks.TaskMonitor;
00027 import weka.core.Instance;
00028 
00035 public class WindowRegressionPerformanceEvaluator extends AbstractOptionHandler
00036         implements ClassificationPerformanceEvaluator {
00037 
00038     private static final long serialVersionUID = 1L;
00039 
00040     public IntOption widthOption = new IntOption("width",
00041             'w', "Size of Window", 1000);
00042 
00043     protected double TotalweightObserved = 0;
00044 
00045     protected Estimator weightObserved;
00046 
00047     protected Estimator squareError;
00048 
00049     protected Estimator averageError;
00050 
00051     protected int numClasses;
00052 
00053     public class Estimator {
00054 
00055         protected double[] window;
00056 
00057         protected int posWindow;
00058 
00059         protected int lenWindow;
00060 
00061         protected int SizeWindow;
00062 
00063         protected double sum;
00064 
00065         public Estimator(int sizeWindow) {
00066             window = new double[sizeWindow];
00067             SizeWindow = sizeWindow;
00068             posWindow = 0;
00069         }
00070 
00071         public void add(double value) {
00072             sum -= window[posWindow];
00073             sum += value;
00074             window[posWindow] = value;
00075             posWindow++;
00076             if (posWindow == SizeWindow) {
00077                 posWindow = 0;
00078             }
00079         }
00080 
00081         public double total() {
00082             return sum;
00083         }
00084     }
00085 
00086     /*   public void setWindowWidth(int w) {
00087     this.width = w;
00088     reset();
00089     }*/
00090     @Override
00091     public void reset() {
00092         reset(this.numClasses);
00093     }
00094 
00095     public void reset(int numClasses) {
00096         this.numClasses = numClasses;
00097         this.weightObserved = new Estimator(this.widthOption.getValue());
00098         this.squareError = new Estimator(this.widthOption.getValue());
00099         this.averageError = new Estimator(this.widthOption.getValue());
00100         this.TotalweightObserved = 0;
00101     }
00102 
00103     @Override
00104     public void addResult(Instance inst, double[] prediction) {
00105         double weight = inst.weight();
00106         if (weight > 0.0) {
00107             if (TotalweightObserved == 0) {
00108                 reset(inst.dataset().numClasses());
00109             }
00110             this.TotalweightObserved += weight;
00111             this.weightObserved.add(weight);
00112 
00113             if (prediction.length > 0) {
00114                 this.squareError.add((inst.classValue() - prediction[0]) * (inst.classValue() - prediction[0]));
00115                 this.averageError.add(Math.abs(inst.classValue() - prediction[0]));
00116             }
00117             //System.out.println(inst.classValue()+", "+prediction[0]);
00118         }
00119     }
00120 
00121     @Override
00122     public Measurement[] getPerformanceMeasurements() {
00123         return new Measurement[]{
00124                     new Measurement("classified instances",
00125                     getTotalWeightObserved()),
00126                     new Measurement("mean absolute error",
00127                     getMeanError()),
00128                     new Measurement("root mean squared error",
00129                     getSquareError())};
00130     }
00131 
00132     public double getTotalWeightObserved() {
00133         return this.weightObserved.total();
00134     }
00135 
00136     public double getMeanError() {
00137         return this.weightObserved.total() > 0.0 ? this.averageError.total()
00138                 / this.weightObserved.total() : 0.0;
00139     }
00140 
00141     public double getSquareError() {
00142         return Math.sqrt(this.weightObserved.total() > 0.0 ? this.squareError.total()
00143                 / this.weightObserved.total() : 0.0);
00144     }
00145 
00146     @Override
00147     public void getDescription(StringBuilder sb, int indent) {
00148         Measurement.getMeasurementsDescription(getPerformanceMeasurements(),
00149                 sb, indent);
00150     }
00151 
00152     @Override
00153     public void prepareForUseImpl(TaskMonitor monitor,
00154             ObjectRepository repository) {
00155     }
00156 }
 All Classes Namespaces Files Functions Variables Enumerations