Reputation: 1331
I coded a class using Apache Maths (whatever) and I have a weird glitch (or maybe there is a problem in my Java code ?).
here is the code
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 wrapper :
(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]})
Basically it is a distribution with data inside in two forms : HashMaps and Arrays.
In Clojure, I tried :
(.probability dist 4)
(.probability dist (int 4))
Both returns NullPointerException, NativeMethodAccessorImpl..blabla Same for the .cumulativeProbabilityMethod (on the other hand, .sample works fine so it may be a problem of HashMap)
I figured out that maybe in the constructor mcp and mp (that are HashMaps) were not properly calculated in the constructor but when I tried :
(.get (.getMCP dist) 4)
it returns me the correct result. MCP hashmap is in fact the result I expected.
So why does my code does not work ? My two lines are "functionally" the same. If I did a typo, I do not see it. Maybe it's because of the immutability thing ?
Thanks !
Upvotes: 0
Views: 124
Reputation: 1331
Ok so I figured out the problem, I forgot that Java was not as flexible as Clojure with numeric primitives.
This code works :
@Override
public double cumulativeProbability(int v) {
return mcp.get((long) v);
}
@Override
public double probability(int v) {
return mp.get((long) v);
}
In Clojure code, the translation of a double is automatic. In Java I passed an Integer where the HasMap expected a Long
Upvotes: 1