,Christian Robert
Université Paris-Dauphine
and
George Casella
University of Florida
Introducing Monte Carlo Methods with R
Solutions to Odd-Numbered Exercises
January 17, 2010
Springer
Berlin Heidelberg NewYork
Hong Kong London Singapore
Milan Paris Tokyo
,
,Preface
The scribes didn’t have a large enough set from which to determine
patterns.
Brandon Sauderson
The Hero of Ages
This partial solution manual to our book Introducing Monte Carlo Methods
with R, published by Springer Verlag in the User R! series, on December 2009,
has been compiled both from our own solutions and from homeworks written
by the following Paris-Dauphine students in the 2009-2010 Master in Statis-
tical Information Processing (TSI): Thomas Bredillet, Anne Sabourin, and
Jiazi Tang. Whenever appropriate, the R code of those students has been
identified by a # (C.) Name in the text. We are grateful to those students for
allowing us to use their solutions. A few solutions in Chapter 4 are also taken
verbatim from the solution manual to Monte Carlo Statistical Methods com-
piled by Roberto Casarin from the University of Brescia (and only available
to instructors from Springer Verlag).
We also incorporated in this manual indications about some typos found
in the first printing that came to our attention while composing this solu-
tion manual have been indicated as well. Following the new “print on de-
mand” strategy of Springer Verlag, these typos will not be found in the
versions of the book purchased in the coming months and should thus be
ignored. (Christian Robert’s book webpage at Université Paris-Dauphine
www.ceremade.dauphine.fr/~xian/books.html is a better reference for the
“complete” list of typos.)
Reproducing the warning Jean-Michel Marin and Christian P. Robert
wrote at the start of the solution manual to Bayesian Core, let us stress
here that some self-study readers of Introducing Monte Carlo Methods with R
may come to the realisation that the solutions provided here are too sketchy
for them because the way we wrote those solutions assumes some minimal
familiarity with the maths, the probability theory and with the statistics be-
,vi Preface
hind the arguments. There is unfortunately a limit to the time and to the
efforts we can put in this solution manual and studying Introducing Monte
Carlo Methods with R requires some prerequisites in maths (such as matrix
algebra and Riemann integrals), in probability theory (such as the use of joint
and conditional densities) and some bases of statistics (such as the notions of
inference, sufficiency and confidence sets) that we cannot cover here. Casella
and Berger (2001) is a good reference in case a reader is lost with the “basic”
concepts or sketchy math derivations.
We obviously welcome solutions, comments and questions on possibly er-
roneous or ambiguous solutions, as well as suggestions for more elegant or
more complete solutions: since this manual is distributed both freely and in-
dependently from the book, it can be updated and corrected [almost] in real
time! Note however that the R codes given in the following pages are not opti-
mised because we prefer to use simple and understandable codes, rather than
condensed and efficient codes, both for time constraints and for pedagogical
purposes: some codes were written by our students. Therefore, if you find
better [meaning, more efficient/faster] codes than those provided along those
pages, we would be glad to hear from you, but that does not mean that we will
automatically substitute your R code for the current one, because readability
is also an important factor.
A final request: this manual comes in two versions, one corresponding to
the odd-numbered exercises and freely available to everyone, and another one
corresponding to a larger collection of exercises and with restricted access
to instructors only. Duplication and dissemination of the more extensive “in-
structors only” version are obviously prohibited since, if the solutions to most
exercises become freely available, the appeal of using our book as a textbook
will be severely reduced. Therefore, if you happen to possess an extended ver-
sion of the manual, please refrain from distributing it and from reproducing
it.
Sceaux and Gainesville Christian P. Robert and George Casella
January 17, 2010
,Contents
1 Basic R programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Exercise 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Exercise 1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Exercise 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Exercise 1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Exercise 1.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Exercise 1.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Exercise 1.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Exercise 1.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 1.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 1.19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 1.21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 1.23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Random Variable Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercise 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercise 2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercise 2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Exercise 2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 2.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 2.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 2.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Exercise 2.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercise 2.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Exercise 2.19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Exercise 2.21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Exercise 2.23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3 Monte Carlo Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Exercise 3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Exercise 3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
,viii Contents
Exercise 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Exercise 3.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Exercise 3.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Exercise 3.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Exercise 3.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Exercise 3.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Exercise 3.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4 Controling and Accelerating Convergence . . . . . . . . . . . . . . . . . . 27
Exercise 4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Exercise 4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Exercise 4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Exercise 4.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Exercise 4.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise 4.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise 4.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise 4.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Exercise 4.19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Exercise 4.21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5 Monte Carlo Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exercise 5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exercise 5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exercise 5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Exercise 5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Exercise 5.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Exercise 5.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Exercise 5.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Exercise 5.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Exercise 5.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Exercise 5.19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exercise 5.21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6 Metropolis-Hastings Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Exercise 6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Exercise 6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Exercise 6.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Exercise 6.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Exercise 6.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Exercise 6.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Exercise 6.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Exercise 6.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7 Gibbs Samplers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Exercise 7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
, Contents ix
Exercise 7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Exercise 7.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Exercise 7.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Exercise 7.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercise 7.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercise 7.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Exercise 7.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Exercise 7.19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Exercise 7.21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Exercise 7.23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Exercise 7.25 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8 Convergence Monitoring for MCMC Algorithms . . . . . . . . . . . 69
Exercise 8.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Exercise 8.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Exercise 8.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Exercise 8.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Exercise 8.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Exercise 8.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Exercise 8.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Exercise 8.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Exercise 8.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
,
, 1
Basic R programming
Exercise 1.1
Self-explanatory.
Exercise 1.3
Self-explanatory.
Exercise 1.5
One problem is the way in which R handles parentheses. So
> n=10
> 1:n
produces
1 2 3 4 5 6 7 8 9 10
but
> n=10
> 1:n-1
produces
0 1 2 3 4 5 6 7 8 9
since the 1:10 command is executed first, then 1 is subtracted.
The command seq(1,n-1,by=1) operates just as 1:(n-1). If n is less than
1 we can use something like seq(1,.05,by=-.01). Try it, and try some other
variations.