Hi Johannes,

I came up with the following (assuming that if step[i] is independent of 
step[i-1] then it is also independent of -step[i-1]):

Cheers  Joe

> # your (unbounded) random walk
> k <- cumsum(c(0,sample(c(-1,1), 1000, rep = T)))
>
> #shift it to positive, to allow following calculations k <- k - min(k) 
> # set your bound (remember min(k) is now zero) wid <- 10
>
> # fold the walk back on itself with 'creases' at wid, 2*wid, 3*wid, ...
> walls <- ifelse((k%/%wid)%%2==1,
>              # if k[i] = a*wid + b, with a odd and b < wid,
>               # invert it and map it to (0, wid)
>               (1+(k%/%wid))*wid - k,
>               # if k[i] = 2a*wid + b, for b < wid, subtract 2a*wid
>               k - wid*(k%/%wid))
>
> # plot the original walk, and overlay the folded one.
> plot(k, ty = 'l')
> lines(walls, col = 'red')
>
> # shift the walk to be centred around 0.  **The walk doesn't start at 
> zero any more** walls <- walls - wid/2
>
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Charles C. Berry
Sent: Thursday, 25 October 2007 4:18 AM
To: Johannes Hüsing
Cc: [EMAIL PROTECTED]
Subject: Re: [R] random walk w/ reflecting boundary: avoid control construct?

On Wed, 24 Oct 2007, Johannes Hüsing wrote:

> Dear expeRts,
> recently I asked for a nice way to re-program a problem without using 
> control constructs such as "for" or "sapply(1:length(x), ...". Is 
> there a way to program a random walk with a reflecting boundary 
> without resorting to such constructs? A working solution is
>
> ranwalk <- function(length, bound) {
>    k <- cumsum(sample(c(-1, 1), length, replace=TRUE))
>    while( any(abs(k) > bound) ) {
>        ri <- min(which(abs(k) > bound))
>        k[ri:length] <- k[ri:length] - 2 * sign(k[ri])
>    }
>    k
> }
>
> but it uses "while" and has the same expression in the "while" 
> statement and the following line. Is there a sensible way to reprogram 
> it using the "whole object"
> approach?

Does this satisfy your 'sensible' sensibility?

ranwalk2 <-
  function(length., bound) {
       k <- cumsum(sample(c(-1, 1), length., replace=TRUE))
       lk <- rep( c( 0:bound, (bound-1):(-bound), (1-bound):(-1)),
                        length=max(abs(k))+1)
       sign(k)*lk[ 1 + abs(k) ]
}

HTH,

  Chuck

>
> ______________________________________________
> R-help@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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:[EMAIL PROTECTED]                  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901


------IMPORTANT - This message has been issued by The Department of 
Agriculture, Fisheries and Forestry (DAFF). The information transmitted is for 
the use of the intended recipient only and may contain confidential and/or 
legally privileged material. It is your responsibility to check any attachments 
for viruses and defects before opening or sending them on. 

Any reproduction, publication, communication, re-transmission, disclosure, 
dissemination or other use of the information contained in this e-mail by 
persons or entities other than the intended recipient is prohibited. The taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you have received this e-mail in 
error please notify the sender and delete all copies of this transmission 
together with any attachments. If you have received this e-mail as part of a 
valid mailing list and no longer want to receive a message such as this one 
advise the sender by return e-mail accordingly. Only e-mail correspondence 
which includes this footer, has been authorised by DAFF 
------

______________________________________________
R-help@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.

Reply via email to