tdub.math

A module with math utilities

Function Summary

chisquared_cdf_c(chi2, ndf)

Calculate \(\chi^2\) probability from the value and NDF.

chisquared_test(h1, err1, h2, err2)

Perform \(\chi^2\) test on two histograms.

kolmogorov_prob(z)

Calculate the Kolmogorov distribution function.

ks_twosample_binned(hist1, hist2, err1, err2)

Calculate KS statistic and p-value for two binned distributions.

Reference

tdub.math.chisquared_cdf_c(chi2, ndf)[source]

Calculate \(\chi^2\) probability from the value and NDF.

See ROOT’s TMath::Prob & ROOT::Math::chisquared_cdf_c. Quoting the ROOT documentation:

Computation of the probability for a certain \(\chi^2\) and number of degrees of freedom (ndf). Calculations are based on the incomplete gamma function \(P(a,x)\), where \(a=\mathrm{ndf}/2\) and \(x=\chi^2/2\).

\(P(a,x)\) represents the probability that the observed \(\chi^2\) for a correct model should be less than the value \(\chi^2\). The returned probability corresponds to \(1-P(a,x)\), which denotes the probability that an observed \(\chi^2\) exceeds the value \(\chi^2\) by chance, even for a correct model.

Parameters
  • chi2 (float) – the \(\chi^2\) value

  • ndf (float) – the degrees of freedom

Returns

the \(\chi^2\) probability

Return type

float

tdub.math.chisquared_test(h1, err1, h2, err2)[source]

Perform \(\chi^2\) test on two histograms.

Parameters
Returns

the \(\chi^2\) test value, the degrees of freedom, and the probability

Return type

(float, int, float)

tdub.math.kolmogorov_prob(z)[source]

Calculate the Kolmogorov distribution function.

See ROOT’s implementation in TMath (TMath::KolmogorovProb).

Parameters

z (float) – the value to test

Returns

the probability that the test statistic exceeds \(z\) (assuming the null hypothesis).

Return type

float

Examples

>>> from tdub.math import kolmogorov_prob
>>> kolmogorov_prob(1.13)
0.15549781841748692
tdub.math.ks_twosample_binned(hist1, hist2, err1, err2)[source]

Calculate KS statistic and p-value for two binned distributions.

See ROOT’s implementation in TH1 (TH1::KolmogorovTest).

Parameters
  • hist1 (numpy.ndarray) – the histogram counts for the first distribution

  • hist2 (numpy.ndarray) – the histogram counts for the second distribution

  • err1 (numpy.ndarray) – the error on the histogram counts for the first distribution

  • err2 (numpy.ndarray) – the error on the histogram counts for the second distribution

Returns

first: the test-statistic; second: the probability of the test (much less than 1 means distributions are incompatible)

Return type

(float, float)

Examples

>>> import pygram11
>>> from tdub.math import ks_twosample_binned
>>> data1, data2, w1, w2 = some_function_to_get_data()
>>> h1, err1 = pygram11.histogram(data1, weights=w1, bins=40, range=(-3, 3))
>>> h2, err2 = pygram11.histogram(data2, weights=w2, bins=40, range=(-3, 3))
>>> kst, ksp = ks_twosample_binned(h1, h2, err1, err2)