-
Notifications
You must be signed in to change notification settings - Fork 17
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
Melanie Bieli
committed
Jun 18, 2020
1 parent
bb93635
commit ba9b037
Showing
14 changed files
with
1,152 additions
and
78 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,10 +4,17 @@ authors = ["Charles Kawczynski <[email protected]>"] | |
version = "0.1.0" | ||
|
||
[deps] | ||
Cloudy = "9e3b23bb-e7cc-4b94-886c-65de2234ba87" | ||
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" | ||
GaussianProcesses = "891a1506-143c-57d2-908e-e1f8e92e6de9" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Optim = "429524aa-4258-5aef-a3af-852621145aeb" | ||
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | ||
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
ScikitLearn = "3646fa90-6ef7-5e7e-9f22-8aca16db6324" | ||
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
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
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
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,64 @@ | ||
using Distributions | ||
using LinearAlgebra | ||
using Random | ||
using Test | ||
|
||
using CalibrateEmulateSample.EKI | ||
|
||
@testset "EKI" begin | ||
|
||
# Seed for pseudo-random number generator | ||
rng_seed = 41 | ||
Random.seed!(rng_seed) | ||
|
||
### Define priors on the parameters u | ||
umin = 0.0 | ||
umax = 20.0 | ||
priors = [Uniform(umin, umax), # prior on u1 | ||
Uniform(umin, umax)] # prior on u2 | ||
|
||
### Define forward model G | ||
function G(u) | ||
3.0 .* u | ||
end | ||
|
||
### Generate (artificial) truth samples | ||
npar = 2 # number of parameters | ||
ut = rand(Uniform(umin, umax), npar) | ||
param_names = ["u1", "u2"] | ||
yt = G(ut) | ||
n_samples = 100 | ||
truth_samples = zeros(n_samples, length(yt)) | ||
noise_level = 0.9 | ||
Γy = noise_level^2 * convert(Array, Diagonal(yt)) | ||
μ = zeros(length(yt)) | ||
for i in 1:n_samples | ||
truth_samples[i, :] = yt + noise_level^2 * rand(MvNormal(μ, Γy)) | ||
end | ||
|
||
### Calibrate: Ensemble Kalman Inversion | ||
N_ens = 50 # number of ensemble members | ||
N_iter = 5 # number of EKI iterations | ||
initial_params = EKI.construct_initial_ensemble(N_ens, priors; | ||
rng_seed=rng_seed) | ||
ekiobj = EKI.EKIObj(initial_params, param_names, | ||
vec(mean(truth_samples, dims=1)), Γy) | ||
|
||
@test size(initial_params) == (N_ens, npar) | ||
@test size(ekiobj.u[end]) == (N_ens, npar) | ||
@test ekiobj.unames == ["u1", "u2"] | ||
@test ekiobj.g_t ≈ [37.29, 15.49] atol=1e-1 | ||
@test ekiobj.cov ≈ [30.29 0.0; 0.0 12.29] atol=1e-1 | ||
@test ekiobj.N_ens == N_ens | ||
|
||
# EKI iterations | ||
for i in 1:N_iter | ||
params_i = ekiobj.u[end] | ||
g_ens = G(params_i) | ||
EKI.update_ensemble!(ekiobj, g_ens) | ||
end | ||
|
||
# EKI results: Test if ensemble has collapsed toward the true parameter values | ||
eki_final_result = vec(mean(ekiobj.u[end], dims=1)) | ||
@test norm(ut - eki_final_result) < 0.5 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Import modules | ||
using Random | ||
using GaussianProcesses | ||
using Test | ||
|
||
using CalibrateEmulateSample.GPEmulator | ||
|
||
@testset "GPEmulator" begin | ||
|
||
# Seed for pseudo-random number generator | ||
rng_seed = 41 | ||
Random.seed!(rng_seed) | ||
|
||
# Training data | ||
n = 10 # number of training points | ||
x = 2.0 * π * rand(n) # predictors/features | ||
y = sin.(x) + 0.05 * randn(n) # predictands/targets | ||
|
||
gppackage = GPJL() | ||
pred_type = YType() | ||
|
||
# Construct kernel: | ||
# Squared exponential kernel (note that hyperparameters are on log scale) | ||
kern = SE(0.0, 0.0) | ||
# log standard deviation of observational noise | ||
logObsNoise = -1.0 | ||
white = Noise(logObsNoise) | ||
GPkernel = kern + white | ||
|
||
# Fit Gaussian Process Regression model | ||
gpobj = GPObj(x, y, gppackage; GPkernel=GPkernel, normalized=false, | ||
prediction_type=pred_type) | ||
new_inputs = [0.0, π/2, π, 3*π/2, 2*π] | ||
μ, σ² = GPEmulator.predict(gpobj, new_inputs) | ||
|
||
@test_throws Exception GPObj(x, y, gppackage; GPkernel=GPkernel, | ||
normalized=true, prediction_type=pred_type) | ||
@test gpobj.inputs == x | ||
@test gpobj.data == y | ||
@test gpobj.input_mean[1] ≈ 3.524 atol=1e-2 | ||
@test gpobj.sqrt_inv_input_cov == nothing | ||
@test gpobj.prediction_type == pred_type | ||
@test μ ≈ [0.0, 1.0, 0.0, -1.0, 0.0] atol=1.0 | ||
@test σ² ≈ [0.859, 0.591, 0.686, 0.645, 0.647] atol=1e-2 | ||
|
||
end |
Oops, something went wrong.