You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/docs-12-using-turing-guide/index.qmd
+4-110Lines changed: 4 additions & 110 deletions
Original file line number
Diff line number
Diff line change
@@ -418,120 +418,14 @@ loglikelihood(model, chn)
418
418
419
419
### Maximum likelihood and maximum a posterior estimates
420
420
421
-
Turing provides support for two mode estimation techniques, [maximum likelihood estimation](https://en.wikipedia.org/wiki/Maximum_likelihood_estimation) (MLE) and [maximum a posterior](https://en.wikipedia.org/wiki/Maximum_a_posteriori_estimation) (MAP) estimation. Optimization is performed by the [Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl) package. Mode estimation is currently a optional tool, and will not be available to you unless you have manually installed Optim and loaded the package with a `using` statement. To install Optim, run `import Pkg; Pkg.add("Optim")`.
422
-
423
-
Mode estimation only works when all model parameters are continuous -- discrete parameters cannot be estimated with MLE/MAP as of yet.
424
-
425
-
To understand how mode estimation works, let us first load Turing and Optim to enable mode estimation, and then declare a model:
426
-
427
-
```{julia}
428
-
# Note that loading Optim explicitly is required for mode estimation to function,
429
-
# as Turing does not load the opimization suite unless Optim is loaded as well.
430
-
using Turing
431
-
using Optim
432
-
433
-
@model function gdemo(x)
434
-
s² ~ InverseGamma(2, 3)
435
-
m ~ Normal(0, sqrt(s²))
436
-
437
-
for i in eachindex(x)
438
-
x[i] ~ Normal(m, sqrt(s²))
439
-
end
440
-
end
441
-
```
442
-
443
-
Once the model is defined, we can construct a model instance as we normally would:
444
-
445
-
```{julia}
446
-
# Create some data to pass to the model.
447
-
data = [1.5, 2.0]
448
-
449
-
# Instantiate the gdemo model with our data.
450
-
model = gdemo(data)
451
-
```
452
-
453
-
Mode estimation is typically quick and easy at this point. Turing extends the function `Optim.optimize` and accepts the structs `MLE()` or `MAP()`, which inform Turing whether to provide an MLE or MAP estimate, respectively. By default, the [LBFGS optimizer](https://julianlsolvers.github.io/Optim.jl/stable/#algo/lbfgs/) is used, though this can be changed. Basic usage is:
421
+
Turing also has functions for estimating the maximum aposteriori and maximum likelihood parameters of a model. This can be done with
454
422
455
423
```{julia}
456
-
# Generate a MLE estimate.
457
-
mle_estimate = optimize(model, MLE())
458
-
459
-
# Generate a MAP estimate.
460
-
map_estimate = optimize(model, MAP())
424
+
mle_estimate = maximum_likelihood(model)
425
+
map_estimate = maximum_a_posteriori(model)
461
426
```
462
427
463
-
If you wish to change to a different optimizer, such as `NelderMead`, simply place your optimizer in the third argument slot:
Some methods may have trouble calculating the mode because not enough iterations were allowed, or the target function moved upwards between function calls. Turing will warn you if Optim fails to converge by running `Optim.converge`. A typical solution to this might be to add more iterations, or allow the optimizer to increase between function iterations:
484
-
485
-
```{julia}
486
-
#| eval: false
487
-
# Increase the iterations and allow function eval to increase between calls.
More options for Optim are available [here](https://julianlsolvers.github.io/Optim.jl/stable/#user/config/).
494
-
495
-
#### Analyzing your mode estimate
496
-
497
-
Turing extends several methods from `StatsBase` that can be used to analyze your mode estimation results. Methods implemented include `vcov`, `informationmatrix`, `coeftable`, `params`, and `coef`, among others.
498
-
499
-
For example, let's examine our ML estimate from above using `coeftable`:
500
-
501
-
```{julia}
502
-
#| eval: false
503
-
# Import StatsBase to use it's statistical methods.
504
-
using StatsBase
505
-
506
-
# Print out the coefficient table.
507
-
coeftable(mle_estimate)
508
-
```
509
-
510
-
511
-
<!-- table -->
512
-
```{.cell-bg}
513
-
─────────────────────────────
514
-
estimate stderror tstat
515
-
─────────────────────────────
516
-
s 0.0625 0.0625 1.0
517
-
m 1.75 0.176777 9.8995
518
-
─────────────────────────────
519
-
```
520
-
521
-
Standard errors are calculated from the Fisher information matrix (inverse Hessian of the log likelihood or log joint). t-statistics will be familiar to frequentist statisticians. Warning -- standard errors calculated in this way may not always be appropriate for MAP estimates, so please be cautious in interpreting them.
522
-
523
-
#### Sampling with the MAP/MLE as initial states
524
-
525
-
You can begin sampling your chain from an MLE/MAP estimate by extracting the vector of parameter values and providing it to the `sample` function with the keyword `initial_params`. For example, here is how to sample from the full posterior using the MAP estimate as the starting point:
526
-
527
-
```{julia}
528
-
#| eval: false
529
-
# Generate an MAP estimate.
530
-
map_estimate = optimize(model, MAP())
531
-
532
-
# Sample with the MAP estimate as the starting point.
0 commit comments