Hi Michael
here are two variations of your loop, and both seem faster than the
original loop (on my computer).
require(rbenchmark)
## your function
loopRec - function(x, alpha){
n - length(x)
y - double(n)
for(i in 1:n){
y[i] - sum(cumprod(rep(alpha, i)) * rev(x[1:i]))
Dear Enrico,
Brilliant! Thank you for the improvements, not sure what I was thinking
using rev. I will test it out to see whether it is fast enough for our
implementation, but your help has been SIGNIFICANT!!!
Thanks heaps!
Michael
On 27/11/2011 10:43 a.m., Enrico Schumann wrote:
Hi
You can make things a lot faster by using the recurrence equation
y[n] = alpha * (y[n-1]+x[n])
loopRec - function(x, alpha){
n - length(x)
y - numeric(n)
if (n == 0) return(y)
y[1] - alpha * x[1]
for(i in seq_len(n)[-1]){
y[i] - alpha * (y[i-1] + x[i])
}
return(y)
}
Hi Florent,
That is great, I was working on solving the recurrence equation and this
was part of that equation. Now I know how to put everything together,
thanks for all the help e everybody!
Cheers,
On 27/11/2011 2:05 p.m., Florent D. wrote:
You can make things a lot faster by using the
You also might look at EMA() in the TTR package for a C implementation. (I
think it matches your problem but can't promise)
Michael
On Nov 27, 2011, at 9:05 AM, Michael Kao mkao006rm...@gmail.com wrote:
Hi Florent,
That is great, I was working on solving the recurrence equation and this
On Nov 27, 2011, at 9:25 AM, R. Michael Weylandt wrote:
You also might look at EMA() in the TTR package for a C
implementation. (I think it matches your problem but can't promise)
It's pretty close for EMA(1:1000, n=1, ratio=0.5) and 7 times faster.
EMA(1:10, n=1, ratio=0.5)
[1]
] generating a vector of y_t = \sum_{i = 1}^t (alpha^i * x_{t
- i + 1})
On Nov 27, 2011, at 9:25 AM, R. Michael Weylandt wrote:
You also might look at EMA() in the TTR package for a C
implementation. (I think it matches your problem but can't promise)
It's pretty close for EMA(1:1000, n=1
... but the original request was to build a series, not approximate
its limit by building a different (but faster) series.
What I suggested is linear in terms of the size of x so you won't find
a faster algorithm. What will make a difference is the implementation,
e.g. C loop versus R loop.
On
-help-boun...@r-project.org] On
Behalf Of Florent D.
Sent: Sunday, November 27, 2011 10:01 AM
To: David Winsemius
Cc: r-help@r-project.org
Subject: Re: [R] generating a vector of y_t = \sum_{i = 1}^t (alpha^i * x_{t
- i + 1})
... but the original request was to build a series, not approximate
of y_t = \sum_{i = 1}^t (alpha^i * x_{t
- i + 1})
On Nov 27, 2011, at 9:25 AM, R. Michael Weylandt wrote:
You also might look at EMA() in the TTR package for a C
implementation. (I think it matches your problem but can't promise)
It's pretty close for EMA(1:1000, n=1, ratio=0.5
On Nov 27, 2011, at 1:00 PM, Florent D. wrote:
... but the original request was to build a series, not approximate
its limit by building a different (but faster) series.
Right. Your function was faster that the earlier ones. But if speed
were the issue, it might make more sense to use
Dear R-help,
I have been trying really hard to generate the following vector given
the data (x) and parameter (alpha) efficiently.
Let y be the output list, the aim is to produce the the following
vector(y) with at least half the time used by the loop example below.
y[1] = alpha * x[1]
12 matches
Mail list logo