Orthogonal distance regression (scipy.odr)#
Package Content#
| The data to fit. |
| The data, with weightings as actual standard deviations and/or covariances. |
| The Model class stores information about the function you wish to fit. |
| The ODR class gathers all information and coordinates the running of the main fitting routine. |
| The Output class stores the output of an ODR run. |
| Low-level function for ODR. |
Warning indicating that the data passed into ODR will cause problems when passed into 'odr' that the user should be aware of. | |
Exception indicating an error in fitting. | |
Exception stopping fitting. | |
| Factory function for a general polynomial model. |
Exponential model | |
Arbitrary-dimensional linear model | |
Univariate linear model | |
Quadratic model |
Usage information#
Introduction#
Why Orthogonal Distance Regression (ODR)? Sometimes one hasmeasurement errors in the explanatory (a.k.a., “independent”)variable(s), not just the response (a.k.a., “dependent”) variable(s).Ordinary Least Squares (OLS) fitting procedures treat the data forexplanatory variables as fixed, i.e., not subject to error of any kind.Furthermore, OLS procedures require that the response variables be anexplicit function of the explanatory variables; sometimes making theequation explicit is impractical and/or introduces errors. ODR canhandle both of these cases with ease, and can even reduce to the OLScase if that is sufficient for the problem.
ODRPACK is a FORTRAN-77 library for performing ODR with possiblynon-linear fitting functions. It uses a modified trust-regionLevenberg-Marquardt-type algorithm[1] to estimate the functionparameters. The fitting functions are provided by Python functionsoperating on NumPy arrays. The required derivatives may be providedby Python functions as well, or may be estimated numerically. ODRPACKcan do explicit or implicit ODR fits, or it can do OLS. Input andoutput variables may be multidimensional. Weights can be provided toaccount for different variances of the observations, and evencovariances between dimensions of the variables.
Thescipy.odr package offers an object-oriented interface toODRPACK, in addition to the low-levelodr function.
Additional background information about ODRPACK can be found in theODRPACK User’s Guide, readingwhich is recommended.
Basic usage#
Define the function you want to fit against.:
deff(B,x):'''Linear function y = m*x + b'''# B is a vector of the parameters.# x is an array of the current x values.# x is in the same format as the x passed to Data or RealData.## Return an array in the same format as y passed to Data or RealData.returnB[0]*x+B[1]
Create a Model.:
linear=Model(f)
Create a Data or RealData instance.:
mydata=Data(x,y,wd=1./power(sx,2),we=1./power(sy,2))
or, when the actual covariances are known:
mydata=RealData(x,y,sx=sx,sy=sy)
Instantiate ODR with your data, model and initial parameter estimate.:
myodr=ODR(mydata,linear,beta0=[1.,2.])
Run the fit.:
myoutput=myodr.run()
Examine output.:
myoutput.pprint()
References#
P. T. Boggs and J. E. Rogers, “Orthogonal Distance Regression,”in “Statistical analysis of measurement error models andapplications: proceedings of the AMS-IMS-SIAM joint summer researchconference held June 10-16, 1989,” Contemporary Mathematics,vol. 112, pg. 186, 1990.