Skip to content


Repository files navigation

MPFITEXY: Straight line fitting with IDL and MPFIT

MPFITEXY is a wrapper around Craig Markwardt’s MPFIT. It finds the best-fitting straight line through data with errors in both coordinates. In the absence of intrinsic scatter it minimizes the same figure of merit at the ASTROIDL FITEXY routine, which is itself an implementation of the well known Numerical Recipes approach to fitting a straight line to data with errors in both coordinates.

MPFITEXY adds a number of extra features not supported by FITEXY:

  • User-specified intrinsic scatter, i.e. the weighting for each data point is of the form (σx2 + β2σy2 + σint2). Intrinsic scatter is physically significant and statistically necessary in situations where, even with perfect data (i.e. no observational errors), the data is scattered about the model. This is the “Nukers’ Estimate” described in Tremaine et al. (2002).

  • σint is automatically adjusted to ensure χ2/(degrees or freedom) ≈ 1 (if the optional /reduce keyword is enabled). This is done using the iterative procedure described in Section 3.2.1 of Bedregal, Aragón-Salamanca & Merrifield (2006).

  • The inverse relationship is optionally fitted, i.e. y is regressed onto x, if the /inv keyword is enabled. You can of course do this in FITEXY by swapping round the variables in the procedure call, but MPFITEXY handles the inversion of the results and the propagation of errors. Inverse fitting is therefore completely transparent to the user. See, e.g. Willick (1994) for the background to why you might want to fit the inverse relationship.

  • You can fix the slope or the intercept using the /fixslope and /fixint keywords.

Installation and use

  1. Download and install MPFIT

  2. Place the files and/or in your IDL path

Calling sequence, usage, etc. is described in each file. These routines have been tested with a relatively narrow set of data, so they may contain bugs or be missing crucial features. Please report problems via Github.


This code merely minimizes a figure of merit, i.e. it solves an engineering problem. This figure of merit happens to be the maximum likelihood estimate of a linear model. This model assumes that data follows a linear model with normally distributed observational noise and intrinsic scatter. You should consider whether this figure of merit is an justified objective function that measures the quality of a model in your scientific or statistical context. For more on this, please read Hogg, Bovy & Lang (2010).


The features of the code are discussed, and relevant literature cited, in Section 4 of Williams, Bureau & Cappellari (2010). In the interests of reproducibility, you should either cite that publication (which I would prefer) or refer to the permanent URL at which MPFITEXY can always be found: MPFITEXY is dependent on the MPFIT package, which you must separately acknowledge by citing Markwardt (2009). So, you could cite MPFITEXY like this:

We fit a linear relationship to the data using the MPFITEXY routine (Williams, Bureau & Cappellari 2010). The MPFITEXY routine depends on the MPFIT package (Markwardt 2009).

or like this

We fit a linear relationship to the data using the MPFITEXY routine.1 The MPFITEXY routine depends on the MPFIT package (Markwardt 2009).




MPFITEXY2 simultaneously fits straight lines to two sets of data with a shared slope. It is useful for measuring the offset between the straight line fits to two samples, while simultaneously using as much information as possible to constrain their shared slope and zero-point.

The data can have errors in the x and y directions, and intrinsic scatter. MPFITEXY2 supports all the convenient features supported by MPFITEXY (optimization of σint, inverse relationship fitting, fixed slope or intercept – see above).

MPFITEXY2 is an implementation of the minimization problem described in Section 4 of Williams, Bureau & Cappellari (2010). The details of your data will affect whether the assumptions we make are justified. I urge you to read that short section before depending on the results of MPFITEXY2.


Linear regression with IDL and MPFIT