[R] Looking for simpler solution to probabilistic question

Berwin A Turlach berwin at maths.uwa.edu.au
Tue Jan 15 14:28:44 CET 2008


G'day Rainer,

On Tue, 15 Jan 2008 14:24:08 +0200
Rainer M Krug <R.M.Krug at gmail.com> wrote:

> I have two processes which take with a certain probability (p1 and
> p2) x number of years to complete (age1 and age2). As soon as thge
> first process is completed, the second one begins. I want to
> calculate the time it takes for the both processes to be completed.
> 
> I have the following script which gives me the answer, butI think
> there must be a more elegant way of doing the calculations between
> the #####

The code between the #### together with the first line after the second
#### could be just shortened to:

> ager <- range(age1) + range(age2)
> ager <- ager[1]:ager[2]
> pp1 <- c(cumsum(p1), rev(cumsum(rev(p1))))
> pp2 <- c(cumsum(p2[-21]), rev(cumsum(rev(p2)))[-1])
> pr <- pp1+pp2
> pr <- pr/sum(pr)

> all.equal(p, pr)
[1] TRUE
> all.equal(age, ager)
[1] TRUE


If this is more elegant is probably in the eye of the beholder, but it
should definitely use less memory. :)

BTW, I am intrigued, in which situation does this problem arise?  The
time it takes the second process to finish seems to depend in a curious
way on the time it took the first process to complete.....

Cheers,

	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba




More information about the R-help mailing list