Statistical comparisons. Kolmogorov Smirnov (KS), Anderson-Darling, Goodman
Source code name: "stat_comparisons.py"
Programming language: Python
Topic: Statistics/Tests
DMelt Version 1. Last modified: 12/11/2015. License: Free
https://datamelt.org/code/cache/stat_comparisons_8469.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



from java.awt import Color
from java.util import Random
from jhplot  import * 

c1 = HPlotJa("Canvas")
c1.setGTitle("Statistical comparisons")
c1.setAutoRange()
c1.visible()

h1 = H1D("Histo1",20, -2, 2.0)
h1.setColor(Color.blue)
h2 = H1D("Histo2",20, -2, 2.0)
r = Random()
for i in range(500):
   h1.fill(r.nextGaussian())      
   h2.fill(r.nextGaussian())        
   if (i<100): h2.fill(2*r.nextGaussian()+2)

h1.setErrAll(1)
h2.setErrAll(0)
c1.draw(h1)
c1.draw(h2)

from hep.aida.util.comparison import *
# Compare the two histograms with the "AndersonDarling" algorithm
result = StatisticalComparison.compare(h1.get(), h2.get(),"AndersonDarling","")
print    "AndersonDarling method=",result.quality(),"/",result.nDof() 

# Compare the two histograms with the "Chi2" algorithm with rejection level at 1%.
result = StatisticalComparison.compare(h1.get(), h2.get(),"chi2","rejectionLevel=0.01")
print    "Chi2 method=",result.quality() ,"/",result.nDof()

# Goodman","KolmogorovSmirnovChi2Approx","KSChi2Approx 
result = StatisticalComparison.compare(h1.get(), h2.get(),"Goodman","rejectionLevel=0.01")
print    "Goodman method=",result.quality() ,"/",result.nDof()

# KolmogorovSmirnov 
result = StatisticalComparison.compare(h1.get(), h2.get(),"KolmogorovSmirnov","rejectionLevel=0.01")
print    "KolmogorovSmirnov  method=",result.quality() ,"/",result.nDof()

# Kuiper: works for P1D 
# result = StatisticalComparison.compare(h1.get(), h2.get(),"Kuiper","rejectionLevel=0.01")
# print    "Kuiper  method=",result.quality() ,"/",result.nDof()