Python wrappers for Orthogonal Distance Regression (ODRPACK).


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


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


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))


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 =

6) Examine output.


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


  • 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/ 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 . Contributions are welcome.


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

Robert Kern


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


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



Table Of Contents

Next topic


This Page