Module Documentation

This page contains documentation to everything symfit has to offer.

Fit

class symfit.core.fit.BaseFit(*args, **kwargs)[source]

Bases: symfit.core.fit.TakesData

Abstract base class for all fitting objects.

error_func(*args, **kwargs)[source]

Every fit object has to define an error_func method, giving the function to be minimized.

eval_jacobian(*args, **kwargs)[source]

Every fit object has to define an eval_jacobian method, giving the jacobian of the function to be minimized.

execute(*args, **kwargs)[source]

Every fit object has to define an execute method. Any * and ** arguments will be passed to the fitting module that is being wrapped, e.g. leastsq.

Args kwargs:
Returns:Instance of FitResults
class symfit.core.fit.BaseModel(model)[source]

Bases: _abcoll.Mapping

ABC for Model‘s. Makes sure models are iterable. Models can be initiated from Mappings or Iterables of Expressions, or from an expression directly. Expressions are not enforced for ducktyping purposes.

__eq__(other)[source]

Model‘s are considered equal when they have the same dependent variables, and the same expressions for those dependent variables. The same is defined here as passing sympy == for the vars themselves, and as expr1 - expr2 == 0 for the expressions. For more info check the sympy docs.

Parameters:other – Instance of Model.
Returns:bool
__getitem__(dependent_var)[source]

Returns the expression belonging to a given dependent variable.

Parameters:dependent_var (Variable) – Instance of Variable
Returns:The expression belonging to dependent_var
__init__(model)[source]

Initiate a Model from a dict:

a = Model({y: x**2})

Preferred way of initiating Model, since now you know what the dependent variable is called.

Parameters:model – dict of Expr, where dependent variables are the keys.
__iter__()[source]
Returns:iterable over self.model_dict
__len__()[source]
Returns:the number of dependent variables for this model.
bounds
Returns:List of tuples of all bounds on parameters.
shared_parameters
Returns:bool, indicating if parameters are shared between the vector components of this model.
vars
Returns:Returns a list of dependent, independent and sigma variables, in that order.
class symfit.core.fit.CallableModel(model)[source]

Bases: symfit.core.fit.BaseModel

Defines a callable model. The usual rules apply to the ordering of the arguments:

  • first independent variables, then dependent variables, then parameters.
  • within each of these groups they are ordered alphabetically.
__call__(*args, **kwargs)[source]

Evaluate the model for a certain value of the independent vars and parameters. Signature for this function contains independent vars and parameters, NOT dependent and sigma vars.

Can be called with both ordered and named parameters. Order is independent vars first, then parameters. Alphabetical order within each group.

Parameters:
  • args
  • kwargs
Returns:

A namedtuple of all the dependent vars evaluated at the desired point. Will always return a tuple, even for scalar valued functions. This is done for consistency.

eval_components(*args, **kwargs)[source]

Evaluate the components of the model with the given data. Used for numerical evaluation.

class symfit.core.fit.ConstrainedNumericalLeastSquares(*args, **kwargs)[source]

Bases: symfit.core.fit.Minimize, symfit.core.fit.HasCovarianceMatrix

This object performs \(\chi^2\) minimization, subject to constraints and bounds. The flexibility of this object also makes it ideal for global fitting problems; problems where multiple datasets have to be fitted using shared parameters. For an example of this, see Global Fitting.

The example in Constrained Least Squares Fit is solved using this object:

a, b, c = parameters('a, b, c')
a_i, b_i, c_i = variables('a_i, b_i, c_i')

model = {a_i: a, b_i: b, c_i: c}

data = np.array([
    [10.1, 9., 10.5, 11.2, 9.5, 9.6, 10.],
    [102.1, 101., 100.4, 100.8, 99.2, 100., 100.8],
    [71.6, 73.2, 69.5, 70.2, 70.8, 70.6, 70.1],
])

fit = ConstrainedNumericalLeastSquares(
    model=model,
    a_i=data[0],
    b_i=data[1],
    c_i=data[2],
    constraints=[Equality(a + b + c, 180)]
)
fit_result = fit.execute()

Unlike NumericalLeastSquares, it also supports vector components of unequal length and is therefore preferred for Global Fitting problems.

