[ 
https://issues.apache.org/jira/browse/RNG-142?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17358225#comment-17358225
 ] 

Alex Herbert commented on RNG-142:
----------------------------------

bq. hence the expectation that the fluent API returns an object of the same 
type.

I think the other expectation is once you have a sampler it does not matter 
that it is of a certain type. All that matters is it produces samples. 
Essentially it hides the type of sampler and just states the contract is to 
provide samples of a certain type. If it shares state then the sampler can be 
duplicated with another RNG. So without breaking binary compatibility this is 
the API we currently have.

{quote}Could we perhaps go one step further
{quote}
Two steps further would be to move to RNG 2.0 and fix the 
withUniformRandomProvider to return the class and not the interface for all 
implementations. The class can be typed by the parameter as suggested in 
[#comment-17357784] that mandates the return type of withUniformRandomProvider. 
This is break in binary compatibility that would make the API return the 
expected types from the methods.

A step back and the suggestion of factory methods to create samplers of a given 
type is of value. The library currently has multiple samplers for Gaussian, 
Poisson, and enumerated probability distributions. In the PR for the composite 
samplers (RNG-138) I added an enum in the class for a choice of enumerated 
distribution. This I am still considering if it is the correct solution. There 
the interface for creating a distribution (below) is implemented by an enum:
{code:java}
public interface DiscreteProbabilitySamplerFactory {
    /**
     * Creates the sampler.
     *
     * @param rng Source of randomness.
     * @param probabilities Discrete probability distribution.
     * @return the sampler
     */
    DiscreteSampler create(UniformRandomProvider rng,
                           double[] probabilities);
}
{code}
Your suggestion is that GaussianSampler has a factory method to create a 
sampler with the chosen algorithm specified by an enum. The idea with the enum 
as a factory would be:
{code:java}
public enum GaussianSamplers {
    ZIGGURAT {
        @Override
        NormalizedGaussianSampler create(UniformRandomProvider rng) {
            return new ZigguratNormalizedGaussianSampler(rng);
        }
    };

    abstract NormalizedGaussianSampler create(UniformRandomProvider rng);

    public GaussianSampler of(UniformRandomProvider rng, double mean, double 
stdDev) {
        return new GaussianSampler(create(rng), mean, stdDev);
    }

    public static GaussianSampler create(UniformRandomProvider rng, double 
mean, double stdDev) {
        return ZIGGURAT.of(rng, mean, stdDev);
    }
}

// ...
GaussianSampler g = GaussianSamplers.ZIGGURAT.of(rng, mean, stdDev);
GaussianSampler g = GaussianSamplers.create(rng, mean, stdDev);
{code}

The create method is just for a default.


> Return type of method "withUniformRandomProvider"
> -------------------------------------------------
>
>                 Key: RNG-142
>                 URL: https://issues.apache.org/jira/browse/RNG-142
>             Project: Commons RNG
>          Issue Type: Improvement
>          Components: sampling
>            Reporter: Gilles Sadowski
>            Priority: Major
>
> Expected usage:
> {code:java}
> NormalizedGaussianSampler n01 = 
> ZigguratNormalizedGaussianSampler.of(RandomSource.create(RandomSource.KISS));
> GaussianSampler g = 
> GaussianSampler.of(n01.withUniformRandomProvider(RandomSource.create(RandomSource.JSF_64)),
>  0.43, 2.1);
> {code}
> Code doesn't compile: Method {{withUniformRandomProvider}} returns a 
> {{SharedStateContinuousSampler}} whereas a {{NormalizedGaussianSampler}} is 
> required.
> Am I missing something?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to