カイ二乗分布を描く。

特に何ということもないんだけれど、自由度 k=4のカイ二乗分布を ROOT と python で描いてみた。自由度 k=4 のカイ二乗分布は、平均値 k = 4、分散 2k = 8、最頻値 k - 2 = 2、中央値 k-2/3+4/27k -8/729k^2=3.4 で分布する。周期性を探す Zn-test (Raileigh Test) でよくお世話になる。

#!/usr/bin/env python

import ROOT
import pylab
from matplotlib import rc

def drange(start, stop, step):
    r = start
    while r < stop:
        yield r
        r += step

x = []
y_pdf = []
y_cdf = []
for i in drange(0,15,0.1):
    x.append(i)
    y_pdf.append(ROOT.Math.chisquared_pdf(i, 4))
    y_cdf.append(ROOT.Math.chisquared_cdf(i, 4))

rc('text', usetex=True)
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica'], 
             'size':'14'})

fig = pylab.figure(figsize=(7,7))
ax = fig.add_subplot(111)

ax.set_xlabel('Chi-square value')
ax.set_ylabel('Probability')
ax.set_title('Chi-square Distribution (Degree of Freedom = 4)')
ax.plot(x, y_pdf, 'r-', x, y_cdf, 'b--')
ax.legend(('Probability Distribution', 'Cumulative Distribution'), 'center right', shadow=False)

fig.savefig("chi2_dof4.pdf")