Re: [RNG] Multiple samplers, shared "UniformRandomProvider"

2021-05-25 Thread Gilles Sadowski
Le mer. 26 mai 2021 à 00:06, Alex Herbert  a écrit :
>
> On Tue, 25 May 2021 at 20:15, Gilles Sadowski  wrote:
>
> > Hi.
> >
> > I wonder if/how we would introduce the following functionality:
> > ---CUT---
> > /**
> >  * @param rng Generator that will be shared in the returned sampler.
> >  * @param list Samplers whose underlying generators will be discarded
> > in
> >  * the returned instance.
> >  * @return a sampler sharing the given provider.
> >  */
> > public static ObjectSampler
> > withUniformRandomProvider(final UniformRandomProvider rng, final
> > SharedStateContinuousSampler... list) {
> > final SharedStateContinuousSampler[] samplers = new
> > SharedStateContinuousSampler[list.length];
> > for (int i = 0; i < list.length; i++) {
> > samplers[i] = list[i].withUniformRandomProvider(rng);
> > }
> >
> > return new ObjectSampler() {
> > /** {@inheritDoc} */
> > @Override
> > public double[] sample() {
> > final double[] out = new double[list.length];
> > for (int i = 0; i < list.length; i++) {
> > out[i] = samplers[i].sample();
> > }
> > return out;
> > }
> > };
> > }
> > ---CUT---
> >
>
> Note it can return SharedStateObjectSampler. The implementation
> is to create a new instance using the same method:
>
> static SharedStateObjectSampler of(UniformRandomProvider rng,
> SharedStateContinuousSampler... list) {
> final SharedStateContinuousSampler[] samplers = new
> SharedStateContinuousSampler[list.length];
> for (int i = 0; i < list.length; i++) {
> samplers[i] = list[i].withUniformRandomProvider(rng);
> }
>
> return new SharedStateObjectSampler() {
> @Override
> public double[] sample() {
> final double[] out = new double[samplers.length];
> for (int i = 0; i < samplers.length; i++) {
> out[i] = samplers[i].sample();
> }
> return out;
> }
>
> @Override
> public SharedStateObjectSampler
> withUniformRandomProvider(UniformRandomProvider rng) {
> return of(rng, samplers);
> }
> };
> }
>
> In this case though the anonymous inner class retains a reference to the
> enclosing class. So chaining the withUniformRandomProvider calls on
> returned objects would have a memory overhead. It would be cleaner to
> return a static class with the same functionality.
>
> Out of interest, do you have a use case?

Purpose is to supersede CM's "UncorrelatedRandomVectorGenerator".

> Possible name:
> CompoundSampler
>
> This is a variation on the CompositeSampler I suggested in another thread.
> A composite sampler (name TBD) is composed of 2 or more samplers. A sampler
> is chosen using a weighted distribution and a single sample returned from 1
> sampler. Here a compound sampler (name TBD) is composed of 2 or more
> samplers. The return sample is a single sample from each sampler in the
> compound.
>
> I will open a ticket and PR with the static CompositeSamplers factory class
> I created. The idea of a compound sampler could be added to the same class.

+1

>
> Alex

[1] 
https://gitbox.apache.org/repos/asf?p=commons-math.git;a=blob;f=commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGenerator.java;h=9df5fef1a39fc535ab66874169f329a9adbef36b;hb=refs/heads/modularized_master

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



Re: [RNG] Multiple samplers, shared "UniformRandomProvider"

2021-05-25 Thread Alex Herbert
On Tue, 25 May 2021 at 20:15, Gilles Sadowski  wrote:

> Hi.
>
> I wonder if/how we would introduce the following functionality:
> ---CUT---
> /**
>  * @param rng Generator that will be shared in the returned sampler.
>  * @param list Samplers whose underlying generators will be discarded
> in
>  * the returned instance.
>  * @return a sampler sharing the given provider.
>  */
> public static ObjectSampler
> withUniformRandomProvider(final UniformRandomProvider rng, final
> SharedStateContinuousSampler... list) {
> final SharedStateContinuousSampler[] samplers = new
> SharedStateContinuousSampler[list.length];
> for (int i = 0; i < list.length; i++) {
> samplers[i] = list[i].withUniformRandomProvider(rng);
> }
>
> return new ObjectSampler() {
> /** {@inheritDoc} */
> @Override
> public double[] sample() {
> final double[] out = new double[list.length];
> for (int i = 0; i < list.length; i++) {
> out[i] = samplers[i].sample();
> }
> return out;
> }
> };
> }
> ---CUT---
>

Note it can return SharedStateObjectSampler. The implementation
is to create a new instance using the same method:

static SharedStateObjectSampler of(UniformRandomProvider rng,
SharedStateContinuousSampler... list) {
final SharedStateContinuousSampler[] samplers = new
SharedStateContinuousSampler[list.length];
for (int i = 0; i < list.length; i++) {
samplers[i] = list[i].withUniformRandomProvider(rng);
}

return new SharedStateObjectSampler() {
@Override
public double[] sample() {
final double[] out = new double[samplers.length];
for (int i = 0; i < samplers.length; i++) {
out[i] = samplers[i].sample();
}
return out;
}

@Override
public SharedStateObjectSampler
withUniformRandomProvider(UniformRandomProvider rng) {
return of(rng, samplers);
}
};
}

In this case though the anonymous inner class retains a reference to the
enclosing class. So chaining the withUniformRandomProvider calls on
returned objects would have a memory overhead. It would be cleaner to
return a static class with the same functionality.

Out of interest, do you have a use case?

Possible name:
CompoundSampler

This is a variation on the CompositeSampler I suggested in another thread.
A composite sampler (name TBD) is composed of 2 or more samplers. A sampler
is chosen using a weighted distribution and a single sample returned from 1
sampler. Here a compound sampler (name TBD) is composed of 2 or more
samplers. The return sample is a single sample from each sampler in the
compound.

I will open a ticket and PR with the static CompositeSamplers factory class
I created. The idea of a compound sampler could be added to the same class.

Alex