Mean of a Gaussian Distribution
This tutorial demonstrates the basic workflow of LikelihoodProfiler.jl using one of the simplest statistical models: estimating the mean of a Gaussian distribution with known variance.
Model and Data
We assume we observe i.i.d. data:
\[X_1, \ldots, X_n \sim \mathcal{N}(\mu, 1)\]
and want to construct a confidence interval for the mean parameter μ.
using LikelihoodProfiler, OptimizationLBFGSB, Distributions, Random
using Plots
Random.seed!(73612768)
n_obs = 10
data = rand(Normal(0, 1), n_obs)10-element Vector{Float64}:
-0.18395430554302725
1.7726939574838576
-0.22783818140144707
-1.2257721932395091
-2.3823829754068884
-0.25220245433692723
-2.0344886935173037
1.0974688645942778
-1.4973706670259876
0.02295632164028553Objective Function (Negative Log-Likelihood)
With known variance $\sigma^2 = 1$, the log-likelihood for μ is:
\[\ell(\mu) = \sum_{i=1}^{n} \log \mathcal{N}(x_i \mid \mu, 1)\]
We minimize negative log-likelihood:
obj(x, p) = -sum(logpdf.(Normal(x[1], 1.0), data))obj (generic function with 1 method)Maximum Likelihood Estimate
x0 = [1.0] # initial guess
optf = OptimizationFunction(obj, AutoForwardDiff())
optprob = OptimizationProblem(optf, x0)
sol = solve(optprob, LBFGSB())
optpars = sol.u # MLE of μ1-element Vector{Float64}:
-0.4910890326752669For a Gaussian with known variance, this should be very close to the sample mean.
Profile Likelihood for the Mean
We now build the ProfileLikelihoodProblem, define profiling method and solve the problem. In this model there is one parameter, so the profile is one-dimensional.
plprob = ProfileLikelihoodProblem( optprob, optpars;
profile_lower = -3.0, profile_upper = 3.0)
method = OptimizationProfiler(optimizer = LBFGSB(),
stepper = FixedStep(; initial_step = 0.01))
sol = solve(plprob, method)
plot(sol, size=(800,300), margins=5Plots.mm)Interpreting the plot
- The curve shows how the negative log-likelihood increases as μ moves away from its MLE.
- The horizontal line marks the confidence threshold for the chosen level (default 95%).
- The intersection points give the profile likelihood confidence interval.
Extracting Confidence Interval Endpoints
ProfileLikelihoodSolution stores the computed profile curves together with confidence interval endpoints and identification retcodes, which indicate whether each parameter is practically identifiable. These values can be accessed directly:
retcodes(sol)
endpoints(sol)1-element Vector{@NamedTuple{left::Float64, right::Float64}}:
(left = -1.3675986121831671, right = 0.38542054683263377)