Commited patch for issue 20112 from Phil Steitz.

EmpiricalDistribution -- represents an empirical probability distribution and
supports generation of data values that are "like" values in an input file
without making any assumptions about the functional form of the probability
distribution that the data come from.   This is useful in simulation
applications where historical data about component performance are
available but do not follow standard distributions (or any application that
requires random data generation from an empirical distribution). Also
generates data for grouped frequency histograms based on the input file.

ValueServer -- a wrapper for RandomData and EmpiricalDistribution that
generates values in each of the following modes:
  * DIGEST_MODE -- uses an empirical distribution
  * REPLAY_MODE -- replays data from an input file
  * UNIFORM_MODE -- generates uniformly distributed random values
  * EXPONENTIAL_MODE -- generates exponentially distributed random
                        values
  * GAUSSIAN_MODE -- generates Gaussian distributed random values
  * CONSTANT_MODE -- returns the same value every time.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140848 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2003-05-21 14:21:15 +00:00
parent a99cbac0bb
commit 5de3587b86
7 changed files with 2098 additions and 0 deletions

View File

@ -74,6 +74,7 @@ The Math project is a library of lightweight, self-contained mathematics and sta
<directory>${pom.build.unitTestSourceDirectory}</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.txt</include>
</includes>
</resource>
</resources>

View File

@ -0,0 +1,180 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
/**
* Represents an <a href=http://random.mat.sbg.ac.at/~ste/dipl/node11.html>
* empirical probability distribution</a> -- a probability distribution derived
* from observed data without making any assumptions about the functional form
* of the population distribution that the data come from.<p>
* Implementations of this interface maintain data structures, called
* <i>distribution digests</i>, that describe empirical distributions and
* support the following operations: <ul>
* <li>loading the distribution from a file of observed data values</li>
* <li>saving and re-loading distribution digests to/from "digest files" </li>
* <li>dividing the input data into "bin ranges" and reporting bin frequency
* counts (data for histogram)</li>
* <li>reporting univariate statistics describing the full set of data values
* as well as the observations within each bin</li>
* <li>generating random values from the distribution</li>
* </ul>
* Applications can use <code>EmpiricalDistribution</code> implementations to
* build grouped frequnecy histograms representing the input data or to
* generate random values "like" those in the input file -- i.e., the values
* generated will follow the distribution of the values in the file.
* @author Phil Steitz
* @version $Revision: 1.1 $
*/
public interface EmpiricalDistribution {
/**
* Computes the empirical distribution from the input file
* @param filePath fully qualified name of a file in the local file system
* @throws IOException if an IO error occurs
*/
void load(String filePath) throws IOException;
/**
* Computes the empirical distribution from the input file
* @param URL url of the input file
* @throws IOException if an IO error occurs
*/
void load(File file) throws IOException;
/**
* Generates a random value from this distribution<p>
* <strong>Preconditions:</strong><ul>
* <li>the distribution must be loaded before invoking this method</li></ul>
* @throws IllegalStateException if the distribution has not been loaded
*/
double getNextValue() throws IllegalStateException;
/**
* <p>Returns a Univariate describing this distribution</p>
* <strong>Preconditions:</strong><ul>
* <li>the distribution must be loaded before invoking this method</li></ul>
* @throws IllegalStateException if the distribution has not been loaded
*/
Univariate getSampleStats();
/**
* Loads a saved distribution from a file.
* @param file File reference for a file containing a digested distribution
* @throws IOException if an error occurs reading the file
*/
void loadDistribution(File file) throws IOException;
/**
* Loads a saved distribution from a file.
* @param filePath fully qualified file path for a file
* containing a digested distribution
* @throws IOException if an error occurs reading the file
*/
void loadDistribution(String filePath) throws IOException;
/**
* Saves distribution to a file. Overwrites the file if it exists.
* <strong>Preconditions:</strong><ul>
* <li>the distribution must be loaded before invoking this method</li></ul>
* @param fully qualified file path for the file to be written
* @throws IOException if an error occurs reading the file
* @throws IllegalStateException if the distribution has not been loaded
*/
void saveDistribution(String filePath) throws
IOException,IllegalStateException;
/**
* Saves distribution to a file. Overwrites the file if it exists.
* <strong>Preconditions:</strong><ul>
* <li>the distribution must be loaded before invoking this method</li></ul>
* @param file File reference for the file to be written
* @throws IOException if an error occurs reading the file
* @throws IllegalStateException if the distribution has not been loaded
*/
void saveDistribution(File file) throws IOException,IllegalStateException;
/**
* property indicating whether or not the distribution has been loaded
* @return true if the distribution has been loaded
*/
boolean isLoaded();
/**
* Returns the number of bins
* @return the number of bins.
*/
int getBinCount();
/**
* Returns a list of Univariates containing statistics describing the
* values in each of the bins. The ArrayList is indexed on the bin number.
* @return ArrayList of bin statistics.
*/
ArrayList getBinStats();
/**
* Returns the array of upper bounds for the bins. Bins are: <br>
* [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],...,
* (upperBounds[binCount-1],max]
* @return array of bin upper bounds
*/
double[] getUpperBounds();
}

