-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor objective/gradient/hessian evaluation #214
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
Merged
Maximilian-Stefan-Ernst
merged 20 commits into
StructuralEquationModels:devel
from
alyst:refactor_evaluate
Oct 29, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8119ad2
remove get_observed()
alyst 1c179d4
fix ridge eval
22e76eb
MeanStructure, HessianEvaluation traits
af09c79
obj/grad/hess: refactor evaluation API
alyst 3c533b6
se_hessian(): rename hessian -> method
alyst 9e33add
se_hessian!(): optimize calc
5ad013e
H_scaling(): cleanup
alyst a32903e
SemOptOptim: remove redundant sem_fit()
alyst 7ab2dcb
SemOptNLopt: remove redundant sem_fit()
alyst 65d1112
SemOptOptim: use evaluate!() directly
alyst 9ac8f88
SemOptNLopt: use evaluate!() directly
alyst cc778e2
SemWLS: dim checks
0d33ba4
fixup formatting
alyst 1c376a5
WLS: use 5-arg mul!()
4a6f51b
ML: use 5-arg mul!()
d2b7e8c
FIML: use 5-arg mul!
d0ea406
fix the error message
alyst 56ec1c9
HessianEvaluation -> HessianEval
alyst c673cd1
MeanStructure -> MeanStruct
alyst 0cecaa8
SemImply: replace common type params with fields
alyst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,54 @@ | ||
| """ | ||
| se_hessian(semfit::SemFit; hessian = :finitediff) | ||
| se_hessian(fit::SemFit; method = :finitediff) | ||
| Return hessian based standard errors. | ||
| Return hessian-based standard errors. | ||
| # Arguments | ||
| - `hessian`: how to compute the hessian. Options are | ||
| - `method`: how to compute the hessian. Options are | ||
| - `:analytic`: (only if an analytic hessian for the model can be computed) | ||
| - `:finitediff`: for finite difference approximation | ||
| """ | ||
| function se_hessian(sem_fit::SemFit; hessian = :finitediff) | ||
| c = H_scaling(sem_fit.model) | ||
|
|
||
| if hessian == :analytic | ||
| par = solution(sem_fit) | ||
| H = zeros(eltype(par), length(par), length(par)) | ||
| hessian!(H, sem_fit.model, sem_fit.solution) | ||
| elseif hessian == :finitediff | ||
| H = FiniteDiff.finite_difference_hessian( | ||
| Base.Fix1(objective!, sem_fit.model), | ||
| sem_fit.solution, | ||
| ) | ||
| elseif hessian == :optimizer | ||
| throw( | ||
| ArgumentError( | ||
| "standard errors from the optimizer hessian are not implemented yet", | ||
| ), | ||
| ) | ||
| elseif hessian == :expected | ||
| throw( | ||
| ArgumentError( | ||
| "standard errors based on the expected hessian are not implemented yet", | ||
| ), | ||
| function se_hessian(fit::SemFit; method = :finitediff) | ||
| c = H_scaling(fit.model) | ||
| params = solution(fit) | ||
| H = similar(params, (length(params), length(params))) | ||
|
|
||
| if method == :analytic | ||
| evaluate!(nothing, nothing, H, fit.model, params) | ||
| elseif method == :finitediff | ||
| FiniteDiff.finite_difference_hessian!( | ||
| H, | ||
| p -> evaluate!(zero(eltype(H)), nothing, nothing, fit.model, p), | ||
| params, | ||
| ) | ||
| elseif method == :optimizer | ||
| error("Standard errors from the optimizer hessian are not implemented yet") | ||
| elseif method == :expected | ||
| error("Standard errors based on the expected hessian are not implemented yet") | ||
| else | ||
| throw(ArgumentError("I don't know how to compute `$hessian` standard-errors")) | ||
| throw(ArgumentError("Unsupported hessian calculation method :$method")) | ||
| end | ||
|
|
||
| invH = c * inv(H) | ||
| se = sqrt.(diag(invH)) | ||
|
|
||
| return se | ||
| H_chol = cholesky!(Symmetric(H)) | ||
| H_inv = LinearAlgebra.inv!(H_chol) | ||
| return [sqrt(c * H_inv[i]) for i in diagind(H_inv)] | ||
| end | ||
|
|
||
| # Addition functions ------------------------------------------------------------- | ||
| H_scaling(model::AbstractSemSingle) = | ||
| H_scaling(model, model.observed, model.imply, model.optimizer, model.loss.functions...) | ||
| function H_scaling(model::AbstractSemSingle) | ||
| if length(model.loss.functions) > 1 | ||
| @warn "Hessian scaling for multiple loss functions is not implemented yet" | ||
| end | ||
| return H_scaling(model.loss.functions[1], model) | ||
| end | ||
|
|
||
| H_scaling(model, obs, imp, optimizer, lossfun::SemML) = 2 / (nsamples(model) - 1) | ||
| H_scaling(lossfun::SemML, model::AbstractSemSingle) = 2 / (nsamples(model) - 1) | ||
|
|
||
| function H_scaling(model, obs, imp, optimizer, lossfun::SemWLS) | ||
| function H_scaling(lossfun::SemWLS, model::AbstractSemSingle) | ||
| @warn "Standard errors for WLS are only correct if a GLS weight matrix (the default) is used." | ||
| return 2 / (nsamples(model) - 1) | ||
| end | ||
|
|
||
| H_scaling(model, obs, imp, optimizer, lossfun::SemFIML) = 2 / nsamples(model) | ||
| H_scaling(lossfun::SemFIML, model::AbstractSemSingle) = 2 / nsamples(model) | ||
|
|
||
| H_scaling(model::SemEnsemble) = 2 / nsamples(model) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.