2 @package MPCDAnalysis.Fit
4 Provides commonly used data fitting functionality.
9 Helps fitting data to arbitrary functions, and handling the results.
21 self, f, xData, yData, yErrs = None,
22 lowerBounds = None, upperBounds = None):
24 Fits the given data to the given fit function `f`.
26 After calling this, any previous fits with this instance, and the
27 corresponding fit results, are discarded.
30 Throws if any argument is of the wrong type.
32 Throws if `xData`, `yData`, and `yErrs` (if not `None`) have
35 Throws if `lowerBounds` or `upperBounds` has the wrong length,
39 The function to fit to; it must be supplied as a callable
40 object, taking, as its first parameter, an instance of
41 `numpy.ndarray` containing all the independent variable
42 values to evaluate the fit function for. This callable must
43 then return an instance of `numpy.ndarray` containing, in the
44 same order, the fit function values corresponding to the
45 elements in the first argument.
46 Any further arguments this callable takes are assumed to be
47 fit parameters that will be optimized for.
49 An iterable containing the independent variable values to fit
52 An iterable containing the target values, which the fit
53 should approach as much as possible. The order must
54 correspond to `xData`.
56 An iterable containing, in an order corresponding to `yData`,
57 the uncertainties (one standard deviation error) on the
59 @param[in] lowerBounds
60 Either `None` to have no lower bounds on the fitting
61 parameters, or a `list`, the length of which corresponds to
62 the number of fitting parameters, where each value represents
63 the lower bound for the corresponding fitting parameter.
64 Values of `None` in this list correspond to no bound for that
66 @param[in] upperBounds
67 As in `lowerBounds`, except that upper bounds are specified.
73 if len(xData) != len(yData):
76 if len(yErrs) != len(yData):
79 for x
in [lowerBounds, upperBounds]:
82 if not isinstance(x, list):
84 if len(x) != getNumberOfArgumentsFromCallable(f) - 1:
90 if isinstance(xData, numpy.ndarray):
94 xValues = numpy.array(xData)
96 xValues = numpy.array([])
98 xValues = numpy.append(xValues, [val])
100 if isinstance(yData, numpy.ndarray):
104 yValues = numpy.array(yData)
106 yValues = numpy.array([])
108 yValues = numpy.append(yValues, [val])
112 absoluteErrors =
False
114 absoluteErrors =
True
116 if isinstance(yErrs, numpy.ndarray):
120 yErrors = numpy.array(yErrors)
122 yErrors = numpy.array([])
124 yErrors = numpy.append(yErrors, [val])
127 bounds = (-numpy.inf, numpy.inf)
129 if lowerBounds
is not None:
131 for b
in lowerBounds:
135 bounds = (_proper, bounds[1])
137 if upperBounds
is not None:
139 for b
in upperBounds:
143 bounds = (bounds[0], _proper)
147 with warnings.catch_warnings():
148 warnings.simplefilter(
"ignore", scipy.optimize.OptimizeWarning)
151 scipy.optimize.curve_fit(
156 absolute_sigma = absoluteErrors,
162 Returns the number of fit parameters used in the last successful call
166 Throws if `fit` has not been called successfully previously.
177 Returns the parameter estimate for the parameter with the given `index`.
180 Throws if `fit` has not been called successfully previously.
182 Throws if any argument is of the wrong type.
184 Throws if `index` is out of bounds.
187 The index of the fit parameter, which must be a non-negative
188 `int` that is smaller than `getFitParameterCount()`.
195 if not isinstance(index, int):
206 Returns the variance of the parameter estimate for the parameter with
207 the given `index`, if such a variance is available, or `numpy.inf` if no
208 variance estimate was possible.
211 Throws if `fit` has not been called successfully previously.
213 Throws if any argument is of the wrong type.
215 Throws if `index` is out of bounds.
218 The index of the fit parameter, which must be a non-negative
219 `int` that is smaller than `getFitParameterCount()`.
226 if not isinstance(index, int):
237 Returns the standard deviation of the parameter estimate for the
238 parameter with the given `index`, if such a standard deviation is
239 available, or `numpy.inf` if no standard deviation estimate was
243 Throws if `fit` has not been called successfully previously.
245 Throws if any argument is of the wrong type.
247 Throws if `index` is out of bounds.
250 The index of the fit parameter, which must be a non-negative
251 `int` that is smaller than `getFitParameterCount()`.
260 Returns the covariance of the parameter estimates for the parameter with
261 the given indices, if such a covariance is available, or `numpy.inf` if
262 no coviariance estimate was possible.
265 Throws if `fit` has not been called successfully previously.
267 Throws if any argument is of the wrong type.
269 Throws if either `index1` or `index2` is out of bounds.
272 The index of one of the fit parameters, which must be a
273 non-negative `int` that is smaller than
274 `getFitParameterCount()`.
276 The index of one of the fit parameters, which must be a
277 non-negative `int` that is smaller than
278 `getFitParameterCount()`.
284 if not isinstance(index1, int):
286 if not isinstance(index2, int):
300 Independently fits multiple sets of data to the given fit function `f`.
303 Throws if any argument is of the wrong type.
305 Throws if any argument has an invalid value.
308 The function to fit to; it must satisfy the conditions for
309 the `f` argument to `fit`.
311 An iterable, with elements being lists of length `3`; the
312 list elements will be passed as the `xData`, `yData`, and
313 `yErrs` parameters to `fit`, and accordingly need to satisfy
314 the conditions there.
316 @return Returns a list of dictionaries, in the order that corresponds
317 to the order in `dataSet`; each dictionary contains the
319 - `data`, the list returned while iterating `dataSet`
320 - `fit`, an instance of this class, on which `fit` has been
321 called with the given `f` and the `data` elements, as
322 described in the documentation for the `dataSet` parameter.
329 fit.fit(f, data[0], data[1], data[2])
330 ret.append({
'data': data,
'fit': fit})
338 Independently fits multiple sets of data to the given fit function `f`,
339 and returns the best fit, as determined by `comparator`.
341 This function returns the equivalent to the result of the following
343 First, let `results = multipleIndependentDataFits(f, dataSet)`.
344 Then, if `results` is empty, return `None`. Otherweise, start by calling
345 the first result in the list the `best` result, and iterate through all
346 other results, with iteration variable `currentResult`. For each
347 iteration, call `comparator(best, currentResult)`; if it returns `True`,
348 let `best = currentResult`.
349 After having completed all iterations, return `best`.
352 Throws if any argument is of the wrong type.
354 Throws if any argument has an invalid value.
357 The function to fit to; it must satisfy the conditions for
358 the `f` argument to `fit`.
360 An iterable, with elements being lists of length `3`; the
361 list elements will be passed as the `xData`, `yData`, and
362 `yErrs` parameters to `fit`, and accordingly need to satisfy
363 the conditions there.
364 @param[in] comparator
365 The callable used to compare two fits; see the main body
366 of this function documentation for details.
369 results = Fit.multipleIndependentDataFits(f, dataSet)
375 for result
in results:
380 if comparator(best, result):
388 Returns whether this instance and `rhs` have the same state.
391 Throws if any argument is of the wrong type.
394 The right-hand-side instance of this class.
397 if not isinstance(rhs, self.__class__):
400 if len(self.__dict__) != len(rhs.__dict__):
404 for k, v
in self.__dict__.items():
405 if k
not in rhs.__dict__:
408 if k ==
"_fitResults":
410 if rhs.__dict__[k]
is not None:
413 if len(v) != len(rhs.__dict__[k]):
416 for x, y
in enumerate(v):
417 if numpy.any(y != rhs.__dict__[k][x]):
421 if v != rhs.__dict__[k]:
429 Returns the negation of `self == rhs`.
432 Throws if any argument is of the wrong type.
435 The right-hand-side instance of this class.
438 return not self == rhs