Re: [R] Looking for simpler solution to probabilistic question

2008-01-15 Thread G. Jay Kerns
Dear Rainer,

If X1 is random on 1:20 with probabilities proportional to
dnorm(,10,2), and X2 is random on 10:30 with probs proportional to
dnorm(,15,1), and the object is to find out the probability
distribution of X1 + X2, then a very quick way is with package distr:

library(distr)
X1 <- DiscreteDistribution(1:20, prob = dnorm(1:20,10,2))
X2 <- DiscreteDistribution(10:30, prob = dnorm(10:30,15,1))
X <- X1 + X2

plot(X) # plot the probabilites

d(X)(11:50) # look at probs individually

Notice that the answer from distr is different from the answers posted
previously.  The answers will be the same if the line

p <- matrix(p1, ncol=length(p1), nrow=length(p2), byrow=TRUE) + p2

is replaced with

p <- matrix(p1, ncol=length(p1), nrow=length(p2), byrow=TRUE) * p2

(the only difference is the multiplication by p2 instead of addition
with p2).  This is following from an assumption of independence
between X1 and X2. Otherwise, you would seem to need to know the joint
distribution of X1 and X2, which isn't mentioned in your problem
description.  Is there additional information about the problem that
would suggest adding p2 rather than multiplying?

I hope this helps,
Jay







On Jan 15, 2008 8:33 AM, Rainer M Krug <[EMAIL PROTECTED]> wrote:
> Berwin A Turlach wrote:
> > G'day Rainer,
> >
> > On Tue, 15 Jan 2008 14:24:08 +0200
> > Rainer M Krug <[EMAIL PROTECTED]> wrote:
> >
>
> >
> >> 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. :)
>
> Thanks - interesting approach which is different to using the outer()
>
> >
> > 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.
>
> These are two growth processes, where the first one is seedling growth
> up to a certain X and the second one is adult growth from size X onwards
> until it reaches a given final size.
>
> I hope my calculations fit the process...
>
>
>
> >
> > Cheers,
> >
> >   Berwin
> >
> > === Full address =
> > Berwin A TurlachTel.: +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: [EMAIL PROTECTED]
> > Singapore 117546http://www.stat.nus.edu.sg/~statba
>
> __
> 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.
>



-- 



***
G. Jay Kerns, Ph.D.
Assistant Professor / Statistics Coordinator
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
E-mail: [EMAIL PROTECTED]
http://www.cc.ysu.edu/~gjkerns/

__
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.


Re: [R] Looking for simpler solution to probabilistic question

2008-01-15 Thread Rainer M Krug
Berwin A Turlach wrote:
> G'day Rainer,
> 
> On Tue, 15 Jan 2008 14:24:08 +0200
> Rainer M Krug <[EMAIL PROTECTED]> wrote:
> 

> 
>> 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. :)

Thanks - interesting approach which is different to using the outer()

> 
> 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.

These are two growth processes, where the first one is seedling growth 
up to a certain X and the second one is adult growth from size X onwards 
until it reaches a given final size.

I hope my calculations fit the process...


> 
> Cheers,
> 
>   Berwin
> 
> === Full address =
> Berwin A TurlachTel.: +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: [EMAIL PROTECTED]
> Singapore 117546http://www.stat.nus.edu.sg/~statba

__
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.


Re: [R] Looking for simpler solution to probabilistic question

2008-01-15 Thread Berwin A Turlach
G'day Rainer,

On Tue, 15 Jan 2008 14:24:08 +0200
Rainer M Krug <[EMAIL PROTECTED]> 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 TurlachTel.: +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: [EMAIL PROTECTED]
Singapore 117546http://www.stat.nus.edu.sg/~statba

__
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.


Re: [R] Looking for simpler solution to probabilistic question

2008-01-15 Thread Rainer M Krug
Hi Dimitris

that was EXACTLY what I was looking for

Thanks

Rainer

Dimitris Rizopoulos wrote:
> matrix(age1, ncol=length(age1), nrow=length(age2), byrow=TRUE) + age2
> 
> can be replaced by
> 
> outer(age2, age1, "+")
> 
> and the same for 'p'.
> 
> 
> Best,
> Dimitris
> 
> 
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
> 
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
> http://www.student.kuleuven.be/~m0390867/dimitris.htm
> 
> 
> - Original Message - From: "Rainer M Krug" <[EMAIL PROTECTED]>
> To: "r-help" <[EMAIL PROTECTED]>
> Sent: Tuesday, January 15, 2008 1:24 PM
> Subject: [R] Looking for simpler solution to probabilistic question
> 
> 
>> Hi
>>
>> 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 #
>>
>> Any ideas welcom,
>>
>> Rainer
>>
>>
>> age1 <- 1:20
>> p1 <- dnorm(age1,10,2)
>>
>> age2 <- 10:30
>> p2 <- dnorm(age2,15,1)
>>
>> ##
>> age <- matrix(age1, ncol=length(age1), nrow=length(age2), byrow=TRUE) 
>> + age2
>>
>> p <- matrix(p1, ncol=length(p1), nrow=length(p2), byrow=TRUE) + p2
>> p <- as.numeric(tapply(p, age, sum))
>> p <- p / sum(p)
>> #
>>
>> age <- sort(unique(as.numeric(age)))
>>
>> plot(age, p)
>>
>> lines(age1, p1)
>> lines(age2, p2)
>>
>> __
>> 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.
>>
> 
> 
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>

__
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.


Re: [R] Looking for simpler solution to probabilistic question

2008-01-15 Thread Dimitris Rizopoulos
matrix(age1, ncol=length(age1), nrow=length(age2), byrow=TRUE) + age2

can be replaced by

outer(age2, age1, "+")

and the same for 'p'.


Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: "Rainer M Krug" <[EMAIL PROTECTED]>
To: "r-help" <[EMAIL PROTECTED]>
Sent: Tuesday, January 15, 2008 1:24 PM
Subject: [R] Looking for simpler solution to probabilistic question


> Hi
>
> 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 
> #
>
> Any ideas welcom,
>
> Rainer
>
>
> age1 <- 1:20
> p1 <- dnorm(age1,10,2)
>
> age2 <- 10:30
> p2 <- dnorm(age2,15,1)
>
> ##
> age <- matrix(age1, ncol=length(age1), nrow=length(age2), 
> byrow=TRUE) + age2
>
> p <- matrix(p1, ncol=length(p1), nrow=length(p2), byrow=TRUE) + p2
> p <- as.numeric(tapply(p, age, sum))
> p <- p / sum(p)
> #
>
> age <- sort(unique(as.numeric(age)))
>
> plot(age, p)
>
> lines(age1, p1)
> lines(age2, p2)
>
> __
> 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
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.


[R] Looking for simpler solution to probabilistic question

2008-01-15 Thread Rainer M Krug
Hi

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 #

Any ideas welcom,

Rainer


age1 <- 1:20
p1 <- dnorm(age1,10,2)

age2 <- 10:30
p2 <- dnorm(age2,15,1)

##
age <- matrix(age1, ncol=length(age1), nrow=length(age2), byrow=TRUE) + age2

p <- matrix(p1, ncol=length(p1), nrow=length(p2), byrow=TRUE) + p2
p <- as.numeric(tapply(p, age, sum))
p <- p / sum(p)
#

age <- sort(unique(as.numeric(age)))

plot(age, p)

lines(age1, p1)
lines(age2, p2)

__
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.