View File

@ -0,0 +1,279 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
import java.util.ArrayList;
import java.io.Serializable;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
/**
* Implements <code>EmpiricalDistribution</code> interface using
* what amounts to the
* <a href=http://nedwww.ipac.caltech.edu/level5/March02/Silverman/Silver2_6.html>
* Variable Kernel Method</a> with Gaussian smoothing:<p>
* <strong>Digesting the input file</strong>
* <ol><li>Pass the file once to compute min and max.</li>
* <li>Divide the range from min-max into <code>binCount</code> "bins."</li>
* <li>Pass the data file again, computing bin counts and univariate
* statistics (mean, std dev.) for each of the bins </li>
* <li>Divide the interval (0,1) into subintervals associated with the bins,
* with the length of a bin's subinterval proportional to its count.</li></ol>
* <strong>Generating random values from the distribution</strong><ol>
* <li>Generate a uniformly distributed value in (0,1) </li>
* <li>Select the subinterval to which the value belongs.
* <li>Generate a random Gaussian value with mean = mean of the associated
* bin and std dev = std dev of associated bin.</li></ol></p><p>
*<strong>USAGE NOTES:</strong><ul>
*<li>The <code>binCount</code> is set by default to 1000. A good rule of thumb
* is to set the bin count to approximately the length of the input file divided
* by 10. See TODO: add reference </li>
*<li>The input file <i>must</i> be a plain text file containing one valid numeric
* entry per line.</li>
* </ol></p>
*
* @author Phil Steitz
* @version $Revision: 1.1 $
*/
public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribution {
/** List of Univariate objects characterizing the bins */
private ArrayList binStats = null;
/** Sample statistics */
Univariate sampleStats = null;
/** number of bins */
private int binCount = 1000;
/** is the distribution loaded? */
private boolean loaded = false;
/** upper bounds of subintervals in (0,1) "belonging" to the bins */
private double[] upperBounds = null;
/**
* Creates a new EmpiricalDistribution with the default bin count
*/
public EmpiricalDistributionImpl() {
binStats = new ArrayList();
}
/**
* Creates a new EmpiricalDistribution with the specified bin count
* @param binCount number of bins
*/
public EmpiricalDistributionImpl(int binCount) {
this.binCount = binCount;
binStats = new ArrayList();
}
public void load(String filePath) throws IOException {
File file = new File(filePath);
load(file);
}
public void load(File file) throws IOException {
// Pass the file once to get sample stats
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));
String str = null;
double val = 0.0;
sampleStats = new UnivariateImpl();
while ((str = in.readLine()) != null) {
val = new Double(str).doubleValue();
sampleStats.addValue(val);
}
in.close();
in = null;
} finally {
if (in != null) try {in.close();} catch (Exception ex) {};
}
// Load array of bin upper bounds -- evenly spaced from min - max
double min = sampleStats.getMin();
double max = sampleStats.getMax();
double delta = (max - min)/(new Double(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
if (!binStats.isEmpty()) {
binStats.clear();
}
for (int i = 0; i < binCount; i++) {
Univariate stats = new UnivariateImpl();
binStats.add(i,stats);
}
// Pass the data again, filling data in binStats Array
try {
in = new BufferedReader(new FileReader(file));
String str = null;
double val = 0.0d;
while ((str = in.readLine()) != null) {
val = new Double(str).doubleValue();
// Find bin and add value to binStats for the bin
boolean found = false;
int i = 0;
while (!found) {
if (i >= binCount) {
throw new RuntimeException("bin alignment error");
}
if (val <= binUpperBounds[i]) {
found = true;
Univariate stats = (Univariate)binStats.get(i);
stats.addValue(val);
}
i++;
}
}
in.close();
in = null;
} finally {
if (in != null) try {in.close();} catch (Exception ex) {};
}
// Assign upperBounds based on bin counts
upperBounds = new double[binCount];
upperBounds[0] =
(((Univariate)binStats.get(0)).getN())/sampleStats.getN();
for (int i = 1; i < binCount-1; i++) {
upperBounds[i] = upperBounds[i-1] +
(((Univariate)binStats.get(i)).getN())/sampleStats.getN();
}
upperBounds[binCount-1] = 1.0d;
loaded = true;
}
/** Generates a random value from this distribution */
public double getNextValue() throws IllegalStateException {
if (!loaded) {
throw new IllegalStateException("distribution not loaded");
}
// Start with a uniformly distributed random number in (0,1)
double x = Math.random();
// Use this to select the bin and generate a Gaussian within the bin
RandomData rd = new RandomDataImpl();
for (int i = 0; i < binCount; i++) {
if (x <= upperBounds[i]) {
Univariate stats = (Univariate)binStats.get(i);
if (stats.getN() > 0.5) { // really mean > 0, but avoid fp error
if (stats.getStandardDeviation() > 0) { // more than one obs
return rd.nextGaussian
(stats.getMean(),stats.getStandardDeviation());
} else {
return stats.getMean(); // only one obs in bin
}
}
}
}
throw new RuntimeException("No bin selected");
}
public void loadDistribution(String filePath) throws IOException {
throw new UnsupportedOperationException("Not Implemented yet :-(");
}
public void loadDistribution(File file) throws IOException {
throw new UnsupportedOperationException("Not Implemented yet :-(");
}
public void saveDistribution(String filePath) throws
IOException,IllegalStateException {
throw new UnsupportedOperationException("Not Implemented yet :-(");
}
public void saveDistribution(File file) throws
IOException,IllegalStateException {
throw new UnsupportedOperationException("Not Implemented yet :-(");
}
public Univariate getSampleStats() {
return sampleStats;
}
public int getBinCount() {
return binCount;
}
public ArrayList getBinStats() {
return binStats;
}
public double[] getUpperBounds() {
return upperBounds;
}
public boolean isLoaded() {
return loaded;
}
}

