How do I fix an off by one error for my for loop?

python_person

I'm having trouble with a for loop. It's giving me an "off by one error." I did count the observations and it was indeed one off. How do I fix it?

Code:

delta = seq(0,1,0.05)
delta2 = seq(0,1,0.01)
delta3 = seq(0,1,0.001)

price1 = rep(1,length(delta))
price2 = rep(1,length(delta2))
price3 = rep(1,length(delta3))

for(i in 2:length(delta)){
  price1[i] = price1[i-1]+price1[i-1]*rnorm(1)*sqrt(delta)
  price1[i+1] = price1[i] + i
  print(price1[i+1])
}


plot(delta,price1[i+1], type = "l") #Error occurs here

for(i in 2:length(delta2)){
  price2[i] = price2[i-1] + price2[i-1]*rnorm(1)*sqrt(delta2)
  price2[i] = price2[i] + i
  print(price2[i])
}

plot(delta2, price2[i], type = "l") #error here

for(i in 2:length(delta3)){
  price3[i] = price3[i-1] +  price3[i-1]*rnorm(1)*sqrt(delta3)
  xprice3[i] = price3[i] + i
  print(price3[i])
}

plot(delta3, price3[i], type = "l") #error here



The error states:

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

Ronak Shah

Probably, you are looking for :

delta = seq(0,1,0.05)
delta2 = seq(0,1,0.01)
delta3 = seq(0,1,0.001)

price1 = rep(1,length(delta))
price2 = rep(1,length(delta2))
price3 = rep(1,length(delta3))

for(i in 2:length(delta)){
   price1[i] = price1[i-1]+price1[i-1]*rnorm(1)*sqrt(delta[i])
}
plot(delta,price1, type = "l")


for(i in 2:length(delta2)){
   price2[i] = price2[i-1]+price2[i-1]*rnorm(1)*sqrt(delta2[i])
}
plot(delta2,price2, type = "l")

for(i in 2:length(delta3)){
  price3[i] = price3[i-1]+price3[i-1]*rnorm(1)*sqrt(delta3[i])
}
plot(delta3,price3, type = "l")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I find a fix for my runtime int casting error?

How do I fix my random number while loop?

How to fix off-by-one issue in for-loop

How do I fix this problem with While and For loop?

How do I fix the indentation error in my code?

How do I fix this syntax error in my game?

How do I fix my sequalize connection error?

How do I fix my For loop and Random problem in Android?

How do I fix this design error on my log in page?

How do I fix my code for my recursive countdown python function so that it only prints “LIFT OFF!’ once?

How do I fix this weird while loop error?

How do I fix this for loop that takes a value in one column to change one in another?

how do I fix my code in order to display one table?

How do I fix this python loop?

How do I fix this manifest error in my ChueckSUR.log?

Why is my while going in a infinity loop? How do I fix it?

How to fix the logical error in my while loop?

Logical error while running scanf in for loop, how do i fix it?

How to fix this loop error in my Code (Loop Valuing Error)

I am getting an infinite loop when I run my program. How do I fix it?

How do I fix this error in my Django settings.py?

How do i fix my class code 'int' object error?

How do I fix my golang import cycle error

how do i fix this subprocess error in my code

C and MinGW: How do I fix my "No such file or directory" error?

How do I fix my current openGL error, my cmakelist may be my issue

How do I fix this syntax error on my Try and Except

I am struggling with a for loop for an array. How do I fix my java code?

How can I fix 'continue not properly in loop' error in my Python while loop?