Skip to content

Commit

Permalink
Merge b132e42 into 60bcdd4
Browse files Browse the repository at this point in the history
  • Loading branch information
Onuchin-Artem committed Feb 2, 2018
2 parents 60bcdd4 + b132e42 commit d2d4445
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ private double cumBinP(int binIndex) {
* @return within-bin kernel parameterized by bStats
*/
protected ContinuousDistribution getKernel(SummaryStatistics bStats) {
if (bStats.getN() == 1 || bStats.getVariance() == 0) {
if (bStats.getN() <= 1 || bStats.getVariance() == 0) {
return new ConstantContinuousDistribution(bStats.getMean());
} else {
return new NormalDistribution(bStats.getMean(), bStats.getStandardDeviation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.commons.rng.RestorableUniformRandomProvider;
import org.apache.commons.statistics.distribution.ContinuousDistribution;
import org.apache.commons.statistics.distribution.ConstantContinuousDistribution;
import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
import org.apache.commons.statistics.distribution.NormalDistribution;
import org.apache.commons.statistics.distribution.ExponentialDistribution;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.analysis.UnivariateFunction;
import org.apache.commons.math4.analysis.integration.BaseAbstractUnivariateIntegrator;
Expand Down Expand Up @@ -528,6 +530,29 @@ public void testKernelOverrideUniform() {
Assert.assertEquals(9.0, dist.inverseCumulativeProbability(0.6), tol);
}

@Test
public void testVerySkewedCase() {

final RestorableUniformRandomProvider randomProvider = RandomSource.create(RandomSource.WELL_19937_C, 1000);

// data we load into EmpiricalDistribution
final ContinuousDistribution.Sampler exponentialDistributionSampler = new ExponentialDistribution(0.05)
.createSampler(randomProvider);
final double[] empiricalDataPoints = new double[3000];
for (int i = 0; i < empiricalDataPoints.length; i++) {
empiricalDataPoints[i] = exponentialDistributionSampler.sample();
}

final EmpiricalDistribution testDistribution = new EmpiricalDistribution(100);
testDistribution.load(empiricalDataPoints);

for (int i = 0; i < 1000; i++) {
final double point = randomProvider.nextDouble();
final double cdf = testDistribution.cumulativeProbability(point);
Assert.assertFalse("point: " + point, Double.isNaN(cdf));
}

}

/**
* Empirical distribution using a constant smoothing kernel.
Expand Down

0 comments on commit d2d4445

Please sign in to comment.