I'm interested in using Fisher information to calculate standard errors for maximum likelihood estimates, as discussed, e.g. here and here.
To make sure everything was implemented correctly, I was comparing my results for a simple model, $x \sim \text{Normal}(\mu, \sigma)$, to the standard error of the mean calculated in the standard way, $\text{SEM} = \frac{s_\text{Corrected}}{\sqrt{n}}$, where $s_\text{Corrected}$ is the corrected sample standard deviation (that is, dividing by $n - 1$).
library(dplyr)
library(purrr)
# Simulate data
n = 20
ncol = 5
X = matrix(NA, n, ncol)
for(i in 1:ncol){
X[,i] = rnorm(n, mean = i, sd = i)
}
means = apply(X, 2, mean)
(sample_sds = apply(X, 2, sd))
## [1] 1.032571 2.100124 2.595767 4.144215 5.024134
(sems_from_sample_sd = sample_sds / sqrt(n))
## [1] 0.2308898 0.4696021 0.5804311 0.9266746 1.1234306
For reasons that will become clear shortly, I also calculated something similar, $\text{SEM}_\text{Alt} = \frac{s_\text{Uncorrected}}{\sqrt{n}}$, where $s_\text{Uncorrected}$ is the uncorrected sample standard deviation, AKA the population standard deviation (dividing by $n$).
#' Function to calculate population (rather than sample) sd
sd_population = function(x){
n = length(x)
sqrt((n-1)/n) * sd(x)
}
(population_sds = apply(X, 2, sd_population))
## [1] 1.006425 2.046948 2.530040 4.039281 4.896920
(sems_from_population_sds = population_sds / sqrt(n))
## [1] 0.2250436 0.4577115 0.5657342 0.9032107 1.0949847
Finally, I tried estimating the same thing by maximising the likelihood for parameters $\mu$ and $\sigma$, and calculating the Fisher information, following the code linked above.
I find first that the maximum likelihood estimate for $\sigma$ matches the uncorrected standard deviation estimate from above. I think I understand why this is the case (because estimation of standard deviations is complicated).
Second, and more confusingly, the standard errors estimated in this way correspond to the standard errors calculated using the population standard deviation statistic. I find this very hard to explain.
# Estimate parameters and SEs with maximum-likelihood and Fisher information
loglik_function = function(pars, x){
sum(dnorm(x, pars[1], pars[2], log = T))
}
fit_model = function(x){
starting_values = c(1, 1)
fit = optim(par = starting_values, fn = loglik_function,
control = list(fnscale = -1), x = x, hessian = T)
fisher_information = solve(-fit$hessian)
se = sqrt(diag(fisher_information))
data.frame(term = c('mu', 'sigma'),
estimate = fit$par,
se = se)
}
model_fits = apply(X, 2, fit_model)
sd_estimates = map_dbl(model_fits, function(df){
filter(df, term == 'sigma')$estimate
})
sd_estimates # Matches population SD
## [1] 1.006358 2.046619 2.529634 4.038780 4.897565
mean_ses = map_dbl(model_fits, function(df){
filter(df, term == 'mu')$se
})
mean_ses # Matches SEs calculated using population SD
## [1] 0.2250285 0.4576378 0.5656434 0.9030987 1.0951288
Can anyone explain, or point me towards an explanation of, what's going on here. Specifically, why does the standard error of the maximum-likelihood estimate correspond to $\frac{s_\text{Uncorrected}}{\sqrt{n}}$, rather than $\frac{s_\text{Corrected}}{\sqrt{n}}$, (which I've always understood to be) the textbook definition of standard error of the mean?
Related to this, are any reasons we should prefer one definition of the standard error over the other?