In order to perform minimization, this object is a subclass of Minimize, and the output might therefore deviate slightly from the MINPACK result given by the more traditional NumericalLeastSquares object.

error_func(p, independent_data, dependent_data, sigma_data, flatten_components=True)[source]

Returns \(\chi^2\), summing over all the vector components and data indices.

This function now supports setting variables to None. Needs mathematical rigor!

Parameters:
  • p – array of floats for the parameters.
  • data – data to be provided to Variable‘s.
  • flatten_components – If True, \(\chi^2\) is returned. If False, the \(\chi^2\) per vector component is returned.
eval_jacobian(p, independent_data, dependent_data, sigma_data)[source]

Evaluates the jacobian of \(\chi^2\), summing over all the vector components and data indices.

Returns:array of len(self.model.params) containing the components of the Jacobian.
execute(*args, **kwargs)[source]

This wraps the execute of :class:’Minimize’ with the calculation of the covariance matrix. Read Minimize.execute for a more general description.

class symfit.core.fit.Constraint(constraint, model)[source]

Bases: symfit.core.fit.Model

Constraints are a special type of model in that they have a type: >=, == etc. They are made to have lhs - rhs == 0 of the original expression.

For example, Eq(y + x, 4) -> Eq(y + x - 4, 0)

Since a constraint belongs to a certain model, it has to be initiated with knowledge of it’s parent model. This is important because all numerical_ methods are done w.r.t. the parameters and variables of the parent model, not the constraint! This is because the constraint might not have all the parameter or variables that the model has, but in order to compute for example the Jacobian we still want to derive w.r.t. all the parameters, not just those present in the constraint.

__init__(constraint, model)[source]
Parameters:
  • constraint – constraint that model should be subjected to.
  • model – A constraint is always tied to a model.
jacobian
Returns:Jacobian ‘Matrix’ filled with the symbolic expressions for all the partial derivatives. Partial derivatives are of the components of the function with respect to the Parameter’s, not the independent Variable’s.
numerical_components
Returns:lambda functions of each of the components in model_dict, to be used in numerical calculation.
numerical_jacobian
Returns:lambda functions of the jacobian matrix of the function, which can be used in numerical optimization.
class symfit.core.fit.Fit(*args, **kwargs)[source]

Bases: object

Your one stop fitting solution! Based on the nature of the input, this object will attempt to select the right fitting type for your problem.

If you need very specific control over how the problem is solved, please use one of the available fitting objects directly.

Currently Fit will select between NumericalLeastSquares and ConstrainedNumericalLeastSquares.

execute(*options, **kwoptions)[source]

Execute Fit, giving any options and kwoptions to the fitting object.

model

Property which returns the Model object, taken from self.fit.model

class symfit.core.fit.FitResults(params, popt, pcov, infodic, mesg, ier, ydata=None, sigma=None)[source]

Bases: object

Class to display the results of a fit in a nice and unambiguous way. All things related to the fit are available on this class, e.g. - parameters + stdev - R squared (Regression coefficient.) - fitting status message

This object is made to behave entirely read-only. This is a bit unnatural to enforce in Python but I feel it is necessary to guarantee the integrity of the results.

__init__(params, popt, pcov, infodic, mesg, ier, ydata=None, sigma=None)[source]

Excuse the ugly names of most of these variables, they are inherited from scipy. Will be changed.

Parameters:
  • params – list of Parameter‘s.
  • popt – best fit parameters, same ordering as in params.
  • pcov – covariance matrix.
  • infodic – dict with fitting info.
  • mesg – Status message.
  • ier – Number of iterations.
  • ydata
__str__()[source]

Pretty print the results as a table.

covariance(param_1, param_2)[source]

Return the covariance between param_1 and param_2.

Parameters:
  • param_1Parameter Instance.
  • param_2Parameter Instance.
Returns:

Covariance of the two params.

infodict

Read-only Property.

iterations

Read-only Property.

params

Read-only Property.

r_squared

r_squared Property.

Returns:Regression coefficient.
status_message

Read-only Property.

stdev(param)[source]

Return the standard deviation in a given parameter as found by the fit.

