Rules
See the Section about Rules for Quizzes and Homeworks on the General Info page.
Your work handed into Canvas should be an Rmarkdown file with text and code chunks that can be run to produce what you did. We do not take your word for what the output is. We may run it ourselves. But we also want the output.
You may ask questions if the wording of the questions are confusing. But the instructor will not be giving hints.
You must be in the classroom, Molecular and Cellular Biology 2-120, to take the quiz.
Quizzes must uploaded by the end of class (1:10). It should actually allow a few minutes after that, but those not uploaded by 1:10 will be marked late. Here is the link for uploading this quiz https://canvas.umn.edu/courses/330843/assignments/2823441.
Homeworks must uploaded before midnight the day they are due. Here is the link for uploading this homework. https://canvas.umn.edu/courses/330843/assignments/2823447.
Quiz 3
Problem 1
Write an R function that, like the example in Section 8 of the course notes about computer arithmetic except that we want it to be for the negative binomial distribution rather than the binomial distribution.
The negative binomial distribution has parameter θ and data x and log likelihood
where, as always, exp denotes the exponential function and log the natural logarithmic function (as in R) and r is a known positive constant (not an unknown parameter). The data x is nonnegative-integer-valued (0, 1, 2, …). The parameter θ is negative-real-valued (− ∞ < θ < 0).
Your function should have signature
logl(theta, x, r)and return a list having components
-
value
, the value of the function, -
gradient
, the first derivative of the function, and -
hessian
, the second derivative of the function.
Like the example in the course notes, the point is to be careful about computer arithmetic, avoiding overflow and catastrophic cancellation as much as possible.
Your function cannot use R function dnbinom
.
For this problem, you do not have to check for invalid argument values.
The formulas for the derivatives are
Show your function working for parameter values
thetas <- (- 10^seq(3, - 3))and for data values both
r <- 2.7 x <- 0and
r <- 10.7 x <- 5
Problem 2
Test your function for the preceding problem like the example in Section 8 of the course notes about computer arithmetic
To test the function value compare with the values of the function
logl.too <- function(theta) dnbinom(x, r, 1 - exp(theta), log = TRUE)and to test the derivatives use numerical derivatives (you can also apply the other method using derivatives calculated by the R function
D
if you like, but that does not count;
we are only going to grade the comparison to numerical derivatives).
Note that numerical differentiation does not give perfectly accurate derivatives. So there may be small discrepancies between the two methods of calculation.
Also note that log likelihood is only defined up to an additive constant that
does not depend on the parameter. Hence your function will give different
answers for the values than R function logl.too
, but the answers
must differ by a constant that does not depend on the value of θ.
Since the derivative of a constant is zero, this does not affect the derivatives.
You may find R function dnbinom
giving bad answers. In
this case, just check that the its answers and the answers for your function
agree when θ is far from minus infinity or zero.
Problem 3
Rewrite your function for problem 1 to add error checks for all three
arguments theta
, x
, and r
.
- Check that all are numeric and finite and have length one.
- Check that
x
is an integer (not necessarily type integer but rather a real number that is an integer). - Check
-
theta
< 0 -
x
≥ 0 -
r
> 0
-
For this problem we don't even care if the function works (that was problems 1 and 2). We just care that all possible user errors are caught. You do not have to test the errors for this problem.