我正在尝试将apache commons math用于一组值的内核密度估计。一个bin碰巧只有一个值,当我尝试调用cumulativeProbability()时,我得到一个NotStrictlyPositiveException。有什么方法可以防止这种情况吗?我不能确定所有的箱子都至少有一个值。
感谢。
答案 0 :(得分:0)
鉴于此错误仍然存在,我按照他们的指导编写了我自己的EmpiricalDistribution
class实现。
我只重新实现了我需要的功能,即计算发行版的熵,但你可以轻松地扩展它以满足你的需求。
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;
}
}
答案 1 :(得分: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.
我使用binCount初始化我的EmpiricalDistribution
等于我初始数据长度的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();