mimiframework / Mimi.jl

Integrated Assessment Modeling Framework
https://www.mimiframework.org
Other
66 stars 34 forks source link

Adds MarginalInstance struct #957

Open jrising opened 1 year ago

jrising commented 1 year ago

Currently, one can only run build! on a MarginalModel, not build. The second is useful for Monte Carlo runs because it gives a ModelInstance where parameters can be updated without requiring new building. This extends that feature to MarginalModels.

A MarginalInstance is a pair of ModelInstances, with the delta so that marginal values can be retrieved. It also has a (currently minimal) set of associated functions so that Monte Carlo code that currently takes a Model can just be generalized to Union{Model, MarginalModel} and the associated code still works.

In the diffs below, sorry that my editor removed spaces at the end of lines. If it's a problem, I can fix that.

An associated PR to MimiFAIRv2 that uses this change will come next.

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 69.73% and project coverage change: -0.10 :warning:

Comparison is base (61abb6b) 84.52% compared to head (74874bd) 84.43%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #957 +/- ## ========================================== - Coverage 84.52% 84.43% -0.10% ========================================== Files 40 40 Lines 3878 4074 +196 ========================================== + Hits 3278 3440 +162 - Misses 600 634 +34 ``` | Flag | Coverage Δ | | |---|---|---| | unittests | `84.43% <69.73%> (-0.10%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework#carryforward-flags-in-the-pull-request-comment) to find out more. | [Impacted Files](https://app.codecov.io/gh/mimiframework/Mimi.jl/pull/957?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework) | Coverage Δ | | |---|---|---| | [src/core/model.jl](https://app.codecov.io/gh/mimiframework/Mimi.jl/pull/957?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework#diff-c3JjL2NvcmUvbW9kZWwuamw=) | `83.33% <0.00%> (-3.63%)` | :arrow_down: | | [src/core/types/model.jl](https://app.codecov.io/gh/mimiframework/Mimi.jl/pull/957?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework#diff-c3JjL2NvcmUvdHlwZXMvbW9kZWwuamw=) | `65.21% <0.00%> (-22.29%)` | :arrow_down: | | [src/core/build.jl](https://app.codecov.io/gh/mimiframework/Mimi.jl/pull/957?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework#diff-c3JjL2NvcmUvYnVpbGQuamw=) | `97.47% <44.44%> (-2.53%)` | :arrow_down: | | [src/core/connections.jl](https://app.codecov.io/gh/mimiframework/Mimi.jl/pull/957?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework#diff-c3JjL2NvcmUvY29ubmVjdGlvbnMuamw=) | `89.20% <83.05%> (-0.60%)` | :arrow_down: | ... and [26 files with indirect coverage changes](https://app.codecov.io/gh/mimiframework/Mimi.jl/pull/957/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mimiframework)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

jrising commented 1 year ago

Here's the MimiFAIRv2 PR: https://github.com/FrankErrickson/MimiFAIRv2.jl/pull/16

lrennels commented 1 year ago

@jrising this seems reasonable to me, am I right that in the diff most of the changes are the line spacing removal and the main functional change is just the addition of the method?

@davidanthoff does this look alright to you?

I'll note that we can also handle this by using the built-in Monte Carlo functionality of Mimi that takes care of the updating of a model instance without building, which is a bit safer for more novice users, but @jrising I know the FAIRv2 one we wrote did not use that functionality and as of now I haven't added one (I have for a paper I'm working on though, so when I get the chance I can port over that option too) so this is likely useful for your workflows. Happy to approve.

jrising commented 1 year ago

@lrennels I only checked in files with changes, so there are minor additions to each of those files. Here's the list of actual additions:

I tried to put these where the equivalent code is for MarginalModels or ModelInstances. There are probably other functions one might want for MarginalInstance to support, but these were the ones I found necessary.

lrennels commented 1 year ago

Sorry for the delay @jrising I'll look at this this week, I think all should be fine I just want to run our dependency tests as well. I will note that I think I won't advertise this use for now, only because ew don't have as many safetys put onto theInstance structures so they would be dangerous to use if you aren't more of an expert (as of course you are :) ).

For example I was reminded the hard way recently that if you run update_param! on a ModelInstance and the size of the array you update is not the same as the one you are updating ... it uses copyto! so you won't get an error that warns you that you are only replacing part of the array. Makes for some super confusing behavior :)

jrising commented 1 year ago

No worries. Thanks for thinking about it.