[R] Simulation using parts of density function

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Wed May 2 10:16:13 CEST 2007


On 02-May-07 07:45:48, Prof Brian Ripley wrote:
> Please do not send everything twice: you are using R-help in both the
> To: 
> and Cc: fields.
> 
> I disagree with Ted: it _is_ much easier to create a generator for this
> purpose.
> 
> Consider
> 
> rtgamma <- function(n, ..., tr = log(5000000))
> {
>      p <- pgamma(tr, ...)
>      qgamma(p*runif(n), ...)
> }
> 
> as inversion (especially at C level) is plenty fast enough.

Of course ... !!

Just to explain Brian's solution above:

Since pgamma(rgamma(...),...) is uniformly distributed on (0,1),
if rgamma is truncated to (0,tr) them pgamma(rgamma) will be
truncated to (0,pgamma(tr)), and hence uniformly distributed
on this range.

Best wishes,
Ted.



--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 02-May-07                                       Time: 09:16:08
------------------------------ XFMail ------------------------------



More information about the R-help mailing list