I would like to use Hipparchus function to compute the p-th percentile of a number of values.
I chose the EstimationType.R_5 to mimic (type = 5) in R software.
On a small sample I obtain an answer which is quite different from what I expected (computed with R, type = 5). See test below.
I am sure the difference is perfectly explainable by slightly different implementation and the fact that the sample is small. Yet I would like to understand the reason a bit more.
Thanks in advance,
test_percentile.java (661 Bytes)
In your example, you should replace
perc = perc.withEstimationType(EstimationType.R_5);
withEstimationType does not change the current instance, it creates a new instance, as documented in the Javadoc. The various
withXXX methods in several Hipparchus classes follow a mix of both the builder design patter and the fluent design pattern. They are expected to be used as follows:
SomeClass theFinalizedObject = new BasicConstructor(...).
So the object you get after the execution of this statement is in fact the fourth object built. The three previous objetcs are only temporary. The reason for this design choice is that it allows highly versatile construction with many variations, while keeping the API simple and readable. Built objects can also be kept immutable, which often improves performances.
This design is well suited when objects are built once (out of a loop) and used several times (inside a loop). Its advantages are more dubious when objects are created on the fly and used only once, because we really build n objects when we need one. In order to handle gracefully also this case, we could (should?) switch to a more canonical builder design pattern, where the builder and the built object correspond to different classes, the builder being mutable while the built object is immutable and built only during a final call to a no-argument
build() method. We did not make this move yet.
Thank you Luc, for pointing out my error and also for the explanation.
I completely overlooked the fact that the withPercentile(…) was returning a new instance.