Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Handle missing values in Kriging
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Aug 27, 2022
1 parent 910ac26 commit 12cd375
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/estimation/kriging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ end

function preprocess(problem::EstimationProblem, solver::Kriging)
# retrieve problem info
pdata = data(problem)
pdata = data(problem)
dtable = values(pdata)
ddomain = domain(pdata)
pdomain = domain(problem)

# result of preprocessing
preproc = Dict{Symbol,NamedTuple}()
Expand All @@ -83,8 +86,23 @@ function preprocess(problem::EstimationProblem, solver::Kriging)
# get user parameters
varparams = covars.params[(var,)]

# find non-missing samples for variable
cols = Tables.columns(dtable)
vals = Tables.getcolumn(cols, var)
inds = findall(!ismissing, vals)

# assert at least one sample is non-missing
if isempty(inds)
throw(AssertionError("all samples of $var are missing, aborting..."))
end

# subset of non-missing samples
vtable = (;var => collect(skipmissing(vals)))
vdomain = view(ddomain, inds)
samples = georef(vtable, vdomain)

# determine which Kriging variant to use
estimator = kriging_ui(domain(pdata),
estimator = kriging_ui(pdomain,
varparams.variogram,
varparams.mean,
varparams.degree,
Expand All @@ -95,13 +113,14 @@ function preprocess(problem::EstimationProblem, solver::Kriging)
maxneighbors = varparams.maxneighbors

# determine bounded search method
bsearcher = searcher_ui(domain(pdata),
bsearcher = searcher_ui(vdomain,
varparams.maxneighbors,
varparams.distance,
varparams.neighborhood)

# save preprocessed input
preproc[var] = (estimator=estimator,
preproc[var] = (samples=samples,
estimator=estimator,
minneighbors=minneighbors,
maxneighbors=maxneighbors,
bsearcher=bsearcher)
Expand All @@ -112,25 +131,38 @@ function preprocess(problem::EstimationProblem, solver::Kriging)
end

function solve(problem::EstimationProblem, solver::Kriging)
# retrive problem info
pdomain = domain(problem)

# preprocess user input
preproc = preprocess(problem, solver)

# results for each variable
μs = []; σs = []
for var in name.(variables(problem))
if preproc[var].maxneighbors nothing
# perform Kriging with reduced number of neighbors
varμ, varσ = solve_approx(problem, var, preproc)
# maximum number of neighbors
maxneighbors = preproc[var].maxneighbors

# non-missing samples
samples = preproc[var].samples

# problem for variable
prob = EstimationProblem(samples, pdomain, var)

# exact vs. approximate Kriging
if isnothing(maxneighbors)
# perform Kriging with all samples as neighbors
varμ, varσ = solve_exact(prob, var, preproc)
else
# perform Kriging with all data points as neighbors
varμ, varσ = solve_exact(problem, var, preproc)
# perform Kriging with reduced number of neighbors
varμ, varσ = solve_approx(prob, var, preproc)
end

push!(μs, var => varμ)
push!(σs, Symbol(var,"_variance") => varσ)
end

georef((; μs..., σs...), domain(problem))
georef((; μs..., σs...), pdomain)
end

function solve_approx(problem::EstimationProblem, var::Symbol, preproc)
Expand All @@ -139,7 +171,10 @@ function solve_approx(problem::EstimationProblem, var::Symbol, preproc)
pdomain = domain(problem)

# unpack preprocessed parameters
estimator, minneighbors, maxneighbors, bsearcher = preproc[var]
estimator = preproc[var].estimator
minneighbors = preproc[var].minneighbors
maxneighbors = preproc[var].maxneighbors
bsearcher = preproc[var].bsearcher

# pre-allocate memory for neighbors
neighbors = Vector{Int}(undef, maxneighbors)
Expand Down Expand Up @@ -186,7 +221,7 @@ function solve_exact(problem::EstimationProblem, var::Symbol, preproc)
pdomain = domain(problem)

# unpack preprocessed parameters
estimator, minneighbors, maxneighbors, bsearcher = preproc[var]
estimator = preproc[var].estimator

# retrieve non-missing data
locs = findall(!ismissing, pdata[var])
Expand Down

2 comments on commit 12cd375

@juliohm
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/66990

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 12cd3759d74448019a3db3106615c4fa3c41c124
git push origin v0.1.0

Please sign in to comment.