MOA 12.03
Real Time Analytics for Data Streams
BasicRegressionPerformanceEvaluator.java
Go to the documentation of this file.
00001 /*
00002  *    BasicRegressionPerformanceEvaluator.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.AbstractMOAObject;
00023 import moa.core.Measurement;
00024 import weka.core.Instance;
00025 
00032 public class BasicRegressionPerformanceEvaluator extends AbstractMOAObject
00033         implements ClassificationPerformanceEvaluator {
00034 
00035     private static final long serialVersionUID = 1L;
00036 
00037     protected double weightObserved;
00038 
00039     protected double squareError;
00040 
00041     protected double averageError;
00042 
00043     @Override
00044     public void reset() {
00045         this.weightObserved = 0.0;
00046         this.squareError = 0.0;
00047         this.averageError = 0.0;
00048     }
00049 
00050     @Override
00051     public void addResult(Instance inst, double[] prediction) {
00052         if (inst.weight() > 0.0) {
00053             this.weightObserved += inst.weight();
00054             if (prediction.length > 0 && this.weightObserved != inst.weight()) {
00055                 this.squareError += (inst.classValue() - prediction[0]) * (inst.classValue() - prediction[0]);
00056                 this.averageError += Math.abs(inst.classValue() - prediction[0]);
00057             }
00058             //System.out.println(inst.classValue()+", "+prediction[0]);
00059         }
00060     }
00061 
00062     @Override
00063     public Measurement[] getPerformanceMeasurements() {
00064         return new Measurement[]{
00065                     new Measurement("classified instances",
00066                     getTotalWeightObserved()),
00067                     new Measurement("mean absolute error",
00068                     getMeanError()),
00069                     new Measurement("root mean squared error",
00070                     getSquareError())};
00071     }
00072 
00073     public double getTotalWeightObserved() {
00074         return this.weightObserved;
00075     }
00076 
00077     public double getMeanError() {
00078         return this.weightObserved > 0.0 ? this.averageError
00079                 / this.weightObserved : 0.0;
00080     }
00081 
00082     public double getSquareError() {
00083         return Math.sqrt(this.weightObserved > 0.0 ? this.squareError
00084                 / this.weightObserved : 0.0);
00085     }
00086 
00087     @Override
00088     public void getDescription(StringBuilder sb, int indent) {
00089         Measurement.getMeasurementsDescription(getPerformanceMeasurements(),
00090                 sb, indent);
00091     }
00092 }
 All Classes Namespaces Files Functions Variables Enumerations