[
https://issues.apache.org/jira/browse/STATISTICS-62?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17685645#comment-17685645
]
Gilles Sadowski commented on STATISTICS-62:
-------------------------------------------
We could use {{withDefaults()}} to avoid the singleton flavour of
{{instance()}} and self-document the existence of default settings:
{code:java}
KolmogorovSmirnovTest.withDefaults()
.with(AlternativeHypothesis.LESS_THAN)
.with(PValueMethod.EXACT)
.with(Inequality.STRICT)
.test(x, y)
{code}
Alternatively, with a "hidden" builder:
{code:java}
KolmogorovSmirnovTest.withDefaults()
.with(AlternativeHypothesis.LESS_THAN)
.with(PValueMethod.EXACT)
.with(Inequality.STRICT)
.get()
.test(x, y)
{code}
All codes below untested...
Generic builder:
{code:java}
public abstract class TestOptionsBuilder<T> {
private AlternativeHypothesis alternativeHypothesis;
// ... other options
TestOptionsBuilder<T> with(AlternativeHypothesis alternativeHypothesis) {
this.alternativeHypothesis = alternativeHypothesis;
return this;
}
/** @return the option. */
protected AlternativeHypothesis alternativeHypothesis() {
return alternativeHypothesis
}
/** @return the test instance. */
public abstract T get();
}
{code}
Then
{code:java}
public class KolmogorovSmirnovTest {
private final AlternativeHypothesis alternativeHypothesis;
private KolmogorovSmirnovTest(AlternativeHypothesis alternativeHypothesis
/* and other options */) {
this.alternativeHypothesis = alternativeHypothesis;
// ... set other options
}
public static TestOptionsBuilder<KolmogorovSmirnovTest> withDefaults() {
final Options defaultOptions = new Options();
defaultOptions.with(AlternativeHypothesis.LESS_THAN);
// ... set other defaults.
return defaultOptions;
}
private static class Options extends
TestOptionsBuilder<KolmogorovSmirnovTest> {
@Override
public KolmogorovSmirnovTest get() {
return new KolmogorovSmirnovTest(alternativeHypothesis());
}
}
}
{code}
We could perhaps also have
{code:java}
/* package-private */ enum SupportedOptions {
ALTERNATIVE_HYPOTHESIS,
P_VALUE_METHOD,
INEQUALITY;
}
{code}
Then
{code:java}
public class KolmogorovSmirnovTest {
// ... same as above.
public static TestOptionsBuilder<KolmogorovSmirnovTest>
withDefaults(boolean throwIfNotSupported) {
// Assuming
// 1. a non-default constructor in the base class:
// protected TestOptionsBuilder(boolean throwIfNotSupported,
// SupportedOptions ... supported)
// 2. if throwIfNotSupported == true, any user call to a "with(...)"
method with an
// argument that is not a supported option would raise an exception.
// 3. if throwIfNotSupported == false, setting a non-supported option
is a no-op.
final Options defaultOptions = new Options(throwIfNotSupported,
SupportedOptions.ALTERNATIVE_HYPOTHESIS);
// ...
}
public static TestOptionsBuilder<KolmogorovSmirnovTest> withDefaults() {
return withDefaults(true);
}
// ...
}
{code}
> Port o.a.c.math.stat.inference to a commons-statistics-inference module
> -----------------------------------------------------------------------
>
> Key: STATISTICS-62
> URL: https://issues.apache.org/jira/browse/STATISTICS-62
> Project: Commons Statistics
> Issue Type: New Feature
> Components: inference
> Affects Versions: 1.0
> Reporter: Alex Herbert
> Priority: Major
>
> The o.a.c.math4.legacy.stat.inference package contains:
>
> {noformat}
> AlternativeHypothesis.java
> BinomialTest.java
> ChiSquareTest.java
> GTest.java
> InferenceTestUtils.java
> KolmogorovSmirnovTest.java
> MannWhitneyUTest.java
> OneWayAnova.java
> TTest.java
> WilcoxonSignedRankTest.java{noformat}
> The are few dependencies on other math packages. The notable exceptions are:
>
> 1. KolmogorovSmirnovTest which requires matrix support. This is for
> multiplication of a square matrix to support a matrix power function. This
> uses a double matrix and the same code is duplicated for a BigFraction
> matrix. Such code can be ported internally to support only the required
> functions. It can also drop the defensive copy strategy used by Commons Math
> in matrices to allow multiply in-place where appropriate for performance
> gains.
> 2. OneWayAnova which collates the sum, sum of squares and count using
> SummaryStatistics. This can be done using an internal class. It is possible
> to call the test method using already computed SummaryStatistics. The method
> that does this using the SummaryStatistics as part of the API can be dropped,
> or supported using an interface that returns: getSum, getSumOfSquares, getN.
> All the inference Test classes have instance methods but no state. The
> InferenceTestUtils is a static class that holds references to a singleton for
> each class and provides static methods to pass through the underlying
> instances.
> I suggest changing the test classes to have only static methods and dropping
> InferenceTestUtils.
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)