ChiSquare Distribution#
- class ChiSquareDistribution(amplitude: float = 1.0, degree_of_freedom: int | float = 1, loc: float = 0.0, scale: float = 1.0, normalize: bool = False)[source]#
Bases:
BaseDistributionClass for
ChiSquareDistributiondistribution.Note
The
ChiSquareDistributionis a special case of theGammaDistribution,\(\alpha\ (\text{shape}) = \text{dof} / 2\),
\(\theta\ (\text{scale}) = 2\).
- Parameters:
amplitude (float, optional) – The amplitude of the PDF. Defaults to 1.0. Ignored if normalize is
True.degree_of_freedom (int or float, optional) – The degree of freedom for the chi-square distribution. Default is 1.0.
loc (float, optional) – The location parameter, for shifting. Defaults to 0.0.
normalize (bool, optional) – If
True, the distribution is normalized so that the total area under the PDF equals 1. Defaults toFalse.
- Examples:
Importing libraries
3import matplotlib.pyplot as plt 4import numpy as np 5from scipy.stats import chi2 6 7from pymultifit.distributions import ChiSquareDistribution
Generating a standard \(\chi^2(1)\) distribution with
pyMultiFitandscipy.9x_values = np.linspace(start=0, stop=5, num=500) 10 11y_multifit = ChiSquareDistribution(degree_of_freedom=1, normalize=True) 12y_scipy = chi2
Plotting PDF and CDF
14f, ax = plt.subplots(1, 2, figsize=(12, 5)) 15 16ax[0].plot(x_values, y_scipy.pdf(x=x_values, df=1), label="Scipy Chi2") 17ax[0].plot(x_values, y_multifit.pdf(x_values), "k:", label="pyMultiFit Chi2") 18ax[0].set_ylabel("f(x)") 19 20ax[1].plot(x_values, y_scipy.cdf(x=x_values, df=1), label="Scipy Chi2") 21ax[1].plot(x_values, y_multifit.cdf(x_values), "k:", label="pyMultiFit Chi2") 22ax[1].set_ylabel("F(x)") 23 24f.suptitle(r"$\chi^2$(1)") 25 26for i in ax: 27 i.set_xlabel("X") 28 i.legend() 29plt.tight_layout()
Generating a translated \(\chi^2(1)\) distribution with \(\text{loc} = 3\).
32y_multifit = ChiSquareDistribution(degree_of_freedom=1, loc=3, normalize=True)
Plotting PDF and CDF
34f, ax = plt.subplots(1, 2, figsize=(12, 5)) 35 36ax[0].plot(x_values, y_scipy.pdf(x=x_values, df=1, loc=3), label="Scipy translated Chi2") 37ax[0].plot(x_values, y_multifit.pdf(x_values), "k:", label="pyMultiFit translated Chi2") 38ax[0].set_ylabel("f(x)") 39 40ax[1].plot(x_values, y_scipy.cdf(x=x_values, df=1, loc=3), label="Scipy translated Chi2") 41ax[1].plot(x_values, y_multifit.cdf(x_values), "k:", label="pyMultiFit translated Chi2") 42ax[1].set_ylabel("F(x)") 43 44f.suptitle(r"$\chi^2$(1, loc=3)") 45 46for i in ax: 47 i.set_xlabel("X") 48 i.legend() 49plt.tight_layout()
- Attributes:
Methods
cdf(x)Compute the cumulative density function (CDF) for the distribution.
from_scipy_params(df[, loc, scale])Instantiate ChiSquareDistribution with scipy parameterization.
logcdf(x)Compute the log cumulative density function (logCDF) for the distribution.
logpdf(x)Compute the log probability density function (logPDF) for the distribution.
pdf(x)Compute the probability density function (PDF) for the distribution.
scipy_like(df[, loc, scale])Instantiate ChiSquareDistribution with scipy parameterization.
stats()Computes and returns the statistical properties of the distribution, including,
- cdf(x: Annotated[ndarray[Any, dtype[float64]], '1D array']) Annotated[ndarray[Any, dtype[float64]], '1D array'][source]#
Compute the cumulative density function (CDF) for the distribution.
- Parameters:
x – Input array at which to evaluate the CDF.
- classmethod from_scipy_params(df: int | float, loc: float = 0.0, scale: float = 1.0) ChiSquareDistribution[source]#
Instantiate ChiSquareDistribution with scipy parameterization.
- Parameters:
- df: int or float
The degree of freedom for the ChiSquare distribution.
- loc: float, optional
The location parameter. Defaults to 0.0.
- scale: float, optional
The scale parameter. Defaults to 1.0
- Returns:
- ChiSquareDistribution
An instance of normalized ChiSquareDistribution.
- logcdf(x: Annotated[ndarray[Any, dtype[float64]], '1D array']) Annotated[ndarray[Any, dtype[float64]], '1D array'][source]#
Compute the log cumulative density function (logCDF) for the distribution.
- Parameters:
x – Input array at which to evaluate the logCDF.
- logpdf(x: Annotated[ndarray[Any, dtype[float64]], '1D array']) Annotated[ndarray[Any, dtype[float64]], '1D array'][source]#
Compute the log probability density function (logPDF) for the distribution.
- Parameters:
x – Input array at which to evaluate the logPDF.
- pdf(x: Annotated[ndarray[Any, dtype[float64]], '1D array']) Annotated[ndarray[Any, dtype[float64]], '1D array'][source]#
Compute the probability density function (PDF) for the distribution.
- Parameters:
x – Input array at which to evaluate the PDF.
- classmethod scipy_like(df: int | float, loc: float = 0.0, scale: float = 1.0) ChiSquareDistribution[source]#
Instantiate ChiSquareDistribution with scipy parameterization.
- Parameters:
- df: int or float
The degree of freedom for the ChiSquare distribution.
- loc: float, optional
The location parameter. Defaults to 0.0.
- scale: float, optional
The scale parameter. Defaults to 1.0
- Returns:
- ChiSquareDistribution
An instance of normalized ChiSquareDistribution.
Deprecated since version v1.0.7: Use from_scipy_params instead of scipy_like. scipy_like will be removed in a future release.
- stats() Dict[str, float][source]#
Computes and returns the statistical properties of the distribution, including,
mean,
median,
variance, and
standard deviation.
- Returns:
A dictionary containing statistical properties such as mean, variance, etc.
- Return type:
Notes
If any of the parameters is not computable for a distribution, this method returns None.
This class internally utilizes the following functions from utilities_d module:
gamma_sr_pdf_()gamma_sr_cdf_()
Recommended Import#
from pymultifit.distributions import ChiSquareDistribution
Full Import#
from pymultifit.distributions.chiSquare_d import ChiSquareDistribution