📄 seriesvector.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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.operator.performance;import edu.udo.cs.yale.tools.ResultService;import edu.udo.cs.yale.operator.ResultObjectAdapter;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.operator.Operator; import java.util.ArrayList;import java.util.ListIterator;import java.util.Iterator;/** A <tt>SeriesVector</tt>-object collects the <tt>PerformanceVector</tt>-objects of a time series * experiment (e.g. of a concept drift simulation experiment). * If the experiment is conducted in several runs, each of which delivers a <tt>RunVector</tt>-object * containing the <tt>PerformanceVector</tt>-objects of all time points in a run, the * <tt>SeriesVector</tt>-object allows to average over the runs for each time point separately * resulting in an averaged <tt>RunVector</tt>-object containing the average <tt>PerformanceVector</tt> * for each time point and to average this <tt>RunVector</tt>-object again to a <tt>PerformanceVector</tt>-object * containing the average over all time points and runs. * * @author Ralf Klinkenberg * @version $Id: SeriesVector.java,v 2.3 2003/04/11 13:42:12 fischer Exp $ */public class SeriesVector extends ResultObjectAdapter { /** prefix string for the result output. */ private String resultString; /** list of <tt>RunVector</tt>-objects */ private ArrayList listOfRuns; /** <tt>RunVector</tt>-object averaged over the runs (one entry for each time point) */ private RunVector averageRun; /** performance averaged over all runs and time points */ private PerformanceVector averagePerformance; /** creates a new empty <tt>SeriesVector</tt>-object. */ public SeriesVector() { resultString = "\nAverage performance in the time series experiment:\n"; listOfRuns = new ArrayList(); averageRun = null; averagePerformance = null; } /** set the prefix for the result string. */ void setResultString (String rs) { resultString = rs; } public int getNUmberOfRund() { return listOfRuns.size(); } /** Zusätzlich zu der ohnehin vererbten Methode noch eine weitere add-Methode * zum hinzufügen von <tt>RunVector</tt>-Objekten. */ public void add(RunVector runVector) { listOfRuns.add(runVector); } /** Returns the <i>i</i>-th {@link RunVector}. */ public RunVector get(int runIndex) { return (RunVector)listOfRuns.get(runIndex); } /** Liefert den i-ten <tt>PerformanceVector</tt> im j-ten <tt>RunVector</tt> zurück * (i = <tt>timeIndex</tt>, j = <tt>runIndex</tt>). */ public PerformanceVector get(int runIndex, int timeIndex) { //return (PerformanceVector)(((RunVector) (listOfRuns.get(runIndex))).get(timeIndex)); return get(runIndex).get(timeIndex); } /** Liefert aus dem i-ten <tt>PerformanceVector</tt> im j-ten <tt>RunVector</tt> das * k-te <tt>PerformanceCriterion</tt> zurück * (i = <tt>timeIndex</tt>, j = <tt>runIndex</tt>, k = <tt>criterionIndex</tt>). */ public PerformanceCriterion get(int runIndex, int timeIndex, int criterionIndex) { return get(runIndex, timeIndex).get(criterionIndex);// return ((PerformanceCriterion)// ((PerformanceVector)// (((RunVector) (listOfRuns.get(runIndex))).get(timeIndex))).get(criterionIndex)); }// /** Liefert die Liste mit den <tt>RunVector</tt>-Objekten zurück.// */// public ArrayList getRunVectorList() {// return listOfRuns;// } /* Berechnet den Mittelwert aus einem <tt>RunVektor</tt> (aus den Kreuzvalidierungsdurchläfen) * von PerformanceVektoren von Performanzkriterien (bei mehreren Kriterien) * und liefert einen Vektor von Performanzkriterien (also einen <tt>PerformanceVector</tt>)zurück, * der für jedes Kriterium den Mittelwert enthält. */ /* public static PerformanceVector average(RunVector vectorList) { // Initialisieren: // --------------- PerformanceVector output = new PerformanceVector(); PerformanceVector first = (PerformanceVector)vectorList.getVectorList().get(0); ListIterator f = first.getCriteriaList().listIterator(); while (f.hasNext()) output.addCriterion((PerformanceCriterion)(((PerformanceCriterion)f.next()).clone())); // Mittelwertarray, soviele wie es Kriterien gibt double[] values = new double[first.getCriteriaList().size()]; // Varianzarray, soviele wie es Kriterien gibt double[] variances = new double[first.getCriteriaList().size()]; // Aufaddieren: // ------------ // Die einzelnen Vektoren mit den Kriterien drin durchgehen ListIterator i = vectorList.getVectorList().listIterator(); while (i.hasNext()) { // Fuer jeden Kriteriumsvektor seinen Wert im Valuesarray aufsummieren PerformanceVector currentCriteriaList = (PerformanceVector)i.next(); int index = 0; ListIterator c = currentCriteriaList.getCriteriaList().listIterator(); while (c.hasNext()) { double value = ((PerformanceCriterion)c.next()).getValue(); values[index++] += value; } } // Mittelwert und Varianz: // ----------------------- // Die Anzahl der Kriterien durchgehen for (int m = 0; m < first.getCriteriaList().size(); m++) { // Mittelwert berechnen: values[m] /= vectorList.getVectorList().size(); // Varianz berechnen: // die einzelnen Vektoren mit den Kriterien durchgehen und jeweils // fuer jedes Kriterium die Differenzen zum Mittelwert usw. bestimmen ListIterator v = vectorList.getVectorList().listIterator(); while (v.hasNext()) { // fuer jeden Kriteriumvektor bestimme die Differenz zum Mittelwert, // quadrier diese und summier sie auf. PerformanceVector currentCriteriaList = (PerformanceVector)v.next(); double var = 0; var = ((PerformanceCriterion)currentCriteriaList.getCriteriaList().get(m)).getValue() - values[m]; var *= var; variances[m] += var; } variances[m] /= vectorList.getVectorList().size(); variances[m] = Math.sqrt(variances[m]); } // Setzen der Werte: // ----------------- ListIterator o = output.getCriteriaList().listIterator(); int m = 0; while (o.hasNext()) { PerformanceCriterion criterion = (PerformanceCriterion)o.next(); criterion.setValue(values[m]); criterion.setVariance(variances[m]); m++; } return output; } */// /** returns a <tt>RunVector</tt>-object with the average of the object in the // * list of <tt>RunVector</tt>-objects, averaged for each time point.// */// public RunVector getTimePointWiseAverageRunVector() {// // Note: This implementation may be more time complex than necessary.// // A more efficient implementation should work similar to 'RunVector.average()'.// if (listOfRuns.size() == 0) return null; // zero runs (trials)// if (((RunVector)(listOfRuns.get(0))).size() == 0) return null; // zero batches// if (get(0,0).size() == 0) return null; // zero criteria// double sum, average, variance;// int no_of_batches = ((RunVector) (listOfRuns.get(0))).size();// int no_of_criteria = get(0,0).size();// PerformanceVector currentPerformanceVector;// PerformanceCriterion criterion = null;// averageRun = new RunVector();// // N runs (= trials) w/ no_of_batches batches w/ no_of_criteria criteria// // Series Run Vector// for (int timeIndex = 0; timeIndex < no_of_batches; timeIndex++) {// currentPerformanceVector = new PerformanceVector();// for (int criterionIndex = 0; criterionIndex < no_of_criteria; criterionIndex++) {// // ---- compute average ----// sum = 0.0;// for (int runIndex = 0; runIndex < listOfRuns.size(); runIndex++) {// criterion = get (runIndex, timeIndex, criterionIndex);// sum += criterion.getValue();// }// average = sum / listOfRuns.size();// // ---- compute variance ----// sum = 0.0;// for (int runIndex = 0; runIndex < listOfRuns.size(); runIndex++) {// criterion = get (runIndex, timeIndex, criterionIndex);// sum += ((criterion.getValue() - average) * (criterion.getValue() - average));// }// variance = sum / listOfRuns.size(); // variance// variance = Math.sqrt(variance); // standard deviation// // ---- create new average criterion and add it to the new PerformanceVector ----// criterion = (PerformanceCriterion) criterion.clone();// criterion.setValue(average);// criterion.setVariance(variance);// currentPerformanceVector.addCriterion (criterion);// }// // ---- add new PerformanceVector to the RunVector ----// averageRun.add(currentPerformanceVector);// }// return averageRun;// } /** Returns a <tt>RunVector</tt>-object with the average of the object in the * list of <tt>RunVector</tt>-objects, averaged for each time point. */ public RunVector getTimePointWiseAverageRunVector() { if (listOfRuns.size() == 0) return null; // zero runs (trials) if (((RunVector)(listOfRuns.get(0))).size() == 0) return null; // zero batches if (get(0,0).size() == 0) return null; // zero criteria int no_of_batches = get(0).size(); int no_of_criteria = get(0, 0).size(); averageRun = new RunVector(); for (int timeIndex = 0; timeIndex < no_of_batches; timeIndex++) { PerformanceVector currentPerformanceVector = new PerformanceVector(); for (int criterionIndex = 0; criterionIndex < no_of_criteria; criterionIndex++) { PerformanceCriterion criterion = (PerformanceCriterion)get(0, timeIndex, criterionIndex).clone(); for (int runIndex = 1; runIndex < listOfRuns.size(); runIndex++) { criterion.buildAverage(get(runIndex, timeIndex, criterionIndex)); } currentPerformanceVector.addCriterion (criterion); } averageRun.add(currentPerformanceVector); } return averageRun; } /** returns a <tt>PerformanceVector</tt>-object with the average performance * over all time points and runs. */ public PerformanceVector getOverallAveragePerformanceVector() { averageRun = getTimePointWiseAverageRunVector(); return averageRun.average(); } /** returns the complete <tt>SeriesVector</tt>-object as a <tt>String</tt>. */ public String toString() { String result = "SeriesVector: \n"; ListIterator i = listOfRuns.listIterator(); while (i.hasNext()) result += (RunVector)i.next() + "\n"; return result; } /** returns a <tt>String</tt> containing a time series of the average value of each performance measures * (averaged over all runs) and the average of all peroformance meausers over all time points and runs. */ public String toResultString() { // Output format: // Averaged time series of performance criterion 'error': 0.1 0.2 0.3 ... // Averaged time series of performance criterion 'recall': 0.1 0.2 0.3 ... // ... // Overall average of performance criterion 'error': 0.2 (standard deviation: 0.15) // ... String result = resultString; PerformanceVector performance = null; PerformanceCriterion criterion = null; // ---- compute the averages ---- // averageRun = getTimePointWiseAverageRunVector(); // implicitly computed by the line below averagePerformance = getOverallAveragePerformanceVector(); // ---- print the averaged time serieses of all performance criteria ---- for (int criterionIndex = 0; criterionIndex < averagePerformance.size(); criterionIndex++) { result += " Averaged time series of performance criterion '"+criterion.getName()+"':"; for (int timeIndex = 0; timeIndex < averageRun.size(); timeIndex++) { try { performance = (PerformanceVector) averageRun.get(timeIndex); } catch (ClassCastException e) { LogService.logException("Other class than PerformanceVector in RunVector: ",e); } try { criterion = (PerformanceCriterion) performance.get(criterionIndex); } catch (ClassCastException e) { LogService.logException("Other class than PerformanceCriterion in PerformanceVector: ",e); } result += " "+criterion.getValue(); } result += "\n"; } // ---- print the overall average (and variance) ---- for (int criteria = 0; criteria < averagePerformance.size(); criteria++) { try { criterion = averagePerformance.get(criteria); } catch (ClassCastException e) { LogService.logException("Other class than PerformanceCriterion in PerformanceVector: ",e); } result += " Overall average of performance criterion '"+criterion.getName()+"': " + criterion.getValue(); if (criterion.getVariance() >= 0) { result += " (" + criterion.getVariance() + ")"; } result += "\n"; // Alternativ: criterion.toResultString() } result += "\n"; return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -