performance - Speeding up an Euler numerical scheme in python -
let's have update scheme looks this:
import numpy np n = 1000 dt = 0.01 x = np.zeros(n) x[0] = 0.5 in xrange(1, n): rand = np.random.normal(loc=0.,scale=1.) x[i] = x[i-1]*(1 + dt + np.sqrt(dt)*rand)
what best strategy speeding code of form, current array element needs previous array element make calculation?
i attempting put vectorised form i'm bit stuck how use trailing array element update current one.
if there better solutions don't involve vectorisation i'm open too.
try this:
x = np.random.randn(1000) x += 1 x[0] = 0.5 y = np.cumprod(x)
answer in y
Comments
Post a Comment