Parameters:paramParameter Instance.
Returns:Standard deviation of param.
value(param)[source]

Return the value in a given parameter as found by the fit.

Parameters:paramParameter Instance.
Returns:Value of param.
variance(param)[source]

Return the variance in a given parameter as found by the fit.

Parameters:paramParameter Instance.
Returns:Variance of param.
class symfit.core.fit.HasCovarianceMatrix[source]

Bases: object

Mixin class for calculating the covariance matrix for any model that has a well-defined Jacobian \(J\). The covariance is then approximated as \(J^T W J\), where W contains the weights of each data point.

Supports vector valued models, but is unable to estimate covariances for those, just variances. Therefore, take the result with a grain of salt for vector models.

covariance_matrix(best_fit_params)[source]

Given best fit parameters, this function finds the covariance matrix. This matrix gives the (co)variance in the parameters.

Parameters:best_fit_paramsdict of best fit parameters as given by .best_fit_params()
Returns:covariance matrix.
class symfit.core.fit.Likelihood(*args, **kwargs)[source]

Bases: symfit.core.fit.Maximize

Fit using a Maximum-Likelihood approach. This object maximizes the log-likelihood function.

error_func(p, independent_data, dependent_data, sigma_data)[source]

Error function to be maximised(!) in the case of log-likelihood fitting.

Parameters:
  • p – guess params
  • data – xdata
Returns:

scalar value of log-likelihood

eval_jacobian(p, independent_data, dependent_data, sigma_data)[source]

Jacobian for log-likelihood is defined as \(\nabla_{\vec{p}}( \log( L(\vec{p} | \vec{x})))\).

Parameters:
  • p – guess params
  • data – data for the variables.
Returns:

array of length number of Parameter‘s in the model, with all partial derivatives evaluated at p, data.

class symfit.core.fit.LinearLeastSquares(*args, **kwargs)[source]

Bases: symfit.core.fit.BaseFit

Experimental. Solves the linear least squares problem analytically. Involves no iterations or approximations, and therefore gives the best possible fit to the data.

The Model provided has to be linear.

Currently, since this object still has to mature, it suffers from the following limitations:

  • It does not check if the model can be linearized by a simple substitution. For example, exp(a * x) -> b * exp(x). You will have to do this manually.
  • Does not use bounds or guesses on the Parameter‘s. Then again, it doesn’t have to, since you have an exact solution. No guesses required.
  • It only works with scalar functions. This is strictly enforced.
__init__(*args, **kwargs)[source]
Raises:ModelError in case of a non-linear model or when a vector valued function is provided.
best_fit_params()[source]

Fits to the data and returns the best fit parameters.

Returns:dict containing parameters and their best-fit values.
covariance_matrix(best_fit_params)[source]

Given best fit parameters, this function finds the covariance matrix. This matrix gives the (co)variance in the parameters.

Parameters:best_fit_paramsdict of best fit parameters as given by .best_fit_params()
Returns:covariance matrix.
execute()[source]

Execute an analytical (Linear) Least Squares Fit. This object works by symbolically solving when \(\nabla \chi^2 = 0\).

To perform this task the expression of \(\nabla \chi^2\) is determined, ignoring that \(\chi^2\) involves summing over all terms. Then the sum is performed by substituting the variables by their respective data and summing all terms, while leaving the parameters symbolic.

The resulting system of equations is then easily solved with sympy.solve.

Returns:FitResult
static is_linear(model)[source]

Test whether model is of linear form in it’s parameters.

Currently this function does not recognize if a model can be considered linear by a simple substitution, such as exp(k x) = k’ exp(x).

Parameters:modelModel instance
Returns:True or False
class symfit.core.fit.Maximize(*args, **kwargs)[source]

Bases: symfit.core.fit.Minimize

Maximize a model subject to constraints. Simply flips the sign on error_func and eval_jacobian in order to maximize.

class symfit.core.fit.Minimize(*args, **kwargs)[source]

Bases: symfit.core.fit.BaseFit

Minimize a model subject to constraints. A wrapper for scipy.optimize.minimize.

Minimize itself doesn’t work when data is provided to its Variables, use one of its subclasses for that.

__init__(*args, **kwargs)[source]

