MOA 12.03
Real Time Analytics for Data Streams
FadingFactorClassificationPerformanceEvaluator.java
Go to the documentation of this file.
00001 /*
00002  *    FadingFactorClassificationPerformanceEvaluator.java
00003  *    Copyright (C) 2007 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.FloatOption;
00025 import moa.options.AbstractOptionHandler;
00026 import moa.tasks.TaskMonitor;
00027 
00028 import weka.core.Utils;
00029 import weka.core.Instance;
00030 
00037 public class FadingFactorClassificationPerformanceEvaluator extends AbstractOptionHandler
00038         implements ClassificationPerformanceEvaluator {
00039 
00040     private static final long serialVersionUID = 1L;
00041 
00042     protected double TotalweightObserved;
00043 
00044     public FloatOption alphaOption = new FloatOption("alpha",
00045             'a', "Fading factor or exponential smoothing factor", .01);
00046 
00047     protected Estimator weightCorrect;
00048 
00049     protected class Estimator {
00050 
00051         protected double alpha;
00052 
00053         protected double estimation;
00054 
00055         protected double b;
00056 
00057         public Estimator(double a) {
00058             alpha = a;
00059             estimation = 0.0;
00060             b = 0.0;
00061         }
00062 
00063         public void add(double value) {
00064             estimation = alpha * estimation + value;
00065             b = alpha * b + 1.0;
00066         }
00067 
00068         public double estimation() {
00069             return b > 0.0 ? estimation / b : 0;
00070         }
00071     }
00072 
00073     /*   public void setalpha(double a) {
00074     this.alpha = a;
00075     reset();
00076     }*/
00077 
00078     @Override
00079     public void reset() {
00080         weightCorrect = new Estimator(this.alphaOption.getValue());
00081     }
00082 
00083     /*public void addClassificationAttempt(int trueClass, double[] classVotes,
00084     double weight) {
00085     if (weight > 0.0) {
00086     this.TotalweightObserved += weight;
00087     if (Utils.maxIndex(classVotes) == trueClass) {
00088     this.weightCorrect.add(1);
00089     } else
00090     this.weightCorrect.add(0);
00091     }
00092     }*/
00093     @Override
00094     public void addResult(Instance inst, double[] classVotes) {
00095         double weight = inst.weight();
00096         int trueClass = (int) inst.classValue();
00097         if (weight > 0.0) {
00098             this.TotalweightObserved += weight;
00099             if (Utils.maxIndex(classVotes) == trueClass) {
00100                 this.weightCorrect.add(1);
00101             } else {
00102                 this.weightCorrect.add(0);
00103             }
00104         }
00105     }
00106 
00107     @Override
00108     public Measurement[] getPerformanceMeasurements() {
00109         return new Measurement[]{
00110                     new Measurement("classified instances",
00111                     this.TotalweightObserved),
00112                     new Measurement("classifications correct (percent)",
00113                     getFractionCorrectlyClassified() * 100.0)};
00114     }
00115 
00116     public double getTotalWeightObserved() {
00117         return this.TotalweightObserved;
00118     }
00119 
00120     public double getFractionCorrectlyClassified() {
00121         return this.weightCorrect.estimation();
00122     }
00123 
00124     public double getFractionIncorrectlyClassified() {
00125         return 1.0 - getFractionCorrectlyClassified();
00126     }
00127 
00128     @Override
00129     public void getDescription(StringBuilder sb, int indent) {
00130         Measurement.getMeasurementsDescription(getPerformanceMeasurements(),
00131                 sb, indent);
00132     }
00133 
00134     @Override
00135     public void prepareForUseImpl(TaskMonitor monitor,
00136             ObjectRepository repository) {
00137         reset();
00138     }
00139 }
 All Classes Namespaces Files Functions Variables Enumerations