private DynamicIntArray initHistogram(Set<? extends Attributable> elements, String key) { DynamicIntArray array = new DynamicIntArray(12, 0); for (Attributable element : elements) { String strVal = element.getAttribute(key); if (strVal != null) { double value = Double.parseDouble(strVal); int bucket = discretizer.index(value); array.set(bucket, array.get(bucket) + 1); } } return array; }
private void calculateBuckets( Set<? extends Person> persons, DynamicDoubleArray sums, DynamicIntArray counts, String xAttrKey, String yAttrKey) { TIntDoubleHashMap sumBuckets = new TIntDoubleHashMap(); TIntIntHashMap countBuckets = new TIntIntHashMap(); for (Person person : persons) { String xValStr = person.getAttribute(xAttrKey); String yValStr = person.getAttribute(yAttrKey); if (xValStr != null && yValStr != null) { double xVal = Double.parseDouble(xValStr); double yVal = Double.parseDouble(yValStr); int bucketIdx = xDataDiscr.index(xVal); sumBuckets.adjustOrPutValue(bucketIdx, yVal, yVal); countBuckets.adjustOrPutValue(bucketIdx, 1, 1); } } TIntDoubleIterator it = sumBuckets.iterator(); for (int i = 0; i < sumBuckets.size(); i++) { it.advance(); int bucketIndex = it.key(); double sum = it.value(); int cnt = countBuckets.get(bucketIndex); sums.set(bucketIndex, sum); counts.set(bucketIndex, cnt); } }
private void initReferenceValues( Set<? extends Person> persons, String xAttrKey, String yAttrKey) { referenceValues = new DynamicDoubleArray(100, Double.NaN); DynamicDoubleArray sums = new DynamicDoubleArray(100, Double.NaN); DynamicIntArray counts = new DynamicIntArray(100, -1); calculateBuckets(persons, sums, counts, xAttrKey, yAttrKey); for (int i = 0; i < sums.size(); i++) { double sum = sums.get(i); int cnt = counts.get(i); if (!Double.isNaN(sum) && cnt > 0) { referenceValues.set(i, sum / (double) cnt); } } }
private double changeBucketContent(int bucketIndex, double value, boolean add) { double oldDiff = calculateDiff(bucketIndex); double sum = bucketSums.get(bucketIndex); int count = bucketCounts.get(bucketIndex); if (add) { bucketSums.set(bucketIndex, sum + value); bucketCounts.set(bucketIndex, count + 1); } else { bucketSums.set(bucketIndex, sum - value); bucketCounts.set(bucketIndex, count - 1); } double newDiff = calculateDiff(bucketIndex); return newDiff - oldDiff; }
public UnivariatFrequency( Set<? extends Attributable> refElements, Set<? extends Attributable> simElements, String attrKey, Discretizer discretizer) { this.discretizer = discretizer; this.attrKey = attrKey; refFreq = initHistogram(refElements, attrKey); simFreq = initHistogram(simElements, attrKey); scaleFactor = simElements.size() / (double) refElements.size(); normFactor = 1; // simElements.size(); int size = Math.max(simFreq.size(), refFreq.size()); for (int i = 0; i < size; i++) { double simVal = simFreq.get(i); double refVal = refFreq.get(i) * scaleFactor; // hamiltonianValue += (Math.abs(simVal - refVal)/ refVal)/normFactor; hamiltonianValue += calculateError(simVal, refVal) / normFactor; } }
private double calculateDiff(int bucketIndex) { double refValue = referenceValues.get(bucketIndex); if (Double.isNaN(refValue)) { // There is no reference value for this bucket. We cannot do any comparison. return 0.0; } else { double sum = bucketSums.get(bucketIndex); int cnt = bucketCounts.get(bucketIndex); if (!Double.isNaN(sum) && cnt > 0) { double simValue = sum / (double) cnt; return Math.abs(refValue - simValue); } else { return 0.0; } } }
public MultivariatMean( Set<? extends Person> refPersons, Set<CachedPerson> simPersons, String xAttrKey, String yAttrKey, Discretizer xDataDiscr) { this.xDataKey = Converters.getObjectKey(xAttrKey); this.yDataKey = Converters.getObjectKey(yAttrKey); this.xDataDiscr = xDataDiscr; initReferenceValues(refPersons, xAttrKey, yAttrKey); initSimulationValues(simPersons, xAttrKey, yAttrKey); // Calculate the initial hamiltonian value. hamiltonianValue = 0; int size = Math.max(referenceValues.size(), bucketCounts.size()); for (int i = 0; i < size; i++) { hamiltonianValue += calculateDiff(i); } }
private double changeBucketContent(int bucketIndex, int value) { double simVal = simFreq.get(bucketIndex); double refVal = refFreq.get(bucketIndex) * scaleFactor; // double oldDiff = Math.abs(simVal - refVal)/refVal; double oldDiff = calculateError(simVal, refVal); simFreq.set(bucketIndex, simFreq.get(bucketIndex) + value); simVal = simFreq.get(bucketIndex); refVal = refFreq.get(bucketIndex) * scaleFactor; // double newDiff = Math.abs(simVal - refVal)/refVal; double newDiff = calculateError(simVal, refVal); return newDiff - oldDiff; }