Reputation: 431
I want to find out how to generate a probability distribution. I am working on the Stochastic Shortest Path Problem where edges have associated probability distributions with associated costs for each probability. I was able to generate a (normal) distribution like this:
0 1 2 3 4
0.15 0.2 0.18 0.22 0.25
such that the sum of all probabilities is 1 by following answers provided in this question. Now, I need to generate other distributions like bi-normal, log-normal and gamma. I would really appreciate any clarifications on these distributions and (pseudo) code, preferably in Java, on generating them.
Upvotes: 0
Views: 1496
Reputation: 795
Normal distribution: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/NormalDistribution.html
Log normal distribution: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/LogNormalDistribution.html
Gamma distribution: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/GammaDistribution.html
I would stay away from 100% home-cooked solutions such as the question you refer to.
Here is some code to generate the distributions. You will have to normalize them yourself to make the sum add up to 1.
import org.apache.commons.math3.distribution.AbstractRealDistribution;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.LogNormalDistribution;
import org.apache.commons.math3.distribution.GammaDistribution;
public class DistributionGenerator {
public static void main(String[] args) {
// Pick one and comment out the others:
//AbstractRealDistribution distr = new NormalDistribution(2.0,0.5); // mean and standard deviation constructor.
//AbstractRealDistribution distr = new LogNormalDistribution(0.0,1.0); // scale and shape constructor.
AbstractRealDistribution distr = new GammaDistribution(2.0, 1.0);
for(int i=0; i<5; ++i)
{
System.out.println( distr.density(i));
}
}
}
Upvotes: 4
Reputation: 18228
Quick search reveals the Colt library and its suite of distributions classes.
Upvotes: 2