Re: [R] Simulation using parts of density function

From: Ted Harding <ted.harding_at_nessie.mcc.ac.uk>
Date: Wed, 02 May 2007 09:16:13 +0100 (BST)


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

______________________________________________
R-help_at_stat.math.ethz.ch 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. Received on Wed 02 May 2007 - 08:21:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 May 2007 - 09:31:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.