Why Orthogonal Distance Regression (ODR)? Sometimes one has measurement 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 for explanatory variables as fixed, i.e., not subject to error of any kind. Furthermore, OLS procedures require that the response variables be an explicit function of the explanatory variables; sometimes making the equation explicit is impractical and/or introduces errors. ODR can handle both of these cases with ease, and can even reduce to the OLS case if that is sufficient for the problem.
ODRPACK is a FORTRAN-77 library for performing ODR with possibly non-linear fitting functions. It uses a modified trust-region Levenberg-Marquardt-type algorithm [R156] to estimate the function parameters. The fitting functions are provided by Python functions operating on NumPy arrays. The required derivatives may be provided by Python functions as well, or may be estimated numerically. ODRPACK can do explicit or implicit ODR fits, or it can do OLS. Input and output variables may be multi-dimensional. Weights can be provided to account for different variances of the observations, and even covariances between dimensions of the variables.
odr provides two interfaces: a single function, and a set of high-level classes that wrap that function; please refer to their docstrings for more information. While the docstring of the function odr does not have a full explanation of its arguments, the classes do, and arguments of the same name usually have the same requirements. Furthermore, the user is urged to at least skim the ODRPACK User’s Guide - “Know Thy Algorithm.”
See the docstrings of odr.odrpack and the functions and classes for usage instructions. The ODRPACK User’s Guide (linked above) is also quite helpful.
[R156] | P. T. Boggs and J. E. Rogers, “Orthogonal Distance Regression,” in “Statistical analysis of measurement error models and applications: proceedings of the AMS-IMS-SIAM joint summer research conference held June 10-16, 1989,” Contemporary Mathematics, vol. 112, pg. 186, 1990. |
odrpack | Python wrappers for Orthogonal Distance Regression (ODRPACK). |
models | Collection of Model instances for use with the odrpack fitting package. |
ODR(data, model[, beta0, delta0, ifixb, ...]) | The ODR class gathers all information and coordinates the running of the |
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. |
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 |
odr_error | |
odr_stop |
The Data class stores the data to fit.
Each argument is attached to the member of the instance of the same name. The structures of x and y are described in the Model class docstring. If y is an integer, then the Data instance can only be used to fit with implicit models where the dimensionality of the response is equal to the specified value of y. The structures of wd and we are described below. meta is an freeform dictionary for application-specific use.
we weights the effect a deviation in the response variable has on the fit. wd weights the effect a deviation in the input variable has on the fit. To handle multidimensional inputs and responses easily, the structure of these arguments has the n’th dimensional axis first. These arguments heavily use the structured arguments feature of ODRPACK to conveniently and flexibly support all options. See the ODRPACK User’s Guide for a full explanation of how these weights are used in the algorithm. Basically, a higher value of the weight for a particular data point makes a deviation at that point more detrimental to the fit.
- we – if we is a scalar, then that value is used for all data points (and
all dimensions of the response variable).
If we is a rank-1 array of length q (the dimensionality of the response variable), then this vector is the diagonal of the covariant weighting matrix for all data points.
If we is a rank-1 array of length n (the number of data points), then the i’th element is the weight for the i’th response variable observation (single-dimensional only).
If we is a rank-2 array of shape (q, q), then this is the full covariant weighting matrix broadcast to each observation.
If we is a rank-2 array of shape (q, n), then we[:,i] is the diagonal of the covariant weighting matrix for the i’th observation.
If we is a rank-3 array of shape (q, q, n), then we[:,:,i] is the full specification of the covariant weighting matrix for each observation.
If the fit is implicit, then only a positive scalar value is used.
- wd – if wd is a scalar, then that value is used for all data points
(and all dimensions of the input variable). If wd = 0, then the covariant weighting matrix for each observation is set to the identity matrix (so each dimension of each observation has the same weight).
If wd is a rank-1 array of length m (the dimensionality of the input variable), then this vector is the diagonal of the covariant weighting matrix for all data points.
If wd is a rank-1 array of length n (the number of data points), then the i’th element is the weight for the i’th input variable observation (single-dimensional only).
If wd is a rank-2 array of shape (m, m), then this is the full covariant weighting matrix broadcast to each observation.
If wd is a rank-2 array of shape (m, n), then wd[:,i] is the diagonal of the covariant weighting matrix for the i’th observation.
If wd is a rank-3 array of shape (m, m, n), then wd[:,:,i] is the full specification of the covariant weighting matrix for each observation.
- fix – fix is the same as ifixx in the class ODR. It is an array of integers
- with the same shape as data.x that determines which input observations are treated as fixed. One can use a sequence of length m (the dimensionality of the input observations) to fix some dimensions for all observations. A value of 0 fixes the observation, a value > 0 makes it free.
meta – optional, freeform dictionary for metadata
Methods
set_meta |
Update the metadata dictionary with the keywords and data provided by keywords.
The Model class stores information about the function you wish to fit.
It stores the function itself, at the least, and optionally stores functions which compute the Jacobians used during fitting. Also, one can provide a function that will provide reasonable starting values for the fit parameters possibly given the set of data.
The initialization method stores these into members of the same name.
- fcn – fit function
- fcn(beta, x) –> y
- fjacb – Jacobian of fcn wrt the fit parameters beta
- fjacb(beta, x) –> @f_i(x,B)/@B_j
- fjacd – Jacobian of fcn wrt the (possibly multidimensional) input
- variable fjacd(beta, x) –> @f_i(x,B)/@x_j
- extra_args – if specified, extra_args should be a tuple of extra
- arguments to pass to fcn, fjacb, and fjacd. Each will be called like the following: apply(fcn, (beta, x) + extra_args)
- estimate – provide estimates of the fit parameters from the data:
- estimate(data) –> estbeta
- implicit – boolean variable which, if TRUE, specifies that the model
- is implicit; i.e fcn(beta, x) ~= 0 and there is no y data to fit against
- meta – optional
- freeform dictionary of metadata for the model
Note that the fcn, fjacb, and fjacd operate on NumPy arrays and return a NumPy array. The estimate object takes an instance of the Data class.
Here are the rules for the shapes of the argument and return arrays:
- x – if the input data is single-dimensional, then x is rank-1
- array; i.e. x = array([1, 2, 3, ...]); x.shape = (n,) If the input data is multi-dimensional, then x is a rank-2 array; i.e., x = array([[1, 2, ...], [2, 4, ...]]); x.shape = (m, n) In all cases, it has the same shape as the input data array passed to odr(). m is the dimensionality of the input data, n is the number of observations.
- y – if the response variable is single-dimensional, then y is a
- rank-1 array, i.e., y = array([2, 4, ...]); y.shape = (n,) If the response variable is multi-dimensional, then y is a rank-2 array, i.e., y = array([[2, 4, ...], [3, 6, ...]]); y.shape = (q, n) where q is the dimensionality of the response variable.
- beta – rank-1 array of length p where p is the number of parameters;
- i.e. beta = array([B_1, B_2, ..., B_p])
- fjacb – if the response variable is multi-dimensional, then the
- return array’s shape is (q, p, n) such that fjacb(x,beta)[l,k,i] = @f_l(X,B)/@B_k evaluated at the i’th data point. If q == 1, then the return array is only rank-2 and with shape (p, n).
- fjacd – as with fjacb, only the return array’s shape is (q, m, n)
- such that fjacd(x,beta)[l,j,i] = @f_l(X,B)/@X_j at the i’th data point. If q == 1, then the return array’s shape is (m, n). If m == 1, the shape is (q, n). If m == q == 1, the shape is (n,).
Methods
set_meta |
Update the metadata dictionary with the keywords and data provided here.
The ODR class gathers all information and coordinates the running of the main fitting routine.
Members of instances of the ODR class have the same names as the arguments to the initialization routine.
Parameters : | data: :
model: :
beta0: :
delta0: optional :
ifixb: optional :
ifixx: optional :
job: optional :
iprint: optional :
errfile: optional :
rptfile: optional :
ndigit: optional :
taufac: optional :
sstol: optional :
partol: optional :
maxit: optional :
stpb: optional :
stpd: optional :
sclb: optional :
scld: optional :
work: optional :
iwork: optional :
output: :
|
---|
Methods
restart | |
run | |
set_iprint | |
set_job |
Restarts the run with iter more iterations.
Parameters : | iter : int, optional
|
---|---|
Returns : | output : Output instance
|
Run the fitting routine with all of the information given.
Returns : | output : Output instance
|
---|
Set the iprint parameter for the printing of computation reports.
If any of the arguments are specified here, then they are set in the iprint member. If iprint is not set manually or with this method, then ODRPACK defaults to no printing. If no filename is specified with the member rptfile, then ODRPACK prints to stdout. One can tell ODRPACK to print to stdout in addition to the specified filename by setting the so_* arguments to this function, but one cannot specify to print to stdout but not a file since one can do that by not specifying a rptfile filename.
There are three reports: initialization, iteration, and final reports. They are represented by the arguments init, iter, and final respectively. The permissible values are 0, 1, and 2 representing “no report”, “short report”, and “long report” respectively.
The argument iter_step (0 <= iter_step <= 9) specifies how often to make the iteration report; the report will be made for every iter_step’th iteration starting with iteration one. If iter_step == 0, then no iteration report is made, regardless of the other arguments.
If the rptfile is None, then any so_* arguments supplied will raise an exception.
Sets the “job” parameter is a hopefully comprehensible way.
If an argument is not specified, then the value is left as is. The default value from class initialization is for all of these options set to 0.
Parameters : | fit_type : {0, 1, 2} int
deriv : {0, 1, 2, 3} int
var_calc : {0, 1, 2} int
del_init : {0, 1} int
restart : {0, 1} int
|
---|
Notes
The permissible values are different from those given on pg. 31 of the ODRPACK User’s Guide only in that one cannot specify numbers greater than the last value for each variable.
If one does not supply functions to compute the Jacobians, the fitting procedure will change deriv to 0, finite differences, as a default. To initialize the input variable offsets by yourself, set del_init to 1 and put the offsets into the “work” variable correctly.
The Output class stores the output of an ODR run.
Takes one argument for initialization, the return value from the function odr.
Notes
The attributes listed as “optional” above are only present if odr was run with full_output=1.
Attributes
beta | ndarray | Estimated parameter values, of shape (q,). |
sd_beta | ndarray | Standard errors of the estimated parameters, of shape (p,). |
cov_beta | ndarray | Covariance matrix of the estimated parameters, of shape (p,p). |
delta | ndarray, optional | Array of estimated errors in input variables, of same shape as x. |
eps | ndarray, optional | Array of estimated errors in response variables, of same shape as y. |
xplus | ndarray, optional | Array of x + delta. |
y | ndarray, optional | Array y = fcn(x + delta). |
res_var | float, optional | Residual variance. |
sum_sqare | float, optional | Sum of squares error. |
sum_square_delta | float, optional | Sum of squares of delta error. |
sum_square_eps | float, optional | Sum of squares of eps error. |
inv_condnum | float, optional | Inverse condition number (cf. ODRPACK UG p. 77). |
rel_error | float, optional | Relative error in function values computed within fcn. |
work | ndarray, optional | Final work array. |
work_ind | dict, optional | Indices into work for drawing out values (cf. ODRPACK UG p. 83). |
info | int, optional | Reason for returning, as output by ODRPACK (cf. ODRPACK UG p. 38). |
stopreason | list of str, optional | info interpreted into English. |
Methods
pprint | Support to pretty-print lists, tuples, & dictionaries recursively. |
Pretty-print important results.