前提
for文を使って、正規分布、t分布(自由度3)、t分布(自由度10)のグラフを順番に表したいです。
今は一つ一つのコードを書いて表示させています。
二重For文を使えば良いのでしょうか?
実現したいこと
- グラフを順番に表示させる
- グラフによってy軸の範囲も違うのでグラフによって範囲を変えたい
発生している問題・エラーメッセージ
エラーメッセージ
python
from scipy.stats import norm, skew, kurtosis, chi2, uniform, t import numpy as np import matplotlib.pyplot as plt n = [10, 20, 30, 50, 100, 300, 500]#樣本數 N = 10000#實驗次數 alfa = 0.05 def My_JB(X): n = X.shape[0] G1 = np.sqrt(n / 6) * skew(X) G2 = np.sqrt(n / 24) * (kurtosis(X)) JB = G1 **2 + G2 **2 p_val = 1-chi2.cdf(JB,df=2) return JB, p_val power = np.zeros(len(n)) for i in range(len(n)): X = norm.rvs(loc = 0, scale =1, size=(n[i],N)) JB_stat, p_val = My_JB(X) power[i] = (p_val <= alfa).mean() print("Power={}" .format(power)) figure, ax= plt.subplots(1) plt.plot(np.arange(len(n)), power, marker = 's') ax.set_xticks(np.arange(len(n))) ax.set_xticklabels(n) ax.set_xlabel('Sample size'), ax.set_ylabel('Power') ax.set_title('The range of $x$ is not appropriate') plt.grid(True) plt.ylim([0.00,0.07]) plt.show() for i in range(len(n)): X = t.rvs(df = 3 ,size=(n[i],N)) JB_stat, p_val = My_JB(X) power[i] = (p_val <= alfa).mean() print("Power={}" .format(power)) figure, ax= plt.subplots(1) plt.plot(np.arange(len(n)), power, marker = 's') ax.set_xticks(np.arange(len(n))) ax.set_xticklabels(n) ax.set_xlabel('X'), ax.set_ylabel('f(x)') ax.set_title('The range of $x$ is not appropriate') plt.grid(True) plt.ylim([0.00,1.10]) plt.show() for i in range(len(n)): X = t.rvs(df = 10 ,size=(n[i],N)) JB_stat, p_val = My_JB(X) power[i] = (p_val <= alfa).mean() print("Power={}" .format(power)) figure, ax= plt.subplots(1) plt.plot(np.arange(len(n)), power, marker = 's') ax.set_xticks(np.arange(len(n))) ax.set_xticklabels(n) ax.set_xlabel('X'), ax.set_ylabel('f(x)') ax.set_title('The range of $x$ is not appropriate') plt.grid(True) plt.ylim([0.00,1.10]) plt.show()
試したこと
二重for文を使って試したがうまくいかなかった
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。