Building a random walk vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Building a random walk vector

Matthew Wilson-2
I'm studying R in my free time.  I want to build a vector where each
element is equal to the element before it in the sequence plus some
random tweak.

In python, I would write:

vec = [100] * 50 # make a 50-element list with each element set to 100
from random import randint
for i, v in enumerate(vec):
    if i is not 0: # if we're not on the first element
        vec[i] = vec[i-1] + randint(-2, 2)

I suspect R has some fancier way of doing this.  How to?

TIA


--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Building a random walk vector

Ben Bolker
Matthew Wilson <matt <at> overlook.homelinux.net> writes:

>
> I'm studying R in my free time.  I want to build a vector where each
> element is equal to the element before it in the sequence plus some
> random tweak.
>

  You will probably get many answers to this, but
I think

vec <- 100+c(0,cumsum(runif(49,-2,2)))

works.

  Ben Bolker

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Building a random walk vector

Duncan Murdoch
In reply to this post by Matthew Wilson-2
On 8/3/2006 9:17 PM, Matthew Wilson wrote:

> I'm studying R in my free time.  I want to build a vector where each
> element is equal to the element before it in the sequence plus some
> random tweak.
>
> In python, I would write:
>
> vec = [100] * 50 # make a 50-element list with each element set to 100
> from random import randint
> for i, v in enumerate(vec):
>     if i is not 0: # if we're not on the first element
>         vec[i] = vec[i-1] + randint(-2, 2)
>
> I suspect R has some fancier way of doing this.  How to?

Assuming randint(-2, 2) gives a value uniform on (-2, 2) a quick way to
do what you want is

vec <- 100 + c(0, cumsum(runif(49, -2, 2)))

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Building a random walk vector

Dirk Eddelbuettel
In reply to this post by Matthew Wilson-2

On 4 August 2006 at 01:17, Matthew Wilson wrote:
| I'm studying R in my free time.  I want to build a vector where each
| element is equal to the element before it in the sequence plus some
| random tweak.
|
| In python, I would write:
|
| vec = [100] * 50 # make a 50-element list with each element set to 100
| from random import randint
| for i, v in enumerate(vec):
|     if i is not 0: # if we're not on the first element
|         vec[i] = vec[i-1] + randint(-2, 2)
|
| I suspect R has some fancier way of doing this.  How to?

Yup, cumsum() is your friend. You only need the first scalar of 100,
vectorisation does the rest. Try

> set.seed(12345)
> Z <- 100 + cumsum(runif(50,-2,2))
> summary(Z)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  98.54  100.80  102.30  102.10  103.40  105.70
> plot(Z, type='l')

Lastly, I think N(0, some_sd) is more customary that U(-2,2) but that is easy
to change.

Cheers, Dik

--
Hell, there are no rules here - we're trying to accomplish something.
                                                  -- Thomas A. Edison

______________________________________________
[hidden email] 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.