This is documentation for an old release of SciPy (version 0.9.0). Read this page in the documentation of the latest stable release (version 1.15.1).

scipy.odr.odrpack

Python wrappers for Orthogonal Distance Regression (ODRPACK).

Classes

Data – stores the data and weights to fit against

RealData – stores data with standard deviations and covariance matrices

Model – stores the model and its related information

Output – stores all of the output from an ODR run

ODR – collects all data and runs the fitting routine

Exceptions

odr_error – error sometimes raised inside odr() and can be raised in the
fitting functions to tell ODRPACK to halt the procedure
odr_stop – error to raise in fitting functions to tell ODRPACK that the data or
parameters given are invalid

Use

Basic use:

1) Define the function you want to fit against.

def f(B, x):
    ''' Linear function y = m*x + b '''
    return B[0]*x + B[1]

    # B is a vector of the parameters.
    # x is an array of the current x values.
    # x is same format as the x passed to Data or RealData.

    # Return an array in the same format as y passed to Data or RealData.

2) Create a Model.

linear = Model(f)

3) Create a Data or RealData instance.

mydata = Data(x, y, wd=1./power(sx,2), we=1./power(sy,2))

or

mydata = RealData(x, y, sx=sx, sy=sy)

4) Instantiate ODR with your data, model and initial parameter estimate.

myodr = ODR(mydata, linear, beta0=[1., 2.])

5) Run the fit.

myoutput = myodr.run()

6) Examine output.

myoutput.pprint()

Read the docstrings and the accompanying tests for more advanced usage.

Notes

  • Array formats – FORTRAN stores its arrays in memory column first, i.e. an array element A(i, j, k) will be next to A(i+1, j, k). In C and, consequently, NumPy, arrays are stored row first: A[i, j, k] is next to A[i, j, k+1]. For efficiency and convenience, the input and output arrays of the fitting function (and its Jacobians) are passed to FORTRAN without transposition. Therefore, where the ODRPACK documentation says that the X array is of shape (N, M), it will be passed to the Python function as an array of shape (M, N). If M==1, the one-dimensional case, then nothing matters; if M>1, then your Python functions will be dealing with arrays that are indexed in reverse of the ODRPACK documentation. No real biggie, but watch out for your indexing of the Jacobians: the i,j’th elements (@f_i/@x_j) evaluated at the n’th observation will be returned as jacd[j, i, n]. Except for the Jacobians, it really is easier to deal with x[0] and x[1] than x[:,0] and x[:,1]. Of course, you can always use the transpose() function from scipy explicitly.
  • Examples – See the accompanying file test/test.py for examples of how to set up fits of your own. Some are taken from the User’s Guide; some are from other sources.
  • Models – Some common models are instantiated in the accompanying module models.py . Contributions are welcome.

Credits

  • Thanks to Arnold Moene and Gerard Vermeulen for fixing some killer bugs.

Robert Kern robert.kern@gmail.com

Functions

odr(fcn, beta0, y, x[, we, wd, fjacb, ...])
report_error(info) Interprets the return code of the odr routine.

Classes

Data(x[, y, we, wd, fix, meta]) The Data class stores the data to fit.
Model(fcn[, fjacb, fjacd, extra_args, ...]) The Model class stores information about the function you wish to fit.
ODR(data, model[, beta0, delta0, ifixb, ...]) The ODR class gathers all information and coordinates the running of the
Output(output) The Output class stores the output of an ODR run.
RealData(x[, y, sx, sy, covx, covy, fix, meta]) The RealData class stores the weightings as actual standard deviations

Exceptions

odr_error
odr_stop

Table Of Contents

Next topic

scipy.odr.models

This Page