Your code is not runnable, it looks like it uses global variables (grids
), it allocates lots of small temporary arrays (e.g. thr_four_y
, prob_3_success
, output_2_success[q,mq,ϵ_1,ϵ_2] * prob_3_success
, and many many others) in the inner loops, it assigns to slices (e.g. output_4_success[q,mq,ϵ_1,ϵ_2,:,:]
) in the wrong order for locality, and so on… there are so many problems it is hard to comment on all of them.
You’ll get more detailed advice if you make a small (20 lines or so) runnable example that illustrates a simplified version of what you are trying to do, instead of pasting in a large-ish function like this.