3

我正在尝试使用 apache commons math 对一组值进行内核密度估计。一个 bin 恰好只有一个值,当我尝试调用累积概率()时,我得到了 NotStrictlyPositiveException。有什么办法可以防止这种情况发生吗?我不能确定所有的箱子都至少有一个值。

谢谢。

4

2 回答 2

0

我的一个发行版出现了同样的错误。

阅读这个类的Javadoc,它说如下:

USAGE NOTES:
The binCount is set by default to 1000.  A good rule of thumb
is to set the bin count to approximately the length of the input 
file divided by 10.

我已经EmpiricalDistribution用 binCount 初始化了我的初始数据长度的 10%,现在一切正常:

double[] baseLine = getBaseLineValues();
...
// Initialise binCount
distribution = new EmpiricalDistribution(baseLine.length/10);
// Load base line data
distribution.load(baseLine);
// Now you can obtain random values based on this distribution
double randomValue = distribution.getNextValue();
于 2015-04-22T14:36:55.397 回答
0

鉴于此错误仍然存​​在,我按照他们的指导编写了自己的EmpiricalDistributionclass实现。我只重新实现了我需要的功能,即计算分布的熵,但您可以轻松地将其扩展到您的需要。

public class EmpiricalDistribution {

    private double[] values;
    private int[] binCountArray;
    private double maxValue, minValue;
    private double mean, stDev;

    public EmpiricalDistribution(double[] values) {
        this.values = values;
        int binCount = NumberUtil.roundToClosestInt(values.length / 10.0);
        binCountArray = new int[binCount];

        maxValue = Double.NEGATIVE_INFINITY;
        minValue = Double.POSITIVE_INFINITY;
        for (double value : values) {
            if (value > maxValue) maxValue = value;
            if (value < minValue) minValue = value;
        }

        double binRange = (maxValue - minValue) / binCount;
        for (double value : values) {
            int bin = (int) ((value - minValue) / binRange);
            bin = Math.min(binCountArray.length - 1, bin);
            binCountArray[bin]++;
        }

        mean = (new Mean()).evaluate(values);
        stDev = (new StandardDeviation()).evaluate(values, mean);
    }

    public double getEntropy() {
        double entropy = 0;
        for (int valuesInBin : binCountArray) {
            if (valuesInBin == 0) continue;

            double binProbability = valuesInBin / (double) values.length;
            entropy -= binProbability * FastMath.log(2, binProbability);
        }

        return entropy;
    }

    public double getMean() {
        return mean;
    }

    public double getStandardDeviation() {
        return stDev;
    }

}
于 2014-04-18T10:40:20.687 回答