View Javadoc

1   /*
2    * Copyright (C) 2004-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package de.fu_berlin.ties.eval;
23  
24  import de.fu_berlin.ties.io.BaseStorable;
25  import de.fu_berlin.ties.io.FieldMap;
26  import de.fu_berlin.ties.util.Util;
27  
28  /***
29   * Counts <em>true</em> and <em>false</em> items and measures the
30   * <em>accuracy</em>: <em>A</em> = <em>true</em> /
31   * (<em>true</em> + <em>false</em>).
32   *
33   * <p>Instances of this class are not thread-safe.
34   *
35   * @author Christian Siefkes
36   * @version $Revision: 1.5 $, $Date: 2006/10/21 16:04:11 $, $Author: siefkes $
37   */
38  public class Accuracy extends BaseStorable implements AccuracyView {
39  
40      /***
41       * Serialization key for the accuracy.
42       */
43      public static final String KEY_ACCURACY = "Accuracy";
44  
45      /***
46       * Serialization key for the number of false items.
47       */
48      public static final String KEY_FALSE_COUNT = "False Count";
49  
50      /***
51       * Serialization key for the number of true items.
52       */
53      public static final String KEY_TRUE_COUNT = "True Count";
54  
55      /***
56       * Optional prefix used for (de)serializing this instance; should typically
57       * end in a space; empty string if not used.
58       */
59      private final String prefix;
60  
61      /***
62       * The number of false decisions.
63       */
64      private long falseCount = 0;
65  
66      /***
67       * The number of true decisions.
68       */
69      private long trueCount = 0;
70  
71      /***
72       * Creates a new empty instance, without using a prefix.
73       */
74      public Accuracy() {
75          this("");
76      }
77  
78      /***
79       * Creates a new empty instance.
80       *
81       * @param prefixString An optional prefix used for (de)serializing this
82       * instance; should typically end in a space; empty string if not used
83       */
84      public Accuracy(final String prefixString) {
85          super();
86          prefix = prefixString;
87      }
88  
89      /***
90       * Creates a new instance from a field map, fulfilling the
91       * {@link de.fu_berlin.ties.io.Storable} contract.
92       *
93       * @param fieldMap map containing the serialized fields
94       * @throws IllegalArgumentException if at least one of the parameters is
95       * negative or missing
96       */
97      public Accuracy(final FieldMap fieldMap) throws IllegalArgumentException {
98          this(fieldMap, "");
99      }
100 
101     /***
102      * Creates a new instance from a field map, using a prefix.
103      *
104      * @param fieldMap map containing the serialized fields
105      * @param prefixString An optional prefix used for (de)serializing this
106      * instance; should typically end in a space; empty string if not used
107      * @throws IllegalArgumentException if at least one of the parameters is
108      * negative or missing
109      */
110     public Accuracy(final FieldMap fieldMap, final String prefixString)
111             throws IllegalArgumentException {
112         prefix = prefixString;
113         trueCount = Util.asLong(fieldMap.get(prefix + KEY_TRUE_COUNT));
114         falseCount = Util.asLong(fieldMap.get(prefix + KEY_FALSE_COUNT));
115 
116         if ((trueCount < 0) || (falseCount < 0)) {
117             throw new IllegalArgumentException("Both '" + prefix
118                 + KEY_TRUE_COUNT + "' (" + trueCount + ") and '" + prefix
119                 + KEY_FALSE_COUNT + "' (" + falseCount + ") must be > 0");
120         }
121     }
122 
123     /***
124      * Calculates and returns the <em>accuracy</em>:
125      * <em>A</em> = <em>true</em> / (<em>true</em> + <em>false</em>).
126      *
127      * @return the accuracy
128      */
129     public double getAccuracy() {
130         return (double) trueCount / (trueCount + falseCount);
131     }
132 
133     /***
134      * Returns the number of false decisions.
135      * @return the number of false decisions
136      */
137     public long getFalseCount() {
138         return falseCount;
139     }
140 
141     /***
142      * Returns the number of true decisions.
143      * @return the number of true decisions
144      */
145     public long getTrueCount() {
146         return trueCount;
147     }
148 
149     /***
150      * Increases the number of false decisions by one.
151      */
152     public void incFalseCount() {
153         falseCount++;
154     }
155 
156     /***
157      * Increases the number of true decisions by one.
158      */
159     public void incTrueCount() {
160         trueCount++;
161     }
162 
163     /***
164      * {@inheritDoc}
165      */
166     public FieldMap storeFields() {
167         final FieldMap result = new FieldMap();
168         result.put(prefix + KEY_ACCURACY, new Double(getAccuracy()));
169         result.put(prefix + KEY_TRUE_COUNT, new Long(trueCount));
170         result.put(prefix + KEY_FALSE_COUNT, new Long(falseCount));
171         return result;
172     }
173 
174 }