import numpy as np #import scipy.stats as stats import seaborn as sns # seaborn package를 이용 import matplotlib.pyplot as plt # matplotlib.pyplot package를 이용 from numpy import random # set the random seed: np.random.seed(12345) r=10000 chi1 = random.chisquare(df=5, size=r) sns.histplot(data=chi1, x=None).set(title='Histogram of Chi-square w/ df=5') plt.show() chi2 = random.chisquare(df=10, size=r) sns.histplot(data=chi2, x=None).set(title='Histogram of Chi-square w/ df=10') plt.show() chi3 = random.chisquare(df=20, size=r) sns.histplot(data=chi3, x=None).set(title='Histogram of Chi-square w/ df=20') plt.show() chi4 = random.chisquare(df=30, size=r) sns.histplot(data=chi4, x=None).set(title='Histogram of Chi-square w/ df=30') plt.show() np.mean(chi1) np.mean(chi2) np.mean(chi3) np.mean(chi4) print("Mean of Chi-square Distribution w/ df=5 is : ", np.mean(chi1)) print("Mean of Chi-square Distribution w/ df=10 is : ", np.mean(chi2)) print("Mean of Chi-square Distribution w/ df=20 is : ", np.mean(chi3)) print("Mean of Chi-square Distribution w/ df=30 is : ", np.mean(chi4)) np.var(chi1, ddof=1) np.var(chi2, ddof=1) np.var(chi3, ddof=1) np.var(chi4, ddof=1) print("Variance of Chi-square Distribution w/ df=5 is : ", np.var(chi1, ddof=1)) print("Variance of Chi-square Distribution w/ df=10 is : ", np.var(chi2, ddof=1)) print("Variance of Chi-square Distribution w/ df=20 is : ", np.var(chi3, ddof=1)) print("Variance of Chi-square Distribution w/ df=30 is : ", np.var(chi4, ddof=1)) fig = plt.figure(figsize=(14,7)) # 두 개의 그래프를 한 페이지에 그림 fig, axs = plt.subplots(ncols=2) fig, ays = plt.subplots(ncols=2) sns.histplot(data=chi1, x=None, ax=axs[0]).set(title='Histogram of Chi-square w/ df=5 & df=10)') sns.histplot(data=chi2, x=None, ax=axs[1]) fig.subplots_adjust(wspace=0.5) # 우측그림의 좌우 간격을 조정 #plt.show() #plt.savefig('C:/BOOK/PyBasics/PyStat/code/chi-1.png') sns.histplot(data=chi3, x=None, ax=ays[0]).set(title='Histogram of Chi-square w/ df=20 & df=30)') sns.histplot(data=chi4, x=None, ax=ays[1]) fig.subplots_adjust(wspace=0.5) # 우측그림의 좌우 간격을 조정 plt.show() #plt.savefig('C:/BOOK/PyBasics/PyStat/code/chi-2.png')