Skip to content

Commit

Permalink
Fix numerical instability in transiogram models
Browse files Browse the repository at this point in the history
juliohm committed Jan 14, 2025
1 parent a9a9539 commit e9f6316
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/theoretical/transiogram/exponential.jl
Original file line number Diff line number Diff line change
@@ -28,5 +28,6 @@ function (t::ExponentialTransiogram)(h)
h′, r′ = unitless(h, r)
v = 1 - exp(-3(h′ / r′))
T = typeof(p[1] * v)
SMatrix{L,L}(i == j ? T(1 - (1 - p[j]) * v) : T((p[j] * v)) for i in 1:L, j in 1:L)
ϵ = T(1e-6) # add small eps for numerical stability
SMatrix{L,L}(i == j ? T(1 - (1 - p[j]) * v) - ϵ * (L - 1) : T(p[j] * v) + ϵ for i in 1:L, j in 1:L)
end
3 changes: 2 additions & 1 deletion src/theoretical/transiogram/gaussian.jl
Original file line number Diff line number Diff line change
@@ -28,5 +28,6 @@ function (t::GaussianTransiogram)(h)
h′, r′ = unitless(h, r)
v = 1 - exp(-3(h′ / r′)^2)
T = typeof(p[1] * v)
SMatrix{L,L}(i == j ? T(1 - (1 - p[j]) * v) : T((p[j] * v)) for i in 1:L, j in 1:L)
ϵ = T(1e-6) # add small eps for numerical stability
SMatrix{L,L}(i == j ? T(1 - (1 - p[j]) * v) - ϵ * (L - 1) : T(p[j] * v) + ϵ for i in 1:L, j in 1:L)
end
7 changes: 5 additions & 2 deletions src/theoretical/transiogram/linear.jl
Original file line number Diff line number Diff line change
@@ -26,8 +26,11 @@ function (t::LinearTransiogram)(h)
p = t.prop
L = length(p)
h′, r′ = unitless(h, r)
v = h′ / r′
T = typeof(p[1] * v)
ϵ = T(1e-6) # add small eps for numerical stability
SMatrix{L,L}(
i == j ? (h′ < r′) * (1 - (1 - p[j]) * (h′ / r′)) + (h′ r′) * p[j] :
(h′ < r′) * (p[j] * (h′ / r′)) + (h′ r′) * p[j] for i in 1:L, j in 1:L
i == j ? (h′ < r′) * T(1 - (1 - p[j]) * v) + (h′ r′) * T(p[j]) - ϵ * (L - 1) :
(h′ < r′) * T(p[j] * v) + (h′ r′) * T(p[j]) + ϵ for i in 1:L, j in 1:L
)
end
5 changes: 3 additions & 2 deletions src/theoretical/transiogram/spherical.jl
Original file line number Diff line number Diff line change
@@ -28,8 +28,9 @@ function (t::SphericalTransiogram)(h)
h′, r′ = unitless(h, r)
v = 3(h′ / r′) / 2 - (h′ / r′)^3 / 2
T = typeof(p[1] * v)
ϵ = T(1e-6) # add small eps for numerical stability
SMatrix{L,L}(
i == j ? (h′ < r′) * T(1 - (1 - p[j]) * v) + (h′ r′) * T(p[j]) : (h′ < r′) * T((p[j] * v)) + (h′ r′) * T(p[j])
for i in 1:L, j in 1:L
i == j ? (h′ < r′) * T(1 - (1 - p[j]) * v) + (h′ r′) * T(p[j]) - ϵ * (L - 1) :
(h′ < r′) * T(p[j] * v) + (h′ r′) * T(p[j]) + ϵ for i in 1:L, j in 1:L
)
end

0 comments on commit e9f6316

Please sign in to comment.