Point estimates
Wren provides the MAP and MLE functions for obtaining point estimates of model parameters (respectively, the maximum a posteriori and maximum likelihood estimates).
This requires you to load Optimization.jl which provides an interface to a variety of optimisation algorithms, as well as a package that provides the actual solvers. In the examples below we use the limited-memory BFGS solver from OptimizationOptimJL.jl.
The extension that provides this functionality is defined in WrenCore, so you don't need to load Wren. This means that you don't need to load other MCMC-related dependencies if you just want to use point estimates.
However, for convenience in the examples below we will use Wren directly.
Note that, depending on which solver you use, you may also need to load an AD backend. Just as with MCMC, Wren uses reverse-mode Enzyme as its default AD backend.
using Wrenusing Wren.Distsusing OptimizationOptimJL: LBFGSimport Enzyme, Optimization@wren function poisson_rate(count) rate ~ Gamma(2.0, 1.0) @observe count ~ Poisson(rate)endmodel = poisson_rate(3)estim = MAP(model, LBFGS())PointEstimate
├─ lp_function => logjoint
├─ lp => -3.019170746988274
├─ parameters => VarNamedTuple
│ └─ rate => 1.9999999998621254
├─ vecmodel::VecModel{…}
└─ solution::OptimizationSolution{…}
└─ retcode => SuccessMAP (and MLE) returns a PointEstimate, from which you can access the fitted (model-space) parameters with parameters. Here, the MAP estimate of rate is approximately 2:
parameters(estim)VarNamedTuple
└─ rate => 1.9999999998621254For the other fields, you should directly access the PointEstimate object's fields. Briefly:
lpis the log-density at the optimum;solutionis the original return value from Optimization.jl. You should check theretcodeto make sure that the optimisation converged successfully.
Likewise, we can use MLE to obtain the maximum likelihood estimate of the same model:
MLE(model, LBFGS())PointEstimate
├─ lp_function => loglikelihood
├─ lp => -1.4959226032237258
├─ parameters => VarNamedTuple
│ └─ rate => 3.0000000004840297
├─ vecmodel::VecModel{…}
└─ solution::OptimizationSolution{…}
└─ retcode => SuccessYou can pass extra keyword arguments, for example to set the maximum number of iterations. These are all forwarded to Optimization.solve; please see the Optimization docs for more information.
MLE(model, LBFGS(); maxiters=10)PointEstimate
├─ lp_function => loglikelihood
├─ lp => -1.4959226032237263
├─ parameters => VarNamedTuple
│ └─ rate => 2.9999999998552975
├─ vecmodel::VecModel{…}
└─ solution::OptimizationSolution{…}
└─ retcode => SuccessWren's optimisation routines are always performed in unconstrained coordinates. Internally this is accomplished by vectorisation of the model, which allows us to plug into the interface that Optimization.jl expects.
However, the objective function that we maximise is always the log-density in the original space: the Jacobian term is not included. (Conceptually, this is because we are trying to find the parameters that maximise the original-space density, not the unconstrained-space density.)
optimize in general
Both MAP and MLE are actually convenience wrappers around optimize, which specify different objective functions to maximise. Specifically, MAP selects lp_function=logjoint, and MLE selects lp_function=loglikelihood.
If you want to have a custom objective function, you can call optimize directly with the lp_function keyword argument. Briefly speaking, the function you provide should map an AccumulatorTuple to a scalar log-density value. You will most likely want to compose the logprior and loglikelihood primitives to construct your own objective function. Here's a contrived example that demonstrates this:
optimize(model, LBFGS(); lp_function=(accs -> logprior(accs) + 2 * loglikelihood(accs)))PointEstimate
├─ lp_function => #4
├─ lp => -4.652433915745684
├─ parameters => VarNamedTuple
│ └─ rate => 2.3333333334194073
├─ vecmodel::VecModel{…}
└─ solution::OptimizationSolution{…}
└─ retcode => SuccessInitialisation
By default, optimisation starts from a draw from the model's priors. You can choose an initialisation strategy for the optimisation using the init keyword argument.
init can either be an AbstractInitialization such as FindFiniteDensity (i.e., try until we get a starting point that has finite log-density), or something that can be converted to it. Some examples are:
# Initialise from [-2, 2] in unconstrained spaceinitial = FindFiniteDensity(FromUnconstrainedInterval(-2, 2))# Draw from a seeded priorusing Random: Xoshiroinitial = FindFiniteDensity(FromPrior(Xoshiro(468)))# Specific values, as a VarNamedTuple in model space.initial = VarNamedTuple(rate=1.5)# then:MAP(model, LBFGS(); init=initial)PointEstimate
├─ lp_function => logjoint
├─ lp => -3.019170746988274
├─ parameters => VarNamedTuple
│ └─ rate => 2.000000003330287
├─ vecmodel::VecModel{…}
└─ solution::OptimizationSolution{…}
└─ retcode => Success