Thanks to visit codestin.com
Credit goes to github.com

Skip to content

data keyword for Fit #281

@tBuLi

Description

@tBuLi

Introduction

The data API to Fit has some drawbacks that need to be addressed. Firstly, the use of *args,**kwargs to send data is not a good design when large amount of variables need to be provided with data. Secondly, the possible names for Variable's are more constrained in symfit then in pure sympy. Thirdly, the result is cast into a dict with variables as keys and data as values.

Therefore, it seems like a better idea to use such a dict in the first place:

xdata = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
ydata = np.array([2.3, 3.3, 4.1, 5.5, 6.7])

a, b = parameters('a, b')
x, y = variables('x, y')
model = Model({y: a * x + b})

data = {x: xdata, y:ydata}
fit = Fit(model, data=data)
fit_result = fit.execute()

For the basics I think this is a very clean API, but there are some more advanced features which should be supported:

  • Provide not only standard deviations but also covariances. This is needed for e.g. ODR.
  • Indexed variable save, for the once to come global fitting API.

Covariances

For co-variances, a possible API would be

data = {x: xdata, y:ydata, Cov(x,y): xycovariance}

where Cov could be a new symbol subclass or a convenience method which returns a new variable encoding this information. Similar objects could then be made for Stdev and Var.

Another option would be to use and expand the .sigmas dict already present on model classes:

>>> model = Model({y: a * x + b})
>>> model.sigmas
{x: sigma_x, x: sigma_x, (x,y): sigma_xy}

such that we could write

data = {x: xdata, y:ydata, model.sigmas[(x,y)]: xycovariance, model.sigmas[y]: yerr}

Global fitting

A global fitting problem will be written using indexed variables in the future:

i = Idx('i', 10)  # Run from 0, ..., 9
a, b = parameters('a, b')
a = IndexedBase(a)
x, y = variables('x, y', cls=IndexedBase)
model = Model({y[i]: a[i] * x[i] + b})

data = {x: (xdata1, ..., xdata10), y[0]: ydata1, ..., , y[9]: ydata10}
fit = Fit(model, data=data)
fit_result = fit.execute()

where x and y use the two different allowable styles of providing data to indexed variables. This can be combined with either of the afore mentioned API covariance styles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions