Explanation of the Kolmogorov-Smirnov Test with applications in Java

I'm trying to apply the Kolmogorov-Smirnov test for goodness-of-fit to a dataset. I have a large set of values, and would like to know whether it fits a Poisson distribution with given $\lambda$.

As I am using Java to generate the dataset, I have found a Java class which is supposed to calculate what I want - it's called jsc.goodnessfit.KolmogorovTest.

There are three methods, approxUpperTailProb(int n,double D), exactUpperTailProb(int n,double D), and getSP().

Did I understand correctly that approxUpperTailProb() calculates the approximate probability that my data fits the given distribution?

Also, the value n is the size of the sample set, but what is D? It's described as "the Kolmogorov-Smirnov statistic; must not be $< 0$ or $> 1$." As far as I understand, it's the maximum distance of any point from my dataset to the corresponding point in the distribution I am comparing with. However, as I am comparing to a general Poisson distribution, I don't know how to calculate D.

Finally, what is getSP()? The documentation states that it "Returns the value of the significance probability." What does that mean?

Any help on this would be much appreciated!

• K-S is for continuous distributions. The Poisson is discrete. Instead, estimate its parameter, use that to compute the probabilities, and conduct a chi-squared test. – whuber Jul 14 '11 at 13:35

import java.util.Scanner;
class ABC
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
int n;
System.out.println("Enter Count of numbers:");
n=s.nextInt();
double d[]=new double[n];
double dp[]=new double[n];
double dm[]=new double[n];
double dplus,dmin,dmax;
System.out.println("Enter "+n+"Numbers:");
for(int i=0;i<n;i++)
{
d[i]=s.nextDouble();
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(d[i]>d[j])
{
double temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
dp[i]=((double)(i+1)/n)-d[i];
}
for(int i=0;i<n;i++)
{
dm[i]=d[i]-((double)i/n);
}
System.out.println("i\tRi\t(i/N-Ri)\tRi-(i-1/N)");
System.out.println("-------------------------------------------");
for(int i=1;i<n;i++)
{
System.out.println((i+1)+"\t"+d[i]+"\t"+dp[i]+"\t"+dm[i]);
}
dplus=dp[0];
dmin=dm[0];
for(int i=0;i<n;i++)
{
if(dp[i]>dplus)
{
dplus=dp[i];
}
}

for(int i=1;i<n;i++)
{
if(dm[i]>dmin)
{
dmin=dm[i];
}
}
System.out.println("D+="+dplus+"\nD-="+dmin);

double D=Math.max(dplus,dmin);

System.out.println("D="+D);

System.out.println("Enter the critical value:");
double da=s.nextDouble();

if(D>da)
{
System.out.println("Hypothesis H0 is Rejected");
}
else
{
System.out.println("Hypothesis H0 is Accepted");
}
}
}

• Welcome to CrossValidated. Thank you for your answer. Can you add some explanation to your code? – Ferdi Mar 22 '17 at 9:37
• I can't understand why you would want to spend time doing statistical calculations in Java when R and python have a wealth of carefully checked functions for these things. – Frank Harrell Jun 22 '18 at 11:47