Because in a lot of use cases for Minimize no data is supplied to variables, all the empty variables are replaced by an empty np array.

Constraints:constraints the minimization is subject to.
error_func(p, independent_data, dependent_data, sigma_data)[source]

The function to be optimized. Scalar valued models are assumed. For Minimize the thing to minimize is simply self.model directly.

Parameters:
  • p – array of floats for the parameters.
  • data – data to be provided to Variable‘s.
eval_jacobian(p, independent_data, dependent_data, sigma_data)[source]

Takes partial derivatives of model w.r.t. each Parameter.

Parameters:
  • p – array of floats for the parameters.
  • data – data to be provided to Variable‘s.
Returns:

array of length number of Parameter‘s in the model, with all partial derivatives evaluated at p, data.

scipy_constraints

Read-only Property of all constraints in a scipy compatible format.

Returns:dict of scipy compatible statements.
class symfit.core.fit.Model(model)[source]

Bases: symfit.core.fit.CallableModel

Model represents a symbolic function and all it’s derived properties such as sum of squares, jacobian etc. Models can be initiated from several objects:

a = Model({y: x**2})
b = Model(y=x**2)

Models are callable. The usual rules apply to the ordering of the arguments:

  • first independent variables, then dependent variables, then parameters.
  • within each of these groups they are ordered alphabetically.

Models are also iterable, behaving as their internal model_dict. In the example above, a[y] returns x**2, len(a) == 1, y in a == True, etc.

__str__()[source]

Printable representation of this model.

Returns:str
chi
Returns:Symbolic Square root of \(\chi^2\). Required for MINPACK optimization only. Denoted as \(\sqrt(\chi^2)\)
chi_jacobian

Return a symbolic jacobian of the \(\sqrt(\chi^2)\) function. Vector of derivatives w.r.t. each parameter. Not a Matrix but a vector! This is because that’s what leastsq needs.

chi_squared
Returns:Symbolic \(\chi^2\)
chi_squared_jacobian

Return a symbolic jacobian of the \(\chi^2\) function. Vector of derivatives w.r.t. each parameter. Not a Matrix but a vector!

eval_components(*args, **kwargs)[source]
Returns:lambda functions of each of the components in model_dict, to be used in numerical calculation.
eval_jacobian(*args, **kwargs)[source]
Returns:lambda functions of the jacobian matrix of the function, which can be used in numerical optimization.
jacobian
Returns:Jacobian ‘Matrix’ filled with the symbolic expressions for all the partial derivatives. Partial derivatives are of the components of the function with respect to the Parameter’s, not the independent Variable’s.
numerical_chi
Returns:lambda function of the .chi method, to be used in MINPACK optimisation.
numerical_chi_jacobian
Returns:lambda functions of the jacobian of the .chi method, which can be used in numerical optimization.
numerical_chi_squared
Returns:lambda function of the .chi_squared method, to be used in numerical optimisation.
numerical_chi_squared_jacobian
Returns:lambda functions of the jacobian of the .chi_squared method.
numerical_components
Returns:lambda functions of each of the components in model_dict, to be used in numerical calculation.
numerical_jacobian
Returns:lambda functions of the jacobian matrix of the function, which can be used in numerical optimization.
exception symfit.core.fit.ModelError[source]

Bases: exceptions.Exception

Raised when a problem occurs with a model.

class symfit.core.fit.NonLinearLeastSquares(*args, **kwargs)[source]

Bases: symfit.core.fit.BaseFit

Experimental. Implements non-linear least squares [wiki_nllsq]. Works by a two step process: First the model is linearised by doing a first order taylor expansion around the guesses for the parameters. Then a LinearLeastSquares fit is performed. This is iterated until a fit of sufficient quality is obtained.

Sensitive to good initial guesses. Providing good initial guesses is a must.

[wiki_nllsq]https://en.wikipedia.org/wiki/Non-linear_least_squares
execute(relative_error=1e-08, max_iter=500)[source]

Perform a non-linear least squares fit.

Parameters:
  • relative_error – Relative error between the sum of squares of subsequent itterations. Once smaller than the value specified, the fit is considered complete.
  • max_iter – Maximum number of iterations before giving up.
