* Note that this implementation IGNORES elements of the input * array that are more than LOGTOLERANCE (currently 30.0) less * than the maximum element. *
* Cursory testing makes me wonder if this is actually much faster than * repeated use of the 2-argument version, however -cas. * @param vals An array log(x1), log(x2), ..., log(xn) * @return log(x1+x2+...+xn) */ public static double sumLogProb (double[] vals) { double max = Double.NEGATIVE_INFINITY; int len = vals.length; int maxidx = 0; for (int i = 0; i < len; i++) { if (vals[i] > max) { max = vals[i]; maxidx = i; } } boolean anyAdded = false; double intermediate = 0.0; double cutoff = max - LOGTOLERANCE; for (int i = 0; i < maxidx; i++) { if (vals[i] >= cutoff) { anyAdded = true; intermediate += Math.exp(vals[i] - max); } } for (int i = maxidx + 1; i < len; i++) { if (vals[i] >= cutoff) { anyAdded = true; intermediate += Math.exp(vals[i] - max); } } if (anyAdded) { return max + Math.log(1.0 + intermediate); } else { return max; } } }