CHISQ
Overview
The CHISQ function computes various statistics and distribution functions for the chi-squared distribution, a fundamental probability distribution in statistical inference. The chi-squared distribution arises when summing the squares of independent standard normal random variables and is widely used in hypothesis testing, confidence interval estimation, and goodness-of-fit tests.
This implementation uses the SciPy scipy.stats.chi2 module. The chi-squared distribution is characterized by a single shape parameter: the degrees of freedom (k or df), which must be greater than zero. The probability density function (PDF) is defined as:
f(x, k) = \frac{1}{2^{k/2} \Gamma(k/2)} x^{k/2 - 1} \exp\left(-\frac{x}{2}\right)
where x > 0, k > 0 is the degrees of freedom, and \Gamma denotes the gamma function. The chi-squared distribution is a special case of the gamma distribution with shape parameter a = k/2 and scale parameter \theta = 2.
The function supports multiple computation methods: pdf (probability density function), cdf (cumulative distribution function), icdf (inverse CDF / percent point function), sf (survival function), isf (inverse survival function), mean, median, var (variance), and std (standard deviation). The distribution’s mean equals the degrees of freedom (\mu = k), and its variance equals twice the degrees of freedom (\sigma^2 = 2k).
Common applications include the chi-squared test for independence in contingency tables, testing the variance of a normally distributed population, and constructing confidence intervals for population variance.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=CHISQ(value, df, loc, scale, chisq_method)
value(float, optional, default: null): Input value for methods requiring a value (pdf, cdf, icdf, sf, isf). Omit for mean, median, var, std.df(float, optional, default: 1): Degrees of freedom (must be > 0)loc(float, optional, default: 0): Location parameterscale(float, optional, default: 1): Scale parameter (must be > 0)chisq_method(str, optional, default: “pdf”): Method to compute (pdf, cdf, icdf, sf, isf, mean, median, var, std)
Returns (float): Distribution result (float), or error message string.
Examples
Example 1: PDF at x=2 with df=3
Inputs:
| value | df | loc | scale | chisq_method |
|---|---|---|---|---|
| 2 | 3 | 0 | 1 |
Excel formula:
=CHISQ(2, 3, 0, 1, "pdf")
Expected output:
0.2076
Example 2: CDF at x=2 with df=3
Inputs:
| value | df | loc | scale | chisq_method |
|---|---|---|---|---|
| 2 | 3 | 0 | 1 | cdf |
Excel formula:
=CHISQ(2, 3, 0, 1, "cdf")
Expected output:
0.4276
Example 3: Inverse CDF at q=0.428 with df=3
Inputs:
| value | df | loc | scale | chisq_method |
|---|---|---|---|---|
| 0.428 | 3 | 0 | 1 | icdf |
Excel formula:
=CHISQ(0.428, 3, 0, 1, "icdf")
Expected output:
2.002
Example 4: Mean of distribution with df=3
Inputs:
| df | loc | scale | chisq_method |
|---|---|---|---|
| 3 | 0 | 1 | mean |
Excel formula:
=CHISQ(3, 0, 1, "mean")
Expected output:
3
Python Code
import math
from scipy.stats import chi2 as scipy_chi2
def chisq(value=None, df=1, loc=0, scale=1, chisq_method='pdf'):
"""
Compute various statistics and functions for the chi-squared distribution from scipy.stats.chi2.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2.html
This example function is provided as-is without any representation of accuracy.
Args:
value (float, optional): Input value for methods requiring a value (pdf, cdf, icdf, sf, isf). Omit for mean, median, var, std. Default is None.
df (float, optional): Degrees of freedom (must be > 0) Default is 1.
loc (float, optional): Location parameter Default is 0.
scale (float, optional): Scale parameter (must be > 0) Default is 1.
chisq_method (str, optional): Method to compute (pdf, cdf, icdf, sf, isf, mean, median, var, std) Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Variance, Std Dev. Default is 'pdf'.
Returns:
float: Distribution result (float), or error message string.
"""
def check_result(result):
"""Convert result to float and check for NaN/inf."""
result = float(result)
if math.isnan(result):
return "Result is NaN (not a number)"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return result
valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}
if not isinstance(chisq_method, str):
return "Invalid input: chisq_method must be a string."
method = chisq_method.lower()
if method not in valid_methods:
return f"Invalid method: {chisq_method}. Must be one of {', '.join(sorted(valid_methods))}."
try:
df = float(df)
loc = float(loc)
scale = float(scale)
except (TypeError, ValueError):
return "Invalid input: df, loc, and scale must be numbers."
if df <= 0:
return "Invalid input: df must be > 0."
if scale <= 0:
return "Invalid input: scale must be > 0."
dist = scipy_chi2(df, loc=loc, scale=scale)
# Methods that require value
if method in ['pdf', 'cdf', 'icdf', 'sf', 'isf']:
if value is None:
return f"Invalid input: missing required argument 'value' for method '{method}'."
try:
value = float(value)
except (TypeError, ValueError):
return "Invalid input: value must be a number."
try:
if method == 'pdf':
result = dist.pdf(value)
elif method == 'cdf':
result = dist.cdf(value)
elif method == 'sf':
result = dist.sf(value)
elif method == 'isf':
if not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for isf."
result = dist.isf(value)
elif method == 'icdf':
if not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for icdf."
result = dist.ppf(value)
except Exception as e:
return f"scipy.stats.chi2 error: {e}"
return check_result(result)
# Methods that do not require value
try:
if method == 'mean':
result = dist.mean()
elif method == 'median':
result = dist.median()
elif method == 'var':
result = dist.var()
elif method == 'std':
result = dist.std()
except Exception as e:
return f"scipy.stats.chi2 error: {e}"
return check_result(result)