Returns:

Instance of FitResults.

class symfit.core.fit.NumericalLeastSquares(*args, **kwargs)[source]

Bases: symfit.core.fit.BaseFit

Solves least squares numerically using leastsqbounds. Gives results consistent with MINPACK except when borders are provided.

error_func(p, independent_data, dependent_data, sigma_data, flatten=True)[source]

Returns the value of the square root of \(\chi^2\), summing over the components.

This function now supports setting variables to None. Needs mathematical rigor!

Parameters:
  • p – array of parameter values.
  • independent_data – Data to provide to the independent variables.
  • dependent_data
  • sigma_data
  • flatten – If True, summing is performed over the data indices (default).
Returns:

\(\sqrt(\chi^2)\)

execute(*options, **kwoptions)[source]
Parameters:
  • options – Any postional arguments to be passed to leastsqbound
  • kwoptions – Any named arguments to be passed to leastsqbound
class symfit.core.fit.ODEModel(model_dict, initial, *lsoda_args, **lsoda_kwargs)[source]

Bases: symfit.core.fit.CallableModel

Model build from a system of ODEs. When the model is called, the ODE is integrated using the LSODA package.

Currently the initial conditions are assumed to specify the first point to begin the integration from. This is enforced. In future versions one should be allowed to specify the initial value as a parameter.

__call__(*args, **kwargs)[source]

Evaluate the model for a certain value of the independent vars and parameters. Signature for this function contains independent vars and parameters, NOT dependent and sigma vars.

Can be called with both ordered and named parameters. Order is independent vars first, then parameters. Alphabetical order within each group.

Parameters:
  • args – Ordered arguments for the parameters and independent variables
  • kwargs – Keyword arguments for the parameters and independent variables
Returns:

A namedtuple of all the dependent vars evaluated at the desired point. Will always return a tuple, even for scalar valued functions. This is done for consistency.

__getitem__(dependent_var)[source]

