Jump to content

Basic statistical methods


Botre

Recommended Posts

mean

median

mode

range

package org.bjornkrols.math;

import java.util.*;

/**
 * Created by Bjorn on 5/05/2015.
 */
public final class Statistics {

    private Statistics() {
        // This class should never be instantiated.
        // Do not delete or make accessible.
    }

    /**
     * @return The average value.
     */
    public static double mean(int... n) {
        double total = 0;
        for (int i : n) total += i;
        return total / n.length;
    }

    /**
     * @return Midpoint between the lowest and highest value of the set.
     */
    public static double median(int... n) {
        Arrays.sort(n);
        int length = n.length;
        int center = n[length / 2];
        return length % 2 == 0 ? mean(n[(length / 2) - 1], center) : center;
    }

    /**
     * @return The most frequently occurring value(s) of the set.
     */
    public static List<Integer> mode(int... n) {
        Map<Integer, Integer> occurrence = new HashMap<Integer, Integer>(n.length);
        int maximum = 0;
        List<Integer> maximumElements = new ArrayList<Integer>();
        for (int i : n) {
            Integer o = occurrence.get(i);
            if (o == null) occurrence.put(i, o = 1);
            else occurrence.put(i, o += 1);
            if (o > maximum) {
                maximum = o;
                maximumElements.clear();
                maximumElements.add(i);
            } else if (o == maximum) maximumElements.add(i);
        }
        return maximumElements;
    }

    /**
     * @return The span of values over which the data set occurs.
     */
    public static int range(int... n) {
        int minimum = Integer.MAX_VALUE;
        int maximum = Integer.MIN_VALUE;
        for (int i : n) {
            if (i < minimum) minimum = i;
            if (i > maximum) maximum = i;
        }
        return maximum - minimum;
    }

}
Edited by Botre
Link to comment
Share on other sites

Store occurance.get(i) in a variable to avoid excess accesses to the map. Same with length/2, to lower the overhead from calculations

The mode method is pretty expensive. Try something a little less intense:

int mode(int...numbers) {
    int highestNumber = 0, mostOccurrences = 0;
    int[] counter = new int[numbers.length];

    for(int i = 0; i < counter.length; i++) {
        int currentNumber = numbers[i];
        int occurrences = counter[currentNumber]++;

        if(mostOccurrences < occurrences) {
            mostOccurrences = occurrences;
            highestNumber = currentNumber;
        }
    }

    return highestNumber;
}
  • Like 2
Link to comment
Share on other sites

 

Store occurance.get(i) in a variable to avoid excess accesses to the map. Same with length/2, to lower the overhead from calculations

The mode method is pretty expensive. Try something a little less intense:

int mode(int...numbers) {
    int highestNumber = 0, mostOccurrences = 0;
    int[] counter = new int[numbers.length];

    for(int i = 0; i < counter.length; i++) {
        int currentNumber = numbers[i];
        int occurrences = counter[currentNumber]++;

        if(mostOccurrences < occurrences) {
            mostOccurrences = occurrences;
            highestNumber = currentNumber;
        }
    }

    return highestNumber;
}

 

Thanks for the feedback!

(updated)

Edited by Botre
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...