tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
233 stars 17 forks source link

Allow Parameters as initial values for ODEModel #279

Closed pckroon closed 4 years ago

pckroon commented 4 years ago

Fixes #160

JohnGoertz commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Sure! See this issue: https://github.com/tBuLi/symfit/issues/284