dimanche 4 décembre 2022

Is there a way to make my 1D random walk code more time efficient here?

So my code plots the average distance from equilibrium of a 1D random walk over 1000 steps. My code works, but takes an inordinate amount of time, I think probably due to the loop inside a loop of the system. Is there a way to make this more efficient or am I stuck with this? Thanks :)

nsteps = 1000
ndim = 1
nwalkers = 100
numpy.seterr(invalid="ignore")

for i in range(100):
    w = walker(numpy.zeros(1))
    ys = w.doSteps(nsteps)
    avgpos = []
    
    for i in range(0, len(ys)):
        avgpos.append(sum(ys[:i+1])/i+1)
    
    plt.plot(range(nsteps+1),avgpos)

The ys are the results from doing n steps. I'm sure the inefficiency is from something within the loop rather than a problem in the earlier code




Aucun commentaire:

Enregistrer un commentaire