MATH-1636: Remove "isSupportedConnected" (as per STATISTICS-48).
This commit is contained in:
parent
142dcaa921
commit
226c1fc638
|
@ -164,31 +164,10 @@ public abstract class AbstractRealDistribution
|
|||
}
|
||||
};
|
||||
|
||||
double x = UnivariateSolverUtils.solve(toSolve,
|
||||
lowerBound,
|
||||
upperBound,
|
||||
getSolverAbsoluteAccuracy());
|
||||
|
||||
if (!isSupportConnected()) {
|
||||
/* Test for plateau. */
|
||||
final double dx = getSolverAbsoluteAccuracy();
|
||||
if (x - dx >= getSupportLowerBound()) {
|
||||
double px = cumulativeProbability(x);
|
||||
if (cumulativeProbability(x - dx) == px) {
|
||||
upperBound = x;
|
||||
while (upperBound - lowerBound > dx) {
|
||||
final double midPoint = 0.5 * (lowerBound + upperBound);
|
||||
if (cumulativeProbability(midPoint) < px) {
|
||||
lowerBound = midPoint;
|
||||
} else {
|
||||
upperBound = midPoint;
|
||||
}
|
||||
}
|
||||
return upperBound;
|
||||
}
|
||||
}
|
||||
}
|
||||
return x;
|
||||
return UnivariateSolverUtils.solve(toSolve,
|
||||
lowerBound,
|
||||
upperBound,
|
||||
getSolverAbsoluteAccuracy());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -465,15 +465,6 @@ public final class EmpiricalDistribution extends AbstractRealDistribution
|
|||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @since 3.1
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportConnected() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The probability of bin i.
|
||||
*
|
||||
|
@ -620,12 +611,6 @@ public final class EmpiricalDistribution extends AbstractRealDistribution
|
|||
return value;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean isSupportConnected() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
|
|
@ -215,18 +215,6 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
|
|||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The support of this distribution is connected.
|
||||
*
|
||||
* @return {@code true}
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportConnected() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
|
|
@ -251,18 +251,6 @@ public class EnumeratedRealDistribution
|
|||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The support of this distribution is connected.
|
||||
*
|
||||
* @return {@code true}
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportConnected() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
|
||||
|
|
|
@ -120,10 +120,5 @@ public class AbstractIntegerDistributionTest {
|
|||
public int getSupportUpperBound() {
|
||||
return 6;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isSupportConnected() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,201 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.math4.legacy.distribution;
|
||||
|
||||
import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
|
||||
import org.apache.commons.math4.legacy.analysis.integration.RombergIntegrator;
|
||||
import org.apache.commons.math4.legacy.analysis.integration.UnivariateIntegrator;
|
||||
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** Various tests related to MATH-699. */
|
||||
public class AbstractRealDistributionTest {
|
||||
|
||||
@Test
|
||||
public void testContinuous() {
|
||||
final double x0 = 0.0;
|
||||
final double x1 = 1.0;
|
||||
final double x2 = 2.0;
|
||||
final double x3 = 3.0;
|
||||
final double p12 = 0.5;
|
||||
final AbstractRealDistribution distribution;
|
||||
distribution = new AbstractRealDistribution() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public double cumulativeProbability(final double x) {
|
||||
if ((x < x0) || (x > x3)) {
|
||||
throw new OutOfRangeException(x, x0, x3);
|
||||
}
|
||||
if (x <= x1) {
|
||||
return p12 * (x - x0) / (x1 - x0);
|
||||
} else if (x <= x2) {
|
||||
return p12;
|
||||
} else if (x <= x3) {
|
||||
return p12 + (1.0 - p12) * (x - x2) / (x3 - x2);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double density(final double x) {
|
||||
if ((x < x0) || (x > x3)) {
|
||||
throw new OutOfRangeException(x, x0, x3);
|
||||
}
|
||||
if (x <= x1) {
|
||||
return p12 / (x1 - x0);
|
||||
} else if (x <= x2) {
|
||||
return 0.0;
|
||||
} else if (x <= x3) {
|
||||
return (1.0 - p12) / (x3 - x2);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getMean() {
|
||||
return ((x0 + x1) * p12 + (x2 + x3) * (1.0 - p12)) / 2.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVariance() {
|
||||
final double meanX = getMean();
|
||||
final double meanX2;
|
||||
meanX2 = ((x0 * x0 + x0 * x1 + x1 * x1) * p12 + (x2 * x2 + x2
|
||||
* x3 + x3 * x3)
|
||||
* (1.0 - p12)) / 3.0;
|
||||
return meanX2 - meanX * meanX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSupportLowerBound() {
|
||||
return x0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSupportUpperBound() {
|
||||
return x3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupportConnected() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
final double expected = x1;
|
||||
final double actual = distribution.inverseCumulativeProbability(p12);
|
||||
Assert.assertEquals("", expected, actual,
|
||||
distribution.getSolverAbsoluteAccuracy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiscontinuous() {
|
||||
final double x0 = 0.0;
|
||||
final double x1 = 0.25;
|
||||
final double x2 = 0.5;
|
||||
final double x3 = 0.75;
|
||||
final double x4 = 1.0;
|
||||
final double p12 = 1.0 / 3.0;
|
||||
final double p23 = 2.0 / 3.0;
|
||||
final AbstractRealDistribution distribution;
|
||||
distribution = new AbstractRealDistribution() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public double cumulativeProbability(final double x) {
|
||||
if ((x < x0) || (x > x4)) {
|
||||
throw new OutOfRangeException(x, x0, x4);
|
||||
}
|
||||
if (x <= x1) {
|
||||
return p12 * (x - x0) / (x1 - x0);
|
||||
} else if (x <= x2) {
|
||||
return p12;
|
||||
} else if (x <= x3) {
|
||||
return p23;
|
||||
} else {
|
||||
return (1.0 - p23) * (x - x3) / (x4 - x3) + p23;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double density(final double x) {
|
||||
if ((x < x0) || (x > x4)) {
|
||||
throw new OutOfRangeException(x, x0, x4);
|
||||
}
|
||||
if (x <= x1) {
|
||||
return p12 / (x1 - x0);
|
||||
} else if (x <= x2) {
|
||||
return 0.0;
|
||||
} else if (x <= x3) {
|
||||
return 0.0;
|
||||
} else {
|
||||
return (1.0 - p23) / (x4 - x3);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getMean() {
|
||||
final UnivariateFunction f = new UnivariateFunction() {
|
||||
|
||||
@Override
|
||||
public double value(final double x) {
|
||||
return x * density(x);
|
||||
}
|
||||
};
|
||||
final UnivariateIntegrator integrator = new RombergIntegrator();
|
||||
return integrator.integrate(Integer.MAX_VALUE, f, x0, x4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVariance() {
|
||||
final double meanX = getMean();
|
||||
final UnivariateFunction f = new UnivariateFunction() {
|
||||
|
||||
@Override
|
||||
public double value(final double x) {
|
||||
return x * x * density(x);
|
||||
}
|
||||
};
|
||||
final UnivariateIntegrator integrator = new RombergIntegrator();
|
||||
final double meanX2 = integrator.integrate(Integer.MAX_VALUE,
|
||||
f, x0, x4);
|
||||
return meanX2 - meanX * meanX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSupportLowerBound() {
|
||||
return x0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSupportUpperBound() {
|
||||
return x4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupportConnected() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
final double expected = x2;
|
||||
final double actual = distribution.inverseCumulativeProbability(p23);
|
||||
Assert.assertEquals("", expected, actual,
|
||||
distribution.getSolverAbsoluteAccuracy());
|
||||
|
||||
}
|
||||
}
|
|
@ -142,14 +142,6 @@ public class EnumeratedIntegerDistributionTest {
|
|||
Assert.assertEquals(7, testDistribution.getSupportUpperBound());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the distribution returns properly that the support is connected.
|
||||
*/
|
||||
@Test
|
||||
public void testIsSupportConnected() {
|
||||
Assert.assertTrue(testDistribution.isSupportConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests sampling.
|
||||
*/
|
||||
|
|
|
@ -162,14 +162,6 @@ public class EnumeratedRealDistributionTest {
|
|||
Assert.assertEquals(7, testDistribution.getSupportUpperBound(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the distribution returns properly that the support is connected.
|
||||
*/
|
||||
@Test
|
||||
public void testIsSupportConnected() {
|
||||
Assert.assertTrue(testDistribution.isSupportConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests sampling.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue