我使用 Apache Maths(无论如何)编写了一个类,但我遇到了一个奇怪的故障(或者我的 Java 代码可能有问题?)。
这是代码
package distributions;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.commons.math3.distribution.AbstractIntegerDistribution;
import org.apache.commons.math3.util.FastMath;
public class CustomDistribution extends AbstractIntegerDistribution {
long[] v;
double[] p;
double[] cp;
Map<Long, Double> mp = new HashMap<Long, Double>();
Map<Long, Double> mcp = new HashMap<Long, Double>();
public CustomDistribution (long[] values, double[] probabilities) {
v = values;
p = probabilities;
int len = values.length;
for (int i = 0; i < len; i++) {
mp.put(v[i], p[i]);
}
cp = new double[len];
cp[0] = p[0];
for (int i = 1; i < len; i++) {
cp[i] = cp[i-1] + p[i];
}
for (int i = 0; i < len; i++) {
mcp.put(v[i], cp[i]);
}
}
public Map<Long, Double> getMCP () {return mcp;}
@Override
public double cumulativeProbability(int v) {
return mcp.get(v);
}
// Not implemented, can be done in Clojure code
@Override
public double getNumericalMean() {
return 0;
}
// Not implemented, can be done in Clojure code
@Override
public double getNumericalVariance() {
return 0;
}
@Override
public int getSupportLowerBound() {
return 0;
}
@Override
public int getSupportUpperBound() {
return 0;
}
@Override
public boolean isSupportConnected() {
return false;
}
@Override
public double probability(int v) {
return mp.get(v);
}
// Uses a naive search implementation, should be ok due to data size
public int sample () {
double r = FastMath.random();
int len = p.length;
Boolean flag = false;
int i = 0;
int result = -1;
while (i < len && flag == false) {
if (cp[i] < r) {
i = i + 1;
}
else {
result = (int) v[i];
flag = true;
}
}
return result;
}
}
Clojure 包装器:
(defn create-distribution
"Creates a distribution from empirical data"
[data]
(let [values (long-array (data :values))
probabilities (double-array (data :probabilities))]
(CustomDistribution. values probabilities)))
(create-distribution {:values [1 2 3 4] :probabilities [0.3 0.2 0.2 0.3]})
基本上它是一种分布,内部有两种形式的数据:HashMaps 和 Arrays。
在 Clojure 中,我尝试过:
(.probability dist 4)
(.probability dist (int 4))
两者都返回 NullPointerException,NativeMethodAccessorImpl..blabla 与 .cumulativeProbabilityMethod 相同(另一方面,.sample 工作正常,因此可能是 HashMap 的问题)
我发现可能在构造函数中 mcp 和 mp (即 HashMaps)没有在构造函数中正确计算,但是当我尝试时:
(.get (.getMCP dist) 4)
它返回给我正确的结果。MCP hashmap 实际上是我预期的结果。
那么为什么我的代码不起作用?我的两条线在“功能上”是相同的。如果我打错了,我看不到它。也许是因为不变性?
谢谢 !