如何解决整数序列的高效块引导
我正在尝试阻止用于 Monte-Carlo 模拟的 bootstrap 样本,并且需要在 Python 中生成包含块的大型索引值(整数)数组。我需要它非常快,但无法弄清楚如何对其进行矢量化。
我想生成大量的路径,其中每条路径包含一个长度为L的整数序列。假设我有一个从0到N的整数数组(代表一个索引),我将从中随机采样构建每条路径。当我采样时,我选择一个从 0 到 N 的随机整数 i,然后用 i,i+1,i+2..,i+w 填充路径以用于某个窗口 w。然后我选择另一个随机起始索引值并继续用新窗口填充路径,重复直到路径完全填充。我对所有路径都这样做。
我想知道是否有一种方法可以在不必遍历每条路径的情况下加快此方法的速度,因为我打算生成大量路径(数百万条)
我的 for 循环方法示例如下:
paths = 10000
path_length = 500
window_length = 5
index = np.arange(0,5000)
simulated_values = np.zeros([paths,path_length])
n_windows = int(np.ceil(path_length/window_length))
for i in range(0,paths):
temp=[]
for n in range(0,n_windows):
random_start = random.randint(0,len(index) - path_length)
temp.extend(range(random_start,random_start + window_length))
simulated_values[i,:] = temp
print(simulated_values)
解决方法
我在名为 recombinator 的 python 包中找到了一个解决方案。好像够快了,有GPU支持,速度更快
https://pypi.org/project/recombinator/
from recombinator.block_bootstrap import circular_block_bootstrap
index = np.arange(0,5000)
path_length = 500
window_length = 5
temp = circular_block_bootstrap(index,block_length=window_length,replications=1000000,replace=True,sub_sample_length=path_length)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。