vendredi 18 juin 2021

Error in list index when converting from R to python

I'm trying to convert my R code

set.seed(2021)
startn <- 10

j <- 1
maxj <- 10^5
n <- startn
time <- 0


while (n[j] > 0 & j < maxj){
   time[j+1] <- time[j] + rexp(1, rate=2*n[j]^2/5)  
   n[j+1] <- n[j] + sample(c(-1,1),1)
   j <- j+1
   }

plot(time, n, xlim=c(0,max(time)), ylim=c(0,max(n)))

into python

n_0 = 10

j = 0
maxj = 10^5
n = [n_0]
time = [0]


while n[j] > 0 & j < maxj:
    time[j+1] = time[j] + np.random.exponential(2*n[j]**2/5) 
    n[j+1] = n[j] + np.random.choice([-1,1])
    j = j+1

but failing with an error list assignment index out of range. Could someone point out my mistake?




Aucun commentaire:

Enregistrer un commentaire