📜  scipy stats.chi() | Python(1)

📅  最后修改于: 2023-12-03 15:20:00.197000             🧑  作者: Mango

Scipy Stats.chi() | Python

The scipy.stats.chi() function calculates the probability density function (PDF) and cumulative distribution function (CDF) for the chi distribution. The chi distribution is a continuous probability distribution that arises in statistics and engineering for various applications, such as testing the goodness of fit of data, analyzing the time between random events, and estimating population variances.

Syntax
scipy.stats.chi(df, loc=0, scale=1)
  • df: degrees of freedom
  • loc: location parameter (default is 0)
  • scale: scale parameter (default is 1)
Parameters
  • x: array_like Values at which to evaluate the PDF or CDF
  • df: float or array_like Degrees of freedom
  • loc: float or array_like Location parameter (default is 0)
  • scale: float or array_like Scale parameter (default is 1)
Returns
  • pdf: ndarray or float Probability density function evaluated at x
  • cdf: ndarray or float Cumulative distribution function evaluated at x
Examples
Probability Density Function (PDF)
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import chi

df = 3

fig, ax = plt.subplots(1, 1)

x = np.linspace(chi.ppf(0.01, df), chi.ppf(0.99, df), 100)
ax.plot(x, chi.pdf(x, df), 'r-', lw=5, alpha=0.6, label='chi pdf')

plt.legend(loc='best')
plt.show()

pdf

Cumulative Distribution Function (CDF)
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import chi

df = 3

fig, ax = plt.subplots(1, 1)

x = np.linspace(chi.ppf(0.01, df), chi.ppf(0.99, df), 100)
ax.plot(x, chi.cdf(x, df), 'b-', lw=5, alpha=0.6, label='chi cdf')

plt.legend(loc='best')
plt.show()

cdf

Random Variates
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import chi

df = 3

fig, ax = plt.subplots(1, 1)

r = chi.rvs(df, size=1000)
ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()

random

Conclusion

In conclusion, the scipy.stats.chi() function is useful for calculating the PDF and CDF of the chi distribution. The degree of freedom, location, and scale parameters can be adjusted to fit specific needs. The function can be used to test the goodness of fit of data, analyze time between random events, and estimate population variances.