Browse Source

Add some useful methods to explicit.Distribution.

master
Dave Parker 8 years ago
parent
commit
b3fa8a534c
  1. 71
      prism/src/explicit/Distribution.java

71
prism/src/explicit/Distribution.java

@ -29,6 +29,7 @@ package explicit;
import java.util.BitSet; import java.util.BitSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
@ -202,19 +203,65 @@ public class Distribution implements Iterable<Entry<Integer, Double>>
} }
/** /**
* 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; double d = 0.0;
Iterator<Entry<Integer, Double>> i = iterator(); Iterator<Entry<Integer, Double>> i = iterator();
while (i.hasNext()) { while (i.hasNext()) {
Map.Entry<Integer, Double> e = i.next(); Map.Entry<Integer, Double> e = i.next();
d += e.getValue();
d += e.getValue() * e.getKey();
} }
return d; return d;
} }
/**
* Get the variance of the distribution.
*/
public double variance()
{
double mean = mean();
double meanSq = 0.0;
Iterator<Entry<Integer, Double>> i = iterator();
while (i.hasNext()) {
Map.Entry<Integer, Double> 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<Entry<Integer, Double>> i = iterator();
while (i.hasNext()) {
Map.Entry<Integer, Double> e = i.next();
mean += e.getValue();
}
return mean;
}
/** /**
* Get the sum of all the probabilities in the distribution except for index j. * Get the sum of all the probabilities in the distribution except for index j.
*/ */
@ -275,4 +322,22 @@ public class Distribution implements Iterable<Entry<Integer, Double>>
{ {
return map.toString(); return map.toString();
} }
public String toStringCSV()
{
String s = "Value";
Iterator<Entry<Integer, Double>> i = iterator();
while (i.hasNext()) {
Map.Entry<Integer, Double> e = i.next();
s += ", " + e.getKey();
}
s += "\nProbability";
i = iterator();
while (i.hasNext()) {
Map.Entry<Integer, Double> e = i.next();
s += ", " + e.getValue();
}
s += "\n";
return s;
}
} }
Loading…
Cancel
Save