|
| 1 | +using Distributions |
| 2 | +using EnsembleKalmanProcesses |
| 3 | +using JLD2 |
| 4 | +using LinearAlgebra |
| 5 | +using Random |
| 6 | +using MCMCChains |
| 7 | +using AdvancedMH |
| 8 | + |
| 9 | +# CES |
| 10 | +using CalibrateEmulateSample.Emulators |
| 11 | +using CalibrateEmulateSample.MarkovChainMonteCarlo |
| 12 | +using CalibrateEmulateSample.Utilities |
| 13 | + |
| 14 | + |
| 15 | +include("./models.jl") |
| 16 | + |
| 17 | +rng = Random.MersenneTwister(41) |
| 18 | +input_dim = 100 |
| 19 | +output_dim = 100 |
| 20 | + |
| 21 | +num_trials = 1 |
| 22 | +for trial in 1:num_trials |
| 23 | + loaded = load("datafiles/ekp_linlinexp_$(trial).jld2") |
| 24 | + ekpobj = loaded["ekpobj"] |
| 25 | + ekp_samples = loaded["ekp_samples"] |
| 26 | + prior = loaded["prior_obj"] |
| 27 | + obs_noise_cov = loaded["obs_noise_cov"] |
| 28 | + y = loaded["y"] |
| 29 | + model = loaded["model"] |
| 30 | + true_parameter = loaded["true_parameter"] |
| 31 | + αs = loaded["alphas"] |
| 32 | + |
| 33 | + min_iter = 1 |
| 34 | + max_iter = min(10, length(αs)) # number of EKP iterations to use data from is at most this |
| 35 | + training_points = [ekp_samples[α] for α in αs[min_iter:max_iter]] |
| 36 | + |
| 37 | + encoder_schedule_ref = [(decorrelate_structure_mat(; retain_var = 1.0), "in_and_out")] |
| 38 | + |
| 39 | + rvs = collect(0.975:0.0125:1.0) |
| 40 | + rkls = collect(0.999:0.0005:1.0) |
| 41 | + all_errs = zeros(length(rvs), 1 + length(αs)) |
| 42 | + |
| 43 | + # as we have more input samples than out stored in ekp. we provide the extended set of outputs to be used in the likelihood informed processor |
| 44 | + final_samples_out = ekp_samples[αs[end]][2] |
| 45 | + encoder_kwargs = encoder_kwargs_from(ekpobj, prior; final_samples_out=final_samples_out) |
| 46 | + |
| 47 | + flat_io_pairs = get_training_points(ekpobj, min_iter:max_iter, g_final=final_samples_out) |
| 48 | + |
| 49 | + names = ["reference", "decorrelate, PCA-in", ["decorrelate, LI-in 1:$(i)" for i in 1:length(αs)]...] |
| 50 | + |
| 51 | + ni_scaling = 0.01 # noise injection into null space scaling (def. 1) |
| 52 | + |
| 53 | + for (idx, (rv, rkl)) in enumerate(zip(rvs, rkls)) |
| 54 | + encoder_schedule_decorrelate = [ |
| 55 | + (decorrelate_structure_mat(retain_var = rv), "in"), |
| 56 | + (decorrelate_structure_mat(), "out"), |
| 57 | + ] |
| 58 | + encoder_schedules_li = [ |
| 59 | + [ |
| 60 | + (decorrelate_structure_mat(), "in_and_out"), |
| 61 | + (likelihood_informed(retain_kl=rkl, iters=1:i), "in"), |
| 62 | + ] for i in 1:length(αs) |
| 63 | + ] |
| 64 | + |
| 65 | + em_ref = forward_map_wrapper( |
| 66 | + param -> forward_map(param, model), |
| 67 | + prior, |
| 68 | + flat_io_pairs; |
| 69 | + encoder_schedule = encoder_schedule_ref, |
| 70 | + encoder_kwargs = deepcopy(encoder_kwargs), |
| 71 | + noise_injector_scaling=ni_scaling, # shouldnt be needed |
| 72 | + ) |
| 73 | + |
| 74 | + em_decorrelate = forward_map_wrapper( |
| 75 | + param -> forward_map(param, model), |
| 76 | + prior, |
| 77 | + flat_io_pairs; |
| 78 | + encoder_schedule = encoder_schedule_decorrelate, |
| 79 | + encoder_kwargs = deepcopy(encoder_kwargs), |
| 80 | + noise_injector_scaling=ni_scaling, |
| 81 | + ) |
| 82 | + |
| 83 | + ems_li = [ |
| 84 | + forward_map_wrapper( |
| 85 | + param -> forward_map(param, model), |
| 86 | + prior, |
| 87 | + flat_io_pairs; |
| 88 | + encoder_schedule = encoder_schedule, |
| 89 | + encoder_kwargs = deepcopy(encoder_kwargs), |
| 90 | + noise_injector_scaling=ni_scaling, |
| 91 | + ) for encoder_schedule in encoder_schedules_li |
| 92 | + ] |
| 93 | + |
| 94 | + post_means = reshape(true_parameter, input_dim, 1) |
| 95 | + post_covs = [] |
| 96 | + |
| 97 | + for (nn, em) in zip(names,vcat(em_ref, em_decorrelate, ems_li...)) |
| 98 | + println(" ") |
| 99 | + @info "Encoding name: $(nn)" |
| 100 | + E,_ = get_encoder_from_schedule(get_encoder_schedule(em), "in") |
| 101 | + if isnothing(E) |
| 102 | + @info "No truncation" |
| 103 | + else |
| 104 | + |
| 105 | + @info "Truncation criteria (var>$(rv), or kl > $(rkl))" |
| 106 | + @info "input dim reduced to: $(size(E,1))" |
| 107 | + |
| 108 | + end |
| 109 | + println(" ") |
| 110 | + u0 = rand(MvNormal(mean(prior), cov(prior))) |
| 111 | + mcmc = MCMCWrapper(RWMHSampling(), y, prior, em; init_params = u0) |
| 112 | + new_step = optimize_stepsize(mcmc; init_stepsize = 0.05, N = 2000, discard_initial = 0) |
| 113 | + |
| 114 | + println("Begin MCMC - with step size ", new_step) |
| 115 | + mcmc = MCMCWrapper(RWMHSampling(), y, prior, em; init_params = u0) |
| 116 | + chain = MarkovChainMonteCarlo.sample( |
| 117 | + mcmc, |
| 118 | + 40_000; |
| 119 | + stepsize = new_step, |
| 120 | + discard_initial = 5_000, |
| 121 | + ) |
| 122 | + posterior = MarkovChainMonteCarlo.get_posterior(mcmc, chain) |
| 123 | + |
| 124 | + post_mean = mean(posterior) |
| 125 | + post_cov = cov(posterior) |
| 126 | + |
| 127 | + post_means = hcat(post_means, reshape(post_mean, input_dim, 1)) |
| 128 | + push!(post_covs, post_cov) |
| 129 | + end |
| 130 | + |
| 131 | + # post_means[:,1] = true parameter |
| 132 | + # post_means[:,2] = ref |
| 133 | + # post_means[:,3] = decor, LI etc. |
| 134 | + # so err cols are normalized diff to ref of (decor, LI etc.) (lower is better) |
| 135 | + all_errs[idx, :] = |
| 136 | + [norm(post_means[:, 2] - v) / norm(post_means[:, 2]) for v in eachcol(post_means[:, 3:end])]' |
| 137 | + end |
| 138 | + @info "error of posterior mean to whitened \"reference\" solution. for $(names[2:end])" |
| 139 | + display(all_errs) |
| 140 | +end |
0 commit comments