DataScience/통계분석
유의수준 $\alpha$의 이해
insightous
2023. 1. 27. 16:51
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy.stats import norm, t, chi2
z_values = np.linspace(-4, 4, 1000)
pdf_values = norm.pdf(z_values)
z_alpha=1.5
fill_color = (0, 0, 0, 0.1) # Light gray in RGBA format.
line_color = (0, 0, 0, 0.5) # Medium gray in RGBA format.
fig, axes = plt.subplots(figsize=(10,4))
axes.plot(z_values,pdf_values)
axes.fill_between(z_values,pdf_values, where=z_values>z_alpha ,color=fill_color)
axes.fill_between(z_values,pdf_values, where=z_values<-z_alpha,color=fill_color)
axes.axvline(x=0,ymin=0,ymax=1,color='lightgrey', linestyle='--')
pdf_z_alpha = norm.pdf(z_alpha)
axes.plot([z_alpha, z_alpha],[0,pdf_z_alpha],color=line_color)
axes.plot([-z_alpha, -z_alpha],[0,pdf_z_alpha],color=line_color)
xt = np.array([-z_alpha, z_alpha])
xtl = [mpl.text.Text(x=0,y=0,text=r'$-Z_{\frac{\alpha}{2}}$'),mpl.text.Text(x=0,y=0,text=r'$Z_{\frac{\alpha}{2}}$')]
axes.set_xticks(ticks=xt, labels=xtl,fontsize=20)
axes.annotate(r"$ \frac{\alpha}{2}$",xy=(1.75,0.05),xycoords='data',
xytext=(2.5,0.1),textcoords='data',
arrowprops=dict(arrowstyle="->",connectionstyle="arc3"),
fontsize=16)
axes.annotate(r"$ \frac{\alpha}{2}$",xy=(-1.75,0.05),xycoords='data',
xytext=(-2.5,0.1),textcoords='data',
arrowprops=dict(arrowstyle="->",connectionstyle="arc3"),
fontsize=16)
axes.text(0.5*(.25+.75), 0.5*(.25+.75), r'$1-\alpha$',
horizontalalignment='center',
verticalalignment='center',
fontsize=20, color='black',
transform=axes.transAxes)
axes.set_xlabel("z values")
axes.set_ylabel("probability for Z value")
axes.set_title("PDF for Z distribution")
plt.show()