TMath Kolmogorov Test

Hi all,

The TMath Kolmogorov test routine fails a simple test to compare random numbers drawn from a uniform distribution.

The test is to compare two arrays filled with a number drawn from a uniform random variable.

The result should be a uniform distribution of the probability between 0 and 1.

The following code, compiled with g++ 4.1.2 and root 5.22 ( g++ test.cpp root-config --cflags --libs )

#include <vector>
#include <algorithm>
#include "TH1.h"
#include "TRandom3.h"
#include "TMath.h"

int main(int argc, const char *argv[])
{
    int n,m;
    n = 1000;
    m = 1000;
    TRandom3 *r = new TRandom3;
    vector<double> v1,v2;
    double a1[n], a2[m];
    TH1D *h1 = new TH1D("ks_hist","",100,0,5);
    TH1D *hp1 = new TH1D("ks_prob1","",100,0,1);
    double z;
    v1.resize(n);
    v2.resize(m);
    for (int j=0;j<10000;j++){
        r->SetSeed(j);
	for (int i=0;i<n;i++) {
            v1[i]=r->Uniform();
        }
        r->SetSeed(j+1);
        for (int i=0;i<m;i++) {
             v2[i]=r->Uniform();
        }
        //test the ROOT function.
        sort(v1.begin(),v1.end());
        sort(v2.begin(),v2.end());
        for (int i=0;i<n;i++) {
            a1[i] = v1[i];
        }
        r->SetSeed(j+1);
	for (int i=0;i<m;i++) {
             a2[i] = v2[i];
        }
        z = TMath::KolmogorovTest(n,a1,m,a2,"");
        ha1->Fill(z);
        hap1->Fill(TMath::KolmogorovProb(z));
    }
    //Plot the histograms as you wish....
}

I enclose the two histograms.

Am I doing something wrong?

Cheers

Alex
kstest_root.pdf (3.76 KB)
ksprob_root.pdf (3.27 KB)

Hi,

the value returned by the function TMath::KolmogorovTest(n,a1,m,a2,"") is already the probability.
If you want the distance first you need to use option “M”, but then you need to renormalize the distance to get the probability

z = TMath::KolmogorovTest(n,a1,m,a2,"M"); 
double prob = TMath::KolmogorovProb(z*TMath::Sqrt(double(n)*double(m)/double(n+m)));

You will see that in the limit of large n,m you will get a uniform probability

Best Regards

Lorenzo