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.
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 <tynashy at 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 at 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. >
I am new to R coding and I am trying to model the returns on the ftse100 since 1990. I have got a vector with all the closing values on each trading day. however, instead of using the difference in the closing values of two consecutive days, (ie dx=diff(x) where x is the vector containing the closing values), i wanted to use the quotient of the two closing values. I have tried the following without any luck> x = c (2434.1 2463.7 2451.6 2444.5 2431.3 2436.3 2412.6 2417.9 2380.1 > 2366.22349.1 2373.9 2336.9 2335.0 2297.1 2291.1 2278.6 2289.9 2314.5 2328.8 2322.0 2337.3 2345.8 2355.1 2348.4 2321.1 2307.4 2331.0 2313.6 2286.9 2293.2 2298.3 2313.8 2325.9 2297.1 2277.0 2259.7 2269.2 2236.7 2249.3 2254.8 2255.4 2238.4 2254.8 2230.5 2216.0 2230.3 2250.0 2234.3 2222.8 2224.5 2226.1 2234.9 2263.9 2238.0 2259.7 2250.3 2258.9 2283.9 2298.2 2266.2 2275.0 2263.0 2247.9 2221.6 2240.7 2231.6 2239.5 2221.1) #extraction of the stock index returns> n=length(x) > d=diff (log(x)) # daily log returns> for (i in 2:n) {+ dx[i]=(d[i])/(d[i-1]) + delta=dx[i] + } # this is what I have tried to do. how do i do this properly in R please. -- View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933738.html Sent from the R help mailing list archive at Nabble.com.
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, <tynashy at 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 <tynashy at 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 at 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 at 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 >