Lecture 15 Cramer–Rao and Efficiency in R

This is the R session for week 5. Last week in lectures we proved the Cramer–Rao inequality (theorem 13.2). This week we will try to translate this bound into numbers: we will consider an efficient estimator with variance on the bound, an inefficient estimator and the reason for its inefficiency, and the uniform model where the bound is beaten because the theorem does not apply. As usual, appendix B has a written version of the material we will discuss, and we will try to predict the outcome of each simulation before we perform the simulation.

15.1 An Efficient Estimator Against the Bound

Our model of choice for this section is X1,…,XnX_{1},\dots,X_{n} i.i.d. Poisson with mean θ=3\theta=3. From example 11.6 we know that the Cramer–Rao bound for an unbiased estimator for θ\theta is θ/n\theta/n. The sample mean, X¯\bar{X}, is an unbiased estimator for θ\theta with Varθ(X¯)=θ/n\mathop{\mathrm{Var}}\nolimits_{\theta}(\bar{X})=\theta/n (see lemma 2.2) and thus, by definition 13.4, is an efficient estimator. We use simulation to verify this claim, using the loop over ns from lecture 9. The command var can be used to compute the sample variance of a vector of estimates, and dividing the bound by this value allows us to estimate the efficiency of the estimator. The full script for this section, with lines intentionally shuffled (except for the function body and the loop which are left as one piece), is given in the following box. Before you can run the script, you will need to arrange the lines yourself.

bound <- theta / ns
data.frame(n = ns, variance = signif(v, 3), bound = signif(bound, 3),
efficiency = round(bound / v, 3))
est_pois <- function(n, theta) {
mean(rpois(n, lambda = theta))
}
set.seed(2026)
for (j in seq_along(ns)) {
v[j] <- var(replicate(10000, est_pois(ns[j], theta)))
}
theta <- 3
v <- numeric(length(ns))
ns <- c(5, 10, 20, 50, 100, 200)

As in lectures 6 and 9, all names must be defined before they are used: the function, theta and ns come first, v needs length(ns), the seed must be set before the loop, and the table must be created last. Before you try the reconstructed script, make a prediction for the efficiency column. Will all entries be exactly 11? Can an entry be larger than 11, despite the fact that theorem 13.2 states that no unbiased estimator can have a variance smaller than the bound?

est_pois <- function(n, theta) {
mean(rpois(n, lambda = theta))
}
theta <- 3
ns <- c(5, 10, 20, 50, 100, 200)
v <- numeric(length(ns))
set.seed(2026)
for (j in seq_along(ns)) {
v[j] <- var(replicate(10000, est_pois(ns[j], theta)))
}
bound <- theta / ns
data.frame(n = ns, variance = signif(v, 3), bound = signif(bound, 3),
efficiency = round(bound / v, 3))
## n variance bound efficiency
## 1 5 0.5970 0.600 1.005
## 2 10 0.3000 0.300 1.001
## 3 20 0.1510 0.150 0.995
## 4 50 0.0590 0.060 1.017
## 5 100 0.0297 0.030 1.010
## 6 200 0.0150 0.015 0.999

The empirical variances in the table follow the bound for all sample sizes, and the estimated efficiencies are clustered around 11 with half of the entries above this value. The following argument shows that this is no contradiction: the statement of the theorem only bounds the true variance Varθ(X¯)\mathop{\mathrm{Var}}\nolimits_{\theta}(\bar{X}), whereas the table shows the variance of 1000010000 random estimates. The random sample is off the true value by one or two percent in either direction, as shown in lecture 6. An estimated efficiency of 1.0171.017 means that, within the limits of error of the simulation, the estimator is efficient.

15.2 Attaining and Missing the Bound

In this section we show that not every unbiased estimator is efficient. As an example, from exercise 13.4 we know that the bias-corrected maximum likelihood estimator θ~=(n−1)/∑iXi\tilde{\theta}=(n-1)/\sum_{i}X_{i} for the exponential rate θ\theta is unbiased and has variance θ2/(n−2)\theta^{2}/(n-2). On the other hand, the bound from example 11.8 is θ2/n\theta^{2}/n and thus the efficiency of this estimator is (n−2)/n(n-2)/n. We can perform a similar analysis as in the previous section, this time for θ~\tilde{\theta} with θ=2\theta=2: To do this we write a new function (below est_pois in the code) to compute the estimator, keep the loop as before, adjust the range of ns to 55 to 100100, and replace the bound by theta^2 / ns. The resulting script is R/S15-efficiency.R. Before you can run the script, you need to write down (n−2)/n(n-2)/n for n=5n=5, 1010, 2020, 5050 and 100100. Considering the simulation error we have observed, for which sample sizes could the simulation be expected to distinguish θ~\tilde{\theta} from an efficient estimator?

