mardi 20 juillet 2021

Efficient block bootstrap of integer sequences

I'm trying to block bootstrap samples for Monte-Carlo simulation and need to generate a large array of index values (integers) containing blocks in Python. I need this to be very fast but cannot figure out how to vectorize it.

I want to generate a large number of paths, where each path contains a sequence of integers of length L. Suppose I have an array of integers (representing an index) form 0 to N, from which I will sample randomly to construct each path. When I sample, I choose a random integer i from 0 to N, and then populate the path with i,i+1,i+2..,i+w for some window w. I then choose another random starting index value and continue to populate the path with the new window, repeating until the path is fully populated. I do this for all paths.

I'm wondering if there is a way to speed this method up without having to loop over each path, since I intend to generate a very large number of paths (millions)

An example of my for loop method is below:

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) 



Aucun commentaire:

Enregistrer un commentaire