Chi-square Distribution¶
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
In [2]:
Copied!
headlines = ["A", "B", "C"]
headline_counts = np.array([14,8,12])
avg = sum(headline_counts) / len(headline_counts)
headlines = ["A", "B", "C"]
headline_counts = np.array([14,8,12])
avg = sum(headline_counts) / len(headline_counts)
In [3]:
Copied!
chi2_stat, p_value = stats.chisquare(f_obs=headline_counts, f_exp=avg)
print(f"Chi-square statistic: {chi2_stat}")
print(f"P value: {p_value}")
chi2_stat, p_value = stats.chisquare(f_obs=headline_counts, f_exp=avg)
print(f"Chi-square statistic: {chi2_stat}")
print(f"P value: {p_value}")
Chi-square statistic: 1.6470588235294115 P value: 0.438879929791555
In [4]:
Copied!
x = np.linspace(0, 10, 100)
y = stats.chi2.pdf(x, df=2)
plt.plot(x, y)
plt.fill_between(x, y, where=(x > chi2_stat), alpha=0.5, label='P-Value')
plt.axvline(chi2_stat, color='black', linestyle='--', label='Statistic')
plt.legend()
plt.show()
x = np.linspace(0, 10, 100)
y = stats.chi2.pdf(x, df=2)
plt.plot(x, y)
plt.fill_between(x, y, where=(x > chi2_stat), alpha=0.5, label='P-Value')
plt.axvline(chi2_stat, color='black', linestyle='--', label='Statistic')
plt.legend()
plt.show()