Statistics/Chi-squared distribution: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: cdf function)
Line 308: Line 308:
def χ2(x, k):
def χ2(x, k):
''' Chi-squared function, the probability distribution function (pdf) for chi-squared '''
''' Chi-squared function, the probability distribution function (pdf) for chi-squared '''
return x ** (k/2 - 1) * exp(-x/2) / (2 ** (k/2) * gamma(k / 2)) if x > 0 else 0.0
return x**(k/2 - 1) * exp(-x/2) / (2**(k/2) * gamma(k / 2)) if x > 0 and k > 0 else 0.0


def gamma_cdf(k, x):
''' lower incomplete gamma by series formula with gamma '''
return x**k * exp(-x) * sum(x**m / gamma(k + m + 1) for m in range(100))




def cdf_χ2(x, k):
def cdf_χ2(x, k):
''' Cumulative probability function (cdf) for chi-squared '''
''' Cumulative probability function (cdf) for chi-squared '''
if x <= 0 or k <= 0:
return gamma_cdf(k / 2, x / 2) if x > 0 and k > 0 else 0.0
return 0
return gammainc(k / 2, x / 2)