On 11/10/15 9:13 AM, Gilles wrote: > Hi Phil, > > On Tue, 10 Nov 2015 03:48:33 +0000 (UTC), pste...@apache.org wrote: >> Repository: commons-math >> Updated Branches: >> refs/heads/MATH_3_X 8aecb842d -> 430c7f456 >> >> >> Added constructors taking sample data as arguments to enumerated >> real >> and integer distributions. JIRA: MATH-1287. >> >> >> [...] >> >> >> http://git-wip-us.apache.org/repos/asf/commons-math/blob/430c7f45/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java >> >> >> ---------------------------------------------------------------------- >> >> diff --git >> >> a/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java >> >> >> b/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java >> >> index 07b96bc..2edb375 100644 >> --- >> >> a/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java >> >> +++ >> >> b/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java >> >> @@ -17,7 +17,10 @@ >> package org.apache.commons.math3.distribution; >> >> import java.util.ArrayList; >> +import java.util.HashMap; >> import java.util.List; >> +import java.util.Map; >> +import java.util.Map.Entry; >> >> import >> org.apache.commons.math3.exception.DimensionMismatchException; >> import org.apache.commons.math3.exception.MathArithmeticException; >> @@ -51,7 +54,7 @@ public class EnumeratedRealDistribution extends >> AbstractRealDistribution { >> protected final EnumeratedDistribution<Double> >> innerDistribution; >> >> /** >> - * Create a discrete distribution using the given probability >> mass function >> + * Create a discrete real-valued distribution using the given >> probability mass function >> * enumeration. >> * <p> >> * <b>Note:</b> this constructor will implicitly create an >> instance of >> @@ -77,7 +80,7 @@ public class EnumeratedRealDistribution extends >> AbstractRealDistribution { >> } >> >> /** >> - * Create a discrete distribution using the given random number >> generator >> + * Create a discrete real-valued distribution using the given >> random number generator >> * and probability mass function enumeration. >> * >> * @param rng random number generator. >> @@ -95,17 +98,73 @@ public class EnumeratedRealDistribution extends >> AbstractRealDistribution { >> throws DimensionMismatchException, NotPositiveException, >> MathArithmeticException, >> NotFiniteNumberException, NotANumberException { >> super(rng); >> + >> + innerDistribution = new EnumeratedDistribution<Double>( >> + rng, createDistribution(singletons, >> probabilities)); >> + } >> + >> + /** >> + * Create a discrete real-valued distribution from the input >> data. Values are assigned >> + * mass based on their frequency. >> + * >> + * @param rng random number generator used for sampling >> + * @param data input dataset >> + */ >> + public EnumeratedRealDistribution(final RandomGenerator rng, >> final double[] data) { >> + super(rng); >> + final Map<Double, Integer> dataMap = new HashMap<Double, >> Integer>(); >> + >> + for (double value : data) { >> + Integer count = dataMap.get(value); >> + if (count == null) { >> + count = new Integer(1); >> + } else { >> + count = new Integer(count.intValue() + 1); >> + } >> + dataMap.put(value, count); >> + } > > > I'd suggest that the code in the above loop be written in the > following way: > > Integer count = dataMap.get(value); > if (count == null) { > count = 0; > } > dataMap.put(value, ++count); > > I think that it is nicer-looking, but it should also be more > efficient > than using the > new Integer(i) > construct for -128 < i < 127.
Thanks for review. Fine by me to change. Phil > > Gilles > >> [...] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org