MOA 12.03
Real Time Analytics for Data Streams
Measurement.java
Go to the documentation of this file.
00001 /*
00002  *    Measurement.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.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.core;
00021 
00022 import java.util.ArrayList;
00023 import java.util.List;
00024 
00025 import moa.AbstractMOAObject;
00026 
00033 public class Measurement extends AbstractMOAObject {
00034 
00035     private static final long serialVersionUID = 1L;
00036 
00037     protected String name;
00038 
00039     protected double value;
00040 
00041     public Measurement(String name, double value) {
00042         this.name = name;
00043         this.value = value;
00044     }
00045 
00046     public String getName() {
00047         return this.name;
00048     }
00049 
00050     public double getValue() {
00051         return this.value;
00052     }
00053 
00054     public static Measurement getMeasurementNamed(String name,
00055             Measurement[] measurements) {
00056         for (Measurement measurement : measurements) {
00057             if (name.equals(measurement.getName())) {
00058                 return measurement;
00059             }
00060         }
00061         return null;
00062     }
00063 
00064     public static void getMeasurementsDescription(Measurement[] measurements,
00065             StringBuilder out, int indent) {
00066         if (measurements.length > 0) {
00067             StringUtils.appendIndented(out, indent, measurements[0].toString());
00068             for (int i = 1; i < measurements.length; i++) {
00069                 StringUtils.appendNewlineIndented(out, indent, measurements[i].toString());
00070             }
00071 
00072         }
00073     }
00074 
00075     public static Measurement[] averageMeasurements(Measurement[][] toAverage) {
00076         List<String> measurementNames = new ArrayList<String>();
00077         for (Measurement[] measurements : toAverage) {
00078             for (Measurement measurement : measurements) {
00079                 if (measurementNames.indexOf(measurement.getName()) < 0) {
00080                     measurementNames.add(measurement.getName());
00081                 }
00082             }
00083         }
00084         GaussianEstimator[] estimators = new GaussianEstimator[measurementNames.size()];
00085         for (int i = 0; i < estimators.length; i++) {
00086             estimators[i] = new GaussianEstimator();
00087         }
00088         for (Measurement[] measurements : toAverage) {
00089             for (Measurement measurement : measurements) {
00090                 estimators[measurementNames.indexOf(measurement.getName())].addObservation(measurement.getValue(), 1.0);
00091             }
00092         }
00093         List<Measurement> averagedMeasurements = new ArrayList<Measurement>();
00094         for (int i = 0; i < measurementNames.size(); i++) {
00095             String mName = measurementNames.get(i);
00096             GaussianEstimator mEstimator = estimators[i];
00097             if (mEstimator.getTotalWeightObserved() > 1.0) {
00098                 averagedMeasurements.add(new Measurement("[avg] " + mName,
00099                         mEstimator.getMean()));
00100                 averagedMeasurements.add(new Measurement("[err] " + mName,
00101                         mEstimator.getStdDev()
00102                         / Math.sqrt(mEstimator.getTotalWeightObserved())));
00103             }
00104         }
00105         return averagedMeasurements.toArray(new Measurement[averagedMeasurements.size()]);
00106     }
00107 
00108     @Override
00109     public void getDescription(StringBuilder sb, int indent) {
00110         sb.append(getName());
00111         sb.append(" = ");
00112         sb.append(StringUtils.doubleToString(getValue(), 3));
00113     }
00114 }
 All Classes Namespaces Files Functions Variables Enumerations