Gives the function defined for the derivative of dependent_var. e.g. \(y' = f(y, t)\), model[y] -> f(y, t)

Parameters:dependent_var
Returns:
__init__(model_dict, initial, *lsoda_args, **lsoda_kwargs)[source]
Parameters:
  • model_dict – Dictionary specifying ODEs. e.g. model_dict = {D(y, x): a * x**2}
  • initialdict of initial conditions for the ODE. Must be provided! e.g. initial = {y: 1.0, x: 0.0}
  • lsoda_args – args to pass to the lsoda solver. See scipy’s odeint for more info.
  • lsoda_kwargs – kwargs to pass to the lsoda solver.
__iter__()[source]
Returns:iterable over self.model_dict
eval_components(*args, **kwargs)[source]

Numerically integrate the system of ODEs.

Parameters:
  • args – Ordered arguments for the parameters and independent variables
  • kwargs – Keyword arguments for the parameters and independent variables
Returns:

class symfit.core.fit.ParameterDict(params, popt, pcov, *args, **kwargs)[source]

Bases: object

Container for all the parameters and their (co)variances. Behaves mostly like an OrderedDict: can be **-ed, allowing the sexy syntax where a model is called with values for the Variables and **params. However, under iteration it behaves like a list! In other words, it preserves order in the params.

__getattr__(name)[source]

A user can access the value of a parameter directly through this object.

Parameters:name – Name of a Parameter. Naming convention: let a = Parameter(). Then: .a gives the value of the parameter. .a_stdev gives the standard deviation.
__getitem__(param_name)[source]

This method allows this object to be addressed as a dict. This allows for the ** unpacking. Therefore return the value of the best fit parameter, as this is what the user expects.

Parameters:param_name – Name of the Parameter whose value your interested in.
Returns:the value of the best fit parameter with name ‘key’.
__iter__()[source]

Iteration over the Parameter instances. :return: iterator

__len__()[source]

Length gives the number of Parameter instances.

Returns:len(self.__params)
covariance_matrix

Returns the covariance matrix.

get_stdev(*args, **kwargs)[source]

Deprecated. :param param: Parameter instance. :return: returns the standard deviation of param :raises: DeprecationWarning

get_value(*args, **kwargs)[source]

Deprecated. :param param: Parameter instance. :return: returns the numerical value of param :raises: DeprecationWarning

keys()[source]
Returns:All Parameter names.
stdev(param)[source]
Parameters:paramParameter instance.
Returns:returns the standard deviation of param
value(param)[source]
Parameters:paramParameter instance.
Returns:returns the numerical value of param
class symfit.core.fit.TakesData(*args, **kwargs)[source]

Bases: object

An base class for everything that takes data. Most importantly, it takes care of linking the provided data to variables. The allowed variables are extracted from the model.

__init__(*args, **kwargs)[source]
Parameters:
  • model – (dict of) sympy expression or Model object.
  • absolute_sigma (bool) – True by default. If the sigma is only used for relative weights in your problem, you could consider setting it to False, but if your sigma are measurement errors, keep it at True. Note that curve_fit has this set to False by default, which is wrong in experimental science.
  • ordered_data – data for dependent, independent and sigma variables. Assigned in the following order: independent vars are assigned first, then dependent vars, then sigma’s in dependent vars. Within each group they are assigned in alphabetical order.
  • named_data – assign dependent, independent and sigma variables data by name.

Standard deviation can be provided to any variable. They have to be prefixed with sigma_. For example, let x be a Variable. Then sigma_x will give the stdev in x.

dependent_data

Read-only Property

Returns:Data belonging to each dependent variable as a dict with variable names as key, data as value.
Return type:collections.OrderedDict
independent_data

Read-only Property

Returns:Data belonging to each independent variable as a dict with variable names as key, data as value.
Return type:collections.OrderedDict
initial_guesses
Returns:Initial guesses for every parameter.
sigma_data

Read-only Property

Returns:Data belonging to each sigma variable as a dict with variable names as key, data as value.
Return type:collections.OrderedDict
class symfit.core.fit.TaylorModel(model)[source]

Bases: symfit.core.fit.Model

A first-order Taylor expansion of a model around given parameter values (\(p_0\)). Is used by NonLinearLeastSquares. Currently only a first order expansion is implemented.

__init__(model)[source]

Make a first order Taylor expansion of model.

Parameters:model – Instance of Model
__str__()[source]

When printing a TaylorModel, the point around which the expansion took place is included.

For example, a Taylor expansion of {y: sin(w * x)} at w = 0 would be printed as:

@{w: 0.0} -> y(x; w) = w*x
p0

Property of the \(p_0\) around which to expand. Should be set by the names of the parameters themselves.

Example:

a = Parameter()
x, y = variables('x, y')
model = TaylorModel({y: sin(a * x)})

model.p0 = {a: 0.0}
params

params returns only the free parameters. Strictly speaking, the expression for a TaylorModel contains both the parameters \(\vec{p}\) and \(\vec{p_0}\) around which to expand, but params should only give \(\vec{p}\). To get a mapping to the \(\vec{p_0}\), use .params_0.

symfit.core.fit.r_squared(model, fit_result, data)[source]

Calculates the coefficient of determination, R^2, for the fit.

(Is not defined properly for vector valued functions.)

Parameters:
  • model – Model instance
  • fit_result – FitResults instance
  • data – data with which the fit was performed.

Argument

class symfit.core.argument.Argument(name=None, **assumptions)[source]

Bases: sympy.core.symbol.Symbol

Base class for symfit symbols. This helps make symfit symbols distinguishable from sympy symbols.

The Argument class also makes DRY possible in defining Argument‘s: it uses inspect to read the lhs of the assignment and uses that as the name for the Argument is none is explicitly set.

For example:

x = Variable()
print(x.name)
>> 'x'
class symfit.core.argument.Parameter(value=1.0, min=None, max=None, fixed=False, name=None, **assumptions)[source]

Bases: symfit.core.argument.Argument

Parameter objects are used to facilitate bounds on function parameters.

__call__(*values, **named_values)

Call an expression to evaluate it at the given point.

Future improvements: I would like if func and signature could be buffered after the first call so they don’t have to be recalculated for every call. However, nothing can be stored on self as sympy uses __slots__ for efficiency. This means there is no instance dict to put stuff in! And I’m pretty sure it’s ill advised to hack into the __slots__ of Expr.

However, for the moment I don’t really notice a performance penalty in running tests.

p.s. In the current setup signature is not even needed since no introspection is possible on the Expr before calling it anyway, which makes calculating the signature absolutely useless. However, I hope that someday some monkey patching expert in shining armour comes by and finds a way to store it in __signature__ upon __init__ of any symfit expr such that calling inspect_sig.signature on a symbolic expression will tell you which arguments to provide.

Parameters:
  • self – Any subclass of sympy.Expr
  • values – Values for the Parameters and Variables of the Expr.
  • named_values – Values for the vars and params by name. named_values is allowed to contain too many values, as this sometimes happens when using **fit_result.params on a submodel. The irrelevant params are simply ignored.
Returns:

The function evaluated at values. The type depends entirely on the input. Typically an array or a float but nothing is enforced.

__init__(value=1.0, min=None, max=None, fixed=False, name=None, **assumptions)[source]
Parameters:
  • value – Initial guess value.
  • min – Lower bound on the parameter value.
  • max – Upper bound on the parameter value.
  • fixed (bool) – Fix the parameter to value during fitting.
  • name – Name of the Parameter.
  • assumptions – assumptions to pass to sympy.
class symfit.core.argument.Variable(name=None, **assumptions)[source]

Bases: symfit.core.argument.Argument

Variable type.

Operators

Monkey Patching module.

This module makes sympy Expressions callable, which makes the whole project feel more consistent.

symfit.core.operators.call(self, *values, **named_values)[source]

Call an expression to evaluate it at the given point.

Future improvements: I would like if func and signature could be buffered after the first call so they don’t have to be recalculated for every call. However, nothing can be stored on self as sympy uses __slots__ for efficiency. This means there is no instance dict to put stuff in! And I’m pretty sure it’s ill advised to hack into the __slots__ of Expr.

However, for the moment I don’t really notice a performance penalty in running tests.

p.s. In the current setup signature is not even needed since no introspection is possible on the Expr before calling it anyway, which makes calculating the signature absolutely useless. However, I hope that someday some monkey patching expert in shining armour comes by and finds a way to store it in __signature__ upon __init__ of any symfit expr such that calling inspect_sig.signature on a symbolic expression will tell you which arguments to provide.

Parameters:
  • self – Any subclass of sympy.Expr
  • values – Values for the Parameters and Variables of the Expr.
  • named_values – Values for the vars and params by name. named_values is allowed to contain too many values, as this sometimes happens when using **fit_result.params on a submodel. The irrelevant params are simply ignored.
Returns:

The function evaluated at values. The type depends entirely on the input. Typically an array or a float but nothing is enforced.

Support

This module contains support functions and convenience methods used throughout symfit. Some are used predominantly internally, others are designed for users.

class symfit.core.support.D[source]

Bases: sympy.core.function.Derivative

Convenience wrapper for sympy.Derivative. Used most notably in defining ODEModel‘s.

class symfit.core.support.RequiredKeyword[source]

Bases: object

Flag variable to indicate that this is a required keyword.

exception symfit.core.support.RequiredKeywordError[source]

Bases: exceptions.Exception

Error raised in case a keyword-only argument is not treated as such.

symfit.core.support.cache(func)[source]

Decorator function that gets a method as its input and either buffers the input, or returns the buffered output. Used in conjunction with properties to take away the standard buffering logic.

Parameters:func
Returns:
class symfit.core.support.deprecated(replacement=None)[source]

Bases: object

Decorator to raise a DeprecationWarning.

__init__(replacement=None)[source]
Parameters:replacement – The function which should now be used instead.
symfit.core.support.jacobian(expr, symbols)[source]

Derive a symbolic expr w.r.t. each symbol in symbols. This returns a symbolic jacobian vector.

Parameters:
  • expr – A sympy Expr.
  • symbols – The symbols w.r.t. which to derive.
symfit.core.support.key2str(target)[source]

In symfit there are many dicts with symbol: value pairs. These can not be used immediately as **kwargs, even though this would make a lot of sense from the context. This function wraps such dict to make them usable as **kwargs immidiately.

Parameters:target – dict to be made save
Returns:dict of str(symbol): value pairs.
class symfit.core.support.keywordonly(**kwonly_arguments)[source]

Bases: object

Decorator class which wraps a python 2 function into one with keyword-only arguments.

Example:

@keywordonly(floor=True)
def f(x, **kwargs):
    floor = kwargs.pop('floor')
    return np.floor(x**2) if floor else x**2

This decorator is not much more than:

floor = kwargs.pop('floor') if 'floor' in kwargs else True

However, I prefer it’s usage because:

  • it’s clear from reading the function declaration there is an option to provide this argument. The information on possible keywords is where you’d expect it to be.
  • you’re guaranteed that the pop works.
  • Perhaps in the future I will make this inspect-compatible so then we come full circle.

Please note that this decorator needs a ** argument on the wrapped function in order to work.

__call__(func)[source]

Returns a decorated version of func, who’s signature now includes the keyword-only arguments.

Parameters:func – the function to be decorated
Returns:the decorated function
symfit.core.support.parameters(names)[source]

Convenience function for the creation of multiple parameters.

Parameters:names – string of parameter names. Should be comma seperated. Example: a, b = parameters(‘a, b’)
symfit.core.support.seperate_symbols(func)[source]

Seperate the symbols in symbolic function func. Return them in alphabetical order.

Parameters:func – scipy symbolic function.
Returns:(vars, params), a tuple of all variables and parameters, each sorted in alphabetical order.
Raises:TypeError – only symfit Variable and Parameter are allowed, not sympy Symbols.
symfit.core.support.sympy_to_py(func, vars, params)[source]

Turn a symbolic expression into a Python lambda function, which has the names of the variables and parameters as it’s argument names.

Parameters:
  • func – sympy expression
  • vars – variables in this model
  • params – parameters in this model
Returns:

lambda function to be used for numerical evaluation of the model. Ordering of the arguments will be vars first, then params.

symfit.core.support.sympy_to_scipy(func, vars, params)[source]

Convert a symbolic expression to one scipy digs. Not used by symfit any more.

Parameters:
  • func – sympy expression
  • vars – variables
  • params – parameters
Returns:

Scipy-style function to be used for numerical evaluation of the model.

symfit.core.support.variables(names)[source]

Convenience function for the creation of multiple variables.

Parameters:names – string of variable names. Should be comma seperated. Example: x, y = variables(‘x, y’)

Distributions

Some common distributions are defined in this module. That way, users can easily build more complicated expressions without making them look hard.

I have deliberately chosen to start these function with a capital, e.g. Gaussian instead of gaussian, because this makes the resulting expressions more readable.

symfit.distributions.Exp(x, l)[source]

Exponential Distribution pdf. :param x: free variable. :param l: rate parameter. :return: sympy.Expr for an Exponential Distribution pdf.

symfit.distributions.Gaussian(x, mu, sig)[source]

Gaussian pdf. :param x: free variable. :param mu: mean of the distribution. :param sig: standard deviation of the distribution. :return: sympy.Expr for a Gaussian pdf.

Contrib

Contrib modules are modules and extensions to symfit provided by other people. This usually means the code is of slightly less quality, and may not survive future versions.

class symfit.contrib.interactive_guess.interactive_guess.InteractiveGuess2D(*args, **kwargs)[source]

Bases: symfit.core.fit.TakesData

A class that provides an graphical, interactive way of guessing initial fitting parameters.

__init__(*args, **kwargs)[source]

Create a matplotlib window with sliders for all parameters in this model, so that you may graphically guess initial fitting parameters. n_points is the number of points drawn for the plot. Data points are plotted as blue points, the proposed model as a red line.

Slider extremes are taken from the parameters where possible. If these are not provided, the minimum is 0; and the maximum is value*2. If no initial value is provided, it defaults to 1.

This will modify the values of the parameters present in model.

Parameters:n_points (int) – The number of points used for drawing the fitted function.
__str__()[source]

Represent the guesses in a human readable way.

Returns:string with the guessed values.
execute(*args, **kwargs)[source]

Execute the interactive guessing procedure.

Parameters:
  • show (bool) – Whether or not to show the figure. Useful for testing.
  • block – Blocking call to matplotlib