Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed May 24, 2024
1 parent 45ac971 commit cb1c74f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ function fit_impl(
uy = unit(first(y))
y = ustrip.(y)

# strip units of `range` and `maxrange`
range′ = isnothing(range) ? range : ustrip(ux, range)
maxrange′ = isnothing(maxrange) ? maxrange : ustrip(ux, maxrange)

# evaluate weights
f = algo.weightfun
w = isnothing(f) ? n / sum(n) : map(xᵢ -> ustrip(f(xᵢ)), x)
Expand All @@ -151,25 +155,25 @@ function fit_impl(
λ = sum(yᵢ -> yᵢ^2, y)

# maximum range, sill and nugget
xmax = maximum(x)
xmax = ustrip(maximum(x))
ymax = maximum(y)
rmax = isnothing(maxrange) ? xmax : maxrange
rmax = isnothing(maxrange) ? xmax : maxrange
smax = isnothing(maxsill) ? ymax : maxsill
nmax = isnothing(maxnugget) ? ymax : maxnugget

# initial guess
rₒ = isnothing(range) ? rmax / 3 : range
rₒ = isnothing(range) ? rmax / 3 : range
sₒ = isnothing(sill) ? 0.95 * smax : sill
nₒ = isnothing(nugget) ? 1e-6 : nugget
θₒ = [ustrip(ux, rₒ), sₒ, nₒ]
θₒ = [rₒ, sₒ, nₒ]

# box constraints
δ = 1e-8
rₗ, rᵤ = isnothing(range) ? (zero(rmax), rmax) : (range - δ * unit(range), range + δ * unit(range))
rₗ, rᵤ = isnothing(range) ? (zero(rmax), rmax) : (range - δ, range + δ)
sₗ, sᵤ = isnothing(sill) ? (zero(smax), smax) : (sill - δ, sill + δ)
nₗ, nᵤ = isnothing(nugget) ? (zero(nmax), nmax) : (nugget - δ, nugget + δ)
l = [ustrip(ux, rₗ), sₗ, nₗ]
u = [ustrip(ux, rᵤ), sᵤ, nᵤ]
l = [rₗ, sₗ, nₗ]
u = [rᵤ, sᵤ, nᵤ]

# solve optimization problem
sol = Optim.optimize-> J(θ) + λ * L(θ), l, u, θₒ)
Expand Down

0 comments on commit cb1c74f

Please sign in to comment.