Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/commons-math
This commit is contained in:
commit
c1c61ce70d
|
@ -54,6 +54,13 @@ If the output is not quite correct, check for invisible trailing spaces!
|
|||
</release>
|
||||
|
||||
<release version="4.0" date="XXXX-XX-XX" description="">
|
||||
<action dev="psteitz" type="fix" issue="MATH-1203">
|
||||
EmpiricalDistribution getKernel fails for buckets with only multiple instances of the same value.
|
||||
</action>
|
||||
<action dev="tn" type="update" issue="MATH-869">
|
||||
"SpearmansCorrelation" will now throw an "MathIllegalArgumentException"
|
||||
if provided with a "NaturalRanking" instance that uses "REMOVED" as "NaNStrategy".
|
||||
</action>
|
||||
<action dev="tn" type="update" issue="MATH-869">
|
||||
"NullArgumentException" extends now "java.lang.NullPointerException"
|
||||
instead of "MathIllegalArgumentException".
|
||||
|
|
|
@ -241,6 +241,7 @@ public enum LocalizedFormats implements Localizable {
|
|||
NOT_STRICTLY_INCREASING_SEQUENCE("points {3} and {2} are not strictly increasing ({1} >= {0})"), /* keep */
|
||||
NOT_SUBTRACTION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not subtraction compatible"),
|
||||
NOT_SUPPORTED_IN_DIMENSION_N("method not supported in dimension {0}"),
|
||||
NOT_SUPPORTED_NAN_STRATEGY("NaN strategy {0} not supported"),
|
||||
NOT_SYMMETRIC_MATRIX("not symmetric matrix"),
|
||||
NON_SYMMETRIC_MATRIX("non symmetric matrix: the difference between entries at ({0},{1}) and ({1},{0}) is larger than {2}"), /* keep */
|
||||
NO_BIN_SELECTED("no bin selected"),
|
||||
|
|
|
@ -33,12 +33,12 @@ public class DefaultMeasurementModel implements MeasurementModel {
|
|||
* The measurement matrix, used to associate the measurement vector to the
|
||||
* internal state estimation vector.
|
||||
*/
|
||||
private RealMatrix measurementMatrix;
|
||||
private final RealMatrix measurementMatrix;
|
||||
|
||||
/**
|
||||
* The measurement noise covariance matrix.
|
||||
*/
|
||||
private RealMatrix measurementNoise;
|
||||
private final RealMatrix measurementNoise;
|
||||
|
||||
/**
|
||||
* Create a new {@link MeasurementModel}, taking double arrays as input parameters for the
|
||||
|
@ -73,11 +73,13 @@ public class DefaultMeasurementModel implements MeasurementModel {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealMatrix getMeasurementMatrix() {
|
||||
return measurementMatrix;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealMatrix getMeasurementNoise() {
|
||||
return measurementNoise;
|
||||
}
|
||||
|
|
|
@ -33,21 +33,21 @@ public class DefaultProcessModel implements ProcessModel {
|
|||
/**
|
||||
* The state transition matrix, used to advance the internal state estimation each time-step.
|
||||
*/
|
||||
private RealMatrix stateTransitionMatrix;
|
||||
private final RealMatrix stateTransitionMatrix;
|
||||
|
||||
/**
|
||||
* The control matrix, used to integrate a control input into the state estimation.
|
||||
*/
|
||||
private RealMatrix controlMatrix;
|
||||
private final RealMatrix controlMatrix;
|
||||
|
||||
/** The process noise covariance matrix. */
|
||||
private RealMatrix processNoiseCovMatrix;
|
||||
private final RealMatrix processNoiseCovMatrix;
|
||||
|
||||
/** The initial state estimation of the observed process. */
|
||||
private RealVector initialStateEstimateVector;
|
||||
private final RealVector initialStateEstimateVector;
|
||||
|
||||
/** The initial error covariance matrix of the observed process. */
|
||||
private RealMatrix initialErrorCovMatrix;
|
||||
private final RealMatrix initialErrorCovMatrix;
|
||||
|
||||
/**
|
||||
* Create a new {@link ProcessModel}, taking double arrays as input parameters.
|
||||
|
@ -139,26 +139,31 @@ public class DefaultProcessModel implements ProcessModel {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealMatrix getStateTransitionMatrix() {
|
||||
return stateTransitionMatrix;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealMatrix getControlMatrix() {
|
||||
return controlMatrix;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealMatrix getProcessNoise() {
|
||||
return processNoiseCovMatrix;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealVector getInitialStateEstimate() {
|
||||
return initialStateEstimateVector;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealMatrix getInitialErrorCovariance() {
|
||||
return initialErrorCovMatrix;
|
||||
}
|
||||
|
|
|
@ -509,6 +509,7 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
* @throws NullArgumentException if the {@link BigFraction} is {@code null}.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction add(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
|
@ -596,6 +597,7 @@ public class BigFraction
|
|||
* than {@code object}, 0 if they are equal.
|
||||
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(final BigFraction object) {
|
||||
BigInteger nOd = numerator.multiply(object.denominator);
|
||||
BigInteger dOn = denominator.multiply(object.numerator);
|
||||
|
@ -662,6 +664,7 @@ public class BigFraction
|
|||
* @throws NullArgumentException if the {@code fraction} is {@code null}.
|
||||
* @throws MathArithmeticException if the fraction to divide by is zero
|
||||
*/
|
||||
@Override
|
||||
public BigFraction divide(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
|
@ -883,6 +886,7 @@ public class BigFraction
|
|||
* the {@code int} to multiply by.
|
||||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction multiply(final int i) {
|
||||
return multiply(BigInteger.valueOf(i));
|
||||
}
|
||||
|
@ -911,6 +915,7 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values.
|
||||
* @throws NullArgumentException if {@code fraction} is {@code null}.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction multiply(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
|
@ -931,6 +936,7 @@ public class BigFraction
|
|||
*
|
||||
* @return the negation of this fraction.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction negate() {
|
||||
return new BigFraction(numerator.negate(), denominator);
|
||||
}
|
||||
|
@ -1026,6 +1032,7 @@ public class BigFraction
|
|||
*
|
||||
* @return the reciprocal fraction.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction reciprocal() {
|
||||
return new BigFraction(denominator, numerator);
|
||||
}
|
||||
|
@ -1096,6 +1103,7 @@ public class BigFraction
|
|||
* @return a {@link BigFraction} instance with the resulting values
|
||||
* @throws NullArgumentException if the {@code fraction} is {@code null}.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction subtract(final BigFraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
|
@ -1140,6 +1148,7 @@ public class BigFraction
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public BigFractionField getField() {
|
||||
return BigFractionField.getInstance();
|
||||
}
|
||||
|
|
|
@ -48,16 +48,19 @@ public class BigFractionField implements Field<BigFraction>, Serializable {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public BigFraction getOne() {
|
||||
return BigFraction.ONE;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public BigFraction getZero() {
|
||||
return BigFraction.ZERO;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Class<? extends FieldElement<BigFraction>> getRuntimeClass() {
|
||||
return BigFraction.class;
|
||||
}
|
||||
|
|
|
@ -310,6 +310,7 @@ public class Fraction
|
|||
* @return -1 if this is less than {@code object}, +1 if this is greater
|
||||
* than {@code object}, 0 if they are equal.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Fraction object) {
|
||||
long nOd = ((long) numerator) * object.denominator;
|
||||
long dOn = ((long) denominator) * object.numerator;
|
||||
|
@ -409,6 +410,7 @@ public class Fraction
|
|||
* Return the additive inverse of this fraction.
|
||||
* @return the negation of this fraction.
|
||||
*/
|
||||
@Override
|
||||
public Fraction negate() {
|
||||
if (numerator==Integer.MIN_VALUE) {
|
||||
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator);
|
||||
|
@ -420,6 +422,7 @@ public class Fraction
|
|||
* Return the multiplicative inverse of this fraction.
|
||||
* @return the reciprocal fraction
|
||||
*/
|
||||
@Override
|
||||
public Fraction reciprocal() {
|
||||
return new Fraction(denominator, numerator);
|
||||
}
|
||||
|
@ -434,6 +437,7 @@ public class Fraction
|
|||
* @throws MathArithmeticException if the resulting numerator or denominator exceeds
|
||||
* {@code Integer.MAX_VALUE}
|
||||
*/
|
||||
@Override
|
||||
public Fraction add(Fraction fraction) {
|
||||
return addSub(fraction, true /* add */);
|
||||
}
|
||||
|
@ -457,6 +461,7 @@ public class Fraction
|
|||
* @throws MathArithmeticException if the resulting numerator or denominator
|
||||
* cannot be represented in an {@code int}.
|
||||
*/
|
||||
@Override
|
||||
public Fraction subtract(Fraction fraction) {
|
||||
return addSub(fraction, false /* subtract */);
|
||||
}
|
||||
|
@ -537,6 +542,7 @@ public class Fraction
|
|||
* @throws MathArithmeticException if the resulting numerator or denominator exceeds
|
||||
* {@code Integer.MAX_VALUE}
|
||||
*/
|
||||
@Override
|
||||
public Fraction multiply(Fraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
|
@ -558,6 +564,7 @@ public class Fraction
|
|||
* @param i the {@code integer} to multiply by.
|
||||
* @return this * i
|
||||
*/
|
||||
@Override
|
||||
public Fraction multiply(final int i) {
|
||||
return new Fraction(numerator * i, denominator);
|
||||
}
|
||||
|
@ -572,6 +579,7 @@ public class Fraction
|
|||
* @throws MathArithmeticException if the resulting numerator or denominator exceeds
|
||||
* {@code Integer.MAX_VALUE}
|
||||
*/
|
||||
@Override
|
||||
public Fraction divide(Fraction fraction) {
|
||||
if (fraction == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||
|
@ -666,6 +674,7 @@ public class Fraction
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public FractionField getField() {
|
||||
return FractionField.getInstance();
|
||||
}
|
||||
|
|
|
@ -48,16 +48,19 @@ public class FractionField implements Field<Fraction>, Serializable {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Fraction getOne() {
|
||||
return Fraction.ONE;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Fraction getZero() {
|
||||
return Fraction.ZERO;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Class<? extends FieldElement<Fraction>> getRuntimeClass() {
|
||||
return Fraction.class;
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ public class BinaryMutation implements MutationPolicy {
|
|||
* @return the mutated chromosome.
|
||||
* @throws MathIllegalArgumentException if <code>original</code> is not an instance of {@link BinaryChromosome}.
|
||||
*/
|
||||
@Override
|
||||
public Chromosome mutate(Chromosome original) throws MathIllegalArgumentException {
|
||||
if (!(original instanceof BinaryChromosome)) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.INVALID_BINARY_CHROMOSOME);
|
||||
|
|
|
@ -57,6 +57,7 @@ public abstract class Chromosome implements Comparable<Chromosome>,Fitness {
|
|||
* <li>0 if the two chromosomes have the same fitness</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(final Chromosome another) {
|
||||
return Double.compare(getFitness(), another.getFitness());
|
||||
}
|
||||
|
|
|
@ -98,6 +98,7 @@ public class CycleCrossover<T> implements CrossoverPolicy {
|
|||
* @throws MathIllegalArgumentException if the chromosomes are not an instance of {@link AbstractListChromosome}
|
||||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
|
||||
throws DimensionMismatchException, MathIllegalArgumentException {
|
||||
|
|
|
@ -77,6 +77,7 @@ public class ElitisticListPopulation extends ListPopulation {
|
|||
*
|
||||
* @return the beginnings of the next generation.
|
||||
*/
|
||||
@Override
|
||||
public Population nextGeneration() {
|
||||
// initialize a new generation with the same parameters
|
||||
ElitisticListPopulation nextGeneration =
|
||||
|
|
|
@ -23,8 +23,9 @@ import org.apache.commons.math4.exception.NumberIsTooSmallException;
|
|||
/**
|
||||
* Stops after a fixed amount of time has elapsed.
|
||||
* <p>
|
||||
* The first time {@link #isSatisfied(Population)} is invoked, the end time of the evolution is determined based on the
|
||||
* provided <code>maxTime</code> value. Once the elapsed time reaches the configured <code>maxTime</code> value,
|
||||
* The first time {@link #isSatisfied(Population)} is invoked, the end time of
|
||||
* the evolution is determined based on the provided <code>maxTime</code> value.
|
||||
* Once the elapsed time reaches the configured <code>maxTime</code> value,
|
||||
* {@link #isSatisfied(Population)} returns true.
|
||||
*
|
||||
* @since 3.1
|
||||
|
@ -67,6 +68,7 @@ public class FixedElapsedTime implements StoppingCondition {
|
|||
* @param population ignored (no impact on result)
|
||||
* @return <code>true</code> IFF the maximum allowed time period has elapsed
|
||||
*/
|
||||
@Override
|
||||
public boolean isSatisfied(final Population population) {
|
||||
if (endTime < 0) {
|
||||
endTime = System.nanoTime() + maxTimePeriod;
|
||||
|
|
|
@ -19,9 +19,11 @@ package org.apache.commons.math4.genetics;
|
|||
import org.apache.commons.math4.exception.NumberIsTooSmallException;
|
||||
|
||||
/**
|
||||
* Stops after a fixed number of generations. Each time {@link #isSatisfied(Population)} is invoked, a generation
|
||||
* counter is incremented. Once the counter reaches the configured <code>maxGenerations</code> value,
|
||||
* {@link #isSatisfied(Population)} returns true.
|
||||
* Stops after a fixed number of generations.
|
||||
* <p>
|
||||
* Each time {@link #isSatisfied(Population)} is invoked, a generation counter
|
||||
* is incremented. Once the counter reaches the configured
|
||||
* {@code maxGenerations} value, {@link #isSatisfied(Population)} returns true.
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
|
@ -52,6 +54,7 @@ public class FixedGenerationCount implements StoppingCondition {
|
|||
* @param population ignored (no impact on result)
|
||||
* @return <code>true</code> IFF the maximum number of generations has been exceeded
|
||||
*/
|
||||
@Override
|
||||
public boolean isSatisfied(final Population population) {
|
||||
if (this.numGenerations < this.maxGenerations) {
|
||||
numGenerations++;
|
||||
|
|
|
@ -36,7 +36,7 @@ import org.apache.commons.math4.exception.util.LocalizedFormats;
|
|||
public abstract class ListPopulation implements Population {
|
||||
|
||||
/** List of chromosomes */
|
||||
private List<Chromosome> chromosomes;
|
||||
private final List<Chromosome> chromosomes;
|
||||
|
||||
/** maximal size of the population */
|
||||
private int populationLimit;
|
||||
|
@ -119,6 +119,7 @@ public abstract class ListPopulation implements Population {
|
|||
* @throws NumberIsTooLargeException if the population would exceed the {@code populationLimit} after
|
||||
* adding this chromosome
|
||||
*/
|
||||
@Override
|
||||
public void addChromosome(final Chromosome chromosome) throws NumberIsTooLargeException {
|
||||
if (chromosomes.size() >= populationLimit) {
|
||||
throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
|
||||
|
@ -131,6 +132,7 @@ public abstract class ListPopulation implements Population {
|
|||
* Access the fittest chromosome in this population.
|
||||
* @return the fittest chromosome.
|
||||
*/
|
||||
@Override
|
||||
public Chromosome getFittestChromosome() {
|
||||
// best so far
|
||||
Chromosome bestChromosome = this.chromosomes.get(0);
|
||||
|
@ -147,6 +149,7 @@ public abstract class ListPopulation implements Population {
|
|||
* Access the maximum population size.
|
||||
* @return the maximum population size.
|
||||
*/
|
||||
@Override
|
||||
public int getPopulationLimit() {
|
||||
return this.populationLimit;
|
||||
}
|
||||
|
@ -172,6 +175,7 @@ public abstract class ListPopulation implements Population {
|
|||
* Access the current population size.
|
||||
* @return the current population size.
|
||||
*/
|
||||
@Override
|
||||
public int getPopulationSize() {
|
||||
return this.chromosomes.size();
|
||||
}
|
||||
|
@ -190,6 +194,7 @@ public abstract class ListPopulation implements Population {
|
|||
*
|
||||
* @return chromosome iterator
|
||||
*/
|
||||
@Override
|
||||
public Iterator<Chromosome> iterator() {
|
||||
return getChromosomes().iterator();
|
||||
}
|
||||
|
|
|
@ -103,6 +103,7 @@ public class NPointCrossover<T> implements CrossoverPolicy {
|
|||
* not an instance of {@link AbstractListChromosome}
|
||||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked") // OK because of instanceof checks
|
||||
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
|
||||
throws DimensionMismatchException, MathIllegalArgumentException {
|
||||
|
|
|
@ -74,6 +74,7 @@ public class OnePointCrossover<T> implements CrossoverPolicy {
|
|||
* not an instance of {@link AbstractListChromosome}
|
||||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked") // OK because of instanceof checks
|
||||
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
|
||||
throws DimensionMismatchException, MathIllegalArgumentException {
|
||||
|
|
|
@ -66,6 +66,7 @@ public class OrderedCrossover<T> implements CrossoverPolicy {
|
|||
* not an instance of {@link AbstractListChromosome}
|
||||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
|
||||
throws DimensionMismatchException, MathIllegalArgumentException {
|
||||
|
|
|
@ -94,6 +94,7 @@ public abstract class RandomKey<T> extends AbstractListChromosome<Double> implem
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<T> decode(final List<T> sequence) {
|
||||
return decodeGeneric(sequence, getRepresentation(), sortedRepresentation);
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ public class RandomKeyMutation implements MutationPolicy {
|
|||
*
|
||||
* @throws MathIllegalArgumentException if <code>original</code> is not a {@link RandomKey} instance
|
||||
*/
|
||||
@Override
|
||||
public Chromosome mutate(final Chromosome original) throws MathIllegalArgumentException {
|
||||
if (!(original instanceof RandomKey<?>)) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.RANDOMKEY_MUTATION_WRONG_CLASS,
|
||||
|
|
|
@ -54,6 +54,7 @@ public class TournamentSelection implements SelectionPolicy {
|
|||
* @return the selected chromosomes.
|
||||
* @throws MathIllegalArgumentException if the tournament arity is bigger than the population size
|
||||
*/
|
||||
@Override
|
||||
public ChromosomePair select(final Population population) throws MathIllegalArgumentException {
|
||||
return new ChromosomePair(tournament((ListPopulation) population),
|
||||
tournament((ListPopulation) population));
|
||||
|
@ -74,6 +75,7 @@ public class TournamentSelection implements SelectionPolicy {
|
|||
}
|
||||
// auxiliary population
|
||||
ListPopulation tournamentPopulation = new ListPopulation(this.arity) {
|
||||
@Override
|
||||
public Population nextGeneration() {
|
||||
// not useful here
|
||||
return null;
|
||||
|
|
|
@ -82,6 +82,7 @@ public class UniformCrossover<T> implements CrossoverPolicy {
|
|||
* not an instance of {@link AbstractListChromosome}
|
||||
* @throws DimensionMismatchException if the length of the two chromosomes is different
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
|
||||
throws DimensionMismatchException, MathIllegalArgumentException {
|
||||
|
|
|
@ -58,6 +58,7 @@ public class DoublePoint implements Clusterable, Serializable {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double[] getPoint() {
|
||||
return point;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ public class CanberraDistance implements DistanceMeasure {
|
|||
private static final long serialVersionUID = -6972277381587032228L;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double compute(double[] a, double[] b) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
|
|
|
@ -29,6 +29,7 @@ public class ChebyshevDistance implements DistanceMeasure {
|
|||
private static final long serialVersionUID = -4694868171115238296L;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double compute(double[] a, double[] b) {
|
||||
return MathArrays.distanceInf(a, b);
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ public class EarthMoversDistance implements DistanceMeasure {
|
|||
private static final long serialVersionUID = -5406732779747414922L;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double compute(double[] a, double[] b) {
|
||||
double lastDistance = 0;
|
||||
double totalDistance = 0;
|
||||
|
|
|
@ -29,6 +29,7 @@ public class EuclideanDistance implements DistanceMeasure {
|
|||
private static final long serialVersionUID = 1717556319784040040L;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double compute(double[] a, double[] b) {
|
||||
return MathArrays.distance(a, b);
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ public class ManhattanDistance implements DistanceMeasure {
|
|||
private static final long serialVersionUID = -9108154600539125566L;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double compute(double[] a, double[] b) {
|
||||
return MathArrays.distance1(a, b);
|
||||
}
|
||||
|
|
|
@ -799,7 +799,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
|
|||
* @return within-bin kernel parameterized by bStats
|
||||
*/
|
||||
protected RealDistribution getKernel(SummaryStatistics bStats) {
|
||||
if (bStats.getN() == 1) {
|
||||
if (bStats.getN() == 1 || bStats.getVariance() == 0) {
|
||||
return new ConstantRealDistribution(bStats.getMean());
|
||||
} else {
|
||||
return new NormalDistribution(randomData.getRandomGenerator(),
|
||||
|
|
|
@ -47,8 +47,6 @@ import org.apache.commons.math4.util.FastMath;
|
|||
* this library are license free. Since no such notice appears in the code these
|
||||
* functions can safely be ported to Commons-Math.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Beta {
|
||||
/** Maximum allowed numerical error. */
|
||||
|
|
|
@ -17,11 +17,6 @@
|
|||
|
||||
package org.apache.commons.math4.stat.correlation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
|
@ -62,14 +57,21 @@ public class SpearmansCorrelation {
|
|||
|
||||
/**
|
||||
* Create a SpearmansCorrelation with the given ranking algorithm.
|
||||
* <p>
|
||||
* From version 4.0 onwards this constructor will throw an exception
|
||||
* if the provided {@link NaturalRanking} uses a {@link NaNStrategy#REMOVED} strategy.
|
||||
*
|
||||
* @param rankingAlgorithm ranking algorithm
|
||||
* @throws MathIllegalArgumentException if the provided {@link RankingAlgorithm} is of
|
||||
* type {@link NaturalRanking} and uses a {@link NaNStrategy#REMOVED} strategy
|
||||
* @since 3.1
|
||||
*/
|
||||
public SpearmansCorrelation(final RankingAlgorithm rankingAlgorithm) {
|
||||
public SpearmansCorrelation(final RankingAlgorithm rankingAlgorithm)
|
||||
throws MathIllegalArgumentException {
|
||||
|
||||
if (rankingAlgorithm instanceof NaturalRanking &&
|
||||
NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.NOT_SUPPORTED_NAN_STRATEGY,
|
||||
NaNStrategy.REMOVED);
|
||||
}
|
||||
|
||||
data = null;
|
||||
this.rankingAlgorithm = rankingAlgorithm;
|
||||
rankCorrelation = null;
|
||||
|
@ -88,15 +90,22 @@ public class SpearmansCorrelation {
|
|||
/**
|
||||
* Create a SpearmansCorrelation with the given input data matrix
|
||||
* and ranking algorithm.
|
||||
* <p>
|
||||
* From version 4.0 onwards this constructor will throw an exception
|
||||
* if the provided {@link NaturalRanking} uses a {@link NaNStrategy#REMOVED} strategy.
|
||||
*
|
||||
* @param dataMatrix matrix of data with columns representing
|
||||
* variables to correlate
|
||||
* @param rankingAlgorithm ranking algorithm
|
||||
* @throws MathIllegalArgumentException if the provided {@link RankingAlgorithm} is of
|
||||
* type {@link NaturalRanking} and uses a {@link NaNStrategy#REMOVED} strategy
|
||||
*/
|
||||
public SpearmansCorrelation(final RealMatrix dataMatrix, final RankingAlgorithm rankingAlgorithm) {
|
||||
public SpearmansCorrelation(final RealMatrix dataMatrix, final RankingAlgorithm rankingAlgorithm)
|
||||
throws MathIllegalArgumentException {
|
||||
|
||||
if (rankingAlgorithm instanceof NaturalRanking &&
|
||||
NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.NOT_SUPPORTED_NAN_STRATEGY,
|
||||
NaNStrategy.REMOVED);
|
||||
}
|
||||
|
||||
this.rankingAlgorithm = rankingAlgorithm;
|
||||
this.data = rankTransform(dataMatrix);
|
||||
rankCorrelation = new PearsonsCorrelation(data);
|
||||
|
@ -167,19 +176,8 @@ public class SpearmansCorrelation {
|
|||
throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
|
||||
xArray.length, 2);
|
||||
} else {
|
||||
double[] x = xArray;
|
||||
double[] y = yArray;
|
||||
if (rankingAlgorithm instanceof NaturalRanking &&
|
||||
NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
|
||||
final Set<Integer> nanPositions = new HashSet<Integer>();
|
||||
|
||||
nanPositions.addAll(getNaNPositions(xArray));
|
||||
nanPositions.addAll(getNaNPositions(yArray));
|
||||
|
||||
x = removeValues(xArray, nanPositions);
|
||||
y = removeValues(yArray, nanPositions);
|
||||
}
|
||||
return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y));
|
||||
return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(xArray),
|
||||
rankingAlgorithm.rank(yArray));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,29 +189,7 @@ public class SpearmansCorrelation {
|
|||
* @return a rank-transformed matrix
|
||||
*/
|
||||
private RealMatrix rankTransform(final RealMatrix matrix) {
|
||||
RealMatrix transformed = null;
|
||||
|
||||
if (rankingAlgorithm instanceof NaturalRanking &&
|
||||
((NaturalRanking) rankingAlgorithm).getNanStrategy() == NaNStrategy.REMOVED) {
|
||||
final Set<Integer> nanPositions = new HashSet<Integer>();
|
||||
for (int i = 0; i < matrix.getColumnDimension(); i++) {
|
||||
nanPositions.addAll(getNaNPositions(matrix.getColumn(i)));
|
||||
}
|
||||
|
||||
// if we have found NaN values, we have to update the matrix size
|
||||
if (!nanPositions.isEmpty()) {
|
||||
transformed = new BlockRealMatrix(matrix.getRowDimension() - nanPositions.size(),
|
||||
matrix.getColumnDimension());
|
||||
for (int i = 0; i < transformed.getColumnDimension(); i++) {
|
||||
transformed.setColumn(i, removeValues(matrix.getColumn(i), nanPositions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (transformed == null) {
|
||||
transformed = matrix.copy();
|
||||
}
|
||||
|
||||
RealMatrix transformed = matrix.copy();
|
||||
for (int i = 0; i < transformed.getColumnDimension(); i++) {
|
||||
transformed.setColumn(i, rankingAlgorithm.rank(transformed.getColumn(i)));
|
||||
}
|
||||
|
@ -221,39 +197,4 @@ public class SpearmansCorrelation {
|
|||
return transformed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the indices of NaN values in the input array.
|
||||
*
|
||||
* @param input the input array
|
||||
* @return a list of NaN positions in the input array
|
||||
*/
|
||||
private List<Integer> getNaNPositions(final double[] input) {
|
||||
final List<Integer> positions = new ArrayList<Integer>();
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (Double.isNaN(input[i])) {
|
||||
positions.add(i);
|
||||
}
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all values from the input array at the specified indices.
|
||||
*
|
||||
* @param input the input array
|
||||
* @param indices a set containing the indices to be removed
|
||||
* @return the input array without the values at the specified indices
|
||||
*/
|
||||
private double[] removeValues(final double[] input, final Set<Integer> indices) {
|
||||
if (indices.isEmpty()) {
|
||||
return input;
|
||||
}
|
||||
final double[] result = new double[input.length - indices.size()];
|
||||
for (int i = 0, j = 0; i < input.length; i++) {
|
||||
if (!indices.contains(i)) {
|
||||
result[j++] = input[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -214,6 +214,7 @@ NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas str
|
|||
NOT_STRICTLY_INCREASING_SEQUENCE = les points {3} et {2} ne sont pas strictement croissants ({1} >= {0})
|
||||
NOT_SUBTRACTION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour la soustraction matricielle
|
||||
NOT_SUPPORTED_IN_DIMENSION_N = m\u00e9thode non disponible en dimension {0}
|
||||
NOT_SUPPORTED_NAN_STRATEGY = strat\u00e9gie "NaN" {0} non disponible
|
||||
NOT_SYMMETRIC_MATRIX = matrice non symm\u00e9trique
|
||||
NON_SYMMETRIC_MATRIX = matrice non symm\u00e9trique: la diff\u00e9rence entre les \u00e9l\u00e9ments ({0},{1}) et ({1},{0}) est sup\u00e9rieure \u00e0 {2}
|
||||
NO_BIN_SELECTED = aucun compartiment s\u00e9lectionn\u00e9
|
||||
|
|
|
@ -29,7 +29,7 @@ public class LocalizedFormatsTest {
|
|||
|
||||
@Test
|
||||
public void testMessageNumber() {
|
||||
Assert.assertEquals(321, LocalizedFormats.values().length);
|
||||
Assert.assertEquals(322, LocalizedFormats.values().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -430,6 +430,21 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* MATH-1203
|
||||
*/
|
||||
@Test
|
||||
public void testNoBinVariance() {
|
||||
final double[] data = {0, 0, 1, 1};
|
||||
EmpiricalDistribution dist = new EmpiricalDistribution(2);
|
||||
dist.load(data);
|
||||
dist.reseedRandomGenerator(1000);
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final double dev = dist.sample();
|
||||
Assert.assertTrue(dev == 0 || dev == 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the bin that x belongs (relative to {@link #makeDistribution()}).
|
||||
*/
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.apache.commons.math4.stat.correlation;
|
||||
|
||||
import org.apache.commons.math4.TestUtils;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.linear.BlockRealMatrix;
|
||||
import org.apache.commons.math4.linear.MatrixUtils;
|
||||
import org.apache.commons.math4.linear.RealMatrix;
|
||||
|
@ -121,19 +122,21 @@ public class SpearmansRankCorrelationTest extends PearsonsCorrelationTest {
|
|||
new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = MathIllegalArgumentException.class)
|
||||
public void testMath891Array() {
|
||||
// NaNStrategy.REMOVED is not supported since 4.0
|
||||
final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
|
||||
final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };
|
||||
|
||||
NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
|
||||
SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
|
||||
|
||||
|
||||
Assert.assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = MathIllegalArgumentException.class)
|
||||
public void testMath891Matrix() {
|
||||
// NaNStrategy.REMOVED is not supported since 4.0
|
||||
final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
|
||||
final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };
|
||||
|
||||
|
|
Loading…
Reference in New Issue