0

I have a function that I need to draw with R using a daily step of 1 for 3 years.

S(t)= S(0)exp(0.06t+0.20w(t))  #(1)
with S(0) =20
w(t) = standard Brownian movement

I am kinda blocked. I know this code should work but I don't know how to have the same length between my "t" and my w(t) length(t)=901 and length(w(t))=902. How to make them alike????

 t <- seq(0,900,length=901) 
 length(t)
v = matrix(rnorm(log(20)+0.06*t,sd=sqrt((0.20)^(2)*t)))
z = matrix(NA, ncol=900, nrow=5)
w = function(t)
{c(0,cumsum(v))}
length(w(t))
Bn <-log(20)+ 0.06*t +0.20*w(t)
Br <- log(Bn)
plot(t,Br,type="l",xlab="Temps") 

for (i in 1:5) # I need to draw 5 path of the function(1)
{z[i,] = c(0,cumsum(Br[i,]))}
dim(z)
u= apply(z,2,mean) #mean of the 5 path

plot(t,z[1,],xlab="temps",type="l",ylab="Movement Brownian")
for (i in 1:5){lines(t,z[i,])}
lines(t,u,lwd=2,col="red")

Please I need help. Thank you for your time.

alison monroe
  • 21
  • 1
  • 1
  • 7
  • 1
    I guess you should just put what you have into a function `w = function(x,pas=1) c(0,cumsum(rnorm(length(x)-1,sd=sqrt(pas))))`. – Frank Oct 29 '15 at 17:14
  • `t <- seq(0,900,length=901) pas <- 1 w = function(t,pas=1) {c(0,cumsum(rnorm(length(t)-1,sd=sqrt(pas))))} Br <- 20*exp(0.06*t +0.20*w) plot(t,Br,type="l",xlab="Temps")` I tried this and R said error because 0.20*w is a numeric argument. I am not good at this. Help – alison monroe Oct 30 '15 at 09:51
  • Oh, I wrote `w` as a function so that you could use it exactly as written in your question, like `w(t)`; or `w(t, pas=2)` if you decide to change that parameter; or `w(tt)`, if you need to use it again for another vector. – Frank Oct 30 '15 at 12:24
  • Thank you. It actually worked. – alison monroe Oct 30 '15 at 12:30

0 Answers0