요약 통계

Python으로 배우는 Monte Carlo 시뮬레이션

Izzy Weber

Curriculum Manager, DataCamp

연령별 결과

연령 1사분위수와 4사분위수 집단의 결과 차이는 무엇인가요? 시뮬레이션 결과의 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...