|
| 1 | +# How to model indenpendent parameter-blocks in the posterior |
| 2 | + |
| 3 | + |
| 4 | +``` @meta |
| 5 | +CurrentModule = HybridVariationalInference |
| 6 | +``` |
| 7 | + |
| 8 | +This guide shows how to configure independent parameter-blocks in the correlations |
| 9 | +of the posterior. |
| 10 | + |
| 11 | +## Motivation |
| 12 | + |
| 13 | +Modelling all correlations among global and site PBM-parameters respectively |
| 14 | +requires many degrees of freedom. |
| 15 | + |
| 16 | +To decrease the number of parameters to estimate, HVI allows to decompose the |
| 17 | +correlations into independent sub-blocks of parameters. |
| 18 | + |
| 19 | +First load necessary packages. |
| 20 | + |
| 21 | +``` julia |
| 22 | +using HybridVariationalInference |
| 23 | +using ComponentArrays: ComponentArrays as CA |
| 24 | +using Bijectors |
| 25 | +using SimpleChains |
| 26 | +using MLUtils |
| 27 | +using JLD2 |
| 28 | +using Random |
| 29 | +using CairoMakie |
| 30 | +using PairPlots # scatterplot matrices |
| 31 | +``` |
| 32 | + |
| 33 | +This tutorial reuses and modifies the fitted object saved at the end of the |
| 34 | +[Basic workflow without GPU](@ref) tutorial. |
| 35 | + |
| 36 | +``` julia |
| 37 | +fname = "intermediate/basic_cpu_results.jld2" |
| 38 | +print(abspath(fname)) |
| 39 | +prob = probo_cor = load(fname, "probo"); |
| 40 | +``` |
| 41 | + |
| 42 | +## Specifying blocks in correlation structure |
| 43 | + |
| 44 | +HVI models the posterior of the parameters at unconstrained scale using a |
| 45 | +multivariate normal distribution. It estimates a parameterization of the |
| 46 | +associated blocks in the correlation matrx and requires a specification |
| 47 | +of the block-structure. |
| 48 | + |
| 49 | +This is done by specifying the positions of the end of the blocks for |
| 50 | +the global (P) and the site-specific parameters (M) respectively using |
| 51 | +a `NamedTuple` of integer vectors. |
| 52 | + |
| 53 | +The defaults specifies a single entry, meaning, there is only one big |
| 54 | +block respectively, spanning all parameters. |
| 55 | + |
| 56 | +``` julia |
| 57 | +cor_ends0 = (P=[length(prob.θP)], M=[length(prob.θM)]) |
| 58 | +``` |
| 59 | + |
| 60 | + (P = [1], M = [2]) |
| 61 | + |
| 62 | +The following specification models one-entry blocks for each each parameter |
| 63 | +in the correlation block the site parameters, i.e. treating all parameters |
| 64 | +independently with not modelling any correlations between them. |
| 65 | + |
| 66 | +``` julia |
| 67 | +cor_ends = (P=[length(prob.θP)], M=1:length(prob.θM)) |
| 68 | +``` |
| 69 | + |
| 70 | + (P = [1], M = 1:2) |
| 71 | + |
| 72 | +## Reinitialize parameters for the posterior approximation. |
| 73 | + |
| 74 | +HVI uses additional fitted parameters to represent the means and the |
| 75 | +covariance matrix of the posterior distribution of model parameters. |
| 76 | +With fewer correlations, also the number of those parameters changes, |
| 77 | +and those parameters must be reinitialized after changing the block structure in |
| 78 | +the correlation matrix. |
| 79 | + |
| 80 | +Here, we obtain construct initial estimates. using [`init_hybrid_ϕunc`](@ref) |
| 81 | + |
| 82 | +``` julia |
| 83 | +ϕunc = init_hybrid_ϕunc(cor_ends, zero(eltype(prob.θM))) |
| 84 | +``` |
| 85 | + |
| 86 | +In this two-site parameter case, the the blocked structure saves only one degree of freedom: |
| 87 | + |
| 88 | +``` julia |
| 89 | +length(ϕunc), length(probo_cor.ϕunc) |
| 90 | +``` |
| 91 | + |
| 92 | + (5, 6) |
| 93 | + |
| 94 | +## Update the problem and redo the inversion |
| 95 | + |
| 96 | +``` julia |
| 97 | +prob_ind = HybridProblem(prob; cor_ends, ϕunc) |
| 98 | +``` |
| 99 | + |
| 100 | +``` julia |
| 101 | +using OptimizationOptimisers |
| 102 | +import Zygote |
| 103 | + |
| 104 | +solver = HybridPosteriorSolver(; alg=Adam(0.02), n_MC=3) |
| 105 | + |
| 106 | +(; probo) = solve(prob_ind, solver; |
| 107 | + callback = callback_loss(100), # output during fitting |
| 108 | + epochs = 20, |
| 109 | +); probo_ind = probo; |
| 110 | +``` |
| 111 | + |
| 112 | +## Compare the correated vs. uncorrelated posterior |
| 113 | + |
| 114 | +First, draw a sample. |
| 115 | + |
| 116 | +``` julia |
| 117 | +n_sample_pred = 400 |
| 118 | +(y_cor, θsP_cor, θsMs_cor) = (; y, θsP, θsMs) = predict_hvi( |
| 119 | + Random.default_rng(), probo_cor; n_sample_pred) |
| 120 | +(y_ind, θsP_ind, θsMs_ind) = (; y, θsP, θsMs) = predict_hvi( |
| 121 | + Random.default_rng(), probo_ind; n_sample_pred) |
| 122 | +``` |
| 123 | + |
| 124 | +``` julia |
| 125 | +i_site = 1 |
| 126 | +θ1 = vcat(θsP_ind, θsMs_ind[i_site,:,:]) |
| 127 | +θ1_nt = NamedTuple(k => CA.getdata(θ1[k,:]) for k in keys(θ1[:,1])) # |
| 128 | +plt = pairplot(θ1_nt) |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +The corner plot of the independent-parameters estimate shows |
| 134 | +no correlations between site parameters, *r*₁ and *K*₁. |
| 135 | + |
| 136 | +``` julia |
| 137 | +i_out = 4 |
| 138 | +fig = Figure(); ax = Axis(fig[1,1], xlabel="mean(y)",ylabel="sd(y)") |
| 139 | +ymean_cor = [mean(y_cor[i_out,s,:]) for s in axes(y_cor, 2)] |
| 140 | +ysd_cor = [std(y_cor[i_out,s,:]) for s in axes(y_cor, 2)] |
| 141 | +scatter!(ax, ymean_cor, ysd_cor, label="correlated") |
| 142 | +ymean_ind = [mean(y_ind[i_out,s,:]) for s in axes(y_ind, 2)] |
| 143 | +ysd_ind = [std(y_ind[i_out,s,:]) for s in axes(y_ind, 2)] |
| 144 | +scatter!(ax, ymean_ind, ysd_ind, label="independent") |
| 145 | +axislegend(ax, unique=true) |
| 146 | +fig |
| 147 | +``` |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +``` julia |
| 152 | +plot_sd_vs_mean = (par) -> begin |
| 153 | + fig = Figure(); ax = Axis(fig[1,1], xlabel="mean($par)",ylabel="sd($par)") |
| 154 | + θmean_cor = [mean(θsMs_cor[s,par,:]) for s in axes(θsMs_cor, 1)] |
| 155 | + θsd_cor = [std(θsMs_cor[s,par,:]) for s in axes(θsMs_cor, 1)] |
| 156 | + scatter!(ax, θmean_cor, θsd_cor, label="correlated") |
| 157 | + θmean_ind = [mean(θsMs_ind[s,par,:]) for s in axes(θsMs_ind, 1)] |
| 158 | + θsd_ind = [std(θsMs_ind[s,par,:]) for s in axes(θsMs_ind, 1)] |
| 159 | + scatter!(ax, θmean_ind, θsd_ind, label="independent") |
| 160 | + axislegend(ax, unique=true) |
| 161 | + fig |
| 162 | +end |
| 163 | +plot_sd_vs_mean(:K1) |
| 164 | +``` |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +The inversion that neglects correlations among site parameters results in |
| 169 | +the same magnitude of estimated uncertainty of predictions. |
| 170 | +However, the uncertainty of the model parameters is severely underestimated |
| 171 | +in this example. |
0 commit comments