You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gilles Sadowski <gi...@gmail.com> on 2021/05/25 19:14:36 UTC
[RNG] Multiple samplers, shared "UniformRandomProvider"
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<double[]>
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<double[]>() {
/** {@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---
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [RNG] Multiple samplers, shared "UniformRandomProvider"
Posted by Gilles Sadowski <gi...@gmail.com>.
Le mer. 26 mai 2021 à 00:06, Alex Herbert <al...@gmail.com> a écrit :
>
> On Tue, 25 May 2021 at 20:15, Gilles Sadowski <gi...@gmail.com> 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<double[]>
> > 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<double[]>() {
> > /** {@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<double[]>. The implementation
> is to create a new instance using the same method:
>
> static SharedStateObjectSampler<double[]> 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<double[]>() {
> @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<double[]>
> 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-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [RNG] Multiple samplers, shared "UniformRandomProvider"
Posted by Alex Herbert <al...@gmail.com>.
On Tue, 25 May 2021 at 20:15, Gilles Sadowski <gi...@gmail.com> 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<double[]>
> 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<double[]>() {
> /** {@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<double[]>. The implementation
is to create a new instance using the same method:
static SharedStateObjectSampler<double[]> 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<double[]>() {
@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<double[]>
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