Restore backwards-compatibility (for release 3.1).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1421910 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2012-12-14 14:57:56 +00:00
parent 7521e5af34
commit 29a297de44
3 changed files with 114 additions and 56 deletions

View File

@ -48,7 +48,7 @@ implements RealDistribution, Serializable {
* {@link #random} instance variable instead.
*/
@Deprecated
protected final RandomDataImpl randomData = new RandomDataImpl();
protected RandomDataImpl randomData = new RandomDataImpl();
/** RNG instance used to generate samples from the distribution. */
protected final RandomGenerator random;
/** Solver absolute accuracy for inverse cumulative computation */

View File

@ -29,8 +29,8 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.math3.distribution.AbstractRealDistribution;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.exception.MathIllegalStateException;
import org.apache.commons.math3.exception.MathInternalError;
import org.apache.commons.math3.exception.NullArgumentException;
@ -134,14 +134,18 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
/** upper bounds of subintervals in (0,1) "belonging" to the bins */
private double[] upperBounds = null;
/** RandomDataImpl instance to use in repeated calls to getNext() */
private final RandomDataGenerator randomData;
/** Data generator. */
private final RandomDataGenerator randomDataGen;
/**
* XXX Enable backward-compatibility (to be removed in 4.0).
*/
private final boolean useRandomDataImpl;
/**
* Creates a new EmpiricalDistribution with the default bin count.
*/
public EmpiricalDistribution() {
this(DEFAULT_BIN_COUNT, new RandomDataGenerator());
this(DEFAULT_BIN_COUNT);
}
/**
@ -150,7 +154,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
* @param binCount number of bins
*/
public EmpiricalDistribution(int binCount) {
this(binCount, new RandomDataGenerator());
this(binCount, (RandomGenerator) null);
}
/**
@ -158,21 +162,75 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
* provided {@link RandomGenerator} as the source of random data.
*
* @param binCount number of bins
* @param generator random data generator (may be null, resulting in default JDK generator)
* @param randomData random data generator (may be null, resulting in a default generator)
* @deprecated As of 3.1. To be removed in 4.0. Please use
* {@link #EmpiricalDistribution(int,RandomDataGenerator)} instead.
*/
@Deprecated
public EmpiricalDistribution(int binCount, RandomDataImpl randomData) {
this.binCount = binCount;
this.randomData = randomData == null ?
new RandomDataImpl() :
randomData;
binStats = new ArrayList<SummaryStatistics>();
useRandomDataImpl = true;
randomDataGen = null;
}
/**
* Creates a new EmpiricalDistribution with the specified bin count using the
* provided {@link RandomGenerator} as the source of random data.
*
* @param randomData random data generator (may be null, resulting in a default generator)
* @deprecated As of 3.1. To be removed in 4.0. Please use
* {@link #EmpiricalDistribution(RandomDataGenerator)} instead.
*/
@Deprecated
public EmpiricalDistribution(RandomDataImpl randomData) {
this(DEFAULT_BIN_COUNT, randomData);
}
/**
* Creates a new EmpiricalDistribution with the specified bin count using the
* provided {@link RandomGenerator} as the source of random data.
*
* @param binCount number of bins
* @param randomData random data generator (may be null, resulting in a default generator)
*/
public EmpiricalDistribution(int binCount, RandomDataGenerator randomData) {
this.binCount = binCount;
this.randomDataGen = randomData == null ?
new RandomDataGenerator() :
randomData;
binStats = new ArrayList<SummaryStatistics>();
useRandomDataImpl = false; // XXX Remove in 4.0
}
/**
* Creates a new EmpiricalDistribution with the specified bin count using the
* provided {@link RandomGenerator} as the source of random data.
*
* @param randomData random data generator (may be null, resulting in a default generator)
*/
public EmpiricalDistribution(RandomDataGenerator randomData) {
this(DEFAULT_BIN_COUNT, randomData);
}
/**
* Creates a new EmpiricalDistribution with the specified bin count using the
* provided {@link RandomGenerator} as the source of random data.
*
* @param binCount number of bins
* @param generator random data generator (may be null, resulting in a default generator)
* @since 3.0
*/
public EmpiricalDistribution(int binCount, RandomGenerator generator) {
super(generator);
this.binCount = binCount;
this.randomData = new RandomDataGenerator(generator);
this.binStats = new ArrayList<SummaryStatistics>();
this(binCount, new RandomDataGenerator(generator));
}
/**
* Creates a new EmpiricalDistribution with default bin count using the
* provided {@link RandomGenerator} as the source of random data.
*
* @param generator random data generator (may be null, resulting in default JDK generator)
* @param generator random data generator (may be null, resulting in default generator)
* @since 3.0
*/
public EmpiricalDistribution(RandomGenerator generator) {
@ -180,32 +238,6 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
}
/**
* Creates a new EmpiricalDistribution with the specified bin count using the
* provided {@link RandomDataImpl} instance as the source of random data.
*
* @param binCount number of bins
* @param randomData random data generator (may be null, resulting in default JDK generator)
* @since 3.0
*/
@SuppressWarnings("deprecation") // Superclass argumentless constructor is deprecated
public EmpiricalDistribution(int binCount, RandomDataGenerator randomData) {
this.binCount = binCount;
this.randomData = randomData;
binStats = new ArrayList<SummaryStatistics>();
}
/**
* Creates a new EmpiricalDistribution with default bin count using the
* provided {@link RandomDataImpl} as the source of random data.
*
* @param randomData random data generator (may be null, resulting in default JDK generator)
* @since 3.0
*/
public EmpiricalDistribution(RandomDataGenerator randomData) {
this(DEFAULT_BIN_COUNT, randomData);
}
/**
* Computes the empirical distribution from the provided
* array of numbers.
*
@ -465,22 +497,41 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED);
}
// Start with a uniformly distributed random number in (0,1)
final double x = randomData.nextUniform(0,1);
// Use this to select the bin and generate a Gaussian within the bin
for (int i = 0; i < binCount; i++) {
if (x <= upperBounds[i]) {
SummaryStatistics stats = binStats.get(i);
if (stats.getN() > 0) {
if (stats.getStandardDeviation() > 0) { // more than one obs
return randomData.nextGaussian(stats.getMean(),
stats.getStandardDeviation());
} else {
return stats.getMean(); // only one obs in bin
}
}
}
if (useRandomDataImpl) {
// XXX backward compatibility.
// Start with a uniformly distributed random number in (0, 1)
final double x = randomData.nextUniform(0,1);
// Use this to select the bin and generate a Gaussian within the bin
for (int i = 0; i < binCount; i++) {
if (x <= upperBounds[i]) {
SummaryStatistics stats = binStats.get(i);
if (stats.getN() > 0) {
if (stats.getStandardDeviation() > 0) { // more than one obs
return randomData.nextGaussian(stats.getMean(),
stats.getStandardDeviation());
} else {
return stats.getMean(); // only one obs in bin
}
}
}
}
} else {
// Start with a uniformly distributed random number in (0, 1)
final double x = randomDataGen.nextUniform(0, 1);
// Use this to select the bin and generate a Gaussian within the bin
for (int i = 0; i < binCount; i++) {
if (x <= upperBounds[i]) {
SummaryStatistics stats = binStats.get(i);
if (stats.getN() > 0) {
if (stats.getStandardDeviation() > 0) { // more than one obs
return randomDataGen.nextGaussian(stats.getMean(),
stats.getStandardDeviation());
} else {
return stats.getMean(); // only one obs in bin
}
}
}
}
}
throw new MathIllegalStateException(LocalizedFormats.NO_BIN_SELECTED);
}
@ -573,7 +624,12 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
* @since 3.0
*/
public void reSeed(long seed) {
randomData.reSeed(seed);
if (useRandomDataImpl) {
// XXX backward compatibility.
randomData.reSeed(seed);
} else {
randomDataGen.reSeed(seed);
}
}
// Distribution methods ---------------------------
@ -763,7 +819,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
*/
@Override
public void reseedRandomGenerator(long seed) {
randomData.reSeed(seed);
reSeed(seed);
}
/**

View File

@ -55,10 +55,12 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
public void setUp() {
super.setUp();
empiricalDistribution = new EmpiricalDistribution(100);
// empiricalDistribution = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API
url = getClass().getResource("testData.txt");
final ArrayList<Double> list = new ArrayList<Double>();
try {
empiricalDistribution2 = new EmpiricalDistribution(100);
// empiricalDistribution2 = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API
BufferedReader in =
new BufferedReader(new InputStreamReader(
url.openStream()));