Implementation of the probability distribution function in Java

I have a function that is in the form of a probability distribution function, as shown below:

formula

Although I discover some lib submission function to get the result, as the above formula did; but I participate in its implementation, therefore, if possible, I would like to do it myself.

Below I can come up with a 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 the right way to implement pdf? Or what parts are I missing?

I appreciate any help.

+7
source share
3 answers

Robert Sedgwick is as good as they are:

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

Take a look at its implementation.

You should also know about M. Abramowitz and IA Stegun, a wonderful classic for such features. It is available for little money from Dover Books. Worth having.

+2
source

Your pdf method implements the probability density function of the normal distribution, but it looks like you want to implement the cumulative normal distribution function (or at least a variant of it). For a normal distribution, this is very nontrivial, since it includes an infinite integral.

You can try numerical integration using this approach, usually trapezoid, but I suspect that as you get farther and farther from the average, inaccuracies begin to accumulate.

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

+1
source

Not sure how appropriate this is, but also check out the Apache Commons math information. I found it pretty clean and helpful.

org.apache.commons.math.stat.descriptive.DescriptiveStatistics works at one end to parse this distribution.

org.apache.commons.math.distribution.Distribution is used at the other end to generate the distribution.

They should give you ideas for the API and some code to study for implementation. There are probably other open source packages that you might want to learn.

0
source

All Articles