Implement probability distribution function in Java problem

I have a function which is in the form of probability distribution function as below:

Although I discover some lib providing function to obtain the result as the above formula would do; but I am learning to implement it so I would like to do it my self if possible.

Below is what I can think of when implementing the function

public double pdf(double x){
  double mean = mean();
  double variance = variance();
  double base = 1/Math.sqrt(2*Math.PI*variance);
  double pow = -(Math.pow((x-mean), 2)/2*variance);
  return Math.pow(base, pow); 
}

Is this is the right way to implement pdf? Or what parts do I miss?

I appreciate any help.


Robert Sedgewick is as good as they come:

http://introcs.cs.princeton.edu/22library/Gaussian.java.html

Take a look at his implementation.

You should also know about M. Abramowitz and IA Stegun, a wonderful classic for functions like this. It's available for little money from Dover Books. Worth having.


Your pdf method implements the probability density function of the normal distribution, but it appears you want to implement the cumulative normal distribution function (or at least a variation of it). For the normal distribution, this is highly nontrivial because it involves an infinite integral.

You could try numerical integration by using an approach such as the trapezium rule, but I suspect that as you get further and further from the mean, the inaccuracies start to build up.

If you want to implement the cumulative normal distribution function, then there are two approaches I'd recommend:

  • a table of pre-calculated values, or
  • numerical approximations such as those on the Wikipedia page for the normal distribution.

  • Not sure how much is relevant, but also check out Apache Commons math stuff. I've found it to be pretty clean and useful.

    org.apache.commons.math.stat.descriptive.DescriptiveStatistics works on one end, to analyze a given distribution.

    org.apache.commons.math.distribution.Distribution is used on the other end to generating the distribution.

    These should give you ideas for APIs and some code to study for the implementation. There are probably other open-source stats packages you might want to study.

    链接地址: http://www.djcxy.com/p/5470.html

    上一篇: 添加一个文件名作为目标的自定义命令

    下一篇: 在Java问题中实现概率分布函数