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)

\[f_X(x)=\frac{x}{4}e^{-x^2/8},\quad x\geq 0.\]

Instructions:

  • Define the PDF from above as a function f(). exp(a) computes \(e^a\).

  • 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: \(\int_0^\infty f_X(x)\mathrm{d}x=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 \([-\infty,\infty]\) 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 \(X^2\).

  • Compute the variance of \(X\). Store the result in variance.

Hints:

  • The expected value of \(X\) is defined as \(E(X)=\int_0^\infty xf_X(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(X^2)-E(X)^2\), where \(E(X^2)=\int_0^\infty x^2f_X(x)\mathrm{d}x\).

4. Standard Normal Distribution I

Let \(Z\sim\mathcal{N}(0, 1)\).

Instructions:

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

Hints:

  • Values of \(\phi(\cdot)\) 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 \(Z\sim\mathcal{N}(0, 1)\).

Instructions:

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

Hints:

  • \(P(|Z|\leq z) = P(-z \leq Z \leq z)\).

  • Probabilities of the form \(P(a \leq Z \leq b)\) can be computed as \(P(Z\leq b)-P(Z\leq a)=F_Z(b)-F_Z(a)\) with \(F_Z(\cdot)\) the cumulative distribution function (CDF) of \(Z\). Alternatively, you may exploit the symmetry of the standard normal distribution.

6. Normal Distribution I

Let \(Y\sim\mathcal{N}(5, 25)\).

Instructions:

  • Compute the 99% quantile of the given distribution, i.e., find \(y\) such that \(\Phi(\frac{y-5}{5})=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 \(Y\sim\mathcal{N}(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\sim\chi^2_{10}\).

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 \(X_1\) and \(X_2\) be two independent normally distributed random variables with \(\mu=0\) and \(\sigma^2=15\).

Instructions:

  • Compute \(P(X_1^2+X_2^2>10)\).

Hints:

  • Note that \(X_1\) and \(X_2\) are not \(\mathcal{N}(0,1)\) but \(\mathcal{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 \(X\sim t_{10000}\) and \(Z\sim\mathcal{N}(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 \(X\sim t_1\). 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 \(Y\sim F(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 \(Y\sim F(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.