title | description |
---|---|
Probability Theory |
Chapter description goes here. |
type: NormalExercise
key: 2bafef99a3
lang: r
xp: 100
skills:
- 1
Suppose you are the lottery fairy in a weekly lottery, where
@instructions
- Draw the winning numbers for this week.
@hint
- You may use the function sample() to draw random numbers, see ?sample.
- The set of elements to be sampled from here is
$\{1,...,49\}$ .
@pre_exercise_code
@sample_code
# draw the winning numbers for this week
@solution
# draw the winning numbers for this week
sample(1:49, size = 6)
@sct
ex() %>% check_function("sample") %>% {
check_arg(., "x") %>% check_equal()
check_arg(., "size") %>% check_equal()
}
success_msg("Well done!")
type: NormalExercise
key: 5d152aaf19
xp: 100
Consider a random variable
@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.
@hint
- 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
$[0,\infty]$ by setting the corresponding arguments to 0 and Inf. You can access the numerical value of the computed integral by appending $value. See ?integral for a detailed description of the function.
@pre_exercise_code
@sample_code
# define the PDF
# integrate f over the domain
@solution
# define the PDF
f <- function(x){x/4*exp(-x^2/8)}
# integrate f over the domain
integrate(f, 0, Inf)$value
@sct
ex() %>% check_fun_def("f") %>% {
check_arguments(.)
check_call(., 1) %>% check_result() %>% check_equal()
check_call(., 4) %>% check_result() %>% check_equal()
check_call(., 10) %>% check_result() %>% check_equal()
check_call(., 100) %>% check_result() %>% check_equal()
check_body(.) %>% check_function(., "exp")
}
ex() %>% check_function("integrate") %>% {
check_arg(., "f") %>% check_equal(eval = FALSE)
check_arg(., "lower") %>% check_equal()
check_arg(., "upper") %>% check_equal()
}
success_msg("Great work!")
type: NormalExercise
key: 457fe23524
xp: 100
In this exercise you have to compute the expected value and the variance of the random variable
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.
@hint
- The expected value of
$X$ is defined as$E(X)=\int_0^\infty xf_X(x)\mathrm{d}x$ . - 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$ .
@pre_exercise_code
f <- function(x){(x/4)*exp(-x^2/8)}
@sample_code
# define the function ex
# compute the expected value of X
# define the function ex2
# compute the variance of X
@solution
# define the function ex
ex <- function(x){x*f(x)}
# compute the expected value of X
expected_value <- integrate(ex, 0, Inf)$value
# define the function ex2
ex2 <- function(x){x^2*f(x)}
# compute the variance of X
variance <- integrate(ex2, 0, Inf)$value - expected_value^2
@sct
ex() %>% check_fun_def("ex") %>% {
check_arguments(.)
check_call(., 1) %>% check_result() %>% check_equal()
check_call(., 4) %>% check_result() %>% check_equal()
check_call(., 10) %>% check_result() %>% check_equal()
check_call(., 100) %>% check_result() %>% check_equal()
check_body(.) %>% check_function(., "f")
}
ex() %>% check_function("integrate", index = 1) %>% {
check_arg(., "f") %>% check_equal(eval = FALSE)
check_arg(., "lower") %>% check_equal()
check_arg(., "upper") %>% check_equal()
}
ex() %>% check_object("expected_value") %>% check_equal()
ex() %>% check_fun_def("ex2") %>% {
check_arguments(.)
check_call(., 1) %>% check_result() %>% check_equal()
check_call(., 4) %>% check_result() %>% check_equal()
check_call(., 10) %>% check_result() %>% check_equal()
check_call(., 100) %>% check_result() %>% check_equal()
check_body(.) %>% check_function(., "f")
}
ex() %>% check_function("integrate", index = 2) %>% {
check_arg(., "f") %>% check_equal(eval = FALSE)
check_arg(., "lower") %>% check_equal()
check_arg(., "upper") %>% check_equal()
}
ex() %>% check_object("variance") %>% check_equal()
success_msg("Good job!")
type: NormalExercise
key: f0622e59c7
xp: 100
Let
@instructions
- Compute
$\phi(3)$ , that is, the value of the standard normal density at$c=3$ .
@hint
- 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 wish to obtain density values of the standard normal distribution.
@pre_exercise_code
@sample_code
# compute the value of the standard normal density at c=3
@solution
# compute the value of the standard normal density at c=3
dnorm(3)
@sct
ex() %>% check_function("dnorm") %>% check_result() %>% check_equal()
success_msg("Well done!")
type: NormalExercise
key: eb56223310
xp: 100
Let
@instructions
- Compute
$P(|Z|\leq 1.64)$ by using the function pnorm().
@hint
-
$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.
@pre_exercise_code
@sample_code
# compute the probability
@solution
# compute the probability
pnorm(1.64) - pnorm(-1.64)
@sct
ex() %>% check_or(
check_function(., "pnorm"),
check_code(., "dnorm", fixed = T)
)
ex() %>% check_output_expr("1 - 2*pnorm(-1.64)")
success_msg("Good Job!")
type: NormalExercise
key: fc7b7c446b
xp: 100
Let
@instructions
- Compute the 99% quantile of the given distribution, i.e., find
$y$ such that$\Phi\left(\frac{y-5}{5}\right)=0.99$ .
@hint
- 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.
@pre_exercise_code
@sample_code
# compute the 99% quantile of a normal distribution with mu = 5 and sigma^2 = 25.
@solution
# compute the 99% quantile of a normal distribution with mu = 5 and sigma^2 = 25.
qnorm(0.99, mean = 5, sd = sqrt(25))
@sct
ex() %>% check_function("qnorm") %>% check_result() %>% check_equal()
success_msg("Great work!")
type: NormalExercise
key: e13d86de1c
xp: 100
Let
@instructions
- Generate
$10$ random numbers from this distribution.
@hint
- 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!
@pre_exercise_code
@sample_code
# generate 10 random numbers from the given distribution.
@solution
# generate 10 random numbers from the given distribution.
rnorm(10, mean = 2, sd = sqrt(12))
@sct
ex() %>% check_function("rnorm") %>% {
check_arg(., "n") %>% check_equal()
check_arg(., "mean") %>% check_equal()
check_arg(., "sd") %>% check_equal()
}
success_msg("Great work!")
type: NormalExercise
key: 7c6da7fa91
xp: 100
Let
@instructions
- Plot the corresponding PDF using curve(). Specify the range of x-values as
$[0,25]$ via the argument xlim.
@hint
- 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.
@pre_exercise_code
@sample_code
# plot the PDF of a chi^2 random variable with df = 10
@solution
# plot the PDF of a chi^2 random variable with df = 10
curve(dchisq(x, df = 10), xlim = c(0, 25))
@sct
ex() %>% check_function("curve") %>% {
check_arg(., "expr") # does not work
check_arg(., "xlim") %>% check_equal()
}
# more robust SCT needed
ex() %>% check_code("dchisq", fixed = T)
success_msg("Great work!")
type: NormalExercise
key: 8e4a8cc2ef
xp: 100
Let
@instructions
- Compute
$P(X_1^2+X_2^2>10)$ .
@hint
- 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.
@pre_exercise_code
@sample_code
# compute the probability
@solution
# compute the probability
pchisq(10/15, df = 2, lower.tail = F)
@sct
ex() %>% check_or(
check_function(., "pchisq"),
check_code(., "dchisq", fixed = T)
)
ex() %>% check_output_expr("1 - pchisq(10/15, df = 2)")
success_msg("Great work!")
type: NormalExercise
key: e0e653696b
xp: 100
Let
@instructions
- Compute the
$95\%$ quantile of both distributions. What do you notice?
@hint
- 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.
@pre_exercise_code
@sample_code
# compute the 95% quantile of a t distribution with 10000 degrees of freedom
# compute the 95% quantile of a standard normal distribution
@solution
# compute the 95% quantile of a t distribution with 10000 degrees of freedom
qt(0.95, df = 10000)
# compute the 95% quantile of a standard normal distribution
qnorm(0.95)
@sct
ex() %>% check_function("qt") %>% check_result() %>% check_equal()
ex() %>% check_function("qnorm") %>% check_result() %>% check_equal()
success_msg("Correct! A t-distributed RV converges to a standard normal as the degrees of freedom get large.")
type: NormalExercise
key: 141baf52d5
xp: 100
Let
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?
@hint
- 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().
@pre_exercise_code
custom_seed(1)
@sample_code
# generate 1000 random numbers from the given distribution. Assign them to the variable x.
# compute the sample mean of x.
@solution
# generate 1000 random numbers from the given distribution. Assign them to the variable x.
x <- rt(1000, df = 1)
# compute the sample mean of x.
mean(x)
@sct
ex() %>% check_object("x") %>% check_equal()
# allow for rcauchy()
ex() %>% check_function("mean") %>% check_result() %>% check_equal()
success_msg("Well done! The expectation is not defined for a t distributed RV with one degree of freedom: although a t distribution with M = 1 is, as every other t distribution, symmetric around zero it actually has no expectation. This explains the value of the sample mean not being close to zero.")
type: NormalExercise
key: e4bfce6847
xp: 100
Let
@instructions
- Plot the quantile function of the given distribution using the function curve().
@hint
- curve() expects the function with their respective parameters (here: degrees of freedom df1 and df2) as an argument.
@pre_exercise_code
@sample_code
# plot the quantile function of the given distribution
@solution
# plot the quantile function of the given distribution
curve(qf(x, df1 = 10, df2 = 4))
@sct
ex() %>% check_function("curve") %>% check_arg(., "expr") # does not work, more robust SCT needed
ex() %>% check_code("qf", fixed = T)
success_msg("Well done!")
type: NormalExercise
key: bfe9c95c43
xp: 100
Let
@instructions
- Compute
$P(1<Y<10)$ by integration of the PDF.
@hint
- 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.
@pre_exercise_code
@sample_code
# compute the probability by integration
@solution
# compute the probability by integration
integrate(df, lower = 1, upper = 10, df1 = 4, df2 = 5)$value
@sct
ex() %>% check_function("integrate") %>% {
check_arg(., "f") %>% check_equal(eval = FALSE)
check_arg(., "lower") %>% check_equal()
check_arg(., "upper") %>% check_equal()
check_arg(., "df1") %>% check_equal()
check_arg(., "df2") %>% check_equal()
}
success_msg("Great work!")