Simple statistics - Java packages for calculating mean, standard deviation, etc

77,337

Solution 1

Apache Commons Math, specifically DescriptiveStatistics and SummaryStatistics.

Solution 2

Since Java SE 8 a number of classes has been added to the platform:

Solution 3

Just responding to this part of the question:

I was quite surprised that there does not appear to be a function to calculate the Mean in the java.lang.Math package...

I don't think I was surprised to find this. There are a lot of "useful algorithms" that the Java class libraries do not implement. They do not implement everything. And in this, they are no different from other programming languages.

Actually It would be a bad thing if Sun did try to implement too much in J2SE:

  1. It would take more designer / developer / technical documenter time ... with no clear "return on investment".

  2. It would increase the Java footprint; e.g. the size of "rt.jar". (Or if they tried to mitigate that, it would result in more platform complexity ... )

  3. For things in the mathematical space, you often need to implement the algorithms in different ways (with different APIs) to cater for different requirements.

  4. For complex things, it may be better for Sun not to try to "standardise" the APIs, but leave it to some other interested / skilled group to do it; e.g. the Apache folks.

Solution 4

import java.util.*;
public class stdevClass {
    public static void main(String[] args){
        int [] list = {1,-2,4,-4,9,-6,16,-8,25,-10};
        double stdev_Result = stdev(list);
        System.out.println(stdev(list));
    }

    public static double stdev(int[] list){
        double sum = 0.0;
        double mean = 0.0;
        double num=0.0;
        double numi = 0.0;
        double deno = 0.0;

        for (int i : list) {
            sum+=i;
        }
        mean = sum/list.length;

        for (int i : list) {
            numi = Math.pow((double) i - mean), 2);
            num+=numi;
        }

        return Math.sqrt(num/list.length);
    }
}

Solution 5

I think there is no direct method and classes in java. We have to build it for our own. For your requirement this code will help you. Calculate Standard Deviation in java

Share:
77,337
Peter Perháč
Author by

Peter Perháč

Currently am on a contract, building APIs for the Valuation Office Agency, transforming the way agents interact with the VOA. Coursera certificate - Functional Programming Principles in Scala Coursera certificate - Functional Program Design in Scala Oracle Certified Associate, Java SE 7 Programmer Oracle Certified Professional, Java SE 7 Programmer II Oracle Certified Expert, Java EE6 Web Component Developer github.com/PeterPerhac July 2018 update: Books currently on my desk: (see my goodreads for up-to-date info) Functional and Reactive Domain Modeling Practical Vim, 2nd Edition (Great) books I parked (for now): Reactive Messaging Patterns with the Actor Model: Applications and Integration in Scala and Akka Functional Programming in Scala Learn You a Haskell for Great Good A Practical Guide to Ubuntu Linux Domain-Driven Design: Tackling Complexity in the Heart of Software Plan to look into: Play framework Linux administration Dale Carnegie books (various) Read cover-to-cover: Advanced Scala with Cats Clean Code Release It! - Design and Deploy Production-Ready Software The Phonix Project Spring in Action (3rd edition) Pragmatic Scala - Create Expressive, Concise, and Scalable Applications UML distilled, 2nd edition Pro C# 2010 and the .NET 4 Platform The Art of Unit Testing: With Examples in C# Read selectively: The Well-Grounded Java Developer - Vital Techniques of Java 7 and polyglot programming Switch - how to change things when change is hard Practical Unit Testing with JUnit and Mockito Effective Java, second edition Java Concurrency in Practice Nationality: Slovak

Updated on July 05, 2022

