[R-SIG-Finance] Speed issue issue with periodReturn

Jeff Ryan jeff.a.ryan at gmail.com
Thu Jul 30 04:21:53 CEST 2009

I suspect we live in marginally different worlds - as 0.05 seems
'trivial' to me.

While all this software is "free" I'd like to reiterate that it
actually costs someone (usually) lots of time.

A better/helpful post could include some effort to identify a
bottleneck, or maybe show what you've accomplished to make it better.

The above said, a few suggestions:

In one CPU hour (10 cents on Amazon?) you could do periodReturn 60,000 times.

Assuming an hour to better the performance by 50%, and at a price of
to code of say $100/hr you'd need to plan on using the new function
120,000,000 times.  These estimates are also quite unrealistic..
300,000,000 times might be closer.  That is one heck of a universe of

If you find yourself calling the function over and over, saving the
results might be useful too.

Part of the above <rant> was to illustrate that there is no free lunch
--- even in the land of free beer.

That said, periodReturn was meant to be easy to use first, and fast a
distant second.


> t1 <- system.time(weeks2 <- diff(log(Cl(GOOG)[endpoints(GOOG,'weeks')]))['2007/'])
> t2 <- system.time(weeks<-periodReturn(GOOG,period="weekly",type='log',subset="2007/"))
> (t2/t1)[3]
> all.equal(coredata(weeks),coredata(weeks2),check.attributes=FALSE)
[1] TRUE

The direct route is 20x faster.

As it turns out, I am refactoring the internal to.period code to be
much faster/memory efficient.  The original was Fortran, which while
very fast, suffers from a lot of required copying in R, so the code is
changing (originally from 2007).  I'll probably also think about
making the simple cases use something more along the direct route.


On Wed, Jul 29, 2009 at 5:06 PM, Ian Coe<ICoe at connectcap.com> wrote:
> Hi,
>  I noticed that the periodReturn function seems to a take non-trivial
> amount of time to compute weekly returns.
>  The calls below all compute the log returns from 1/1/2007 to now and
> they take slightly different amounts of time.
>  I'd like to be able to compute weekly returns as fast as possible.
> Does anyone have suggestions for minimizing the processing time?
> Different data type?  Different function to call?
> Thanks,
> Ian
>> yhoo=getSymbols("GOOG",source="yahoo",from = "01-01-2004")
>> system.time(weeks<-periodReturn(GOOG,period="weekly",type='log',
> subset="2007::"))
>   user  system elapsed
>   0.06    0.00    0.06
>> yhoo=getSymbols("GOOG",source="yahoo",from = "01-01-2007")
>> system.time(weeks<-periodReturn(GOOG,period="weekly",type='log',
> subset="2007::"))
>   user  system elapsed
>   0.04    0.00    0.07
>> yhoo=getSymbols("GOOG",source="yahoo")
>> system.time(weeks<-periodReturn(GOOG,period="weekly",type='log'))
>   user  system elapsed
>   0.05    0.00    0.05
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.

Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics

More information about the R-SIG-Finance mailing list