STATISTICAL LABORATORYPractical 6: Assessed courseworkST104Term 3, 2019Important InformationThis practical session is assessed. The deadline for submission is 11am on Thursday,9th May.Your reports should be submitted electronically on Moodle. Here is the link for submission:https://moodle.warwick.ac.uk/mod/assign/view.php?id=674702You can also find the submission link on the right hand side of the module webpage.Please note that your report must:- be submitted in PDF,- be no more than 5 sides of A4 in length (excluding figures),- be 12pt font for the main body of the report.You will lose marks if you do not follow these requirements.Also, please make sure you include your student ID code and lab group number on thefront sheet, and NOT your name!Exercises1. Pseudo-random numbers and the inversion method.(a) For arbitrary choices of initial seeds U1 and U2 (in the interval (0, 1]), letUi+2 = [Ui+1 + Ui] (mod 1) i ∈ N.Why is this not a good pseudo-random independent U(0, 1] generator? [2 marks](b) During lectures we have seen how the inversion method can be used to simulatefrom a Bernoulli(p) distribution using U ~ U(0, 1]. To achieve this task thefunction generate.bernoulli() is written and presented below.generate.bernoulli sample bernoulli return(bernoulli)}Explain in words what each line of code is doing and briefly justify why thismethod does what it was intended to do. [3 marks]1(c) How would you simulate from a Geometric(p) distribution:(i) Using U ~ U(0, 1]?Hint: Show that F(x) = 1 (1 p)bxc, where bxc denotes the greatestinteger less than or equal to x. [2.5 marks](ii) Using E ~ Exp(1)? [1.5 marks]Write a function generate.geometric() that, given a sample size n and aprobability p, returns a vector of length n which contains realisations from aGeometric(p). Your function should use either a sample of size n from theU(0, 1] distribution or a sample of size n from the Exp(1) distribution. Chooseonly one of these two approaches but you should NOT use the built-in rgeom()function. Investigate (via comparisons you deem appropriate) what happens ifyou change the size n or the probability p. Try to experiment with the followingcombinations of n and p and include your comments in the report.n p10 0.110000 0.110 0.910000 0.9[4 marks][Total: 13 marks]2. Bernoulli random variables and their relatives.(a) Given a source of Bernoulli random variables, it’s relatively easy to write afunction to generate from the Binomial distribution (remember that a Binomialrandom variable with parameters n and p is the sum of n independent Bernoullirandom variables of common success probability p). Write a function which givenn and p will generate a single Binomial(n, p) random variable. Your function canmake use of generate.bernoulli() as given above (or any other user definedfunction which simulates from a Bernoulli(p) distribution) but you should NOTuse the built-in rbinom() function. [1 mark](b) Write another function which, given m, n and p, will generate m realisationsfrom the Binomial(n, p) distribution. You can use any of the previously definedfunctions but you should NOT use the built-in rbinom() function. Use yourfunction to generate 5000 realisations from a Binomial(10, 0.25) distribution. Inyour report, only include the code for the function and the R command you usedto call it (NOT the 5000 realisations). [1.5 marks](c) Plot a histogram o代写ST104留学生作业、代做STATISTICAL LABORATORY作业、R程序语言作业代写、代做R实验作业 代做f those realisations (normalised like a probability density).You may need to use the argument breaks to get a sensible histogram. In yourreport, include both the histrogram and the R command you used to obtain thehistogram.[1.5 marks]2(d) Use R to compute the sample mean and sample variance for your realisations. Inyour report, include both the R commands you used and your answers. How doyour answers compare to the expectation and variance of a Binomial(10, 0.25)random variable? [2 marks](e) If we wish to plot the graph of a function in R, we can evaluate that functionon a grid of points and use the plotting functions to join the dots. Use seq togenerate a suitable grid of points to add the density of a normal distribution ofthe same mean and variance to your histogram. Use the lines function (whichadds lines to an existing graph rather than plotting a new one) to add a blueline showing this normal density to your histogram. Include both your code andthe corresponding graph in your report. [2 marks](f) Repeat steps (b)-(e) for a Binomial(1000, 0.25) distribution. What do you observe?In your report, only include the corresponding histogram (with the correspondingnormal density in blue) and your comments. [2 marks](g) Repeat steps (b)-(e) for a Binomial(10000, 0.0001) distribution. What do youobserve? Try to also add the probability mass function of a Poisson distributionwith the same mean. For the Poisson mass function use a red colour. Whatis significant about what you observe here? In your report only include thecorresponding histogram (with the corresponding Normal density in blue andPoisson mass function in red) and your comments. [3 marks][Total: 13 marks]3. Convolutions.(a) Use rexp() to obtain a sample of 10,000 Exp(1) random variables. Plot ahistogram of your sample on the same scale as a probability density. What doyou observe and why? In your report, include your code, the histogram andyour comments. [1.5 marks](b) Write a function which has one argument, n, and which returns a vector of lengthn each element of which is obtained as the sum of two Exp(1) random variables.Plot a histogram of the values obtained using this function for n = 10, 000. Inyour report, include your code and the histogram. [1.5 marks](c) Compute the density of the sum E1 and E2 if these random variables are independentExp(1) distributed random variables. How does this density relate withthe density of a Gamma(α, β) distribution? Add the density you obtained to thehistogram you produced in part (b) using red colour. In your report, include thecomputations, your comments, the code and the corresponding plot. [4 marks](d) Adapt the function you wrote in part (b) to accept 2 arguments, n and k, and toreturn a vector of length n, each element of which comprises the sum of k independentExp(1) random variables. Include your code in the report. [2 marks](e) Plot a histogram of the values you obtain using the function of part (d) fork = 10 and k = 50, when n = 10, 000. In your report, include the code3you used to obtain the vectors (NOT the vectors) and the corresponding histograms.[2 marks](f) Do the histograms you obtain resemble any common probability density? If so,add the appropriate density function to the plot in red colour. In your report,include your answer (only brief justification needed and not a proof) as well asthe corresponding histograms with the appropriate density. [3 marks][Total: 14 marks]Note: For full marks, do not forget to add suitable titles to your plots.4转自:http://www.7daixie.com/2019050657139032.html
网友评论