-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add particle inflation functionality
- Loading branch information
1 parent
5109b7f
commit a282c89
Showing
4 changed files
with
187 additions
and
1 deletion.
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
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,99 @@ | ||
using Distributions | ||
using LinearAlgebra | ||
using Random | ||
using Test | ||
|
||
using EnsembleKalmanProcesses | ||
using EnsembleKalmanProcesses.ParameterDistributions | ||
using EnsembleKalmanProcesses.Localizers | ||
import EnsembleKalmanProcesses: construct_mean, construct_cov, construct_sigma_ensemble | ||
const EKP = EnsembleKalmanProcesses | ||
|
||
|
||
# Read inverse problem definitions | ||
include("../EnsembleKalmanProcess/inverse_problem.jl") | ||
|
||
n_obs = 30 # dimension of synthetic observation from G(u) | ||
ϕ_star = [-1.0, 2.0] # True parameters in constrained space | ||
n_par = size(ϕ_star, 1) | ||
noise_level = 0.1 # Defining the observation noise level (std) | ||
N_ens = 50 # number of ensemble members | ||
N_iter = 10 | ||
|
||
|
||
obs_corrmat = Diagonal(Matrix(I, n_obs, n_obs)) | ||
|
||
prior_1 = Dict("distribution" => Parameterized(Normal(0.0, 0.5)), "constraint" => bounded(-2, 2), "name" => "cons_p") | ||
prior_2 = Dict("distribution" => Parameterized(Normal(3.0, 0.5)), "constraint" => no_constraint(), "name" => "uncons_p") | ||
prior = ParameterDistribution([prior_1, prior_2]) | ||
prior_mean = mean(prior) | ||
prior_cov = cov(prior) | ||
|
||
|
||
# Define a few inverse problems to compare algorithmic performance | ||
rng_seed = 42 | ||
rng = Random.MersenneTwister(rng_seed) | ||
|
||
|
||
initial_ensemble = EKP.construct_initial_ensemble(rng, prior, N_ens) | ||
|
||
Δts = [0.5, 0.75, 0.95] | ||
|
||
@testset "Inflation" begin | ||
|
||
ekiobj = nothing | ||
|
||
for Δt_i in 1:length(Δts) | ||
Δt = Δts[Δt_i] | ||
|
||
# Get inverse problem | ||
y_obs, G, Γy, A = | ||
linear_inv_problem(ϕ_star, noise_level, n_obs, prior, rng; obs_corrmat = obs_corrmat, return_matrix = true) | ||
|
||
ekiobj = EKP.EnsembleKalmanProcess( | ||
initial_ensemble, | ||
y_obs, | ||
Γy, | ||
Inversion(); | ||
Δt = Δt, | ||
rng = rng, | ||
failure_handler_method = SampleSuccGauss(), | ||
) | ||
|
||
g_ens = G(get_u_final(ekiobj)) | ||
|
||
# EKI iterations | ||
for i in 1:N_iter | ||
# Check SampleSuccGauss handler | ||
params_i = get_u_final(ekiobj) | ||
|
||
g_ens = G(params_i) | ||
|
||
# standard update | ||
EKP.update_ensemble!(ekiobj, g_ens, EKP.get_process(ekiobj)) | ||
ekp_standard = deepcopy(ekiobj) | ||
# add noise to parameters | ||
EKP.multiplicative_inflation!(ekiobj) | ||
ekp_stochastic = deepcopy(ekiobj) | ||
|
||
# ensure stochastic update preserves ensemble mean | ||
@test get_u_mean_final(ekp_standard) ≈ get_u_mean_final(ekp_stochastic) atol = 1e-10 | ||
|
||
# ensure stochastic update increases variance of ensemble | ||
ϕ_final_standard = get_ϕ_final(prior, ekp_standard) | ||
ϕ_final_stochastic = get_ϕ_final(prior, ekp_stochastic) | ||
ϕ_mean_final_standard = get_ϕ_mean_final(prior, ekp_standard) | ||
ϕ_mean_final_stochastic = get_ϕ_mean_final(prior, ekp_stochastic) | ||
@test norm(ϕ_final_stochastic .- ϕ_mean_final_stochastic) > norm(ϕ_final_standard .- ϕ_mean_final_standard) | ||
|
||
# ensure parameter noise is only added in final iteration | ||
u_standard = get_u(ekp_standard) | ||
u_stochastic = get_u(ekp_stochastic) | ||
@test u_standard[1:(end - 1)] == u_stochastic[1:(end - 1)] | ||
@test u_standard[end] != u_stochastic[end] | ||
|
||
end | ||
end | ||
# stochastic update should not affect initial parameter ensemble (drawn from prior) | ||
@test get_u_prior(ekiobj) == initial_ensemble | ||
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