est_exp <- function(n, theta) {
(n - 1) / sum(rexp(n, rate = theta))
}
data.frame(n = ns, efficiency = round(bound / v, 3), exact = (ns - 2) / ns)
## n efficiency exact
## 1 5 0.607 0.60
## 2 10 0.803 0.80
## 3 20 0.903 0.90
## 4 50 0.929 0.96
## 5 100 1.008 0.98

For n=5n=5, 1010 and 2020 the simulation coincides with the exact efficiencies up to two decimal digits. For n=50n=50 and 100100 the difference to 11 is 4%4\% and 2%2\%, comparable to the size of the simulation error. Thus, the last two rows do not give useful results and we would need significantly more than 1000010000 repetitions to resolve these results. The loss of efficiency is a constant factor which goes to zero as nn increases: θ~\tilde{\theta} is asymptotically efficient in the sense of lecture 17, but not efficient.

Where does the difference come from? The proof of theorem 13.2 in lecture 13 uses the Cauchy–Schwarz inequality between the estimator TT and the score ℓ′⁢(θ)\ell^{\prime}(\theta). Dividing through shows that the efficiency of TT is given by the squared correlation between TT and the score. This equals 11, if and only if T−θT-\theta is a multiple of the score. For the Poisson sample X¯−θ\bar{X}-\theta equals θ/n\theta/n times the score, and thus the correlation is 11 without any calculation. For the exponential sample θ~−θ\tilde{\theta}-\theta is a function of the score, but not a linear one. The following function computes the estimation error and the score for a given sample of size n=5n=5. The command cor can be used to compute the correlation of two vectors. Predict the value of the squared correlation before you run the code.

n <- 5
err_score <- function(n, theta) {
x <- rexp(n, rate = theta)
c((n - 1) / sum(x) - theta, n / theta - sum(x))
}
set.seed(2026)
expo <- replicate(2000, err_score(n, 2))
cor(expo[1, ], expo[2, ])^2
## [1] 0.6147813

The squared correlation 0.6150.615 coincides with the efficiency 3/53/5 from the table, up to simulation error. A plot of expo[1, ] against expo[2, ] shows that the 20002000 points lie on a curve instead of on a straight line. The curvature of this curve is evidence that the bound does not account for all of the variance.

15.3 Beating the Bound Without Breaking It

Example 13.6 is this week’s counterexample: for X1,…,XnX_{1},\dots,X_{n} i.i.d. uniformly distributed on the set (0,θ)(0,\theta), the estimator (n+1)⁢maxi⁡Xi/n(n+1)\max_{i}X_{i}/n is unbiased and has variance θ2/(n⁢(n+2))\theta^{2}/\bigl{(}n(n+2)\bigr{)}, whereas the Cramer–Rao formula, using the value 𝔼θ⁢(ℓ′⁢(θ)2)=1/θ2\mathbb{E}_{\theta}\bigl{(}\ell^{\prime}(\theta)^{2}\bigr{)}=1/\theta^{2} from example 11.11, would give θ2/n\theta^{2}/n. The following function computes, for the same sample, the bias-corrected maximum and the method of moments estimator 2⁢X¯2\bar{X}, both of which are unbiased. Thus, the variances are what the theorem would be about, if it held. The command apply can be used to apply a function to every row (second argument 1) or column (2) of a matrix, in this case to compute the variance var for each row of estimates. Before you try the code, make a prediction: which of the two columns will be below the column θ2/n\theta^{2}/n? Will the other column be above it?

est_both <- function(n, theta) {
x <- runif(n, min = 0, max = theta)
c((n + 1) * max(x) / n, 2 * mean(x))
}
theta <- 1
ns <- c(5, 10, 20, 50, 100, 200, 500, 1000)
vars <- matrix(0, nrow = length(ns), ncol = 2)
set.seed(2026)
for (j in seq_along(ns)) {
ests <- replicate(10000, est_both(ns[j], theta))
vars[j, ] <- apply(ests, 1, var)
}
rownames(vars) <- ns
colnames(vars) <- c("(n + 1) max / n", "2 * mean")
signif(cbind(vars, "theta^2 / n" = theta^2 / ns), 3)
## (n + 1) max / n 2 * mean theta^2 / n
## 5 2.76e-02 0.065900 0.200
## 10 8.35e-03 0.033300 0.100
## 20 2.37e-03 0.016600 0.050
## 50 3.79e-04 0.006610 0.020
## 100 9.59e-05 0.003310 0.010
## 200 2.59e-05 0.001660 0.005
## 500 3.93e-06 0.000668 0.002
## 1000 1.00e-06 0.000331 0.001

The variance of 2⁢X¯2\bar{X} is θ2/(3⁢n)\theta^{2}/(3n) by exercise 2.2, one third of the formal bound at every nn. The first column is below the formal bound throughout and falls away from it: at n=1000n=1000 the variance of the corrected maximum is 10−610^{-6}, one thousandth of θ2/n\theta^{2}/n. As in lecture 9, a plot on log-log axes shows the rates. Predict the slopes of the two sets of points and of the dashed line θ2/n\theta^{2}/n.

plot(ns, vars[, 1], log = "xy", pch = 16, xlab = "n", ylab = "variance",
ylim = range(vars, theta^2 / ns))
points(ns, vars[, 2], pch = 1)
lines(ns, theta^2 / ns, lty = 2)
lines(ns, theta^2 / (ns * (ns + 2)), lty = 3)
legend("bottomleft", legend = c("(n + 1) max / n", "2 * mean"),
pch = c(16, 1))
Log-log plot of variance as a function of n, for
n ranging from 5 to 1000. A dashed line with slope -1
runs from 0.2 down to 0.001. The open circles
are on a parallel line, one third of the height of the main
line. The filled circles are on a dotted line, which has a
slope that is twice the slope of the main line. The dotted line
reaches one millionth at n=1000, and thus is
significantly lower than the dashed line.
Figure 15.1: Simulated variances for (n+1)⁢maxi⁡Xi/n(n+1)\max_{i}X_{i}/n (filled circles) and 2⁢X¯2\bar{X} (open circles), for the uniform parameter θ=1\theta=1, on logarithmic axes. The dashed line shows the value θ2/n\theta^{2}/n from the Cramer–Rao formula. The dotted line shows the exact variance θ2/(n⁢(n+2))\theta^{2}/\bigl{(}n(n+2)\bigr{)} from example 13.6.

In figure 15.1 the open circles are parallel to the dashed line with slope −1-1, corresponding to the usual rate, and the filled circles follow the dotted line with slope −2-2: figure 9.2 again, with the value θ2/n\theta^{2}/n added and filled circles placed below it from the first sample size on. Nothing is broken. The regularity conditions of theorem 13.2 are violated by the uniform distribution, since the support of the distribution moves with θ\theta: example 11.11 shows that the score does not have mean zero and that the differentiation under the integral sign used in the proof is not possible. Thus, the value θ2/n\theta^{2}/n cannot be a bound and indeed it is a formula used outside its domain of validity, and an estimator with variance smaller than this value is called super-efficient. The same failure of regularity is shown in exercise 17.3 to cause the rate 1/n1/n of the maximum.

15.4 A Task to Take Home

The following task allows you to practice the methods we have learned in this lecture, in a style similar to the practical report. First, you should repeat the study of section 15.1 for the Bernoulli distribution with θ=0.3\theta=0.3, where X¯\bar{X} is unbiased and the bound is θ⁢(1−θ)/n\theta(1-\theta)/n as shown in example 11.5. Create a table which shows the estimated efficiencies for the sample sizes in ns. Next, consider the sample variance S2S^{2}, computed using var, as an estimator for σ2\sigma^{2} for a normal sample with μ=0\mu=0 and σ2=1\sigma^{2}=1. The bound 2⁢σ4/n2\sigma^{4}/n for this estimator is given in example 14.6. Compute the exact variance of S2S^{2} using the chi-squared distribution of (n−1)⁢S2/σ2(n-1)S^{2}/\sigma^{2} from proposition A.3. Compare the exact efficiency of your estimator to the values in the table. Finally, in three sentences, discuss which of the two estimators is efficient, how much less efficient the other one is, and how many repetitions of your simulation would be required to see the less efficient estimator at n=100n=100.

Before the next session, you should type the commands used in this session, and should check that you get the same results as the text.

Summary.
  • •
    ​

    The written reference for the functions var and apply is appendix B.

  • •
    ​

    The estimated efficiency, i.e. the ratio of the Cramer–Rao bound to the sample variance of the simulated estimates, fluctuates around the true efficiency by one or two percent for 1000010000 repetitions and can be greater than 11. A simulation is only able to detect an efficiency loss, if the loss is bigger than this error.

  • •
    ​

    The Poisson mean is efficient for all nn. The bias-corrected exponential-rate estimator has efficiency (n−2)/n(n-2)/n. The efficiency is the square of the correlation between the estimator and the score, and equals 11, if and only if the estimator is a linear function of the score.

  • •
    ​

    For the uniform parameter, the variance of (n+1)⁢maxi⁡Xi/n(n+1)\max_{i}X_{i}/n decreases as  1/n21/n^{2}, and is smaller than the value θ2/n\theta^{2}/n from the Cramer–Rao formula, since the regularity conditions are violated. The formula is not a bound in this case.