

Hi all,
Thanks a lot for anyone's help in advance.
I am trying to find a way to compute the daytoday return (log return) from
a n x r matrix containing, n different stocks and price quotes over r days.
The time series of prices are already split by using unstack function.
For the result, I would like to see a n x (r1) matrix, where by each entry
is the dayoverday return of each stock.
I tried to look into the zoo package, however it seems to give only the
plots but not the actual data.
Would apply function work in this case?
Thanks a lot!
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hope this helps
a < matrix(runif(150),nrow=3,ncol=50)
p2r < function(x) 100 * diff(log(x))
t(apply(a,1,function(x){p2r(c(x))}))
On Mon, Jun 7, 2010 at 8:41 AM, Anyi Zhu < [hidden email]> wrote:
> Hi all,
>
>
>
> Thanks a lot for anyone's help in advance.
>
>
>
> I am trying to find a way to compute the daytoday return (log return)
> from
> a n x r matrix containing, n different stocks and price quotes over r days.
> The time series of prices are already split by using unstack function.
>
>
>
> For the result, I would like to see a n x (r1) matrix, where by each entry
> is the dayoverday return of each stock.
>
>
>
> I tried to look into the zoo package, however it seems to give only the
>
plots but not the actual data.
>
take a look at
vignette("zooquickref",package="zoo")
It gives an exact solution to your problem
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Actually the 'apply' is not necessary.
The original poster has stocks as rows
rather than the customary columns, so
the following should suffice:
retmat < diff(log(t(pricemat)))
Questions that are specifically financial
should be sent to rsigfinance (you need
to subscribe before posting).
On 07/06/2010 07:12, sayan dasgupta wrote:
> Hope this helps
>
> a< matrix(runif(150),nrow=3,ncol=50)
> p2r< function(x) 100 * diff(log(x))
> t(apply(a,1,function(x){p2r(c(x))}))
>
>
>
>
> On Mon, Jun 7, 2010 at 8:41 AM, Anyi Zhu< [hidden email]> wrote:
>
>> Hi all,
>>
>>
>>
>> Thanks a lot for anyone's help in advance.
>>
>>
>>
>> I am trying to find a way to compute the daytoday return (log return)
>> from
>> a n x r matrix containing, n different stocks and price quotes over r days.
>> The time series of prices are already split by using unstack function.
>>
>
>>
>>
>> For the result, I would like to see a n x (r1) matrix, where by each entry
>> is the dayoverday return of each stock.
>>
>>
>>
>> I tried to look into the zoo package, however it seems to give only the
>>
> plots but not the actual data.
>>
> take a look at
> vignette("zooquickref",package="zoo")
> It gives an exact solution to your problem
>
>
>
>>
>>
>>
>> Would apply function work in this case?
>>
>>
>>
>> Thanks a lot!
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide
>> http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>

Patrick Burns
[hidden email]
http://www.burnsstat.com(home of 'Some hints for the R beginner'
and 'The R Inferno')
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


You can use diff.zoo like this:
library(zoo)
z < zoo(matrix(1:24, 6))
z
diff(log(z))
# also try
diff(z, arith = FALSE)  1
See ?diff.zoo
and read the three zoo vignettes (pdf documents):
vignette(package = "zoo") # lists them
vignette("zoo")
etc.
On Sun, Jun 6, 2010 at 11:11 PM, Anyi Zhu < [hidden email]> wrote:
> Hi all,
>
>
>
> Thanks a lot for anyone's help in advance.
>
>
>
> I am trying to find a way to compute the daytoday return (log return) from
> a n x r matrix containing, n different stocks and price quotes over r days.
> The time series of prices are already split by using unstack function.
>
>
>
> For the result, I would like to see a n x (r1) matrix, where by each entry
> is the dayoverday return of each stock.
>
>
>
> I tried to look into the zoo package, however it seems to give only the
> plots but not the actual data.
>
>
>
> Would apply function work in this case?
>
>
>
> Thanks a lot!
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Thanks a lot Sayan, I'll give it a try and let you know how it goes.
From: sayan dasgupta [mailto: [hidden email]]
Sent: June0710 2:13 AM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [R] Computing dayoverday log return for a matrix containing
multiple time series
Hope this helps
a < matrix(runif(150),nrow=3,ncol=50)
p2r < function(x) 100 * diff(log(x))
t(apply(a,1,function(x){p2r(c(x))}))
On Mon, Jun 7, 2010 at 8:41 AM, Anyi Zhu < [hidden email]> wrote:
Hi all,
Thanks a lot for anyone's help in advance.
I am trying to find a way to compute the daytoday return (log return) from
a n x r matrix containing, n different stocks and price quotes over r days.
The time series of prices are already split by using unstack function.
For the result, I would like to see a n x (r1) matrix, where by each entry
is the dayoverday return of each stock.
I tried to look into the zoo package, however it seems to give only the
plots but not the actual data.
take a look at
vignette("zooquickref",package="zoo")
It gives an exact solution to your problem
Would apply function work in this case?
Thanks a lot!
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


