You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/07/15 17:05:41 UTC

svn commit: r1503286 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/stat/Frequency.java test/java/org/apache/commons/math3/stat/FrequencyTest.java

Author: sebb
Date: Mon Jul 15 15:05:41 2013
New Revision: 1503286

URL: http://svn.apache.org/r1503286
Log:
MATH-1000 Add mode function to Frequency class

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1503286&r1=1503285&r2=1503286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Mon Jul 15 15:05:41 2013
@@ -51,6 +51,9 @@ If the output is not quite correct, chec
   </properties>
   <body>
     <release version="x.y" date="TBD" description="TBD">
+      <action dev="sebb" type="add" issue="MATH-1000">
+        Add mode function to Frequency class.
+      </action>
       <action dev="erans" type="fix" issue="MATH-1005" due-to="Roman Werpachowski">
         Fixed "MathArrays.linearCombination" when array length is 1.
       </action>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java?rev=1503286&r1=1503285&r2=1503286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java Mon Jul 15 15:05:41 2013
@@ -18,10 +18,13 @@ package org.apache.commons.math3.stat;
 
 import java.io.Serializable;
 import java.text.NumberFormat;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Comparator;
+import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.TreeMap;
 
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
@@ -493,6 +496,39 @@ public class Frequency implements Serial
         return getCumPct(Character.valueOf(v));
     }
 
+    /**
+     * Returns the mode value(s) in comparator order.
+     * 
+     * @return a list containing the value(s) which appear most often.
+     * @since 3.3
+     */
+    public List<Comparable<?>> getMode() {
+        long mostPopular = 0; // frequencies are always positive
+        
+        // Get the max count first, so we avoid having to recreate the List each time
+        for(Long l : freqTable.values()) {
+            long frequency = l.longValue();
+            if (frequency > mostPopular) {
+                mostPopular = frequency;
+            }
+        }
+
+        List<Comparable<?>> modeList = new ArrayList<Comparable<?>>();
+        for (Entry<Comparable<?>, Long> ent : freqTable.entrySet()) {
+            long frequency = ent.getValue().longValue();
+            if (frequency == mostPopular) {
+               modeList.add(ent.getKey());
+// Alternatively, to avoid scanning the entries twice, keep recreating the set
+// To use this approach, comment out the values() scan loop above and uncomment below              
+//            } else if (frequency > mostPopular) {
+//                modeList.clear(); // the previous List is obsolete
+//                modeList.add(ent.getKey());
+//                mostPopular = frequency;
+            }
+        }
+        return modeList;
+    }
+
     //----------------------------------------------------------------------------------------------
 
     /**

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java?rev=1503286&r1=1503285&r2=1503286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java Mon Jul 15 15:05:41 2013
@@ -334,18 +334,28 @@ public final class FrequencyTest {
         Assert.assertEquals(1, f.getCount(THREEE));        
     }
     
-//    @Test
-//    public void testMode() {
-//        Assert.assertEquals(0, f.getMode().size());
-//        f.addValue("1");
-//        Assert.assertEquals(1, f.getMode().size());
-//        f.addValue("2");
-//        Assert.assertEquals(2, f.getMode().size());
-//        Assert.assertTrue(f.getMode().contains("1"));
-//        Assert.assertTrue(f.getMode().contains("2"));
-//        f.addValue("2");
-//        Assert.assertEquals(1, f.getMode().size());
-//        Assert.assertFalse(f.getMode().contains("1"));
-//        Assert.assertTrue(f.getMode().contains("2"));
-//    }
+    @Test
+    public void testMode() {
+        List<Comparable<?>> mode;
+        mode = f.getMode();
+        Assert.assertEquals(0, mode.size());
+
+        f.addValue("3");
+        mode = f.getMode();
+        Assert.assertEquals(1, mode.size());
+        Assert.assertEquals("3", mode.get(0));
+
+        f.addValue("2");
+        mode = f.getMode();
+        Assert.assertEquals(2, mode.size());
+        Assert.assertEquals("2", mode.get(0));
+        Assert.assertEquals("3",mode.get(1));
+
+        f.addValue("2");
+        mode = f.getMode();
+        Assert.assertEquals(1, mode.size());
+        Assert.assertEquals("2", mode.get(0));
+        Assert.assertFalse(mode.contains("1"));
+        Assert.assertTrue(mode.contains("2"));
+    }
 }