0

Could you guys help me which apache-commons-math classes can I use to calculate the average of the top third of the population.

To calculate the average I know can use org.apache.commons.math3.stat.descriptive.DescriptiveStatistics.

How to get the top third of the population?

Example

Population: 0, 0, 0, 0, 0, 1, 2, 2, 3, 5,14

Top third: 2, 3, 5, 14

Average = 24/4= 6.0

Community
  • 1
  • 1
KirdApe
  • 323
  • 1
  • 4
  • 12
  • apache-commons-math doesn't seem to provide built in top-X. But it shouldn't really be a hard problem, easily workable from sortedValues – Deltharis Oct 20 '14 at 11:36

1 Answers1

1

First of all what do you call top third of population? If set is divides by 3 and remainder is 0, then its is simple, but in your case 11%3 = 2. So You should know how to get top third when remainder is not equal 0.

I would suggest You to use Arrays procedures, to get Top third of set. If You still want to use DescriptiveStatistics you can invoke it.

    double[] set = {0, 0, 0, 0, 0, 1, 2, 2, 3,4,5};

    Arrays.sort(set);

    int from = 0;
    if (set.length % 3==0){
        from = set.length/3*2 ;
    }
    if (set.length % 3 != 0){
        from = Math.round(set.length/3*2) + 1;
    }

    double [] topThirdSet = Arrays.copyOfRange(set,from , set.length);
    DescriptiveStatistics ds = new DescriptiveStatistics(topThirdSet);
    System.out.println(ds.getMean());
K.I.
  • 759
  • 1
  • 11
  • 30
  • The steps I need to do: 1. Sort the individuals 2.Select the one third of the population with the highest values 3. Calculate average for that population – KirdApe Oct 23 '14 at 10:10