摘要統計

Python 的 Monte Carlo 模擬

Izzy Weber

Curriculum Manager, DataCamp

年齡與結果

第一與第四個年齡分位的族群,結果差多少? 模擬結果中 Age 變數的長條圖

Python 的 Monte Carlo 模擬

年齡與結果

print(np.quantile(df_summary["age"], 0.25))
print(np.quantile(df_summary["age"], 0.75))
39.78584463094688
57.52092642086441
Python 的 Monte Carlo 模擬

年齡與結果

age_q25 = np.quantile(df_summary["age"], 0.25)
age_q75 = np.quantile(df_summary["age"], 0.75)


mean_age_q75_outcome = np.mean(df_summary[df_summary["age"] > age_q75]["predicted_y"]) mean_age_q25_outcome = np.mean(df_summary[df_summary["age"] < age_q25]["predicted_y"])
mean_age_q75_outcome - mean_age_q25_outcome
34.09429663553621
Python 的 Monte Carlo 模擬

依年齡與 bmi 的結果差異

  • bmi 與年齡第 1 與第 4 分位的族群,結果差多少?
  • 這個差異的 95% 信賴區間與標準差是多少?
Python 的 Monte Carlo 模擬

依年齡與 bmi 的結果差異

y_diffs = []
for i in range(1000):
    simulation_results = st.multivariate_normal.rvs(mean=mean_dia, size=1000, cov=cov_dia)
    df_results = pd.DataFrame(simulation_results, columns=["age", "bmi", "bp", "tc", "ldl", 
                                                           "hdl", "tch", "ltg", "glu"])

predicted_y = regr_model.predict(df_results) df_y = pd.DataFrame(predicted_y, columns=["predicted_y"]) df_sum = pd.concat([df_results, df_y], axis=1)
age_q25 = np.quantile(df_sum["age"], 0.25) age_q75 = np.quantile(df_sum["age"], 0.75) bmi_q25 = np.quantile(df_sum["bmi"], 0.25) bmi_q75 = np.quantile(df_sum["bmi"], 0.75)
q75_outcome = np.mean(df_sum[(df_sum["bmi"] > bmi_q75) & (df_sum["age"] > age_q75)]["predicted_y"]) q25_outcome = np.mean(df_sum[(df_sum["bmi"] < bmi_q25) & (df_sum["age"] < age_q25)]["predicted_y"])
y_diff = q75_outcome - q25_outcome y_diffs.append(y_diff)
Python 的 Monte Carlo 模擬

依年齡與 bmi 的結果差異

  • 平均值:
    np.mean(y_diffs)
    
132.4948511247819
  • 95% 信賴區間:
    np.quantile(y_diffs,0.025)
    
120.73340800299707
np.quantile(y_diffs,0.975)
144.1344994507557
  • 標準差:
    np.std(y_diffs)
    
5.9322225537128
  • 結果的長條圖:
    sns.histplot(y_diffs)
    
    y_diffs 的長條圖
Python 的 Monte Carlo 模擬

一起來練習吧!

Python 的 Monte Carlo 模擬

Preparing Video For Download...