Archive for Introducing Monte Carlo Methods with R

The confusing gamma parameter

Posted in Books, R, Statistics with tags , , , , on May 13, 2011 by xi'an

Boris from Ottawa sent me this email about Introducing Monte Carlo Methods with R:

As I went through the exercises and examples, I believe I found a typo in exercise 6.4 on page 176 that is not in the list of typos posted on  your website.  For simulation of Gamma(a,1) random variables with  candidate distribution Gamma([a],b), the optimal choice of b seems to be  a/[a] rather than [a]/a as suggested in the book.  Since the ratio dgamma(x,a,1)/dgamma(x,a,[a]/a) is unbounded, simulations with candidate distribution Gamma([a],[a]/a) yields poor approximation to the target distribution.

The problem with this exercise and the gamma distribution

f(x|a,b)=\dfrac{x^{a-1}e^{-bx}}{b^a\Gamma(a)}

in general is that it can be parameterised in terms of the scale or in terms of the rate, as recognised by the R [d/p/q/r]gamma functions:

[sourcecode language=”r” gutter=”false”]

GammaDist                package:stats                 R Documentation

The Gamma Distribution

Description:

Density, distribution function, quantile function and random
generation for the Gamma distribution with parameters ‘shape’ and
‘scale’.

Usage:

dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE)
pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE,
log.p = FALSE)
qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE,
log.p = FALSE)
rgamma(n, shape, rate = 1, scale = 1/rate)

Arguments:

x, q: vector of quantiles.

p: vector of probabilities.

n: number of observations. If ‘length(n) > 1’, the length is
taken to be the number required.

rate: an alternative way to specify the scale.

shape, scale: shape and scale parameters.  Must be positive, ‘scale’
strictly.
[/sourcecode]

Thus, Boris understood b to be the scale parameter, while we meant b to be the rate parameter, meaning we are in fine in agreement about the solution! The deeper question is, why use a duplicated and hence confusing parameterisation?! The reason for doing so is that, while the scale is the natural parameter, the rate has the nicer (Bayesian) property of enjoying a gamma conjugate prior (rather than an inverse gamma conjugate prior). This is why the gamma distribution is implicitly calibrated by the rate, instead of the scale, in most of the Bayesian literature.

Another review of Introducing … R

Posted in Books, R, Statistics, University life with tags , , on May 3, 2011 by xi'an

The March 2011 issue of JASA contains a review of Introducing Monte Carlo Methods with R by Hedibert Lopes. As in the previous review, the poor quality of the figures is (rightly) pointed out by Hedi. However, the main message of the review remains very positive and, furthermore, Hedi advertises the ‘Og itself in the review! E.g.,

Another dynamic media for the iteration between the reader and the book is Christian Robert’s blog where, among other things, readers and users of his several books point out typos, ask questions, make suggestions, and more importantly, find answers to these requests. For more details, visit http://www.ceremade.dauphine.fr/~xian/books.html or http://xianblog.fr/2010/02/ 10/ typos-in-chapters-1-8.

Just one minor point: Hedi mentions that he hopes the figures defaults can be corrected in the second edition. Actually, because this book is part of the print-on-demand strategy, we can make changes almost instantly. So taking into account the use of jpeg files instead of pdf ones, and the many typos signaled by readers, I sent a revised version of the book a few weeks ago and thus hope in-coming books should be free of those defects. As a conclusion, here is Hedi’s

I am convinced the book will be particularly useful for (and should be next to the computer of) a large body of hands-on graduate students, researchers, instructors, and practitioners with at least one year of graduate-level statistical background and interested in simultaneously learning and applying R programming and Monte Carlo tools to assist the statistical analysis and computation of their scientific problems.

Typos sorted, at last!

Posted in Books, R, Statistics, University life with tags , , , , , , , , on March 24, 2011 by xi'an


After posting so many entries about typos in my books (making you wonder how there could be any text left!) and postponing their classification for so long, I decided on Saturday afternoon to collect those entries into a comprehensive pdf document that should be more useful for readers. I incidentally noticed that my book web-page had not been updated for the publication of Méthodes de Monte-Carlo avec R two months ago and did update it… I am obviously more than grateful to all those who contacted me, either thru the Og or by email to point out those numerous typos. As the Wheel had started rolling (or weaving), I took the opportunity to clean the original LaTeX files as well, including Introducing Monte Carlo methods with R. Since this is a print-on-demand edition, the changes should be immediate.

cut&paste typo in R book

Posted in Books, R, Statistics with tags , , on March 3, 2011 by xi'an

A casualty of cut-and-paste in Chapter 3 of Introducing Monte Carlo Methods with R. Brad McNeney from Simon Fraser sent me a nice email about the end of Example 3.6 missing a marginal estimate. Indeed, it does. And it should have been obvious from the “estimates” we derived, 19 and 16, which are not even on the support of the posterior distribution represented on Figure 3.5… The R code is given as

[sourcecode gutter=”false” language=”r”]
> mean(y[,1]*apply(y,1,f)/den)/mean(apply(y,1,h)/den)
[1] 19.33745
> mean(y[,2]*apply(y,1,f)/den)/mean(apply(y,1,h)/den)
[1] 16.54468
[/sourcecode]

and should have been

[sourcecode gutter=”false” language=”r”]
> mean(y[,1]*f(y)/den)/mean(f(y)/den)
[1] 94.08314
> mean(y[,2]*f(y)/den)/mean(f(y)/den)
[1] 80.42832
[/sourcecode]

As also suggested by Brad, a similar modification applies to the remark after eqn. (3.7):

mean(apply(y,1,h)/den)

should be

mean(f(y)/den)

Introducing Monte Carlo Methods with R [precision]

Posted in Books, R, Statistics with tags , on January 17, 2011 by xi'an

Doug Rivers, professor of Political Sciences in Stanford, kindly sent me this email yesterday night:

The 2nd displayed equation in section 2.1.2 on p. 44 is garbled (it might be interpreted as saying that U and X have the same distribution). I think you intended:

P(U \leq u) = P[F(X) \leq u] = P[F^{-1}(F(X)) \leq F^{-1}(u)] = P[X \leq F^{-1}(u)] = F[F^{-1}(u)] = u

And indeed we should have stated the implicit convention that u=F(x) before this equation, i.e.

P(U \leq F(x))=P[F(X) \leq F(x)]=P[X \leq x]=F(x)

(although U and X cannot be seen to have the same distribution as they live in different spaces).