View File

@ -0,0 +1,316 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.net.URL;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Generates values for use in simulation applications.<br>
* How values are generated is determined by the <code>mode</code>
* property. <p>
* Supported <code>mode</code> values are: <ul>
* <li> DIGEST_MODE -- uses an empirical distribution </li>
* <li> REPLAY_MODE -- replays data from <code>valuesFile</code></li>
* <li> UNIFORM_MODE -- generates uniformly distributed random values with
* mean = <code>mu</code> </li>
* <li> EXPONENTIAL_MODE -- generates exponentially distributed random values
* with mean = <code>mu</code></li>
* <li> GAUSSIAN_MODE -- generates Gaussian distributed random values with
* mean = <code>mu</code> and
* standard deviation = <code>sigma</code></li>
* <li> CONSTANT_MODE -- returns <code>mu</code> every time.</li></ul>
*
* @author Phil Steitz
* @version $Revision: 1.1 $
*
*/
public class ValueServer {
/** mode determines how values are generated */
private int mode = 5;
/** URI to raw data values */
private URL valuesFileURL = null;
/** Mean for use with non-data-driven modes */
private double mu = 0.0;
/** Standard deviation for use with GAUSSIAN_MODE */
private double sigma = 0.0;
/** Empirical probability distribution for use with DIGEST_MODE */
private EmpiricalDistribution empiricalDistribution = null;
/** file pointer for REPLAY_MODE */
private BufferedReader filePointer = null;
/** RandomDataImpl to use for random data generation */
private RandomDataImpl randomData = new RandomDataImpl();
// Data generation modes ======================================
/** Use empirical distribution */
public static final int DIGEST_MODE = 0;
/** Replay data from valuesFilePath */
public static final int REPLAY_MODE = 1;
/** Uniform random variates with mean = mu */
public static final int UNIFORM_MODE = 2;
/** Exponential random variates with mean = mu */
public static final int EXPONENTIAL_MODE = 3;
/** Gaussian random variates with mean = mu, std dev = sigma */
public static final int GAUSSIAN_MODE = 4;
/** Always return mu */
public static final int CONSTANT_MODE = 5;
/** Creates new ValueServer */
public ValueServer() {
}
/**
* Returns the next generated value, generated according
* to the mode value (see MODE constants)
*
* @return generated value
* @throws IOException in REPLAY_MODE if file I/O error occurs
*/
public double getNext() throws IOException {
switch (mode) {
case DIGEST_MODE: return getNextDigest();
case REPLAY_MODE: return getNextReplay();
case UNIFORM_MODE: return getNextUniform();
case EXPONENTIAL_MODE: return getNextExponential();
case GAUSSIAN_MODE: return getNextGaussian();
case CONSTANT_MODE: return mu;
default: throw new IllegalStateException
("Bad mode: " + mode);
}
}
/**
* Computes the empirical distribution using values from file
* in <code>valuesFilePath</code>, using the default number of bins.
* <p>
* <code>valuesFileURL</code> must exist and be
* readable by *this at runtime.
* <p>
* This method must be called before using <code>getNext()</code>
* with <code>mode = DISGEST_MODE</code>
*
* @throws IOException if an I/O error occurs reading the input file
*/
public void computeDistribution() throws IOException {
empiricalDistribution = new EmpiricalDistributionImpl();
empiricalDistribution.load(valuesFileURL.getFile());
}
/**
* Computes the empirical distribution using values from the file
* in <code>valuesFilePath</code> and <code>binCount</code> bins.
* <p>
* <code>valuesFileURL</code> must exist and be
* readable by *this at runtime.
* <p>
* This method must be called before using <code>getNext()</code>
* with <code>mode = DISGEST_MODE</code>
*
* @throws IOException if an error occurs reading the input file
*/
public void computeDistribution(int binCount)
throws IOException{
empiricalDistribution = new EmpiricalDistributionImpl(binCount);
empiricalDistribution.load(valuesFileURL.getFile());
mu = empiricalDistribution.getSampleStats().getMean();
sigma = empiricalDistribution.getSampleStats().getStandardDeviation();
}
/**
* Gets a random value in DIGEST_MODE.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li>Before this method is called, <code>computeDistribution()</code>
* must have completed successfully; otherwise an
* <code>IllegalStateException</code> will be thrown</li></ul>
*
* @return next random value form the empirical distribution digest
*/
private double getNextDigest() {
if ((empiricalDistribution == null) ||
(empiricalDistribution.getBinStats().size() == 0)) {
throw new IllegalStateException("Digest not initialized");
}
return empiricalDistribution.getNextValue();
}
/**
* Gets next sequential value from the <code>valuesFilePath</code>
* opened by <code>openReplayFile()</code>.
* <p>
* Throws an IOException if <code>filePointer</code> is null or read fails.
* Will wrap around to BOF is EOF is encountered.
* <p>
* <strong>Preconditions</strong>: <ul>
* <li> openReplayfile() must have completed successfully before
* invoking this method; otherwise an <code>IlleglaStateException</code>
* will be thrown</li></ul>
*
* @return next value from the replay file
* @throws IOException if there is a problem reading from the file
*/
private double getNextReplay() throws IOException{
String str = null;
if (filePointer == null) {
throw new IllegalStateException("replay file not open");
}
if ((str = filePointer.readLine()) == null) {
closeReplayFile();
openReplayFile();
str = filePointer.readLine();
}
return new Double(str).doubleValue();
}
/**
* Gets a uniformly distributed random value with mean = mu
*
* @return random uniform value
*/
private double getNextUniform() {
return 2.0*mu*Math.random();
}
/**
* Gets an exponentially distributed random value with mean = mu
*
* @return random exponential value
*/
private double getNextExponential() {
return randomData.nextExponential(mu);
}
/**
* Gets a Gaussian distributed random value with mean = mu
* and standard deviation = sigma
*
* @return random Gaussian value
*/
private double getNextGaussian() {
return randomData.nextGaussian(mu,sigma);
}
/** Getter for property mode.
* @return Value of property mode.
*/
public int getMode() {
return mode;
}
/** Setter for property mode.
* @param mode New value of property mode.
*/
public void setMode(int mode) {
this.mode = mode;
}
/** Getter for property valuesFilePath.
* @return Value of property valuesFilePath.
*/
public String getValuesFileURL() {
return valuesFileURL.toString();
}
/** Setter for property valuesFilePath.
* @param valuesFilePath New value of property valuesFilePath.
*/
public void setValuesFileURL(String URL) throws MalformedURLException {
this.valuesFileURL = new URL(URL);
}
/** Getter for property empiricalDistribution.
* @return Value of property empiricalDistribution.
*/
public EmpiricalDistribution getEmpiricalDistribution() {
return empiricalDistribution;
}
/**
* Opens <code>valuesFilePath</code> to use in REPLAY_MODE
*
* @throws IOException if an error occurs opening the file
*/
public void openReplayFile() throws IOException {
filePointer = new BufferedReader(new FileReader
(new File(valuesFileURL.getFile())));
}
/**
* Closes <code>valuesFilePath</code> after use in REPLAY_MODE
*
* @throws IOException if an error occurs closing the file
*/
public void closeReplayFile() throws IOException {
if (filePointer != null) {
filePointer.close();
}
}
}

