贝叶斯自举法BayesianBootstrap(5)
2023-04-29 来源:飞速影视
result = estimator(df, weights=w)
return result
我们这里使用joblib 库并行化计算。
from joblib import Parallel, delayed
def bootstrap(boot_method, df, estimator, K):
r = Parallel(n_jobs=8)(delayed(boot_method)(df, estimator, seed=i) for i in range(K))
return r
最后,让我们写一个比较结果的函数。
def compare_boot(df, boot1, boot2, estimator, title, K=1000):
s1 = bootstrap(boot1, df, estimator, K)
s2 = bootstrap(boot2, df, estimator, K)
df = pd.DataFrame({"Estimate": s1 s2,
"Estimator": ["Classic"]*K ["Bayes"]*K})
sns.histplot(data=df, x="Estimate", hue="Estimator")
plt.legend([f"Bayes: {np.mean(s2):.2f} ({np.std(s2):.2f})",
f"Classic: {np.mean(s1):.2f} ({np.std(s1):.2f})"])
plt.title(f"Bootstrap Estimates of {title}")
现在开始比较:
compare_boot(X, classic_boot, bayes_boot, np.average, "Sample Mean")
![贝叶斯自举法BayesianBootstrap](http://fs.qqfile.top/re/aHR0cHM6Ly9waWNzMC5iYWlkdS5jb20vZmVlZC8wZmY0MWJkNWFkNmVkZGM0ZjgwODExZTVlOWNmNzhmNzUzNjYzM2U0LnBuZ0BmX2F1dG8_dG9rZW49NzAxYTNlMmIxM2ZkZjExZGY5MDU4MWVkYzkxMjgxY2I1)