This book is in Open Review. We want your feedback to make the book better for you and other students. You may annotate some text by selecting it with the cursor and then click the on the pop-up menu. You can also see the annotations of others: click the in the upper right hand corner of the page

2.3 Exercises

1. Sampling

Suppose you are the lottery fairy in a weekly lottery, where 6 out of 49 unique numbers are drawn.

Instructions:

  • Draw the winning numbers for this week.

Hints:

  • You may use the function sample() to draw random numbers, see ?sample.

  • The set of elements to be sampled from here is {1,...,49}.

2. Probability Density Function

Consider a random variable X with probability density function (PDF)

fX(x)=x4ex2/8,x0.

Instructions:

  • Define the PDF from above as a function f(). exp(a) computes ea.

  • Check whether the function you have defined is indeed a PDF.

Hints:

  • Use function(x) {…} to define a function which takes the argument x.

  • In order for f() to be a PDF, its integral over the whole domain has to equal 1: 0fX(x)dx=1.

  • The function integrate() performs integration. You have to specify the function to be integrated as well as lower and upper limits of integration. These may be set to [,] by setting the corresponding arguments to -Inf and Inf. You can access the numerical value of the computed integral by appending $value. See `?integral for a detailed description of the function.

3. Expected Value and Variance

In this exercise you have to compute the expected value and the variance of the random variable X considered in the previous exercise.

The PDF f() from the previous exercise is available in your working environment.

Instructions:

  • Define a suitable function ex() which integrates to the expected value of X.

  • Compute the expected value of X. Store the result in expected_value.

  • Define a suitable function ex2() which integrates to the expected value of X2.

  • Compute the variance of X. Store the result in variance.

Hints:

  • The expected value of X is defined as E(X)=0xfX(x)dx.

  • The value of an integral computed by integrate() can be obtained via $value.

  • The variance of X is defined as Var(X)=E(X2)E(X)2, where E(X2)=0x2fX(x)dx.

4. Standard Normal Distribution I

Let ZN(0,1).

Instructions:

  • Compute ϕ(3), that is, the value of the standard normal density at c=3.

Hints:

  • Values of ϕ() can be computed using dnorm(). Note that by default dnorm() uses mean = 0 and sd = 1 so there is no need to set the corresponding arguments when you whish to obtain density values of the standard normal distribution.

5. Standard Normal Distribution II

Let ZN(0,1).

Instructions:

  • Compute P(|Z|1.64) by using the function pnorm().

Hints:

  • P(|Z|z)=P(zZz).

  • Probabilities of the form P(aZb) can be computed as P(Zb)P(Za)=FZ(b)FZ(a) with FZ() the cumulative distribution function (CDF) of Z. Alternatively, you may exploit the symmetry of the standard normal distribution.

6. Normal Distribution I

Let YN(5,25).

Instructions:

  • Compute the 99% quantile of the given distribution, i.e., find y such that Φ(y55)=0.99.

Hints:

  • You can compute quantiles of the normal distribution by using the function qnorm().

  • Besides the quantile to be computed you have to specify the mean and the standard deviation of the distribution. This is done via the arguments mean and sd. Note that sd sets the standard deviation, not the variance!

  • sqrt(a) returns the square root of the numeric argument a.

7. Normal Distribution II

Let YN(2,12).

Instructions:

  • Generate 10 random numbers from this distribution.

Hints:

  • You can use rnorm() to draw random numbers from a normal distribution.

  • Besides the number of draws you have to specify the mean and the standard deviation of the distribution. This can be done via the arguments mean and sd. Note that sd requires the standard deviation, not the variance!

8. Chi-squared Distribution I

Let Wχ102.

Instructions:

  • Plot the corresponding PDF using curve(). Specify the range of x-values as [0,25] via the argument xlim.

Hints:

  • curve() expects a function and its parameters as arguments (here dchisq() and the degrees of freedom df).

  • The range of x-values in xlim can be passed as a vector of interval bounds.

9. Chi-squared Distribution II

Let X1 and X2 be two independent normally distributed random variables with μ=0 and σ2=15.

Instructions:

  • Compute P(X12+X22>10).

Hints:

  • Note that X1 and X2 are not N(0,1) but N(0,15) distributed. Hence you have to scale appropriately. Afterwards you can use pchisq() to compute the probability.
  • The argument lower.tail may be helpful.

10. Student t Distribution I

Let Xt10000 and ZN(0,1).

Instructions:

  • Compute the 95% quantile of both distributions. What do you notice?

Hints:

  • You may use qt() and qnorm() to compute quantiles of the given distributions.

  • For the t distribution you have to specify the degrees of freedom df.

11. Student t Distribution II

Let Xt1. Once the session has initialized you will see the plot of the corresponding probability density function (PDF).

Instructions:

  • Generate 1000 random numbers from this distribution and assign them to the variable x.

  • Compute the sample mean of x. Can you explain the result?

Hints:

  • You can use rt() to draw random numbers from a t distribution.

  • Note that the t distribution is fully determined through the degree(s) of freedom. Specify them via the argument df.

  • To compute the sample mean of a vector you can use the function mean().

12. F Distribution I

Let YF(10,4).

Instructions:

  • Plot the quantile function of the given distribution using the function curve().

Hints:

  • curve() expects the function with their respective parameters (here: degrees of freedom df1 and df2) as an argument.

13. F Distribution II

Let YF(4,5).

Instructions:

  • Compute P(1<Y<10) by integration of the PDF.

Hints:

  • Besides providing the function to be integrated, you have to specify lower and upper bounds of integration.

  • The additional parameters of the distribution (here df1 and df2) also have to be passed inside the call of integrate().

  • The value of the integral can be obtained via $value.