View File

@ -0,0 +1,162 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import java.io.File;
import java.net.URL;
/**
* Test cases for the EmpiricalDistribution class
*
* @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/05/21 14:21:15 $
*/
public final class EmpiricalDistributionTest extends TestCase {
private EmpiricalDistribution empiricalDistribution = null;
private File file = null;
public EmpiricalDistributionTest(String name) {
super(name);
}
public void setUp() {
empiricalDistribution = new EmpiricalDistributionImpl(100);
URL url = getClass().getResource("testData.txt");
file = new File(url.getFile());
}
public static Test suite() {
TestSuite suite = new TestSuite(EmpiricalDistributionTest.class);
suite.setName("EmpiricalDistribution Tests");
return suite;
}
/**
* Test EmpiricalDistrbution.load() using sample data file.<br>
* Check that the sampleCount, mu and sigma match data in
* the sample data file.
*/
public void testLoad() throws Exception {
empiricalDistribution.load(file);
// testData File has 10000 values, with mean ~ 5.0, std dev ~ 1
// Make sure that loaded distribution matches this
assertEquals(empiricalDistribution.getSampleStats().getN(),1000,10E-7);
//TODO: replace with statistical tests
assertEquals
(empiricalDistribution.getSampleStats().getMean(),
5.069831575018909,10E-7);
assertEquals
(empiricalDistribution.getSampleStats().getStandardDeviation(),
1.0173699343977738,10E-7);
}
/**
* Generate 1000 random values and make sure they look OK.<br>
* Note that there is a non-zero (but very small) probability that
* these tests will fail even if the code is working as designed.
*/
public void testNext() throws Exception {
tstGen(0.1);
}
/**
* Make sure exception thrown if digest getNext is attempted
* before loading empiricalDistribution.
*/
public void testNexFail() {
try {
empiricalDistribution.getNextValue();
fail("Expecting IllegalStateException");
} catch (IllegalStateException ex) {;}
}
/**
* Make sure we can handle a grid size that is too fine
*/
public void testGridTooFine() throws Exception {
empiricalDistribution = new EmpiricalDistributionImpl(10000);
tstGen(0.1);
}
/**
* How about too fat?
*/
public void testGridTooFat() throws Exception {
empiricalDistribution = new EmpiricalDistributionImpl(1);
tstGen(5); // ridiculous tolerance; but ridiculous grid size
// really just checking to make sure we do not bomb
}
private void tstGen(double tolerance)throws Exception {
empiricalDistribution.load(file);
Univariate stats = new UnivariateImpl();
for (int i = 1; i < 1000; i++) {
stats.addValue(empiricalDistribution.getNextValue());
}
//TODO: replace these with statistical tests -- refactor as necessary
assertEquals("mean", stats.getMean(),5.069831575018909,tolerance);
assertEquals
("std dev", stats.getStandardDeviation(),1.0173699343977738,tolerance);
}
}

