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

Allow Parameters as initial values for ODEModel #279

Merged
merged 5 commits into from
Nov 6, 2019

Conversation

pckroon
Copy link
Collaborator

@pckroon pckroon commented Oct 30, 2019

Fixes #160

Copy link
Owner

@tBuLi tBuLi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR! I have some remaining questions and suggestions, but I'm really happy this got done.

symfit/core/models.py Show resolved Hide resolved
symfit/core/models.py Show resolved Hide resolved
@@ -1100,7 +1103,7 @@ def eval_components(self, *args, **kwargs):
initial_dependent,
t_bigger,
args=tuple(
bound_arguments.arguments[param.name] for param in self.params),
bound_arguments.arguments[param.name] for param in self.model_params),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this switch needed? I'm guessing because the parameter corresponding to the initial value should not be provided here? I feel like this needs either a comment or a better variable name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was needed because a0 should not be passed to the python version of the component, to be integrated by scipy. Instead, a0 should be passed to scipy in initial_dependent.
I'll add a comment

tests/test_ode.py Outdated Show resolved Hide resolved
tests/test_ode.py Outdated Show resolved Hide resolved
symfit/core/models.py Outdated Show resolved Hide resolved
symfit/core/models.py Outdated Show resolved Hide resolved
symfit/core/models.py Outdated Show resolved Hide resolved
tests/test_ode.py Outdated Show resolved Hide resolved
@tBuLi tBuLi merged commit 866ead6 into tBuLi:master Nov 6, 2019
@JohnGoertz
Copy link

This is great! Is there a way to "scale" the initial values? For example, I have the following equation:

logistic_eqn = {
    sf.D(s1,c): r*s1*(1-s1/K1)
}

logistic_model = sf.ODEModel(logistic_eqn, initial = {c:0.0,s1:s1_0})

This works fine if I give a good guess for s1_0, but I know that the local minima in the model are evenly distributed in the "log" space of the initial values. If I brute force the residuals, I find the global minima occurs at r = 0.8 and log10(s1_0) = -7.5, but there are other local minima at log10(s1_0) ~ -5.5, -6.5, -8.5, -9.5 with r = 0.6, 0.7, 0.9, and 1.0, respectively.

I worry that a naive minimizer will explore the initial values in linear space, thus inefficiently. I've tried giving a first guess for s1_0 as -6.5 then specifying initial = {...,s1:10**s1_0,...}, but I get the error can't convert expression to float.

Any suggestions? I can provide more code if helpful, or post to StackOverflow if that's better.

@pckroon
Copy link
Collaborator Author

pckroon commented Nov 21, 2019

Very interesting point!
Since it's an ODEModel we can't get the analytical Jacobian, and finding it with numerical differentiation in every step is too expensive. Some minimizers however (BFGS springs to mind) approximate it as they go along. Those should be able to pick up on the underlying structure. (Very few minimizers are truly naive).
For normal models you can work around this by introducing an intermediate variable, but I don't expect that will work here due to how this is implemented. Instead of passing just a Parameter as initial value, you kind of want to be able to pass an expression that evaluates to a single value.
Could you make an issue along those lines?

@JohnGoertz
Copy link

Sure! See this issue: #284

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parameters should be allowed as initial values to ODEModel
3 participants