Comments

  • Peter Perháč
    Peter Perháč almost 2 years

    Could you please suggest any simple Java statistics packages?

    I don't necessarily need any of the advanced stuff. I was quite surprised that there does not appear to be a function to calculate the Mean in the java.lang.Math package...

    What are you guys using for this?


    EDIT

    Regarding:

    How hard is it to write a simple class that calculates means and standard deviations?

    Well, not hard. I only asked this question after having hand-coded these. But it only added to my Java frustration not to have these simplest functions available at hand when I needed them. I don't remember the formula for calculating stdev by heart :)

  • Peter Perháč
    Peter Perháč over 14 years
    thanks. I take it there isn't really a reason for looking any further. You're satisfied with Apache Commons, or is it just so-so, good-enough, could-be-better?
  • Grundlefleck
    Grundlefleck over 14 years
    Just discovered this library, precisely for calculating mean, standard deviation. Very easy to pick up. +1
  • John Paulett
    John Paulett over 14 years
    I've found it to fit my needs well. While I've never personally run into this issue or cared, I have a coworker who found it to be slower when computing the mean of an array than just doing a loop to add the values then dividing by the size of the array. However, his code was averaging things that would likely never cause integer overflow errors. I assume that Commons Math is a little smarter and won't let integers overflow.
  • Stephen C
    Stephen C over 14 years
    The APIs use double not int or long so integer overflow is not an issue. However, they cannot handle value sets with more than Integer.MAX_VALUE doubles.
  • duffymo
    duffymo over 14 years
    You should not have to store an array to calculate a mean or standard deviation. It's easy to do both without having to take up all that memory.
  • duffymo
    duffymo over 14 years
    How hard is it to write a simple class that calculates means and standard deviations? Must there be a library for everything?
  • John Paulett
    John Paulett over 14 years
    @duffymo, the original data was in an array, so it was just keeping a running total and then dividing by the size of the array.
  • duffymo
    duffymo over 14 years
    Yes, I realize that. All I'm saying is that an array isn't necessary. It's not even desirable if you're trying to minimize the amount of memory you consume.
  • Peter Perháč
    Peter Perháč over 14 years
    @duffymo As a classic Java programmer, I am definitely not concerned by stuff like how much memory do my programs consume. (<-- joking, of course) As to How hard is it to write a simple class that calculates means and standard deviations? well, not hard. I only asked this question after having hand-coded these. But it only added to my Java frustration not to have these simplest functions available at my hand when I needed them. I don't remember the formula for calculating stdev by heart :)
  • Stephen C
    Stephen C over 14 years
    @duffymo - my reading of the Apache library APIs is that they require you to pass the values to be averaged in an array.
  • Stephen C
    Stephen C over 14 years
    @MasterPeter - but I'm sure you remember the URL for Wikipedia by heart :-) :-)
  • Grundlefleck
    Grundlefleck over 14 years
    @duffymo, while I also would have found it easy to write the functions I used in commons-math, stupid mistakes can be made by anyone, at any time. Sometimes I prefer not to leave it to chance. Also, in some cases it's preferable to up the memory footprint in exchange for a tested solution. All depends on the situation I guess...
  • duffymo
    duffymo over 14 years
    @Grundlefleck - I agree that everyone makes stupid mistakes, and I realize the value of libraries, but a simple mean and standard deviation calculator are low on the risk scale. It's easy to write, easy to test, and put aside. There's an argument that says minimizing dependencies is a good idea, too. Why add another library to your app when it's so easy to roll your own?
  • duffymo
    duffymo over 14 years
    @Stephen C - agreed. I'm saying that's fine when you have a reasonable number of values, but as the array size grows you'll have a problem storing them. What do you do in the case of a runtime app that you want to keep a running tab on mean and standard deviation of values as they arrive? Your array won't be very useful in that situation.
  • Michael Rusch
    Michael Rusch over 12 years
    Just to save people a few clicks: DescriptiveStatistics is the one that holds all of the values you send it in memory, and SummaryStatistics does not hold them in memory.
  • cowls
    cowls almost 9 years
    Answered two years after John Paulett's answer. Clearly there are libraries available to do it. I wouldnt implement your own unless for educational purposes.
  • wvdz
    wvdz over 7 years
    Nice, but pretty lame that it doesn't provide getStd()
  • Enrico Giurin
    Enrico Giurin over 7 years
    I can't understand why the method to calculate the deviation wasn't added to IntSummaryStatistics.