Please remember to do your homework completely in RMarkdown!
Approximating a distribution by a large sample.
Use the function
dbeta()
to produce a plot of the pdf for \(X\sim Beta(15,7)\).Now use the function
rbeta()
to generate lots of data drawn from this distribution and then produce a (relative frequency) histogram for the data.Now combine parts a and b to show that if you have a sufficient amount of data, then the histogram has the same shape as the pdf.
In a few sentences, describe what this exercise illustrates and how it is related to the Metropolis algorithm.
Implement the Metropolis algorithm as discussed in class. Your should write your own code and it should be commented. This function should take the following inputs:
- the number of “visits” to be made
- a vector indicating the range of parameter values (For this simple example, there are only seven values with positive probabilities, but, for the next problem, there is a continuum of possible values.)
- a function that will return the value of the numerator for a given value of the parameter
The function should return a vector of the “visits”.
Use the function from the previous problem to show that the distribution of the “visits” vector looks like the target distribution in Figure 7.3.
Modify your code so that it applies the Metropolis algorithm to a Bernoulli likelihood and beta prior. See section 7.3.1 in the text and, in particular, the procedure outlined on page 158. After writing the code, use it on the same example illustrated in figure 7.4. You should be able to reproduce the three different histograms. (They don’t have to be notated like the ones in the book.)
Read pages 168-170 and then give a straightforward description of what ESS means. (We will go over this in some more detail later in the chapter.) Then, discuss this notion in light of the example illustrated in Figure 7.4.