Added method that was missing in the original commit: "getDimensions()",
renamed to "getDimension()".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1400017 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2012-10-19 09:55:01 +00:00
parent a355d8c20a
commit bdd5b45cf7
4 changed files with 22 additions and 16 deletions

View File

@ -31,7 +31,7 @@ public abstract class AbstractMultivariateRealDistribution
/** RNG instance used to generate samples from the distribution. */ /** RNG instance used to generate samples from the distribution. */
protected final RandomGenerator random; protected final RandomGenerator random;
/** The number of dimensions or columns in the multivariate distribution. */ /** The number of dimensions or columns in the multivariate distribution. */
private final int numDimensions; private final int dimension;
/** /**
* @param rng Random number generator. * @param rng Random number generator.
@ -40,7 +40,7 @@ public abstract class AbstractMultivariateRealDistribution
protected AbstractMultivariateRealDistribution(RandomGenerator rng, protected AbstractMultivariateRealDistribution(RandomGenerator rng,
int n) { int n) {
random = rng; random = rng;
numDimensions = n; dimension = n;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -48,14 +48,9 @@ public abstract class AbstractMultivariateRealDistribution
random.setSeed(seed); random.setSeed(seed);
} }
/** /** {@inheritDoc} */
* Gets the number of dimensions (i.e. the number of random variables) of public int getDimension() {
* the distribution. return dimension;
*
* @return the number of dimensions.
*/
public int getDimensions() {
return numDimensions;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -67,7 +62,7 @@ public abstract class AbstractMultivariateRealDistribution
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
sampleSize); sampleSize);
} }
final double[][] out = new double[sampleSize][numDimensions]; final double[][] out = new double[sampleSize][dimension];
for (int i = 0; i < sampleSize; i++) { for (int i = 0; i < sampleSize; i++) {
out[i] = sample(); out[i] = sample();
} }

View File

@ -175,7 +175,7 @@ public class MultivariateNormalDistribution
/** {@inheritDoc} */ /** {@inheritDoc} */
public double density(final double[] vals) throws DimensionMismatchException { public double density(final double[] vals) throws DimensionMismatchException {
final int dim = getDimensions(); final int dim = getDimension();
if (vals.length != dim) { if (vals.length != dim) {
throw new DimensionMismatchException(vals.length, dim); throw new DimensionMismatchException(vals.length, dim);
} }
@ -192,7 +192,7 @@ public class MultivariateNormalDistribution
* @return the standard deviations. * @return the standard deviations.
*/ */
public double[] getStandardDeviations() { public double[] getStandardDeviations() {
final int dim = getDimensions(); final int dim = getDimension();
final double[] std = new double[dim]; final double[] std = new double[dim];
final double[][] s = covarianceMatrix.getData(); final double[][] s = covarianceMatrix.getData();
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {
@ -203,7 +203,7 @@ public class MultivariateNormalDistribution
/** {@inheritDoc} */ /** {@inheritDoc} */
public double[] sample() { public double[] sample() {
final int dim = getDimensions(); final int dim = getDimension();
final double[] normalVals = new double[dim]; final double[] normalVals = new double[dim];
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {

View File

@ -49,6 +49,15 @@ public interface MultivariateRealDistribution {
*/ */
void reseedRandomGenerator(long seed); void reseedRandomGenerator(long seed);
/**
* Gets the number of random variables of the distribution.
* It is the size of the array returned by the {@link #sample() sample}
* method.
*
* @return the number of variables.
*/
int getDimension();
/** /**
* Generates a random value vector sampled from this distribution. * Generates a random value vector sampled from this distribution.
* *
@ -63,6 +72,8 @@ public interface MultivariateRealDistribution {
* @return an array representing the random samples. * @return an array representing the random samples.
* @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
* if {@code sampleSize} is not positive. * if {@code sampleSize} is not positive.
*
* @see #sample()
*/ */
double[][] sample(int sampleSize) throws NotStrictlyPositiveException; double[][] sample(int sampleSize) throws NotStrictlyPositiveException;
} }

View File

@ -56,7 +56,7 @@ public class MultivariateNormalDistributionTest {
final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
final RealMatrix s = d.getCovariances(); final RealMatrix s = d.getCovariances();
final int dim = d.getDimensions(); final int dim = d.getDimension();
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) { for (int j = 0; j < dim; j++) {
Assert.assertEquals(sigma[i][j], s.getEntry(i, j), 0); Assert.assertEquals(sigma[i][j], s.getEntry(i, j), 0);
@ -78,7 +78,7 @@ public class MultivariateNormalDistributionTest {
final int n = 500000; final int n = 500000;
final double[][] samples = d.sample(n); final double[][] samples = d.sample(n);
final int dim = d.getDimensions(); final int dim = d.getDimension();
final double[] sampleMeans = new double[dim]; final double[] sampleMeans = new double[dim];
for (int i = 0; i < samples.length; i++) { for (int i = 0; i < samples.length; i++) {