Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Linear Regression Using Python
Theory
Suppose that you want to fit a set data points , where , to a straight line, . The process of determining the best-fit line is called linear regression. This involves choosing the parameters and to minimize the sum of the squares of the differences between the data points and the linear function. How the difference are defined varies. If there are only uncertainties in the y direction, then the differences in the vertical direction (the gray lines in the figure below) are used. If there are uncertainties in both the and directions, the orthogonal (perpendicular) distances from the line (the dotted red lines in the figure below) are used.
For the case where there are only uncertainties in the y direction, there is an analytical solution to the problem. If the uncertainty in is , then the difference squared for each point is weighted by . If there are no uncertainties, each point is given an equal weight of one. The function to be minimized with respect to variations in the parameters, and , is
The analytical solutions for the best-fit parameters that minimize (see pp. 181-189 of An Introduction to Error Analysis: The Study of Uncertainties in Physical Measurements by John R. Taylor, for example) are
and The uncertainties in the parameters are
and All of the sums in the four previous equations are over from 1 to .
For the case where there are uncertainties in both and , there is no analytical solution. One method that can be used in that case is called orthogonal distance regression (ODR).
Implementation in Python
The linear_fit function that performs these calculations is defined in the file "fitting.py" which must be located in the same directory as the Python program using it. If there are no uncertainties or only uncertainties in the direction, the analytical expressions above are used. If there are uncertainties in both the and directions, the scipy.odr module is used.
An example of performing a linear fit with uncertainties in the direction is shown below. The first command imports the function. Arrays containing the data points ( and ) are sent to the function. If only one array of uncertainties (called in the example) is sent, they are assumed to be in the direction. In the example, the array function (from the pylab library) is used to turn lists into arrays. It is also possible to read data from a file. The fitting function returns the best-fit parameters (called and in the example), their uncertainties (called and in the example), the reduced chi squared, and the degrees of freedom (called and in the example). The last two quantities are defined in the next section.
An example of performing a linear fit with uncertainties in both the and direction is shown below. Arrays containing the data points ( and ) and their uncertainties ( and ) are sent to the function. Note the order of the uncertainties! The uncertainty in is optional, so it is second. This is also consistent with the errorbar function (see below).
Intrepeting the Results
Plotting data with error bars and a best-fit line together gives a rough idea of whether or not the fit is good. If the line passes within most of the error bars, the fit is probably pretty good. The first line below makes a list of 100 points between the minimum and maximum values of in the data. The second line finds the value of for the best fit line.
When plotting the data with errorbars in both directions, note that the array of uncertainties in the direction () comes before the array of uncertainties in the direction () in the errorbar function. In this case, points were not plotted for the data because they would hide the smallest error bars.
The reduced chi squared and the degrees of freedom can also be used to judge the goodness of the fit. If is the number of data points and is the number of parameters (or constraints) in the fit, the number degrees of freedom is
For a linear fit, because there two parameters for a line. The reduced chi squared is defined as
According to Taylor (p. 271), “If we obtain a value of of order one or less, then we have no reason to doubt our expected distribution; if we obtain a value of much larger than one, our expected distribution is unlikely to be correct.” For an observed value (from fitting data) of the reduced chi square (), you can look up the probability of randomly getting a larger with degrees of freedom on the table below (from Appendix D of Taylor’s book). A typical standard is to reject a fit if
In other words, if the reduced chi squared for a fit is unlikely to occur randomly, then the fit is not a good one. In the first example above, five data points are fit with a line and . Since , the table gives
and there is no reason to reject the fit.
From Error Analysis by John Taylor
Linearization
Some other functions with two parameters can be linearized so that a linear fit can be used. There are two common examples. First, if data is expected to fit an exponential function
then
Therefore, vs. can be fit with a line in order to determine and . Don’t forget that the uncertainty of is not the same as the uncertainty of . Also, the linear fit gives and its uncertainty, which is not the same as the uncertainty of . The uncertainties must be propagated correctly. Second, if data is expected to fit power law of the form
then
Note that the base 10 logarithm is typically used for power laws. In this case, vs. can be fit with a line. Don't forget that the uncertainties of and are not the same as the uncertainties of and . The power is the slope, so its uncertainty comes directly from the fit. However, the uncertainty of the from the y intercept is not the same as the uncertainty of . That uncertainty must be propagated.
Additional Documentation
More information is available at: https://docs.scipy.org/doc/scipy/reference/odr.html