-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from TuringLang/ml/models
- Loading branch information
Showing
7 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,5 @@ Proposals.RSlice | |
Models | ||
Models.GaussianShells | ||
Models.CorrelatedGaussian | ||
Models.Eggbox | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Eggbox | ||
|
||
This example will explore the classic eggbox function using [`Models.Eggbox`](@ref). | ||
|
||
## Setup | ||
|
||
For this example, you'll need to add the following packages | ||
```julia | ||
julia>]add Distributions MCMCChains Measurements NestedSamplers StatsBase StatsPlots | ||
``` | ||
|
||
```@setup eggbox | ||
using AbstractMCMC | ||
using Random | ||
AbstractMCMC.setprogress!(false) | ||
Random.seed!(8452) | ||
``` | ||
|
||
## Define model | ||
|
||
```@example eggbox | ||
using NestedSamplers | ||
model, logz = Models.Eggbox() | ||
nothing; # hide | ||
``` | ||
|
||
let's take a look at a couple of parameters to see what the log-likelihood surface looks like | ||
|
||
```@example eggbox | ||
using StatsPlots | ||
x = range(0, 1, length=1000) | ||
y = range(0, 1, length=1000) | ||
logf = [model.loglike([xi, yi]) for yi in y, xi in x] | ||
heatmap( | ||
x, y, logf, | ||
xlims=extrema(x), | ||
ylims=extrema(y), | ||
xlabel="x", | ||
ylabel="y", | ||
) | ||
``` | ||
|
||
## Sample | ||
|
||
```@example eggbox | ||
using MCMCChains | ||
using StatsBase | ||
# using multi-ellipsoid for bounds | ||
# using default rejection sampler for proposals | ||
sampler = Nested(2, 500) | ||
chain, state = sample(model, sampler; dlogz=0.01, param_names=["x", "y"]) | ||
# resample chain using statistical weights | ||
chain_resampled = sample(chain, Weights(vec(chain[:weights])), length(chain)); | ||
nothing # hide | ||
``` | ||
|
||
## Results | ||
|
||
```@example eggbox | ||
chain_resampled | ||
``` | ||
|
||
```@example eggbox | ||
marginalkde(chain[:x], chain[:y]) | ||
plot!(xlims=(0, 1), ylims=(0, 1), sp=2) | ||
plot!(xlims=(0, 1), sp=1) | ||
plot!(ylims=(0, 1), sp=3) | ||
``` | ||
|
||
```@example eggbox | ||
density(chain_resampled, xlims=(0, 1)) | ||
vline!(0.1:0.2:0.9, c=:black, ls=:dash, sp=1) | ||
vline!(0.1:0.2:0.9, c=:black, ls=:dash, sp=2) | ||
``` | ||
|
||
```@example eggbox | ||
using Measurements | ||
logz_est = state.logz ± state.logzerr | ||
diff = logz_est - logz | ||
println("logz: $logz") | ||
println("estimate: $logz_est") | ||
println("diff: $diff") | ||
nothing # hide | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ using LogExpFunctions | |
|
||
include("shells.jl") | ||
include("correlated.jl") | ||
include("eggbox.jl") | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@doc raw""" | ||
Models.Eggbox() | ||
Eggbox/Egg carton likelihood function | ||
```math | ||
z(x, y) = \left[a + \cos\frac{x}{b} \cdot \cos\frac{x}{b} \right]^5 | ||
``` | ||
# Examples | ||
```jldoctest | ||
julia> model, lnZ = Models.Eggbox(); | ||
julia> lnZ | ||
235.88 | ||
``` | ||
""" | ||
function Eggbox() | ||
tmax = 5π | ||
|
||
# uniform prior from 0, 1 | ||
prior(X) = X | ||
function loglike(X) | ||
a = cos(tmax * (2 * first(X) - 1) / 2) | ||
b = cos(tmax * (2 * last(X) - 1) / 2) | ||
return (2 + a * b)^5 | ||
end | ||
|
||
lnZ = 235.88 # where do we get this from?? | ||
return NestedModel(loglike, prior), lnZ | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters