Commit change inadvertenly omitted from r817128. JIRA: MATH-298

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@817132 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2009-09-21 02:13:39 +00:00
parent e9771d7eb5
commit 97fb25eb35
1 changed files with 49 additions and 29 deletions

View File

@ -69,6 +69,15 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
/** Sample statistics */ /** Sample statistics */
private SummaryStatistics sampleStats = null; private SummaryStatistics sampleStats = null;
/** Max loaded value */
private double max = Double.NEGATIVE_INFINITY;
/** Min loaded value */
private double min = Double.POSITIVE_INFINITY;
/** Grid size */
private double delta = 0d;
/** number of bins */ /** number of bins */
private int binCount = 1000; private int binCount = 1000;
@ -195,11 +204,9 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
/** /**
* Compute bin stats. * Compute bin stats.
* *
* @param min minimum value
* @param delta grid size
* @throws Exception if an error occurs computing bin stats * @throws Exception if an error occurs computing bin stats
*/ */
public abstract void computeBinStats(double min, double delta) public abstract void computeBinStats()
throws Exception; throws Exception;
/** /**
* Compute sample statistics. * Compute sample statistics.
@ -256,18 +263,16 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
/** /**
* Computes binStats * Computes binStats
* *
* @param min minimum value
* @param delta grid size
* @throws IOException if an IO error occurs * @throws IOException if an IO error occurs
*/ */
@Override @Override
public void computeBinStats(double min, double delta) public void computeBinStats()
throws IOException { throws IOException {
String str = null; String str = null;
double val = 0.0d; double val = 0.0d;
while ((str = inputStream.readLine()) != null) { while ((str = inputStream.readLine()) != null) {
val = Double.parseDouble(str); val = Double.parseDouble(str);
SummaryStatistics stats = binStats.get(findBin(min, val, delta)); SummaryStatistics stats = binStats.get(findBin(val));
stats.addValue(val); stats.addValue(val);
} }
@ -296,7 +301,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
/** /**
* <code>DataAdapter</code> for data provided as array of doubles. * <code>DataAdapter</code> for data provided as array of doubles.
*/ */
private class ArrayDataAdapter extends DataAdapter{ private class ArrayDataAdapter extends DataAdapter {
/** Array of input data values */ /** Array of input data values */
private double[] inputArray; private double[] inputArray;
@ -325,16 +330,14 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
/** /**
* Computes binStats * Computes binStats
* *
* @param min minimum value
* @param delta grid size
* @throws IOException if an IO error occurs * @throws IOException if an IO error occurs
*/ */
@Override @Override
public void computeBinStats(double min, double delta) public void computeBinStats()
throws IOException { throws IOException {
for (int i = 0; i < inputArray.length; i++) { for (int i = 0; i < inputArray.length; i++) {
SummaryStatistics stats = SummaryStatistics stats =
binStats.get(findBin(min, inputArray[i], delta)); binStats.get(findBin(inputArray[i]));
stats.addValue(inputArray[i]); stats.addValue(inputArray[i]);
} }
} }
@ -347,16 +350,10 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
* @throws IOException if an IO error occurs * @throws IOException if an IO error occurs
*/ */
private void fillBinStats(Object in) throws IOException { private void fillBinStats(Object in) throws IOException {
// Load array of bin upper bounds -- evenly spaced from min - max // Set up grid
double min = sampleStats.getMin(); min = sampleStats.getMin();
double max = sampleStats.getMax(); max = sampleStats.getMax();
double delta = (max - min)/(Double.valueOf(binCount)).doubleValue(); delta = (max - min)/(Double.valueOf(binCount)).doubleValue();
double[] binUpperBounds = new double[binCount];
binUpperBounds[0] = min + delta;
for (int i = 1; i< binCount - 1; i++) {
binUpperBounds[i] = binUpperBounds[i-1] + delta;
}
binUpperBounds[binCount -1] = max;
// Initialize binStats ArrayList // Initialize binStats ArrayList
if (!binStats.isEmpty()) { if (!binStats.isEmpty()) {
@ -371,7 +368,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
DataAdapterFactory aFactory = new DataAdapterFactory(); DataAdapterFactory aFactory = new DataAdapterFactory();
DataAdapter da = aFactory.getAdapter(in); DataAdapter da = aFactory.getAdapter(in);
try { try {
da.computeBinStats(min, delta); da.computeBinStats();
} catch (IOException ioe) { } catch (IOException ioe) {
// don't wrap exceptions which are already IOException // don't wrap exceptions which are already IOException
throw ioe; throw ioe;
@ -396,12 +393,10 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
/** /**
* Returns the index of the bin to which the given value belongs * Returns the index of the bin to which the given value belongs
* *
* @param min the minimum value
* @param value the value whose bin we are trying to find * @param value the value whose bin we are trying to find
* @param delta the grid size
* @return the index of the bin containing the value * @return the index of the bin containing the value
*/ */
private int findBin(double min, double value, double delta) { private int findBin(double value) {
return Math.min( return Math.min(
Math.max((int) Math.ceil((value- min) / delta) - 1, 0), Math.max((int) Math.ceil((value- min) / delta) - 1, 0),
binCount - 1); binCount - 1);
@ -472,14 +467,39 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
} }
/** /**
* Returns (a fresh copy of) the array of upper bounds for the bins. * <p>Returns a fresh copy of the array of upper bounds for the bins.
Bins are: <br/> * Bins are: <br/>
* [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],..., * [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],...,
* (upperBounds[binCount-1],max] * (upperBounds[binCount-2], upperBounds[binCount-1] = max].</p>
*
* <p>Note: In versions 1.0-2.0 of commons-math, this method
* incorrectly returned the array of probability generator upper
* bounds now returned by {@link #getGeneratorUpperBounds()}.</p>
* *
* @return array of bin upper bounds * @return array of bin upper bounds
*/ */
public double[] getUpperBounds() { public double[] getUpperBounds() {
double[] binUpperBounds = new double[binCount];
binUpperBounds[0] = min + delta;
for (int i = 1; i < binCount - 1; i++) {
binUpperBounds[i] = binUpperBounds[i-1] + delta;
}
binUpperBounds[binCount - 1] = max;
return binUpperBounds;
}
/**
* <p>Returns a fresh copy of the array of upper bounds of the subintervals
* of [0,1] used in generating data from the empirical distribution.
* Subintervals correspond to bins with lengths proportional to bin counts.</p>
*
* <p>In versions 1.0-2.0 of commons-math, this array was (incorrectly) returned
* by {@link #getUpperBounds()}.</p>
*
* @since 2.1
* @return array of upper bounds of subintervals used in data generation
*/
public double[] getGeneratorUpperBounds() {
int len = upperBounds.length; int len = upperBounds.length;
double[] out = new double[len]; double[] out = new double[len];
System.arraycopy(upperBounds, 0, out, 0, len); System.arraycopy(upperBounds, 0, out, 0, len);