贝叶斯自举法BayesianBootstrap(6)

2023-04-29 来源:飞速影视
在这种情况下,这两个过程都给出了非常相似的结果。 这两个分布非常接近,而且估计量的估计平均值和标准偏差几乎相同,与我们选择的自举无关。
那么哪个过程更快呢?
import time
def compare_time(df, boot1, boot2, estimator, K=1000):
t1, t2 = np.zeros(K), np.zeros(K)
for k in range(K):
# Classic bootstrap
start = time.time()
boot1(df, estimator)
t1[k] = time.time() - start
# Bayesian bootstrap
start = time.time()
boot2(df, estimator)
t2[k] = time.time() - start
print(f"Bayes wins {np.mean(t1 > t2)*100}% of the time (by {np.mean((t1 - t2)/t1*100):.2f}%)")
结果如下:
Bayes wins 97.8% of the time (by 66.46%)
贝叶斯自举快了很多。
2、没有权重怎么办? 也没问题
如果我们有一个不接受权重的估计量,例如中位数? 我们可以进行两级抽样:我们采样权重,然后根据权重采样观测值。
def twolv_boot(df, estimator, seed=1):
np.random.seed(seed)
w = np.random.dirichlet(np.ones(len(df))*4, 1)[0]
df_boot = df.sample(n=len(df)*10, replace=True, weights=w, random_state=seed)
result = estimator(df_boot)
return result
看看结果:
np.random.seed(1)
X = pd.Series(np.random.normal(0, 10, 1000))
相关影视
合作伙伴
本站仅为学习交流之用,所有视频和图片均来自互联网收集而来,版权归原创者所有,本网站只提供web页面服务,并不提供资源存储,也不参与录制、上传
若本站收录的节目无意侵犯了贵司版权,请发邮件(我们会在3个工作日内删除侵权内容,谢谢。)

www.fs94.org-飞速影视 粤ICP备74369512号