I did not realise making a zoo object is that convenient. Thanks a lot
Gabor.
Original Message
From: Gabor Grothendieck [mailto: [hidden email]]
Sent: June0710 5:20 AM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [R] Computing dayoverday log return for a matrix containing
multiple time series
You can use diff.zoo like this:
library(zoo)
z < zoo(matrix(1:24, 6))
z
diff(log(z))
# also try
diff(z, arith = FALSE)  1
See ?diff.zoo
and read the three zoo vignettes (pdf documents):
vignette(package = "zoo") # lists them
vignette("zoo")
etc.
On Sun, Jun 6, 2010 at 11:11 PM, Anyi Zhu < [hidden email]> wrote:
> Hi all,
>
>
>
> Thanks a lot for anyone's help in advance.
>
>
>
> I am trying to find a way to compute the daytoday return (log return)
from
> a n x r matrix containing, n different stocks and price quotes over r
days.
> The time series of prices are already split by using unstack function.
>
>
>
> For the result, I would like to see a n x (r1) matrix, where by each
entry
> is the dayoverday return of each stock.
>
>
>
> I tried to look into the zoo package, however it seems to give only the
> plots but not the actual data.
>
>
>
> Would apply function work in this case?
>
>
>
> Thanks a lot!
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


> Original Message
> From: [hidden email]
> [mailto: [hidden email]] On Behalf Of sayan dasgupta
> Sent: Sunday, June 06, 2010 11:13 PM
> To: [hidden email]
> Cc: [hidden email]
> Subject: Re: [R] Computing dayoverday log return for a
> matrix containingmultiple time series
>
> Hope this helps
>
> a < matrix(runif(150),nrow=3,ncol=50)
> p2r < function(x) 100 * diff(log(x))
> t(apply(a,1,function(x){p2r(c(x))}))
The following expressions compute the same
thing as that t(apply(...)) does:
100 * (log(a[,1])  log(a[,ncol(a)]))
or
100 * log(a[,1]/a[,ncol(a)])
The [,1] (all but the first column) and
[,ncol(a)] (all but the last column) is
a way to work with lagged data.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
>
>
>
> On Mon, Jun 7, 2010 at 8:41 AM, Anyi Zhu < [hidden email]> wrote:
>
> > Hi all,
> >
> >
> >
> > Thanks a lot for anyone's help in advance.
> >
> >
> >
> > I am trying to find a way to compute the daytoday return
> (log return)
> > from
> > a n x r matrix containing, n different stocks and price
> quotes over r days.
> > The time series of prices are already split by using
> unstack function.
> >
>
> >
> >
> > For the result, I would like to see a n x (r1) matrix,
> where by each entry
> > is the dayoverday return of each stock.
> >
> >
> >
> > I tried to look into the zoo package, however it seems to
> give only the
> >
> plots but not the actual data.
> >
> take a look at
> vignette("zooquickref",package="zoo")
> It gives an exact solution to your problem
>
>
>
> >
> >
> >
> > Would apply function work in this case?
> >
> >
> >
> > Thanks a lot!
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide
> > http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

