commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Herbert <alex.d.herb...@gmail.com>
Subject [rng] SharedStateSampler
Date Fri, 19 Jul 2019 09:35:52 GMT
This interface has been added in v1.3:

/**
 * Applies to samplers that can share state between instances. Samplers can be created with
a
 * new source of randomness that sample from the same state.
 *
 * @param <R> Type of the sampler.
 * @since 1.3
 */
public interface SharedStateSampler<R> {
    /**
     * Create a new instance of the sampler with the same underlying state using the given
     * uniform random provider as the source of randomness.
     *
     * @param rng Generator of uniformly distributed random numbers.
     * @return the sampler
     */
    R withUniformRandomProvider(UniformRandomProvider rng);
}

All of the DiscreteSampler and ContinuousSampler classes have been updated to implement SharedStateSampler.

This is the equivalent of:

/**
 * Sampler that generates values of type {@code int} and can create new instances to sample
 * from the same state with a given source of randomness.
 *
 * @since 1.3
 */
public interface SharedStateDiscreteSampler
    extends DiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler> {
    // Marker interface
}


If this combined marker interface is added to the library it would simplify a lot of the code
for samplers which have internal delegates to avoid casting. With this interface they can
hold a SharedStateDiscreteSampler rather than a DiscreteSampler delegate and directly use
it to create new delegates.

For example PoissonSampler code which wraps a specific implementation:

/**
 * @param rng Generator of uniformly distributed random numbers.
 * @param source Source to copy.
 */
private PoissonSampler(UniformRandomProvider rng,
        PoissonSampler source) {
    super(null);

    if (source.poissonSamplerDelegate instanceof SmallMeanPoissonSampler) {
        poissonSamplerDelegate =
            ((SmallMeanPoissonSampler)source.poissonSamplerDelegate).withUniformRandomProvider(rng);
    } else {
        poissonSamplerDelegate =
            ((LargeMeanPoissonSampler)source.poissonSamplerDelegate).withUniformRandomProvider(rng);
    }
}

Becomes:

private PoissonSampler(UniformRandomProvider rng,
        PoissonSampler source) {
    super(null);
    poissonSamplerDelegate = source.poissonSamplerDelegate.withUniformRandomProvider(rng);
}


I propose to add:

public interface SharedStateDiscreteSampler
    extends DiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler> {
    // Marker interface
}

public interface SharedStateContinuousSampler
    extends ContinuousSampler, SharedStateSampler<SharedStateContinuousSampler> {
    // Marker interface
}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message