View File

@ -0,0 +1,160 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import java.net.URL;
/**
* Test cases for the ValueServer class.
*
* @author Phil Steitz
* @version $Revision: 1.1 $
*/
public final class ValueServerTest extends TestCase {
private ValueServer vs = new ValueServer();
public ValueServerTest(String name) {
super(name);
}
public void setUp() {
vs.setMode(ValueServer.DIGEST_MODE);
try {
URL url = getClass().getResource("testData.txt");
vs.setValuesFileURL(url.toExternalForm());
} catch (Exception ex) {
fail("malformed test URL");
}
}
public static Test suite() {
TestSuite suite = new TestSuite(ValueServerTest.class);
suite.setName("ValueServer Tests");
return suite;
}
/**
* Generate 1000 random values and make sure they look OK.<br>
* Note that there is a non-zero (but very small) probability that
* these tests will fail even if the code is working as designed.
*/
public void testNextDigest() throws Exception{
double next = 0.0;
double tolerance = 0.1;
vs.computeDistribution();
Univariate stats = new UnivariateImpl();
for (int i = 1; i < 1000; i++) {
next = vs.getNext();
stats.addValue(next);
}
assertEquals("mean", stats.getMean(),5.069831575018909,tolerance);
assertEquals
("std dev", stats.getStandardDeviation(),1.0173699343977738,tolerance);
}
/**
* Make sure exception thrown if digest getNext is attempted
* before loading empiricalDistribution.
*/
public void testNextDigestFail() throws Exception {
try {
vs.getNext();
fail("Expecting IllegalStateException");
} catch (IllegalStateException ex) {;}
}
/**
* Make sure exception thrown if nextReplay() is attempted
* before opening replay file.
*/
public void testNextReplayFail() throws Exception {
try {
vs.setMode(ValueServer.REPLAY_MODE);
vs.getNext();
fail("Expecting IllegalStateException");
} catch (IllegalStateException ex) {;}
}
/**
* Test ValueServer REPLAY_MODE using values in testData file.<br>
* Check that the values 1,2,1001,1002 match data file values 1 and 2.
* the sample data file.
*/
public void testReplay() throws Exception {
double firstDataValue = 4.038625496201205;
double secondDataValue = 3.6485326248346936;
double tolerance = 10E-15;
double compareValue = 0.0d;
vs.setMode(ValueServer.REPLAY_MODE);
vs.openReplayFile();
compareValue = vs.getNext();
assertEquals(compareValue,firstDataValue,tolerance);
compareValue = vs.getNext();
assertEquals(compareValue,secondDataValue,tolerance);
for (int i = 3; i < 1001; i++) {
compareValue = vs.getNext();
}
compareValue = vs.getNext();
assertEquals(compareValue,firstDataValue,tolerance);
compareValue = vs.getNext();
assertEquals(compareValue,secondDataValue,tolerance);
}
}

File diff suppressed because it is too large Load Diff