-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bad162
commit 7c779a0
Showing
3 changed files
with
320 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#Ensemble Transform Kalman Inversion: specific structures and function definitions | ||
|
||
""" | ||
TransformInversion <: Process | ||
An ensemble transform Kalman inversion process. | ||
# Fields | ||
$(TYPEDFIELDS) | ||
""" | ||
struct TransformInversion{FT <: AbstractFloat} <: Process | ||
"Inverse of the observation error covariance matrix" | ||
Γ_inv::Union{AbstractMatrix{FT}, UniformScaling{FT}} | ||
end | ||
|
||
function FailureHandler(process::TransformInversion, method::IgnoreFailures) | ||
failsafe_update(ekp, u, g, y, obs_noise_cov, failed_ens) = etki_update(ekp, u, g, y, obs_noise_cov) | ||
return FailureHandler{TransformInversion, IgnoreFailures}(failsafe_update) | ||
end | ||
|
||
""" | ||
FailureHandler(process::TransformInversion, method::SampleSuccGauss) | ||
Provides a failsafe update that | ||
- updates the successful ensemble according to the ETKI update, | ||
- updates the failed ensemble by sampling from the updated successful ensemble. | ||
""" | ||
function FailureHandler(process::TransformInversion, method::SampleSuccGauss) | ||
function failsafe_update(ekp, u, g, y, obs_noise_cov, failed_ens) | ||
successful_ens = filter(x -> !(x in failed_ens), collect(1:size(g, 2))) | ||
n_failed = length(failed_ens) | ||
u[:, successful_ens] = etki_update(ekp, u[:, successful_ens], g[:, successful_ens], y, obs_noise_cov) | ||
if !isempty(failed_ens) | ||
u[:, failed_ens] = sample_empirical_gaussian(u[:, successful_ens], n_failed) | ||
end | ||
return u | ||
end | ||
return FailureHandler{TransformInversion, SampleSuccGauss}(failsafe_update) | ||
end | ||
|
||
""" | ||
etki_update( | ||
ekp::EnsembleKalmanProcess{FT, IT, TransformInversion}, | ||
u::AbstractMatrix{FT}, | ||
g::AbstractMatrix{FT}, | ||
y::AbstractVector{FT}, | ||
obs_noise_cov::Union{AbstractMatrix{CT}, UniformScaling{CT}}, | ||
) where {FT <: Real, IT, CT <: Real} | ||
Returns the updated parameter vectors given their current values and | ||
the corresponding forward model evaluations. | ||
""" | ||
function etki_update( | ||
ekp::EnsembleKalmanProcess{FT, IT, TransformInversion{FT}}, | ||
u::AbstractMatrix{FT}, | ||
g::AbstractMatrix{FT}, | ||
y::AbstractVector{FT}, | ||
obs_noise_cov::Union{AbstractMatrix{CT}, UniformScaling{CT}}, | ||
) where {FT <: Real, IT, CT <: Real} | ||
m = size(u, 2) | ||
Γ_inv = ekp.process.Γ_inv | ||
|
||
X = FT.((u .- mean(u, dims = 2)) / sqrt(m - 1)) | ||
Y = FT.((g .- mean(g, dims = 2)) / sqrt(m - 1)) | ||
Ω = inv(I + Y' * Γ_inv * Y) | ||
w = FT.(Ω * Y' * Γ_inv * (y .- mean(g, dims = 2))) | ||
|
||
return mean(u, dims = 2) .+ X * (w .+ sqrt(m - 1) * real(sqrt(Ω))) # [N_par × N_ens] | ||
end | ||
|
||
""" | ||
update_ensemble!( | ||
ekp::EnsembleKalmanProcess{FT, IT, TransformInversion}, | ||
g::AbstractMatrix{FT}, | ||
process::TransformInversion; | ||
failed_ens = nothing, | ||
) where {FT, IT} | ||
Updates the ensemble according to a TransformInversion process. | ||
Inputs: | ||
- ekp :: The EnsembleKalmanProcess to update. | ||
- g :: Model outputs, they need to be stored as a `N_obs × N_ens` array (i.e data are columms). | ||
- process :: Type of the EKP. | ||
- failed_ens :: Indices of failed particles. If nothing, failures are computed as columns of `g` with NaN entries. | ||
""" | ||
function update_ensemble!( | ||
ekp::EnsembleKalmanProcess{FT, IT, TransformInversion{FT}}, | ||
g::AbstractMatrix{FT}, | ||
process::TransformInversion{FT}; | ||
failed_ens = nothing, | ||
) where {FT, IT} | ||
|
||
# u: N_par × N_ens | ||
# g: N_obs × N_ens | ||
u = get_u_final(ekp) | ||
N_obs = size(g, 1) | ||
cov_init = cov(u, dims = 2) | ||
|
||
if ekp.verbose | ||
if get_N_iterations(ekp) == 0 | ||
@info "Iteration 0 (prior)" | ||
@info "Covariance trace: $(tr(cov_init))" | ||
end | ||
|
||
@info "Iteration $(get_N_iterations(ekp)+1) (T=$(sum(ekp.Δt)))" | ||
end | ||
|
||
fh = ekp.failure_handler | ||
|
||
# Scale noise using Δt | ||
scaled_obs_noise_cov = ekp.obs_noise_cov / ekp.Δt[end] | ||
|
||
y = ekp.obs_mean | ||
|
||
if isnothing(failed_ens) | ||
_, failed_ens = split_indices_by_success(g) | ||
end | ||
if !isempty(failed_ens) | ||
@info "$(length(failed_ens)) particle failure(s) detected. Handler used: $(nameof(typeof(fh).parameters[2]))." | ||
end | ||
|
||
u = fh.failsafe_update(ekp, u, g, y, scaled_obs_noise_cov, failed_ens) | ||
|
||
# store new parameters (and model outputs) | ||
push!(ekp.u, DataContainer(u, data_are_columns = true)) | ||
push!(ekp.g, DataContainer(g, data_are_columns = true)) | ||
|
||
# Store error | ||
compute_error!(ekp) | ||
|
||
# Diagnostics | ||
cov_new = cov(get_u_final(ekp), dims = 2) | ||
|
||
if ekp.verbose | ||
@info "Covariance-weighted error: $(get_error(ekp)[end])\nCovariance trace: $(tr(cov_new))\nCovariance trace ratio (current/previous): $(tr(cov_new)/tr(cov_init))" | ||
end | ||
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