DMelt:Statistics/2 Distribution Functions
Distribution functions
Many useful distribution functions can be found in cern.jet.stat.Probability package. The package contains numerical integration of certain probability distributions. Below we will show how to use the normal distribution which is very useful distribution in many statistical analyses.
In the example below we will compute the probability that our random outcome is within a specified interval using the normal distribution. The code below returns the area under the normal probability density function, integrated from minus infinity to -1.17 (assumes mean is zero, variance is one).
from cern.jet.stat.Probability import * print normal(-1.17)
For the two-sided case, one can multiply the result by 2.
Let us consider two other methods: Here an approach based on the javanpst class:
from javanpst.distributions.common.continuous import NormalDistribution p=NormalDistribution() print "1 sigma=",p.computeCumulativeProbability(1) print "2 sigma=",p.computeCumulativeProbability(2) print "\n Inverse 0.95 sigma. Method 1=",p.inverseNormalDistribution(0.95)
Alternatively, use Apache common math and its org.apache.commons.math3.distribution.NormalDistribution class:
from org.apache.commons.math3.distribution import NormalDistribution p=NormalDistribution() print "Inverse 0.95 sigma. Method 2=",p.inverseCumulativeProbability(0.95)
One can also calculate the inverse function: This returns the value, x, for which the area under the Normal (Gaussian) probability density function (integrated from minus infinity to x) is equal to the argument y (assumes mean is zero, variance is one):
from cern.jet.stat.Probability import * print normalInverse(0.12)
This is especially important it statistics: assume that in 12% cases a value X can be as large as X(max) due to a chance. Then the above code generate in how many "sigma"s you can define such access of events (1.174 sigma).
Test distributions
You can find a number of statistical distributions javanpst.distributions.tests.package-summary
Continues distributions
Discrete distributions
Plotting distribution functions
Now let us show how to plot density functions. This is relatively easy if you already read this manual before. Here is a simply code to plot the Cauchy distribution function:
from java.awt import Color from jhplot import * from jdistlib import * def func(min,max): # evaluate Cauchy function logz=Cauchy(1, 2) # initialaze function f=P1D("Cauchy") # container to fill step=(max-min)/100.0 for i in range(100): x=min+step*i f.add(x,logz.density(x, True) ) return f c1 = HPlot('Beta') # plot c1.visible(); c1.setAutoRange() c1.setMarginLeft(80) c1.setNameX("X") c1.setNameY("Values") p=func(-10,10) # plot between 1 and 100 p.setStyle('l') p.setPenWidth(4) p.setColor(Color.blue) c1.draw(p)
The image of the Cauchy function generated by this code is shown below: