Statistical comparison of two histograms (chi2, p-values)
Code: "stat_compare_h1d.py"
Programming language: Python
DMelt Version 1. Last modified: 12/10/2015. License: Free
http://jwork.org/dmelt/code/cache/stat_compare_h1d_4336.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 jhplot import H1D 
from java.util import Random

h1 = H1D("H1",20, -1, 1)
h2 = H1D("H2",20, -1, 1)
rand = Random()

# fill histograms sligly differently. Second distribution is narrower
for i in range(100):
      d=rand.nextGaussian()
      h1.fill(d)
      h2.fill(d*0.9)

#  compare histograms
d=h1.compareChi2(h2) # h1, h2 are H1D or H2D histograms defined above
print "chi2 / ndf =",d["chi2"]/d["ndf"]
print "p-value=",d["p-value"]


from hep.aida.util.comparison import *
r=StatisticalComparison.compare(h1.get(), h2.get(),'KS')
print"KolmogorovSmirnov  method=",r.quality() ,"/",r.nDof()