Reputation: 3356
I'm trying to get the percentile of a particular number within a distribution using the Apache Commons Math3 library, and the Percentile class:
(I'm consuming this in Scala)
If I do:
new Percentile().evaluate(Array(1,2,3,4,5), 80)
Then I get 4
back. However, I want to go the other direction, and give 4
as the input, and get back 80
as the result, i.e., the percentile of a given number, not the number at a given percentile.
None of the methods on this class seem to fit or give the result I want. Am I misusing the class? Is there another class I should be using?
Upvotes: 7
Views: 5864
Reputation: 802
Seems not to work, as this test should pass (but in fact it doesn't)
@Test
public void testCalculatePercentile() {
//given
double[] values = new double[]{2,3,3,3};
EmpiricalDistribution distribution = new EmpiricalDistribution(values.length);
distribution.load(values);
//when
double percentile = distribution.cumulativeProbability(2.7d);
//then
assertThat(percentile).isEqualTo(0.25);
}
Upvotes: 0
Reputation: 10051
You can use an EmpiricalDistribution loaded with your base line values:
@Test
public void testCalculatePercentile() {
//given
double[] values = new double[]{1,2,3,4,5};
EmpiricalDistribution distribution = new EmpiricalDistribution(values.length);
distribution.load(values);
//when
double percentile = distribution.cumulativeProbability(4);
//then
assertThat(percentile).isEqualTo(0.8);
}
Upvotes: 4