Tutorial
You can also download this tutorial as a Jupyter notebook and a plain Julia source file.
using MGVIusing Distributionsusing Randomusing ValueShapesusing LinearAlgebraimport Zygoteimport DistributionsAD # provides the AD rules Zygote needs for Distributionsusing AutoDiffOperatorsusing LinearSolve: KrylovJL_CGWe want to fit a 3-degree polynomial using two data sets (a and b). MGVI requires a model expressed as a function of the model parameters and returning an instance of the Distribution. In this example, since we have two sets of independent measurements, we express them as ValueShapes.NamedTupleDist.
We assume errors are normally distributed with unknown covariance, which has to be learned as well.
context = MGVIContext(ADSelector(Zygote))const _x1_grid = [Float64(i)/10 for i in 1:25]const _x2_grid = [Float64(i)/10 + 0.1 for i in 1:15]const _common_grid = sort(vcat(_x1_grid, _x2_grid))function _mean(x_grid, p) p[1]*10 .+ p[2]*40 .* x_grid .+ p[3]*600 .* x_grid.^2 .+ p[4]*80 .* x_grid.^3endfunction model(p) dist1 = product_distribution(Normal.(_mean(_x1_grid, p), p[5]^2*60)) dist2 = product_distribution(Normal.(_mean(_x2_grid, p), p[5]^2*60)) NamedTupleDist(a=dist1, b=dist2)endmodel (generic function with 1 method)Here we define the ground truth of the parameters, as well as an initial guess.
const true_params = [ -0.3 -1.5 0.2 -0.5 0.3]const starting_point = [ 0.2 0.5 -0.1 0.3 -0.6];function pprintln(obj) show(stdout, "text/plain", obj) println()end;using Plotsrng = MersenneTwister(157);We draw data directly from the model, using the true parameter values:
data = rand(rng, model(true_params), 1)[1];function _mean(x::Vector) _mean(_common_grid, x)endinit_plots =() -> let truth = _mean(true_params) plot!(_common_grid, truth, markercolor=:blue, linecolor=:blue, label="truth") scatter!(_common_grid, _mean(starting_point), markercolor=:orange, markerstrokewidth=0, markersize=3, label="init") scatter!(vcat(_x1_grid, _x2_grid), reduce(vcat, data), markercolor=:black, markerstrokewidth=0, markersize=3, label="data")end;Before we start the optimization, let's have an initial look at the data. It is also interesting to see how our starting guess performs.
p = plot()init_plots()savefig("tutorial-plot1.pdf")Now we are ready to run one iteration of the MGVI. The output contains an updated parameter estimate (center_point), which we can compare to the true parameters.
config = MGVIConfig( linsolver = KrylovJL_CG((;itmax=10)), optimizer = MGVI.NewtonCG())result, center_point = mgvi_step(model, data, 3, starting_point, config, context)@info hcat(center_point, true_params)psavefig("tutorial-plot2.pdf")[ Info: [0.2073024915003369 -0.3; 0.546025396549963 -1.5; 0.0020778771257229794 0.2; -0.041806200542254736 -0.5; -0.5471709127366212 0.3]plot_iteration = (result, center_point, label) -> let #error_mat = mgvi_kl_errors(full_model, params) #display(error_mat) #errors = sqrt.(error_mat[diagind(error_mat)]) #yerr = abs.(line(common_grid, params+errors) - line(common_grid, params-errors)) #scatter!(common_grid, line(common_grid, params), markercolor=:green, label=label, yerr=yerr) for sample in eachcol(result.samples) scatter!(_common_grid, _mean(Vector(sample)), markercolor=:gray, markeralpha=0.3, markersize=2, label=nothing) end scatter!(_common_grid, _mean(center_point), markercolor=:green, label=label)end;Now let's also plot the curve corresponding to the new parameters after the first iteration:
p = plot()init_plots()plot_iteration(result, center_point, "first")psavefig("tutorial-3.pdf")plot_iteration_light = (center_point, counter) -> let scatter!(_common_grid, _mean(center_point), markercolor=:green, markersize=3, markeralpha=2*atan(counter/18)/π, label=nothing)end;From the plot above we see that one iteration is not enough. Let's do 5 more steps and plot the evolution of estimates.
config = MGVIConfig( linsolver = KrylovJL_CG((;itmax=10)), optimizer = MGVI.NewtonCG())init_plots()plt = scatter()for i in 1:5 @info result.mnlp @info hcat(center_point, true_params) global result, center_point = mgvi_step(model, data, 10, center_point, config, context) plot_iteration_light(center_point, i)end@info minimum(result.mnlp)@info hcat(center_point, true_params)pltsavefig("tutorial-plot4.pdf")[ Info: 179.03318828908252
[ Info: [0.2073024915003369 -0.3; 0.546025396549963 -1.5; 0.0020778771257229794 0.2; -0.041806200542254736 -0.5; -0.5471709127366212 0.3]
[ Info: 148.0841910681067
[ Info: [-0.8791537110781024 -0.3; -0.864211513971891 -1.5; 0.15530157206737444 0.2; -0.40753214506627294 -0.5; -0.4241153866650011 0.3]
[ Info: 135.64530465819198
[ Info: [-0.9556886380873221 -0.3; -1.0358602310069946 -1.5; 0.17119567309256437 0.2; -0.44324412310800526 -0.5; -0.32350587422005056 0.3]
[ Info: 129.9768100588979
[ Info: [-0.3923313358872276 -0.3; -1.4498508046121599 -1.5; 0.19258929532452307 0.2; -0.47893062962774646 -0.5; -0.3052987208357476 0.3]
[ Info: 129.9876593724912
[ Info: [-0.3918907012974614 -0.3; -1.4492724465784725 -1.5; 0.19291375992983875 0.2; -0.4808027811552809 -0.5; -0.3023599286423117 0.3]
[ Info: 130.40839706696897
[ Info: [-0.2897316815465179 -0.3; -1.504936394742995 -1.5; 0.1954968480854686 0.2; -0.48491804761974955 -0.5; -0.30392306929898455 0.3]Finally, let's plot the last estimate and compare it to the truth. Also, notice, that gray dots represent samples from the approximation.
p = plot()init_plots()plot_iteration(result, center_point, "last")psavefig("tutorial-5.pdf")This page was generated using Literate.jl.