Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/GEK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,18 @@ end
# follows from the arithmetic, so a dual number is not truncated back to the
# design's type.
function _gek_r(k::GEK, val)
n = length(k.x)
d = length(k.x[1])
ks = [exp(-sum(k.theta[l] * (val[l] - k.x[i][l])^2 for l in 1:d)) for i in 1:n]
x, theta = k.x, k.theta
n = length(x)
d = length(x[1])
ks = [exp(-sum(theta[l] * (val[l] - x[i][l])^2 for l in 1:d)) for i in 1:n]
# Flattened in `_gek_deriv_index` order — point by point, and within a point,
# coordinate by coordinate — so the layout matches `_gek_covariance`. The
# index arithmetic inverts `n + (i - 1) * d + l`; a nested comprehension
# would read better but lowers to a `Flatten` iterator that reverse-mode AD
# accumulates with `push!`.
dks = [
let i = (j - 1) ÷ d + 1, l = (j - 1) % d + 1
2 * k.theta[l] * (val[l] - k.x[i][l]) * ks[i]
2 * theta[l] * (val[l] - x[i][l]) * ks[i]
end
for j in 1:(n * d)
]
Expand Down
8 changes: 5 additions & 3 deletions src/InverseDistanceSurrogate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ function (inverSurr::InverseDistanceSurrogate)(val)
_check_dimension(inverSurr, val)
# A row-matrix query would broadcast into a d x d outer difference.
point = _as_point(val)
# Keep the mutable surrogate out of the closures differentiated by Zygote.
x, y = inverSurr.x, inverSurr.y
p = inverSurr.p
d = [norm(point .- inverSurr.x[i]) for i in eachindex(inverSurr.x)]
d = [norm(point .- x[i]) for i in eachindex(x)]
d_min = minimum(d)
# A query on a sample point returns that response, averaged over ties,
# instead of Inf / Inf. Tested through the weight rather than `d_min == 0`
# because `norm` of a zero vector of duals is NaN, and a dual carrying a
# nonzero derivative is not equal to zero.
if !isfinite(d_min^(-p))
hits = findall(dᵢ -> !isfinite(dᵢ^(-p)), d)
return sum(inverSurr.y[i] for i in hits) / length(hits)
return sum(y[i] for i in hits) / length(hits)
end
# (d / d_min)^(-p) is the same convex combination as d^(-p) with the
# largest weight pinned at one, so nothing overflows or underflows.
w = (d ./ d_min) .^ (-p)
return sum(w[i] * inverSurr.y[i] for i in eachindex(w)) / sum(w)
return sum(w[i] * y[i] for i in eachindex(w)) / sum(w)
end

function SurrogatesBase.update!(inverSurr::InverseDistanceSurrogate, x_new, y_new)
Expand Down
7 changes: 4 additions & 3 deletions src/Kriging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ end
# containers (`(5.0)[1] === 5.0`), so one implementation serves both the scalar
# and the multidimensional case.
function _kriging_r(k::Kriging, val)
d = length(k.x[1])
x, theta, p = k.x, k.theta, k.p
d = length(x[1])
return [
exp(-sum(k.theta[l] * abs(val[l] - k.x[i][l])^k.p[l] for l in 1:d))
for i in eachindex(k.x)
exp(-sum(theta[l] * abs(val[l] - x[i][l])^p[l] for l in 1:d))
for i in eachindex(x)
]
end

Expand Down
10 changes: 6 additions & 4 deletions src/Lobachevsky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ end

function (loba::LobachevskySurrogate)(val::Number)
_check_dimension(loba, val)
x, alpha, n = loba.x, loba.alpha, loba.n
return _loba_combine(
loba.coeff,
j -> phi_nj1D(val, loba.x[j], loba.alpha, loba.n),
length(loba.x)
j -> phi_nj1D(val, x[j], alpha, n),
length(x)
)
end

Expand Down Expand Up @@ -195,10 +196,11 @@ end

function (loba::LobachevskySurrogate)(val)
_check_dimension(loba, val)
x, alpha, n = loba.x, loba.alpha, loba.n
return _loba_combine(
loba.coeff,
j -> phi_njND(val, loba.x[j], loba.alpha, loba.n),
length(loba.x)
j -> phi_njND(val, x[j], alpha, n),
length(x)
)
end

Expand Down
Loading