Use method reference to create a sampler

This commit is contained in:
aherbert 2022-12-02 13:33:39 +00:00
parent f7ef9b625b
commit 6d767220ed
2 changed files with 4 additions and 56 deletions

View File

@ -23,8 +23,6 @@ import org.apache.commons.math4.legacy.exception.OutOfRangeException;
import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler; import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler;
import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction;
import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
import org.apache.commons.math4.core.jdkmath.JdkMath; import org.apache.commons.math4.core.jdkmath.JdkMath;
/** /**
@ -194,31 +192,7 @@ public abstract class AbstractIntegerDistribution
/**{@inheritDoc} */ /**{@inheritDoc} */
@Override @Override
public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) { public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) {
return new DiscreteDistribution.Sampler() { // Inversion method distribution sampler.
/** return InverseTransformDiscreteSampler.of(rng, this::inverseCumulativeProbability)::sample;
* Inversion method distribution sampler.
*/
private final DiscreteSampler sampler =
new InverseTransformDiscreteSampler(rng, createICPF());
/** {@inheritDoc} */
@Override
public int sample() {
return sampler.sample();
}
};
}
/**
* @return an instance for use by {@link #createSampler(UniformRandomProvider)}
*/
private DiscreteInverseCumulativeProbabilityFunction createICPF() {
return new DiscreteInverseCumulativeProbabilityFunction() {
/** {@inheritDoc} */
@Override
public int inverseCumulativeProbability(double p) {
return AbstractIntegerDistribution.this.inverseCumulativeProbability(p);
}
};
} }
} }

View File

@ -24,8 +24,6 @@ import org.apache.commons.math4.legacy.exception.OutOfRangeException;
import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler; import org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler;
import org.apache.commons.rng.sampling.distribution.ContinuousInverseCumulativeProbabilityFunction;
import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
import org.apache.commons.math4.core.jdkmath.JdkMath; import org.apache.commons.math4.core.jdkmath.JdkMath;
/** /**
@ -211,31 +209,7 @@ public abstract class AbstractRealDistribution
/**{@inheritDoc} */ /**{@inheritDoc} */
@Override @Override
public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) { public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
return new ContinuousDistribution.Sampler() { // Inversion method distribution sampler.
/** return InverseTransformContinuousSampler.of(rng, this::inverseCumulativeProbability)::sample;
* Inversion method distribution sampler.
*/
private final ContinuousSampler sampler =
new InverseTransformContinuousSampler(rng, createICPF());
/** {@inheritDoc} */
@Override
public double sample() {
return sampler.sample();
}
};
}
/**
* @return an instance for use by {@link #createSampler(UniformRandomProvider)}
*/
private ContinuousInverseCumulativeProbabilityFunction createICPF() {
return new ContinuousInverseCumulativeProbabilityFunction() {
/** {@inheritDoc} */
@Override
public double inverseCumulativeProbability(double p) {
return AbstractRealDistribution.this.inverseCumulativeProbability(p);
}
};
} }
} }