📄 averagable.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.tools.math;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.operator.ResultObjectAdapter;
import java.util.*;
import java.text.*;
/** Superclass for all objects which can be averaged. Averagable objects can be stored in a average vector.
*
* @version $Id: Averagable.java,v 1.6 2004/09/12 11:09:53 ingomierswa Exp $
*/
public abstract class Averagable extends ResultObjectAdapter implements Cloneable, Comparable {
/** Used for formatting values in the {@link #toString()} method. */
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();
/** Used for formatting values in the {@link #formatValue(double)} method. */
private static final NumberFormat PERCENT_FORMAT = NumberFormat.getPercentInstance();
static {
PERCENT_FORMAT.setMaximumFractionDigits(2);
PERCENT_FORMAT.setMinimumFractionDigits(2);
NUMBER_FORMAT.setMaximumFractionDigits(3);
NUMBER_FORMAT.setMinimumFractionDigits(3);
}
/** The averages are summed up each time buildAverage is called. */
private double meanSum = Double.NaN;
/** The squared averages are summed up each time buildAverage is called. */
private double meanSquaredSum = Double.NaN;
/** Counts the number of times, build average was executed. */
private int averageCount = 0;
/** Returns the name of this averagable. */
public abstract String getName();
/** Returns the value of the averagable (the average itself). */
public abstract double getValue();
/** Returns the variance of the averagable. */
public abstract double getVariance();
/** Must be implemented by subclasses such that it copies
* all values of <code>other</code> to <code>this</code>.
* When this method is called, it is guaranteed, that <code>other</code> is a subclass
* of the class of the object it is called on. */
protected abstract void cloneAveragable(Averagable other);
// ================================================================================
/** This method builds the average of two averagables of the same type.
* First this method checks if the classes of <code>this</code> and
* <code>performance</code> are the same and if the
* {@link #getName()} methods return the same String. Otherwise
* a RuntimeException is thrown.
* <br>
* The value of <code>averagable.</code>{@link #getValue()} is added to
* {@link #meanSum}, its square is added to
* {@link #meanSquaredSum} and
* {@link #averageCount} is increased by one. These
* values are used in the {@link #getMakroAverage()} and
* {@link #getMakroVariance()} methods.
* <br>
* Subclasses should implement this method to build the weighted average of
* <code>this</code> measured performance and <code>performance</code>. They
* must be weighted by the number of examples used for calculating the
* criteria. Subclasses <code>must</code> call <code>super.buildAverage</code>
* before they do anything that changes the return value of
* <code>performance.</code>{@link #getValue()}! */
public void buildAverage(Averagable averagable) {
if (!averagable.getClass().equals(this.getClass()))
throw new RuntimeException("Cannot build average of different averagable types ("+
this.getClass().getName()+"/"+averagable.getClass().getName()+").");
if (!averagable.getName().equals(this.getName()))
throw new RuntimeException("Cannot build average of different averagable types ("+
this.getName()+"/"+averagable.getName()+").");
if (averageCount == 0) { // count yourself
double value = this.getValue();
meanSum = value;
meanSquaredSum = value*value;
averageCount = 1;
}
double value = averagable.getValue();
meanSum += value;
meanSquaredSum += value*value;
averageCount++;
}
/** Returns the standard deviation of the performance criterion. */
public double getStandardDeviation() {
double variance = getVariance();
if (Double.isNaN(variance)) return Double.NaN;
else return Math.sqrt(variance);
}
/** Returns the average value of all performance criteria average by using the
* {@link #buildAverage(Averagable)} method. */
public double getMakroAverage() {
return meanSum / averageCount;
}
/** Returns the variance of all performance criteria average by using the
* {@link #buildAverage(Averagable)} method. */
public double getMakroVariance() {
double mean = getMakroAverage();
return meanSquaredSum / averageCount - mean * mean;
}
/** Returns the standard deviation of all performance criteria average by using the
* {@link #buildAverage(Averagable)} method. */
public double getMakroStandardDeviation() {
return Math.sqrt(getMakroVariance());
}
/** Returns a (deep) clone of this averagable. */
public Object clone() {
try {
Class clazz = this.getClass();
Averagable clone = (Averagable)clazz.newInstance();
clone.meanSum = this.meanSum;
clone.meanSquaredSum = this.meanSquaredSum;
clone.averageCount = this.averageCount;
clone.cloneAveragable(this);
return clone;
} catch (Exception e) {
LogService.logException("In Averagable.clone():", e);
return null;
}
}
// ================================================================================
/** Indicates wether or not percentage format should be used in the
* {@link #toString} method. The default implementation returns false. */
public boolean formatPercent() { return false; }
/** Formats the value for the {@link #toString()} method. */
protected String formatValue(double value) {
if (Double.isNaN(value)) return "unknown";
else {
if (formatPercent()) {
return PERCENT_FORMAT.format(value);
} else {
return NUMBER_FORMAT.format(value);
}
}
}
/** Formats the standard deviation for the {@link #toString()} method. */
protected String formatDeviation(double dev) {
if (formatPercent()) {
return PERCENT_FORMAT.format(dev);
} else {
return NUMBER_FORMAT.format(dev);
}
}
public String toString() {
String str = getName()+": "+formatValue(getValue());
double sd = getStandardDeviation();
if (!Double.isNaN(sd)) str += " +/- "+formatDeviation(sd);
if (averageCount > 0) {
str += " (makro avg: "+formatValue(getMakroAverage());
sd = getMakroStandardDeviation();
if (!Double.isNaN(sd)) str += " +/- "+formatDeviation(sd);
str += ")";
}
return str;
}
/** Returns a HTML result string. Default implementation returns toString(). */
public String toHTML() {
return toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -