From b3fa8a534c5a6a9e43a96635a0b44e491baeebe2 Mon Sep 17 00:00:00 2001 From: Dave Parker Date: Wed, 7 Feb 2018 11:23:04 +0000 Subject: [PATCH] Add some useful methods to explicit.Distribution. --- prism/src/explicit/Distribution.java | 71 ++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/prism/src/explicit/Distribution.java b/prism/src/explicit/Distribution.java index b47a474c..01df3efb 100644 --- a/prism/src/explicit/Distribution.java +++ b/prism/src/explicit/Distribution.java @@ -29,6 +29,7 @@ package explicit; import java.util.BitSet; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -202,18 +203,64 @@ public class Distribution implements Iterable> } /** - * Get the sum of the probabilities in the distribution. + * Get the mean of the distribution. */ - public double sum() + public double mean() { double d = 0.0; Iterator> i = iterator(); while (i.hasNext()) { Map.Entry e = i.next(); - d += e.getValue(); + d += e.getValue() * e.getKey(); } return d; } + + /** + * Get the variance of the distribution. + */ + public double variance() + { + double mean = mean(); + double meanSq = 0.0; + Iterator> i = iterator(); + while (i.hasNext()) { + Map.Entry e = i.next(); + meanSq += e.getValue() * e.getKey() * e.getKey(); + } + return Math.abs(meanSq - mean * mean); + } + + /** + * Get the standard deviation of the distribution. + */ + public double standardDeviation() + { + return Math.sqrt(variance()); + } + + /** + * Get the relative standard deviation of the distribution, + * i.e., as a percentage of the mean. + */ + public double standardDeviationRelative() + { + return 100.0 * standardDeviation() / mean(); + } + + /** + * Get the sum of the probabilities in the distribution. + */ + public double sum() + { + double mean = 0.0; + Iterator> i = iterator(); + while (i.hasNext()) { + Map.Entry e = i.next(); + mean += e.getValue(); + } + return mean; + } /** * Get the sum of all the probabilities in the distribution except for index j. @@ -275,4 +322,22 @@ public class Distribution implements Iterable> { return map.toString(); } + + public String toStringCSV() + { + String s = "Value"; + Iterator> i = iterator(); + while (i.hasNext()) { + Map.Entry e = i.next(); + s += ", " + e.getKey(); + } + s += "\nProbability"; + i = iterator(); + while (i.hasNext()) { + Map.Entry e = i.next(); + s += ", " + e.getValue(); + } + s += "\n"; + return s; + } }