Try it yourself: x = seq(1, 11, by = 2)
diff(log(x)) log(x[-1]/x[-length(x)]) all.equal(diff(log(x)), log(x[-1]/x[-length(x)])) It seems like you don't really understand logs / log returns and why they are used by some in quant finance: might I suggest you read this: http://quantivity.wordpress.com/2011/02/21/why-log-returns/ My quick explanation is that log returns aggregate nicely over time while simple returns aggregate nicely over a portfolio. Thus, for a single instrument portfolio, log returns play very nicely with vectorized thinking in R. Michael On Mon, Oct 24, 2011 at 12:39 PM, <tyna...@yahoo.co.uk> wrote: > lets say >> x(t)=c(1,3,5,7,9,11) #value of the index at close of trading day > > does this mean these two are the same? >> d(t) = diff(log( x(t) ) and > d(t) = log ( x(t)/x(t-1) ) > > > <quote author='Michael Weylandt'> > Assuming that d(x) is equal to x, (I don't know a d() function in R) > these should be the same. > > log(a/b) = log(a) - log(b) = diff(log(c(a,b)) > > If you mean simple returns instead of continuous/log returns, perhaps try > this: > > x[-1]/x[-length(x)] - 1 > > Michael > > On Mon, Oct 24, 2011 at 11:44 AM, tynashy <tyna...@yahoo.co.uk> wrote: >> how do I code the following in R. I want to produce a vector where dx=log( >> (d(x))/(d(x-1)) ). I can do it for dx=diff(log(x)). I am learning/trying >> to >> model log returns of a stock market index. But instead of using the >> difference of the closing values of two consecutive days, i want to use >> the >> log of the quotient of the two days. any help is most appreciated. d is a >> vector of the closing values of the stock market index of length 5000. >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933588.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > </quote> > Quoted from: > http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933681.html > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.