ST104 - 2021 - Statistical Laboratory
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
ST104 - 2021 - Statistical Laboratory
Pratical 6 - Assessed coursework 2
Exercises
1. Rejection sampling
The density function fx (x; k, λ) (or fx (x)) of a Weibull distribution W (k, λ) with shape parameter k > 0 and scale parameter λ > 0 is defined as:
fx (x; k, λ) = ╱ 、 亿 − 1 exp ╱ ≠ ╱ 、 亿、, for x > 0.
(a) We want to obtain samples from a Weibull distribution. Explain why a uniform distribution is not
a suitable enveloppe function (or proposal distribution) in this case. [2 marks]
(b) We are then going to sample from a truncated Weibull distribution, with x e [0, 3]. The density function fx (x; k, λ) (or fx (x)) of a truncated Weibull distribution W (k, λ) with shape parameter k > 0 and scale parameter λ > 0 for x e [0, 3] is defined as:
fx (x; k, λ) = A ╱ 、 亿 − 1 exp ╱ ≠ ╱ 、 亿、, for x e [0, 3].
= 0 otherwise
Show that A = , where Fx is the cumulative density function of a (non-truncated) W (k, λ). [2 marks]
(c) Write a function truncatedweibullpdf that returns the pdf for the truncated Weibull distribution calculated at a single value x, for parameters k > 0, λ > 0.
[3 marks]
(d) Using your previous function, plot, for x e (0, 3) the pdf for:
● k = 1, λ = 1
● k = 2, λ = 1
● k = 5, λ = 2
[2 marks]
(e) Write a function uniform2d that generates two-dimensional co-ordinates where the x-values are samples from U (0, Mα ) and the y-values are samples from U (0, Mg ) for some positive constants Mα
and Mg . [1 mark]
(f) The pdf for the Weibull(5,2) distribution, as worked out from the above, is
fx (x; 5, 2) = A ╱ 、 4 exp ╱ ≠ ╱ 、 5 、
Use the optimize function (look at the help file if needed to see which arguments you need) to find the maximum value of f(x; 5,2), for x e (0, 3), and show it on your previous plot.
Note: you can also find that maximum value analytically, or get an upper bound just by looking at the graph. However, since in a following question, you are asked to write a function for any values of k and α, using optimize helps making these computations automatically.
The value found is that of M , the minimum height of the rectangle. If you weren’t able to find that value, you can set M = 1.
[2 marks]
(g) Write a function rejection that rejects unsuitable samples from the rectangle [0 , 3] × [0, M]. These
will be any co-ordinate (x乞 , y乞 ) found from the uniform2d function for which y乞 > f (xi; 5, 2). Or that accepts suitable samples, for which y乞 < f (xi; 5, 2). The input will be a 2 × n matrix, being the
output from the uniform2d function. [2 marks]
(h) Uniformly sample 50 000 sample values from the rectangle [0 , 3]×[0, M]. Use the rejection function
to reject samples that do not lie under the curve f (x; 5, 2). Plot the points that have not been rejected in a scatter diagram. What do you notice? [2 marks]
(i) Plot the x-values of the points generated in the previous question in the form of an histogram. What do you notice? [1 mark]
(j) You now have everything you need to sample as many values as you wish from the Weibull(5, 2)
distribution. Try adapting the above method to sample from any other Weibull distributions. Show
plots for the combinations of k and α given in (d). [3 marks]
2. Cauchy distribution
Also known as the Lorentz distribution, especially among physicists, and named after French mathemati- cian Augustin-Louis Cauchy, (although it had been studied before, in particular by Poisson) the Cauchy distribution is a family of continuous probability distributions. It has been used in many applications such as mechanical and electrical theory, physical anthropology, measurement problems, risk and finan- cial analysis. It was also used to model the points of impact of a fixed straight line of particles emitted from a point source.
The density function fx (x) of a (standard) Cauchy distribution is defined as:
fx (x) = , for x e R.
(a) Determine the cumulative distribution function Fx (x), and then find the inverse function Fx(−)1 of Fx . [3 marks]
(b) Describe an algorithm which uses the inverse transform method to generate n values sampled from
a standard Cauchy distribution. [2 marks]
(c) Apply your methodology to generate a sample of 100000 values from a Cauchy distribution. Plot a normalised histogram (like a probability density) of these realisations. [2 marks]
(d) On that same histogram, add a line that shows the probability density fuction of the standard Cauchy distribution. [2 marks]
(e) By using only the U (0, 1) distribution as a source of randomness, find a way to calculate the following
integral with R:
7
fx (x)dx.
5
[Hint: first, a suitable (linear) change of variable could be useful.]
You can check your answer by hand or by using R functions.
[3 marks]
3. Cauchy distribution and rejection sampling
We have seen in the lectures that we can not sample Cauchy random variables using a normal density as enveloppe function or proposal distribution. However, it is possible to generate standard normal random variables from Cauchy.
(a) Justify why the Cauchy distribution is an appropriate proposal distribution, and find the optimal
value of M . [3 marks]
(b) Sample 100000 samples u from a uniform distribution, and re-use your sample of 100000 y values
from a Cauchy distribution obtained in the previous part (use rcauchy if you did not manage to make the previous part work). Derive the condition that u and y must satisfy in order for a single
value y乞 to be accepted as a sample from a standard normal distribution. [3 marks]
(c) Apply that methodology to generate samples from a standard normal distribution. Plot a normalised histogram of these realisations. Add a line showing the probability density function of a standard normal distribution. [4 marks]
(d) We also said that we have a probability 1/M for a single sample from the proposal distribution to be accepted as a sample from the target distribution. Show that it was verified (approximately) in your rejection sampler in this exercise. [2 marks]
2022-05-13