tBuLi / symfit

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

Matrix compatibility and interdependent components #201

Closed tBuLi closed 5 years ago

tBuLi commented 5 years ago

This PR makes two big new changes: support for sympy.MatrixExpr as variables and as an indirect consequence it also adds component interdependence.

Consider e.g.

a, b = parameters('a, b')
x, y, z = variables('x, y, z')
model_dict = {
    y: a**3 * x + b**2,
    z: y**2 + a * b
}

This could be a great asset when y is expensive to compute and used multiple times. In such a model, y is no longer a dependent variable, but rather an interdependent one.

The big challenge here was how to calculate the Jacobian and Hessian for such a model, since also there we would like D(z, a) to be a function of D(y, a):

{D(y, a): 3 * a**2 * x,
 D(y, b): 2 * b,
 D(z, a): b + 2 * y * D(y, a),
 D(z, b): a + 2 * y * D(y, b),
}

This PR so far addresses:

pckroon commented 5 years ago

Referencing #39

tBuLi commented 5 years ago

Don't worry, it's still very much a work in progress ;). The non-trivial comments I left open were things I wanted to think about carefully before implementing them.

For future reference: this mentiones how the hessian relates to the covariance matrix: https://www8.cs.umu.se/kurser/5DA001/HT07/lectures/lsq-handouts.pdf

My suggestion is to remove finite differences entirely, and to always use the Hessian of the objective to calculate covariances. This removes the discussion about using finite differences for optimization and will finally clean-up the mess that is covariance matrix calculation at this point.

pckroon commented 5 years ago

My suggestion is to remove finite differences entirely, and to always use the Hessian of the objective to calculate covariances. This removes the discussion about using finite differences for optimization and will finally clean-up the mess that is covariance matrix calculation at this point.

Bad plan, we still need it to get a covariance matrix for e.g. ODEModels and NumericalModels. What we can do, however, is deprecate the finite difference Jacobian, and change it for a finite difference Hessian method. Or keep both next to each other.