Jump to content

Basic statistical methods


Recommended Posts

Posted (edited)

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
Posted

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
Posted (edited)

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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