Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AbstractCostFunction in ParamEstim #164

Merged
merged 1 commit into from
Dec 25, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/src/ParamEstim.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The `ParamEstim` submodule provides support for optimization-based parameter est

```@docs
ScaledSolution
AbstractCostFunction
RSSCostFunction
candidate
```
61 changes: 39 additions & 22 deletions src/ParamEstim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,42 @@
end

"""
RSSCostFunction{fit_D0}(func, prob::InverseProblem[; D0tol, oi_hint])
abstract type AbstractCostFunction{fit_D0} end

Abstract cost function for parameter estimation.

# Type parameters
- `fit_D0::Bool`: whether to fit an additional constant factor `D0` that affects the diffusivity. Values
of `D0` can be found with relative efficiency without additional solver calls; so if any such constant
factors affecting the diffusivity are unknown, it is recommended not to fit those factors directly but set
`fit_D0` to `true` instead. Values of `D0` are found internally by local optimization. If `true`, the
`candidate` function will return a `ScaledSolution` that includes the found value of `D0`.
"""
abstract type AbstractCostFunction{fit_D0} end

function (cf::AbstractCostFunction)(params::AbstractVector,
::NullParameters = NullParameters())
cf(candidate(cf, params))
end

_solve(cf::AbstractCostFunction, params::AbstractVector) = _solve(cf, cf._func(params))
_solve(::AbstractCostFunction, prob::AbstractProblem) = solve(prob, verbose = false)
_solve(::AbstractCostFunction, sol::Solution) = sol

Check warning on line 79 in src/ParamEstim.jl

View check run for this annotation

Codecov / codecov/patch

src/ParamEstim.jl#L79

Added line #L79 was not covered by tests

"""
candidate(cf::AbstractCostFunction, ::AbstractVector)
candidate(cf::AbstractCostFunction, ::Fronts.AbstractProblem)
candidate(cf::AbstractCostFunction, ::Fronts.Solution)

Return the candidate solution for a given cost function and parameter values, problem, or solution.
"""
candidate(cf::AbstractCostFunction, params::AbstractVector) = candidate(cf,
_solve(cf, params))
candidate(cf::AbstractCostFunction, prob::AbstractProblem) = candidate(cf, _solve(cf, prob))

Check warning on line 90 in src/ParamEstim.jl

View check run for this annotation

Codecov / codecov/patch

src/ParamEstim.jl#L90

Added line #L90 was not covered by tests
candidate(::AbstractCostFunction{false}, sol::Solution) = sol

"""
RSSCostFunction{fit_D0}(func, prob::InverseProblem[; D0tol, oi_hint]) <: AbstractCostFunction

Residual sum of squares cost function for parameter estimation.

Expand Down Expand Up @@ -97,7 +132,8 @@

If you need to know more than just the cost, call the `candidate` function instead.
"""
struct RSSCostFunction{fit_D0, _Tfunc, _Tprob, _TD0tol, _Toi_hint, _Tsorptivity}
struct RSSCostFunction{fit_D0, _Tfunc, _Tprob, _TD0tol, _Toi_hint, _Tsorptivity} <:
AbstractCostFunction{fit_D0}
_func::_Tfunc
_prob::_Tprob
_D0tol::_TD0tol
Expand Down Expand Up @@ -135,25 +171,6 @@
end
end

function (cf::RSSCostFunction)(params::AbstractVector, ::NullParameters = NullParameters())
cf(candidate(cf, params))
end

_solve(cf::RSSCostFunction, params::AbstractVector) = _solve(cf, cf._func(params))
_solve(::RSSCostFunction, prob::AbstractProblem) = solve(prob, verbose = false)
_solve(::RSSCostFunction, sol::Solution) = sol

"""
candidate(cf::RSSCostFunction, ::AbstractVector)
candidate(cf::RSSCostFunction, ::Fronts.AbstractProblem)
candidate(cf::RSSCostFunction, ::Fronts.Solution)

Return the candidate solution for a given cost function and parameter values, problem, or solution.
"""
candidate(cf::RSSCostFunction, params::AbstractVector) = candidate(cf, _solve(cf, params))
candidate(cf::RSSCostFunction, prob::AbstractProblem) = candidate(cf, _solve(cf, prob))
candidate(::RSSCostFunction{false}, sol::Solution) = sol

function candidate(cf::RSSCostFunction{true}, sol::Solution)
if !successful_retcode(sol)
return ScaledSolution(sol, NaN)
Expand Down Expand Up @@ -184,6 +201,6 @@
return ScaledSolution(sol, scaling.param[1])
end

export RSSCostFunction, candidate
export AbstractCostFunction, RSSCostFunction, candidate

end
Loading